diff --git a/cmds/monkey/README.NETWORK.txt b/cmds/monkey/README.NETWORK.txt index b80cea9f7..51b56c87d 100644 --- a/cmds/monkey/README.NETWORK.txt +++ b/cmds/monkey/README.NETWORK.txt @@ -92,6 +92,14 @@ type string This command will simulate a user typing the given string on the keyboard by generating the proper KeyEvents. +quit + +Fully quit the monkey and accept no new sessions. + +done + +Close the current session and allow a new session to connect + OTHER NOTES There are some convenience features added to allow running without diff --git a/cmds/monkey/src/com/android/commands/monkey/Monkey.java b/cmds/monkey/src/com/android/commands/monkey/Monkey.java index 6b10147a6..709a70c1a 100644 --- a/cmds/monkey/src/com/android/commands/monkey/Monkey.java +++ b/cmds/monkey/src/com/android/commands/monkey/Monkey.java @@ -369,7 +369,12 @@ public class Monkey { mEventSource = new MonkeySourceScript(mScriptFileName, mThrottle); mEventSource.setVerbose(mVerbose); } else if (mServerPort != -1) { - mEventSource = new MonkeySourceNetwork(mServerPort); + try { + mEventSource = new MonkeySourceNetwork(mServerPort); + } catch (IOException e) { + System.out.println("Error binding to network socket."); + return -5; + } mCount = Integer.MAX_VALUE; } else { // random source by default diff --git a/cmds/monkey/src/com/android/commands/monkey/MonkeySourceNetwork.java b/cmds/monkey/src/com/android/commands/monkey/MonkeySourceNetwork.java index 5937f09c1..7a2bccd30 100644 --- a/cmds/monkey/src/com/android/commands/monkey/MonkeySourceNetwork.java +++ b/cmds/monkey/src/com/android/commands/monkey/MonkeySourceNetwork.java @@ -359,6 +359,8 @@ public class MonkeySourceNetwork implements MonkeyEventSource { // QUIT command private static final String QUIT = "quit"; + // DONE command + private static final String DONE = "done"; // command response strings private static final String OK = "OK"; @@ -398,13 +400,19 @@ public class MonkeySourceNetwork implements MonkeyEventSource { private final CommandQueueImpl commandQueue = new CommandQueueImpl(); - private final int port; private BufferedReader input; private PrintWriter output; private boolean started = false; - public MonkeySourceNetwork(int port) { - this.port = port; + private ServerSocket serverSocket; + private Socket clientSocket; + + public MonkeySourceNetwork(int port) throws IOException { + // Only bind this to local host. This means that you can only + // talk to the monkey locally, or though adb port forwarding. + serverSocket = new ServerSocket(port, + 0, // default backlog + InetAddress.getLocalHost()); } /** @@ -415,19 +423,24 @@ public class MonkeySourceNetwork implements MonkeyEventSource { * @param port the port to listen on */ private void startServer() throws IOException { - // Only bind this to local host. This means that you can only - // talk to the monkey locally, or though adb port forwarding. - ServerSocket server = new ServerSocket(port, - 0, // default backlog - InetAddress.getLocalHost()); - Socket s = server.accept(); + clientSocket = serverSocket.accept(); // At this point, we have a client connected. Wake the device // up in preparation for doing some commands. wake(); - input = new BufferedReader(new InputStreamReader(s.getInputStream())); + input = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); // auto-flush - output = new PrintWriter(s.getOutputStream(), true); + output = new PrintWriter(clientSocket.getOutputStream(), true); + } + + /** + * Stop the server from running so it can reconnect a new client. + */ + private void stopServer() throws IOException { + clientSocket.close(); + input.close(); + output.close(); + started = false; } /** @@ -529,8 +542,24 @@ public class MonkeySourceNetwork implements MonkeyEventSource { String command = input.readLine(); if (command == null) { Log.d(TAG, "Connection dropped."); - return null; + // Treat this exactly the same as if the user had + // ended the session cleanly with a done commant. + command = DONE; } + + if (DONE.equals(command)) { + // stop the server so it can accept new connections + try { + stopServer(); + } catch (IOException e) { + Log.e(TAG, "Got IOException shutting down!", e); + return null; + } + // return a noop event so we keep executing the main + // loop + return new MonkeyNoopEvent(); + } + // Do quit checking here if (QUIT.equals(command)) { // then we're done