mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-15 20:29:52 +01:00
Each request packet should contain the unique ID of the offer to which
it responds.
This commit is contained in:
@@ -815,7 +815,7 @@ class ReadWriteLockDatabaseComponent<Txn> extends DatabaseComponentImpl<Txn> {
|
||||
db.abortTransaction(txn);
|
||||
throw e;
|
||||
}
|
||||
r.writeBitmap(request, offered.size());
|
||||
r.writeRequest(o.getId(), request, offered.size());
|
||||
} finally {
|
||||
subscriptionLock.readLock().unlock();
|
||||
}
|
||||
|
||||
@@ -627,7 +627,7 @@ class SynchronizedDatabaseComponent<Txn> extends DatabaseComponentImpl<Txn> {
|
||||
db.abortTransaction(txn);
|
||||
throw e;
|
||||
}
|
||||
r.writeBitmap(request, offered.size());
|
||||
r.writeRequest(o.getId(), request, offered.size());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,14 +10,11 @@ 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 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,14 +11,11 @@ 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;
|
||||
|
||||
@@ -13,15 +13,12 @@ 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 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,14 +11,11 @@ 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;
|
||||
|
||||
@@ -18,8 +18,6 @@ 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<MessageId> messageIdReader;
|
||||
@@ -29,7 +27,6 @@ class MessageReader implements ObjectReader<Message> {
|
||||
private final Signature signature;
|
||||
private final MessageDigest messageDigest;
|
||||
|
||||
@Inject
|
||||
MessageReader(CryptoComponent crypto,
|
||||
ObjectReader<MessageId> messageIdReader,
|
||||
ObjectReader<Group> groupReader,
|
||||
|
||||
@@ -4,8 +4,9 @@ import java.util.Collection;
|
||||
|
||||
import net.sf.briar.api.protocol.MessageId;
|
||||
import net.sf.briar.api.protocol.Offer;
|
||||
import net.sf.briar.api.protocol.OfferId;
|
||||
|
||||
interface OfferFactory {
|
||||
|
||||
Offer createOffer(Collection<MessageId> offered);
|
||||
Offer createOffer(OfferId id, Collection<MessageId> offered);
|
||||
}
|
||||
|
||||
@@ -4,10 +4,11 @@ import java.util.Collection;
|
||||
|
||||
import net.sf.briar.api.protocol.MessageId;
|
||||
import net.sf.briar.api.protocol.Offer;
|
||||
import net.sf.briar.api.protocol.OfferId;
|
||||
|
||||
class OfferFactoryImpl implements OfferFactory {
|
||||
|
||||
public Offer createOffer(Collection<MessageId> offered) {
|
||||
return new OfferImpl(offered);
|
||||
public Offer createOffer(OfferId id, Collection<MessageId> offered) {
|
||||
return new OfferImpl(id, offered);
|
||||
}
|
||||
}
|
||||
|
||||
20
components/net/sf/briar/protocol/OfferIdReader.java
Normal file
20
components/net/sf/briar/protocol/OfferIdReader.java
Normal file
@@ -0,0 +1,20 @@
|
||||
package net.sf.briar.protocol;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import net.sf.briar.api.protocol.OfferId;
|
||||
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 OfferIdReader implements ObjectReader<OfferId> {
|
||||
|
||||
public OfferId readObject(Reader r) throws IOException {
|
||||
r.readUserDefinedTag(Tags.OFFER_ID);
|
||||
byte[] b = r.readBytes(UniqueId.LENGTH);
|
||||
if(b.length != UniqueId.LENGTH) throw new FormatException();
|
||||
return new OfferId(b);
|
||||
}
|
||||
}
|
||||
@@ -4,15 +4,22 @@ import java.util.Collection;
|
||||
|
||||
import net.sf.briar.api.protocol.MessageId;
|
||||
import net.sf.briar.api.protocol.Offer;
|
||||
import net.sf.briar.api.protocol.OfferId;
|
||||
|
||||
class OfferImpl implements Offer {
|
||||
|
||||
private final OfferId id;
|
||||
private final Collection<MessageId> offered;
|
||||
|
||||
OfferImpl(Collection<MessageId> offered) {
|
||||
OfferImpl(OfferId id, Collection<MessageId> offered) {
|
||||
this.id = id;
|
||||
this.offered = offered;
|
||||
}
|
||||
|
||||
public OfferId getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public Collection<MessageId> getMessageIds() {
|
||||
return offered;
|
||||
}
|
||||
|
||||
@@ -1,40 +1,47 @@
|
||||
package net.sf.briar.protocol;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.security.MessageDigest;
|
||||
import java.util.Collection;
|
||||
|
||||
import net.sf.briar.api.crypto.CryptoComponent;
|
||||
import net.sf.briar.api.protocol.MessageId;
|
||||
import net.sf.briar.api.protocol.Offer;
|
||||
import net.sf.briar.api.protocol.OfferId;
|
||||
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 MessageDigest messageDigest;
|
||||
private final ObjectReader<MessageId> messageIdReader;
|
||||
private final OfferFactory offerFactory;
|
||||
|
||||
@Inject
|
||||
OfferReader(ObjectReader<MessageId> messageIdReader,
|
||||
OfferReader(CryptoComponent crypto, ObjectReader<MessageId> messageIdReader,
|
||||
OfferFactory offerFactory) {
|
||||
messageDigest = crypto.getMessageDigest();
|
||||
this.messageIdReader = messageIdReader;
|
||||
this.offerFactory = offerFactory;
|
||||
}
|
||||
|
||||
public Offer readObject(Reader r) throws IOException {
|
||||
// Initialise the consumer
|
||||
// Initialise the consumers
|
||||
Consumer counting = new CountingConsumer(Offer.MAX_SIZE);
|
||||
DigestingConsumer digesting = new DigestingConsumer(messageDigest);
|
||||
messageDigest.reset();
|
||||
// Read the data
|
||||
r.addConsumer(counting);
|
||||
r.addConsumer(digesting);
|
||||
r.readUserDefinedTag(Tags.OFFER);
|
||||
r.addObjectReader(Tags.MESSAGE_ID, messageIdReader);
|
||||
Collection<MessageId> messages = r.readList(MessageId.class);
|
||||
r.removeObjectReader(Tags.MESSAGE_ID);
|
||||
r.removeConsumer(digesting);
|
||||
r.removeConsumer(counting);
|
||||
// Build and return the offer
|
||||
return offerFactory.createOffer(messages);
|
||||
OfferId id = new OfferId(messageDigest.digest());
|
||||
return offerFactory.createOffer(id, messages);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,6 +12,7 @@ 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.protocol.Offer;
|
||||
import net.sf.briar.api.protocol.OfferId;
|
||||
import net.sf.briar.api.protocol.ProtocolReaderFactory;
|
||||
import net.sf.briar.api.protocol.Request;
|
||||
import net.sf.briar.api.protocol.SubscriptionUpdate;
|
||||
@@ -81,14 +82,21 @@ public class ProtocolModule extends AbstractModule {
|
||||
}
|
||||
|
||||
@Provides
|
||||
ObjectReader<Offer> getOfferReader(ObjectReader<MessageId> messageIdReader,
|
||||
OfferFactory offerFactory) {
|
||||
return new OfferReader(messageIdReader, offerFactory);
|
||||
ObjectReader<OfferId> getOfferIdReader() {
|
||||
return new OfferIdReader();
|
||||
}
|
||||
|
||||
@Provides
|
||||
ObjectReader<Request> getRequestReader(RequestFactory requestFactory) {
|
||||
return new RequestReader(requestFactory);
|
||||
ObjectReader<Offer> getOfferReader(CryptoComponent crypto,
|
||||
ObjectReader<MessageId> messageIdReader,
|
||||
OfferFactory offerFactory) {
|
||||
return new OfferReader(crypto, messageIdReader, offerFactory);
|
||||
}
|
||||
|
||||
@Provides
|
||||
ObjectReader<Request> getRequestReader(ObjectReader<OfferId> offerIdReader,
|
||||
RequestFactory requestFactory) {
|
||||
return new RequestReader(offerIdReader, requestFactory);
|
||||
}
|
||||
|
||||
@Provides
|
||||
|
||||
@@ -2,9 +2,10 @@ package net.sf.briar.protocol;
|
||||
|
||||
import java.util.BitSet;
|
||||
|
||||
import net.sf.briar.api.protocol.OfferId;
|
||||
import net.sf.briar.api.protocol.Request;
|
||||
|
||||
interface RequestFactory {
|
||||
|
||||
Request createRequest(BitSet requested);
|
||||
Request createRequest(OfferId offerId, BitSet requested);
|
||||
}
|
||||
|
||||
@@ -2,11 +2,12 @@ package net.sf.briar.protocol;
|
||||
|
||||
import java.util.BitSet;
|
||||
|
||||
import net.sf.briar.api.protocol.OfferId;
|
||||
import net.sf.briar.api.protocol.Request;
|
||||
|
||||
class RequestFactoryImpl implements RequestFactory {
|
||||
|
||||
public Request createRequest(BitSet requested) {
|
||||
return new RequestImpl(requested);
|
||||
public Request createRequest(OfferId offerId, BitSet requested) {
|
||||
return new RequestImpl(offerId, requested);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,16 +2,23 @@ package net.sf.briar.protocol;
|
||||
|
||||
import java.util.BitSet;
|
||||
|
||||
import net.sf.briar.api.protocol.OfferId;
|
||||
import net.sf.briar.api.protocol.Request;
|
||||
|
||||
class RequestImpl implements Request {
|
||||
|
||||
private final OfferId offerId;
|
||||
private final BitSet requested;
|
||||
|
||||
RequestImpl(BitSet requested) {
|
||||
RequestImpl(OfferId offerId, BitSet requested) {
|
||||
this.offerId = offerId;
|
||||
this.requested = requested;
|
||||
}
|
||||
|
||||
public OfferId getOfferId() {
|
||||
return offerId;
|
||||
}
|
||||
|
||||
public BitSet getBitmap() {
|
||||
return requested;
|
||||
}
|
||||
|
||||
@@ -3,20 +3,21 @@ package net.sf.briar.protocol;
|
||||
import java.io.IOException;
|
||||
import java.util.BitSet;
|
||||
|
||||
import net.sf.briar.api.protocol.OfferId;
|
||||
import net.sf.briar.api.protocol.Request;
|
||||
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 RequestReader implements ObjectReader<Request> {
|
||||
|
||||
private final ObjectReader<OfferId> offerIdReader;
|
||||
private final RequestFactory requestFactory;
|
||||
|
||||
@Inject
|
||||
RequestReader(RequestFactory requestFactory) {
|
||||
RequestReader(ObjectReader<OfferId> offerIdReader,
|
||||
RequestFactory requestFactory) {
|
||||
this.offerIdReader = offerIdReader;
|
||||
this.requestFactory = requestFactory;
|
||||
}
|
||||
|
||||
@@ -26,6 +27,8 @@ class RequestReader implements ObjectReader<Request> {
|
||||
// Read the data
|
||||
r.addConsumer(counting);
|
||||
r.readUserDefinedTag(Tags.REQUEST);
|
||||
r.addObjectReader(Tags.OFFER_ID, offerIdReader);
|
||||
OfferId offerId = r.readUserDefined(Tags.OFFER_ID, OfferId.class);
|
||||
byte[] bitmap = r.readBytes(Request.MAX_SIZE);
|
||||
r.removeConsumer(counting);
|
||||
// Convert the bitmap into a BitSet
|
||||
@@ -36,6 +39,6 @@ class RequestReader implements ObjectReader<Request> {
|
||||
if((bitmap[i] & bit) != 0) b.set(i * 8 + j);
|
||||
}
|
||||
}
|
||||
return requestFactory.createRequest(b);
|
||||
return requestFactory.createRequest(offerId, b);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,14 +10,11 @@ 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 SubscriptionReader implements ObjectReader<SubscriptionUpdate> {
|
||||
|
||||
private final ObjectReader<Group> groupReader;
|
||||
private final SubscriptionFactory subscriptionFactory;
|
||||
|
||||
@Inject
|
||||
SubscriptionReader(ObjectReader<Group> groupReader,
|
||||
SubscriptionFactory subscriptionFactory) {
|
||||
this.groupReader = groupReader;
|
||||
|
||||
@@ -10,13 +10,10 @@ 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 TransportReader implements ObjectReader<TransportUpdate> {
|
||||
|
||||
private final TransportFactory transportFactory;
|
||||
|
||||
@Inject
|
||||
TransportReader(TransportFactory transportFactory) {
|
||||
this.transportFactory = transportFactory;
|
||||
}
|
||||
|
||||
@@ -2,9 +2,12 @@ package net.sf.briar.protocol.writers;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.security.DigestOutputStream;
|
||||
import java.security.MessageDigest;
|
||||
|
||||
import net.sf.briar.api.protocol.MessageId;
|
||||
import net.sf.briar.api.protocol.Offer;
|
||||
import net.sf.briar.api.protocol.OfferId;
|
||||
import net.sf.briar.api.protocol.Tags;
|
||||
import net.sf.briar.api.protocol.writers.OfferWriter;
|
||||
import net.sf.briar.api.serial.Writer;
|
||||
@@ -14,17 +17,20 @@ class OfferWriterImpl implements OfferWriter {
|
||||
|
||||
private final OutputStream out;
|
||||
private final Writer w;
|
||||
private final MessageDigest messageDigest;
|
||||
|
||||
private boolean started = false, finished = false;
|
||||
private boolean started = false;
|
||||
|
||||
OfferWriterImpl(OutputStream out, WriterFactory writerFactory) {
|
||||
this.out = out;
|
||||
OfferWriterImpl(OutputStream out, WriterFactory writerFactory,
|
||||
MessageDigest messageDigest) {
|
||||
this.out = new DigestOutputStream(out, messageDigest);
|
||||
w = writerFactory.createWriter(out);
|
||||
this.messageDigest = messageDigest;
|
||||
}
|
||||
|
||||
public boolean writeMessageId(MessageId m) throws IOException {
|
||||
if(finished) throw new IllegalStateException();
|
||||
if(!started) {
|
||||
messageDigest.reset();
|
||||
w.writeUserDefinedTag(Tags.OFFER);
|
||||
w.writeListStart();
|
||||
started = true;
|
||||
@@ -35,15 +41,16 @@ class OfferWriterImpl implements OfferWriter {
|
||||
return true;
|
||||
}
|
||||
|
||||
public void finish() throws IOException {
|
||||
if(finished) throw new IllegalStateException();
|
||||
public OfferId finish() throws IOException {
|
||||
if(!started) {
|
||||
messageDigest.reset();
|
||||
w.writeUserDefinedTag(Tags.OFFER);
|
||||
w.writeListStart();
|
||||
started = true;
|
||||
}
|
||||
w.writeListEnd();
|
||||
out.flush();
|
||||
finished = true;
|
||||
started = false;
|
||||
return new OfferId(messageDigest.digest());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -36,7 +36,7 @@ class ProtocolWriterFactoryImpl implements ProtocolWriterFactory {
|
||||
}
|
||||
|
||||
public OfferWriter createOfferWriter(OutputStream out) {
|
||||
return new OfferWriterImpl(out, writerFactory);
|
||||
return new OfferWriterImpl(out, writerFactory, messageDigest);
|
||||
}
|
||||
|
||||
public RequestWriter createRequestWriter(OutputStream out) {
|
||||
|
||||
@@ -4,6 +4,7 @@ import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.util.BitSet;
|
||||
|
||||
import net.sf.briar.api.protocol.OfferId;
|
||||
import net.sf.briar.api.protocol.Tags;
|
||||
import net.sf.briar.api.protocol.writers.RequestWriter;
|
||||
import net.sf.briar.api.serial.Writer;
|
||||
@@ -19,8 +20,11 @@ class RequestWriterImpl implements RequestWriter {
|
||||
w = writerFactory.createWriter(out);
|
||||
}
|
||||
|
||||
public void writeBitmap(BitSet b, int length) throws IOException {
|
||||
public void writeRequest(OfferId offerId, BitSet b, int length)
|
||||
throws IOException {
|
||||
w.writeUserDefinedTag(Tags.REQUEST);
|
||||
w.writeUserDefinedTag(Tags.OFFER_ID);
|
||||
w.writeBytes(offerId.getBytes());
|
||||
// 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];
|
||||
|
||||
Reference in New Issue
Block a user