New polling logic for Bluetooth. #251

The polling interval increases exponentially each time polling is unsuccessful, up to a maximum of 60 minutes. The interval is reset to 2 minutes whenever a connection is made and whenever Bluetooth is re-enabled.
This commit is contained in:
akwizgran
2016-02-12 15:40:01 +00:00
parent a774b3419a
commit c081c08ff5
16 changed files with 216 additions and 133 deletions

View File

@@ -13,6 +13,7 @@ import org.briarproject.api.TransportId;
import org.briarproject.api.android.AndroidExecutor;
import org.briarproject.api.contact.ContactId;
import org.briarproject.api.crypto.PseudoRandom;
import org.briarproject.api.plugins.Backoff;
import org.briarproject.api.plugins.duplex.DuplexPlugin;
import org.briarproject.api.plugins.duplex.DuplexPluginCallback;
import org.briarproject.api.plugins.duplex.DuplexTransportConnection;
@@ -68,8 +69,9 @@ class DroidtoothPlugin implements DuplexPlugin {
private final Context appContext;
private final SecureRandom secureRandom;
private final Clock clock;
private final Backoff backoff;
private final DuplexPluginCallback callback;
private final int maxLatency, pollingInterval;
private final int maxLatency;
private volatile boolean running = false;
private volatile boolean wasEnabledByUs = false;
@@ -81,16 +83,15 @@ class DroidtoothPlugin implements DuplexPlugin {
DroidtoothPlugin(Executor ioExecutor, AndroidExecutor androidExecutor,
Context appContext, SecureRandom secureRandom, Clock clock,
DuplexPluginCallback callback, int maxLatency,
int pollingInterval) {
Backoff backoff, DuplexPluginCallback callback, int maxLatency) {
this.ioExecutor = ioExecutor;
this.androidExecutor = androidExecutor;
this.appContext = appContext;
this.secureRandom = secureRandom;
this.clock = clock;
this.backoff = backoff;
this.callback = callback;
this.maxLatency = maxLatency;
this.pollingInterval = pollingInterval;
}
public TransportId getId() {
@@ -173,6 +174,7 @@ class DroidtoothPlugin implements DuplexPlugin {
}
LOG.info("Socket bound");
socket = ss;
backoff.reset();
callback.transportEnabled();
acceptContactConnections();
}
@@ -216,6 +218,7 @@ class DroidtoothPlugin implements DuplexPlugin {
String address = s.getRemoteDevice().getAddress();
LOG.info("Connection from " + address);
}
backoff.reset();
callback.incomingConnectionCreated(wrapSocket(s));
}
}
@@ -244,11 +247,12 @@ class DroidtoothPlugin implements DuplexPlugin {
}
public int getPollingInterval() {
return pollingInterval;
return backoff.getPollingInterval();
}
public void poll(Collection<ContactId> connected) {
if (!isRunning()) return;
backoff.increment();
// Try to connect to known devices in parallel
Map<ContactId, TransportProperties> remote =
callback.getRemoteProperties();
@@ -263,8 +267,10 @@ class DroidtoothPlugin implements DuplexPlugin {
public void run() {
if (!running) return;
BluetoothSocket s = connect(address, uuid);
if (s != null)
if (s != null) {
backoff.reset();
callback.outgoingConnectionCreated(c, wrapSocket(s));
}
}
});
}