Commit 43c7d2cb authored by akwizgran's avatar akwizgran
Browse files

Add service discovery test.

parent 6ad3a303
......@@ -24,8 +24,9 @@ import static org.briarproject.interferometer.MainViewModel.StartResult.NO_BLUET
import static org.briarproject.interferometer.MainViewModel.TestPhase.BT_DISABLED;
import static org.briarproject.interferometer.MainViewModel.TestPhase.BT_ENABLED;
import static org.briarproject.interferometer.MainViewModel.TestPhase.CONNECTIONS;
import static org.briarproject.interferometer.MainViewModel.TestPhase.DISCOVERY;
import static org.briarproject.interferometer.MainViewModel.TestPhase.DEVICE_DISCOVERY;
import static org.briarproject.interferometer.MainViewModel.TestPhase.READY;
import static org.briarproject.interferometer.MainViewModel.TestPhase.SERVICE_DISCOVERY;
import static org.briarproject.interferometer.Utils.formatSpeed;
public class MainActivity extends AppCompatActivity {
......@@ -88,8 +89,10 @@ public class MainActivity extends AppCompatActivity {
testPhaseTextView.setText(R.string.testing_bt_disabled);
} else if (phase == BT_ENABLED) {
testPhaseTextView.setText(R.string.testing_bt_enabled);
} else if (phase == DISCOVERY) {
testPhaseTextView.setText(R.string.testing_discovery);
} else if (phase == DEVICE_DISCOVERY) {
testPhaseTextView.setText(R.string.testing_device_discovery);
} else if (phase == SERVICE_DISCOVERY) {
testPhaseTextView.setText(R.string.testing_service_discovery);
} else if (phase == CONNECTIONS) {
testPhaseTextView.setText(R.string.testing_connections);
}
......
......@@ -40,10 +40,11 @@ import static android.bluetooth.BluetoothAdapter.ACTION_STATE_CHANGED;
import static android.bluetooth.BluetoothAdapter.EXTRA_STATE;
import static android.bluetooth.BluetoothAdapter.STATE_OFF;
import static android.bluetooth.BluetoothAdapter.STATE_ON;
import static android.bluetooth.BluetoothDevice.ACTION_UUID;
import static android.content.Context.WIFI_SERVICE;
import static android.net.wifi.WifiManager.WIFI_MODE_FULL_HIGH_PERF;
import static java.util.concurrent.Executors.newCachedThreadPool;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static java.util.concurrent.TimeUnit.SECONDS;
import static org.briarproject.interferometer.MainViewModel.ConnectionState.DISCONNECTED;
import static org.briarproject.interferometer.MainViewModel.ConnectionState.DOWNLOADING;
import static org.briarproject.interferometer.MainViewModel.ConnectionState.UPLOADING;
......@@ -53,24 +54,34 @@ import static org.briarproject.interferometer.MainViewModel.StartResult.STARTED;
import static org.briarproject.interferometer.MainViewModel.TestPhase.BT_DISABLED;
import static org.briarproject.interferometer.MainViewModel.TestPhase.BT_ENABLED;
import static org.briarproject.interferometer.MainViewModel.TestPhase.CONNECTIONS;
import static org.briarproject.interferometer.MainViewModel.TestPhase.DISCOVERY;
import static org.briarproject.interferometer.MainViewModel.TestPhase.DEVICE_DISCOVERY;
import static org.briarproject.interferometer.MainViewModel.TestPhase.READY;
import static org.briarproject.interferometer.MainViewModel.TestPhase.SERVICE_DISCOVERY;
import static org.briarproject.interferometer.Utils.mean;
public class MainViewModel extends AndroidViewModel {
enum TestPhase {READY, BT_DISABLED, BT_ENABLED, DISCOVERY, CONNECTIONS}
enum TestPhase {
READY,
BT_DISABLED,
BT_ENABLED,
DEVICE_DISCOVERY,
SERVICE_DISCOVERY,
CONNECTIONS
}
enum ConnectionState {DISCONNECTED, DOWNLOADING, UPLOADING}
enum StartResult {INVALID_ADDRESS, NO_BLUETOOTH, STARTED}
static final String DOWNLOAD_DISABLED = "downloadDisabled";
static final String UPLOAD_DISABLED = "uploadDisabled";
static final String DOWNLOAD_ENABLED = "downloadEnabled";
static final String UPLOAD_ENABLED = "uploadEnabled";
static final String DOWNLOAD_DISCOVERY = "downloadDiscovery";
static final String UPLOAD_DISCOVERY = "uploadDiscovery";
static final String DOWNLOAD_DISABLED = "downDisabled";
static final String UPLOAD_DISABLED = "upDisabled";
static final String DOWNLOAD_ENABLED = "downEnabled";
static final String UPLOAD_ENABLED = "upEnabled";
static final String DOWNLOAD_DEVICE_DISCOVERY = "downDeviceDiscovery";
static final String UPLOAD_DEVICE_DISCOVERY = "upDeviceDiscovery";
static final String DOWNLOAD_SERVICE_DISCOVERY = "downServiceDiscovery";
static final String UPLOAD_SERVICE_DISCOVERY = "upServiceDiscovery";
static final String DOWNLOAD_CONNECTIONS = "downloadConnections";
static final String UPLOAD_CONNECTIONS = "uploadConnections";
static final String CONSUMED = "consumed";
......@@ -140,9 +151,9 @@ public class MainViewModel extends AndroidViewModel {
testPhase.postValue(BT_DISABLED);
Log.i(TAG, "Testing throughput with Bluetooth disabled");
List<Long> downloadDisabled = new ArrayList<>();
List<Long> uploadDisabled = new ArrayList<>();
if (!testThroughput(hostPort, downloadDisabled, uploadDisabled)) return;
List<Long> downDisabled = new ArrayList<>();
List<Long> upDisabled = new ArrayList<>();
if (!testThroughput(hostPort, downDisabled, upDisabled)) return;
if (!enableBluetooth(adapter)) {
Log.w(TAG, "Failed to enable Bluetooth");
......@@ -151,23 +162,34 @@ public class MainViewModel extends AndroidViewModel {
testPhase.postValue(BT_ENABLED);
Log.i(TAG, "Testing throughput with Bluetooth enabled");
List<Long> downloadEnabled = new ArrayList<>();
List<Long> uploadEnabled = new ArrayList<>();
if (!testThroughput(hostPort, downloadEnabled, uploadEnabled)) return;
testPhase.postValue(DISCOVERY);
Log.i(TAG, "Testing throughput with Bluetooth discovery");
List<Long> downloadDiscovery = new ArrayList<>();
List<Long> uploadDiscovery = new ArrayList<>();
Future<?> discoveryTask = startDiscovery(adapter);
List<Long> downEnabled = new ArrayList<>();
List<Long> upEnabled = new ArrayList<>();
if (!testThroughput(hostPort, downEnabled, upEnabled)) return;
testPhase.postValue(DEVICE_DISCOVERY);
Log.i(TAG, "Testing throughput with device discovery");
List<Long> downDeviceDiscovery = new ArrayList<>();
List<Long> upDeviceDiscovery = new ArrayList<>();
Future<?> deviceDiscoveryTask = startDeviceDiscovery(adapter);
try {
if (!testThroughput(hostPort, downloadDiscovery, uploadDiscovery)) return;
if (!testThroughput(hostPort, downDeviceDiscovery, upDeviceDiscovery)) return;
} finally {
discoveryTask.cancel(true);
deviceDiscoveryTask.cancel(true);
}
testPhase.postValue(SERVICE_DISCOVERY);
Log.i(TAG, "Testing throughput with service discovery");
List<Long> downServiceDiscovery = new ArrayList<>();
List<Long> upServiceDiscovery = new ArrayList<>();
Future<?> serviceDiscoveryTask = startServiceDiscovery(adapter);
try {
if (!testThroughput(hostPort, downServiceDiscovery, upServiceDiscovery)) return;
} finally {
serviceDiscoveryTask.cancel(true);
}
testPhase.postValue(CONNECTIONS);
Log.i(TAG, "Testing throughput with Bluetooth connections");
Log.i(TAG, "Testing throughput with connections");
List<Long> downloadConnections = new ArrayList<>();
List<Long> uploadConnections = new ArrayList<>();
Future<?> connectionTask = startConnections(adapter);
......@@ -181,12 +203,14 @@ public class MainViewModel extends AndroidViewModel {
Log.i(TAG, "Test finished");
Bundle b = new Bundle();
b.putDouble(DOWNLOAD_DISABLED, mean(downloadDisabled));
b.putDouble(UPLOAD_DISABLED, mean(uploadDisabled));
b.putDouble(DOWNLOAD_ENABLED, mean(downloadEnabled));
b.putDouble(UPLOAD_ENABLED, mean(uploadEnabled));
b.putDouble(DOWNLOAD_DISCOVERY, mean(downloadDiscovery));
b.putDouble(UPLOAD_DISCOVERY, mean(uploadDiscovery));
b.putDouble(DOWNLOAD_DISABLED, mean(downDisabled));
b.putDouble(UPLOAD_DISABLED, mean(upDisabled));
b.putDouble(DOWNLOAD_ENABLED, mean(downEnabled));
b.putDouble(UPLOAD_ENABLED, mean(upEnabled));
b.putDouble(DOWNLOAD_DEVICE_DISCOVERY, mean(downDeviceDiscovery));
b.putDouble(UPLOAD_DEVICE_DISCOVERY, mean(upDeviceDiscovery));
b.putDouble(DOWNLOAD_SERVICE_DISCOVERY, mean(downServiceDiscovery));
b.putDouble(UPLOAD_SERVICE_DISCOVERY, mean(upServiceDiscovery));
b.putDouble(DOWNLOAD_CONNECTIONS, mean(downloadConnections));
b.putDouble(UPLOAD_CONNECTIONS, mean(uploadConnections));
b.putBoolean(CONSUMED, false);
......@@ -269,22 +293,22 @@ public class MainViewModel extends AndroidViewModel {
}
}
private Future<?> startDiscovery(BluetoothAdapter adapter) {
private Future<?> startDeviceDiscovery(BluetoothAdapter adapter) {
Application app = getApplication();
return executorService.submit(() -> {
while (!Thread.currentThread().isInterrupted()) {
BluetoothDiscoveryReceiver receiver = new BluetoothDiscoveryReceiver();
app.registerReceiver(receiver, new IntentFilter(ACTION_DISCOVERY_FINISHED));
try {
Log.i(TAG, "Starting discovery");
Log.i(TAG, "Starting device discovery");
adapter.startDiscovery();
if (receiver.waitForDiscoveryToFinish()) {
Log.i(TAG, "Discovery finished");
Log.i(TAG, "Device discovery finished");
} else {
Log.w(TAG, "Discovery did not finish within timeout");
Log.w(TAG, "Device discovery did not finish within timeout");
}
} catch (InterruptedException e) {
Log.w(TAG, "Discovery interrupted");
Log.w(TAG, "Device discovery interrupted");
return;
} finally {
app.unregisterReceiver(receiver);
......@@ -294,6 +318,37 @@ public class MainViewModel extends AndroidViewModel {
});
}
private Future<?> startServiceDiscovery(BluetoothAdapter adapter) {
Application app = getApplication();
return executorService.submit(() -> {
byte[] mac = new byte[6];
Random random = new Random();
while (!Thread.currentThread().isInterrupted()) {
BluetoothDiscoveryReceiver receiver = new BluetoothDiscoveryReceiver();
app.registerReceiver(receiver, new IntentFilter(ACTION_UUID));
random.nextBytes(mac);
BluetoothDevice device = adapter.getRemoteDevice(mac);
try {
if (device.fetchUuidsWithSdp()) {
Log.i(TAG, "Starting service discovery");
if (receiver.waitForDiscoveryToFinish()) {
Log.i(TAG, "Service discovery finished");
} else {
Log.w(TAG, "Service discovery did not finish within timeout");
}
} else {
Log.w(TAG, "Failed to start service discovery");
}
} catch (InterruptedException e) {
Log.w(TAG, "Service discovery interrupted");
return;
} finally {
app.unregisterReceiver(receiver);
}
}
});
}
private Future<?> startConnections(BluetoothAdapter adapter) {
return executorService.submit(() -> {
byte[] mac = new byte[6];
......@@ -491,8 +546,6 @@ public class MainViewModel extends AndroidViewModel {
private static class BluetoothStateReceiver extends BroadcastReceiver {
private static final int TIMEOUT = 10_000;
private final CountDownLatch latch = new CountDownLatch(1);
private final int targetState;
......@@ -502,33 +555,25 @@ public class MainViewModel extends AndroidViewModel {
@Override
public void onReceive(Context context, Intent intent) {
int state = intent.getIntExtra(EXTRA_STATE, 0);
Log.i(TAG, "Bluetooth state changed to " + state);
if (state == targetState) {
Log.i(TAG, "Reached target state");
latch.countDown();
}
if (intent.getIntExtra(EXTRA_STATE, 0) == targetState) latch.countDown();
}
private boolean waitForTargetState() throws InterruptedException {
return latch.await(TIMEOUT, MILLISECONDS);
return latch.await(10, SECONDS);
}
}
private static class BluetoothDiscoveryReceiver extends BroadcastReceiver {
private static final int TIMEOUT = 60_000;
private final CountDownLatch latch = new CountDownLatch(1);
@Override
public void onReceive(Context context, Intent intent) {
Log.i(TAG, "Discovery finished");
latch.countDown();
}
private boolean waitForDiscoveryToFinish() throws InterruptedException {
return latch.await(TIMEOUT, MILLISECONDS);
return latch.await(60, SECONDS);
}
}
}
......@@ -14,13 +14,15 @@ import androidx.fragment.app.DialogFragment;
import static org.briarproject.interferometer.MainViewModel.CONSUMED;
import static org.briarproject.interferometer.MainViewModel.DOWNLOAD_CONNECTIONS;
import static org.briarproject.interferometer.MainViewModel.DOWNLOAD_DEVICE_DISCOVERY;
import static org.briarproject.interferometer.MainViewModel.DOWNLOAD_DISABLED;
import static org.briarproject.interferometer.MainViewModel.DOWNLOAD_DISCOVERY;
import static org.briarproject.interferometer.MainViewModel.DOWNLOAD_ENABLED;
import static org.briarproject.interferometer.MainViewModel.DOWNLOAD_SERVICE_DISCOVERY;
import static org.briarproject.interferometer.MainViewModel.UPLOAD_CONNECTIONS;
import static org.briarproject.interferometer.MainViewModel.UPLOAD_DEVICE_DISCOVERY;
import static org.briarproject.interferometer.MainViewModel.UPLOAD_DISABLED;
import static org.briarproject.interferometer.MainViewModel.UPLOAD_DISCOVERY;
import static org.briarproject.interferometer.MainViewModel.UPLOAD_ENABLED;
import static org.briarproject.interferometer.MainViewModel.UPLOAD_SERVICE_DISCOVERY;
import static org.briarproject.interferometer.Utils.formatSpeed;
public class ResultsDialogFragment extends DialogFragment {
......@@ -51,10 +53,15 @@ public class ResultsDialogFragment extends DialogFragment {
formatSpeed(args.getDouble(DOWNLOAD_ENABLED)),
formatSpeed(args.getDouble(UPLOAD_ENABLED))));
TextView discovery = v.findViewById(R.id.discovery);
discovery.setText(getString(R.string.results_format_discovery,
formatSpeed(args.getDouble(DOWNLOAD_DISCOVERY)),
formatSpeed(args.getDouble(UPLOAD_DISCOVERY))));
TextView deviceDiscovery = v.findViewById(R.id.device_discovery);
deviceDiscovery.setText(getString(R.string.results_format_device_discovery,
formatSpeed(args.getDouble(DOWNLOAD_DEVICE_DISCOVERY)),
formatSpeed(args.getDouble(UPLOAD_DEVICE_DISCOVERY))));
TextView serviceDiscovery = v.findViewById(R.id.service_discovery);
serviceDiscovery.setText(getString(R.string.results_format_service_discovery,
formatSpeed(args.getDouble(DOWNLOAD_SERVICE_DISCOVERY)),
formatSpeed(args.getDouble(UPLOAD_SERVICE_DISCOVERY))));
TextView connections = v.findViewById(R.id.connections);
connections.setText(getString(R.string.results_format_connections,
......
......@@ -18,7 +18,13 @@
android:layout_marginBottom="8dp" />
<TextView
android:id="@+id/discovery"
android:id="@+id/device_discovery"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp" />
<TextView
android:id="@+id/service_discovery"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp" />
......
......@@ -11,8 +11,9 @@
<string name="testing_bt_disabled">Testing with Bluetooth disabled</string>
<string name="testing_bt_enabled">Testing with Bluetooth enabled</string>
<string name="testing_discovery">Testing with Bluetooth discovery</string>
<string name="testing_connections">Testing with Bluetooth connections</string>
<string name="testing_device_discovery">Testing with device discovery</string>
<string name="testing_service_discovery">Testing with service discovery</string>
<string name="testing_connections">Testing with connections</string>
<string name="download_format">Download: %s</string>
<string name="upload_format">Upload: %s</string>
......@@ -20,6 +21,7 @@
<string name="results_title">Wi-Fi Throughput</string>
<string name="results_format_bt_disabled">BT disabled: %s down, %s up</string>
<string name="results_format_bt_enabled">BT enabled: %s down, %s up</string>
<string name="results_format_discovery">Discovery: %s down, %s up</string>
<string name="results_format_device_discovery">Device discovery: %s down, %s up</string>
<string name="results_format_service_discovery">Service discovery: %s down, %s up</string>
<string name="results_format_connections">Connections: %s down, %s up</string>
</resources>
Supports Markdown
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