Removed SubscriptionUpdate and SubscriptionAck.

This commit is contained in:
akwizgran
2016-01-27 16:31:07 +00:00
parent e85b2161e4
commit 9fdc510843
27 changed files with 27 additions and 885 deletions

View File

@@ -17,8 +17,6 @@ import org.briarproject.api.sync.GroupId;
import org.briarproject.api.sync.Message;
import org.briarproject.api.sync.MessageId;
import org.briarproject.api.sync.MessageStatus;
import org.briarproject.api.sync.SubscriptionAck;
import org.briarproject.api.sync.SubscriptionUpdate;
import org.briarproject.api.sync.ValidationManager.Validity;
import org.briarproject.api.transport.TransportKeys;
@@ -420,23 +418,6 @@ interface Database<T> {
*/
Collection<Contact> getSubscribers(T txn, GroupId g) throws DbException;
/**
* Returns a subscription ack for the given contact, or null if no ack is
* due.
* <p>
* Locking: write.
*/
SubscriptionAck getSubscriptionAck(T txn, ContactId c) throws DbException;
/**
* Returns a subscription update for the given contact and updates its
* expiry time using the given latency, or returns null if no update is due.
* <p>
* Locking: write.
*/
SubscriptionUpdate getSubscriptionUpdate(T txn, ContactId c,
int maxLatency) throws DbException;
/**
* Returns all transport keys for the given transport.
* <p>

View File

@@ -26,7 +26,6 @@ import org.briarproject.api.event.MessageToRequestEvent;
import org.briarproject.api.event.MessageValidatedEvent;
import org.briarproject.api.event.MessagesAckedEvent;
import org.briarproject.api.event.MessagesSentEvent;
import org.briarproject.api.event.RemoteSubscriptionsUpdatedEvent;
import org.briarproject.api.event.SettingsUpdatedEvent;
import org.briarproject.api.event.SubscriptionAddedEvent;
import org.briarproject.api.event.SubscriptionRemovedEvent;
@@ -46,8 +45,6 @@ import org.briarproject.api.sync.MessageId;
import org.briarproject.api.sync.MessageStatus;
import org.briarproject.api.sync.Offer;
import org.briarproject.api.sync.Request;
import org.briarproject.api.sync.SubscriptionAck;
import org.briarproject.api.sync.SubscriptionUpdate;
import org.briarproject.api.sync.ValidationManager.Validity;
import org.briarproject.api.transport.TransportKeys;
@@ -434,47 +431,6 @@ class DatabaseComponentImpl<T> implements DatabaseComponent {
return Collections.unmodifiableList(messages);
}
public SubscriptionAck generateSubscriptionAck(ContactId c)
throws DbException {
lock.writeLock().lock();
try {
T txn = db.startTransaction();
try {
if (!db.containsContact(txn, c))
throw new NoSuchContactException();
SubscriptionAck a = db.getSubscriptionAck(txn, c);
db.commitTransaction(txn);
return a;
} catch (DbException e) {
db.abortTransaction(txn);
throw e;
}
} finally {
lock.writeLock().unlock();
}
}
public SubscriptionUpdate generateSubscriptionUpdate(ContactId c,
int maxLatency) throws DbException {
lock.writeLock().lock();
try {
T txn = db.startTransaction();
try {
if (!db.containsContact(txn, c))
throw new NoSuchContactException();
SubscriptionUpdate u =
db.getSubscriptionUpdate(txn, c, maxLatency);
db.commitTransaction(txn);
return u;
} catch (DbException e) {
db.abortTransaction(txn);
throw e;
}
} finally {
lock.writeLock().unlock();
}
}
public Collection<Group> getAvailableGroups(ClientId c) throws DbException {
lock.readLock().lock();
try {
@@ -1066,46 +1022,6 @@ class DatabaseComponentImpl<T> implements DatabaseComponent {
if (requested) eventBus.broadcast(new MessageRequestedEvent(c));
}
public void receiveSubscriptionAck(ContactId c, SubscriptionAck a)
throws DbException {
lock.writeLock().lock();
try {
T txn = db.startTransaction();
try {
if (!db.containsContact(txn, c))
throw new NoSuchContactException();
db.setSubscriptionUpdateAcked(txn, c, a.getVersion());
db.commitTransaction(txn);
} catch (DbException e) {
db.abortTransaction(txn);
throw e;
}
} finally {
lock.writeLock().unlock();
}
}
public void receiveSubscriptionUpdate(ContactId c, SubscriptionUpdate u)
throws DbException {
boolean updated;
lock.writeLock().lock();
try {
T txn = db.startTransaction();
try {
if (!db.containsContact(txn, c))
throw new NoSuchContactException();
updated = db.setGroups(txn, c, u.getGroups(), u.getVersion());
db.commitTransaction(txn);
} catch (DbException e) {
db.abortTransaction(txn);
throw e;
}
} finally {
lock.writeLock().unlock();
}
if (updated) eventBus.broadcast(new RemoteSubscriptionsUpdatedEvent(c));
}
public void removeContact(ContactId c) throws DbException {
lock.writeLock().lock();
try {

View File

@@ -20,8 +20,6 @@ import org.briarproject.api.sync.GroupId;
import org.briarproject.api.sync.Message;
import org.briarproject.api.sync.MessageId;
import org.briarproject.api.sync.MessageStatus;
import org.briarproject.api.sync.SubscriptionAck;
import org.briarproject.api.sync.SubscriptionUpdate;
import org.briarproject.api.sync.ValidationManager.Validity;
import org.briarproject.api.system.Clock;
import org.briarproject.api.transport.IncomingKeys;
@@ -1718,94 +1716,6 @@ abstract class JdbcDatabase implements Database<Connection> {
}
}
public SubscriptionAck getSubscriptionAck(Connection txn, ContactId c)
throws DbException {
PreparedStatement ps = null;
ResultSet rs = null;
try {
String sql = "SELECT remoteVersion FROM groupVersions"
+ " WHERE contactId = ? AND remoteAcked = FALSE";
ps = txn.prepareStatement(sql);
ps.setInt(1, c.getInt());
rs = ps.executeQuery();
if (!rs.next()) {
rs.close();
ps.close();
return null;
}
long version = rs.getLong(1);
if (rs.next()) throw new DbStateException();
rs.close();
ps.close();
sql = "UPDATE groupVersions SET remoteAcked = TRUE"
+ " WHERE contactId = ?";
ps = txn.prepareStatement(sql);
ps.setInt(1, c.getInt());
int affected = ps.executeUpdate();
if (affected != 1) throw new DbStateException();
ps.close();
return new SubscriptionAck(version);
} catch (SQLException e) {
tryToClose(ps);
tryToClose(rs);
throw new DbException(e);
}
}
public SubscriptionUpdate getSubscriptionUpdate(Connection txn, ContactId c,
int maxLatency) throws DbException {
long now = clock.currentTimeMillis();
PreparedStatement ps = null;
ResultSet rs = null;
try {
String sql = "SELECT g.groupId, clientId, descriptor,"
+ " localVersion, txCount"
+ " FROM groups AS g"
+ " JOIN groupVisibilities AS gvis"
+ " ON g.groupId = gvis.groupId"
+ " JOIN groupVersions AS gver"
+ " ON gvis.contactId = gver.contactId"
+ " WHERE gvis.contactId = ?"
+ " AND localVersion > localAcked"
+ " AND expiry < ?";
ps = txn.prepareStatement(sql);
ps.setInt(1, c.getInt());
ps.setLong(2, now);
rs = ps.executeQuery();
List<Group> groups = new ArrayList<Group>();
Set<GroupId> ids = new HashSet<GroupId>();
long version = 0;
int txCount = 0;
while (rs.next()) {
GroupId id = new GroupId(rs.getBytes(1));
if (!ids.add(id)) throw new DbStateException();
ClientId clientId = new ClientId(rs.getBytes(2));
byte[] descriptor = rs.getBytes(3);
groups.add(new Group(id, clientId, descriptor));
version = rs.getLong(4);
txCount = rs.getInt(5);
}
rs.close();
ps.close();
if (groups.isEmpty()) return null;
sql = "UPDATE groupVersions"
+ " SET expiry = ?, txCount = txCount + 1"
+ " WHERE contactId = ?";
ps = txn.prepareStatement(sql);
ps.setLong(1, calculateExpiry(now, maxLatency, txCount));
ps.setInt(2, c.getInt());
int affected = ps.executeUpdate();
if (affected != 1) throw new DbStateException();
ps.close();
groups = Collections.unmodifiableList(groups);
return new SubscriptionUpdate(groups, version);
} catch (SQLException e) {
tryToClose(ps);
tryToClose(rs);
throw new DbException(e);
}
}
public Map<ContactId, TransportKeys> getTransportKeys(Connection txn,
TransportId t) throws DbException {
PreparedStatement ps = null;