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

@@ -28,6 +28,7 @@ import net.sf.briar.api.protocol.writers.OfferWriter;
import net.sf.briar.api.protocol.writers.RequestWriter;
import net.sf.briar.api.protocol.writers.SubscriptionUpdateWriter;
import net.sf.briar.api.protocol.writers.TransportUpdateWriter;
import net.sf.briar.api.transport.ConnectionContext;
import net.sf.briar.api.transport.ConnectionWindow;
/**
@@ -114,10 +115,11 @@ public interface DatabaseComponent {
TransportConfig getConfig(TransportId t) throws DbException;
/**
* Returns an outgoing connection number for the given contact and
* Returns an outgoing connection context for the given contact and
* transport.
*/
long getConnectionNumber(ContactId c, TransportIndex i) throws DbException;
ConnectionContext getConnectionContext(ContactId c, TransportIndex i)
throws DbException;
/**
* Returns the connection reordering window for the given contact and

View File

@@ -0,0 +1,10 @@
package net.sf.briar.api.transport;
import net.sf.briar.api.ContactId;
import net.sf.briar.api.protocol.TransportIndex;
public interface ConnectionContextFactory {
ConnectionContext createConnectionContext(ContactId c, TransportIndex i,
long connection);
}

View File

@@ -20,6 +20,7 @@ import net.sf.briar.api.protocol.MessageId;
import net.sf.briar.api.protocol.Transport;
import net.sf.briar.api.protocol.TransportId;
import net.sf.briar.api.protocol.TransportIndex;
import net.sf.briar.api.transport.ConnectionContext;
import net.sf.briar.api.transport.ConnectionWindow;
/**
@@ -183,12 +184,12 @@ interface Database<T> {
TransportConfig getConfig(T txn, TransportId t) throws DbException;
/**
* Allocates and returns a connection number for the given contact and
* Returns an outgoing connection context for the given contact and
* transport.
* <p>
* Locking: contact read, window write.
*/
long getConnectionNumber(T txn, ContactId c, TransportIndex i)
ConnectionContext getConnectionContext(T txn, ContactId c, TransportIndex i)
throws DbException;
/**

View File

@@ -60,6 +60,7 @@ import net.sf.briar.api.protocol.writers.OfferWriter;
import net.sf.briar.api.protocol.writers.RequestWriter;
import net.sf.briar.api.protocol.writers.SubscriptionUpdateWriter;
import net.sf.briar.api.protocol.writers.TransportUpdateWriter;
import net.sf.briar.api.transport.ConnectionContext;
import net.sf.briar.api.transport.ConnectionWindow;
import com.google.inject.Inject;
@@ -700,7 +701,7 @@ DatabaseCleaner.Callback {
}
}
public long getConnectionNumber(ContactId c, TransportIndex i)
public ConnectionContext getConnectionContext(ContactId c, TransportIndex i)
throws DbException {
contactLock.readLock().lock();
try {
@@ -709,9 +710,9 @@ DatabaseCleaner.Callback {
try {
T txn = db.startTransaction();
try {
long outgoing = db.getConnectionNumber(txn, c, i);
ConnectionContext ctx = db.getConnectionContext(txn, c, i);
db.commitTransaction(txn);
return outgoing;
return ctx;
} catch(DbException e) {
db.abortTransaction(txn);
throw e;

View File

@@ -9,6 +9,7 @@ import net.sf.briar.api.db.DatabaseDirectory;
import net.sf.briar.api.db.DatabaseMaxSize;
import net.sf.briar.api.db.DatabasePassword;
import net.sf.briar.api.protocol.GroupFactory;
import net.sf.briar.api.transport.ConnectionContextFactory;
import net.sf.briar.api.transport.ConnectionWindowFactory;
import com.google.inject.AbstractModule;
@@ -25,10 +26,11 @@ public class DatabaseModule extends AbstractModule {
@Provides
Database<Connection> getDatabase(@DatabaseDirectory File dir,
@DatabasePassword Password password, @DatabaseMaxSize long maxSize,
ConnectionContextFactory connectionContextFactory,
ConnectionWindowFactory connectionWindowFactory,
GroupFactory groupFactory) {
return new H2Database(dir, password, maxSize, connectionWindowFactory,
groupFactory);
return new H2Database(dir, password, maxSize, connectionContextFactory,
connectionWindowFactory, groupFactory);
}
@Provides @Singleton

View File

@@ -16,6 +16,7 @@ import net.sf.briar.api.db.DatabaseMaxSize;
import net.sf.briar.api.db.DatabasePassword;
import net.sf.briar.api.db.DbException;
import net.sf.briar.api.protocol.GroupFactory;
import net.sf.briar.api.transport.ConnectionContextFactory;
import net.sf.briar.api.transport.ConnectionWindowFactory;
import org.apache.commons.io.FileSystemUtils;
@@ -34,11 +35,13 @@ class H2Database extends JdbcDatabase {
private final long maxSize;
@Inject
H2Database(@DatabaseDirectory File dir, @DatabasePassword Password password,
H2Database(@DatabaseDirectory File dir,
@DatabasePassword Password password,
@DatabaseMaxSize long maxSize,
ConnectionContextFactory connectionContextFactory,
ConnectionWindowFactory connectionWindowFactory,
GroupFactory groupFactory) {
super(connectionWindowFactory, groupFactory,
super(connectionContextFactory, connectionWindowFactory, groupFactory,
"BINARY(32)", "BINARY", "INT NOT NULL AUTO_INCREMENT");
home = new File(dir, "db");
this.password = password;

View File

@@ -35,6 +35,8 @@ import net.sf.briar.api.protocol.ProtocolConstants;
import net.sf.briar.api.protocol.Transport;
import net.sf.briar.api.protocol.TransportId;
import net.sf.briar.api.protocol.TransportIndex;
import net.sf.briar.api.transport.ConnectionContext;
import net.sf.briar.api.transport.ConnectionContextFactory;
import net.sf.briar.api.transport.ConnectionWindow;
import net.sf.briar.api.transport.ConnectionWindowFactory;
import net.sf.briar.util.FileUtils;
@@ -265,10 +267,11 @@ abstract class JdbcDatabase implements Database<Connection> {
private static final Logger LOG =
Logger.getLogger(JdbcDatabase.class.getName());
// Different database libraries use different names for certain types
private final String hashType, binaryType, counterType;
private final ConnectionContextFactory connectionContextFactory;
private final ConnectionWindowFactory connectionWindowFactory;
private final GroupFactory groupFactory;
// Different database libraries use different names for certain types
private final String hashType, binaryType, counterType;
private final LinkedList<Connection> connections =
new LinkedList<Connection>(); // Locking: self
@@ -278,9 +281,11 @@ abstract class JdbcDatabase implements Database<Connection> {
protected abstract Connection createConnection() throws SQLException;
JdbcDatabase(ConnectionWindowFactory connectionWindowFactory,
JdbcDatabase(ConnectionContextFactory connectionContextFactory,
ConnectionWindowFactory connectionWindowFactory,
GroupFactory groupFactory, String hashType, String binaryType,
String counterType) {
this.connectionContextFactory = connectionContextFactory;
this.connectionWindowFactory = connectionWindowFactory;
this.groupFactory = groupFactory;
this.hashType = hashType;
@@ -939,7 +944,7 @@ abstract class JdbcDatabase implements Database<Connection> {
}
}
public long getConnectionNumber(Connection txn, ContactId c,
public ConnectionContext getConnectionContext(Connection txn, ContactId c,
TransportIndex i) throws DbException {
PreparedStatement ps = null;
ResultSet rs = null;
@@ -963,7 +968,8 @@ abstract class JdbcDatabase implements Database<Connection> {
if(rs.next()) throw new DbStateException();
rs.close();
ps.close();
return outgoing;
return connectionContextFactory.createConnectionContext(c, i,
outgoing);
} catch(SQLException e) {
tryToClose(rs);
tryToClose(ps);

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);
}
}

View File

@@ -561,7 +561,7 @@ public abstract class DatabaseComponentTest extends TestCase {
} catch(NoSuchContactException expected) {}
try {
db.getConnectionNumber(contactId, remoteIndex);
db.getConnectionContext(contactId, remoteIndex);
fail();
} catch(NoSuchContactException expected) {}

View File

@@ -37,6 +37,7 @@ import net.sf.briar.api.protocol.ProtocolConstants;
import net.sf.briar.api.protocol.Transport;
import net.sf.briar.api.protocol.TransportId;
import net.sf.briar.api.protocol.TransportIndex;
import net.sf.briar.api.transport.ConnectionContextFactory;
import net.sf.briar.api.transport.ConnectionWindow;
import net.sf.briar.api.transport.ConnectionWindowFactory;
import net.sf.briar.crypto.CryptoModule;
@@ -65,6 +66,7 @@ public class H2DatabaseTest extends TestCase {
private final String passwordString = "foo bar";
private final Password password = new TestPassword();
private final Random random = new Random();
private final ConnectionContextFactory connectionContextFactory;
private final ConnectionWindowFactory connectionWindowFactory;
private final GroupFactory groupFactory;
@@ -94,6 +96,8 @@ public class H2DatabaseTest extends TestCase {
new ProtocolWritersModule(), new SerialModule(),
new TransportBatchModule(), new TransportModule(),
new TransportStreamModule(), new TestDatabaseModule(testDir));
connectionContextFactory =
i.getInstance(ConnectionContextFactory.class);
connectionWindowFactory = i.getInstance(ConnectionWindowFactory.class);
groupFactory = i.getInstance(GroupFactory.class);
authorId = new AuthorId(TestUtils.getRandomId());
@@ -1885,7 +1889,8 @@ public class H2DatabaseTest extends TestCase {
private Database<Connection> open(boolean resume) throws Exception {
Database<Connection> db = new H2Database(testDir, password, MAX_SIZE,
connectionWindowFactory, groupFactory);
connectionContextFactory, connectionWindowFactory,
groupFactory);
db.open(resume);
return db;
}