Refactored transport component and renamed WritersModule.

The goal of the refactoring was to clean up the dependencies of
IncomingBatchConnection and OutgoingBatchConnection.
This commit is contained in:
akwizgran
2011-09-27 19:21:44 +01:00
parent 6ed8d89e59
commit 4aff0c4f88
21 changed files with 105 additions and 161 deletions

View File

@@ -14,45 +14,32 @@ import net.sf.briar.api.protocol.writers.ProtocolWriterFactory;
import net.sf.briar.api.protocol.writers.SubscriptionWriter;
import net.sf.briar.api.protocol.writers.TransportWriter;
import net.sf.briar.api.transport.ConnectionWriter;
import net.sf.briar.api.transport.ConnectionWriterFactory;
import net.sf.briar.api.transport.batch.BatchTransportWriter;
class OutgoingBatchConnection {
private final BatchTransportWriter trans;
private final ConnectionWriterFactory connFactory;
private final ConnectionWriter conn;
private final DatabaseComponent db;
private final ProtocolWriterFactory protoFactory;
private final int transportId;
private final long connection;
private final ContactId contactId;
OutgoingBatchConnection(BatchTransportWriter trans,
ConnectionWriterFactory connFactory, DatabaseComponent db,
ProtocolWriterFactory protoFactory, int transportId,
long connection, ContactId contactId) {
this.trans = trans;
this.connFactory = connFactory;
OutgoingBatchConnection(ConnectionWriter conn, DatabaseComponent db,
ProtocolWriterFactory protoFactory, ContactId contactId) {
this.conn = conn;
this.db = db;
this.protoFactory = protoFactory;
this.transportId = transportId;
this.connection = connection;
this.contactId = contactId;
}
void write() throws DbException, IOException {
byte[] secret = db.getSharedSecret(contactId);
ConnectionWriter conn = connFactory.createConnectionWriter(
trans.getOutputStream(), true, transportId, connection, secret);
OutputStream out = conn.getOutputStream();
// There should be enough space for a packet
long capacity = conn.getCapacity(trans.getCapacity());
long capacity = conn.getCapacity();
if(capacity < MAX_PACKET_LENGTH) throw new IOException();
// Write a transport update
TransportWriter t = protoFactory.createTransportWriter(out);
db.generateTransportUpdate(contactId, t);
// If there's space, write a subscription update
capacity = conn.getCapacity(trans.getCapacity());
capacity = conn.getCapacity();
if(capacity >= MAX_PACKET_LENGTH) {
SubscriptionWriter s = protoFactory.createSubscriptionWriter(out);
db.generateSubscriptionUpdate(contactId, s);
@@ -60,14 +47,14 @@ class OutgoingBatchConnection {
// Write acks until you can't write acks no more
AckWriter a = protoFactory.createAckWriter(out);
do {
capacity = conn.getCapacity(trans.getCapacity());
capacity = conn.getCapacity();
int max = (int) Math.min(MAX_PACKET_LENGTH, capacity);
a.setMaxPacketLength(max);
} while(db.generateAck(contactId, a));
// Write batches until you can't write batches no more
BatchWriter b = protoFactory.createBatchWriter(out);
do {
capacity = conn.getCapacity(trans.getCapacity());
capacity = conn.getCapacity();
int max = (int) Math.min(MAX_PACKET_LENGTH, capacity);
b.setMaxPacketLength(max);
} while(db.generateBatch(contactId, b));