Readers, writers and factories for subscription and transport updates.

This commit is contained in:
akwizgran
2011-07-23 21:46:47 +01:00
parent 30271c14ce
commit 941460e3bc
34 changed files with 423 additions and 53 deletions

View File

@@ -19,10 +19,10 @@ class AckWriterImpl implements AckWriter {
AckWriterImpl(OutputStream out, WriterFactory writerFactory) {
this.out = out;
this.w = writerFactory.createWriter(out);
w = writerFactory.createWriter(out);
}
public boolean addBatchId(BatchId b) throws IOException {
public boolean writeBatchId(BatchId b) throws IOException {
if(finished) throw new IllegalStateException();
if(!started) {
w.writeUserDefinedTag(Tags.ACK);

View File

@@ -31,7 +31,7 @@ class BatchWriterImpl implements BatchWriter {
return Batch.MAX_SIZE - 3;
}
public boolean addMessage(byte[] message) throws IOException {
public boolean writeMessage(byte[] message) throws IOException {
if(finished) throw new IllegalStateException();
if(!started) {
messageDigest.reset();

View File

@@ -33,12 +33,10 @@ class PacketWriterFactoryImpl implements PacketWriterFactory {
}
public SubscriptionWriter createSubscriptionWriter(OutputStream out) {
// TODO Auto-generated method stub
return null;
return new SubscriptionWriterImpl(out, writerFactory);
}
public TransportWriter createTransportWriter(OutputStream out) {
// TODO Auto-generated method stub
return null;
return new TransportWriterImpl(out, writerFactory);
}
}

View File

@@ -0,0 +1,33 @@
package net.sf.briar.protocol.writers;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Collection;
import net.sf.briar.api.protocol.Group;
import net.sf.briar.api.protocol.Tags;
import net.sf.briar.api.protocol.writers.SubscriptionWriter;
import net.sf.briar.api.serial.Writer;
import net.sf.briar.api.serial.WriterFactory;
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;
}
}

View File

@@ -0,0 +1,33 @@
package net.sf.briar.protocol.writers;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Map;
import net.sf.briar.api.protocol.Tags;
import net.sf.briar.api.protocol.writers.TransportWriter;
import net.sf.briar.api.serial.Writer;
import net.sf.briar.api.serial.WriterFactory;
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);
}
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;
}
}