/** * Initializes and starts the network thread. * * @throws IOException * - if network thread initialization fails */ public void start() throws IOException { if (!isNetworkThreadRunning()) { socketServerThread = new Thread(connectionHandler); socketServerThread.start(); LOGGER.info("Started network thread."); } else { LOGGER.error("Network thread start requested, but it's already running."); } }
@Override public Object handle(Object[] args) { isRunning = false; if (socketServer != null) { synchronized (socketServer) { if (socketServer != null) { socketServer.terminate(); } } } return UIAutomatorRequest.VOID_RESPONSE; }
/** * Main entry point being called when the UI automator process is started. */ public void testRun() { Log4JConfigurator.configure(); AccessibilityHelper accessibilityHelper = AccessibilityFactory.getAccessibilityHelper(); accessibilityHelper.initializeAccessibilityEventListener(); try { socketServer = new OnDeviceSocketServer<UIAutomatorRequest>(this, ConnectionConstants.UI_AUTOMATOR_PORT); socketServer.start(); isRunning = true; LOGGER.info("Automator socket server started successfully."); } catch (IOException e) { LOGGER.error("Could not start ATMOSPHERE socket server", e); } while (isRunning) { try { Thread.sleep(SLEEP_TIEMOUT); } catch (InterruptedException e) { // Nothing to do here } } }
/** * Terminates the work of the socket server. */ public void terminate() { if (!isNetworkThreadRunning()) { LOGGER.warn("Could not terminate network thread. Network thread is not running."); return; } LOGGER.info("Stopping socket server..."); connectionHandler.terminate(); try { socketServerThread.join(SOCKET_THREAD_KILL_TIMEOUT); LOGGER.debug("Network thread should be stopped now..."); } catch (InterruptedException e) { LOGGER.debug("Error stopping network thread...", e); } try { connectionHandler.terminateSocketServer(); LOGGER.info("Socket connection stopped successfully!"); } catch (IOException e) { LOGGER.error("Could not close opened connection.", e); } } }