mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-21 07:09:56 +01:00
Implemented OfferWriter and RequestWriter, made all the writers
reusable (though not thread-safe), and guiced the readers.
This commit is contained in:
@@ -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