Return a connection context for outgoing connections (the secret will

be included in this context in the near future).
This commit is contained in:
akwizgran
2011-11-15 17:47:30 +00:00
parent fabdaf5957
commit d02a68edfc
13 changed files with 78 additions and 30 deletions

View File

@@ -0,0 +1,14 @@
package net.sf.briar.transport;
import net.sf.briar.api.ContactId;
import net.sf.briar.api.protocol.TransportIndex;
import net.sf.briar.api.transport.ConnectionContext;
import net.sf.briar.api.transport.ConnectionContextFactory;
class ConnectionContextFactoryImpl implements ConnectionContextFactory {
public ConnectionContext createConnectionContext(ContactId c,
TransportIndex i, long connection) {
return new ConnectionContextImpl(c, i, connection);
}
}

View File

@@ -1,5 +1,6 @@
package net.sf.briar.transport;
import net.sf.briar.api.transport.ConnectionContextFactory;
import net.sf.briar.api.transport.ConnectionDispatcher;
import net.sf.briar.api.transport.ConnectionReaderFactory;
import net.sf.briar.api.transport.ConnectionRecogniser;
@@ -12,6 +13,8 @@ public class TransportModule extends AbstractModule {
@Override
protected void configure() {
bind(ConnectionContextFactory.class).to(
ConnectionContextFactoryImpl.class);
bind(ConnectionDispatcher.class).to(ConnectionDispatcherImpl.class);
bind(ConnectionReaderFactory.class).to(
ConnectionReaderFactoryImpl.class);

View File

@@ -17,6 +17,7 @@ import net.sf.briar.api.protocol.writers.ProtocolWriterFactory;
import net.sf.briar.api.protocol.writers.SubscriptionUpdateWriter;
import net.sf.briar.api.protocol.writers.TransportUpdateWriter;
import net.sf.briar.api.transport.BatchTransportWriter;
import net.sf.briar.api.transport.ConnectionContext;
import net.sf.briar.api.transport.ConnectionWriter;
import net.sf.briar.api.transport.ConnectionWriterFactory;
@@ -47,10 +48,11 @@ class OutgoingBatchConnection {
void write() {
try {
byte[] secret = db.getSharedSecret(contactId, false);
long connection = db.getConnectionNumber(contactId, transportIndex);
ConnectionContext ctx =
db.getConnectionContext(contactId, transportIndex);
ConnectionWriter conn = connFactory.createConnectionWriter(
writer.getOutputStream(), writer.getCapacity(),
transportIndex, connection, secret);
transportIndex, ctx.getConnectionNumber(), secret);
OutputStream out = conn.getOutputStream();
// There should be enough space for a packet
long capacity = conn.getRemainingCapacity();

View File

@@ -8,6 +8,7 @@ import net.sf.briar.api.db.DbException;
import net.sf.briar.api.protocol.ProtocolReaderFactory;
import net.sf.briar.api.protocol.TransportIndex;
import net.sf.briar.api.protocol.writers.ProtocolWriterFactory;
import net.sf.briar.api.transport.ConnectionContext;
import net.sf.briar.api.transport.ConnectionReader;
import net.sf.briar.api.transport.ConnectionReaderFactory;
import net.sf.briar.api.transport.ConnectionWriter;
@@ -16,7 +17,7 @@ import net.sf.briar.api.transport.StreamTransportConnection;
public class OutgoingStreamConnection extends StreamConnection {
private long connectionNum = -1L; // Locking: this
private ConnectionContext ctx = null; // Locking: this
OutgoingStreamConnection(ConnectionReaderFactory connReaderFactory,
ConnectionWriterFactory connWriterFactory, DatabaseComponent db,
@@ -32,29 +33,27 @@ public class OutgoingStreamConnection extends StreamConnection {
protected ConnectionReader createConnectionReader() throws DbException,
IOException {
synchronized(this) {
if(connectionNum == -1L) {
connectionNum = db.getConnectionNumber(contactId,
transportIndex);
if(ctx == null) {
ctx = db.getConnectionContext(contactId, transportIndex);
}
}
byte[] secret = db.getSharedSecret(contactId, true);
return connReaderFactory.createConnectionReader(
connection.getInputStream(), transportIndex, connectionNum,
secret);
connection.getInputStream(), transportIndex,
ctx.getConnectionNumber(), secret);
}
@Override
protected ConnectionWriter createConnectionWriter() throws DbException,
IOException {
synchronized(this) {
if(connectionNum == -1L) {
connectionNum = db.getConnectionNumber(contactId,
transportIndex);
if(ctx == null) {
ctx = db.getConnectionContext(contactId, transportIndex);
}
}
byte[] secret = db.getSharedSecret(contactId, false);
return connWriterFactory.createConnectionWriter(
connection.getOutputStream(), Long.MAX_VALUE, transportIndex,
connectionNum, secret);
ctx.getConnectionNumber(), secret);
}
}