mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-14 03:39:05 +01:00
Implemented OfferWriter and RequestWriter, made all the writers
reusable (though not thread-safe), and guiced the readers.
This commit is contained in:
@@ -676,11 +676,10 @@ class ReadWriteLockDatabaseComponent<Txn> extends DatabaseComponentImpl<Txn> {
|
||||
try {
|
||||
subscriptionLock.readLock().lock();
|
||||
try {
|
||||
BitSet request;
|
||||
Collection<MessageId> offered = o.getMessages();
|
||||
BitSet request = new BitSet(offered.size());
|
||||
Txn txn = db.startTransaction();
|
||||
try {
|
||||
Collection<MessageId> offered = o.getMessages();
|
||||
request = new BitSet(offered.size());
|
||||
Iterator<MessageId> it = offered.iterator();
|
||||
for(int i = 0; it.hasNext(); i++) {
|
||||
// If the message is not in the database, or if
|
||||
@@ -694,7 +693,7 @@ class ReadWriteLockDatabaseComponent<Txn> extends DatabaseComponentImpl<Txn> {
|
||||
db.abortTransaction(txn);
|
||||
throw e;
|
||||
}
|
||||
r.writeBitmap(request);
|
||||
r.writeBitmap(request, offered.size());
|
||||
} finally {
|
||||
subscriptionLock.readLock().unlock();
|
||||
}
|
||||
|
||||
@@ -497,11 +497,10 @@ class SynchronizedDatabaseComponent<Txn> extends DatabaseComponentImpl<Txn> {
|
||||
synchronized(messageLock) {
|
||||
synchronized(messageStatusLock) {
|
||||
synchronized(subscriptionLock) {
|
||||
BitSet request;
|
||||
Collection<MessageId> offered = o.getMessages();
|
||||
BitSet request = new BitSet(offered.size());
|
||||
Txn txn = db.startTransaction();
|
||||
try {
|
||||
Collection<MessageId> offered = o.getMessages();
|
||||
request = new BitSet(offered.size());
|
||||
Iterator<MessageId> it = offered.iterator();
|
||||
for(int i = 0; it.hasNext(); i++) {
|
||||
// If the message is not in the database, or if
|
||||
@@ -515,7 +514,7 @@ class SynchronizedDatabaseComponent<Txn> extends DatabaseComponentImpl<Txn> {
|
||||
db.abortTransaction(txn);
|
||||
throw e;
|
||||
}
|
||||
r.writeBitmap(request);
|
||||
r.writeBitmap(request, offered.size());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,11 +9,14 @@ import net.sf.briar.api.protocol.Tags;
|
||||
import net.sf.briar.api.serial.ObjectReader;
|
||||
import net.sf.briar.api.serial.Reader;
|
||||
|
||||
import com.google.inject.Inject;
|
||||
|
||||
class AckReader implements ObjectReader<Ack> {
|
||||
|
||||
private final ObjectReader<BatchId> batchIdReader;
|
||||
private final AckFactory ackFactory;
|
||||
|
||||
@Inject
|
||||
AckReader(ObjectReader<BatchId> batchIdReader, AckFactory ackFactory) {
|
||||
this.batchIdReader = batchIdReader;
|
||||
this.ackFactory = ackFactory;
|
||||
|
||||
@@ -11,11 +11,14 @@ import net.sf.briar.api.protocol.Tags;
|
||||
import net.sf.briar.api.serial.ObjectReader;
|
||||
import net.sf.briar.api.serial.Reader;
|
||||
|
||||
import com.google.inject.Inject;
|
||||
|
||||
class AuthorReader implements ObjectReader<Author> {
|
||||
|
||||
private final MessageDigest messageDigest;
|
||||
private final AuthorFactory authorFactory;
|
||||
|
||||
@Inject
|
||||
AuthorReader(CryptoComponent crypto, AuthorFactory authorFactory) {
|
||||
messageDigest = crypto.getMessageDigest();
|
||||
this.authorFactory = authorFactory;
|
||||
|
||||
@@ -12,12 +12,15 @@ import net.sf.briar.api.protocol.Tags;
|
||||
import net.sf.briar.api.serial.ObjectReader;
|
||||
import net.sf.briar.api.serial.Reader;
|
||||
|
||||
import com.google.inject.Inject;
|
||||
|
||||
class BatchReader implements ObjectReader<Batch> {
|
||||
|
||||
private final MessageDigest messageDigest;
|
||||
private final ObjectReader<Message> messageReader;
|
||||
private final BatchFactory batchFactory;
|
||||
|
||||
@Inject
|
||||
BatchReader(CryptoComponent crypto, ObjectReader<Message> messageReader,
|
||||
BatchFactory batchFactory) {
|
||||
messageDigest = crypto.getMessageDigest();
|
||||
|
||||
@@ -11,11 +11,14 @@ import net.sf.briar.api.protocol.Tags;
|
||||
import net.sf.briar.api.serial.ObjectReader;
|
||||
import net.sf.briar.api.serial.Reader;
|
||||
|
||||
import com.google.inject.Inject;
|
||||
|
||||
class GroupReader implements ObjectReader<Group> {
|
||||
|
||||
private final MessageDigest messageDigest;
|
||||
private final GroupFactory groupFactory;
|
||||
|
||||
@Inject
|
||||
GroupReader(CryptoComponent crypto, GroupFactory groupFactory) {
|
||||
messageDigest = crypto.getMessageDigest();
|
||||
this.groupFactory = groupFactory;
|
||||
|
||||
@@ -19,6 +19,8 @@ import net.sf.briar.api.serial.FormatException;
|
||||
import net.sf.briar.api.serial.ObjectReader;
|
||||
import net.sf.briar.api.serial.Reader;
|
||||
|
||||
import com.google.inject.Inject;
|
||||
|
||||
class MessageReader implements ObjectReader<Message> {
|
||||
|
||||
private final ObjectReader<Group> groupReader;
|
||||
@@ -27,6 +29,7 @@ class MessageReader implements ObjectReader<Message> {
|
||||
private final Signature signature;
|
||||
private final MessageDigest messageDigest;
|
||||
|
||||
@Inject
|
||||
MessageReader(CryptoComponent crypto, ObjectReader<Group> groupReader,
|
||||
ObjectReader<Author> authorReader) {
|
||||
this.groupReader = groupReader;
|
||||
|
||||
@@ -1,10 +1,17 @@
|
||||
package net.sf.briar.protocol;
|
||||
|
||||
import net.sf.briar.api.crypto.CryptoComponent;
|
||||
import net.sf.briar.api.protocol.Author;
|
||||
import net.sf.briar.api.protocol.AuthorFactory;
|
||||
import net.sf.briar.api.protocol.BatchId;
|
||||
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.serial.ObjectReader;
|
||||
|
||||
import com.google.inject.AbstractModule;
|
||||
import com.google.inject.Provides;
|
||||
|
||||
public class ProtocolModule extends AbstractModule {
|
||||
|
||||
@@ -14,6 +21,32 @@ public class ProtocolModule extends AbstractModule {
|
||||
bind(AuthorFactory.class).to(AuthorFactoryImpl.class);
|
||||
bind(BatchFactory.class).to(BatchFactoryImpl.class);
|
||||
bind(GroupFactory.class).to(GroupFactoryImpl.class);
|
||||
bind(SubscriptionFactory.class).to(SubscriptionFactoryImpl.class);
|
||||
bind(TransportFactory.class).to(TransportFactoryImpl.class);
|
||||
bind(MessageEncoder.class).to(MessageEncoderImpl.class);
|
||||
}
|
||||
|
||||
@Provides
|
||||
ObjectReader<BatchId> getBatchIdReader() {
|
||||
return new BatchIdReader();
|
||||
}
|
||||
|
||||
@Provides
|
||||
ObjectReader<Group> getGroupReader(CryptoComponent crypto,
|
||||
GroupFactory groupFactory) {
|
||||
return new GroupReader(crypto, groupFactory);
|
||||
}
|
||||
|
||||
@Provides
|
||||
ObjectReader<Author> getAuthorReader(CryptoComponent crypto,
|
||||
AuthorFactory authorFactory) {
|
||||
return new AuthorReader(crypto, authorFactory);
|
||||
}
|
||||
|
||||
@Provides
|
||||
ObjectReader<Message> getMessageReader(CryptoComponent crypto,
|
||||
ObjectReader<Group> groupReader,
|
||||
ObjectReader<Author> authorReader) {
|
||||
return new MessageReader(crypto, groupReader, authorReader);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,11 +9,14 @@ import net.sf.briar.api.protocol.Tags;
|
||||
import net.sf.briar.api.serial.ObjectReader;
|
||||
import net.sf.briar.api.serial.Reader;
|
||||
|
||||
import com.google.inject.Inject;
|
||||
|
||||
class SubscriptionReader implements ObjectReader<Subscriptions> {
|
||||
|
||||
private final ObjectReader<Group> groupReader;
|
||||
private final SubscriptionFactory subscriptionFactory;
|
||||
|
||||
@Inject
|
||||
SubscriptionReader(ObjectReader<Group> groupReader,
|
||||
SubscriptionFactory subscriptionFactory) {
|
||||
this.groupReader = groupReader;
|
||||
|
||||
@@ -8,10 +8,13 @@ import net.sf.briar.api.protocol.Transports;
|
||||
import net.sf.briar.api.serial.ObjectReader;
|
||||
import net.sf.briar.api.serial.Reader;
|
||||
|
||||
import com.google.inject.Inject;
|
||||
|
||||
class TransportReader implements ObjectReader<Transports> {
|
||||
|
||||
private final TransportFactory transportFactory;
|
||||
|
||||
@Inject
|
||||
TransportReader(TransportFactory transportFactory) {
|
||||
this.transportFactory = transportFactory;
|
||||
}
|
||||
|
||||
@@ -15,7 +15,7 @@ class AckWriterImpl implements AckWriter {
|
||||
private final OutputStream out;
|
||||
private final Writer w;
|
||||
|
||||
private boolean started = false, finished = false;
|
||||
private boolean started = false;
|
||||
|
||||
AckWriterImpl(OutputStream out, WriterFactory writerFactory) {
|
||||
this.out = out;
|
||||
@@ -23,7 +23,6 @@ class AckWriterImpl implements AckWriter {
|
||||
}
|
||||
|
||||
public boolean writeBatchId(BatchId b) throws IOException {
|
||||
if(finished) throw new IllegalStateException();
|
||||
if(!started) {
|
||||
w.writeUserDefinedTag(Tags.ACK);
|
||||
w.writeListStart();
|
||||
@@ -36,7 +35,6 @@ class AckWriterImpl implements AckWriter {
|
||||
}
|
||||
|
||||
public void finish() throws IOException {
|
||||
if(finished) throw new IllegalStateException();
|
||||
if(!started) {
|
||||
w.writeUserDefinedTag(Tags.ACK);
|
||||
w.writeListStart();
|
||||
@@ -44,6 +42,6 @@ class AckWriterImpl implements AckWriter {
|
||||
}
|
||||
w.writeListEnd();
|
||||
out.flush();
|
||||
finished = true;
|
||||
started = false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,7 +18,7 @@ class BatchWriterImpl implements BatchWriter {
|
||||
private final Writer w;
|
||||
private final MessageDigest messageDigest;
|
||||
|
||||
private boolean started = false, finished = false;
|
||||
private boolean started = false;
|
||||
|
||||
BatchWriterImpl(OutputStream out, WriterFactory writerFactory,
|
||||
MessageDigest messageDigest) {
|
||||
@@ -32,7 +32,6 @@ class BatchWriterImpl implements BatchWriter {
|
||||
}
|
||||
|
||||
public boolean writeMessage(byte[] message) throws IOException {
|
||||
if(finished) throw new IllegalStateException();
|
||||
if(!started) {
|
||||
messageDigest.reset();
|
||||
w.writeUserDefinedTag(Tags.BATCH);
|
||||
@@ -47,7 +46,6 @@ class BatchWriterImpl implements BatchWriter {
|
||||
}
|
||||
|
||||
public BatchId finish() throws IOException {
|
||||
if(finished) throw new IllegalStateException();
|
||||
if(!started) {
|
||||
messageDigest.reset();
|
||||
w.writeUserDefinedTag(Tags.BATCH);
|
||||
@@ -56,7 +54,7 @@ class BatchWriterImpl implements BatchWriter {
|
||||
}
|
||||
w.writeListEnd();
|
||||
out.flush();
|
||||
finished = true;
|
||||
started = false;
|
||||
return new BatchId(messageDigest.digest());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,49 @@
|
||||
package net.sf.briar.protocol.writers;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
|
||||
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.protocol.writers.OfferWriter;
|
||||
import net.sf.briar.api.serial.Writer;
|
||||
import net.sf.briar.api.serial.WriterFactory;
|
||||
|
||||
class OfferWriterImpl implements OfferWriter {
|
||||
|
||||
private final OutputStream out;
|
||||
private final Writer w;
|
||||
|
||||
private boolean started = false, finished = false;
|
||||
|
||||
OfferWriterImpl(OutputStream out, WriterFactory writerFactory) {
|
||||
this.out = out;
|
||||
w = writerFactory.createWriter(out);
|
||||
}
|
||||
|
||||
public boolean writeMessageId(MessageId m) throws IOException {
|
||||
if(finished) throw new IllegalStateException();
|
||||
if(!started) {
|
||||
w.writeUserDefinedTag(Tags.OFFER);
|
||||
w.writeListStart();
|
||||
started = true;
|
||||
}
|
||||
int capacity = Offer.MAX_SIZE - (int) w.getBytesWritten() - 1;
|
||||
if(capacity < MessageId.SERIALISED_LENGTH) return false;
|
||||
m.writeTo(w);
|
||||
return true;
|
||||
}
|
||||
|
||||
public void finish() throws IOException {
|
||||
if(finished) throw new IllegalStateException();
|
||||
if(!started) {
|
||||
w.writeUserDefinedTag(Tags.OFFER);
|
||||
w.writeListStart();
|
||||
started = true;
|
||||
}
|
||||
w.writeListEnd();
|
||||
out.flush();
|
||||
finished = true;
|
||||
}
|
||||
}
|
||||
@@ -6,7 +6,9 @@ import java.security.MessageDigest;
|
||||
import net.sf.briar.api.crypto.CryptoComponent;
|
||||
import net.sf.briar.api.protocol.writers.AckWriter;
|
||||
import net.sf.briar.api.protocol.writers.BatchWriter;
|
||||
import net.sf.briar.api.protocol.writers.OfferWriter;
|
||||
import net.sf.briar.api.protocol.writers.PacketWriterFactory;
|
||||
import net.sf.briar.api.protocol.writers.RequestWriter;
|
||||
import net.sf.briar.api.protocol.writers.SubscriptionWriter;
|
||||
import net.sf.briar.api.protocol.writers.TransportWriter;
|
||||
import net.sf.briar.api.serial.WriterFactory;
|
||||
@@ -33,6 +35,14 @@ class PacketWriterFactoryImpl implements PacketWriterFactory {
|
||||
return new BatchWriterImpl(out, writerFactory, messageDigest);
|
||||
}
|
||||
|
||||
public OfferWriter createOfferWriter(OutputStream out) {
|
||||
return new OfferWriterImpl(out, writerFactory);
|
||||
}
|
||||
|
||||
public RequestWriter createRequestWriter(OutputStream out) {
|
||||
return new RequestWriterImpl(out, writerFactory);
|
||||
}
|
||||
|
||||
public SubscriptionWriter createSubscriptionWriter(OutputStream out) {
|
||||
return new SubscriptionWriterImpl(out, writerFactory);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
package net.sf.briar.protocol.writers;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.util.BitSet;
|
||||
|
||||
import net.sf.briar.api.protocol.Tags;
|
||||
import net.sf.briar.api.protocol.writers.RequestWriter;
|
||||
import net.sf.briar.api.serial.Writer;
|
||||
import net.sf.briar.api.serial.WriterFactory;
|
||||
|
||||
class RequestWriterImpl implements RequestWriter {
|
||||
|
||||
private final OutputStream out;
|
||||
private final Writer w;
|
||||
|
||||
RequestWriterImpl(OutputStream out, WriterFactory writerFactory) {
|
||||
this.out = out;
|
||||
w = writerFactory.createWriter(out);
|
||||
}
|
||||
|
||||
public void writeBitmap(BitSet b, int length) throws IOException {
|
||||
w.writeUserDefinedTag(Tags.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];
|
||||
// I'm kind of surprised BitSet doesn't have a method for this
|
||||
for(int i = 0; i < length; i++) {
|
||||
if(b.get(i)) {
|
||||
int offset = i / 8;
|
||||
byte bit = (byte) (128 >> i % 8);
|
||||
bitmap[offset] |= bit;
|
||||
}
|
||||
}
|
||||
w.writeBytes(bitmap);
|
||||
out.flush();
|
||||
}
|
||||
}
|
||||
@@ -15,19 +15,15 @@ class SubscriptionWriterImpl implements SubscriptionWriter {
|
||||
private final OutputStream out;
|
||||
private final Writer w;
|
||||
|
||||
private boolean used = false;
|
||||
|
||||
SubscriptionWriterImpl(OutputStream out, WriterFactory writerFactory) {
|
||||
this.out = out;
|
||||
w = writerFactory.createWriter(out);
|
||||
}
|
||||
|
||||
public void writeSubscriptions(Collection<Group> subs) throws IOException {
|
||||
if(used) throw new IllegalStateException();
|
||||
w.writeUserDefinedTag(Tags.SUBSCRIPTIONS);
|
||||
w.writeList(subs);
|
||||
w.writeInt64(System.currentTimeMillis());
|
||||
out.flush();
|
||||
used = true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,8 +14,6 @@ class TransportWriterImpl implements TransportWriter {
|
||||
private final OutputStream out;
|
||||
private final Writer w;
|
||||
|
||||
private boolean used = false;
|
||||
|
||||
TransportWriterImpl(OutputStream out, WriterFactory writerFactory) {
|
||||
this.out = out;
|
||||
w = writerFactory.createWriter(out);
|
||||
@@ -23,11 +21,9 @@ class TransportWriterImpl implements TransportWriter {
|
||||
|
||||
public void writeTransports(Map<String, String> transports)
|
||||
throws IOException {
|
||||
if(used) throw new IllegalStateException();
|
||||
w.writeUserDefinedTag(Tags.TRANSPORTS);
|
||||
w.writeMap(transports);
|
||||
w.writeInt64(System.currentTimeMillis());
|
||||
out.flush();
|
||||
used = true;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user