Read and write offers. Mostly boilerplate.

This commit is contained in:
akwizgran
2011-07-26 22:00:39 +01:00
parent 9e78837055
commit 0cf8477504
12 changed files with 156 additions and 16 deletions

View File

@@ -6,6 +6,7 @@ import java.util.Collection;
import net.sf.briar.api.protocol.Ack;
import net.sf.briar.api.protocol.BatchId;
import net.sf.briar.api.protocol.Tags;
import net.sf.briar.api.serial.Consumer;
import net.sf.briar.api.serial.ObjectReader;
import net.sf.briar.api.serial.Reader;
@@ -24,7 +25,7 @@ class AckReader implements ObjectReader<Ack> {
public Ack readObject(Reader r) throws IOException {
// Initialise the consumer
CountingConsumer counting = new CountingConsumer(Ack.MAX_SIZE);
Consumer counting = new CountingConsumer(Ack.MAX_SIZE);
// Read and digest the data
r.addConsumer(counting);
r.readUserDefinedTag(Tags.ACK);

View File

@@ -9,6 +9,7 @@ import net.sf.briar.api.protocol.Batch;
import net.sf.briar.api.protocol.BatchId;
import net.sf.briar.api.protocol.Message;
import net.sf.briar.api.protocol.Tags;
import net.sf.briar.api.serial.Consumer;
import net.sf.briar.api.serial.ObjectReader;
import net.sf.briar.api.serial.Reader;
@@ -30,7 +31,7 @@ class BatchReader implements ObjectReader<Batch> {
public Batch readObject(Reader r) throws IOException {
// Initialise the consumers
CountingConsumer counting = new CountingConsumer(Batch.MAX_SIZE);
Consumer counting = new CountingConsumer(Batch.MAX_SIZE);
DigestingConsumer digesting = new DigestingConsumer(messageDigest);
messageDigest.reset();
// Read and digest the data

View File

@@ -0,0 +1,20 @@
package net.sf.briar.protocol;
import java.io.IOException;
import net.sf.briar.api.protocol.MessageId;
import net.sf.briar.api.protocol.Tags;
import net.sf.briar.api.protocol.UniqueId;
import net.sf.briar.api.serial.FormatException;
import net.sf.briar.api.serial.ObjectReader;
import net.sf.briar.api.serial.Reader;
class MessageIdReader implements ObjectReader<MessageId> {
public MessageId readObject(Reader r) throws IOException {
r.readUserDefinedTag(Tags.MESSAGE_ID);
byte[] b = r.readBytes();
if(b.length != UniqueId.LENGTH) throw new FormatException();
return new MessageId(b);
}
}

View File

@@ -14,7 +14,6 @@ import net.sf.briar.api.protocol.Group;
import net.sf.briar.api.protocol.Message;
import net.sf.briar.api.protocol.MessageId;
import net.sf.briar.api.protocol.Tags;
import net.sf.briar.api.protocol.UniqueId;
import net.sf.briar.api.serial.FormatException;
import net.sf.briar.api.serial.ObjectReader;
import net.sf.briar.api.serial.Reader;
@@ -23,6 +22,7 @@ import com.google.inject.Inject;
class MessageReader implements ObjectReader<Message> {
private final ObjectReader<MessageId> messageIdReader;
private final ObjectReader<Group> groupReader;
private final ObjectReader<Author> authorReader;
private final KeyParser keyParser;
@@ -30,8 +30,11 @@ class MessageReader implements ObjectReader<Message> {
private final MessageDigest messageDigest;
@Inject
MessageReader(CryptoComponent crypto, ObjectReader<Group> groupReader,
MessageReader(CryptoComponent crypto,
ObjectReader<MessageId> messageIdReader,
ObjectReader<Group> groupReader,
ObjectReader<Author> authorReader) {
this.messageIdReader = messageIdReader;
this.groupReader = groupReader;
this.authorReader = authorReader;
keyParser = crypto.getKeyParser();
@@ -47,10 +50,9 @@ class MessageReader implements ObjectReader<Message> {
// Read the initial tag
r.readUserDefinedTag(Tags.MESSAGE);
// Read the parent's message ID
r.readUserDefinedTag(Tags.MESSAGE_ID);
byte[] b = r.readBytes();
if(b.length != UniqueId.LENGTH) throw new FormatException();
MessageId parent = new MessageId(b);
r.addObjectReader(Tags.MESSAGE_ID, messageIdReader);
MessageId parent = r.readUserDefined(Tags.MESSAGE_ID, MessageId.class);
r.removeObjectReader(Tags.MESSAGE_ID);
// Read the group
r.addObjectReader(Tags.GROUP, groupReader);
Group group = r.readUserDefined(Tags.GROUP, Group.class);

View File

@@ -0,0 +1,11 @@
package net.sf.briar.protocol;
import java.util.Collection;
import net.sf.briar.api.protocol.MessageId;
import net.sf.briar.api.protocol.Offer;
interface OfferFactory {
Offer createOffer(Collection<MessageId> messages);
}

View File

@@ -0,0 +1,13 @@
package net.sf.briar.protocol;
import java.util.Collection;
import net.sf.briar.api.protocol.MessageId;
import net.sf.briar.api.protocol.Offer;
class OfferFactoryImpl implements OfferFactory {
public Offer createOffer(Collection<MessageId> messages) {
return new OfferImpl(messages);
}
}

View File

@@ -0,0 +1,19 @@
package net.sf.briar.protocol;
import java.util.Collection;
import net.sf.briar.api.protocol.MessageId;
import net.sf.briar.api.protocol.Offer;
class OfferImpl implements Offer {
private final Collection<MessageId> messages;
OfferImpl(Collection<MessageId> messages) {
this.messages = messages;
}
public Collection<MessageId> getMessages() {
return messages;
}
}

View File

@@ -0,0 +1,40 @@
package net.sf.briar.protocol;
import java.io.IOException;
import java.util.Collection;
import net.sf.briar.api.protocol.MessageId;
import net.sf.briar.api.protocol.Offer;
import net.sf.briar.api.protocol.Tags;
import net.sf.briar.api.serial.Consumer;
import net.sf.briar.api.serial.ObjectReader;
import net.sf.briar.api.serial.Reader;
import com.google.inject.Inject;
class OfferReader implements ObjectReader<Offer> {
private final ObjectReader<MessageId> messageIdReader;
private final OfferFactory offerFactory;
@Inject
OfferReader(ObjectReader<MessageId> messageIdReader,
OfferFactory offerFactory) {
this.messageIdReader = messageIdReader;
this.offerFactory = offerFactory;
}
public Offer readObject(Reader r) throws IOException {
// Initialise the consumer
Consumer counting = new CountingConsumer(Offer.MAX_SIZE);
// Read the data
r.addConsumer(counting);
r.readUserDefinedTag(Tags.OFFER);
r.addObjectReader(Tags.MESSAGE_ID, messageIdReader);
Collection<MessageId> messages = r.readList(MessageId.class);
r.removeObjectReader(Tags.MESSAGE_ID);
r.removeConsumer(counting);
// Build and return the offer
return offerFactory.createOffer(messages);
}
}

View File

@@ -8,6 +8,7 @@ import net.sf.briar.api.protocol.Group;
import net.sf.briar.api.protocol.GroupFactory;
import net.sf.briar.api.protocol.Message;
import net.sf.briar.api.protocol.MessageEncoder;
import net.sf.briar.api.protocol.MessageId;
import net.sf.briar.api.serial.ObjectReader;
import com.google.inject.AbstractModule;
@@ -21,6 +22,7 @@ public class ProtocolModule extends AbstractModule {
bind(AuthorFactory.class).to(AuthorFactoryImpl.class);
bind(BatchFactory.class).to(BatchFactoryImpl.class);
bind(GroupFactory.class).to(GroupFactoryImpl.class);
bind(OfferFactory.class).to(OfferFactoryImpl.class);
bind(SubscriptionFactory.class).to(SubscriptionFactoryImpl.class);
bind(TransportFactory.class).to(TransportFactoryImpl.class);
bind(MessageEncoder.class).to(MessageEncoderImpl.class);
@@ -31,6 +33,11 @@ public class ProtocolModule extends AbstractModule {
return new BatchIdReader();
}
@Provides
ObjectReader<MessageId> getMessageIdReader() {
return new MessageIdReader();
}
@Provides
ObjectReader<Group> getGroupReader(CryptoComponent crypto,
GroupFactory groupFactory) {
@@ -45,8 +52,10 @@ public class ProtocolModule extends AbstractModule {
@Provides
ObjectReader<Message> getMessageReader(CryptoComponent crypto,
ObjectReader<MessageId> messageIdReader,
ObjectReader<Group> groupReader,
ObjectReader<Author> authorReader) {
return new MessageReader(crypto, groupReader, authorReader);
return new MessageReader(crypto, messageIdReader, groupReader,
authorReader);
}
}

View File

@@ -6,6 +6,7 @@ import java.util.Collection;
import net.sf.briar.api.protocol.Group;
import net.sf.briar.api.protocol.Subscriptions;
import net.sf.briar.api.protocol.Tags;
import net.sf.briar.api.serial.Consumer;
import net.sf.briar.api.serial.ObjectReader;
import net.sf.briar.api.serial.Reader;
@@ -25,8 +26,7 @@ class SubscriptionReader implements ObjectReader<Subscriptions> {
public Subscriptions readObject(Reader r) throws IOException {
// Initialise the consumer
CountingConsumer counting =
new CountingConsumer(Subscriptions.MAX_SIZE);
Consumer counting = new CountingConsumer(Subscriptions.MAX_SIZE);
// Read the data
r.addConsumer(counting);
r.readUserDefinedTag(Tags.SUBSCRIPTIONS);

View File

@@ -5,6 +5,7 @@ import java.util.Map;
import net.sf.briar.api.protocol.Tags;
import net.sf.briar.api.protocol.Transports;
import net.sf.briar.api.serial.Consumer;
import net.sf.briar.api.serial.ObjectReader;
import net.sf.briar.api.serial.Reader;
@@ -21,7 +22,7 @@ class TransportReader implements ObjectReader<Transports> {
public Transports readObject(Reader r) throws IOException {
// Initialise the consumer
CountingConsumer counting = new CountingConsumer(Transports.MAX_SIZE);
Consumer counting = new CountingConsumer(Transports.MAX_SIZE);
// Read the data
r.addConsumer(counting);
r.readUserDefinedTag(Tags.TRANSPORTS);