/** * Terminates the work of the socket server. * * @throws IOException * - if closing the input or output streams fails */ public void terminateSocketServer() throws IOException { socketServer.stop(); } }
@Override public Object handle(Object[] args) { isRunning = false; if (socketServer != null) { synchronized (socketServer) { if (socketServer != null) { socketServer.terminate(); } } } return UIAutomatorRequest.VOID_RESPONSE; }
/** * Creates an object that can start a socket server on the given port in a new thread. The socket server will listen * for and respond to requests from the agent using the given request handler. * * @param requestHandler * - the {@link RequestHandler} instance that will be used for request handling * @param port * - port on which to open a socket server * @throws IOException * - if the input or output stream initialization fails */ public OnDeviceSocketServer(RequestHandler<T> requestHandler, int port) throws IOException { connectionHandler = new SocketConnectionHandler<T>(requestHandler, port); }
/** * 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); } } }
@Override public void run() { isRunning = true; while (isRunning) { try { LOGGER.info("Waiting for connection..."); socketServer.acceptConnection(); LOGGER.info("Connection accepted, receiving request..."); Request<T> request = socketServer.handle(); LOGGER.info("Handled request '" + request.getType() + "'."); } catch (SocketException se) { LOGGER.error("Error in connection.", se); } catch (IOException ioe) { LOGGER.error("Error in I/O.", ioe); } catch (ClassNotFoundException e) { LOGGER.error("Unsupported request sent.", e); } // Closing the connection in a separate block try { socketServer.endConnection(); LOGGER.info("Connection closed."); } catch (IOException e) { LOGGER.error("Error in I/O while closing connection.", e); } } }
/** * Handles the next request sent by the client using the {@link RequestHandler} that was specified when this class * was created. * * @return the {@link Request} that was handled. * @throws IOException * if an I/O exception of some sort has occurred. * @throws ClassNotFoundException * thrown when an application fails to load in a class. */ @SuppressWarnings("unchecked") public Request<T> handle() throws IOException, ClassNotFoundException { Request<T> request = (Request<T>) receiveObject(); Object response = requestHandler.handle(request); sendObject(response); return request; } }
/** * 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 } } }
/** * Creates a socket server on the given port. The socket server receives and responds to agent requests using the * request handler. * * @param requestHandler * - the {@link RequestHandler} instance that will be used for request handling * @param port * - port on which to open a socket server * @throws IOException * - if the input or output stream initialization fails */ public SocketConnectionHandler(RequestHandler<T> requestHandler, int port) throws IOException { socketServer = new DeviceSocketServer<T>(requestHandler, port); isRunning = false; }
/** * Checks whether the network thread is initialized and running. * * @return <code>true</code> if the network thread is running, <code>false</code> otherwise */ public boolean isNetworkThreadRunning() { boolean isSocketThreadInitialized = socketServerThread != null && socketServerThread.isAlive(); boolean isSocketServerRunning = connectionHandler != null && connectionHandler.isRunning(); return isSocketThreadInitialized && isSocketServerRunning; }
/** * Closes client connection and destroys all resources, allocated by the socket server. * * @throws IOException * if an I/O exception has occurred. */ public void stop() throws IOException { endConnection(); if (serverSocket != null) { serverSocket.close(); } } }
/** * 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."); } }