Moved the messaging protocol one step closer to BSP.

This breaks backward compatibility for the wire protocol and messages
stored in the database. The database schema version has been
incremented.
This commit is contained in:
akwizgran
2015-05-01 16:58:49 +01:00
parent ffcc8b6b38
commit 32c9ce50d9
33 changed files with 732 additions and 750 deletions

View File

@@ -64,8 +64,8 @@ import org.briarproject.api.transport.TemporarySecret;
*/
abstract class JdbcDatabase implements Database<Connection> {
private static final int SCHEMA_VERSION = 8;
private static final int MIN_SCHEMA_VERSION = 8;
private static final int SCHEMA_VERSION = 9;
private static final int MIN_SCHEMA_VERSION = 9;
private static final String CREATE_SETTINGS =
"CREATE TABLE settings"

View File

@@ -1,7 +1,5 @@
package org.briarproject.messaging;
import static org.briarproject.api.messaging.Types.AUTHOR;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
@@ -45,10 +43,10 @@ class AuthorFactoryImpl implements AuthorFactory {
ByteArrayOutputStream out = new ByteArrayOutputStream();
Writer w = writerFactory.createWriter(out);
try {
w.writeStructStart(AUTHOR);
w.writeListStart();
w.writeString(name);
w.writeBytes(publicKey);
w.writeStructEnd();
w.writeListEnd();
} catch(IOException e) {
// Shouldn't happen with ByteArrayOutputStream
throw new RuntimeException();

View File

@@ -2,7 +2,6 @@ package org.briarproject.messaging;
import static org.briarproject.api.AuthorConstants.MAX_AUTHOR_NAME_LENGTH;
import static org.briarproject.api.AuthorConstants.MAX_PUBLIC_KEY_LENGTH;
import static org.briarproject.api.messaging.Types.AUTHOR;
import java.io.IOException;
@@ -13,9 +12,9 @@ import org.briarproject.api.crypto.CryptoComponent;
import org.briarproject.api.crypto.MessageDigest;
import org.briarproject.api.serial.DigestingConsumer;
import org.briarproject.api.serial.Reader;
import org.briarproject.api.serial.StructReader;
import org.briarproject.api.serial.ObjectReader;
class AuthorReader implements StructReader<Author> {
class AuthorReader implements ObjectReader<Author> {
private final MessageDigest messageDigest;
@@ -23,16 +22,16 @@ class AuthorReader implements StructReader<Author> {
messageDigest = crypto.getMessageDigest();
}
public Author readStruct(Reader r) throws IOException {
public Author readObject(Reader r) throws IOException {
// Set up the reader
DigestingConsumer digesting = new DigestingConsumer(messageDigest);
r.addConsumer(digesting);
// Read and digest the data
r.readStructStart(AUTHOR);
r.readListStart();
String name = r.readString(MAX_AUTHOR_NAME_LENGTH);
if(name.length() == 0) throw new FormatException();
byte[] publicKey = r.readBytes(MAX_PUBLIC_KEY_LENGTH);
r.readStructEnd();
r.readListEnd();
// Reset the reader
r.removeConsumer(digesting);
// Build and return the author

View File

@@ -3,7 +3,7 @@ package org.briarproject.messaging;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static java.util.logging.Level.INFO;
import static java.util.logging.Level.WARNING;
import static org.briarproject.api.messaging.MessagingConstants.MAX_PACKET_LENGTH;
import static org.briarproject.api.messaging.MessagingConstants.MAX_PAYLOAD_LENGTH;
import java.io.IOException;
import java.util.Collection;
@@ -260,7 +260,7 @@ class DuplexOutgoingSession implements MessagingSession, EventListener {
if(interrupted) return;
try {
Collection<byte[]> b = db.generateRequestedBatch(contactId,
MAX_PACKET_LENGTH, maxLatency);
MAX_PAYLOAD_LENGTH, maxLatency);
if(LOG.isLoggable(INFO))
LOG.info("Generated batch: " + (b != null));
if(b != null) writerTasks.add(new WriteBatch(b));

View File

@@ -1,7 +1,6 @@
package org.briarproject.messaging;
import static org.briarproject.api.messaging.MessagingConstants.GROUP_SALT_LENGTH;
import static org.briarproject.api.messaging.Types.GROUP;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
@@ -37,10 +36,10 @@ class GroupFactoryImpl implements GroupFactory {
ByteArrayOutputStream out = new ByteArrayOutputStream();
Writer w = writerFactory.createWriter(out);
try {
w.writeStructStart(GROUP);
w.writeListStart();
w.writeString(name);
w.writeBytes(salt);
w.writeStructEnd();
w.writeListEnd();
} catch(IOException e) {
// Shouldn't happen with ByteArrayOutputStream
throw new RuntimeException();

View File

@@ -2,7 +2,6 @@ package org.briarproject.messaging;
import static org.briarproject.api.messaging.MessagingConstants.GROUP_SALT_LENGTH;
import static org.briarproject.api.messaging.MessagingConstants.MAX_GROUP_NAME_LENGTH;
import static org.briarproject.api.messaging.Types.GROUP;
import java.io.IOException;
@@ -13,9 +12,9 @@ import org.briarproject.api.messaging.Group;
import org.briarproject.api.messaging.GroupId;
import org.briarproject.api.serial.DigestingConsumer;
import org.briarproject.api.serial.Reader;
import org.briarproject.api.serial.StructReader;
import org.briarproject.api.serial.ObjectReader;
class GroupReader implements StructReader<Group> {
class GroupReader implements ObjectReader<Group> {
private final MessageDigest messageDigest;
@@ -23,16 +22,16 @@ class GroupReader implements StructReader<Group> {
messageDigest = crypto.getMessageDigest();
}
public Group readStruct(Reader r) throws IOException {
public Group readObject(Reader r) throws IOException {
DigestingConsumer digesting = new DigestingConsumer(messageDigest);
// Read and digest the data
r.addConsumer(digesting);
r.readStructStart(GROUP);
r.readListStart();
String name = r.readString(MAX_GROUP_NAME_LENGTH);
if(name.length() == 0) throw new FormatException();
byte[] salt = r.readBytes(GROUP_SALT_LENGTH);
if(salt.length != GROUP_SALT_LENGTH) throw new FormatException();
r.readStructEnd();
r.readListEnd();
r.removeConsumer(digesting);
// Build and return the group
GroupId id = new GroupId(messageDigest.digest());

View File

@@ -3,11 +3,8 @@ package org.briarproject.messaging;
import static org.briarproject.api.AuthorConstants.MAX_SIGNATURE_LENGTH;
import static org.briarproject.api.messaging.MessagingConstants.MAX_BODY_LENGTH;
import static org.briarproject.api.messaging.MessagingConstants.MAX_CONTENT_TYPE_LENGTH;
import static org.briarproject.api.messaging.MessagingConstants.MAX_PACKET_LENGTH;
import static org.briarproject.api.messaging.MessagingConstants.MAX_PAYLOAD_LENGTH;
import static org.briarproject.api.messaging.MessagingConstants.MESSAGE_SALT_LENGTH;
import static org.briarproject.api.messaging.Types.AUTHOR;
import static org.briarproject.api.messaging.Types.GROUP;
import static org.briarproject.api.messaging.Types.MESSAGE;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
@@ -77,7 +74,7 @@ class MessageFactoryImpl implements MessageFactory {
ByteArrayOutputStream out = new ByteArrayOutputStream();
Writer w = writerFactory.createWriter(out);
// Initialise the consumers
CountingConsumer counting = new CountingConsumer(MAX_PACKET_LENGTH);
CountingConsumer counting = new CountingConsumer(MAX_PAYLOAD_LENGTH);
w.addConsumer(counting);
Consumer digestingConsumer = new DigestingConsumer(messageDigest);
w.addConsumer(digestingConsumer);
@@ -88,7 +85,7 @@ class MessageFactoryImpl implements MessageFactory {
w.addConsumer(signingConsumer);
}
// Write the message
w.writeStructStart(MESSAGE);
w.writeListStart();
if(parent == null) w.writeNull();
else w.writeBytes(parent.getBytes());
writeGroup(w, group);
@@ -111,7 +108,7 @@ class MessageFactoryImpl implements MessageFactory {
throw new IllegalArgumentException();
w.writeBytes(sig);
}
w.writeStructEnd();
w.writeListEnd();
// Hash the message, including the signature, to get the message ID
w.removeConsumer(digestingConsumer);
MessageId id = new MessageId(messageDigest.digest());
@@ -120,16 +117,16 @@ class MessageFactoryImpl implements MessageFactory {
}
private void writeGroup(Writer w, Group g) throws IOException {
w.writeStructStart(GROUP);
w.writeListStart();
w.writeString(g.getName());
w.writeBytes(g.getSalt());
w.writeStructEnd();
w.writeListEnd();
}
private void writeAuthor(Writer w, Author a) throws IOException {
w.writeStructStart(AUTHOR);
w.writeListStart();
w.writeString(a.getName());
w.writeBytes(a.getPublicKey());
w.writeStructEnd();
w.writeListEnd();
}
}

View File

@@ -3,9 +3,8 @@ package org.briarproject.messaging;
import static org.briarproject.api.AuthorConstants.MAX_SIGNATURE_LENGTH;
import static org.briarproject.api.messaging.MessagingConstants.MAX_BODY_LENGTH;
import static org.briarproject.api.messaging.MessagingConstants.MAX_CONTENT_TYPE_LENGTH;
import static org.briarproject.api.messaging.MessagingConstants.MAX_PACKET_LENGTH;
import static org.briarproject.api.messaging.MessagingConstants.MAX_PAYLOAD_LENGTH;
import static org.briarproject.api.messaging.MessagingConstants.MESSAGE_SALT_LENGTH;
import static org.briarproject.api.messaging.Types.MESSAGE;
import java.io.IOException;
@@ -17,27 +16,27 @@ import org.briarproject.api.messaging.MessageId;
import org.briarproject.api.messaging.UnverifiedMessage;
import org.briarproject.api.serial.CopyingConsumer;
import org.briarproject.api.serial.CountingConsumer;
import org.briarproject.api.serial.ObjectReader;
import org.briarproject.api.serial.Reader;
import org.briarproject.api.serial.StructReader;
class MessageReader implements StructReader<UnverifiedMessage> {
class MessageReader implements ObjectReader<UnverifiedMessage> {
private final StructReader<Group> groupReader;
private final StructReader<Author> authorReader;
private final ObjectReader<Group> groupReader;
private final ObjectReader<Author> authorReader;
MessageReader(StructReader<Group> groupReader,
StructReader<Author> authorReader) {
MessageReader(ObjectReader<Group> groupReader,
ObjectReader<Author> authorReader) {
this.groupReader = groupReader;
this.authorReader = authorReader;
}
public UnverifiedMessage readStruct(Reader r) throws IOException {
public UnverifiedMessage readObject(Reader r) throws IOException {
CopyingConsumer copying = new CopyingConsumer();
CountingConsumer counting = new CountingConsumer(MAX_PACKET_LENGTH);
CountingConsumer counting = new CountingConsumer(MAX_PAYLOAD_LENGTH);
r.addConsumer(copying);
r.addConsumer(counting);
// Read the start of the struct
r.readStructStart(MESSAGE);
// Read the start of the message
r.readListStart();
// Read the parent's message ID, if there is one
MessageId parent = null;
if(r.hasNull()) {
@@ -48,11 +47,11 @@ class MessageReader implements StructReader<UnverifiedMessage> {
parent = new MessageId(b);
}
// Read the group
Group group = groupReader.readStruct(r);
Group group = groupReader.readObject(r);
// Read the author, if there is one
Author author = null;
if(r.hasNull()) r.readNull();
else author = authorReader.readStruct(r);
else author = authorReader.readObject(r);
// Read the content type
String contentType = r.readString(MAX_CONTENT_TYPE_LENGTH);
// Read the timestamp
@@ -71,11 +70,12 @@ class MessageReader implements StructReader<UnverifiedMessage> {
byte[] signature = null;
if(author == null) r.readNull();
else signature = r.readBytes(MAX_SIGNATURE_LENGTH);
// Read the end of the struct
r.readStructEnd();
// The signature will be verified later
// Read the end of the message
r.readListEnd();
// Reset the reader
r.removeConsumer(counting);
r.removeConsumer(copying);
// Build and return the unverified message
byte[] raw = copying.getCopy();
return new UnverifiedMessage(parent, group, author, contentType,
timestamp, raw, signature, bodyStart, body.length,

View File

@@ -14,7 +14,7 @@ import org.briarproject.api.messaging.PacketReaderFactory;
import org.briarproject.api.messaging.PacketWriterFactory;
import org.briarproject.api.messaging.SubscriptionUpdate;
import org.briarproject.api.messaging.UnverifiedMessage;
import org.briarproject.api.serial.StructReader;
import org.briarproject.api.serial.ObjectReader;
import com.google.inject.AbstractModule;
import com.google.inject.Provides;
@@ -34,25 +34,25 @@ public class MessagingModule extends AbstractModule {
}
@Provides
StructReader<Author> getAuthorReader(CryptoComponent crypto) {
ObjectReader<Author> getAuthorReader(CryptoComponent crypto) {
return new AuthorReader(crypto);
}
@Provides
StructReader<Group> getGroupReader(CryptoComponent crypto) {
ObjectReader<Group> getGroupReader(CryptoComponent crypto) {
return new GroupReader(crypto);
}
@Provides
StructReader<UnverifiedMessage> getMessageReader(
StructReader<Group> groupReader,
StructReader<Author> authorReader) {
ObjectReader<UnverifiedMessage> getMessageReader(
ObjectReader<Group> groupReader,
ObjectReader<Author> authorReader) {
return new MessageReader(groupReader, authorReader);
}
@Provides
StructReader<SubscriptionUpdate> getSubscriptionUpdateReader(
StructReader<Group> groupReader) {
ObjectReader<SubscriptionUpdate> getSubscriptionUpdateReader(
ObjectReader<Group> groupReader) {
return new SubscriptionUpdateReader(groupReader);
}
}

View File

@@ -9,18 +9,18 @@ import org.briarproject.api.messaging.PacketReaderFactory;
import org.briarproject.api.messaging.SubscriptionUpdate;
import org.briarproject.api.messaging.UnverifiedMessage;
import org.briarproject.api.serial.ReaderFactory;
import org.briarproject.api.serial.StructReader;
import org.briarproject.api.serial.ObjectReader;
class PacketReaderFactoryImpl implements PacketReaderFactory {
private final ReaderFactory readerFactory;
private final StructReader<UnverifiedMessage> messageReader;
private final StructReader<SubscriptionUpdate> subscriptionUpdateReader;
private final ObjectReader<UnverifiedMessage> messageReader;
private final ObjectReader<SubscriptionUpdate> subscriptionUpdateReader;
@Inject
PacketReaderFactoryImpl(ReaderFactory readerFactory,
StructReader<UnverifiedMessage> messageReader,
StructReader<SubscriptionUpdate> subscriptionUpdateReader) {
ObjectReader<UnverifiedMessage> messageReader,
ObjectReader<SubscriptionUpdate> subscriptionUpdateReader) {
this.readerFactory = readerFactory;
this.messageReader = messageReader;
this.subscriptionUpdateReader = subscriptionUpdateReader;

View File

@@ -3,18 +3,21 @@ package org.briarproject.messaging;
import static org.briarproject.api.TransportPropertyConstants.MAX_PROPERTIES_PER_TRANSPORT;
import static org.briarproject.api.TransportPropertyConstants.MAX_PROPERTY_LENGTH;
import static org.briarproject.api.TransportPropertyConstants.MAX_TRANSPORT_ID_LENGTH;
import static org.briarproject.api.messaging.MessagingConstants.MAX_PACKET_LENGTH;
import static org.briarproject.api.messaging.Types.ACK;
import static org.briarproject.api.messaging.Types.MESSAGE;
import static org.briarproject.api.messaging.Types.OFFER;
import static org.briarproject.api.messaging.Types.REQUEST;
import static org.briarproject.api.messaging.Types.RETENTION_ACK;
import static org.briarproject.api.messaging.Types.RETENTION_UPDATE;
import static org.briarproject.api.messaging.Types.SUBSCRIPTION_ACK;
import static org.briarproject.api.messaging.Types.SUBSCRIPTION_UPDATE;
import static org.briarproject.api.messaging.Types.TRANSPORT_ACK;
import static org.briarproject.api.messaging.Types.TRANSPORT_UPDATE;
import static org.briarproject.api.messaging.MessagingConstants.HEADER_LENGTH;
import static org.briarproject.api.messaging.MessagingConstants.MAX_PAYLOAD_LENGTH;
import static org.briarproject.api.messaging.MessagingConstants.PROTOCOL_VERSION;
import static org.briarproject.api.messaging.PacketTypes.ACK;
import static org.briarproject.api.messaging.PacketTypes.MESSAGE;
import static org.briarproject.api.messaging.PacketTypes.OFFER;
import static org.briarproject.api.messaging.PacketTypes.REQUEST;
import static org.briarproject.api.messaging.PacketTypes.RETENTION_ACK;
import static org.briarproject.api.messaging.PacketTypes.RETENTION_UPDATE;
import static org.briarproject.api.messaging.PacketTypes.SUBSCRIPTION_ACK;
import static org.briarproject.api.messaging.PacketTypes.SUBSCRIPTION_UPDATE;
import static org.briarproject.api.messaging.PacketTypes.TRANSPORT_ACK;
import static org.briarproject.api.messaging.PacketTypes.TRANSPORT_UPDATE;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
@@ -39,42 +42,82 @@ import org.briarproject.api.messaging.SubscriptionUpdate;
import org.briarproject.api.messaging.TransportAck;
import org.briarproject.api.messaging.TransportUpdate;
import org.briarproject.api.messaging.UnverifiedMessage;
import org.briarproject.api.serial.Consumer;
import org.briarproject.api.serial.CountingConsumer;
import org.briarproject.api.serial.Reader;
import org.briarproject.api.serial.ReaderFactory;
import org.briarproject.api.serial.StructReader;
import org.briarproject.api.serial.ObjectReader;
import org.briarproject.util.ByteUtils;
// This class is not thread-safe
class PacketReaderImpl implements PacketReader {
private final StructReader<UnverifiedMessage> messageReader;
private final StructReader<SubscriptionUpdate> subscriptionUpdateReader;
private final Reader r;
private enum State { BUFFER_EMPTY, BUFFER_FULL, EOF };
private final ReaderFactory readerFactory;
private final ObjectReader<UnverifiedMessage> messageReader;
private final ObjectReader<SubscriptionUpdate> subscriptionUpdateReader;
private final InputStream in;
private final byte[] header, payload;
private State state = State.BUFFER_EMPTY;
private int payloadLength = 0;
PacketReaderImpl(ReaderFactory readerFactory,
StructReader<UnverifiedMessage> messageReader,
StructReader<SubscriptionUpdate> subscriptionUpdateReader,
ObjectReader<UnverifiedMessage> messageReader,
ObjectReader<SubscriptionUpdate> subscriptionUpdateReader,
InputStream in) {
this.readerFactory = readerFactory;
this.messageReader = messageReader;
this.subscriptionUpdateReader = subscriptionUpdateReader;
r = readerFactory.createReader(in);
this.in = in;
header = new byte[HEADER_LENGTH];
payload = new byte[MAX_PAYLOAD_LENGTH];
}
private void readPacket() throws IOException {
assert state == State.BUFFER_EMPTY;
// Read the header
int offset = 0;
while(offset < HEADER_LENGTH) {
int read = in.read(header, offset, HEADER_LENGTH - offset);
if(read == -1) {
if(offset > 0) throw new FormatException();
state = State.EOF;
return;
}
offset += read;
}
// Check the protocol version
if(header[0] != PROTOCOL_VERSION) throw new FormatException();
// Read the payload length
payloadLength = ByteUtils.readUint16(header, 2);
if(payloadLength > MAX_PAYLOAD_LENGTH) throw new FormatException();
// Read the payload
offset = 0;
while(offset < payloadLength) {
int read = in.read(payload, offset, payloadLength - offset);
if(read == -1) throw new FormatException();
offset += read;
}
state = State.BUFFER_FULL;
}
public boolean eof() throws IOException {
return r.eof();
if(state == State.BUFFER_EMPTY) readPacket();
assert state != State.BUFFER_EMPTY;
return state == State.EOF;
}
public boolean hasAck() throws IOException {
return r.hasStruct(ACK);
return !eof() && header[1] == ACK;
}
public Ack readAck() throws IOException {
if(!hasAck()) throw new FormatException();
// Set up the reader
Consumer counting = new CountingConsumer(MAX_PACKET_LENGTH);
r.addConsumer(counting);
// Read the start of the struct
r.readStructStart(ACK);
InputStream bais = new ByteArrayInputStream(payload, 0, payloadLength);
Reader r = readerFactory.createReader(bais);
// Read the start of the payload
r.readListStart();
// Read the message IDs
List<MessageId> acked = new ArrayList<MessageId>();
r.readListStart();
@@ -86,32 +129,41 @@ class PacketReaderImpl implements PacketReader {
}
if(acked.isEmpty()) throw new FormatException();
r.readListEnd();
// Read the end of the struct
r.readStructEnd();
// Reset the reader
r.removeConsumer(counting);
// Read the end of the payload
r.readListEnd();
if(!r.eof()) throw new FormatException();
state = State.BUFFER_EMPTY;
// Build and return the ack
return new Ack(Collections.unmodifiableList(acked));
}
public boolean hasMessage() throws IOException {
return r.hasStruct(MESSAGE);
return !eof() && header[1] == MESSAGE;
}
public UnverifiedMessage readMessage() throws IOException {
return messageReader.readStruct(r);
if(!hasMessage()) throw new FormatException();
// Set up the reader
InputStream bais = new ByteArrayInputStream(payload, 0, payloadLength);
Reader r = readerFactory.createReader(bais);
// Read and build the message
UnverifiedMessage m = messageReader.readObject(r);
if(!r.eof()) throw new FormatException();
state = State.BUFFER_EMPTY;
return m;
}
public boolean hasOffer() throws IOException {
return r.hasStruct(OFFER);
return !eof() && header[1] == OFFER;
}
public Offer readOffer() throws IOException {
if(!hasOffer()) throw new FormatException();
// Set up the reader
Consumer counting = new CountingConsumer(MAX_PACKET_LENGTH);
r.addConsumer(counting);
// Read the start of the struct
r.readStructStart(OFFER);
InputStream bais = new ByteArrayInputStream(payload, 0, payloadLength);
Reader r = readerFactory.createReader(bais);
// Read the start of the payload
r.readListStart();
// Read the message IDs
List<MessageId> offered = new ArrayList<MessageId>();
r.readListStart();
@@ -123,27 +175,28 @@ class PacketReaderImpl implements PacketReader {
}
if(offered.isEmpty()) throw new FormatException();
r.readListEnd();
// Read the end of the struct
r.readStructEnd();
// Reset the reader
r.removeConsumer(counting);
// Read the end of the payload
r.readListEnd();
if(!r.eof()) throw new FormatException();
state = State.BUFFER_EMPTY;
// Build and return the offer
return new Offer(Collections.unmodifiableList(offered));
}
public boolean hasRequest() throws IOException {
return r.hasStruct(REQUEST);
return !eof() && header[1] == REQUEST;
}
public Request readRequest() throws IOException {
if(!hasRequest()) throw new FormatException();
// Set up the reader
Consumer counting = new CountingConsumer(MAX_PACKET_LENGTH);
r.addConsumer(counting);
// Read the start of the struct
r.readStructStart(REQUEST);
// Read the message IDs
List<MessageId> requested = new ArrayList<MessageId>();
InputStream bais = new ByteArrayInputStream(payload, 0, payloadLength);
Reader r = readerFactory.createReader(bais);
// Read the start of the payload
r.readListStart();
// Read the message IDs
r.readListStart();
List<MessageId> requested = new ArrayList<MessageId>();
while(!r.hasListEnd()) {
byte[] b = r.readBytes(UniqueId.LENGTH);
if(b.length != UniqueId.LENGTH)
@@ -152,85 +205,134 @@ class PacketReaderImpl implements PacketReader {
}
if(requested.isEmpty()) throw new FormatException();
r.readListEnd();
// Read the end of the struct
r.readStructEnd();
// Reset the reader
r.removeConsumer(counting);
// Read the end of the payload
r.readListEnd();
if(!r.eof()) throw new FormatException();
state = State.BUFFER_EMPTY;
// Build and return the request
return new Request(Collections.unmodifiableList(requested));
}
public boolean hasRetentionAck() throws IOException {
return r.hasStruct(RETENTION_ACK);
return !eof() && header[1] == RETENTION_ACK;
}
public RetentionAck readRetentionAck() throws IOException {
r.readStructStart(RETENTION_ACK);
if(!hasRetentionAck()) throw new FormatException();
// Set up the reader
InputStream bais = new ByteArrayInputStream(payload, 0, payloadLength);
Reader r = readerFactory.createReader(bais);
// Read the start of the payload
r.readListStart();
// Read the version
long version = r.readInteger();
if(version < 0) throw new FormatException();
r.readStructEnd();
// Read the end of the payload
r.readListEnd();
if(!r.eof()) throw new FormatException();
state = State.BUFFER_EMPTY;
// Build and return the retention ack
return new RetentionAck(version);
}
public boolean hasRetentionUpdate() throws IOException {
return r.hasStruct(RETENTION_UPDATE);
return !eof() && header[1] == RETENTION_UPDATE;
}
public RetentionUpdate readRetentionUpdate() throws IOException {
r.readStructStart(RETENTION_UPDATE);
if(!hasRetentionUpdate()) throw new FormatException();
// Set up the reader
InputStream bais = new ByteArrayInputStream(payload, 0, payloadLength);
Reader r = readerFactory.createReader(bais);
// Read the start of the payload
r.readListStart();
// Read the retention time and version
long retention = r.readInteger();
if(retention < 0) throw new FormatException();
long version = r.readInteger();
if(version < 0) throw new FormatException();
r.readStructEnd();
// Read the end of the payload
r.readListEnd();
if(!r.eof()) throw new FormatException();
state = State.BUFFER_EMPTY;
// Build and return the retention update
return new RetentionUpdate(retention, version);
}
public boolean hasSubscriptionAck() throws IOException {
return r.hasStruct(SUBSCRIPTION_ACK);
return !eof() && header[1] == SUBSCRIPTION_ACK;
}
public SubscriptionAck readSubscriptionAck() throws IOException {
r.readStructStart(SUBSCRIPTION_ACK);
if(!hasSubscriptionAck()) throw new FormatException();
// Set up the reader
InputStream bais = new ByteArrayInputStream(payload, 0, payloadLength);
Reader r = readerFactory.createReader(bais);
// Read the start of the payload
r.readListStart();
// Read the version
long version = r.readInteger();
if(version < 0) throw new FormatException();
r.readStructEnd();
// Read the end of the payload
r.readListEnd();
if(!r.eof()) throw new FormatException();
state = State.BUFFER_EMPTY;
// Build and return the subscription ack
return new SubscriptionAck(version);
}
public boolean hasSubscriptionUpdate() throws IOException {
return r.hasStruct(SUBSCRIPTION_UPDATE);
return !eof() && header[1] == SUBSCRIPTION_UPDATE;
}
public SubscriptionUpdate readSubscriptionUpdate() throws IOException {
return subscriptionUpdateReader.readStruct(r);
if(!hasSubscriptionUpdate()) throw new FormatException();
// Set up the reader
InputStream bais = new ByteArrayInputStream(payload, 0, payloadLength);
Reader r = readerFactory.createReader(bais);
// Read and build the subscription update
SubscriptionUpdate u = subscriptionUpdateReader.readObject(r);
if(!r.eof()) throw new FormatException();
state = State.BUFFER_EMPTY;
return u;
}
public boolean hasTransportAck() throws IOException {
return r.hasStruct(TRANSPORT_ACK);
return !eof() && header[1] == TRANSPORT_ACK;
}
public TransportAck readTransportAck() throws IOException {
r.readStructStart(TRANSPORT_ACK);
if(!hasTransportAck()) throw new FormatException();
// Set up the reader
InputStream bais = new ByteArrayInputStream(payload, 0, payloadLength);
Reader r = readerFactory.createReader(bais);
// Read the start of the payload
r.readListStart();
// Read the transport ID and version
String idString = r.readString(MAX_TRANSPORT_ID_LENGTH);
if(idString.length() == 0) throw new FormatException();
TransportId id = new TransportId(idString);
long version = r.readInteger();
if(version < 0) throw new FormatException();
r.readStructEnd();
// Read the end of the payload
r.readListEnd();
if(!r.eof()) throw new FormatException();
state = State.BUFFER_EMPTY;
// Build and return the transport ack
return new TransportAck(id, version);
}
public boolean hasTransportUpdate() throws IOException {
return r.hasStruct(TRANSPORT_UPDATE);
return !eof() && header[1] == TRANSPORT_UPDATE;
}
public TransportUpdate readTransportUpdate() throws IOException {
if(!hasTransportUpdate()) throw new FormatException();
// Set up the reader
Consumer counting = new CountingConsumer(MAX_PACKET_LENGTH);
r.addConsumer(counting);
// Read the start of the struct
r.readStructStart(TRANSPORT_UPDATE);
InputStream bais = new ByteArrayInputStream(payload, 0, payloadLength);
Reader r = readerFactory.createReader(bais);
// Read the start of the payload
r.readListStart();
// Read the transport ID
String idString = r.readString(MAX_TRANSPORT_ID_LENGTH);
if(idString.length() == 0) throw new FormatException();
@@ -249,10 +351,10 @@ class PacketReaderImpl implements PacketReader {
// Read the version number
long version = r.readInteger();
if(version < 0) throw new FormatException();
// Read the end of the struct
r.readStructEnd();
// Reset the reader
r.removeConsumer(counting);
// Read the end of the payload
r.readListEnd();
if(!r.eof()) throw new FormatException();
state = State.BUFFER_EMPTY;
// Build and return the transport update
return new TransportUpdate(id, new TransportProperties(p), version);
}

View File

@@ -1,17 +1,19 @@
package org.briarproject.messaging;
import static org.briarproject.api.messaging.MessagingConstants.MAX_PACKET_LENGTH;
import static org.briarproject.api.messaging.Types.ACK;
import static org.briarproject.api.messaging.Types.GROUP;
import static org.briarproject.api.messaging.Types.OFFER;
import static org.briarproject.api.messaging.Types.REQUEST;
import static org.briarproject.api.messaging.Types.RETENTION_ACK;
import static org.briarproject.api.messaging.Types.RETENTION_UPDATE;
import static org.briarproject.api.messaging.Types.SUBSCRIPTION_ACK;
import static org.briarproject.api.messaging.Types.SUBSCRIPTION_UPDATE;
import static org.briarproject.api.messaging.Types.TRANSPORT_ACK;
import static org.briarproject.api.messaging.Types.TRANSPORT_UPDATE;
import static org.briarproject.api.messaging.MessagingConstants.HEADER_LENGTH;
import static org.briarproject.api.messaging.MessagingConstants.MAX_PAYLOAD_LENGTH;
import static org.briarproject.api.messaging.MessagingConstants.PROTOCOL_VERSION;
import static org.briarproject.api.messaging.PacketTypes.ACK;
import static org.briarproject.api.messaging.PacketTypes.OFFER;
import static org.briarproject.api.messaging.PacketTypes.REQUEST;
import static org.briarproject.api.messaging.PacketTypes.RETENTION_ACK;
import static org.briarproject.api.messaging.PacketTypes.RETENTION_UPDATE;
import static org.briarproject.api.messaging.PacketTypes.SUBSCRIPTION_ACK;
import static org.briarproject.api.messaging.PacketTypes.SUBSCRIPTION_UPDATE;
import static org.briarproject.api.messaging.PacketTypes.TRANSPORT_ACK;
import static org.briarproject.api.messaging.PacketTypes.TRANSPORT_UPDATE;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
@@ -19,6 +21,7 @@ import org.briarproject.api.messaging.Ack;
import org.briarproject.api.messaging.Group;
import org.briarproject.api.messaging.MessageId;
import org.briarproject.api.messaging.Offer;
import org.briarproject.api.messaging.PacketTypes;
import org.briarproject.api.messaging.PacketWriter;
import org.briarproject.api.messaging.Request;
import org.briarproject.api.messaging.RetentionAck;
@@ -30,118 +33,161 @@ import org.briarproject.api.messaging.TransportUpdate;
import org.briarproject.api.serial.SerialComponent;
import org.briarproject.api.serial.Writer;
import org.briarproject.api.serial.WriterFactory;
import org.briarproject.util.ByteUtils;
// This class is not thread-safe
class PacketWriterImpl implements PacketWriter {
private final SerialComponent serial;
private final WriterFactory writerFactory;
private final OutputStream out;
private final Writer w;
private final byte[] header;
private final ByteArrayOutputStream payload;
PacketWriterImpl(SerialComponent serial, WriterFactory writerFactory,
OutputStream out) {
this.serial = serial;
this.writerFactory = writerFactory;
this.out = out;
w = writerFactory.createWriter(out);
header = new byte[HEADER_LENGTH];
header[0] = PROTOCOL_VERSION;
payload = new ByteArrayOutputStream(MAX_PAYLOAD_LENGTH);
}
public int getMaxMessagesForAck(long capacity) {
return getMaxMessagesForPacket(capacity, ACK);
return getMaxMessagesForPacket(capacity);
}
public int getMaxMessagesForRequest(long capacity) {
return getMaxMessagesForPacket(capacity, REQUEST);
return getMaxMessagesForPacket(capacity);
}
public int getMaxMessagesForOffer(long capacity) {
return getMaxMessagesForPacket(capacity, OFFER);
return getMaxMessagesForPacket(capacity);
}
private int getMaxMessagesForPacket(long capacity, int structId) {
int packet = (int) Math.min(capacity, MAX_PACKET_LENGTH);
int overhead = serial.getSerialisedStructStartLength(structId)
+ serial.getSerialisedListStartLength()
+ serial.getSerialisedListEndLength()
+ serial.getSerialisedStructEndLength();
private int getMaxMessagesForPacket(long capacity) {
int payload = (int) Math.min(capacity - HEADER_LENGTH,
MAX_PAYLOAD_LENGTH);
int overhead = serial.getSerialisedListStartLength() * 2
+ serial.getSerialisedListEndLength() * 2;
int idLength = serial.getSerialisedUniqueIdLength();
return (packet - overhead) / idLength;
return (payload - overhead) / idLength;
}
private void writePacket(byte packetType) throws IOException {
header[1] = packetType;
ByteUtils.writeUint16(payload.size(), header, 2);
out.write(header);
payload.writeTo(out);
payload.reset();
}
public void writeAck(Ack a) throws IOException {
w.writeStructStart(ACK);
assert payload.size() == 0;
Writer w = writerFactory.createWriter(payload);
w.writeListStart();
w.writeListStart();
for(MessageId m : a.getMessageIds()) w.writeBytes(m.getBytes());
w.writeListEnd();
w.writeStructEnd();
w.writeListEnd();
writePacket(ACK);
}
public void writeMessage(byte[] raw) throws IOException {
header[1] = PacketTypes.MESSAGE;
ByteUtils.writeUint16(raw.length, header, 2);
out.write(header);
out.write(raw);
}
public void writeOffer(Offer o) throws IOException {
w.writeStructStart(OFFER);
assert payload.size() == 0;
Writer w = writerFactory.createWriter(payload);
w.writeListStart();
w.writeListStart();
for(MessageId m : o.getMessageIds()) w.writeBytes(m.getBytes());
w.writeListEnd();
w.writeStructEnd();
w.writeListEnd();
writePacket(OFFER);
}
public void writeRequest(Request r) throws IOException {
w.writeStructStart(REQUEST);
assert payload.size() == 0;
Writer w = writerFactory.createWriter(payload);
w.writeListStart();
w.writeListStart();
for(MessageId m : r.getMessageIds()) w.writeBytes(m.getBytes());
w.writeListEnd();
w.writeStructEnd();
w.writeListEnd();
writePacket(REQUEST);
}
public void writeRetentionAck(RetentionAck a) throws IOException {
w.writeStructStart(RETENTION_ACK);
assert payload.size() == 0;
Writer w = writerFactory.createWriter(payload);
w.writeListStart();
w.writeInteger(a.getVersion());
w.writeStructEnd();
w.writeListEnd();
writePacket(RETENTION_ACK);
}
public void writeRetentionUpdate(RetentionUpdate u) throws IOException {
w.writeStructStart(RETENTION_UPDATE);
assert payload.size() == 0;
Writer w = writerFactory.createWriter(payload);
w.writeListStart();
w.writeInteger(u.getRetentionTime());
w.writeInteger(u.getVersion());
w.writeStructEnd();
w.writeListEnd();
writePacket(RETENTION_UPDATE);
}
public void writeSubscriptionAck(SubscriptionAck a) throws IOException {
w.writeStructStart(SUBSCRIPTION_ACK);
assert payload.size() == 0;
Writer w = writerFactory.createWriter(payload);
w.writeListStart();
w.writeInteger(a.getVersion());
w.writeStructEnd();
w.writeListEnd();
writePacket(SUBSCRIPTION_ACK);
}
public void writeSubscriptionUpdate(SubscriptionUpdate u)
throws IOException {
w.writeStructStart(SUBSCRIPTION_UPDATE);
assert payload.size() == 0;
Writer w = writerFactory.createWriter(payload);
w.writeListStart();
w.writeListStart();
for(Group g : u.getGroups()) {
w.writeStructStart(GROUP);
w.writeListStart();
w.writeString(g.getName());
w.writeBytes(g.getSalt());
w.writeStructEnd();
w.writeListEnd();
}
w.writeListEnd();
w.writeInteger(u.getVersion());
w.writeStructEnd();
w.writeListEnd();
writePacket(SUBSCRIPTION_UPDATE);
}
public void writeTransportAck(TransportAck a) throws IOException {
w.writeStructStart(TRANSPORT_ACK);
assert payload.size() == 0;
Writer w = writerFactory.createWriter(payload);
w.writeListStart();
w.writeString(a.getId().getString());
w.writeInteger(a.getVersion());
w.writeStructEnd();
w.writeListEnd();
writePacket(TRANSPORT_ACK);
}
public void writeTransportUpdate(TransportUpdate u) throws IOException {
w.writeStructStart(TRANSPORT_UPDATE);
assert payload.size() == 0;
Writer w = writerFactory.createWriter(payload);
w.writeListStart();
w.writeString(u.getId().getString());
w.writeMap(u.getProperties());
w.writeInteger(u.getVersion());
w.writeStructEnd();
w.writeListEnd();
writePacket(TRANSPORT_UPDATE);
}
public void flush() throws IOException {

View File

@@ -2,7 +2,7 @@ package org.briarproject.messaging;
import static java.util.logging.Level.INFO;
import static java.util.logging.Level.WARNING;
import static org.briarproject.api.messaging.MessagingConstants.MAX_PACKET_LENGTH;
import static org.briarproject.api.messaging.MessagingConstants.MAX_PAYLOAD_LENGTH;
import java.io.IOException;
import java.util.Collection;
@@ -167,7 +167,7 @@ class SimplexOutgoingSession implements MessagingSession, EventListener {
if(interrupted) return;
try {
Collection<byte[]> b = db.generateBatch(contactId,
MAX_PACKET_LENGTH, maxLatency);
MAX_PAYLOAD_LENGTH, maxLatency);
if(LOG.isLoggable(INFO))
LOG.info("Generated batch: " + (b != null));
if(b == null) decrementOutstandingQueries();

View File

@@ -1,8 +1,7 @@
package org.briarproject.messaging;
import static org.briarproject.api.messaging.MessagingConstants.MAX_PACKET_LENGTH;
import static org.briarproject.api.messaging.MessagingConstants.MAX_PAYLOAD_LENGTH;
import static org.briarproject.api.messaging.MessagingConstants.MAX_SUBSCRIPTIONS;
import static org.briarproject.api.messaging.Types.SUBSCRIPTION_UPDATE;
import java.io.IOException;
import java.util.ArrayList;
@@ -17,29 +16,29 @@ import org.briarproject.api.messaging.GroupId;
import org.briarproject.api.messaging.SubscriptionUpdate;
import org.briarproject.api.serial.Consumer;
import org.briarproject.api.serial.CountingConsumer;
import org.briarproject.api.serial.ObjectReader;
import org.briarproject.api.serial.Reader;
import org.briarproject.api.serial.StructReader;
class SubscriptionUpdateReader implements StructReader<SubscriptionUpdate> {
class SubscriptionUpdateReader implements ObjectReader<SubscriptionUpdate> {
private final StructReader<Group> groupReader;
private final ObjectReader<Group> groupReader;
SubscriptionUpdateReader(StructReader<Group> groupReader) {
SubscriptionUpdateReader(ObjectReader<Group> groupReader) {
this.groupReader = groupReader;
}
public SubscriptionUpdate readStruct(Reader r) throws IOException {
public SubscriptionUpdate readObject(Reader r) throws IOException {
// Set up the reader
Consumer counting = new CountingConsumer(MAX_PACKET_LENGTH);
Consumer counting = new CountingConsumer(MAX_PAYLOAD_LENGTH);
r.addConsumer(counting);
// Read the start of the struct
r.readStructStart(SUBSCRIPTION_UPDATE);
// Read the start of the update
r.readListStart();
// Read the subscriptions, rejecting duplicates
List<Group> groups = new ArrayList<Group>();
Set<GroupId> ids = new HashSet<GroupId>();
r.readListStart();
for(int i = 0; i < MAX_SUBSCRIPTIONS && !r.hasListEnd(); i++) {
Group g = groupReader.readStruct(r);
Group g = groupReader.readObject(r);
if(!ids.add(g.getId())) throw new FormatException(); // Duplicate
groups.add(g);
}
@@ -47,8 +46,8 @@ class SubscriptionUpdateReader implements StructReader<SubscriptionUpdate> {
// Read the version number
long version = r.readInteger();
if(version < 0) throw new FormatException();
// Read the end of the struct
r.readStructEnd();
// Read the end of the update
r.readListEnd();
// Reset the reader
r.removeConsumer(counting);
// Build and return the subscription update

View File

@@ -0,0 +1,21 @@
package org.briarproject.serial;
interface ObjectTypes {
byte NULL = 0x00;
byte BOOLEAN = 0x11;
byte INT_8 = 0x21;
byte INT_16 = 0x22;
byte INT_32 = 0x24;
byte INT_64 = 0x28;
byte FLOAT_64 = 0x38;
byte STRING_8 = 0x41;
byte STRING_16 = 0x42;
byte STRING_32 = 0x44;
byte RAW_8 = 0x51;
byte RAW_16 = 0x52;
byte RAW_32 = 0x54;
byte LIST = 0x60;
byte MAP = 0x70;
byte END = (byte) 0x80;
}

View File

@@ -1,23 +1,20 @@
package org.briarproject.serial;
import static org.briarproject.serial.Tag.BYTES_16;
import static org.briarproject.serial.Tag.BYTES_32;
import static org.briarproject.serial.Tag.BYTES_8;
import static org.briarproject.serial.Tag.END;
import static org.briarproject.serial.Tag.FALSE;
import static org.briarproject.serial.Tag.FLOAT;
import static org.briarproject.serial.Tag.INTEGER_16;
import static org.briarproject.serial.Tag.INTEGER_32;
import static org.briarproject.serial.Tag.INTEGER_64;
import static org.briarproject.serial.Tag.INTEGER_8;
import static org.briarproject.serial.Tag.LIST;
import static org.briarproject.serial.Tag.MAP;
import static org.briarproject.serial.Tag.NULL;
import static org.briarproject.serial.Tag.STRING_16;
import static org.briarproject.serial.Tag.STRING_32;
import static org.briarproject.serial.Tag.STRING_8;
import static org.briarproject.serial.Tag.STRUCT;
import static org.briarproject.serial.Tag.TRUE;
import static org.briarproject.serial.ObjectTypes.END;
import static org.briarproject.serial.ObjectTypes.FLOAT_64;
import static org.briarproject.serial.ObjectTypes.INT_16;
import static org.briarproject.serial.ObjectTypes.INT_32;
import static org.briarproject.serial.ObjectTypes.INT_64;
import static org.briarproject.serial.ObjectTypes.INT_8;
import static org.briarproject.serial.ObjectTypes.LIST;
import static org.briarproject.serial.ObjectTypes.MAP;
import static org.briarproject.serial.ObjectTypes.NULL;
import static org.briarproject.serial.ObjectTypes.RAW_16;
import static org.briarproject.serial.ObjectTypes.RAW_32;
import static org.briarproject.serial.ObjectTypes.RAW_8;
import static org.briarproject.serial.ObjectTypes.STRING_16;
import static org.briarproject.serial.ObjectTypes.STRING_32;
import static org.briarproject.serial.ObjectTypes.STRING_8;
import java.io.IOException;
import java.io.InputStream;
@@ -37,7 +34,7 @@ class ReaderImpl implements Reader {
private final Collection<Consumer> consumers = new ArrayList<Consumer>(0);
private boolean hasLookahead = false, eof = false;
private byte next, nextStructId;
private byte next;
private byte[] buf = new byte[8];
ReaderImpl(InputStream in) {
@@ -54,21 +51,12 @@ class ReaderImpl implements Reader {
return;
}
next = (byte) i;
// If necessary, read another lookahead byte
if(next == STRUCT) {
i = in.read();
if(i == -1) throw new FormatException();
nextStructId = (byte) i;
}
hasLookahead = true;
}
private void consumeLookahead() throws IOException {
assert hasLookahead;
for(Consumer c : consumers) {
c.write(next);
if(next == STRUCT) c.write(nextStructId);
}
for(Consumer c : consumers) c.write(next);
hasLookahead = false;
}
@@ -101,11 +89,10 @@ class ReaderImpl implements Reader {
if(hasBoolean()) skipBoolean();
else if(hasInteger()) skipInteger();
else if(hasFloat()) skipFloat();
else if(hasString()) skipString(Integer.MAX_VALUE);
else if(hasBytes()) skipBytes(Integer.MAX_VALUE);
else if(hasString()) skipString();
else if(hasBytes()) skipBytes();
else if(hasList()) skipList();
else if(hasMap()) skipMap();
else if(hasStruct()) skipStruct();
else if(hasNull()) skipNull();
else throw new FormatException();
}
@@ -127,36 +114,59 @@ class ReaderImpl implements Reader {
if(!consumers.remove(c)) throw new IllegalArgumentException();
}
public boolean hasNull() throws IOException {
if(!hasLookahead) readLookahead();
if(eof) return false;
return next == NULL;
}
public void readNull() throws IOException {
if(!hasNull()) throw new FormatException();
consumeLookahead();
}
public void skipNull() throws IOException {
if(!hasNull()) throw new FormatException();
hasLookahead = false;
}
public boolean hasBoolean() throws IOException {
if(!hasLookahead) readLookahead();
if(eof) return false;
return next == FALSE || next == TRUE;
return next == ObjectTypes.BOOLEAN;
}
public boolean readBoolean() throws IOException {
if(!hasBoolean()) throw new FormatException();
consumeLookahead();
return next == TRUE;
return readBoolean(true);
}
private boolean readBoolean(boolean consume) throws IOException {
readIntoBuffer(1, consume);
if(buf[0] != 0 && buf[0] != 1) throw new FormatException();
return buf[0] == 1;
}
public void skipBoolean() throws IOException {
if(!hasBoolean()) throw new FormatException();
skip(1);
hasLookahead = false;
}
public boolean hasInteger() throws IOException {
if(!hasLookahead) readLookahead();
if(eof) return false;
return next == INTEGER_8 || next == INTEGER_16 || next == INTEGER_32 ||
next == INTEGER_64;
return next == INT_8 || next == INT_16 || next == INT_32 ||
next == INT_64;
}
public long readInteger() throws IOException {
if(!hasInteger()) throw new FormatException();
consumeLookahead();
if(next == INTEGER_8) return readInt8(true);
if(next == INTEGER_16) return readInt16(true);
if(next == INTEGER_32) return readInt32(true);
if(next == INT_8) return readInt8(true);
if(next == INT_16) return readInt16(true);
if(next == INT_32) return readInt32(true);
return readInt64(true);
}
@@ -193,9 +203,9 @@ class ReaderImpl implements Reader {
public void skipInteger() throws IOException {
if(!hasInteger()) throw new FormatException();
if(next == INTEGER_8) skip(1);
else if(next == INTEGER_16) skip(2);
else if(next == INTEGER_32) skip(4);
if(next == INT_8) skip(1);
else if(next == INT_16) skip(2);
else if(next == INT_32) skip(4);
else skip(8);
hasLookahead = false;
}
@@ -203,7 +213,7 @@ class ReaderImpl implements Reader {
public boolean hasFloat() throws IOException {
if(!hasLookahead) readLookahead();
if(eof) return false;
return next == FLOAT;
return next == FLOAT_64;
}
public double readFloat() throws IOException {
@@ -244,10 +254,10 @@ class ReaderImpl implements Reader {
throw new FormatException();
}
public void skipString(int maxLength) throws IOException {
public void skipString() throws IOException {
if(!hasString()) throw new FormatException();
int length = readStringLength(false);
if(length < 0 || length > maxLength) throw new FormatException();
if(length < 0) throw new FormatException();
skip(length);
hasLookahead = false;
}
@@ -255,7 +265,7 @@ class ReaderImpl implements Reader {
public boolean hasBytes() throws IOException {
if(!hasLookahead) readLookahead();
if(eof) return false;
return next == BYTES_8 || next == BYTES_16 || next == BYTES_32;
return next == RAW_8 || next == RAW_16 || next == RAW_32;
}
public byte[] readBytes(int maxLength) throws IOException {
@@ -270,16 +280,16 @@ class ReaderImpl implements Reader {
}
private int readBytesLength(boolean consume) throws IOException {
if(next == BYTES_8) return readInt8(consume);
if(next == BYTES_16) return readInt16(consume);
if(next == BYTES_32) return readInt32(consume);
if(next == RAW_8) return readInt8(consume);
if(next == RAW_16) return readInt16(consume);
if(next == RAW_32) return readInt32(consume);
throw new FormatException();
}
public void skipBytes(int maxLength) throws IOException {
public void skipBytes() throws IOException {
if(!hasBytes()) throw new FormatException();
int length = readBytesLength(false);
if(length < 0 || length > maxLength) throw new FormatException();
if(length < 0) throw new FormatException();
skip(length);
hasLookahead = false;
}
@@ -349,53 +359,4 @@ class ReaderImpl implements Reader {
}
hasLookahead = false;
}
public boolean hasStruct() throws IOException {
if(!hasLookahead) readLookahead();
if(eof) return false;
return next == STRUCT;
}
public boolean hasStruct(int id) throws IOException {
if(id < 0 || id > 255) throw new IllegalArgumentException();
if(!hasLookahead) readLookahead();
if(eof) return false;
return next == STRUCT && (nextStructId & 0xFF) == id;
}
public void readStructStart(int id) throws IOException {
if(!hasStruct(id)) throw new FormatException();
consumeLookahead();
}
public boolean hasStructEnd() throws IOException {
return hasEnd();
}
public void readStructEnd() throws IOException {
readEnd();
}
public void skipStruct() throws IOException {
if(!hasStruct()) throw new FormatException();
hasLookahead = false;
while(!hasStructEnd()) skipObject();
hasLookahead = false;
}
public boolean hasNull() throws IOException {
if(!hasLookahead) readLookahead();
if(eof) return false;
return next == NULL;
}
public void readNull() throws IOException {
if(!hasNull()) throw new FormatException();
consumeLookahead();
}
public void skipNull() throws IOException {
if(!hasNull()) throw new FormatException();
hasLookahead = false;
}
}

View File

@@ -15,16 +15,6 @@ class SerialComponentImpl implements SerialComponent {
return 1;
}
public int getSerialisedStructStartLength(int id) {
// STRUCT tag, 8-bit ID
return 2;
}
public int getSerialisedStructEndLength() {
// END tag
return 1;
}
public int getSerialisedUniqueIdLength() {
// BYTES_8, BYTES_16 or BYTES_32 tag, length, bytes
return 1 + getLengthBytes(UniqueId.LENGTH) + UniqueId.LENGTH;

View File

@@ -1,23 +0,0 @@
package org.briarproject.serial;
interface Tag {
byte FALSE = 0x00;
byte TRUE = 0x01;
byte INTEGER_8 = 0x02;
byte INTEGER_16 = 0x03;
byte INTEGER_32 = 0x04;
byte INTEGER_64 = 0x05;
byte FLOAT = 0x06;
byte STRING_8 = 0x07;
byte STRING_16 = 0x08;
byte STRING_32 = 0x09;
byte BYTES_8 = 0x0A;
byte BYTES_16 = 0x0B;
byte BYTES_32 = 0x0C;
byte LIST = 0x0D;
byte MAP = 0x0E;
byte STRUCT = 0x0F;
byte END = 0x10;
byte NULL = 0x11;
}

View File

@@ -1,18 +1,21 @@
package org.briarproject.serial;
import static org.briarproject.serial.Tag.BYTES_16;
import static org.briarproject.serial.Tag.BYTES_32;
import static org.briarproject.serial.Tag.BYTES_8;
import static org.briarproject.serial.Tag.FALSE;
import static org.briarproject.serial.Tag.FLOAT;
import static org.briarproject.serial.Tag.INTEGER_16;
import static org.briarproject.serial.Tag.INTEGER_32;
import static org.briarproject.serial.Tag.INTEGER_64;
import static org.briarproject.serial.Tag.INTEGER_8;
import static org.briarproject.serial.Tag.STRING_16;
import static org.briarproject.serial.Tag.STRING_32;
import static org.briarproject.serial.Tag.STRING_8;
import static org.briarproject.serial.Tag.TRUE;
import static org.briarproject.serial.ObjectTypes.BOOLEAN;
import static org.briarproject.serial.ObjectTypes.END;
import static org.briarproject.serial.ObjectTypes.FLOAT_64;
import static org.briarproject.serial.ObjectTypes.INT_16;
import static org.briarproject.serial.ObjectTypes.INT_32;
import static org.briarproject.serial.ObjectTypes.INT_64;
import static org.briarproject.serial.ObjectTypes.INT_8;
import static org.briarproject.serial.ObjectTypes.LIST;
import static org.briarproject.serial.ObjectTypes.MAP;
import static org.briarproject.serial.ObjectTypes.NULL;
import static org.briarproject.serial.ObjectTypes.RAW_16;
import static org.briarproject.serial.ObjectTypes.RAW_32;
import static org.briarproject.serial.ObjectTypes.RAW_8;
import static org.briarproject.serial.ObjectTypes.STRING_16;
import static org.briarproject.serial.ObjectTypes.STRING_32;
import static org.briarproject.serial.ObjectTypes.STRING_8;
import java.io.IOException;
import java.io.OutputStream;
@@ -52,23 +55,28 @@ class WriterImpl implements Writer {
if(!consumers.remove(c)) throw new IllegalArgumentException();
}
public void writeNull() throws IOException {
write(NULL);
}
public void writeBoolean(boolean b) throws IOException {
if(b) write(TRUE);
else write(FALSE);
write(BOOLEAN);
if(b) write((byte) 1);
else write((byte) 0);
}
public void writeInteger(long i) throws IOException {
if(i >= Byte.MIN_VALUE && i <= Byte.MAX_VALUE) {
write(INTEGER_8);
write(INT_8);
write((byte) i);
} else if(i >= Short.MIN_VALUE && i <= Short.MAX_VALUE) {
write(INTEGER_16);
write(INT_16);
writeInt16((short) i);
} else if(i >= Integer.MIN_VALUE && i <= Integer.MAX_VALUE) {
write(INTEGER_32);
write(INT_32);
writeInt32((int) i);
} else {
write(INTEGER_64);
write(INT_64);
writeInt64(i);
}
}
@@ -97,7 +105,7 @@ class WriterImpl implements Writer {
}
public void writeFloat(double d) throws IOException {
write(FLOAT);
write(FLOAT_64);
writeInt64(Double.doubleToRawLongBits(d));
}
@@ -118,22 +126,22 @@ class WriterImpl implements Writer {
public void writeBytes(byte[] b) throws IOException {
if(b.length <= Byte.MAX_VALUE) {
write(BYTES_8);
write(RAW_8);
write((byte) b.length);
} else if(b.length <= Short.MAX_VALUE) {
write(BYTES_16);
write(RAW_16);
writeInt16((short) b.length);
} else {
write(BYTES_32);
write(RAW_32);
writeInt32(b.length);
}
write(b);
}
public void writeList(Collection<?> c) throws IOException {
write(Tag.LIST);
write(ObjectTypes.LIST);
for(Object o : c) writeObject(o);
write(Tag.END);
write(ObjectTypes.END);
}
private void writeObject(Object o) throws IOException {
@@ -154,42 +162,28 @@ class WriterImpl implements Writer {
}
public void writeListStart() throws IOException {
write(Tag.LIST);
write(LIST);
}
public void writeListEnd() throws IOException {
write(Tag.END);
write(END);
}
public void writeMap(Map<?, ?> m) throws IOException {
write(Tag.MAP);
write(MAP);
for(Entry<?, ?> e : m.entrySet()) {
writeObject(e.getKey());
writeObject(e.getValue());
}
write(Tag.END);
write(END);
}
public void writeMapStart() throws IOException {
write(Tag.MAP);
write(MAP);
}
public void writeMapEnd() throws IOException {
write(Tag.END);
}
public void writeStructStart(int id) throws IOException {
if(id < 0 || id > 255) throw new IllegalArgumentException();
write(Tag.STRUCT);
write((byte) id);
}
public void writeStructEnd() throws IOException {
write(Tag.END);
}
public void writeNull() throws IOException {
write(Tag.NULL);
write(END);
}
private void write(byte b) throws IOException {