Commit a4a45efd authored by akwizgran's avatar akwizgran

Broadcast event when polling newly added contact.

parent 208ae6a4
Pipeline #3501 passed with stage
in 7 minutes and 56 seconds
package org.briarproject.bramble.api.rendezvous; package org.briarproject.bramble.api.rendezvous;
import org.briarproject.bramble.api.plugin.TransportId; import org.briarproject.bramble.api.contact.PendingContactId;
/** /**
* Interface for the poller that makes rendezvous connections to pending * Interface for the poller that makes rendezvous connections to pending
...@@ -8,5 +8,5 @@ import org.briarproject.bramble.api.plugin.TransportId; ...@@ -8,5 +8,5 @@ import org.briarproject.bramble.api.plugin.TransportId;
*/ */
public interface RendezvousPoller { public interface RendezvousPoller {
long getLastPollTime(TransportId t); long getLastPollTime(PendingContactId p);
} }
...@@ -90,7 +90,7 @@ class RendezvousPollerImpl implements RendezvousPoller, Service, EventListener { ...@@ -90,7 +90,7 @@ class RendezvousPollerImpl implements RendezvousPoller, Service, EventListener {
private final Clock clock; private final Clock clock;
private final AtomicBoolean used = new AtomicBoolean(false); private final AtomicBoolean used = new AtomicBoolean(false);
private final Map<TransportId, Long> lastPollTimes = private final Map<PendingContactId, Long> lastPollTimes =
new ConcurrentHashMap<>(); new ConcurrentHashMap<>();
// Executor that runs one task at a time // Executor that runs one task at a time
...@@ -126,8 +126,8 @@ class RendezvousPollerImpl implements RendezvousPoller, Service, EventListener { ...@@ -126,8 +126,8 @@ class RendezvousPollerImpl implements RendezvousPoller, Service, EventListener {
} }
@Override @Override
public long getLastPollTime(TransportId t) { public long getLastPollTime(PendingContactId p) {
Long time = lastPollTimes.get(t); Long time = lastPollTimes.get(p);
return time == null ? 0 : time; return time == null ? 0 : time;
} }
...@@ -227,6 +227,7 @@ class RendezvousPollerImpl implements RendezvousPoller, Service, EventListener { ...@@ -227,6 +227,7 @@ class RendezvousPollerImpl implements RendezvousPoller, Service, EventListener {
private void removePendingContact(PendingContactId p) { private void removePendingContact(PendingContactId p) {
// We can come here twice if a pending contact expires and is removed // We can come here twice if a pending contact expires and is removed
if (cryptoStates.remove(p) == null) return; if (cryptoStates.remove(p) == null) return;
lastPollTimes.remove(p);
for (PluginState ps : pluginStates.values()) { for (PluginState ps : pluginStates.values()) {
RendezvousEndpoint endpoint = ps.endpoints.remove(p); RendezvousEndpoint endpoint = ps.endpoints.remove(p);
if (endpoint != null) tryToClose(endpoint, LOG, INFO); if (endpoint != null) tryToClose(endpoint, LOG, INFO);
...@@ -246,9 +247,10 @@ class RendezvousPollerImpl implements RendezvousPoller, Service, EventListener { ...@@ -246,9 +247,10 @@ class RendezvousPollerImpl implements RendezvousPoller, Service, EventListener {
Handler h = new Handler(e.getKey(), t, false); Handler h = new Handler(e.getKey(), t, false);
properties.add(new Pair<>(props, h)); properties.add(new Pair<>(props, h));
} }
lastPollTimes.put(t, clock.currentTimeMillis()); List<PendingContactId> polled = new ArrayList<>(ps.endpoints.keySet());
eventBus.broadcast(new RendezvousPollEvent(t, long now = clock.currentTimeMillis();
new ArrayList<>(ps.endpoints.keySet()))); for (PendingContactId p : polled) lastPollTimes.put(p, now);
eventBus.broadcast(new RendezvousPollEvent(t, polled));
ps.plugin.poll(properties); ps.plugin.poll(properties);
} }
...@@ -294,9 +296,13 @@ class RendezvousPollerImpl implements RendezvousPoller, Service, EventListener { ...@@ -294,9 +296,13 @@ class RendezvousPollerImpl implements RendezvousPoller, Service, EventListener {
for (PluginState ps : pluginStates.values()) { for (PluginState ps : pluginStates.values()) {
RendezvousEndpoint endpoint = ps.endpoints.get(p); RendezvousEndpoint endpoint = ps.endpoints.get(p);
if (endpoint != null) { if (endpoint != null) {
TransportId t = ps.plugin.getId();
TransportProperties props = TransportProperties props =
endpoint.getRemoteTransportProperties(); endpoint.getRemoteTransportProperties();
Handler h = new Handler(p, ps.plugin.getId(), false); Handler h = new Handler(p, t, false);
lastPollTimes.put(p, clock.currentTimeMillis());
eventBus.broadcast(
new RendezvousPollEvent(t, singletonList(p)));
ps.plugin.poll(singletonList(new Pair<>(props, h))); ps.plugin.poll(singletonList(new Pair<>(props, h)));
} }
} }
......
...@@ -24,6 +24,7 @@ import org.briarproject.bramble.api.rendezvous.KeyMaterialSource; ...@@ -24,6 +24,7 @@ import org.briarproject.bramble.api.rendezvous.KeyMaterialSource;
import org.briarproject.bramble.api.rendezvous.RendezvousEndpoint; import org.briarproject.bramble.api.rendezvous.RendezvousEndpoint;
import org.briarproject.bramble.api.rendezvous.event.RendezvousConnectionClosedEvent; import org.briarproject.bramble.api.rendezvous.event.RendezvousConnectionClosedEvent;
import org.briarproject.bramble.api.rendezvous.event.RendezvousConnectionOpenedEvent; import org.briarproject.bramble.api.rendezvous.event.RendezvousConnectionOpenedEvent;
import org.briarproject.bramble.api.rendezvous.event.RendezvousPollEvent;
import org.briarproject.bramble.api.system.Clock; import org.briarproject.bramble.api.system.Clock;
import org.briarproject.bramble.test.BrambleMockTestCase; import org.briarproject.bramble.test.BrambleMockTestCase;
import org.briarproject.bramble.test.CaptureArgumentAction; import org.briarproject.bramble.test.CaptureArgumentAction;
...@@ -191,6 +192,9 @@ public class RendezvousPollerImplTest extends BrambleMockTestCase { ...@@ -191,6 +192,9 @@ public class RendezvousPollerImplTest extends BrambleMockTestCase {
// Poll newly added pending contact // Poll newly added pending contact
oneOf(rendezvousEndpoint).getRemoteTransportProperties(); oneOf(rendezvousEndpoint).getRemoteTransportProperties();
will(returnValue(transportProperties)); will(returnValue(transportProperties));
oneOf(clock).currentTimeMillis();
will(returnValue(beforeExpiry));
oneOf(eventBus).broadcast(with(any(RendezvousPollEvent.class)));
oneOf(plugin).poll(with(collectionOf(pairOf( oneOf(plugin).poll(with(collectionOf(pairOf(
equal(transportProperties), equal(transportProperties),
any(ConnectionHandler.class))))); any(ConnectionHandler.class)))));
...@@ -242,6 +246,9 @@ public class RendezvousPollerImplTest extends BrambleMockTestCase { ...@@ -242,6 +246,9 @@ public class RendezvousPollerImplTest extends BrambleMockTestCase {
// Poll newly added pending contact // Poll newly added pending contact
oneOf(rendezvousEndpoint).getRemoteTransportProperties(); oneOf(rendezvousEndpoint).getRemoteTransportProperties();
will(returnValue(transportProperties)); will(returnValue(transportProperties));
oneOf(clock).currentTimeMillis();
will(returnValue(beforeExpiry));
oneOf(eventBus).broadcast(with(any(RendezvousPollEvent.class)));
oneOf(plugin).poll(with(collectionOf(pairOf( oneOf(plugin).poll(with(collectionOf(pairOf(
equal(transportProperties), equal(transportProperties),
any(ConnectionHandler.class))))); any(ConnectionHandler.class)))));
......
...@@ -18,7 +18,6 @@ import org.briarproject.bramble.api.event.Event; ...@@ -18,7 +18,6 @@ import org.briarproject.bramble.api.event.Event;
import org.briarproject.bramble.api.event.EventBus; import org.briarproject.bramble.api.event.EventBus;
import org.briarproject.bramble.api.event.EventListener; import org.briarproject.bramble.api.event.EventListener;
import org.briarproject.bramble.api.nullsafety.NotNullByDefault; import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
import org.briarproject.bramble.api.plugin.TorConstants;
import org.briarproject.bramble.api.rendezvous.RendezvousPoller; import org.briarproject.bramble.api.rendezvous.RendezvousPoller;
import org.briarproject.bramble.api.rendezvous.event.RendezvousPollEvent; import org.briarproject.bramble.api.rendezvous.event.RendezvousPollEvent;
...@@ -83,14 +82,14 @@ public class PendingContactListViewModel extends AndroidViewModel ...@@ -83,14 +82,14 @@ public class PendingContactListViewModel extends AndroidViewModel
private void loadPendingContacts() { private void loadPendingContacts() {
dbExecutor.execute(() -> { dbExecutor.execute(() -> {
try { try {
long lastPoll =
rendezvousPoller.getLastPollTime(TorConstants.ID);
Collection<Pair<PendingContact, PendingContactState>> pairs = Collection<Pair<PendingContact, PendingContactState>> pairs =
contactManager.getPendingContacts(); contactManager.getPendingContacts();
List<PendingContactItem> items = new ArrayList<>(pairs.size()); List<PendingContactItem> items = new ArrayList<>(pairs.size());
for (Pair<PendingContact, PendingContactState> p : pairs) { for (Pair<PendingContact, PendingContactState> pair : pairs) {
items.add(new PendingContactItem(p.getFirst(), PendingContact p = pair.getFirst();
p.getSecond(), lastPoll)); long lastPoll = rendezvousPoller.getLastPollTime(p.getId());
items.add(new PendingContactItem(p, pair.getSecond(),
lastPoll));
} }
pendingContacts.postValue(items); pendingContacts.postValue(items);
} catch (DbException e) { } catch (DbException e) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment