Commit 1e4dd3c1 authored by akwizgran's avatar akwizgran

Add optional idle period between discovery/connection attempts.

parent 43c7d2cb
......@@ -32,7 +32,7 @@ import static org.briarproject.interferometer.Utils.formatSpeed;
public class MainActivity extends AppCompatActivity {
private MainViewModel viewModel;
private EditText serverAddressEditText;
private EditText serverAddressEditText, idlePeriodEditText;
private Button testButton;
private TextView testPhaseTextView, throughputTextView;
......@@ -46,6 +46,7 @@ public class MainActivity extends AppCompatActivity {
viewModel = new ViewModelProvider(this).get(MainViewModel.class);
serverAddressEditText = findViewById(R.id.server_address_edit_text);
idlePeriodEditText = findViewById(R.id.idle_period_edit_text);
testPhaseTextView = findViewById(R.id.test_phase_text_view);
throughputTextView = findViewById(R.id.throughput_text_view);
testButton = findViewById(R.id.test_button);
......@@ -107,7 +108,9 @@ public class MainActivity extends AppCompatActivity {
public void onTestClick(View view) {
TestPhase phase = viewModel.getTestPhase().getValue();
if (phase == null || phase == READY) {
StartResult result = viewModel.startTest(serverAddressEditText.getText().toString());
String serverAddress = serverAddressEditText.getText().toString();
String idlePeriod = idlePeriodEditText.getText().toString();
StartResult result = viewModel.startTest(serverAddress, idlePeriod);
if (result == INVALID_ADDRESS) {
Toast.makeText(this, R.string.server_address_error, LENGTH_SHORT).show();
} else if (result == NO_BLUETOOTH) {
......
......@@ -127,9 +127,10 @@ public class MainViewModel extends AndroidViewModel {
}
@UiThread
StartResult startTest(String serverAddress) {
StartResult startTest(String serverAddress, String idlePeriod) {
HostPort hostPort = parseServerAddress(serverAddress);
if (hostPort == null) return INVALID_ADDRESS;
int idleSeconds = parseIdlePeriod(idlePeriod);
BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
if (adapter == null) return NO_BLUETOOTH;
......@@ -170,7 +171,7 @@ public class MainViewModel extends AndroidViewModel {
Log.i(TAG, "Testing throughput with device discovery");
List<Long> downDeviceDiscovery = new ArrayList<>();
List<Long> upDeviceDiscovery = new ArrayList<>();
Future<?> deviceDiscoveryTask = startDeviceDiscovery(adapter);
Future<?> deviceDiscoveryTask = startDeviceDiscovery(adapter, idleSeconds);
try {
if (!testThroughput(hostPort, downDeviceDiscovery, upDeviceDiscovery)) return;
} finally {
......@@ -181,7 +182,7 @@ public class MainViewModel extends AndroidViewModel {
Log.i(TAG, "Testing throughput with service discovery");
List<Long> downServiceDiscovery = new ArrayList<>();
List<Long> upServiceDiscovery = new ArrayList<>();
Future<?> serviceDiscoveryTask = startServiceDiscovery(adapter);
Future<?> serviceDiscoveryTask = startServiceDiscovery(adapter, idleSeconds);
try {
if (!testThroughput(hostPort, downServiceDiscovery, upServiceDiscovery)) return;
} finally {
......@@ -192,7 +193,7 @@ public class MainViewModel extends AndroidViewModel {
Log.i(TAG, "Testing throughput with connections");
List<Long> downloadConnections = new ArrayList<>();
List<Long> uploadConnections = new ArrayList<>();
Future<?> connectionTask = startConnections(adapter);
Future<?> connectionTask = startConnections(adapter, idleSeconds);
try {
if (!testThroughput(hostPort, downloadConnections, uploadConnections)) return;
} finally {
......@@ -259,6 +260,14 @@ public class MainViewModel extends AndroidViewModel {
}
}
private int parseIdlePeriod(String s) {
try {
return Math.max(0, Integer.parseInt(s));
} catch (NumberFormatException e) {
return 0;
}
}
private boolean disableBluetooth(BluetoothAdapter adapter) {
if (!adapter.isEnabled()) return true;
BluetoothStateReceiver receiver = new BluetoothStateReceiver(STATE_OFF);
......@@ -293,7 +302,7 @@ public class MainViewModel extends AndroidViewModel {
}
}
private Future<?> startDeviceDiscovery(BluetoothAdapter adapter) {
private Future<?> startDeviceDiscovery(BluetoothAdapter adapter, int idleSeconds) {
Application app = getApplication();
return executorService.submit(() -> {
while (!Thread.currentThread().isInterrupted()) {
......@@ -307,6 +316,7 @@ public class MainViewModel extends AndroidViewModel {
} else {
Log.w(TAG, "Device discovery did not finish within timeout");
}
if (idleSeconds > 0) Thread.sleep(idleSeconds * 1000);
} catch (InterruptedException e) {
Log.w(TAG, "Device discovery interrupted");
return;
......@@ -318,7 +328,7 @@ public class MainViewModel extends AndroidViewModel {
});
}
private Future<?> startServiceDiscovery(BluetoothAdapter adapter) {
private Future<?> startServiceDiscovery(BluetoothAdapter adapter, int idleSeconds) {
Application app = getApplication();
return executorService.submit(() -> {
byte[] mac = new byte[6];
......@@ -336,6 +346,7 @@ public class MainViewModel extends AndroidViewModel {
} else {
Log.w(TAG, "Service discovery did not finish within timeout");
}
if (idleSeconds > 0) Thread.sleep(idleSeconds * 1000);
} else {
Log.w(TAG, "Failed to start service discovery");
}
......@@ -349,7 +360,7 @@ public class MainViewModel extends AndroidViewModel {
});
}
private Future<?> startConnections(BluetoothAdapter adapter) {
private Future<?> startConnections(BluetoothAdapter adapter, int idleSeconds) {
return executorService.submit(() -> {
byte[] mac = new byte[6];
byte[] uuidBytes = new byte[16];
......@@ -368,6 +379,14 @@ public class MainViewModel extends AndroidViewModel {
} catch (IOException e) {
// Expected
}
if (idleSeconds > 0) {
try {
Thread.sleep(idleSeconds * 1000);
} catch (InterruptedException e) {
Log.w(TAG, "Sleep interrupted");
return;
}
}
}
});
}
......
......@@ -17,6 +17,15 @@
android:text="@string/default_server_address"
tools:ignore="Autofill" />
<EditText
android:id="@+id/idle_period_edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/idle_period"
android:inputType="number"
android:text="@string/default_idle_period"
tools:ignore="Autofill" />
<Button
android:id="@+id/test_button"
android:layout_width="wrap_content"
......
......@@ -3,6 +3,9 @@
<string name="server_address">Server address</string>
<string name="default_server_address">139.162.237.194:40404</string>
<string name="idle_period">Idle period in seconds</string>
<string name="default_idle_period">0</string>
<string name="server_address_error">Invalid address</string>
<string name="no_bluetooth_error">Device does not have Bluetooth</string>
......
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