Fix bridge stopping mechanism.
This commit is contained in:
parent
e1895caa9d
commit
c60ec32e05
@ -45,4 +45,7 @@ public class ProgramakerAndroidBridge {
|
||||
this.bridgeRunner.run();
|
||||
}
|
||||
|
||||
public void stop() {
|
||||
bridgeRunner.stop();
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -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)
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user