Squashed commit of the following:

commit d36aba1463cf8dca9dea7b4e436a71c6821f341a
Author: Bill Napier <napier@google.com>
Date:   Tue Aug 11 16:36:07 2009 -0700

    Change disconnect to be the same as if the user had issued a done command.

commit 4ab3ef149e815188105e10febd5a00ee5d0574fc
Author: Bill Napier <napier@google.com>
Date:   Tue Aug 11 16:31:16 2009 -0700

    Add "done" command to allow multiple sessions.
This commit is contained in:
Bill Napier
2009-08-11 16:36:43 -07:00
parent 4bff8ab0e9
commit 9d8557c4b3
3 changed files with 55 additions and 13 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -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