mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-13 03:09:04 +01:00
Return a connection context for outgoing connections (the secret will
be included in this context in the near future).
This commit is contained in:
@@ -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;
|
||||
|
||||
/**
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user