Skip to content
Snippets Groups Projects
Commit 75cab358 authored by akwizgran's avatar akwizgran
Browse files

Added ConnectionListener interface for contact list and supporting code.

parent f0e9bcc1
No related branches found
No related tags found
No related merge requests found
package net.sf.briar.api.transport;
import net.sf.briar.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,9 +10,15 @@ import net.sf.briar.api.messaging.TransportId;
*/
public interface ConnectionRegistry {
void addListener(ConnectionListener c);
void removeListener(ConnectionListener c);
void registerConnection(ContactId c, TransportId t);
void unregisterConnection(ContactId c, TransportId t);
Collection<ContactId> getConnectedContacts(TransportId t);
boolean isConnected(ContactId c);
}
......@@ -6,41 +6,81 @@ import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CopyOnWriteArrayList;
import net.sf.briar.api.ContactId;
import net.sf.briar.api.messaging.TransportId;
import net.sf.briar.api.transport.ConnectionListener;
import net.sf.briar.api.transport.ConnectionRegistry;
class ConnectionRegistryImpl implements ConnectionRegistry {
// Locking: this
private final Map<TransportId, Map<ContactId, Integer>> connections;
// Locking: this
private final Map<ContactId, Integer> contactCounts;
private final List<ConnectionListener> listeners;
ConnectionRegistryImpl() {
connections = new HashMap<TransportId, Map<ContactId, Integer>>();
contactCounts = new HashMap<ContactId, Integer>();
listeners = new CopyOnWriteArrayList<ConnectionListener>();
}
public synchronized void registerConnection(ContactId c, TransportId t) {
Map<ContactId, Integer> m = connections.get(t);
if(m == null) {
m = new HashMap<ContactId, Integer>();
connections.put(t, m);
public void addListener(ConnectionListener c) {
listeners.add(c);
}
public void removeListener(ConnectionListener c) {
listeners.remove(c);
}
public void registerConnection(ContactId c, TransportId t) {
boolean firstConnection = false;
synchronized(this) {
Map<ContactId, Integer> m = connections.get(t);
if(m == null) {
m = new HashMap<ContactId, Integer>();
connections.put(t, m);
}
Integer count = m.get(c);
if(count == null) m.put(c, 1);
else m.put(c, count + 1);
count = contactCounts.get(c);
if(count == null) {
firstConnection = true;
contactCounts.put(c, 1);
} else {
contactCounts.put(c, count + 1);
}
}
Integer count = m.get(c);
if(count == null) m.put(c, 1);
else m.put(c, count + 1);
if(firstConnection)
for(ConnectionListener l : listeners) l.contactConnected(c);
}
public synchronized void unregisterConnection(ContactId c, TransportId t) {
Map<ContactId, Integer> m = connections.get(t);
if(m == null) throw new IllegalArgumentException();
Integer count = m.remove(c);
if(count == null) throw new IllegalArgumentException();
if(count == 1) {
if(m.isEmpty()) connections.remove(t);
} else {
m.put(c, count - 1);
public void unregisterConnection(ContactId c, TransportId t) {
boolean lastConnection = false;
synchronized(this) {
Map<ContactId, Integer> m = connections.get(t);
if(m == null) throw new IllegalArgumentException();
Integer count = m.remove(c);
if(count == null) throw new IllegalArgumentException();
if(count == 1) {
if(m.isEmpty()) connections.remove(t);
} else {
m.put(c, count - 1);
}
count = contactCounts.get(c);
if(count == null) throw new IllegalArgumentException();
if(count == 1) {
lastConnection = true;
contactCounts.remove(c);
} else {
contactCounts.put(c, count - 1);
}
}
if(lastConnection)
for(ConnectionListener l : listeners) l.contactDisconnected(c);
}
public synchronized Collection<ContactId> getConnectedContacts(
......@@ -50,4 +90,8 @@ class ConnectionRegistryImpl implements ConnectionRegistry {
List<ContactId> keys = new ArrayList<ContactId>(m.keySet());
return Collections.unmodifiableList(keys);
}
public synchronized boolean isConnected(ContactId c) {
return contactCounts.containsKey(c);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment