mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-13 11:19:04 +01:00
DO NOT MERGE: Add hacky poller method for testing discovery.
This commit is contained in:
@@ -11,6 +11,7 @@ import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
import org.briarproject.bramble.api.plugin.ConnectionHandler;
|
||||
import org.briarproject.bramble.api.plugin.ConnectionManager;
|
||||
import org.briarproject.bramble.api.plugin.ConnectionRegistry;
|
||||
import org.briarproject.bramble.api.plugin.DiscoveryHandler;
|
||||
import org.briarproject.bramble.api.plugin.Plugin;
|
||||
import org.briarproject.bramble.api.plugin.PluginManager;
|
||||
import org.briarproject.bramble.api.plugin.TransportConnectionReader;
|
||||
@@ -211,7 +212,7 @@ class PollerImpl implements Poller, EventListener {
|
||||
@IoExecutor
|
||||
private void poll(Plugin p) {
|
||||
TransportId t = p.getId();
|
||||
if (LOG.isLoggable(INFO)) LOG.info("Polling plugin " + t);
|
||||
if (LOG.isLoggable(INFO)) LOG.info("Polling " + t);
|
||||
try {
|
||||
Map<ContactId, TransportProperties> remote =
|
||||
transportPropertyManager.getRemoteProperties(t);
|
||||
@@ -221,8 +222,10 @@ class PollerImpl implements Poller, EventListener {
|
||||
new ArrayList<>();
|
||||
for (Entry<ContactId, TransportProperties> e : remote.entrySet()) {
|
||||
ContactId c = e.getKey();
|
||||
if (!connected.contains(c))
|
||||
properties.add(new Pair<>(e.getValue(), new Handler(c, t)));
|
||||
if (!connected.contains(c)) {
|
||||
ConnHandler handler = new ConnHandler(c, t);
|
||||
properties.add(new Pair<>(e.getValue(), handler));
|
||||
}
|
||||
}
|
||||
if (!properties.isEmpty()) p.poll(properties);
|
||||
} catch (DbException e) {
|
||||
@@ -230,6 +233,30 @@ class PollerImpl implements Poller, EventListener {
|
||||
}
|
||||
}
|
||||
|
||||
@IoExecutor
|
||||
private void discover(DuplexPlugin p) {
|
||||
TransportId t = p.getId();
|
||||
if (LOG.isLoggable(INFO)) LOG.info("Discovering peers for " + t);
|
||||
try {
|
||||
Map<ContactId, TransportProperties> remote =
|
||||
transportPropertyManager.getRemoteProperties(t);
|
||||
Collection<ContactId> connected =
|
||||
connectionRegistry.getConnectedContacts(t);
|
||||
List<Pair<TransportProperties, DiscoveryHandler>> properties =
|
||||
new ArrayList<>();
|
||||
for (Entry<ContactId, TransportProperties> e : remote.entrySet()) {
|
||||
ContactId c = e.getKey();
|
||||
if (!connected.contains(c)) {
|
||||
DiscoHandler handler = new DiscoHandler(c, p);
|
||||
properties.add(new Pair<>(e.getValue(), handler));
|
||||
}
|
||||
}
|
||||
if (!properties.isEmpty()) p.discoverPeers(properties);
|
||||
} catch (DbException e) {
|
||||
logException(LOG, WARNING, e);
|
||||
}
|
||||
}
|
||||
|
||||
private class ScheduledPollTask {
|
||||
|
||||
private final PollTask task;
|
||||
@@ -269,16 +296,23 @@ class PollerImpl implements Poller, EventListener {
|
||||
int delay = plugin.getPollingInterval();
|
||||
if (randomiseNext) delay = (int) (delay * random.nextDouble());
|
||||
schedule(plugin, delay, false);
|
||||
poll(plugin);
|
||||
// FIXME: Revert
|
||||
if (plugin instanceof DuplexPlugin) {
|
||||
DuplexPlugin d = (DuplexPlugin) plugin;
|
||||
if (d.supportsDiscovery()) discover(d);
|
||||
else poll(d);
|
||||
} else {
|
||||
poll(plugin);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private class Handler implements ConnectionHandler {
|
||||
private class ConnHandler implements ConnectionHandler {
|
||||
|
||||
private final ContactId contactId;
|
||||
private final TransportId transportId;
|
||||
|
||||
private Handler(ContactId contactId, TransportId transportId) {
|
||||
private ConnHandler(ContactId contactId, TransportId transportId) {
|
||||
this.contactId = contactId;
|
||||
this.transportId = transportId;
|
||||
}
|
||||
@@ -301,4 +335,27 @@ class PollerImpl implements Poller, EventListener {
|
||||
transportId, w);
|
||||
}
|
||||
}
|
||||
|
||||
private class DiscoHandler implements DiscoveryHandler {
|
||||
|
||||
private final ContactId contactId;
|
||||
private final DuplexPlugin plugin;
|
||||
|
||||
private DiscoHandler(ContactId contactId, DuplexPlugin plugin) {
|
||||
this.contactId = contactId;
|
||||
this.plugin = plugin;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handleDevice(TransportProperties p) {
|
||||
LOG.info("Discovered contact via " + plugin.getId());
|
||||
ioExecutor.execute(() -> {
|
||||
DuplexTransportConnection c = plugin.createConnection(p);
|
||||
if (c != null) {
|
||||
connectionManager.manageOutgoingConnection(contactId,
|
||||
plugin.getId(), c);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -351,6 +351,9 @@ public class PollerImplTest extends BrambleMockTestCase {
|
||||
oneOf(scheduler).schedule(with(any(Runnable.class)),
|
||||
with((long) (pollingInterval * 0.5)), with(MILLISECONDS));
|
||||
will(returnValue(future));
|
||||
// FIXME: Revert
|
||||
oneOf(plugin).supportsDiscovery();
|
||||
will(returnValue(false));
|
||||
// Get the transport properties and connected contacts
|
||||
oneOf(transportPropertyManager).getRemoteProperties(transportId);
|
||||
will(returnValue(singletonMap(contactId, properties)));
|
||||
@@ -394,6 +397,9 @@ public class PollerImplTest extends BrambleMockTestCase {
|
||||
oneOf(scheduler).schedule(with(any(Runnable.class)),
|
||||
with((long) (pollingInterval * 0.5)), with(MILLISECONDS));
|
||||
will(returnValue(future));
|
||||
// FIXME: Revert
|
||||
oneOf(plugin).supportsDiscovery();
|
||||
will(returnValue(false));
|
||||
// Get the transport properties and connected contacts
|
||||
oneOf(transportPropertyManager).getRemoteProperties(transportId);
|
||||
will(returnValue(singletonMap(contactId, properties)));
|
||||
|
||||
Reference in New Issue
Block a user