From 0729d7a114af89559259aedc9ed0d7da49f45f3b Mon Sep 17 00:00:00 2001 From: Johannes Mockenhaupt Date: Fri, 14 Jul 2017 20:24:32 +0200 Subject: [PATCH] RuffyScripter: Abort a running command after 90s timeout --- .../jotomo/ruffyscripter/RuffyScripter.java | 23 +++++++++++++++---- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/de/jotomo/ruffyscripter/RuffyScripter.java b/app/src/main/java/de/jotomo/ruffyscripter/RuffyScripter.java index 11c782b5b7..175536172a 100644 --- a/app/src/main/java/de/jotomo/ruffyscripter/RuffyScripter.java +++ b/app/src/main/java/de/jotomo/ruffyscripter/RuffyScripter.java @@ -113,6 +113,9 @@ public class RuffyScripter { } ensureConnected(); + // TODO reuse thread, scheduler ... + Thread cmdThread; + // TODO make this a safe lock synchronized (this) { cmdResult = null; @@ -122,7 +125,7 @@ public class RuffyScripter { // wait till pump is ready for input waitForMenuUpdate(); log.debug("Cmd execution: connection ready, executing cmd " + cmd); - new Thread(new Runnable() { + cmdThread = new Thread(new Runnable() { @Override public void run() { try { @@ -134,16 +137,25 @@ public class RuffyScripter { } } - }).start(); + }); + cmdThread.start(); } // TODO really? + long timeout = System.currentTimeMillis() + 90 * 1000; while (activeCmd != null) { SystemClock.sleep(500); log.trace("Waiting for running command to complete"); + if (System.currentTimeMillis() > timeout) { + log.error("Running command " + activeCmd + " timed out"); + cmdThread.interrupt(); + activeCmd = null; + cmdResult = null; + return new CommandResult().success(false).enacted(false).message("Command timed out"); + } } - log.debug("Command result: " + cmdResult); + log.debug("Command result: " + cmdResult); CommandResult r = cmdResult; cmdResult = null; return r; @@ -162,10 +174,11 @@ public class RuffyScripter { } try { - boolean connectSuccesful = ruffyService.doRTConnect() == 0; - log.debug("Connect init successful: " + connectSuccesful); + boolean connectInitSuccessful = ruffyService.doRTConnect() == 0; + log.debug("Connect init successful: " + connectInitSuccessful); while (currentMenu == null) { log.debug("Waiting for first menu update to be sent"); + // waitForMenuUpdate times out after 90s and throws a CommandException waitForMenuUpdate(); } } catch (RemoteException e) {