From c60ec32e05ddea1c4e8f23bd3a8176a32863444d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20Mart=C3=ADnez=20Portela?= Date: Wed, 27 May 2020 19:55:28 +0200 Subject: [PATCH] Fix bridge stopping mechanism. --- .../bridge/ProgramakerAndroidBridge.java | 3 +++ .../bridge/ProgramakerBridgeService.java | 23 +++++++++++++------ .../programaker/bridge/ProgramakerBridge.kt | 9 +++++++- 3 files changed, 27 insertions(+), 8 deletions(-) diff --git a/app/src/main/java/com/codigoparallevar/minicards/bridge/ProgramakerAndroidBridge.java b/app/src/main/java/com/codigoparallevar/minicards/bridge/ProgramakerAndroidBridge.java index f401bdb..5047618 100644 --- a/app/src/main/java/com/codigoparallevar/minicards/bridge/ProgramakerAndroidBridge.java +++ b/app/src/main/java/com/codigoparallevar/minicards/bridge/ProgramakerAndroidBridge.java @@ -45,4 +45,7 @@ public class ProgramakerAndroidBridge { this.bridgeRunner.run(); } + public void stop() { + bridgeRunner.stop(); + } } diff --git a/app/src/main/java/com/codigoparallevar/minicards/bridge/ProgramakerBridgeService.java b/app/src/main/java/com/codigoparallevar/minicards/bridge/ProgramakerBridgeService.java index e91887e..94b37c8 100644 --- a/app/src/main/java/com/codigoparallevar/minicards/bridge/ProgramakerBridgeService.java +++ b/app/src/main/java/com/codigoparallevar/minicards/bridge/ProgramakerBridgeService.java @@ -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,14 +181,16 @@ public class ProgramakerBridgeService extends Service { } private void onBridgeFailedAfterConnected() { - Log.e(LogTag, "Bridge stopped after connected. Waiting 10s then restarting"); - setBridgeStatusNotification(getString(R.string.bridge_service_failed_restarting), false); - try { - Thread.sleep(WAIT_TIME_BEFORE_RESTART_MILLIS); - } catch (InterruptedException e) { - e.printStackTrace(); + if (!stopped) { + Log.e(LogTag, "Bridge stopped after connected. Waiting 10s then restarting"); + setBridgeStatusNotification(getString(R.string.bridge_service_failed_restarting), false); + try { + Thread.sleep(WAIT_TIME_BEFORE_RESTART_MILLIS); + } catch (InterruptedException e) { + e.printStackTrace(); + } + connectBridge(); } - connectBridge(); } @Override @@ -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); } } diff --git a/app/src/main/java/com/programaker/bridge/ProgramakerBridge.kt b/app/src/main/java/com/programaker/bridge/ProgramakerBridge.kt index dfd8fc1..9e094f9 100644 --- a/app/src/main/java/com/programaker/bridge/ProgramakerBridge.kt +++ b/app/src/main/java/com/programaker/bridge/ProgramakerBridge.kt @@ -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) + } } \ No newline at end of file