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

View File

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