mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-18 13:49:53 +01:00
Call the listeners when contacts are added and removed.
This commit is contained in:
@@ -4,6 +4,7 @@ package net.sf.briar.api.db;
|
|||||||
public interface DatabaseListener {
|
public interface DatabaseListener {
|
||||||
|
|
||||||
static enum Event {
|
static enum Event {
|
||||||
|
CONTACTS_UPDATED,
|
||||||
MESSAGES_ADDED,
|
MESSAGES_ADDED,
|
||||||
SUBSCRIPTIONS_UPDATED,
|
SUBSCRIPTIONS_UPDATED,
|
||||||
TRANSPORTS_UPDATED
|
TRANSPORTS_UPDATED
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ import java.util.logging.Logger;
|
|||||||
|
|
||||||
import net.sf.briar.api.ContactId;
|
import net.sf.briar.api.ContactId;
|
||||||
import net.sf.briar.api.Rating;
|
import net.sf.briar.api.Rating;
|
||||||
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.DbException;
|
||||||
import net.sf.briar.api.db.NoSuchContactException;
|
import net.sf.briar.api.db.NoSuchContactException;
|
||||||
import net.sf.briar.api.protocol.Ack;
|
import net.sf.briar.api.protocol.Ack;
|
||||||
@@ -106,17 +106,17 @@ class ReadWriteLockDatabaseComponent<Txn> extends DatabaseComponentImpl<Txn> {
|
|||||||
public ContactId addContact(Map<String, Map<String, String>> transports,
|
public ContactId addContact(Map<String, Map<String, String>> transports,
|
||||||
byte[] secret) throws DbException {
|
byte[] secret) throws DbException {
|
||||||
if(LOG.isLoggable(Level.FINE)) LOG.fine("Adding contact");
|
if(LOG.isLoggable(Level.FINE)) LOG.fine("Adding contact");
|
||||||
|
ContactId c;
|
||||||
contactLock.writeLock().lock();
|
contactLock.writeLock().lock();
|
||||||
try {
|
try {
|
||||||
transportLock.writeLock().lock();
|
transportLock.writeLock().lock();
|
||||||
try {
|
try {
|
||||||
Txn txn = db.startTransaction();
|
Txn txn = db.startTransaction();
|
||||||
try {
|
try {
|
||||||
ContactId c = db.addContact(txn, transports, secret);
|
c = db.addContact(txn, transports, secret);
|
||||||
db.commitTransaction(txn);
|
db.commitTransaction(txn);
|
||||||
if(LOG.isLoggable(Level.FINE))
|
if(LOG.isLoggable(Level.FINE))
|
||||||
LOG.fine("Added contact " + c);
|
LOG.fine("Added contact " + c);
|
||||||
return c;
|
|
||||||
} catch(DbException e) {
|
} catch(DbException e) {
|
||||||
db.abortTransaction(txn);
|
db.abortTransaction(txn);
|
||||||
throw e;
|
throw e;
|
||||||
@@ -127,6 +127,9 @@ class ReadWriteLockDatabaseComponent<Txn> extends DatabaseComponentImpl<Txn> {
|
|||||||
} finally {
|
} finally {
|
||||||
contactLock.writeLock().unlock();
|
contactLock.writeLock().unlock();
|
||||||
}
|
}
|
||||||
|
// Call the listeners outside the lock
|
||||||
|
callListeners(Event.CONTACTS_UPDATED);
|
||||||
|
return c;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addLocallyGeneratedMessage(Message m) throws DbException {
|
public void addLocallyGeneratedMessage(Message m) throws DbException {
|
||||||
@@ -167,7 +170,7 @@ class ReadWriteLockDatabaseComponent<Txn> extends DatabaseComponentImpl<Txn> {
|
|||||||
contactLock.readLock().unlock();
|
contactLock.readLock().unlock();
|
||||||
}
|
}
|
||||||
// Call the listeners outside the lock
|
// Call the listeners outside the lock
|
||||||
if(added) callListeners(DatabaseListener.Event.MESSAGES_ADDED);
|
if(added) callListeners(Event.MESSAGES_ADDED);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void findLostBatches(ContactId c) throws DbException {
|
public void findLostBatches(ContactId c) throws DbException {
|
||||||
@@ -781,7 +784,7 @@ class ReadWriteLockDatabaseComponent<Txn> extends DatabaseComponentImpl<Txn> {
|
|||||||
contactLock.readLock().unlock();
|
contactLock.readLock().unlock();
|
||||||
}
|
}
|
||||||
// Call the listeners outside the lock
|
// Call the listeners outside the lock
|
||||||
if(anyAdded) callListeners(DatabaseListener.Event.MESSAGES_ADDED);
|
if(anyAdded) callListeners(Event.MESSAGES_ADDED);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void receiveOffer(ContactId c, Offer o, RequestWriter r)
|
public void receiveOffer(ContactId c, Offer o, RequestWriter r)
|
||||||
@@ -913,6 +916,8 @@ class ReadWriteLockDatabaseComponent<Txn> extends DatabaseComponentImpl<Txn> {
|
|||||||
} finally {
|
} finally {
|
||||||
contactLock.writeLock().unlock();
|
contactLock.writeLock().unlock();
|
||||||
}
|
}
|
||||||
|
// Call the listeners outside the lock
|
||||||
|
callListeners(Event.CONTACTS_UPDATED);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setConnectionWindow(ContactId c, int transportId,
|
public void setConnectionWindow(ContactId c, int transportId,
|
||||||
@@ -984,7 +989,7 @@ class ReadWriteLockDatabaseComponent<Txn> extends DatabaseComponentImpl<Txn> {
|
|||||||
transportLock.writeLock().unlock();
|
transportLock.writeLock().unlock();
|
||||||
}
|
}
|
||||||
// Call the listeners outside the lock
|
// Call the listeners outside the lock
|
||||||
if(changed) callListeners(DatabaseListener.Event.TRANSPORTS_UPDATED);
|
if(changed) callListeners(Event.TRANSPORTS_UPDATED);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setTransportProperties(String name,
|
public void setTransportProperties(String name,
|
||||||
@@ -1008,7 +1013,7 @@ class ReadWriteLockDatabaseComponent<Txn> extends DatabaseComponentImpl<Txn> {
|
|||||||
transportLock.writeLock().unlock();
|
transportLock.writeLock().unlock();
|
||||||
}
|
}
|
||||||
// Call the listeners outside the lock
|
// Call the listeners outside the lock
|
||||||
if(changed) callListeners(DatabaseListener.Event.TRANSPORTS_UPDATED);
|
if(changed) callListeners(Event.TRANSPORTS_UPDATED);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setVisibility(GroupId g, Collection<ContactId> visible)
|
public void setVisibility(GroupId g, Collection<ContactId> visible)
|
||||||
@@ -1059,7 +1064,7 @@ class ReadWriteLockDatabaseComponent<Txn> extends DatabaseComponentImpl<Txn> {
|
|||||||
subscriptionLock.writeLock().unlock();
|
subscriptionLock.writeLock().unlock();
|
||||||
}
|
}
|
||||||
// Call the listeners outside the lock
|
// Call the listeners outside the lock
|
||||||
if(added) callListeners(DatabaseListener.Event.SUBSCRIPTIONS_UPDATED);
|
if(added) callListeners(Event.SUBSCRIPTIONS_UPDATED);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void unsubscribe(GroupId g) throws DbException {
|
public void unsubscribe(GroupId g) throws DbException {
|
||||||
@@ -1097,6 +1102,6 @@ class ReadWriteLockDatabaseComponent<Txn> extends DatabaseComponentImpl<Txn> {
|
|||||||
contactLock.readLock().unlock();
|
contactLock.readLock().unlock();
|
||||||
}
|
}
|
||||||
// Call the listeners outside the lock
|
// Call the listeners outside the lock
|
||||||
if(removed) callListeners(DatabaseListener.Event.SUBSCRIPTIONS_UPDATED);
|
if(removed) callListeners(Event.SUBSCRIPTIONS_UPDATED);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -11,7 +11,7 @@ import java.util.logging.Logger;
|
|||||||
|
|
||||||
import net.sf.briar.api.ContactId;
|
import net.sf.briar.api.ContactId;
|
||||||
import net.sf.briar.api.Rating;
|
import net.sf.briar.api.Rating;
|
||||||
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.DbException;
|
||||||
import net.sf.briar.api.db.NoSuchContactException;
|
import net.sf.briar.api.db.NoSuchContactException;
|
||||||
import net.sf.briar.api.protocol.Ack;
|
import net.sf.briar.api.protocol.Ack;
|
||||||
@@ -89,21 +89,24 @@ class SynchronizedDatabaseComponent<Txn> extends DatabaseComponentImpl<Txn> {
|
|||||||
public ContactId addContact(Map<String, Map<String, String>> transports,
|
public ContactId addContact(Map<String, Map<String, String>> transports,
|
||||||
byte[] secret) throws DbException {
|
byte[] secret) throws DbException {
|
||||||
if(LOG.isLoggable(Level.FINE)) LOG.fine("Adding contact");
|
if(LOG.isLoggable(Level.FINE)) LOG.fine("Adding contact");
|
||||||
|
ContactId c;
|
||||||
synchronized(contactLock) {
|
synchronized(contactLock) {
|
||||||
synchronized(transportLock) {
|
synchronized(transportLock) {
|
||||||
Txn txn = db.startTransaction();
|
Txn txn = db.startTransaction();
|
||||||
try {
|
try {
|
||||||
ContactId c = db.addContact(txn, transports, secret);
|
c = db.addContact(txn, transports, secret);
|
||||||
db.commitTransaction(txn);
|
db.commitTransaction(txn);
|
||||||
if(LOG.isLoggable(Level.FINE))
|
if(LOG.isLoggable(Level.FINE))
|
||||||
LOG.fine("Added contact " + c);
|
LOG.fine("Added contact " + c);
|
||||||
return c;
|
|
||||||
} catch(DbException e) {
|
} catch(DbException e) {
|
||||||
db.abortTransaction(txn);
|
db.abortTransaction(txn);
|
||||||
throw e;
|
throw e;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// Call the listeners outside the lock
|
||||||
|
callListeners(Event.CONTACTS_UPDATED);
|
||||||
|
return c;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addLocallyGeneratedMessage(Message m) throws DbException {
|
public void addLocallyGeneratedMessage(Message m) throws DbException {
|
||||||
@@ -139,7 +142,7 @@ class SynchronizedDatabaseComponent<Txn> extends DatabaseComponentImpl<Txn> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Call the listeners outside the lock
|
// Call the listeners outside the lock
|
||||||
if(added) callListeners(DatabaseListener.Event.MESSAGES_ADDED);
|
if(added) callListeners(Event.MESSAGES_ADDED);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void findLostBatches(ContactId c) throws DbException {
|
public void findLostBatches(ContactId c) throws DbException {
|
||||||
@@ -597,7 +600,7 @@ class SynchronizedDatabaseComponent<Txn> extends DatabaseComponentImpl<Txn> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Call the listeners outside the lock
|
// Call the listeners outside the lock
|
||||||
if(anyAdded) callListeners(DatabaseListener.Event.MESSAGES_ADDED);
|
if(anyAdded) callListeners(Event.MESSAGES_ADDED);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void receiveOffer(ContactId c, Offer o, RequestWriter r)
|
public void receiveOffer(ContactId c, Offer o, RequestWriter r)
|
||||||
@@ -693,6 +696,8 @@ class SynchronizedDatabaseComponent<Txn> extends DatabaseComponentImpl<Txn> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// Call the listeners outside the lock
|
||||||
|
callListeners(Event.CONTACTS_UPDATED);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setConnectionWindow(ContactId c, int transportId,
|
public void setConnectionWindow(ContactId c, int transportId,
|
||||||
@@ -749,7 +754,7 @@ class SynchronizedDatabaseComponent<Txn> extends DatabaseComponentImpl<Txn> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Call the listeners outside the lock
|
// Call the listeners outside the lock
|
||||||
if(changed) callListeners(DatabaseListener.Event.TRANSPORTS_UPDATED);
|
if(changed) callListeners(Event.TRANSPORTS_UPDATED);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setTransportProperties(String name,
|
public void setTransportProperties(String name,
|
||||||
@@ -770,7 +775,7 @@ class SynchronizedDatabaseComponent<Txn> extends DatabaseComponentImpl<Txn> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Call the listeners outside the lock
|
// Call the listeners outside the lock
|
||||||
if(changed) callListeners(DatabaseListener.Event.TRANSPORTS_UPDATED);
|
if(changed) callListeners(Event.TRANSPORTS_UPDATED);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setVisibility(GroupId g, Collection<ContactId> visible)
|
public void setVisibility(GroupId g, Collection<ContactId> visible)
|
||||||
@@ -812,7 +817,7 @@ class SynchronizedDatabaseComponent<Txn> extends DatabaseComponentImpl<Txn> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Call the listeners outside the lock
|
// Call the listeners outside the lock
|
||||||
if(added) callListeners(DatabaseListener.Event.SUBSCRIPTIONS_UPDATED);
|
if(added) callListeners(Event.SUBSCRIPTIONS_UPDATED);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void unsubscribe(GroupId g) throws DbException {
|
public void unsubscribe(GroupId g) throws DbException {
|
||||||
@@ -838,6 +843,6 @@ class SynchronizedDatabaseComponent<Txn> extends DatabaseComponentImpl<Txn> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Call the listeners outside the lock
|
// Call the listeners outside the lock
|
||||||
if(removed) callListeners(DatabaseListener.Event.SUBSCRIPTIONS_UPDATED);
|
if(removed) callListeners(Event.SUBSCRIPTIONS_UPDATED);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ import net.sf.briar.api.db.DatabaseListener;
|
|||||||
import net.sf.briar.api.db.DbException;
|
import net.sf.briar.api.db.DbException;
|
||||||
import net.sf.briar.api.db.NoSuchContactException;
|
import net.sf.briar.api.db.NoSuchContactException;
|
||||||
import net.sf.briar.api.db.Status;
|
import net.sf.briar.api.db.Status;
|
||||||
|
import net.sf.briar.api.db.DatabaseListener.Event;
|
||||||
import net.sf.briar.api.protocol.Ack;
|
import net.sf.briar.api.protocol.Ack;
|
||||||
import net.sf.briar.api.protocol.AuthorId;
|
import net.sf.briar.api.protocol.AuthorId;
|
||||||
import net.sf.briar.api.protocol.Batch;
|
import net.sf.briar.api.protocol.Batch;
|
||||||
@@ -101,6 +102,7 @@ public abstract class DatabaseComponentTest extends TestCase {
|
|||||||
// addContact(transports)
|
// addContact(transports)
|
||||||
oneOf(database).addContact(txn, transports, secret);
|
oneOf(database).addContact(txn, transports, secret);
|
||||||
will(returnValue(contactId));
|
will(returnValue(contactId));
|
||||||
|
oneOf(listener).eventOccurred(Event.CONTACTS_UPDATED);
|
||||||
// getContacts()
|
// getContacts()
|
||||||
oneOf(database).getContacts(txn);
|
oneOf(database).getContacts(txn);
|
||||||
will(returnValue(Collections.singletonList(contactId)));
|
will(returnValue(Collections.singletonList(contactId)));
|
||||||
@@ -125,8 +127,7 @@ public abstract class DatabaseComponentTest extends TestCase {
|
|||||||
oneOf(database).containsSubscription(txn, groupId);
|
oneOf(database).containsSubscription(txn, groupId);
|
||||||
will(returnValue(false));
|
will(returnValue(false));
|
||||||
oneOf(database).addSubscription(txn, group);
|
oneOf(database).addSubscription(txn, group);
|
||||||
oneOf(listener).eventOccurred(
|
oneOf(listener).eventOccurred(Event.SUBSCRIPTIONS_UPDATED);
|
||||||
DatabaseListener.Event.SUBSCRIPTIONS_UPDATED);
|
|
||||||
// subscribe(group) again
|
// subscribe(group) again
|
||||||
oneOf(group).getId();
|
oneOf(group).getId();
|
||||||
will(returnValue(groupId));
|
will(returnValue(groupId));
|
||||||
@@ -139,8 +140,7 @@ public abstract class DatabaseComponentTest extends TestCase {
|
|||||||
oneOf(database).containsSubscription(txn, groupId);
|
oneOf(database).containsSubscription(txn, groupId);
|
||||||
will(returnValue(true));
|
will(returnValue(true));
|
||||||
oneOf(database).removeSubscription(txn, groupId);
|
oneOf(database).removeSubscription(txn, groupId);
|
||||||
oneOf(listener).eventOccurred(
|
oneOf(listener).eventOccurred(Event.SUBSCRIPTIONS_UPDATED);
|
||||||
DatabaseListener.Event.SUBSCRIPTIONS_UPDATED);
|
|
||||||
// unsubscribe(groupId) again
|
// unsubscribe(groupId) again
|
||||||
oneOf(database).containsSubscription(txn, groupId);
|
oneOf(database).containsSubscription(txn, groupId);
|
||||||
will(returnValue(false));
|
will(returnValue(false));
|
||||||
@@ -151,6 +151,7 @@ public abstract class DatabaseComponentTest extends TestCase {
|
|||||||
connectionWindow);
|
connectionWindow);
|
||||||
// removeContact(contactId)
|
// removeContact(contactId)
|
||||||
oneOf(database).removeContact(txn, contactId);
|
oneOf(database).removeContact(txn, contactId);
|
||||||
|
oneOf(listener).eventOccurred(Event.CONTACTS_UPDATED);
|
||||||
// close()
|
// close()
|
||||||
oneOf(cleaner).stopCleaning();
|
oneOf(cleaner).stopCleaning();
|
||||||
oneOf(database).close();
|
oneOf(database).close();
|
||||||
@@ -1124,8 +1125,7 @@ public abstract class DatabaseComponentTest extends TestCase {
|
|||||||
oneOf(database).setSendability(txn, messageId, 0);
|
oneOf(database).setSendability(txn, messageId, 0);
|
||||||
oneOf(database).commitTransaction(txn);
|
oneOf(database).commitTransaction(txn);
|
||||||
// The message was added, so the listener should be called
|
// The message was added, so the listener should be called
|
||||||
oneOf(listener).eventOccurred(
|
oneOf(listener).eventOccurred(Event.MESSAGES_ADDED);
|
||||||
DatabaseListener.Event.MESSAGES_ADDED);
|
|
||||||
}});
|
}});
|
||||||
DatabaseComponent db = createDatabaseComponent(database, cleaner);
|
DatabaseComponent db = createDatabaseComponent(database, cleaner);
|
||||||
|
|
||||||
@@ -1180,8 +1180,7 @@ public abstract class DatabaseComponentTest extends TestCase {
|
|||||||
will(returnValue(Collections.singletonMap("foo", properties)));
|
will(returnValue(Collections.singletonMap("foo", properties)));
|
||||||
oneOf(database).setTransportProperties(txn, "foo", properties1);
|
oneOf(database).setTransportProperties(txn, "foo", properties1);
|
||||||
oneOf(database).commitTransaction(txn);
|
oneOf(database).commitTransaction(txn);
|
||||||
oneOf(listener).eventOccurred(
|
oneOf(listener).eventOccurred(Event.TRANSPORTS_UPDATED);
|
||||||
DatabaseListener.Event.TRANSPORTS_UPDATED);
|
|
||||||
}});
|
}});
|
||||||
DatabaseComponent db = createDatabaseComponent(database, cleaner);
|
DatabaseComponent db = createDatabaseComponent(database, cleaner);
|
||||||
|
|
||||||
@@ -1234,8 +1233,7 @@ public abstract class DatabaseComponentTest extends TestCase {
|
|||||||
will(returnValue(config));
|
will(returnValue(config));
|
||||||
oneOf(database).setTransportConfig(txn, "foo", config1);
|
oneOf(database).setTransportConfig(txn, "foo", config1);
|
||||||
oneOf(database).commitTransaction(txn);
|
oneOf(database).commitTransaction(txn);
|
||||||
oneOf(listener).eventOccurred(
|
oneOf(listener).eventOccurred(Event.TRANSPORTS_UPDATED);
|
||||||
DatabaseListener.Event.TRANSPORTS_UPDATED);
|
|
||||||
}});
|
}});
|
||||||
DatabaseComponent db = createDatabaseComponent(database, cleaner);
|
DatabaseComponent db = createDatabaseComponent(database, cleaner);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user