Skip to content
Snippets Groups Projects
Verified Commit de41836e authored by Torsten Grote's avatar Torsten Grote
Browse files

Move URL retrieval to WebServerManager

and add some thread annotations
parent f394fcf3
No related branches found
No related tags found
1 merge request!6ViewModel cleanup
......@@ -151,7 +151,7 @@ public class HotspotFragment extends Fragment {
public void onButtonClick(View view) {
if (hotspotStarted) {
button.setEnabled(false);
viewModel.getHotspotManager().stopWifiP2pHotspot();
viewModel.stopWifiP2pHotspot();
} else {
conditionManager.startConditionChecks();
}
......
......@@ -14,6 +14,7 @@ import java.util.logging.Logger;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.annotation.UiThread;
import static android.content.Context.WIFI_P2P_SERVICE;
import static android.content.Context.WIFI_SERVICE;
......@@ -75,6 +76,7 @@ class HotspotManager implements ActionListener {
lockTag = ctx.getPackageName() + ":app-sharing-hotspot";
}
@UiThread
void startWifiP2pHotspot() {
if (wifiP2pManager == null) {
listener.onHotspotError(ctx.getString(R.string.no_wifi_direct));
......
......@@ -8,23 +8,21 @@ import org.briarproject.hotspot.HotspotState.HotspotStarted;
import org.briarproject.hotspot.HotspotState.HotspotStopped;
import org.briarproject.hotspot.HotspotState.NetworkConfig;
import org.briarproject.hotspot.HotspotState.StartingHotspot;
import org.briarproject.hotspot.WebServerManager.WebServerState;
import java.net.InetAddress;
import java.util.logging.Logger;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.UiThread;
import androidx.annotation.WorkerThread;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import static android.content.Context.WIFI_SERVICE;
import static android.os.Build.VERSION.SDK_INT;
import static java.util.logging.Level.INFO;
import static java.util.logging.Logger.getLogger;
import static org.briarproject.hotspot.HotspotManager.HotspotListener;
import static org.briarproject.hotspot.NetworkUtils.getAccessPointAddress;
import static org.briarproject.hotspot.WebServerManager.WebServerListener;
public class MainViewModel extends AndroidViewModel
......@@ -63,19 +61,21 @@ public class MainViewModel extends AndroidViewModel
return is5GhzSupported;
}
HotspotManager getHotspotManager() {
return hotspotManager;
}
@UiThread
void startWifiP2pHotspot() {
hotspotManager.startWifiP2pHotspot();
}
@UiThread
void stopWifiP2pHotspot() {
// stop the webserver before the hotspot
webServerManager.stopWebServer();
hotspotManager.stopWifiP2pHotspot();
}
@Override
protected void onCleared() {
// TODO: remove from managers?
hotspotManager.stopWifiP2pHotspot();
webServerManager.stopWebServer();
stopWifiP2pHotspot();
}
@Override
......@@ -86,12 +86,13 @@ public class MainViewModel extends AndroidViewModel
@Nullable
// Field to temporarily store the network config received via onHotspotStarted()
// in order to post it along with a HotspotStarted status
private NetworkConfig networkConfig;
private volatile NetworkConfig networkConfig;
@Override
public void onHotspotStarted(NetworkConfig networkConfig) {
this.networkConfig = networkConfig;
LOG.info("starting webserver");
// TODO: offload this to the IoExecutor
webServerManager.startWebServer();
}
......@@ -108,32 +109,17 @@ public class MainViewModel extends AndroidViewModel
}
@Override
public void onWebServerStateChanged(WebServerState webServerStatus) {
switch (webServerStatus) {
case STARTED:
onWebServerStarted();
break;
case ERROR:
status.postValue(new HotspotError(
getApplication().getString(R.string.web_server_error)));
break;
}
}
private void onWebServerStarted() {
String url = "http://192.168.49.1:9999";
InetAddress address = getAccessPointAddress();
if (address == null) {
LOG.info(
"Could not find access point address, assuming 192.168.49.1");
} else {
if (LOG.isLoggable(INFO)) {
LOG.info("Access point address " + address.getHostAddress());
}
url = "http://" + address.getHostAddress() + ":9999";
}
@WorkerThread
public void onWebServerStarted(String url) {
status.postValue(new HotspotStarted(networkConfig, url));
networkConfig = null;
}
@Override
@WorkerThread
public void onWebServerError() {
status.postValue(new HotspotError(
getApplication().getString(R.string.web_server_error)));
}
}
......@@ -14,10 +14,12 @@ import static fi.iki.elonen.NanoHTTPD.Response.Status.OK;
public class WebServer extends NanoHTTPD {
final static int PORT = 9999;
private final Context ctx;
public WebServer(Context ctx) {
super(9999);
super(PORT);
this.ctx = ctx;
}
......
......@@ -3,22 +3,26 @@ package org.briarproject.hotspot;
import android.content.Context;
import java.io.IOException;
import java.net.InetAddress;
import java.util.logging.Logger;
import androidx.annotation.WorkerThread;
import static java.util.logging.Level.INFO;
import static java.util.logging.Level.WARNING;
import static java.util.logging.Logger.getLogger;
import static org.briarproject.hotspot.LogUtils.logException;
import static org.briarproject.hotspot.WebServerManager.WebServerState.ERROR;
import static org.briarproject.hotspot.WebServerManager.WebServerState.STARTED;
import static org.briarproject.hotspot.NetworkUtils.getAccessPointAddress;
import static org.briarproject.hotspot.WebServer.PORT;
class WebServerManager {
enum WebServerState {STARTED, ERROR}
interface WebServerListener {
@WorkerThread
void onWebServerStarted(String url);
void onWebServerStateChanged(WebServerState status);
@WorkerThread
void onWebServerError();
}
private static final Logger LOG =
......@@ -32,17 +36,30 @@ class WebServerManager {
webServer = new WebServer(ctx);
}
@WorkerThread
void startWebServer() {
// TODO: offload this to the IoExecutor
new Thread(() -> {
try {
webServer.start();
} catch (IOException e) {
logException(LOG, WARNING, e);
listener.onWebServerStateChanged(ERROR);
try {
webServer.start();
onWebServerStarted();
} catch (IOException e) {
logException(LOG, WARNING, e);
listener.onWebServerError();
}
}
private void onWebServerStarted() {
String url = "http://192.168.49.1:" + PORT;
InetAddress address = getAccessPointAddress();
if (address == null) {
LOG.info(
"Could not find access point address, assuming 192.168.49.1");
} else {
if (LOG.isLoggable(INFO)) {
LOG.info("Access point address " + address.getHostAddress());
}
listener.onWebServerStateChanged(STARTED);
}).start();
url = "http://" + address.getHostAddress() + ":" + PORT;
}
listener.onWebServerStarted(url);
}
void stopWebServer() {
......
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