Fix bridge stopping mechanism.

This commit is contained in:
Sergio Martínez Portela 2020-05-27 19:55:28 +02:00
parent e1895caa9d
commit c60ec32e05
3 changed files with 27 additions and 8 deletions

View File

@ -45,4 +45,7 @@ public class ProgramakerAndroidBridge {
this.bridgeRunner.run();
}
public void stop() {
bridgeRunner.stop();
}
}

View File

@ -35,6 +35,7 @@ public class ProgramakerBridgeService extends Service {
private static final long WAIT_TIME_BEFORE_RESTART_MILLIS = 10000; // 10s
private ProgramakerAndroidBridge bridge = null;
private static final String LogTag = "PM BridgeService";
private boolean stopped = false;
@Override
public void onCreate() {
@ -107,6 +108,7 @@ public class ProgramakerBridgeService extends Service {
}
private void connectBridge() {
stopped = false;
ConfigManager config = new ConfigManager(this);
String token = config.getToken();
@ -179,6 +181,7 @@ public class ProgramakerBridgeService extends Service {
}
private void onBridgeFailedAfterConnected() {
if (!stopped) {
Log.e(LogTag, "Bridge stopped after connected. Waiting 10s then restarting");
setBridgeStatusNotification(getString(R.string.bridge_service_failed_restarting), false);
try {
@ -188,6 +191,7 @@ public class ProgramakerBridgeService extends Service {
}
connectBridge();
}
}
@Override
public IBinder onBind(Intent intent) {
@ -197,6 +201,11 @@ public class ProgramakerBridgeService extends Service {
@Override
public void onDestroy() {
stopped = true;
ProgramakerAndroidBridge bridge = this.bridge;
if (bridge != null) {
bridge.stop();
}
setBridgeStatusNotification(getString(R.string.bridge_service_failed_stopping), true);
}
}

View File

@ -17,6 +17,7 @@ class ProgramakerBridge(
private val onReady: Runnable,
private val onComplete: Runnable
) : WebSocketListener() {
private var webSocket: WebSocket? = null
private val utf8: Charset = Charset.forName("UTF-8")
private val gson = Gson()
@ -46,6 +47,7 @@ class ProgramakerBridge(
// Websocket management
override fun onOpen(webSocket: WebSocket, response: Response) {
this.webSocket = webSocket
webSocket.send(config.serialize())
onReady.run()
}
@ -77,7 +79,8 @@ class ProgramakerBridge(
}
override fun onFailure(webSocket: WebSocket, t: Throwable, response: Response?) {
Log.e(LogTag, t.toString(), t)
Log.e(LogTag, "Error: $t", t)
webSocket.close(1000, null)
}
// Protocol handling
@ -166,4 +169,8 @@ class ProgramakerBridge(
)
this.config.configManager.bridgeConnectionId = userId
}
fun stop() {
webSocket?.close(1000, null)
}
}