mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-20 22:59:54 +01:00
Each request packet should contain the unique ID of the offer to which
it responds.
This commit is contained in:
@@ -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