mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-16 20:59:54 +01:00
Use the event bus to observe contacts connecting and disconnecting.
This commit is contained in:
@@ -31,6 +31,8 @@ import org.briarproject.api.db.DbException;
|
|||||||
import org.briarproject.api.db.MessageHeader;
|
import org.briarproject.api.db.MessageHeader;
|
||||||
import org.briarproject.api.db.NoSuchContactException;
|
import org.briarproject.api.db.NoSuchContactException;
|
||||||
import org.briarproject.api.event.ContactAddedEvent;
|
import org.briarproject.api.event.ContactAddedEvent;
|
||||||
|
import org.briarproject.api.event.ContactConnectedEvent;
|
||||||
|
import org.briarproject.api.event.ContactDisconnectedEvent;
|
||||||
import org.briarproject.api.event.ContactRemovedEvent;
|
import org.briarproject.api.event.ContactRemovedEvent;
|
||||||
import org.briarproject.api.event.Event;
|
import org.briarproject.api.event.Event;
|
||||||
import org.briarproject.api.event.EventBus;
|
import org.briarproject.api.event.EventBus;
|
||||||
@@ -38,7 +40,6 @@ import org.briarproject.api.event.EventListener;
|
|||||||
import org.briarproject.api.event.MessageAddedEvent;
|
import org.briarproject.api.event.MessageAddedEvent;
|
||||||
import org.briarproject.api.event.MessageExpiredEvent;
|
import org.briarproject.api.event.MessageExpiredEvent;
|
||||||
import org.briarproject.api.messaging.GroupId;
|
import org.briarproject.api.messaging.GroupId;
|
||||||
import org.briarproject.api.transport.ConnectionListener;
|
|
||||||
import org.briarproject.api.transport.ConnectionRegistry;
|
import org.briarproject.api.transport.ConnectionRegistry;
|
||||||
|
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
@@ -61,7 +62,7 @@ import android.widget.Toast;
|
|||||||
|
|
||||||
public class ContactListActivity extends BriarActivity
|
public class ContactListActivity extends BriarActivity
|
||||||
implements OnClickListener, OnItemClickListener, OnCreateContextMenuListener,
|
implements OnClickListener, OnItemClickListener, OnCreateContextMenuListener,
|
||||||
EventListener, ConnectionListener {
|
EventListener {
|
||||||
|
|
||||||
private static final int MENU_ITEM_DELETE = 1;
|
private static final int MENU_ITEM_DELETE = 1;
|
||||||
private static final Logger LOG =
|
private static final Logger LOG =
|
||||||
@@ -128,7 +129,6 @@ EventListener, ConnectionListener {
|
|||||||
public void onResume() {
|
public void onResume() {
|
||||||
super.onResume();
|
super.onResume();
|
||||||
eventBus.addListener(this);
|
eventBus.addListener(this);
|
||||||
connectionRegistry.addListener(this);
|
|
||||||
loadContacts();
|
loadContacts();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -213,7 +213,6 @@ EventListener, ConnectionListener {
|
|||||||
public void onPause() {
|
public void onPause() {
|
||||||
super.onPause();
|
super.onPause();
|
||||||
eventBus.removeListener(this);
|
eventBus.removeListener(this);
|
||||||
connectionRegistry.removeListener(this);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void onClick(View view) {
|
public void onClick(View view) {
|
||||||
@@ -271,6 +270,10 @@ EventListener, ConnectionListener {
|
|||||||
public void eventOccurred(Event e) {
|
public void eventOccurred(Event e) {
|
||||||
if(e instanceof ContactAddedEvent) {
|
if(e instanceof ContactAddedEvent) {
|
||||||
loadContacts();
|
loadContacts();
|
||||||
|
} else if(e instanceof ContactConnectedEvent) {
|
||||||
|
setConnected(((ContactConnectedEvent) e).getContactId(), true);
|
||||||
|
} else if(e instanceof ContactDisconnectedEvent) {
|
||||||
|
setConnected(((ContactDisconnectedEvent) e).getContactId(), false);
|
||||||
} else if(e instanceof ContactRemovedEvent) {
|
} else if(e instanceof ContactRemovedEvent) {
|
||||||
LOG.info("Contact removed");
|
LOG.info("Contact removed");
|
||||||
removeItem(((ContactRemovedEvent) e).getContactId());
|
removeItem(((ContactRemovedEvent) e).getContactId());
|
||||||
@@ -335,14 +338,6 @@ EventListener, ConnectionListener {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public void contactConnected(ContactId c) {
|
|
||||||
setConnected(c, true);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void contactDisconnected(ContactId c) {
|
|
||||||
setConnected(c, false);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void setConnected(final ContactId c, final boolean connected) {
|
private void setConnected(final ContactId c, final boolean connected) {
|
||||||
runOnUiThread(new Runnable() {
|
runOnUiThread(new Runnable() {
|
||||||
public void run() {
|
public void run() {
|
||||||
|
|||||||
@@ -0,0 +1,20 @@
|
|||||||
|
package org.briarproject.api.event;
|
||||||
|
|
||||||
|
import org.briarproject.api.ContactId;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* An event that is broadcast when a contact connects that was not previously
|
||||||
|
* connected via any transport.
|
||||||
|
*/
|
||||||
|
public class ContactConnectedEvent extends Event {
|
||||||
|
|
||||||
|
private final ContactId contactId;
|
||||||
|
|
||||||
|
public ContactConnectedEvent(ContactId contactId) {
|
||||||
|
this.contactId = contactId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ContactId getContactId() {
|
||||||
|
return contactId;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
package org.briarproject.api.event;
|
||||||
|
|
||||||
|
import org.briarproject.api.ContactId;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* An event that is broadcast when a contact disconnects and is no longer
|
||||||
|
* connected via any transport.
|
||||||
|
*/
|
||||||
|
public class ContactDisconnectedEvent extends Event {
|
||||||
|
|
||||||
|
private final ContactId contactId;
|
||||||
|
|
||||||
|
public ContactDisconnectedEvent(ContactId contactId) {
|
||||||
|
this.contactId = contactId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ContactId getContactId() {
|
||||||
|
return contactId;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,13 +0,0 @@
|
|||||||
package org.briarproject.api.transport;
|
|
||||||
|
|
||||||
import org.briarproject.api.ContactId;
|
|
||||||
|
|
||||||
/** An interface for listening for connection and disconnection events. */
|
|
||||||
public interface ConnectionListener {
|
|
||||||
|
|
||||||
/** Called when a contact connects and has no existing connections. */
|
|
||||||
void contactConnected(ContactId c);
|
|
||||||
|
|
||||||
/** Called when a contact disconnects and has no remaining connections. */
|
|
||||||
void contactDisconnected(ContactId c);
|
|
||||||
}
|
|
||||||
@@ -10,10 +10,6 @@ import org.briarproject.api.TransportId;
|
|||||||
*/
|
*/
|
||||||
public interface ConnectionRegistry {
|
public interface ConnectionRegistry {
|
||||||
|
|
||||||
void addListener(ConnectionListener c);
|
|
||||||
|
|
||||||
void removeListener(ConnectionListener c);
|
|
||||||
|
|
||||||
void registerConnection(ContactId c, TransportId t);
|
void registerConnection(ContactId c, TransportId t);
|
||||||
|
|
||||||
void unregisterConnection(ContactId c, TransportId t);
|
void unregisterConnection(ContactId c, TransportId t);
|
||||||
|
|||||||
@@ -8,37 +8,33 @@ import java.util.Collections;
|
|||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.concurrent.CopyOnWriteArrayList;
|
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
import org.briarproject.api.ContactId;
|
import org.briarproject.api.ContactId;
|
||||||
import org.briarproject.api.TransportId;
|
import org.briarproject.api.TransportId;
|
||||||
import org.briarproject.api.transport.ConnectionListener;
|
import org.briarproject.api.event.ContactConnectedEvent;
|
||||||
|
import org.briarproject.api.event.ContactDisconnectedEvent;
|
||||||
|
import org.briarproject.api.event.EventBus;
|
||||||
import org.briarproject.api.transport.ConnectionRegistry;
|
import org.briarproject.api.transport.ConnectionRegistry;
|
||||||
|
|
||||||
|
import com.google.inject.Inject;
|
||||||
|
|
||||||
class ConnectionRegistryImpl implements ConnectionRegistry {
|
class ConnectionRegistryImpl implements ConnectionRegistry {
|
||||||
|
|
||||||
private static final Logger LOG =
|
private static final Logger LOG =
|
||||||
Logger.getLogger(ConnectionRegistryImpl.class.getName());
|
Logger.getLogger(ConnectionRegistryImpl.class.getName());
|
||||||
|
|
||||||
|
private final EventBus eventBus;
|
||||||
// Locking: this
|
// Locking: this
|
||||||
private final Map<TransportId, Map<ContactId, Integer>> connections;
|
private final Map<TransportId, Map<ContactId, Integer>> connections;
|
||||||
// Locking: this
|
// Locking: this
|
||||||
private final Map<ContactId, Integer> contactCounts;
|
private final Map<ContactId, Integer> contactCounts;
|
||||||
private final List<ConnectionListener> listeners;
|
|
||||||
|
|
||||||
ConnectionRegistryImpl() {
|
@Inject
|
||||||
|
ConnectionRegistryImpl(EventBus eventBus) {
|
||||||
|
this.eventBus = eventBus;
|
||||||
connections = new HashMap<TransportId, Map<ContactId, Integer>>();
|
connections = new HashMap<TransportId, Map<ContactId, Integer>>();
|
||||||
contactCounts = new HashMap<ContactId, Integer>();
|
contactCounts = new HashMap<ContactId, Integer>();
|
||||||
listeners = new CopyOnWriteArrayList<ConnectionListener>();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void addListener(ConnectionListener c) {
|
|
||||||
listeners.add(c);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void removeListener(ConnectionListener c) {
|
|
||||||
listeners.remove(c);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void registerConnection(ContactId c, TransportId t) {
|
public void registerConnection(ContactId c, TransportId t) {
|
||||||
@@ -63,7 +59,7 @@ class ConnectionRegistryImpl implements ConnectionRegistry {
|
|||||||
}
|
}
|
||||||
if(firstConnection) {
|
if(firstConnection) {
|
||||||
LOG.info("Contact connected");
|
LOG.info("Contact connected");
|
||||||
for(ConnectionListener l : listeners) l.contactConnected(c);
|
eventBus.broadcast(new ContactConnectedEvent(c));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -91,7 +87,7 @@ class ConnectionRegistryImpl implements ConnectionRegistry {
|
|||||||
}
|
}
|
||||||
if(lastConnection) {
|
if(lastConnection) {
|
||||||
LOG.info("Contact disconnected");
|
LOG.info("Contact disconnected");
|
||||||
for(ConnectionListener l : listeners) l.contactDisconnected(c);
|
eventBus.broadcast(new ContactDisconnectedEvent(c));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -22,9 +22,10 @@ public class TransportModule extends AbstractModule {
|
|||||||
ConnectionReaderFactoryImpl.class);
|
ConnectionReaderFactoryImpl.class);
|
||||||
bind(ConnectionRecogniser.class).to(
|
bind(ConnectionRecogniser.class).to(
|
||||||
ConnectionRecogniserImpl.class).in(Singleton.class);
|
ConnectionRecogniserImpl.class).in(Singleton.class);
|
||||||
bind(ConnectionRegistry.class).toInstance(new ConnectionRegistryImpl());
|
bind(ConnectionRegistry.class).to(
|
||||||
bind(ConnectionWriterFactory.class).to(
|
ConnectionRegistryImpl.class).in(Singleton.class);;
|
||||||
ConnectionWriterFactoryImpl.class);
|
bind(ConnectionWriterFactory.class).to(
|
||||||
|
ConnectionWriterFactoryImpl.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Provides @Singleton
|
@Provides @Singleton
|
||||||
|
|||||||
@@ -7,7 +7,12 @@ import java.util.Collections;
|
|||||||
import org.briarproject.BriarTestCase;
|
import org.briarproject.BriarTestCase;
|
||||||
import org.briarproject.api.ContactId;
|
import org.briarproject.api.ContactId;
|
||||||
import org.briarproject.api.TransportId;
|
import org.briarproject.api.TransportId;
|
||||||
|
import org.briarproject.api.event.ContactConnectedEvent;
|
||||||
|
import org.briarproject.api.event.ContactDisconnectedEvent;
|
||||||
|
import org.briarproject.api.event.EventBus;
|
||||||
import org.briarproject.api.transport.ConnectionRegistry;
|
import org.briarproject.api.transport.ConnectionRegistry;
|
||||||
|
import org.jmock.Expectations;
|
||||||
|
import org.jmock.Mockery;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
public class ConnectionRegistryImplTest extends BriarTestCase {
|
public class ConnectionRegistryImplTest extends BriarTestCase {
|
||||||
@@ -24,13 +29,24 @@ public class ConnectionRegistryImplTest extends BriarTestCase {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testRegisterAndUnregister() {
|
public void testRegisterAndUnregister() {
|
||||||
ConnectionRegistry c = new ConnectionRegistryImpl();
|
Mockery context = new Mockery();
|
||||||
|
final EventBus eventBus = context.mock(EventBus.class);
|
||||||
|
context.checking(new Expectations() {{
|
||||||
|
exactly(3).of(eventBus).broadcast(with(any(
|
||||||
|
ContactConnectedEvent.class)));
|
||||||
|
oneOf(eventBus).broadcast(with(any(
|
||||||
|
ContactDisconnectedEvent.class)));
|
||||||
|
}});
|
||||||
|
|
||||||
|
ConnectionRegistry c = new ConnectionRegistryImpl(eventBus);
|
||||||
|
|
||||||
// The registry should be empty
|
// The registry should be empty
|
||||||
assertEquals(Collections.emptyList(),
|
assertEquals(Collections.emptyList(),
|
||||||
c.getConnectedContacts(transportId));
|
c.getConnectedContacts(transportId));
|
||||||
assertEquals(Collections.emptyList(),
|
assertEquals(Collections.emptyList(),
|
||||||
c.getConnectedContacts(transportId1));
|
c.getConnectedContacts(transportId1));
|
||||||
// Check that a registered connection shows up
|
// Check that a registered connection shows up - this should
|
||||||
|
// broadcast a ContactConnectedEvent
|
||||||
c.registerConnection(contactId, transportId);
|
c.registerConnection(contactId, transportId);
|
||||||
assertEquals(Arrays.asList(contactId),
|
assertEquals(Arrays.asList(contactId),
|
||||||
c.getConnectedContacts(transportId));
|
c.getConnectedContacts(transportId));
|
||||||
@@ -48,7 +64,8 @@ public class ConnectionRegistryImplTest extends BriarTestCase {
|
|||||||
c.getConnectedContacts(transportId));
|
c.getConnectedContacts(transportId));
|
||||||
assertEquals(Collections.emptyList(),
|
assertEquals(Collections.emptyList(),
|
||||||
c.getConnectedContacts(transportId1));
|
c.getConnectedContacts(transportId1));
|
||||||
// Unregister the other connection - lookup should be affected
|
// Unregister the other connection - lookup should be affected -
|
||||||
|
// this should broadcast a ContactDisconnectedEvent
|
||||||
c.unregisterConnection(contactId, transportId);
|
c.unregisterConnection(contactId, transportId);
|
||||||
assertEquals(Collections.emptyList(),
|
assertEquals(Collections.emptyList(),
|
||||||
c.getConnectedContacts(transportId));
|
c.getConnectedContacts(transportId));
|
||||||
@@ -59,7 +76,8 @@ public class ConnectionRegistryImplTest extends BriarTestCase {
|
|||||||
c.unregisterConnection(contactId, transportId);
|
c.unregisterConnection(contactId, transportId);
|
||||||
fail();
|
fail();
|
||||||
} catch(IllegalArgumentException expected) {}
|
} catch(IllegalArgumentException expected) {}
|
||||||
// Register both contacts with one transport, one contact with both
|
// Register both contacts with one transport, one contact with both -
|
||||||
|
// this should broadcast two ContactConnectedEvents
|
||||||
c.registerConnection(contactId, transportId);
|
c.registerConnection(contactId, transportId);
|
||||||
c.registerConnection(contactId1, transportId);
|
c.registerConnection(contactId1, transportId);
|
||||||
c.registerConnection(contactId1, transportId1);
|
c.registerConnection(contactId1, transportId1);
|
||||||
@@ -69,5 +87,6 @@ public class ConnectionRegistryImplTest extends BriarTestCase {
|
|||||||
assertTrue(connected.contains(contactId1));
|
assertTrue(connected.contains(contactId1));
|
||||||
assertEquals(Arrays.asList(contactId1),
|
assertEquals(Arrays.asList(contactId1),
|
||||||
c.getConnectedContacts(transportId1));
|
c.getConnectedContacts(transportId1));
|
||||||
|
context.assertIsSatisfied();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user