Renamed "user-defined types" as "structs" in the serialisation format.

This commit is contained in:
akwizgran
2011-12-02 11:36:45 +00:00
parent 2fb797a197
commit f7360cddde
33 changed files with 141 additions and 139 deletions

View File

@@ -30,7 +30,7 @@ class AckReader implements ObjectReader<Ack> {
new CountingConsumer(ProtocolConstants.MAX_PACKET_LENGTH);
// Read and digest the data
r.addConsumer(counting);
r.readUserDefinedId(Types.ACK);
r.readStructId(Types.ACK);
r.addObjectReader(Types.BATCH_ID, batchIdReader);
Collection<BatchId> batches = r.readList(BatchId.class);
r.removeObjectReader(Types.BATCH_ID);
@@ -42,7 +42,7 @@ class AckReader implements ObjectReader<Ack> {
private static class BatchIdReader implements ObjectReader<BatchId> {
public BatchId readObject(Reader r) throws IOException {
r.readUserDefinedId(Types.BATCH_ID);
r.readStructId(Types.BATCH_ID);
byte[] b = r.readBytes(UniqueId.LENGTH);
if(b.length != UniqueId.LENGTH) throw new FormatException();
return new BatchId(b);

View File

@@ -29,7 +29,7 @@ class AuthorFactoryImpl implements AuthorFactory {
throws IOException {
ByteArrayOutputStream out = new ByteArrayOutputStream();
Writer w = writerFactory.createWriter(out);
w.writeUserDefinedId(Types.AUTHOR);
w.writeStructId(Types.AUTHOR);
w.writeString(name);
w.writeBytes(publicKey);
MessageDigest messageDigest = crypto.getMessageDigest();

View File

@@ -29,7 +29,7 @@ class AuthorReader implements ObjectReader<Author> {
messageDigest.reset();
// Read and digest the data
r.addConsumer(digesting);
r.readUserDefinedId(Types.AUTHOR);
r.readStructId(Types.AUTHOR);
String name = r.readString(ProtocolConstants.MAX_AUTHOR_NAME_LENGTH);
byte[] publicKey = r.readBytes(ProtocolConstants.MAX_PUBLIC_KEY_LENGTH);
r.removeConsumer(digesting);

View File

@@ -38,7 +38,7 @@ class BatchReader implements ObjectReader<Batch> {
// Read and digest the data
r.addConsumer(counting);
r.addConsumer(digesting);
r.readUserDefinedId(Types.BATCH);
r.readStructId(Types.BATCH);
r.addObjectReader(Types.MESSAGE, messageReader);
List<Message> messages = r.readList(Message.class);
r.removeObjectReader(Types.MESSAGE);

View File

@@ -28,7 +28,7 @@ class GroupFactoryImpl implements GroupFactory {
public Group createGroup(String name, byte[] publicKey) throws IOException {
ByteArrayOutputStream out = new ByteArrayOutputStream();
Writer w = writerFactory.createWriter(out);
w.writeUserDefinedId(Types.GROUP);
w.writeStructId(Types.GROUP);
w.writeString(name);
if(publicKey == null) w.writeNull();
else w.writeBytes(publicKey);

View File

@@ -29,7 +29,7 @@ class GroupReader implements ObjectReader<Group> {
messageDigest.reset();
// Read and digest the data
r.addConsumer(digesting);
r.readUserDefinedId(Types.GROUP);
r.readStructId(Types.GROUP);
String name = r.readString(ProtocolConstants.MAX_GROUP_NAME_LENGTH);
byte[] publicKey = null;
if(r.hasNull()) r.readNull();

View File

@@ -107,7 +107,7 @@ class MessageFactoryImpl implements MessageFactory {
w.addConsumer(groupConsumer);
}
// Write the message
w.writeUserDefinedId(Types.MESSAGE);
w.writeStructId(Types.MESSAGE);
if(parent == null) w.writeNull();
else w.writeBytes(parent.getBytes());
if(group == null) w.writeNull();
@@ -153,7 +153,7 @@ class MessageFactoryImpl implements MessageFactory {
}
private void writeGroup(Writer w, Group g) throws IOException {
w.writeUserDefinedId(Types.GROUP);
w.writeStructId(Types.GROUP);
w.writeString(g.getName());
byte[] publicKey = g.getPublicKey();
if(publicKey == null) w.writeNull();
@@ -161,7 +161,7 @@ class MessageFactoryImpl implements MessageFactory {
}
private void writeAuthor(Writer w, Author a) throws IOException {
w.writeUserDefinedId(Types.AUTHOR);
w.writeStructId(Types.AUTHOR);
w.writeString(a.getName());
w.writeBytes(a.getPublicKey());
}

View File

@@ -12,7 +12,7 @@ import net.sf.briar.api.serial.Reader;
class MessageIdReader implements ObjectReader<MessageId> {
public MessageId readObject(Reader r) throws IOException {
r.readUserDefinedId(Types.MESSAGE_ID);
r.readStructId(Types.MESSAGE_ID);
byte[] b = r.readBytes(UniqueId.LENGTH);
if(b.length != UniqueId.LENGTH) throw new FormatException();
return new MessageId(b);

View File

@@ -50,14 +50,14 @@ class MessageReader implements ObjectReader<Message> {
r.addConsumer(copying);
r.addConsumer(counting);
// Read the initial tag
r.readUserDefinedId(Types.MESSAGE);
r.readStructId(Types.MESSAGE);
// Read the parent's message ID, if there is one
MessageId parent = null;
if(r.hasNull()) {
r.readNull();
} else {
r.addObjectReader(Types.MESSAGE_ID, messageIdReader);
parent = r.readUserDefined(Types.MESSAGE_ID, MessageId.class);
parent = r.readStruct(Types.MESSAGE_ID, MessageId.class);
r.removeObjectReader(Types.MESSAGE_ID);
}
// Read the group, if there is one
@@ -66,7 +66,7 @@ class MessageReader implements ObjectReader<Message> {
r.readNull();
} else {
r.addObjectReader(Types.GROUP, groupReader);
group = r.readUserDefined(Types.GROUP, Group.class);
group = r.readStruct(Types.GROUP, Group.class);
r.removeObjectReader(Types.GROUP);
}
// Read the author, if there is one
@@ -75,7 +75,7 @@ class MessageReader implements ObjectReader<Message> {
r.readNull();
} else {
r.addObjectReader(Types.AUTHOR, authorReader);
author = r.readUserDefined(Types.AUTHOR, Author.class);
author = r.readStruct(Types.AUTHOR, Author.class);
r.removeObjectReader(Types.AUTHOR);
}
// Read the subject

View File

@@ -29,7 +29,7 @@ class OfferReader implements ObjectReader<Offer> {
new CountingConsumer(ProtocolConstants.MAX_PACKET_LENGTH);
// Read the data
r.addConsumer(counting);
r.readUserDefinedId(Types.OFFER);
r.readStructId(Types.OFFER);
r.addObjectReader(Types.MESSAGE_ID, messageIdReader);
Collection<MessageId> messages = r.readList(MessageId.class);
r.removeObjectReader(Types.MESSAGE_ID);

View File

@@ -39,51 +39,51 @@ class ProtocolReaderImpl implements ProtocolReader {
}
public boolean hasAck() throws IOException {
return reader.hasUserDefined(Types.ACK);
return reader.hasStruct(Types.ACK);
}
public Ack readAck() throws IOException {
return reader.readUserDefined(Types.ACK, Ack.class);
return reader.readStruct(Types.ACK, Ack.class);
}
public boolean hasBatch() throws IOException {
return reader.hasUserDefined(Types.BATCH);
return reader.hasStruct(Types.BATCH);
}
public Batch readBatch() throws IOException {
return reader.readUserDefined(Types.BATCH, Batch.class);
return reader.readStruct(Types.BATCH, Batch.class);
}
public boolean hasOffer() throws IOException {
return reader.hasUserDefined(Types.OFFER);
return reader.hasStruct(Types.OFFER);
}
public Offer readOffer() throws IOException {
return reader.readUserDefined(Types.OFFER, Offer.class);
return reader.readStruct(Types.OFFER, Offer.class);
}
public boolean hasRequest() throws IOException {
return reader.hasUserDefined(Types.REQUEST);
return reader.hasStruct(Types.REQUEST);
}
public Request readRequest() throws IOException {
return reader.readUserDefined(Types.REQUEST, Request.class);
return reader.readStruct(Types.REQUEST, Request.class);
}
public boolean hasSubscriptionUpdate() throws IOException {
return reader.hasUserDefined(Types.SUBSCRIPTION_UPDATE);
return reader.hasStruct(Types.SUBSCRIPTION_UPDATE);
}
public SubscriptionUpdate readSubscriptionUpdate() throws IOException {
return reader.readUserDefined(Types.SUBSCRIPTION_UPDATE,
return reader.readStruct(Types.SUBSCRIPTION_UPDATE,
SubscriptionUpdate.class);
}
public boolean hasTransportUpdate() throws IOException {
return reader.hasUserDefined(Types.TRANSPORT_UPDATE);
return reader.hasStruct(Types.TRANSPORT_UPDATE);
}
public TransportUpdate readTransportUpdate() throws IOException {
return reader.readUserDefined(Types.TRANSPORT_UPDATE, TransportUpdate.class);
return reader.readStruct(Types.TRANSPORT_UPDATE, TransportUpdate.class);
}
}

View File

@@ -25,7 +25,7 @@ class RequestReader implements ObjectReader<Request> {
new CountingConsumer(ProtocolConstants.MAX_PACKET_LENGTH);
// Read the data
r.addConsumer(counting);
r.readUserDefinedId(Types.REQUEST);
r.readStructId(Types.REQUEST);
byte[] bitmap = r.readBytes(ProtocolConstants.MAX_PACKET_LENGTH);
r.removeConsumer(counting);
// Convert the bitmap into a BitSet

View File

@@ -30,7 +30,7 @@ class SubscriptionUpdateReader implements ObjectReader<SubscriptionUpdate> {
new CountingConsumer(ProtocolConstants.MAX_PACKET_LENGTH);
// Read the data
r.addConsumer(counting);
r.readUserDefinedId(Types.SUBSCRIPTION_UPDATE);
r.readStructId(Types.SUBSCRIPTION_UPDATE);
r.addObjectReader(Types.GROUP, groupReader);
Map<Group, Long> subs = r.readMap(Group.class, Long.class);
r.removeObjectReader(Types.GROUP);

View File

@@ -35,7 +35,7 @@ class TransportUpdateReader implements ObjectReader<TransportUpdate> {
new CountingConsumer(ProtocolConstants.MAX_PACKET_LENGTH);
// Read the data
r.addConsumer(counting);
r.readUserDefinedId(Types.TRANSPORT_UPDATE);
r.readStructId(Types.TRANSPORT_UPDATE);
r.addObjectReader(Types.TRANSPORT, transportReader);
Collection<Transport> transports = r.readList(Transport.class);
r.removeObjectReader(Types.TRANSPORT);
@@ -58,7 +58,7 @@ class TransportUpdateReader implements ObjectReader<TransportUpdate> {
private static class TransportReader implements ObjectReader<Transport> {
public Transport readObject(Reader r) throws IOException {
r.readUserDefinedId(Types.TRANSPORT);
r.readStructId(Types.TRANSPORT);
// Read the ID
byte[] b = r.readBytes(UniqueId.LENGTH);
if(b.length != UniqueId.LENGTH) throw new FormatException();

View File

@@ -23,7 +23,7 @@ class AckWriterImpl implements AckWriter {
AckWriterImpl(OutputStream out, SerialComponent serial,
WriterFactory writerFactory) {
this.out = out;
headerLength = serial.getSerialisedUserDefinedIdLength(Types.ACK)
headerLength = serial.getSerialisedStructIdLength(Types.ACK)
+ serial.getSerialisedListStartLength();
idLength = serial.getSerialisedUniqueIdLength(Types.BATCH_ID);
footerLength = serial.getSerialisedListEndLength();
@@ -41,7 +41,7 @@ class AckWriterImpl implements AckWriter {
int overhead = started ? footerLength : headerLength + footerLength;
if(capacity < idLength + overhead) return false;
if(!started) start();
w.writeUserDefinedId(Types.BATCH_ID);
w.writeStructId(Types.BATCH_ID);
w.writeBytes(b.getBytes());
capacity -= idLength;
return true;
@@ -56,7 +56,7 @@ class AckWriterImpl implements AckWriter {
}
private void start() throws IOException {
w.writeUserDefinedId(Types.ACK);
w.writeStructId(Types.ACK);
w.writeListStart();
capacity -= headerLength;
started = true;

View File

@@ -28,7 +28,7 @@ class BatchWriterImpl implements BatchWriter {
BatchWriterImpl(OutputStream out, SerialComponent serial,
WriterFactory writerFactory, MessageDigest messageDigest) {
this.out = out;
headerLength = serial.getSerialisedUserDefinedIdLength(Types.BATCH)
headerLength = serial.getSerialisedStructIdLength(Types.BATCH)
+ serial.getSerialisedListStartLength();
footerLength = serial.getSerialisedListEndLength();
w = writerFactory.createWriter(this.out);
@@ -70,7 +70,7 @@ class BatchWriterImpl implements BatchWriter {
private void start() throws IOException {
messageDigest.reset();
w.addConsumer(digestingConsumer);
w.writeUserDefinedId(Types.BATCH);
w.writeStructId(Types.BATCH);
w.writeListStart();
remaining -= headerLength;
started = true;

View File

@@ -23,7 +23,7 @@ class OfferWriterImpl implements OfferWriter {
OfferWriterImpl(OutputStream out, SerialComponent serial,
WriterFactory writerFactory) {
this.out = out;
headerLength = serial.getSerialisedUserDefinedIdLength(Types.OFFER)
headerLength = serial.getSerialisedStructIdLength(Types.OFFER)
+ serial.getSerialisedListStartLength();
idLength = serial.getSerialisedUniqueIdLength(Types.MESSAGE_ID);
footerLength = serial.getSerialisedListEndLength();
@@ -41,7 +41,7 @@ class OfferWriterImpl implements OfferWriter {
int overhead = started ? footerLength : headerLength + footerLength;
if(capacity < idLength + overhead) return false;
if(!started) start();
w.writeUserDefinedId(Types.MESSAGE_ID);
w.writeStructId(Types.MESSAGE_ID);
w.writeBytes(m.getBytes());
capacity -= idLength;
return true;
@@ -56,7 +56,7 @@ class OfferWriterImpl implements OfferWriter {
}
private void start() throws IOException {
w.writeUserDefinedId(Types.OFFER);
w.writeStructId(Types.OFFER);
w.writeListStart();
capacity -= headerLength;
started = true;

View File

@@ -21,7 +21,7 @@ class RequestWriterImpl implements RequestWriter {
public void writeRequest(BitSet b, int length)
throws IOException {
w.writeUserDefinedId(Types.REQUEST);
w.writeStructId(Types.REQUEST);
// If the number of bits isn't a multiple of 8, round up to a byte
int bytes = length % 8 == 0 ? length / 8 : length / 8 + 1;
byte[] bitmap = new byte[bytes];

View File

@@ -24,7 +24,7 @@ class SubscriptionUpdateWriterImpl implements SubscriptionUpdateWriter {
public void writeSubscriptions(Map<Group, Long> subs, long timestamp)
throws IOException {
w.writeUserDefinedId(Types.SUBSCRIPTION_UPDATE);
w.writeStructId(Types.SUBSCRIPTION_UPDATE);
w.writeMapStart();
for(Entry<Group, Long> e : subs.entrySet()) {
writeGroup(w, e.getKey());
@@ -36,7 +36,7 @@ class SubscriptionUpdateWriterImpl implements SubscriptionUpdateWriter {
}
private void writeGroup(Writer w, Group g) throws IOException {
w.writeUserDefinedId(Types.GROUP);
w.writeStructId(Types.GROUP);
w.writeString(g.getName());
byte[] publicKey = g.getPublicKey();
if(publicKey == null) w.writeNull();

View File

@@ -22,10 +22,10 @@ class TransportUpdateWriterImpl implements TransportUpdateWriter {
public void writeTransports(Collection<Transport> transports,
long timestamp) throws IOException {
w.writeUserDefinedId(Types.TRANSPORT_UPDATE);
w.writeStructId(Types.TRANSPORT_UPDATE);
w.writeListStart();
for(Transport p : transports) {
w.writeUserDefinedId(Types.TRANSPORT);
w.writeStructId(Types.TRANSPORT);
w.writeBytes(p.getId().getBytes());
w.writeInt32(p.getIndex().getInt());
w.writeMap(p);

View File

@@ -51,7 +51,7 @@ class ReaderImpl implements Reader {
}
next = (byte) i;
// If necessary, read another lookahead byte
if(next == Tag.USER) {
if(next == Tag.STRUCT) {
i = in.read();
if(i == -1) throw new FormatException();
nextNext = (byte) i;
@@ -64,7 +64,7 @@ class ReaderImpl implements Reader {
assert hasLookahead;
for(Consumer c : consumers) {
c.write(next);
if(next == Tag.USER) c.write(nextNext);
if(next == Tag.STRUCT) c.write(nextNext);
}
hasLookahead = false;
}
@@ -376,7 +376,7 @@ class ReaderImpl implements Reader {
}
private Object readObject() throws IOException {
if(hasUserDefined()) return readUserDefined();
if(hasStruct()) return readStruct();
if(hasBoolean()) return Boolean.valueOf(readBoolean());
if(hasUint7()) return Byte.valueOf(readUint7());
if(hasInt8()) return Byte.valueOf(readInt8());
@@ -396,19 +396,19 @@ class ReaderImpl implements Reader {
throw new FormatException();
}
private boolean hasUserDefined() throws IOException {
private boolean hasStruct() throws IOException {
if(!hasLookahead) readLookahead(true);
if(eof) return false;
return next == Tag.USER
|| (next & Tag.SHORT_USER_MASK) == Tag.SHORT_USER;
return next == Tag.STRUCT
|| (next & Tag.SHORT_STRUCT_MASK) == Tag.SHORT_STRUCT;
}
private Object readUserDefined() throws IOException {
if(!hasUserDefined()) throw new FormatException();
int tag;
if(next == Tag.USER) tag = 0xFF & nextNext;
else tag = 0xFF & next ^ Tag.SHORT_USER;
return readUserDefined(tag, Object.class);
private Object readStruct() throws IOException {
if(!hasStruct()) throw new FormatException();
int id;
if(next == Tag.STRUCT) id = 0xFF & nextNext;
else id = 0xFF & next ^ Tag.SHORT_STRUCT;
return readStruct(id, Object.class);
}
private <T> T readObject(Class<T> t) throws IOException {
@@ -529,19 +529,19 @@ class ReaderImpl implements Reader {
consumeLookahead();
}
public boolean hasUserDefined(int id) throws IOException {
public boolean hasStruct(int id) throws IOException {
if(id < 0 || id > 255) throw new IllegalArgumentException();
if(!hasLookahead) readLookahead(true);
if(eof) return false;
if(next == Tag.USER)
if(next == Tag.STRUCT)
return id == (0xFF & nextNext);
else if((next & Tag.SHORT_USER_MASK) == Tag.SHORT_USER)
return id == (0xFF & next ^ Tag.SHORT_USER);
else if((next & Tag.SHORT_STRUCT_MASK) == Tag.SHORT_STRUCT)
return id == (0xFF & next ^ Tag.SHORT_STRUCT);
else return false;
}
public <T> T readUserDefined(int id, Class<T> t) throws IOException {
if(!hasUserDefined(id)) throw new FormatException();
public <T> T readStruct(int id, Class<T> t) throws IOException {
if(!hasStruct(id)) throw new FormatException();
if(id >= objectReaders.length) throw new FormatException();
ObjectReader<?> o = objectReaders[id];
if(o == null) throw new FormatException();
@@ -552,8 +552,8 @@ class ReaderImpl implements Reader {
}
}
public void readUserDefinedId(int id) throws IOException {
if(!hasUserDefined(id)) throw new FormatException();
public void readStructId(int id) throws IOException {
if(!hasStruct(id)) throw new FormatException();
consumeLookahead();
}
}

View File

@@ -6,16 +6,18 @@ import net.sf.briar.api.serial.SerialComponent;
class SerialComponentImpl implements SerialComponent {
public int getSerialisedListEndLength() {
// END tag
return 1;
}
public int getSerialisedListStartLength() {
// LIST_START tag
return 1;
}
public int getSerialisedUniqueIdLength(int id) {
// User-defined ID, BYTES tag, length spec, bytes
return getSerialisedUserDefinedIdLength(id) + 1
// Struct ID, BYTES tag, length spec, bytes
return getSerialisedStructIdLength(id) + 1
+ getSerialisedLengthSpecLength(UniqueId.LENGTH) + UniqueId.LENGTH;
}
@@ -26,7 +28,7 @@ class SerialComponentImpl implements SerialComponent {
return 5; // Int32
}
public int getSerialisedUserDefinedIdLength(int id) {
public int getSerialisedStructIdLength(int id) {
assert id >= 0 && id <= 255;
return id < 32 ? 1 : 2;
}

View File

@@ -18,7 +18,7 @@ interface Tag {
static final byte MAP_START = -14; // 1111 0010
static final byte END = -15; // 1111 0001
static final byte NULL = -16; // 1111 0000
static final byte USER = -17; // 1110 1111
static final byte STRUCT = -17; // 1110 1111
static final int SHORT_MASK = 0xF0; // Match first four bits
static final int SHORT_STRING = 0x80; // 1000 xxxx
@@ -26,6 +26,6 @@ interface Tag {
static final int SHORT_LIST = 0xA0; // 1010 xxxx
static final int SHORT_MAP = 0xB0; // 1011 xxxx
static final int SHORT_USER_MASK = 0xE0; // Match first three bits
static final int SHORT_USER = 0xC0; // 110x xxxx
static final int SHORT_STRUCT_MASK = 0xE0; // Match first three bits
static final int SHORT_STRUCT = 0xC0; // 110x xxxx
}

View File

@@ -187,12 +187,12 @@ class WriterImpl implements Writer {
write(Tag.NULL);
}
public void writeUserDefinedId(int id) throws IOException {
public void writeStructId(int id) throws IOException {
if(id < 0 || id > 255) throw new IllegalArgumentException();
if(id < 32) {
write((byte) (Tag.SHORT_USER | id));
write((byte) (Tag.SHORT_STRUCT | id));
} else {
write(Tag.USER);
write(Tag.STRUCT);
write((byte) id);
}
}