Merge branch 'client-helper' into 'master'

Helper class to reduce client boilerplate

* Renamed BdfReader methods for consistency with BdfList/BdfDictionary
* Added readList() and readDictionary() methods to BdfReader
* Added ClientHelper to reduce boilerplate when converting messages and metadata to and from BDF
* Moved PrivateGroupFactory to the same package as ClientHelper


See merge request !114
This commit is contained in:
akwizgran
2016-02-29 14:37:45 +00:00
23 changed files with 552 additions and 126 deletions

View File

@@ -0,0 +1,258 @@
package org.briarproject.clients;
import com.google.inject.Inject;
import org.briarproject.api.FormatException;
import org.briarproject.api.clients.ClientHelper;
import org.briarproject.api.data.BdfDictionary;
import org.briarproject.api.data.BdfList;
import org.briarproject.api.data.BdfReader;
import org.briarproject.api.data.BdfReaderFactory;
import org.briarproject.api.data.BdfWriter;
import org.briarproject.api.data.BdfWriterFactory;
import org.briarproject.api.data.MetadataEncoder;
import org.briarproject.api.data.MetadataParser;
import org.briarproject.api.db.DatabaseComponent;
import org.briarproject.api.db.DbException;
import org.briarproject.api.db.Metadata;
import org.briarproject.api.db.Transaction;
import org.briarproject.api.sync.GroupId;
import org.briarproject.api.sync.Message;
import org.briarproject.api.sync.MessageFactory;
import org.briarproject.api.sync.MessageId;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
class ClientHelperImpl implements ClientHelper {
private final DatabaseComponent db;
private final MessageFactory messageFactory;
private final BdfReaderFactory bdfReaderFactory;
private final BdfWriterFactory bdfWriterFactory;
private final MetadataParser metadataParser;
private final MetadataEncoder metadataEncoder;
@Inject
ClientHelperImpl(DatabaseComponent db, MessageFactory messageFactory,
BdfReaderFactory bdfReaderFactory,
BdfWriterFactory bdfWriterFactory, MetadataParser metadataParser,
MetadataEncoder metadataEncoder) {
this.db = db;
this.messageFactory = messageFactory;
this.bdfReaderFactory = bdfReaderFactory;
this.bdfWriterFactory = bdfWriterFactory;
this.metadataParser = metadataParser;
this.metadataEncoder = metadataEncoder;
}
@Override
public Message createMessage(GroupId g, long timestamp, BdfDictionary body)
throws FormatException {
ByteArrayOutputStream out = new ByteArrayOutputStream();
BdfWriter writer = bdfWriterFactory.createWriter(out);
try {
writer.writeDictionary(body);
} catch (FormatException e) {
throw e;
} catch (IOException e) {
// Shouldn't happen with ByteArrayOutputStream
throw new RuntimeException(e);
}
byte[] raw = out.toByteArray();
return messageFactory.createMessage(g, timestamp, raw);
}
@Override
public Message createMessage(GroupId g, long timestamp, BdfList body)
throws FormatException {
ByteArrayOutputStream out = new ByteArrayOutputStream();
BdfWriter writer = bdfWriterFactory.createWriter(out);
try {
writer.writeList(body);
} catch (FormatException e) {
throw e;
} catch (IOException e) {
// Shouldn't happen with ByteArrayOutputStream
throw new RuntimeException(e);
}
byte[] raw = out.toByteArray();
return messageFactory.createMessage(g, timestamp, raw);
}
@Override
public BdfDictionary getMessageAsDictionary(MessageId m) throws DbException,
FormatException {
BdfDictionary dictionary;
Transaction txn = db.startTransaction();
try {
dictionary = getMessageAsDictionary(txn, m);
txn.setComplete();
} finally {
db.endTransaction(txn);
}
return dictionary;
}
@Override
public BdfDictionary getMessageAsDictionary(Transaction txn, MessageId m)
throws DbException, FormatException {
byte[] raw = db.getRawMessage(txn, m);
if (raw == null) return null;
ByteArrayInputStream in = new ByteArrayInputStream(raw);
BdfReader reader = bdfReaderFactory.createReader(in);
BdfDictionary dictionary;
try {
dictionary = reader.readDictionary();
if (!reader.eof()) throw new FormatException();
} catch (FormatException e) {
throw e;
} catch (IOException e) {
// Shouldn't happen with ByteArrayInputStream
throw new RuntimeException(e);
}
return dictionary;
}
@Override
public BdfList getMessageAsList(MessageId m) throws DbException,
FormatException {
BdfList list;
Transaction txn = db.startTransaction();
try {
list = getMessageAsList(txn, m);
txn.setComplete();
} finally {
db.endTransaction(txn);
}
return list;
}
@Override
public BdfList getMessageAsList(Transaction txn, MessageId m)
throws DbException, FormatException {
byte[] raw = db.getRawMessage(txn, m);
if (raw == null) return null;
ByteArrayInputStream in = new ByteArrayInputStream(raw);
BdfReader reader = bdfReaderFactory.createReader(in);
BdfList list;
try {
list = reader.readList();
if (!reader.eof()) throw new FormatException();
} catch (FormatException e) {
throw e;
} catch (IOException e) {
// Shouldn't happen with ByteArrayInputStream
throw new RuntimeException(e);
}
return list;
}
@Override
public BdfDictionary getGroupMetadataAsDictionary(GroupId g)
throws DbException, FormatException {
BdfDictionary dictionary;
Transaction txn = db.startTransaction();
try {
dictionary = getGroupMetadataAsDictionary(txn, g);
txn.setComplete();
} finally {
db.endTransaction(txn);
}
return dictionary;
}
@Override
public BdfDictionary getGroupMetadataAsDictionary(Transaction txn,
GroupId g) throws DbException, FormatException {
Metadata metadata = db.getGroupMetadata(txn, g);
return metadataParser.parse(metadata);
}
@Override
public BdfDictionary getMessageMetadataAsDictionary(MessageId m)
throws DbException, FormatException {
BdfDictionary dictionary;
Transaction txn = db.startTransaction();
try {
dictionary = getMessageMetadataAsDictionary(txn, m);
txn.setComplete();
} finally {
db.endTransaction(txn);
}
return dictionary;
}
@Override
public BdfDictionary getMessageMetadataAsDictionary(Transaction txn,
MessageId m) throws DbException, FormatException {
Metadata metadata = db.getMessageMetadata(txn, m);
return metadataParser.parse(metadata);
}
@Override
public Map<MessageId, BdfDictionary> getMessageMetatataAsDictionary(
GroupId g) throws DbException, FormatException {
Map<MessageId, BdfDictionary> map;
Transaction txn = db.startTransaction();
try {
map = getMessageMetadataAsDictionary(txn, g);
txn.setComplete();
} finally {
db.endTransaction(txn);
}
return map;
}
@Override
public Map<MessageId, BdfDictionary> getMessageMetadataAsDictionary(
Transaction txn, GroupId g) throws DbException, FormatException {
Map<MessageId, Metadata> raw = db.getMessageMetadata(txn, g);
Map<MessageId, BdfDictionary> parsed =
new HashMap<MessageId, BdfDictionary>(raw.size());
for (Entry<MessageId, Metadata> e : raw.entrySet())
parsed.put(e.getKey(), metadataParser.parse(e.getValue()));
return Collections.unmodifiableMap(parsed);
}
@Override
public void mergeGroupMetadata(GroupId g, BdfDictionary metadata)
throws DbException, FormatException {
Transaction txn = db.startTransaction();
try {
mergeGroupMetadata(txn, g, metadata);
txn.setComplete();
} finally {
db.endTransaction(txn);
}
}
@Override
public void mergeGroupMetadata(Transaction txn, GroupId g,
BdfDictionary metadata) throws DbException, FormatException {
db.mergeGroupMetadata(txn, g, metadataEncoder.encode(metadata));
}
@Override
public void mergeMessageMetadata(MessageId m, BdfDictionary metadata)
throws DbException, FormatException {
Transaction txn = db.startTransaction();
try {
mergeMessageMetadata(txn, m, metadata);
txn.setComplete();
} finally {
db.endTransaction(txn);
}
}
@Override
public void mergeMessageMetadata(Transaction txn, MessageId m,
BdfDictionary metadata) throws DbException, FormatException {
db.mergeMessageMetadata(txn, m, metadataEncoder.encode(metadata));
}
}

View File

@@ -0,0 +1,15 @@
package org.briarproject.clients;
import com.google.inject.AbstractModule;
import org.briarproject.api.clients.ClientHelper;
import org.briarproject.api.clients.PrivateGroupFactory;
public class ClientsModule extends AbstractModule {
@Override
protected void configure() {
bind(ClientHelper.class).to(ClientHelperImpl.class);
bind(PrivateGroupFactory.class).to(PrivateGroupFactoryImpl.class);
}
}

View File

@@ -1,8 +1,9 @@
package org.briarproject.sync;
package org.briarproject.clients;
import com.google.inject.Inject;
import org.briarproject.api.Bytes;
import org.briarproject.api.clients.PrivateGroupFactory;
import org.briarproject.api.contact.Contact;
import org.briarproject.api.data.BdfWriter;
import org.briarproject.api.data.BdfWriterFactory;
@@ -10,7 +11,6 @@ import org.briarproject.api.identity.AuthorId;
import org.briarproject.api.sync.ClientId;
import org.briarproject.api.sync.Group;
import org.briarproject.api.sync.GroupFactory;
import org.briarproject.api.sync.PrivateGroupFactory;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

View File

@@ -1,11 +1,14 @@
package org.briarproject.data;
import org.briarproject.api.FormatException;
import org.briarproject.api.data.BdfDictionary;
import org.briarproject.api.data.BdfList;
import org.briarproject.api.data.BdfReader;
import java.io.IOException;
import java.io.InputStream;
import static org.briarproject.api.data.BdfDictionary.NULL_VALUE;
import static org.briarproject.data.Types.DICTIONARY;
import static org.briarproject.data.Types.END;
import static org.briarproject.data.Types.FALSE;
@@ -74,11 +77,26 @@ class BdfReaderImpl implements BdfReader {
}
}
private Object readObject() throws IOException {
if (hasNull()) {
readNull();
return NULL_VALUE;
}
if (hasBoolean()) return readBoolean();
if (hasLong()) return readLong();
if (hasDouble()) return readDouble();
if (hasString()) return readString(Integer.MAX_VALUE);
if (hasRaw()) return readRaw(Integer.MAX_VALUE);
if (hasList()) return readList();
if (hasDictionary()) return readDictionary();
throw new FormatException();
}
private void skipObject() throws IOException {
if (hasNull()) skipNull();
else if (hasBoolean()) skipBoolean();
else if (hasInteger()) skipInteger();
else if (hasFloat()) skipFloat();
else if (hasLong()) skipLong();
else if (hasDouble()) skipDouble();
else if (hasString()) skipString();
else if (hasRaw()) skipRaw();
else if (hasList()) skipList();
@@ -129,15 +147,15 @@ class BdfReaderImpl implements BdfReader {
hasLookahead = false;
}
public boolean hasInteger() throws IOException {
public boolean hasLong() throws IOException {
if (!hasLookahead) readLookahead();
if (eof) return false;
return next == INT_8 || next == INT_16 || next == INT_32 ||
next == INT_64;
}
public long readInteger() throws IOException {
if (!hasInteger()) throw new FormatException();
public long readLong() throws IOException {
if (!hasLong()) throw new FormatException();
hasLookahead = false;
if (next == INT_8) return readInt8();
if (next == INT_16) return readInt16();
@@ -169,8 +187,8 @@ class BdfReaderImpl implements BdfReader {
return value;
}
public void skipInteger() throws IOException {
if (!hasInteger()) throw new FormatException();
public void skipLong() throws IOException {
if (!hasLong()) throw new FormatException();
if (next == INT_8) skip(1);
else if (next == INT_16) skip(2);
else if (next == INT_32) skip(4);
@@ -178,14 +196,14 @@ class BdfReaderImpl implements BdfReader {
hasLookahead = false;
}
public boolean hasFloat() throws IOException {
public boolean hasDouble() throws IOException {
if (!hasLookahead) readLookahead();
if (eof) return false;
return next == FLOAT_64;
}
public double readFloat() throws IOException {
if (!hasFloat()) throw new FormatException();
public double readDouble() throws IOException {
if (!hasDouble()) throw new FormatException();
hasLookahead = false;
readIntoBuffer(8);
long value = 0;
@@ -193,8 +211,8 @@ class BdfReaderImpl implements BdfReader {
return Double.longBitsToDouble(value);
}
public void skipFloat() throws IOException {
if (!hasFloat()) throw new FormatException();
public void skipDouble() throws IOException {
if (!hasDouble()) throw new FormatException();
skip(8);
hasLookahead = false;
}
@@ -268,6 +286,15 @@ class BdfReaderImpl implements BdfReader {
return next == LIST;
}
public BdfList readList() throws IOException {
if (!hasList()) throw new FormatException();
BdfList list = new BdfList();
readListStart();
while (!hasListEnd()) list.add(readObject());
readListEnd();
return list;
}
public void readListStart() throws IOException {
if (!hasList()) throw new FormatException();
hasLookahead = false;
@@ -305,6 +332,16 @@ class BdfReaderImpl implements BdfReader {
return next == DICTIONARY;
}
public BdfDictionary readDictionary() throws IOException {
if (!hasDictionary()) throw new FormatException();
BdfDictionary dictionary = new BdfDictionary();
readDictionaryStart();
while (!hasDictionaryEnd())
dictionary.put(readString(Integer.MAX_VALUE), readObject());
readDictionaryEnd();
return dictionary;
}
public void readDictionaryStart() throws IOException {
if (!hasDictionary()) throw new FormatException();
hasLookahead = false;

View File

@@ -11,6 +11,7 @@ import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import static org.briarproject.api.data.BdfDictionary.NULL_VALUE;
import static org.briarproject.data.Types.DICTIONARY;
import static org.briarproject.data.Types.END;
import static org.briarproject.data.Types.FALSE;
@@ -55,7 +56,7 @@ class BdfWriterImpl implements BdfWriter {
else out.write(FALSE);
}
public void writeInteger(long i) throws IOException {
public void writeLong(long i) throws IOException {
if (i >= Byte.MIN_VALUE && i <= Byte.MAX_VALUE) {
out.write(INT_8);
out.write((byte) i);
@@ -94,7 +95,7 @@ class BdfWriterImpl implements BdfWriter {
out.write((byte) ((i << 56) >> 56));
}
public void writeFloat(double d) throws IOException {
public void writeDouble(double d) throws IOException {
out.write(FLOAT_64);
writeInt64(Double.doubleToRawLongBits(d));
}
@@ -135,14 +136,14 @@ class BdfWriterImpl implements BdfWriter {
}
private void writeObject(Object o) throws IOException {
if (o == null) writeNull();
if (o == null || o == NULL_VALUE) writeNull();
else if (o instanceof Boolean) writeBoolean((Boolean) o);
else if (o instanceof Byte) writeInteger((Byte) o);
else if (o instanceof Short) writeInteger((Short) o);
else if (o instanceof Integer) writeInteger((Integer) o);
else if (o instanceof Long) writeInteger((Long) o);
else if (o instanceof Float) writeFloat((Float) o);
else if (o instanceof Double) writeFloat((Double) o);
else if (o instanceof Byte) writeLong((Byte) o);
else if (o instanceof Short) writeLong((Short) o);
else if (o instanceof Integer) writeLong((Integer) o);
else if (o instanceof Long) writeLong((Long) o);
else if (o instanceof Float) writeDouble((Float) o);
else if (o instanceof Double) writeDouble((Double) o);
else if (o instanceof String) writeString((String) o);
else if (o instanceof byte[]) writeRaw((byte[]) o);
else if (o instanceof Bytes) writeRaw(((Bytes) o).getBytes());

View File

@@ -41,7 +41,7 @@ class ForumListValidator implements MessageValidator {
MESSAGE_HEADER_LENGTH, raw.length - MESSAGE_HEADER_LENGTH);
BdfReader r = bdfReaderFactory.createReader(in);
r.readListStart();
long version = r.readInteger();
long version = r.readLong();
if (version < 0) throw new FormatException();
r.readListStart();
while (!r.hasListEnd()) {

View File

@@ -77,7 +77,7 @@ class ForumPostFactoryImpl implements ForumPostFactory {
BdfWriter w = bdfWriterFactory.createWriter(out);
w.writeListStart();
w.writeRaw(groupId.getBytes());
w.writeInteger(timestamp);
w.writeLong(timestamp);
if (parent == null) w.writeNull();
else w.writeRaw(parent.getBytes());
writeAuthor(w, author);

View File

@@ -117,7 +117,7 @@ class ForumPostValidator implements MessageValidator {
BdfWriter w = bdfWriterFactory.createWriter(out);
w.writeListStart();
w.writeRaw(m.getGroupId().getBytes());
w.writeInteger(m.getTimestamp());
w.writeLong(m.getTimestamp());
if (parent == null) w.writeNull();
else w.writeRaw(parent.getBytes());
writeAuthor(w, author);

View File

@@ -3,6 +3,7 @@ package org.briarproject.forum;
import com.google.inject.Inject;
import org.briarproject.api.FormatException;
import org.briarproject.api.clients.PrivateGroupFactory;
import org.briarproject.api.contact.Contact;
import org.briarproject.api.contact.ContactId;
import org.briarproject.api.contact.ContactManager.AddContactHook;
@@ -28,7 +29,6 @@ import org.briarproject.api.sync.GroupId;
import org.briarproject.api.sync.Message;
import org.briarproject.api.sync.MessageFactory;
import org.briarproject.api.sync.MessageId;
import org.briarproject.api.sync.PrivateGroupFactory;
import org.briarproject.api.sync.ValidationManager.ValidationHook;
import org.briarproject.api.system.Clock;
import org.briarproject.util.StringUtils;
@@ -345,7 +345,7 @@ class ForumSharingManagerImpl implements ForumSharingManager, AddContactHook,
BdfReader r = bdfReaderFactory.createReader(in);
try {
r.readListStart();
r.skipInteger(); // Version
r.skipLong(); // Version
r.readListStart();
while (!r.hasListEnd()) {
r.readListStart();
@@ -387,7 +387,7 @@ class ForumSharingManagerImpl implements ForumSharingManager, AddContactHook,
BdfWriter w = bdfWriterFactory.createWriter(out);
try {
w.writeListStart();
w.writeInteger(version);
w.writeLong(version);
w.writeListStart();
for (Forum f : forums) {
w.writeListStart();

View File

@@ -196,13 +196,13 @@ abstract class Connector extends Thread {
protected void sendTimestamp(BdfWriter w, long timestamp)
throws IOException {
w.writeInteger(timestamp);
w.writeLong(timestamp);
w.flush();
if (LOG.isLoggable(INFO)) LOG.info(pluginName + " sent timestamp");
}
protected long receiveTimestamp(BdfReader r) throws IOException {
long timestamp = r.readInteger();
long timestamp = r.readLong();
if (timestamp < 0) throw new FormatException();
if (LOG.isLoggable(INFO)) LOG.info(pluginName + " received timestamp");
return timestamp;

View File

@@ -3,6 +3,7 @@ package org.briarproject.messaging;
import com.google.inject.Inject;
import org.briarproject.api.FormatException;
import org.briarproject.api.clients.PrivateGroupFactory;
import org.briarproject.api.contact.Contact;
import org.briarproject.api.contact.ContactId;
import org.briarproject.api.contact.ContactManager.AddContactHook;
@@ -24,7 +25,6 @@ import org.briarproject.api.sync.Group;
import org.briarproject.api.sync.GroupId;
import org.briarproject.api.sync.MessageId;
import org.briarproject.api.sync.MessageStatus;
import org.briarproject.api.sync.PrivateGroupFactory;
import org.briarproject.util.StringUtils;
import java.io.ByteArrayInputStream;

View File

@@ -5,6 +5,7 @@ import com.google.inject.Inject;
import org.briarproject.api.DeviceId;
import org.briarproject.api.FormatException;
import org.briarproject.api.TransportId;
import org.briarproject.api.clients.PrivateGroupFactory;
import org.briarproject.api.contact.Contact;
import org.briarproject.api.contact.ContactId;
import org.briarproject.api.contact.ContactManager.AddContactHook;
@@ -30,7 +31,6 @@ import org.briarproject.api.sync.GroupId;
import org.briarproject.api.sync.Message;
import org.briarproject.api.sync.MessageFactory;
import org.briarproject.api.sync.MessageId;
import org.briarproject.api.sync.PrivateGroupFactory;
import org.briarproject.api.system.Clock;
import org.briarproject.util.StringUtils;
@@ -288,7 +288,7 @@ class TransportPropertyManagerImpl implements TransportPropertyManager,
w.writeListStart();
w.writeRaw(dev.getBytes());
w.writeString(t.getString());
w.writeInteger(version);
w.writeLong(version);
w.writeDictionary(p);
w.writeListEnd();
} catch (IOException e) {
@@ -342,7 +342,7 @@ class TransportPropertyManagerImpl implements TransportPropertyManager,
r.readListStart();
r.skipRaw(); // Device ID
r.skipString(); // Transport ID
r.skipInteger(); // Version
r.skipLong(); // Version
r.readDictionaryStart();
while (!r.hasDictionaryEnd()) {
String key = r.readString(MAX_PROPERTY_LENGTH);

View File

@@ -57,7 +57,7 @@ class TransportPropertyValidator implements MessageValidator {
if (deviceId.length != UniqueId.LENGTH) throw new FormatException();
String transportId = r.readString(MAX_TRANSPORT_ID_LENGTH);
if (transportId.length() == 0) throw new FormatException();
long version = r.readInteger();
long version = r.readLong();
if (version < 0) throw new FormatException();
r.readDictionaryStart();
for (int i = 0; !r.hasDictionaryEnd(); i++) {

View File

@@ -9,7 +9,6 @@ import org.briarproject.api.sync.GroupFactory;
import org.briarproject.api.sync.MessageFactory;
import org.briarproject.api.sync.PacketReaderFactory;
import org.briarproject.api.sync.PacketWriterFactory;
import org.briarproject.api.sync.PrivateGroupFactory;
import org.briarproject.api.sync.SyncSessionFactory;
import org.briarproject.api.sync.ValidationManager;
@@ -23,7 +22,6 @@ public class SyncModule extends AbstractModule {
bind(MessageFactory.class).to(MessageFactoryImpl.class);
bind(PacketReaderFactory.class).to(PacketReaderFactoryImpl.class);
bind(PacketWriterFactory.class).to(PacketWriterFactoryImpl.class);
bind(PrivateGroupFactory.class).to(PrivateGroupFactoryImpl.class);
bind(SyncSessionFactory.class).to(
SyncSessionFactoryImpl.class).in(Singleton.class);
}