mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-14 11:49:04 +01:00
Implemented incoming and outgoing batch connections (untested).
This commit is contained in:
@@ -34,6 +34,10 @@ class ProtocolReaderImpl implements ProtocolReader {
|
||||
reader.addObjectReader(Types.TRANSPORT_UPDATE, transportReader);
|
||||
}
|
||||
|
||||
public boolean eof() throws IOException {
|
||||
return reader.eof();
|
||||
}
|
||||
|
||||
public boolean hasAck() throws IOException {
|
||||
return reader.hasUserDefined(Types.ACK);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,70 @@
|
||||
package net.sf.briar.transport.batch;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
import net.sf.briar.api.ContactId;
|
||||
import net.sf.briar.api.FormatException;
|
||||
import net.sf.briar.api.db.DatabaseComponent;
|
||||
import net.sf.briar.api.db.DbException;
|
||||
import net.sf.briar.api.protocol.Ack;
|
||||
import net.sf.briar.api.protocol.Batch;
|
||||
import net.sf.briar.api.protocol.ProtocolReader;
|
||||
import net.sf.briar.api.protocol.ProtocolReaderFactory;
|
||||
import net.sf.briar.api.protocol.SubscriptionUpdate;
|
||||
import net.sf.briar.api.protocol.TransportUpdate;
|
||||
import net.sf.briar.api.transport.ConnectionReader;
|
||||
import net.sf.briar.api.transport.ConnectionReaderFactory;
|
||||
import net.sf.briar.api.transport.batch.BatchTransportReader;
|
||||
|
||||
class IncomingBatchConnection {
|
||||
|
||||
private final BatchTransportReader trans;
|
||||
private final ConnectionReaderFactory connFactory;
|
||||
private final DatabaseComponent db;
|
||||
private final ProtocolReaderFactory protoFactory;
|
||||
private final int transportId;
|
||||
private final long connection;
|
||||
private final ContactId contactId;
|
||||
|
||||
IncomingBatchConnection(BatchTransportReader trans,
|
||||
ConnectionReaderFactory connFactory, DatabaseComponent db,
|
||||
ProtocolReaderFactory protoFactory, int transportId,
|
||||
long connection, ContactId contactId) {
|
||||
this.trans = trans;
|
||||
this.connFactory = connFactory;
|
||||
this.db = db;
|
||||
this.protoFactory = protoFactory;
|
||||
this.transportId = transportId;
|
||||
this.connection = connection;
|
||||
this.contactId = contactId;
|
||||
}
|
||||
|
||||
void read() throws DbException, IOException {
|
||||
byte[] secret = db.getSharedSecret(contactId);
|
||||
ConnectionReader conn = connFactory.createConnectionReader(
|
||||
trans.getInputStream(), false, transportId, connection, secret);
|
||||
InputStream in = conn.getInputStream();
|
||||
ProtocolReader proto = protoFactory.createProtocolReader(in);
|
||||
// Read packets until EOF
|
||||
while(!proto.eof()) {
|
||||
if(proto.hasAck()) {
|
||||
Ack a = proto.readAck();
|
||||
db.receiveAck(contactId, a);
|
||||
} else if(proto.hasBatch()) {
|
||||
Batch b = proto.readBatch();
|
||||
db.receiveBatch(contactId, b);
|
||||
} else if(proto.hasSubscriptionUpdate()) {
|
||||
SubscriptionUpdate s = proto.readSubscriptionUpdate();
|
||||
db.receiveSubscriptionUpdate(contactId, s);
|
||||
} else if(proto.hasTransportUpdate()) {
|
||||
TransportUpdate t = proto.readTransportUpdate();
|
||||
db.receiveTransportUpdate(contactId, t);
|
||||
} else {
|
||||
throw new FormatException();
|
||||
}
|
||||
}
|
||||
// Close the input stream
|
||||
in.close();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
package net.sf.briar.transport.batch;
|
||||
|
||||
import static net.sf.briar.api.protocol.ProtocolConstants.MAX_PACKET_LENGTH;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
|
||||
import net.sf.briar.api.ContactId;
|
||||
import net.sf.briar.api.db.DatabaseComponent;
|
||||
import net.sf.briar.api.db.DbException;
|
||||
import net.sf.briar.api.protocol.writers.AckWriter;
|
||||
import net.sf.briar.api.protocol.writers.BatchWriter;
|
||||
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 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;
|
||||
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());
|
||||
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());
|
||||
if(capacity >= MAX_PACKET_LENGTH) {
|
||||
SubscriptionWriter s = protoFactory.createSubscriptionWriter(out);
|
||||
db.generateSubscriptionUpdate(contactId, s);
|
||||
}
|
||||
// Write acks until you can't write acks no more
|
||||
AckWriter a = protoFactory.createAckWriter(out);
|
||||
do {
|
||||
capacity = conn.getCapacity(trans.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());
|
||||
int max = (int) Math.min(MAX_PACKET_LENGTH, capacity);
|
||||
b.setMaxPacketLength(max);
|
||||
} while(db.generateBatch(contactId, b));
|
||||
// Close the output stream
|
||||
out.close();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user