mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-14 19:59:05 +01:00
Converted database events from an enum to classes to allow them to
carry data.
This commit is contained in:
@@ -406,13 +406,14 @@ interface Database<T> {
|
||||
void removeMessage(T txn, MessageId m) throws DbException;
|
||||
|
||||
/**
|
||||
* Unsubscribes from the given group. Any messages belonging to the group
|
||||
* Unsubscribes from the given group and returns true if a subscription
|
||||
* previously existed. Any messages belonging to the group
|
||||
* are deleted from the database.
|
||||
* <p>
|
||||
* Locking: contacts read, messages write, messageStatuses write,
|
||||
* subscriptions write.
|
||||
*/
|
||||
void removeSubscription(T txn, GroupId g) throws DbException;
|
||||
boolean removeSubscription(T txn, GroupId g) throws DbException;
|
||||
|
||||
/**
|
||||
* Sets the configuration for the given transport, replacing any existing
|
||||
|
||||
@@ -25,11 +25,17 @@ import net.sf.briar.api.TransportConfig;
|
||||
import net.sf.briar.api.TransportId;
|
||||
import net.sf.briar.api.TransportProperties;
|
||||
import net.sf.briar.api.db.DatabaseComponent;
|
||||
import net.sf.briar.api.db.DatabaseListener;
|
||||
import net.sf.briar.api.db.DatabaseListener.Event;
|
||||
import net.sf.briar.api.db.DbException;
|
||||
import net.sf.briar.api.db.NoSuchContactException;
|
||||
import net.sf.briar.api.db.Status;
|
||||
import net.sf.briar.api.db.event.BatchReceivedEvent;
|
||||
import net.sf.briar.api.db.event.ContactAddedEvent;
|
||||
import net.sf.briar.api.db.event.ContactRemovedEvent;
|
||||
import net.sf.briar.api.db.event.DatabaseEvent;
|
||||
import net.sf.briar.api.db.event.DatabaseListener;
|
||||
import net.sf.briar.api.db.event.MessagesAddedEvent;
|
||||
import net.sf.briar.api.db.event.SubscriptionsUpdatedEvent;
|
||||
import net.sf.briar.api.db.event.TransportsUpdatedEvent;
|
||||
import net.sf.briar.api.protocol.Ack;
|
||||
import net.sf.briar.api.protocol.AuthorId;
|
||||
import net.sf.briar.api.protocol.Batch;
|
||||
@@ -146,12 +152,12 @@ DatabaseCleaner.Callback {
|
||||
contactLock.writeLock().unlock();
|
||||
}
|
||||
// Call the listeners outside the lock
|
||||
callListeners(Event.CONTACTS_UPDATED);
|
||||
callListeners(new ContactAddedEvent(c));
|
||||
return c;
|
||||
}
|
||||
|
||||
/** Notifies all listeners of a database event. */
|
||||
private void callListeners(DatabaseListener.Event e) {
|
||||
private void callListeners(DatabaseEvent e) {
|
||||
synchronized(listeners) {
|
||||
if(!listeners.isEmpty()) {
|
||||
// Shuffle the listeners so we don't always send new messages
|
||||
@@ -199,7 +205,7 @@ DatabaseCleaner.Callback {
|
||||
contactLock.readLock().unlock();
|
||||
}
|
||||
// Call the listeners outside the lock
|
||||
if(added) callListeners(Event.MESSAGES_ADDED);
|
||||
if(added) callListeners(new MessagesAddedEvent());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -316,7 +322,7 @@ DatabaseCleaner.Callback {
|
||||
contactLock.readLock().unlock();
|
||||
}
|
||||
// Call the listeners outside the lock
|
||||
if(added) callListeners(Event.MESSAGES_ADDED);
|
||||
if(added) callListeners(new MessagesAddedEvent());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -937,8 +943,8 @@ DatabaseCleaner.Callback {
|
||||
contactLock.readLock().unlock();
|
||||
}
|
||||
// Call the listeners outside the lock
|
||||
callListeners(Event.BATCH_RECEIVED);
|
||||
if(anyAdded) callListeners(Event.MESSAGES_ADDED);
|
||||
callListeners(new BatchReceivedEvent());
|
||||
if(anyAdded) callListeners(new MessagesAddedEvent());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1102,7 +1108,7 @@ DatabaseCleaner.Callback {
|
||||
contactLock.writeLock().unlock();
|
||||
}
|
||||
// Call the listeners outside the lock
|
||||
callListeners(Event.CONTACTS_UPDATED);
|
||||
callListeners(new ContactRemovedEvent(c));
|
||||
}
|
||||
|
||||
public void setConfig(TransportId t, TransportConfig config)
|
||||
@@ -1126,7 +1132,7 @@ DatabaseCleaner.Callback {
|
||||
transportLock.writeLock().unlock();
|
||||
}
|
||||
// Call the listeners outside the lock
|
||||
if(changed) callListeners(Event.TRANSPORTS_UPDATED);
|
||||
if(changed) callListeners(new TransportsUpdatedEvent());
|
||||
}
|
||||
|
||||
public void setConnectionWindow(ContactId c, TransportId t,
|
||||
@@ -1172,7 +1178,7 @@ DatabaseCleaner.Callback {
|
||||
transportLock.writeLock().unlock();
|
||||
}
|
||||
// Call the listeners outside the lock
|
||||
if(changed) callListeners(Event.TRANSPORTS_UPDATED);
|
||||
if(changed) callListeners(new TransportsUpdatedEvent());
|
||||
}
|
||||
|
||||
public void setRating(AuthorId a, Rating r) throws DbException {
|
||||
@@ -1317,7 +1323,7 @@ DatabaseCleaner.Callback {
|
||||
subscriptionLock.writeLock().unlock();
|
||||
}
|
||||
// Call the listeners outside the lock
|
||||
if(added) callListeners(Event.SUBSCRIPTIONS_UPDATED);
|
||||
if(added) callListeners(new SubscriptionsUpdatedEvent());
|
||||
}
|
||||
|
||||
public void unsubscribe(GroupId g) throws DbException {
|
||||
@@ -1333,10 +1339,7 @@ DatabaseCleaner.Callback {
|
||||
try {
|
||||
T txn = db.startTransaction();
|
||||
try {
|
||||
if(db.containsSubscription(txn, g)) {
|
||||
db.removeSubscription(txn, g);
|
||||
removed = true;
|
||||
}
|
||||
removed = db.removeSubscription(txn, g);
|
||||
db.commitTransaction(txn);
|
||||
} catch(DbException e) {
|
||||
db.abortTransaction(txn);
|
||||
@@ -1355,7 +1358,7 @@ DatabaseCleaner.Callback {
|
||||
contactLock.readLock().unlock();
|
||||
}
|
||||
// Call the listeners outside the lock
|
||||
if(removed) callListeners(Event.SUBSCRIPTIONS_UPDATED);
|
||||
if(removed) callListeners(new SubscriptionsUpdatedEvent());
|
||||
}
|
||||
|
||||
public void checkFreeSpaceAndClean() throws DbException {
|
||||
|
||||
@@ -1754,7 +1754,7 @@ abstract class JdbcDatabase implements Database<Connection> {
|
||||
}
|
||||
}
|
||||
|
||||
public void removeSubscription(Connection txn, GroupId g)
|
||||
public boolean removeSubscription(Connection txn, GroupId g)
|
||||
throws DbException {
|
||||
PreparedStatement ps = null;
|
||||
try {
|
||||
@@ -1762,8 +1762,9 @@ abstract class JdbcDatabase implements Database<Connection> {
|
||||
ps = txn.prepareStatement(sql);
|
||||
ps.setBytes(1, g.getBytes());
|
||||
int affected = ps.executeUpdate();
|
||||
if(affected != 1) throw new DbStateException();
|
||||
if(affected > 1) throw new DbStateException();
|
||||
ps.close();
|
||||
return affected > 0;
|
||||
} catch(SQLException e) {
|
||||
tryToClose(ps);
|
||||
throw new DbException(e);
|
||||
|
||||
@@ -16,9 +16,12 @@ import net.sf.briar.api.ContactId;
|
||||
import net.sf.briar.api.TransportId;
|
||||
import net.sf.briar.api.crypto.CryptoComponent;
|
||||
import net.sf.briar.api.db.DatabaseComponent;
|
||||
import net.sf.briar.api.db.DatabaseListener;
|
||||
import net.sf.briar.api.db.DbException;
|
||||
import net.sf.briar.api.db.NoSuchContactException;
|
||||
import net.sf.briar.api.db.event.ContactAddedEvent;
|
||||
import net.sf.briar.api.db.event.ContactRemovedEvent;
|
||||
import net.sf.briar.api.db.event.DatabaseEvent;
|
||||
import net.sf.briar.api.db.event.DatabaseListener;
|
||||
import net.sf.briar.api.transport.ConnectionRecogniser;
|
||||
import net.sf.briar.api.transport.ConnectionWindow;
|
||||
|
||||
@@ -127,9 +130,9 @@ DatabaseListener {
|
||||
return contactId;
|
||||
}
|
||||
|
||||
public void eventOccurred(Event e) {
|
||||
public void eventOccurred(DatabaseEvent e) {
|
||||
// When the set of contacts changes we need to re-initialise everything
|
||||
if(e == Event.CONTACTS_UPDATED) {
|
||||
if(e instanceof ContactAddedEvent || e instanceof ContactRemovedEvent) {
|
||||
synchronized(this) {
|
||||
initialised = false;
|
||||
}
|
||||
|
||||
@@ -4,7 +4,7 @@ interface Flags {
|
||||
|
||||
// Flags raised by the database listener
|
||||
static final int BATCH_RECEIVED = 1;
|
||||
static final int CONTACTS_UPDATED = 2;
|
||||
static final int CONTACT_REMOVED = 2;
|
||||
static final int MESSAGES_ADDED = 4;
|
||||
static final int SUBSCRIPTIONS_UPDATED = 8;
|
||||
static final int TRANSPORTS_UPDATED = 16;
|
||||
|
||||
@@ -14,8 +14,14 @@ import net.sf.briar.api.ContactId;
|
||||
import net.sf.briar.api.FormatException;
|
||||
import net.sf.briar.api.TransportId;
|
||||
import net.sf.briar.api.db.DatabaseComponent;
|
||||
import net.sf.briar.api.db.DatabaseListener;
|
||||
import net.sf.briar.api.db.DbException;
|
||||
import net.sf.briar.api.db.event.BatchReceivedEvent;
|
||||
import net.sf.briar.api.db.event.ContactRemovedEvent;
|
||||
import net.sf.briar.api.db.event.DatabaseEvent;
|
||||
import net.sf.briar.api.db.event.DatabaseListener;
|
||||
import net.sf.briar.api.db.event.MessagesAddedEvent;
|
||||
import net.sf.briar.api.db.event.SubscriptionsUpdatedEvent;
|
||||
import net.sf.briar.api.db.event.TransportsUpdatedEvent;
|
||||
import net.sf.briar.api.protocol.Ack;
|
||||
import net.sf.briar.api.protocol.Batch;
|
||||
import net.sf.briar.api.protocol.MessageId;
|
||||
@@ -81,19 +87,28 @@ abstract class StreamConnection implements DatabaseListener {
|
||||
protected abstract ConnectionWriter createConnectionWriter()
|
||||
throws DbException, IOException ;
|
||||
|
||||
public void eventOccurred(Event e) {
|
||||
public void eventOccurred(DatabaseEvent e) {
|
||||
synchronized(this) {
|
||||
if(e == Event.BATCH_RECEIVED)
|
||||
if(e instanceof BatchReceivedEvent) {
|
||||
writerFlags |= Flags.BATCH_RECEIVED;
|
||||
else if(e == Event.CONTACTS_UPDATED)
|
||||
writerFlags |= Flags.CONTACTS_UPDATED;
|
||||
else if(e == Event.MESSAGES_ADDED)
|
||||
notifyAll();
|
||||
} else if(e instanceof ContactRemovedEvent) {
|
||||
ContactId c = ((ContactRemovedEvent) e).getContactId();
|
||||
if(contactId.equals(c)) {
|
||||
writerFlags |= Flags.CONTACT_REMOVED;
|
||||
notifyAll();
|
||||
}
|
||||
} else if(e instanceof MessagesAddedEvent) {
|
||||
writerFlags |= Flags.MESSAGES_ADDED;
|
||||
else if(e == Event.SUBSCRIPTIONS_UPDATED)
|
||||
notifyAll();
|
||||
} else if(e instanceof SubscriptionsUpdatedEvent) {
|
||||
// FIXME: Check whether the change affected this contact
|
||||
writerFlags |= Flags.SUBSCRIPTIONS_UPDATED;
|
||||
else if(e == Event.TRANSPORTS_UPDATED)
|
||||
notifyAll();
|
||||
} else if(e instanceof TransportsUpdatedEvent) {
|
||||
writerFlags |= Flags.TRANSPORTS_UPDATED;
|
||||
notifyAll();
|
||||
notifyAll();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -207,11 +222,9 @@ abstract class StreamConnection implements DatabaseListener {
|
||||
writerFlags = 0;
|
||||
}
|
||||
// Handle the flags in approximate order of urgency
|
||||
if((flags & Flags.CONTACTS_UPDATED) != 0) {
|
||||
if(!db.getContacts().contains(contactId)) {
|
||||
connection.dispose(true);
|
||||
return;
|
||||
}
|
||||
if((flags & Flags.CONTACT_REMOVED) != 0) {
|
||||
connection.dispose(true);
|
||||
return;
|
||||
}
|
||||
if((flags & Flags.TRANSPORTS_UPDATED) != 0) {
|
||||
sendTransports(transportWriter);
|
||||
@@ -246,11 +259,9 @@ abstract class StreamConnection implements DatabaseListener {
|
||||
writerFlags = 0;
|
||||
}
|
||||
// Handle the flags in approximate order of urgency
|
||||
if((flags & Flags.CONTACTS_UPDATED) != 0) {
|
||||
if(!db.getContacts().contains(contactId)) {
|
||||
connection.dispose(true);
|
||||
return;
|
||||
}
|
||||
if((flags & Flags.CONTACT_REMOVED) != 0) {
|
||||
connection.dispose(true);
|
||||
return;
|
||||
}
|
||||
if((flags & Flags.TRANSPORTS_UPDATED) != 0) {
|
||||
sendTransports(transportWriter);
|
||||
@@ -279,11 +290,9 @@ abstract class StreamConnection implements DatabaseListener {
|
||||
writerFlags = 0;
|
||||
}
|
||||
// Handle the flags in approximate order of urgency
|
||||
if((flags & Flags.CONTACTS_UPDATED) != 0) {
|
||||
if(!db.getContacts().contains(contactId)) {
|
||||
connection.dispose(true);
|
||||
return;
|
||||
}
|
||||
if((flags & Flags.CONTACT_REMOVED) != 0) {
|
||||
connection.dispose(true);
|
||||
return;
|
||||
}
|
||||
if((flags & Flags.TRANSPORTS_UPDATED) != 0) {
|
||||
sendTransports(transportWriter);
|
||||
|
||||
Reference in New Issue
Block a user