Retransmission without backoff for messages and updates.

This commit is contained in:
akwizgran
2013-02-06 19:06:14 +00:00
parent bec8543bfa
commit 4c5657321d
9 changed files with 274 additions and 206 deletions

View File

@@ -113,10 +113,12 @@ public interface DatabaseComponent {
RetentionAck generateRetentionAck(ContactId c) throws DbException; RetentionAck generateRetentionAck(ContactId c) throws DbException;
/** /**
* Generates a retention update for the given contact. Returns null if no * Generates a retention update for the given contact, for transmission
* update is due. * over a transport with the given latency. Returns null if no update is
* due.
*/ */
RetentionUpdate generateRetentionUpdate(ContactId c) throws DbException; RetentionUpdate generateRetentionUpdate(ContactId c, long maxLatency)
throws DbException;
/** /**
* Generates a subscription ack for the given contact. Returns null if no * Generates a subscription ack for the given contact. Returns null if no
@@ -125,10 +127,11 @@ public interface DatabaseComponent {
SubscriptionAck generateSubscriptionAck(ContactId c) throws DbException; SubscriptionAck generateSubscriptionAck(ContactId c) throws DbException;
/** /**
* Generates a subscription update for the given contact. Returns null if * Generates a subscription update for the given contact, for transmission
* no update is due. * over a transport with the given latency. Returns null if no update is
* due.
*/ */
SubscriptionUpdate generateSubscriptionUpdate(ContactId c) SubscriptionUpdate generateSubscriptionUpdate(ContactId c, long maxLatency)
throws DbException; throws DbException;
/** /**
@@ -139,11 +142,12 @@ public interface DatabaseComponent {
throws DbException; throws DbException;
/** /**
* Generates a batch of transport updates for the given contact. Returns * Generates a batch of transport updates for the given contact, for
* null if no updates are due. * transmission over a transport with the given latency. Returns null if no
* updates are due.
*/ */
Collection<TransportUpdate> generateTransportUpdates(ContactId c) Collection<TransportUpdate> generateTransportUpdates(ContactId c,
throws DbException; long maxLatency) throws DbException;
/** Returns the configuration for the given transport. */ /** Returns the configuration for the given transport. */
TransportConfig getConfig(TransportId t) throws DbException; TransportConfig getConfig(TransportId t) throws DbException;

View File

@@ -25,6 +25,8 @@ import net.sf.briar.api.messaging.TransportUpdate;
import net.sf.briar.api.transport.Endpoint; import net.sf.briar.api.transport.Endpoint;
import net.sf.briar.api.transport.TemporarySecret; import net.sf.briar.api.transport.TemporarySecret;
// FIXME: Document the preconditions for calling each method
/** /**
* A low-level interface to the database (DatabaseComponent provides a * A low-level interface to the database (DatabaseComponent provides a
* high-level interface). Most operations take a transaction argument, which is * high-level interface). Most operations take a transaction argument, which is
@@ -103,15 +105,6 @@ interface Database<T> {
*/ */
void addMessageToAck(T txn, ContactId c, MessageId m) throws DbException; void addMessageToAck(T txn, ContactId c, MessageId m) throws DbException;
/**
* Records the given messages as needing to be acknowledged by the given
* expiry time.
* <p>
* Locking: contact read, message write.
*/
void addOutstandingMessages(T txn, ContactId c, Collection<MessageId> sent,
long expiry) throws DbException;
/** /**
* Stores the given message, or returns false if the message is already in * Stores the given message, or returns false if the message is already in
* the database. * the database.
@@ -308,9 +301,9 @@ interface Database<T> {
byte[] getRawMessage(T txn, MessageId m) throws DbException; byte[] getRawMessage(T txn, MessageId m) throws DbException;
/** /**
* Returns the message identified by the given ID, in serialised form, or * Returns the message identified by the given ID, in serialised form.
* null if the message is not present in the database or is not sendable to * Returns null if the message is not present in the database or is not
* the given contact. * sendable to the given contact.
* <p> * <p>
* Locking: contact read, message read, subscription read. * Locking: contact read, message read, subscription read.
*/ */
@@ -355,12 +348,13 @@ interface Database<T> {
RetentionAck getRetentionAck(T txn, ContactId c) throws DbException; RetentionAck getRetentionAck(T txn, ContactId c) throws DbException;
/** /**
* Returns a retention update for the given contact, or null if no update * Returns a retention update for the given contact and updates its expiry
* is due. * time using the given latency. Returns null if no update is due.
* <p> * <p>
* Locking: contact read, retention write. * Locking: contact read, retention write.
*/ */
RetentionUpdate getRetentionUpdate(T txn, ContactId c) throws DbException; RetentionUpdate getRetentionUpdate(T txn, ContactId c, long maxLatency)
throws DbException;
/** /**
* Returns all temporary secrets. * Returns all temporary secrets.
@@ -416,13 +410,13 @@ interface Database<T> {
SubscriptionAck getSubscriptionAck(T txn, ContactId c) throws DbException; SubscriptionAck getSubscriptionAck(T txn, ContactId c) throws DbException;
/** /**
* Returns a subscription update for the given contact, or null if no * Returns a subscription update for the given contact and updates its
* update is due. * expiry time using the given latency. Returns null if no update is due.
* <p> * <p>
* Locking: contact read, subscription write. * Locking: contact read, subscription write.
*/ */
SubscriptionUpdate getSubscriptionUpdate(T txn, ContactId c) SubscriptionUpdate getSubscriptionUpdate(T txn, ContactId c,
throws DbException; long maxLatency) throws DbException;
/** /**
* Returns a collection of transport acks for the given contact, or null if * Returns a collection of transport acks for the given contact, or null if
@@ -434,13 +428,14 @@ interface Database<T> {
throws DbException; throws DbException;
/** /**
* Returns a collection of transport updates for the given contact, or * Returns a collection of transport updates for the given contact and
* null if no updates are due. * updates their expiry times using the given latency. Returns null if no
* updates are due.
* <p> * <p>
* Locking: contact read, transport write. * Locking: contact read, transport write.
*/ */
Collection<TransportUpdate> getTransportUpdates(T txn, ContactId c) Collection<TransportUpdate> getTransportUpdates(T txn, ContactId c,
throws DbException; long maxLatency) throws DbException;
/** /**
* Returns the version number of the * Returns the version number of the
@@ -572,6 +567,15 @@ interface Database<T> {
void setConnectionWindow(T txn, ContactId c, TransportId t, long period, void setConnectionWindow(T txn, ContactId c, TransportId t, long period,
long centre, byte[] bitmap) throws DbException; long centre, byte[] bitmap) throws DbException;
/**
* Updates the expiry times of the given messages with respect to the given
* contact, using the latency of the transport over which they were sent.
* <p>
* Locking: contact read, message write.
*/
void setMessageExpiry(T txn, ContactId c, Collection<MessageId> sent,
long maxLatency) throws DbException;
/** /**
* Sets the user's rating for the given author. * Sets the user's rating for the given author.
* <p> * <p>

View File

@@ -495,7 +495,7 @@ DatabaseCleaner.Callback {
// Get some sendable messages from the database // Get some sendable messages from the database
contactLock.readLock().lock(); contactLock.readLock().lock();
try { try {
messageLock.readLock().lock(); messageLock.writeLock().lock();
try { try {
subscriptionLock.readLock().lock(); subscriptionLock.readLock().lock();
try { try {
@@ -515,17 +515,15 @@ DatabaseCleaner.Callback {
subscriptionLock.readLock().unlock(); subscriptionLock.readLock().unlock();
} }
} finally { } finally {
messageLock.readLock().unlock(); messageLock.writeLock().unlock();
} }
if(messages.isEmpty()) return null; if(messages.isEmpty()) return null;
// Calculate the expiry time of the messages
long expiry = calculateExpiryTime(maxLatency);
// Record the messages as sent // Record the messages as sent
messageLock.writeLock().lock(); messageLock.writeLock().lock();
try { try {
T txn = db.startTransaction(); T txn = db.startTransaction();
try { try {
db.addOutstandingMessages(txn, c, ids, expiry); db.setMessageExpiry(txn, c, ids, maxLatency);
db.commitTransaction(txn); db.commitTransaction(txn);
} catch(DbException e) { } catch(DbException e) {
db.abortTransaction(txn); db.abortTransaction(txn);
@@ -580,14 +578,12 @@ DatabaseCleaner.Callback {
messageLock.readLock().unlock(); messageLock.readLock().unlock();
} }
if(messages.isEmpty()) return null; if(messages.isEmpty()) return null;
// Calculate the expiry times of the messages
long expiry = calculateExpiryTime(maxLatency);
// Record the messages as sent // Record the messages as sent
messageLock.writeLock().lock(); messageLock.writeLock().lock();
try { try {
T txn = db.startTransaction(); T txn = db.startTransaction();
try { try {
db.addOutstandingMessages(txn, c, ids, expiry); db.setMessageExpiry(txn, c, ids, maxLatency);
db.commitTransaction(txn); db.commitTransaction(txn);
} catch(DbException e) { } catch(DbException e) {
db.abortTransaction(txn); db.abortTransaction(txn);
@@ -602,14 +598,6 @@ DatabaseCleaner.Callback {
return Collections.unmodifiableList(messages); return Collections.unmodifiableList(messages);
} }
private long calculateExpiryTime(long maxLatency) {
long roundTrip = maxLatency * 2;
if(roundTrip < 0) roundTrip = Long.MAX_VALUE; // Overflow
long expiry = clock.currentTimeMillis() + roundTrip;
if(expiry < 0) expiry = Long.MAX_VALUE; // Overflow
return expiry;
}
public Offer generateOffer(ContactId c, int maxMessages) public Offer generateOffer(ContactId c, int maxMessages)
throws DbException { throws DbException {
Collection<MessageId> offered; Collection<MessageId> offered;
@@ -660,7 +648,7 @@ DatabaseCleaner.Callback {
} }
} }
public RetentionUpdate generateRetentionUpdate(ContactId c) public RetentionUpdate generateRetentionUpdate(ContactId c, long maxLatency)
throws DbException { throws DbException {
contactLock.readLock().lock(); contactLock.readLock().lock();
try { try {
@@ -670,7 +658,8 @@ DatabaseCleaner.Callback {
try { try {
if(!db.containsContact(txn, c)) if(!db.containsContact(txn, c))
throw new NoSuchContactException(); throw new NoSuchContactException();
RetentionUpdate u = db.getRetentionUpdate(txn, c); RetentionUpdate u =
db.getRetentionUpdate(txn, c, maxLatency);
db.commitTransaction(txn); db.commitTransaction(txn);
return u; return u;
} catch(DbException e) { } catch(DbException e) {
@@ -710,8 +699,8 @@ DatabaseCleaner.Callback {
} }
} }
public SubscriptionUpdate generateSubscriptionUpdate(ContactId c) public SubscriptionUpdate generateSubscriptionUpdate(ContactId c,
throws DbException { long maxLatency) throws DbException {
contactLock.readLock().lock(); contactLock.readLock().lock();
try { try {
subscriptionLock.writeLock().lock(); subscriptionLock.writeLock().lock();
@@ -720,7 +709,8 @@ DatabaseCleaner.Callback {
try { try {
if(!db.containsContact(txn, c)) if(!db.containsContact(txn, c))
throw new NoSuchContactException(); throw new NoSuchContactException();
SubscriptionUpdate u = db.getSubscriptionUpdate(txn, c); SubscriptionUpdate u =
db.getSubscriptionUpdate(txn, c, maxLatency);
db.commitTransaction(txn); db.commitTransaction(txn);
return u; return u;
} catch(DbException e) { } catch(DbException e) {
@@ -760,8 +750,8 @@ DatabaseCleaner.Callback {
} }
} }
public Collection<TransportUpdate> generateTransportUpdates(ContactId c) public Collection<TransportUpdate> generateTransportUpdates(ContactId c,
throws DbException { long maxLatency) throws DbException {
contactLock.readLock().lock(); contactLock.readLock().lock();
try { try {
transportLock.writeLock().lock(); transportLock.writeLock().lock();
@@ -771,7 +761,7 @@ DatabaseCleaner.Callback {
if(!db.containsContact(txn, c)) if(!db.containsContact(txn, c))
throw new NoSuchContactException(); throw new NoSuchContactException();
Collection<TransportUpdate> updates = Collection<TransportUpdate> updates =
db.getTransportUpdates(txn, c); db.getTransportUpdates(txn, c, maxLatency);
db.commitTransaction(txn); db.commitTransaction(txn);
return updates; return updates;
} catch(DbException e) { } catch(DbException e) {

View File

@@ -69,7 +69,7 @@ abstract class JdbcDatabase implements Database<Connection> {
// Locking: contact read, subscription // Locking: contact read, subscription
private static final String CREATE_GROUP_VISIBILITIES = private static final String CREATE_GROUP_VISIBILITIES =
"CREATE TABLE groupVisibilities" "CREATE TABLE groupVisibilities"
+ " (contactId INT NOT NULL," + " (contactId INT UNSIGNED NOT NULL,"
+ " groupId HASH NOT NULL," + " groupId HASH NOT NULL,"
+ " FOREIGN KEY (contactId)" + " FOREIGN KEY (contactId)"
+ " REFERENCES contacts (contactId)" + " REFERENCES contacts (contactId)"
@@ -81,7 +81,7 @@ abstract class JdbcDatabase implements Database<Connection> {
// Locking: contact read, subscription // Locking: contact read, subscription
private static final String CREATE_CONTACT_GROUPS = private static final String CREATE_CONTACT_GROUPS =
"CREATE TABLE contactGroups" "CREATE TABLE contactGroups"
+ " (contactId INT NOT NULL," + " (contactId INT UNSIGNED NOT NULL,"
+ " groupId HASH NOT NULL," // Not a foreign key + " groupId HASH NOT NULL," // Not a foreign key
+ " name VARCHAR NOT NULL," + " name VARCHAR NOT NULL,"
+ " key BINARY," // Null for unrestricted groups + " key BINARY," // Null for unrestricted groups
@@ -93,11 +93,12 @@ abstract class JdbcDatabase implements Database<Connection> {
// Locking: contact read, subscription // Locking: contact read, subscription
private static final String CREATE_GROUP_VERSIONS = private static final String CREATE_GROUP_VERSIONS =
"CREATE TABLE groupVersions" "CREATE TABLE groupVersions"
+ " (contactId INT NOT NULL," + " (contactId INT UNSIGNED NOT NULL,"
+ " localVersion BIGINT NOT NULL," + " localVersion BIGINT UNSIGNED NOT NULL,"
+ " localAcked BIGINT NOT NULL," + " localAcked BIGINT UNSIGNED NOT NULL,"
+ " remoteVersion BIGINT NOT NULL," + " remoteVersion BIGINT UNSIGNED NOT NULL,"
+ " remoteAcked BOOLEAN NOT NULL," + " remoteAcked BOOLEAN NOT NULL,"
+ " expiry BIGINT UNSIGNED NOT NULL,"
+ " PRIMARY KEY (contactId)," + " PRIMARY KEY (contactId),"
+ " FOREIGN KEY (contactid)" + " FOREIGN KEY (contactid)"
+ " REFERENCES contacts (contactId)" + " REFERENCES contacts (contactId)"
@@ -111,13 +112,13 @@ abstract class JdbcDatabase implements Database<Connection> {
+ " groupId HASH," // Null for private messages + " groupId HASH," // Null for private messages
+ " authorId HASH," // Null for private or anonymous msgs + " authorId HASH," // Null for private or anonymous msgs
+ " subject VARCHAR NOT NULL," + " subject VARCHAR NOT NULL,"
+ " timestamp BIGINT NOT NULL," + " timestamp BIGINT UNSIGNED NOT NULL,"
+ " length INT NOT NULL," + " length INT UNSIGNED NOT NULL,"
+ " bodyStart INT NOT NULL," + " bodyStart INT UNSIGNED NOT NULL,"
+ " bodyLength INT NOT NULL," + " bodyLength INT UNSIGNED NOT NULL,"
+ " raw BLOB NOT NULL," + " raw BLOB NOT NULL,"
+ " sendability INT," // Null for private messages + " sendability INT UNSIGNED," // Null for private messages
+ " contactId INT," // Null for group messages + " contactId INT UNSIGNED," // Null for group messages
+ " read BOOLEAN NOT NULL," + " read BOOLEAN NOT NULL,"
+ " starred BOOLEAN NOT NULL," + " starred BOOLEAN NOT NULL,"
+ " PRIMARY KEY (messageId)," + " PRIMARY KEY (messageId),"
@@ -144,7 +145,7 @@ abstract class JdbcDatabase implements Database<Connection> {
private static final String CREATE_MESSAGES_TO_ACK = private static final String CREATE_MESSAGES_TO_ACK =
"CREATE TABLE messagesToAck" "CREATE TABLE messagesToAck"
+ " (messageId HASH NOT NULL," + " (messageId HASH NOT NULL,"
+ " contactId INT NOT NULL," + " contactId INT UNSIGNED NOT NULL,"
+ " PRIMARY KEY (messageId, contactId)," + " PRIMARY KEY (messageId, contactId),"
+ " FOREIGN KEY (contactId)" + " FOREIGN KEY (contactId)"
+ " REFERENCES contacts (contactId)" + " REFERENCES contacts (contactId)"
@@ -154,9 +155,9 @@ abstract class JdbcDatabase implements Database<Connection> {
private static final String CREATE_STATUSES = private static final String CREATE_STATUSES =
"CREATE TABLE statuses" "CREATE TABLE statuses"
+ " (messageId HASH NOT NULL," + " (messageId HASH NOT NULL,"
+ " contactId INT NOT NULL," + " contactId INT UNSIGNED NOT NULL,"
+ " seen BOOLEAN NOT NULL," + " seen BOOLEAN NOT NULL,"
+ " expiry BIGINT NOT NULL," + " expiry BIGINT UNSIGNED NOT NULL,"
+ " PRIMARY KEY (messageId, contactId)," + " PRIMARY KEY (messageId, contactId),"
+ " FOREIGN KEY (messageId)" + " FOREIGN KEY (messageId)"
+ " REFERENCES messages (messageId)" + " REFERENCES messages (messageId)"
@@ -181,12 +182,13 @@ abstract class JdbcDatabase implements Database<Connection> {
// Locking: contact read, retention // Locking: contact read, retention
private static final String CREATE_RETENTION_VERSIONS = private static final String CREATE_RETENTION_VERSIONS =
"CREATE TABLE retentionVersions" "CREATE TABLE retentionVersions"
+ " (contactId INT NOT NULL," + " (contactId INT UNSIGNED NOT NULL,"
+ " retention BIGINT NOT NULL," + " retention BIGINT UNSIGNED NOT NULL,"
+ " localVersion BIGINT NOT NULL," + " localVersion BIGINT UNSIGNED NOT NULL,"
+ " localAcked BIGINT NOT NULL," + " localAcked BIGINT UNSIGNED NOT NULL,"
+ " remoteVersion BIGINT NOT NULL," + " remoteVersion BIGINT UNSIGNED NOT NULL,"
+ " remoteAcked BOOLEAN NOT NULL," + " remoteAcked BOOLEAN NOT NULL,"
+ " expiry BIGINT UNSIGNED NOT NULL,"
+ " PRIMARY KEY (contactId)," + " PRIMARY KEY (contactId),"
+ " FOREIGN KEY (contactId)" + " FOREIGN KEY (contactId)"
+ " REFERENCES contacts (contactId)" + " REFERENCES contacts (contactId)"
@@ -221,10 +223,11 @@ abstract class JdbcDatabase implements Database<Connection> {
// Locking: contact read, transport // Locking: contact read, transport
private static final String CREATE_TRANSPORT_VERSIONS = private static final String CREATE_TRANSPORT_VERSIONS =
"CREATE TABLE transportVersions" "CREATE TABLE transportVersions"
+ " (contactId HASH NOT NULL," + " (contactId INT UNSIGNED NOT NULL,"
+ " transportId HASH NOT NULL," + " transportId HASH NOT NULL,"
+ " localVersion BIGINT NOT NULL," + " localVersion BIGINT UNSIGNED NOT NULL,"
+ " localAcked BIGINT NOT NULL," + " localAcked BIGINT UNSIGNED NOT NULL,"
+ " expiry BIGINT UNSIGNED NOT NULL,"
+ " PRIMARY KEY (contactId, transportId)," + " PRIMARY KEY (contactId, transportId),"
+ " FOREIGN KEY (contactId)" + " FOREIGN KEY (contactId)"
+ " REFERENCES contacts (contactId)" + " REFERENCES contacts (contactId)"
@@ -236,7 +239,7 @@ abstract class JdbcDatabase implements Database<Connection> {
// Locking: contact read, transport // Locking: contact read, transport
private static final String CREATE_CONTACT_TRANSPORT_PROPS = private static final String CREATE_CONTACT_TRANSPORT_PROPS =
"CREATE TABLE contactTransportProperties" "CREATE TABLE contactTransportProperties"
+ " (contactId INT NOT NULL," + " (contactId INT UNSIGNED NOT NULL,"
+ " transportId HASH NOT NULL," // Not a foreign key + " transportId HASH NOT NULL," // Not a foreign key
+ " key VARCHAR NOT NULL," + " key VARCHAR NOT NULL,"
+ " value VARCHAR NOT NULL," + " value VARCHAR NOT NULL,"
@@ -248,9 +251,9 @@ abstract class JdbcDatabase implements Database<Connection> {
// Locking: contact read, transport // Locking: contact read, transport
private static final String CREATE_CONTACT_TRANSPORT_VERSIONS = private static final String CREATE_CONTACT_TRANSPORT_VERSIONS =
"CREATE TABLE contactTransportVersions" "CREATE TABLE contactTransportVersions"
+ " (contactId HASH NOT NULL," + " (contactId INT UNSIGNED NOT NULL,"
+ " transportId HASH NOT NULL," // Not a foreign key + " transportId HASH NOT NULL," // Not a foreign key
+ " remoteVersion BIGINT NOT NULL," + " remoteVersion BIGINT UNSIGNED NOT NULL,"
+ " remoteAcked BOOLEAN NOT NULL," + " remoteAcked BOOLEAN NOT NULL,"
+ " PRIMARY KEY (contactId, transportId)," + " PRIMARY KEY (contactId, transportId),"
+ " FOREIGN KEY (contactId)" + " FOREIGN KEY (contactId)"
@@ -260,11 +263,11 @@ abstract class JdbcDatabase implements Database<Connection> {
// Locking: contact read, transport read, window // Locking: contact read, transport read, window
private static final String CREATE_ENDPOINTS = private static final String CREATE_ENDPOINTS =
"CREATE TABLE endpoints" "CREATE TABLE endpoints"
+ " (contactId INT NOT NULL," + " (contactId INT UNSIGNED NOT NULL,"
+ " transportId HASH NOT NULL," + " transportId HASH NOT NULL,"
+ " epoch BIGINT NOT NULL," + " epoch BIGINT UNSIGNED NOT NULL,"
+ " clockDiff BIGINT NOT NULL," + " clockDiff BIGINT UNSIGNED NOT NULL,"
+ " latency BIGINT NOT NULL," + " latency BIGINT UNSIGNED NOT NULL,"
+ " alice BOOLEAN NOT NULL," + " alice BOOLEAN NOT NULL,"
+ " PRIMARY KEY (contactId, transportId)," + " PRIMARY KEY (contactId, transportId),"
+ " FOREIGN KEY (contactId)" + " FOREIGN KEY (contactId)"
@@ -277,12 +280,12 @@ abstract class JdbcDatabase implements Database<Connection> {
// Locking: contact read, transport read, window // Locking: contact read, transport read, window
private static final String CREATE_SECRETS = private static final String CREATE_SECRETS =
"CREATE TABLE secrets" "CREATE TABLE secrets"
+ " (contactId INT NOT NULL," + " (contactId INT UNSIGNED NOT NULL,"
+ " transportId HASH NOT NULL," + " transportId HASH NOT NULL,"
+ " period BIGINT NOT NULL," + " period BIGINT UNSIGNED NOT NULL,"
+ " secret SECRET NOT NULL," + " secret SECRET NOT NULL,"
+ " outgoing BIGINT NOT NULL," + " outgoing BIGINT UNSIGNED NOT NULL,"
+ " centre BIGINT NOT NULL," + " centre BIGINT UNSIGNED NOT NULL,"
+ " bitmap BINARY NOT NULL," + " bitmap BINARY NOT NULL,"
+ " PRIMARY KEY (contactId, transportId, period)," + " PRIMARY KEY (contactId, transportId, period),"
+ " FOREIGN KEY (contactId)" + " FOREIGN KEY (contactId)"
@@ -505,9 +508,10 @@ abstract class JdbcDatabase implements Database<Connection> {
rs.close(); rs.close();
ps.close(); ps.close();
// Create a retention version row // Create a retention version row
sql = "INSERT INTO retentionVersions (contactId, retention," sql = "INSERT INTO retentionVersions"
+ " localVersion, localAcked, remoteVersion, remoteAcked)" + " (contactId, retention, localVersion, localAcked,"
+ " VALUES (?, ZERO(), ?, ZERO(), ZERO(), TRUE)"; + " remoteVersion, remoteAcked, expiry)"
+ " VALUES (?, ZERO(), ?, ZERO(), ZERO(), TRUE, ZERO())";
ps = txn.prepareStatement(sql); ps = txn.prepareStatement(sql);
ps.setInt(1, c.getInt()); ps.setInt(1, c.getInt());
ps.setInt(2, 1); ps.setInt(2, 1);
@@ -516,8 +520,8 @@ abstract class JdbcDatabase implements Database<Connection> {
ps.close(); ps.close();
// Create a group version row // Create a group version row
sql = "INSERT INTO groupVersions (contactId, localVersion," sql = "INSERT INTO groupVersions (contactId, localVersion,"
+ " localAcked, remoteVersion, remoteAcked)" + " localAcked, remoteVersion, remoteAcked, expiry)"
+ " VALUES (?, ?, ZERO(), ZERO(), TRUE)"; + " VALUES (?, ?, ZERO(), ZERO(), TRUE, ZERO())";
ps = txn.prepareStatement(sql); ps = txn.prepareStatement(sql);
ps.setInt(1, c.getInt()); ps.setInt(1, c.getInt());
ps.setInt(2, 1); ps.setInt(2, 1);
@@ -533,9 +537,9 @@ abstract class JdbcDatabase implements Database<Connection> {
rs.close(); rs.close();
ps.close(); ps.close();
if(transports.isEmpty()) return c; if(transports.isEmpty()) return c;
sql = "INSERT INTO transportVersions" sql = "INSERT INTO transportVersions (contactId, transportId,"
+ " (contactId, transportId, localVersion, localAcked)" + " localVersion, localAcked, expiry)"
+ " VALUES (?, ?, ?, ZERO())"; + " VALUES (?, ?, ?, ZERO(), ZERO())";
ps = txn.prepareStatement(sql); ps = txn.prepareStatement(sql);
ps.setInt(1, c.getInt()); ps.setInt(1, c.getInt());
ps.setInt(3, 1); ps.setInt(3, 1);
@@ -646,33 +650,6 @@ abstract class JdbcDatabase implements Database<Connection> {
} }
} }
public void addOutstandingMessages(Connection txn, ContactId c,
Collection<MessageId> sent, long expiry) throws DbException {
PreparedStatement ps = null;
try {
// Update the expiry time of each message
String sql = "UPDATE statuses SET expiry = ?"
+ " WHERE messageId = ? AND contactId = ?";
ps = txn.prepareStatement(sql);
ps.setLong(1, expiry);
ps.setInt(3, c.getInt());
for(MessageId m : sent) {
ps.setBytes(2, m.getBytes());
ps.addBatch();
}
int[] batchAffected = ps.executeBatch();
if(batchAffected.length != sent.size())
throw new DbStateException();
for(int i = 0; i < batchAffected.length; i++) {
if(batchAffected[i] > 1) throw new DbStateException();
}
ps.close();
} catch(SQLException e) {
tryToClose(ps);
throw new DbException(e);
}
}
public boolean addPrivateMessage(Connection txn, Message m, ContactId c) public boolean addPrivateMessage(Connection txn, Message m, ContactId c)
throws DbException { throws DbException {
assert m.getGroup() == null; assert m.getGroup() == null;
@@ -812,9 +789,9 @@ abstract class JdbcDatabase implements Database<Connection> {
rs.close(); rs.close();
ps.close(); ps.close();
if(contacts.isEmpty()) return; if(contacts.isEmpty()) return;
sql = "INSERT INTO transportVersions" sql = "INSERT INTO transportVersions (contactId, transportId,"
+ " (contactId, transportId, localVersion, localAcked)" + " localVersion, localAcked, expiry)"
+ " VALUES (?, ?, ?, ZERO())"; + " VALUES (?, ?, ?, ZERO(), ZERO())";
ps = txn.prepareStatement(sql); ps = txn.prepareStatement(sql);
ps.setBytes(2, t.getBytes()); ps.setBytes(2, t.getBytes());
ps.setInt(3, 1); ps.setInt(3, 1);
@@ -848,7 +825,8 @@ abstract class JdbcDatabase implements Database<Connection> {
if(affected != 1) throw new DbStateException(); if(affected != 1) throw new DbStateException();
ps.close(); ps.close();
// Bump the subscription version // Bump the subscription version
sql = "UPDATE groupVersions SET localVersion = localVersion + ?" sql = "UPDATE groupVersions"
+ " SET localVersion = localVersion + ?, expiry = ZERO()"
+ " WHERE contactId = ?"; + " WHERE contactId = ?";
ps = txn.prepareStatement(sql); ps = txn.prepareStatement(sql);
ps.setInt(1, 1); ps.setInt(1, 1);
@@ -1444,7 +1422,7 @@ abstract class JdbcDatabase implements Database<Connection> {
+ " WHERE m.messageId = ?" + " WHERE m.messageId = ?"
+ " AND cg.contactId = ?" + " AND cg.contactId = ?"
+ " AND timestamp >= retention" + " AND timestamp >= retention"
+ " AND seen = FALSE AND expiry < ?" + " AND seen = FALSE AND s.expiry < ?"
+ " AND sendability > ZERO()"; + " AND sendability > ZERO()";
ps = txn.prepareStatement(sql); ps = txn.prepareStatement(sql);
ps.setBytes(1, m.getBytes()); ps.setBytes(1, m.getBytes());
@@ -1557,19 +1535,23 @@ abstract class JdbcDatabase implements Database<Connection> {
} }
} }
public RetentionUpdate getRetentionUpdate(Connection txn, ContactId c) public RetentionUpdate getRetentionUpdate(Connection txn, ContactId c,
throws DbException { long maxLatency) throws DbException {
long now = clock.currentTimeMillis();
PreparedStatement ps = null; PreparedStatement ps = null;
ResultSet rs = null; ResultSet rs = null;
try { try {
String sql = "SELECT timestamp, localVersion" String sql = "SELECT timestamp, localVersion"
+ " FROM messages AS m" + " FROM messages AS m"
+ " JOIN retentionVersions AS rv" + " JOIN retentionVersions AS rv"
+ " WHERE rv.contactId = ? AND localVersion > localAcked" + " WHERE rv.contactId = ?"
+ " AND localVersion > localAcked"
+ " AND expiry < ?"
+ " ORDER BY timestamp LIMIT ?"; + " ORDER BY timestamp LIMIT ?";
ps = txn.prepareStatement(sql); ps = txn.prepareStatement(sql);
ps.setInt(1, c.getInt()); ps.setInt(1, c.getInt());
ps.setInt(2, 1); ps.setLong(2, now);
ps.setInt(3, 1);
rs = ps.executeQuery(); rs = ps.executeQuery();
if(!rs.next()) { if(!rs.next()) {
rs.close(); rs.close();
@@ -1582,6 +1564,13 @@ abstract class JdbcDatabase implements Database<Connection> {
if(rs.next()) throw new DbStateException(); if(rs.next()) throw new DbStateException();
rs.close(); rs.close();
ps.close(); ps.close();
sql = "UPDATE retentionVersions SET expiry = ? WHERE contactId = ?";
ps = txn.prepareStatement(sql);
ps.setLong(1, calculateExpiry(now, maxLatency));
ps.setInt(2, c.getInt());
int affected = ps.executeUpdate();
if(affected != 1) throw new DbStateException();
ps.close();
return new RetentionUpdate(retention, version); return new RetentionUpdate(retention, version);
} catch(SQLException e) { } catch(SQLException e) {
tryToClose(ps); tryToClose(ps);
@@ -1692,7 +1681,7 @@ abstract class JdbcDatabase implements Database<Connection> {
+ " AND cg.contactId = s.contactId" + " AND cg.contactId = s.contactId"
+ " WHERE cg.contactId = ?" + " WHERE cg.contactId = ?"
+ " AND timestamp >= retention" + " AND timestamp >= retention"
+ " AND seen = FALSE AND expiry < ?" + " AND seen = FALSE AND s.expiry < ?"
+ " AND sendability > ZERO()" + " AND sendability > ZERO()"
+ " ORDER BY timestamp DESC"; + " ORDER BY timestamp DESC";
ps = txn.prepareStatement(sql); ps = txn.prepareStatement(sql);
@@ -1822,8 +1811,9 @@ abstract class JdbcDatabase implements Database<Connection> {
} }
} }
public SubscriptionUpdate getSubscriptionUpdate(Connection txn, ContactId c) public SubscriptionUpdate getSubscriptionUpdate(Connection txn, ContactId c,
throws DbException { long maxLatency) throws DbException {
long now = clock.currentTimeMillis();
PreparedStatement ps = null; PreparedStatement ps = null;
ResultSet rs = null; ResultSet rs = null;
try { try {
@@ -1834,9 +1824,11 @@ abstract class JdbcDatabase implements Database<Connection> {
+ " JOIN groupVersions AS ver" + " JOIN groupVersions AS ver"
+ " ON vis.contactId = ver.contactId" + " ON vis.contactId = ver.contactId"
+ " WHERE vis.contactId = ?" + " WHERE vis.contactId = ?"
+ " AND localVersion > localAcked"; + " AND localVersion > localAcked"
+ " AND expiry < ?";
ps = txn.prepareStatement(sql); ps = txn.prepareStatement(sql);
ps.setInt(1, c.getInt()); ps.setInt(1, c.getInt());
ps.setLong(2, now);
rs = ps.executeQuery(); rs = ps.executeQuery();
List<Group> subs = new ArrayList<Group>(); List<Group> subs = new ArrayList<Group>();
long version = 0; long version = 0;
@@ -1850,6 +1842,13 @@ abstract class JdbcDatabase implements Database<Connection> {
rs.close(); rs.close();
ps.close(); ps.close();
if(subs.isEmpty()) return null; if(subs.isEmpty()) return null;
sql = "UPDATE groupVersions SET expiry = ? WHERE contactId = ?";
ps = txn.prepareStatement(sql);
ps.setLong(1, calculateExpiry(now, maxLatency));
ps.setInt(2, c.getInt());
int affected = ps.executeUpdate();
if(affected != 1) throw new DbStateException();
ps.close();
subs = Collections.unmodifiableList(subs); subs = Collections.unmodifiableList(subs);
return new SubscriptionUpdate(subs, version); return new SubscriptionUpdate(subs, version);
} catch(SQLException e) { } catch(SQLException e) {
@@ -1902,7 +1901,8 @@ abstract class JdbcDatabase implements Database<Connection> {
} }
public Collection<TransportUpdate> getTransportUpdates(Connection txn, public Collection<TransportUpdate> getTransportUpdates(Connection txn,
ContactId c) throws DbException { ContactId c, long maxLatency) throws DbException {
long now = clock.currentTimeMillis();
PreparedStatement ps = null; PreparedStatement ps = null;
ResultSet rs = null; ResultSet rs = null;
try { try {
@@ -1911,9 +1911,11 @@ abstract class JdbcDatabase implements Database<Connection> {
+ " JOIN transportVersions AS tv" + " JOIN transportVersions AS tv"
+ " ON tp.transportId = tv.transportId" + " ON tp.transportId = tv.transportId"
+ " WHERE tv.contactId = ?" + " WHERE tv.contactId = ?"
+ " AND localVersion > localAcked"; + " AND localVersion > localAcked"
+ " AND expiry < ?";
ps = txn.prepareStatement(sql); ps = txn.prepareStatement(sql);
ps.setInt(1, c.getInt()); ps.setInt(1, c.getInt());
ps.setLong(2, now);
rs = ps.executeQuery(); rs = ps.executeQuery();
List<TransportUpdate> updates = new ArrayList<TransportUpdate>(); List<TransportUpdate> updates = new ArrayList<TransportUpdate>();
TransportId lastId = null; TransportId lastId = null;
@@ -1931,6 +1933,22 @@ abstract class JdbcDatabase implements Database<Connection> {
rs.close(); rs.close();
ps.close(); ps.close();
if(updates.isEmpty()) return null; if(updates.isEmpty()) return null;
sql = "UPDATE transportVersions SET expiry = ?"
+ " WHERE contactId = ? AND transportId = ?";
ps = txn.prepareStatement(sql);
ps.setLong(1, calculateExpiry(now, maxLatency));
ps.setInt(2, c.getInt());
for(TransportUpdate u : updates) {
ps.setBytes(3, u.getId().getBytes());
ps.addBatch();
}
int [] batchAffected = ps.executeBatch();
if(batchAffected.length != updates.size())
throw new DbStateException();
for(int i = 0; i < batchAffected.length; i++) {
if(batchAffected[i] != 1) throw new DbStateException();
}
ps.close();
return Collections.unmodifiableList(updates); return Collections.unmodifiableList(updates);
} catch(SQLException e) { } catch(SQLException e) {
tryToClose(ps); tryToClose(ps);
@@ -2045,7 +2063,7 @@ abstract class JdbcDatabase implements Database<Connection> {
+ " AND cg.contactId = s.contactId" + " AND cg.contactId = s.contactId"
+ " WHERE cg.contactId = ?" + " WHERE cg.contactId = ?"
+ " AND timestamp >= retention" + " AND timestamp >= retention"
+ " AND seen = FALSE AND expiry < ?" + " AND seen = FALSE AND s.expiry < ?"
+ " AND sendability > ZERO()" + " AND sendability > ZERO()"
+ " LIMIT ?"; + " LIMIT ?";
ps = txn.prepareStatement(sql); ps = txn.prepareStatement(sql);
@@ -2110,7 +2128,7 @@ abstract class JdbcDatabase implements Database<Connection> {
PreparedStatement ps = null; PreparedStatement ps = null;
try { try {
String sql = "UPDATE retentionVersions" String sql = "UPDATE retentionVersions"
+ " SET localVersion = localVersion + ?"; + " SET localVersion = localVersion + ?, expiry = ZERO()";
ps = txn.prepareStatement(sql); ps = txn.prepareStatement(sql);
ps.setInt(1, 1); ps.setInt(1, 1);
ps.executeUpdate(); ps.executeUpdate();
@@ -2226,7 +2244,8 @@ abstract class JdbcDatabase implements Database<Connection> {
ps.close(); ps.close();
if(visible.isEmpty()) return; if(visible.isEmpty()) return;
// Bump the subscription version for the affected contacts // Bump the subscription version for the affected contacts
sql = "UPDATE groupVersions SET localVersion = localVersion + ?" sql = "UPDATE groupVersions"
+ " SET localVersion = localVersion + ?, expiry = ZERO()"
+ " WHERE contactId = ?"; + " WHERE contactId = ?";
ps = txn.prepareStatement(sql); ps = txn.prepareStatement(sql);
ps.setInt(1, 1); ps.setInt(1, 1);
@@ -2277,7 +2296,8 @@ abstract class JdbcDatabase implements Database<Connection> {
if(affected != 1) throw new DbStateException(); if(affected != 1) throw new DbStateException();
ps.close(); ps.close();
// Bump the subscription version // Bump the subscription version
sql = "UPDATE groupVersions SET localVersion = localVersion + ?" sql = "UPDATE groupVersions"
+ " SET localVersion = localVersion + ?, expiry = ZERO()"
+ " WHERE contactId = ?"; + " WHERE contactId = ?";
ps = txn.prepareStatement(sql); ps = txn.prepareStatement(sql);
ps.setInt(1, 1); ps.setInt(1, 1);
@@ -2305,7 +2325,7 @@ abstract class JdbcDatabase implements Database<Connection> {
PreparedStatement ps = null; PreparedStatement ps = null;
try { try {
String sql = "UPDATE transportVersions" String sql = "UPDATE transportVersions"
+ " SET localVersion = localVersion + ?" + " SET localVersion = localVersion + ?, expiry = ZERO()"
+ " WHERE transportId = ?"; + " WHERE transportId = ?";
ps = txn.prepareStatement(sql); ps = txn.prepareStatement(sql);
ps.setInt(1, 1); ps.setInt(1, 1);
@@ -2385,20 +2405,26 @@ abstract class JdbcDatabase implements Database<Connection> {
} }
} }
public void setRetentionTime(Connection txn, ContactId c, long retention, public void setMessageExpiry(Connection txn, ContactId c,
long version) throws DbException { Collection<MessageId> sent, long maxLatency) throws DbException {
long now = clock.currentTimeMillis();
PreparedStatement ps = null; PreparedStatement ps = null;
try { try {
String sql = "UPDATE retentionVersions SET retention = ?," String sql = "UPDATE statuses SET expiry = ?"
+ " remoteVersion = ?, remoteAcked = FALSE" + " WHERE messageId = ? AND contactId = ?";
+ " WHERE contactId = ? AND remoteVersion < ?";
ps = txn.prepareStatement(sql); ps = txn.prepareStatement(sql);
ps.setLong(1, retention); ps.setLong(1, calculateExpiry(now, maxLatency));
ps.setLong(2, version);
ps.setInt(3, c.getInt()); ps.setInt(3, c.getInt());
ps.setLong(4, version); for(MessageId m : sent) {
int affected = ps.executeUpdate(); ps.setBytes(2, m.getBytes());
if(affected > 1) throw new DbStateException(); ps.addBatch();
}
int[] batchAffected = ps.executeBatch();
if(batchAffected.length != sent.size())
throw new DbStateException();
for(int i = 0; i < batchAffected.length; i++) {
if(batchAffected[i] > 1) throw new DbStateException();
}
ps.close(); ps.close();
} catch(SQLException e) { } catch(SQLException e) {
tryToClose(ps); tryToClose(ps);
@@ -2406,25 +2432,12 @@ abstract class JdbcDatabase implements Database<Connection> {
} }
} }
public void setRetentionUpdateAcked(Connection txn, ContactId c, private long calculateExpiry(long now, long maxLatency) {
long version) throws DbException { long roundTrip = maxLatency * 2;
PreparedStatement ps = null; if(roundTrip < 0) return Long.MAX_VALUE; // Overflow;
try { long expiry = now + roundTrip;
String sql = "UPDATE retentionVersions SET localAcked = ?" if(expiry < 0) return Long.MAX_VALUE; // Overflow
+ " WHERE contactId = ?" return expiry;
+ " AND localAcked < ? AND localVersion >= ?";
ps = txn.prepareStatement(sql);
ps.setLong(1, version);
ps.setInt(2, c.getInt());
ps.setLong(3, version);
ps.setLong(4, version);
int affected = ps.executeUpdate();
if(affected > 1) throw new DbStateException();
ps.close();
} catch(SQLException e) {
tryToClose(ps);
throw new DbException(e);
}
} }
public Rating setRating(Connection txn, AuthorId a, Rating r) public Rating setRating(Connection txn, AuthorId a, Rating r)
@@ -2586,6 +2599,48 @@ abstract class JdbcDatabase implements Database<Connection> {
} }
} }
public void setRetentionTime(Connection txn, ContactId c, long retention,
long version) throws DbException {
PreparedStatement ps = null;
try {
String sql = "UPDATE retentionVersions SET retention = ?,"
+ " remoteVersion = ?, remoteAcked = FALSE"
+ " WHERE contactId = ? AND remoteVersion < ?";
ps = txn.prepareStatement(sql);
ps.setLong(1, retention);
ps.setLong(2, version);
ps.setInt(3, c.getInt());
ps.setLong(4, version);
int affected = ps.executeUpdate();
if(affected > 1) throw new DbStateException();
ps.close();
} catch(SQLException e) {
tryToClose(ps);
throw new DbException(e);
}
}
public void setRetentionUpdateAcked(Connection txn, ContactId c,
long version) throws DbException {
PreparedStatement ps = null;
try {
String sql = "UPDATE retentionVersions SET localAcked = ?"
+ " WHERE contactId = ?"
+ " AND localAcked < ? AND localVersion >= ?";
ps = txn.prepareStatement(sql);
ps.setLong(1, version);
ps.setInt(2, c.getInt());
ps.setLong(3, version);
ps.setLong(4, version);
int affected = ps.executeUpdate();
if(affected > 1) throw new DbStateException();
ps.close();
} catch(SQLException e) {
tryToClose(ps);
throw new DbException(e);
}
}
public void setSendability(Connection txn, MessageId m, int sendability) public void setSendability(Connection txn, MessageId m, int sendability)
throws DbException { throws DbException {
PreparedStatement ps = null; PreparedStatement ps = null;

View File

@@ -3,6 +3,7 @@ package net.sf.briar.messaging.duplex;
import static java.util.logging.Level.INFO; import static java.util.logging.Level.INFO;
import static java.util.logging.Level.WARNING; import static java.util.logging.Level.WARNING;
import static net.sf.briar.api.Rating.GOOD; import static net.sf.briar.api.Rating.GOOD;
import static net.sf.briar.api.messaging.MessagingConstants.MAX_PACKET_LENGTH;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
@@ -87,6 +88,7 @@ abstract class DuplexConnection implements DatabaseListener {
private final Executor dbExecutor, verificationExecutor; private final Executor dbExecutor, verificationExecutor;
private final MessageVerifier messageVerifier; private final MessageVerifier messageVerifier;
private final long maxLatency;
private final AtomicBoolean canSendOffer, disposed; private final AtomicBoolean canSendOffer, disposed;
private final BlockingQueue<Runnable> writerTasks; private final BlockingQueue<Runnable> writerTasks;
@@ -116,6 +118,7 @@ abstract class DuplexConnection implements DatabaseListener {
this.transport = transport; this.transport = transport;
contactId = ctx.getContactId(); contactId = ctx.getContactId();
transportId = ctx.getTransportId(); transportId = ctx.getTransportId();
maxLatency = transport.getMaxLatency();
canSendOffer = new AtomicBoolean(false); canSendOffer = new AtomicBoolean(false);
disposed = new AtomicBoolean(false); disposed = new AtomicBoolean(false);
writerTasks = new LinkedBlockingQueue<Runnable>(); writerTasks = new LinkedBlockingQueue<Runnable>();
@@ -467,8 +470,7 @@ abstract class DuplexConnection implements DatabaseListener {
assert writer != null; assert writer != null;
try { try {
Collection<byte[]> batch = db.generateBatch(contactId, Collection<byte[]> batch = db.generateBatch(contactId,
Integer.MAX_VALUE, transport.getMaxLatency(), MAX_PACKET_LENGTH, maxLatency, requested);
requested);
if(batch == null) new GenerateOffer().run(); if(batch == null) new GenerateOffer().run();
else writerTasks.add(new WriteBatch(batch, requested)); else writerTasks.add(new WriteBatch(batch, requested));
} catch(DbException e) { } catch(DbException e) {
@@ -583,7 +585,8 @@ abstract class DuplexConnection implements DatabaseListener {
public void run() { public void run() {
try { try {
RetentionUpdate u = db.generateRetentionUpdate(contactId); RetentionUpdate u =
db.generateRetentionUpdate(contactId, maxLatency);
if(u != null) writerTasks.add(new WriteRetentionUpdate(u)); if(u != null) writerTasks.add(new WriteRetentionUpdate(u));
} catch(DbException e) { } catch(DbException e) {
if(LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e); if(LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e);
@@ -649,7 +652,8 @@ abstract class DuplexConnection implements DatabaseListener {
public void run() { public void run() {
try { try {
SubscriptionUpdate u = db.generateSubscriptionUpdate(contactId); SubscriptionUpdate u =
db.generateSubscriptionUpdate(contactId, maxLatency);
if(u != null) writerTasks.add(new WriteSubscriptionUpdate(u)); if(u != null) writerTasks.add(new WriteSubscriptionUpdate(u));
} catch(DbException e) { } catch(DbException e) {
if(LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e); if(LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e);
@@ -717,7 +721,7 @@ abstract class DuplexConnection implements DatabaseListener {
public void run() { public void run() {
try { try {
Collection<TransportUpdate> t = Collection<TransportUpdate> t =
db.generateTransportUpdates(contactId); db.generateTransportUpdates(contactId, maxLatency);
if(t != null) writerTasks.add(new WriteTransportUpdates(t)); if(t != null) writerTasks.add(new WriteTransportUpdates(t));
} catch(DbException e) { } catch(DbException e) {
if(LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e); if(LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e);

View File

@@ -42,6 +42,7 @@ class OutgoingSimplexConnection {
private final SimplexTransportWriter transport; private final SimplexTransportWriter transport;
private final ContactId contactId; private final ContactId contactId;
private final TransportId transportId; private final TransportId transportId;
private final long maxLatency;
OutgoingSimplexConnection(DatabaseComponent db, OutgoingSimplexConnection(DatabaseComponent db,
ConnectionRegistry connRegistry, ConnectionRegistry connRegistry,
@@ -56,6 +57,7 @@ class OutgoingSimplexConnection {
this.transport = transport; this.transport = transport;
contactId = ctx.getContactId(); contactId = ctx.getContactId();
transportId = ctx.getTransportId(); transportId = ctx.getTransportId();
maxLatency = transport.getMaxLatency();
} }
void write() { void write() {
@@ -69,7 +71,6 @@ class OutgoingSimplexConnection {
throw new EOFException(); throw new EOFException();
PacketWriter writer = packetWriterFactory.createPacketWriter(out, PacketWriter writer = packetWriterFactory.createPacketWriter(out,
transport.shouldFlush()); transport.shouldFlush());
long maxLatency = transport.getMaxLatency();
// Send the initial packets: updates and acks // Send the initial packets: updates and acks
boolean hasSpace = writeTransportAcks(conn, writer); boolean hasSpace = writeTransportAcks(conn, writer);
if(hasSpace) hasSpace = writeTransportUpdates(conn, writer); if(hasSpace) hasSpace = writeTransportUpdates(conn, writer);
@@ -128,7 +129,7 @@ class OutgoingSimplexConnection {
PacketWriter writer) throws DbException, IOException { PacketWriter writer) throws DbException, IOException {
assert conn.getRemainingCapacity() >= MAX_PACKET_LENGTH; assert conn.getRemainingCapacity() >= MAX_PACKET_LENGTH;
Collection<TransportUpdate> updates = Collection<TransportUpdate> updates =
db.generateTransportUpdates(contactId); db.generateTransportUpdates(contactId, maxLatency);
if(updates == null) return true; if(updates == null) return true;
for(TransportUpdate u : updates) { for(TransportUpdate u : updates) {
writer.writeTransportUpdate(u); writer.writeTransportUpdate(u);
@@ -149,7 +150,8 @@ class OutgoingSimplexConnection {
private boolean writeSubscriptionUpdate(ConnectionWriter conn, private boolean writeSubscriptionUpdate(ConnectionWriter conn,
PacketWriter writer) throws DbException, IOException { PacketWriter writer) throws DbException, IOException {
assert conn.getRemainingCapacity() >= MAX_PACKET_LENGTH; assert conn.getRemainingCapacity() >= MAX_PACKET_LENGTH;
SubscriptionUpdate u = db.generateSubscriptionUpdate(contactId); SubscriptionUpdate u =
db.generateSubscriptionUpdate(contactId, maxLatency);
if(u == null) return true; if(u == null) return true;
writer.writeSubscriptionUpdate(u); writer.writeSubscriptionUpdate(u);
return conn.getRemainingCapacity() >= MAX_PACKET_LENGTH; return conn.getRemainingCapacity() >= MAX_PACKET_LENGTH;
@@ -167,7 +169,7 @@ class OutgoingSimplexConnection {
private boolean writeRetentionUpdate(ConnectionWriter conn, private boolean writeRetentionUpdate(ConnectionWriter conn,
PacketWriter writer) throws DbException, IOException { PacketWriter writer) throws DbException, IOException {
assert conn.getRemainingCapacity() >= MAX_PACKET_LENGTH; assert conn.getRemainingCapacity() >= MAX_PACKET_LENGTH;
RetentionUpdate u = db.generateRetentionUpdate(contactId); RetentionUpdate u = db.generateRetentionUpdate(contactId, maxLatency);
if(u == null) return true; if(u == null) return true;
writer.writeRetentionUpdate(u); writer.writeRetentionUpdate(u);
return conn.getRemainingCapacity() >= MAX_PACKET_LENGTH; return conn.getRemainingCapacity() >= MAX_PACKET_LENGTH;

View File

@@ -523,7 +523,7 @@ public abstract class DatabaseComponentTest extends BriarTestCase {
} catch(NoSuchContactException expected) {} } catch(NoSuchContactException expected) {}
try { try {
db.generateRetentionUpdate(contactId); db.generateRetentionUpdate(contactId, 123);
fail(); fail();
} catch(NoSuchContactException expected) {} } catch(NoSuchContactException expected) {}
@@ -533,7 +533,7 @@ public abstract class DatabaseComponentTest extends BriarTestCase {
} catch(NoSuchContactException expected) {} } catch(NoSuchContactException expected) {}
try { try {
db.generateSubscriptionUpdate(contactId); db.generateSubscriptionUpdate(contactId, 123);
fail(); fail();
} catch(NoSuchContactException expected) {} } catch(NoSuchContactException expected) {}
@@ -543,7 +543,7 @@ public abstract class DatabaseComponentTest extends BriarTestCase {
} catch(NoSuchContactException expected) {} } catch(NoSuchContactException expected) {}
try { try {
db.generateTransportUpdates(contactId); db.generateTransportUpdates(contactId, 123);
fail(); fail();
} catch(NoSuchContactException expected) {} } catch(NoSuchContactException expected) {}
@@ -696,7 +696,7 @@ public abstract class DatabaseComponentTest extends BriarTestCase {
oneOf(database).getRawMessage(txn, messageId1); oneOf(database).getRawMessage(txn, messageId1);
will(returnValue(raw1)); will(returnValue(raw1));
// Record the outstanding messages // Record the outstanding messages
oneOf(database).addOutstandingMessages(txn, contactId, sendable, oneOf(database).setMessageExpiry(txn, contactId, sendable,
Long.MAX_VALUE); Long.MAX_VALUE);
}}); }});
DatabaseComponent db = createDatabaseComponent(database, cleaner, DatabaseComponent db = createDatabaseComponent(database, cleaner,
@@ -733,8 +733,8 @@ public abstract class DatabaseComponentTest extends BriarTestCase {
will(returnValue(raw1)); // Message is sendable will(returnValue(raw1)); // Message is sendable
oneOf(database).getRawMessageIfSendable(txn, contactId, messageId2); oneOf(database).getRawMessageIfSendable(txn, contactId, messageId2);
will(returnValue(null)); // Message is not sendable will(returnValue(null)); // Message is not sendable
// Record the outstanding message // Mark the message as sent
oneOf(database).addOutstandingMessages(txn, contactId, oneOf(database).setMessageExpiry(txn, contactId,
Arrays.asList(messageId1), Long.MAX_VALUE); Arrays.asList(messageId1), Long.MAX_VALUE);
}}); }});
DatabaseComponent db = createDatabaseComponent(database, cleaner, DatabaseComponent db = createDatabaseComponent(database, cleaner,
@@ -788,13 +788,14 @@ public abstract class DatabaseComponentTest extends BriarTestCase {
allowing(database).commitTransaction(txn); allowing(database).commitTransaction(txn);
allowing(database).containsContact(txn, contactId); allowing(database).containsContact(txn, contactId);
will(returnValue(true)); will(returnValue(true));
oneOf(database).getSubscriptionUpdate(txn, contactId); oneOf(database).getSubscriptionUpdate(txn, contactId,
Long.MAX_VALUE);
will(returnValue(null)); will(returnValue(null));
}}); }});
DatabaseComponent db = createDatabaseComponent(database, cleaner, DatabaseComponent db = createDatabaseComponent(database, cleaner,
shutdown); shutdown);
assertNull(db.generateSubscriptionUpdate(contactId)); assertNull(db.generateSubscriptionUpdate(contactId, Long.MAX_VALUE));
context.assertIsSatisfied(); context.assertIsSatisfied();
} }
@@ -812,13 +813,15 @@ public abstract class DatabaseComponentTest extends BriarTestCase {
allowing(database).commitTransaction(txn); allowing(database).commitTransaction(txn);
allowing(database).containsContact(txn, contactId); allowing(database).containsContact(txn, contactId);
will(returnValue(true)); will(returnValue(true));
oneOf(database).getSubscriptionUpdate(txn, contactId); oneOf(database).getSubscriptionUpdate(txn, contactId,
Long.MAX_VALUE);
will(returnValue(new SubscriptionUpdate(Arrays.asList(group), 1))); will(returnValue(new SubscriptionUpdate(Arrays.asList(group), 1)));
}}); }});
DatabaseComponent db = createDatabaseComponent(database, cleaner, DatabaseComponent db = createDatabaseComponent(database, cleaner,
shutdown); shutdown);
SubscriptionUpdate u = db.generateSubscriptionUpdate(contactId); SubscriptionUpdate u = db.generateSubscriptionUpdate(contactId,
Long.MAX_VALUE);
assertEquals(Arrays.asList(group), u.getGroups()); assertEquals(Arrays.asList(group), u.getGroups());
assertEquals(1, u.getVersion()); assertEquals(1, u.getVersion());
@@ -838,13 +841,13 @@ public abstract class DatabaseComponentTest extends BriarTestCase {
allowing(database).commitTransaction(txn); allowing(database).commitTransaction(txn);
allowing(database).containsContact(txn, contactId); allowing(database).containsContact(txn, contactId);
will(returnValue(true)); will(returnValue(true));
oneOf(database).getTransportUpdates(txn, contactId); oneOf(database).getTransportUpdates(txn, contactId, Long.MAX_VALUE);
will(returnValue(null)); will(returnValue(null));
}}); }});
DatabaseComponent db = createDatabaseComponent(database, cleaner, DatabaseComponent db = createDatabaseComponent(database, cleaner,
shutdown); shutdown);
assertNull(db.generateTransportUpdates(contactId)); assertNull(db.generateTransportUpdates(contactId, Long.MAX_VALUE));
context.assertIsSatisfied(); context.assertIsSatisfied();
} }
@@ -862,15 +865,15 @@ public abstract class DatabaseComponentTest extends BriarTestCase {
allowing(database).commitTransaction(txn); allowing(database).commitTransaction(txn);
allowing(database).containsContact(txn, contactId); allowing(database).containsContact(txn, contactId);
will(returnValue(true)); will(returnValue(true));
oneOf(database).getTransportUpdates(txn, contactId); oneOf(database).getTransportUpdates(txn, contactId, Long.MAX_VALUE);
will(returnValue(Arrays.asList(new TransportUpdate(transportId, will(returnValue(Arrays.asList(new TransportUpdate(transportId,
transportProperties, 1)))); transportProperties, 1))));
}}); }});
DatabaseComponent db = createDatabaseComponent(database, cleaner, DatabaseComponent db = createDatabaseComponent(database, cleaner,
shutdown); shutdown);
Collection<TransportUpdate> updates = db.generateTransportUpdates( Collection<TransportUpdate> updates =
contactId); db.generateTransportUpdates(contactId, Long.MAX_VALUE);
assertNotNull(updates); assertNotNull(updates);
assertEquals(1, updates.size()); assertEquals(1, updates.size());
TransportUpdate u = updates.iterator().next(); TransportUpdate u = updates.iterator().next();

View File

@@ -523,7 +523,7 @@ public class H2DatabaseTest extends BriarTestCase {
assertTrue(it.hasNext()); assertTrue(it.hasNext());
assertEquals(messageId, it.next()); assertEquals(messageId, it.next());
assertFalse(it.hasNext()); assertFalse(it.hasNext());
db.addOutstandingMessages(txn, contactId, Arrays.asList(messageId), db.setMessageExpiry(txn, contactId, Arrays.asList(messageId),
Long.MAX_VALUE); Long.MAX_VALUE);
// The message should no longer be sendable // The message should no longer be sendable

View File

@@ -113,19 +113,22 @@ public class OutgoingSimplexConnectionTest extends BriarTestCase {
oneOf(db).generateTransportAcks(contactId); oneOf(db).generateTransportAcks(contactId);
will(returnValue(null)); will(returnValue(null));
// No transport updates to send // No transport updates to send
oneOf(db).generateTransportUpdates(contactId); oneOf(db).generateTransportUpdates(with(contactId),
with(any(long.class)));
will(returnValue(null)); will(returnValue(null));
// No subscription ack to send // No subscription ack to send
oneOf(db).generateSubscriptionAck(contactId); oneOf(db).generateSubscriptionAck(contactId);
will(returnValue(null)); will(returnValue(null));
// No subscription update to send // No subscription update to send
oneOf(db).generateSubscriptionUpdate(contactId); oneOf(db).generateSubscriptionUpdate(with(contactId),
with(any(long.class)));
will(returnValue(null)); will(returnValue(null));
// No retention ack to send // No retention ack to send
oneOf(db).generateRetentionAck(contactId); oneOf(db).generateRetentionAck(contactId);
will(returnValue(null)); will(returnValue(null));
// No retention update to send // No retention update to send
oneOf(db).generateRetentionUpdate(contactId); oneOf(db).generateRetentionUpdate(with(contactId),
with(any(long.class)));
will(returnValue(null)); will(returnValue(null));
// No acks to send // No acks to send
oneOf(db).generateAck(with(contactId), with(any(int.class))); oneOf(db).generateAck(with(contactId), with(any(int.class)));
@@ -160,19 +163,22 @@ public class OutgoingSimplexConnectionTest extends BriarTestCase {
oneOf(db).generateTransportAcks(contactId); oneOf(db).generateTransportAcks(contactId);
will(returnValue(null)); will(returnValue(null));
// No transport updates to send // No transport updates to send
oneOf(db).generateTransportUpdates(contactId); oneOf(db).generateTransportUpdates(with(contactId),
with(any(long.class)));
will(returnValue(null)); will(returnValue(null));
// No subscription ack to send // No subscription ack to send
oneOf(db).generateSubscriptionAck(contactId); oneOf(db).generateSubscriptionAck(contactId);
will(returnValue(null)); will(returnValue(null));
// No subscription update to send // No subscription update to send
oneOf(db).generateSubscriptionUpdate(contactId); oneOf(db).generateSubscriptionUpdate(with(contactId),
with(any(long.class)));
will(returnValue(null)); will(returnValue(null));
// No retention ack to send // No retention ack to send
oneOf(db).generateRetentionAck(contactId); oneOf(db).generateRetentionAck(contactId);
will(returnValue(null)); will(returnValue(null));
// No retention update to send // No retention update to send
oneOf(db).generateRetentionUpdate(contactId); oneOf(db).generateRetentionUpdate(with(contactId),
with(any(long.class)));
will(returnValue(null)); will(returnValue(null));
// One ack to send // One ack to send
oneOf(db).generateAck(with(contactId), with(any(int.class))); oneOf(db).generateAck(with(contactId), with(any(int.class)));