Skip to content
Snippets Groups Projects
Commit 1e4dd3c1 authored by akwizgran's avatar akwizgran
Browse files

Add optional idle period between discovery/connection attempts.

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