aboutsummaryrefslogtreecommitdiffstats
path: root/apps/SdkController/SdkControllerLib
diff options
context:
space:
mode:
Diffstat (limited to 'apps/SdkController/SdkControllerLib')
-rwxr-xr-xapps/SdkController/SdkControllerLib/src/com/android/tools/sdkcontroller/lib/EmulatorConnection.java62
-rw-r--r--apps/SdkController/SdkControllerLib/src/com/android/tools/sdkcontroller/lib/EmulatorListener.java11
2 files changed, 40 insertions, 33 deletions
diff --git a/apps/SdkController/SdkControllerLib/src/com/android/tools/sdkcontroller/lib/EmulatorConnection.java b/apps/SdkController/SdkControllerLib/src/com/android/tools/sdkcontroller/lib/EmulatorConnection.java
index aa21ca7..f62ef44 100755
--- a/apps/SdkController/SdkControllerLib/src/com/android/tools/sdkcontroller/lib/EmulatorConnection.java
+++ b/apps/SdkController/SdkControllerLib/src/com/android/tools/sdkcontroller/lib/EmulatorConnection.java
@@ -517,23 +517,9 @@ public class EmulatorConnection {
* @param port TCP port where emulator connects.
* @param ctype Defines connection type to use (sync / async). See comments
* to EmulatorConnection class for more info.
- * @throws IOException
+ * @param listener EmulatorConnection event listener. Must not be null.
*/
- public EmulatorConnection(int port, EmulatorConnectionType ctype) throws IOException {
- constructEmulator(port, ctype);
- }
-
- /**
- * Constructs EmulatorConnection instance.
- *
- * @param port TCP port where emulator connects.
- * @param ctype Defines connection type to use (sync / async). See comments
- * to EmulatorConnection class for more info.
- * @param listener EmulatorConnection event listener.
- * @throws IOException
- */
- public EmulatorConnection(int port, EmulatorConnectionType ctype, EmulatorListener listener)
- throws IOException {
+ public EmulatorConnection(int port, EmulatorConnectionType ctype, EmulatorListener listener) {
mListener = listener;
constructEmulator(port, ctype);
}
@@ -552,31 +538,41 @@ public class EmulatorConnection {
* <p/>
* Important: Apps targeting Honeycomb+ SDK are not allowed to do networking on their main
* thread. The caller is responsible to make sure this is NOT called from a main UI thread.
+ * <p/>
+ * On error or success, this calls
+ * {@link EmulatorListener#onEmulatorBindResult(boolean, Exception)} to indicate whether
+ * the socket was properly bound.
+ * The IO loop will start after the method reported a successful bind.
*
* @param port TCP port where emulator connects.
* @param ctype Defines connection type to use (sync / async). See comments
* to EmulatorConnection class for more info.
- * @throws IOException
*/
- private void constructEmulator(final int port, EmulatorConnectionType ctype) throws IOException {
- mConnectionType = ctype;
- // Create I/O looper.
- mSelector = SelectorProvider.provider().openSelector();
-
- // Create non-blocking server socket that would listen for connections,
- // and bind it to the given port on the local host.
- mServerSocket = ServerSocketChannel.open();
- mServerSocket.configureBlocking(false);
- InetAddress local = InetAddress.getLocalHost();
- final InetSocketAddress address = new InetSocketAddress(local, port);
- mServerSocket.socket().bind(address);
-
- // Register 'accept' I/O on the server socket.
- mServerSocket.register(mSelector, SelectionKey.OP_ACCEPT);
+ private void constructEmulator(final int port, EmulatorConnectionType ctype) {
- // TODO we need a call back onBindSuccess(success or exception)
+ try {
+ mConnectionType = ctype;
+ // Create I/O looper.
+ mSelector = SelectorProvider.provider().openSelector();
+
+ // Create non-blocking server socket that would listen for connections,
+ // and bind it to the given port on the local host.
+ mServerSocket = ServerSocketChannel.open();
+ mServerSocket.configureBlocking(false);
+ InetAddress local = InetAddress.getLocalHost();
+ final InetSocketAddress address = new InetSocketAddress(local, port);
+ mServerSocket.socket().bind(address);
+
+ // Register 'accept' I/O on the server socket.
+ mServerSocket.register(mSelector, SelectionKey.OP_ACCEPT);
+ } catch (IOException e) {
+ mListener.onEmulatorBindResult(false, e);
+ return;
+ }
+ mListener.onEmulatorBindResult(true, null);
Logv("EmulatorConnection listener is created for port " + port);
+
// Start I/O looper and dispatcher.
new Thread(new Runnable() {
@Override
diff --git a/apps/SdkController/SdkControllerLib/src/com/android/tools/sdkcontroller/lib/EmulatorListener.java b/apps/SdkController/SdkControllerLib/src/com/android/tools/sdkcontroller/lib/EmulatorListener.java
index 7a5c1af..f707822 100644
--- a/apps/SdkController/SdkControllerLib/src/com/android/tools/sdkcontroller/lib/EmulatorListener.java
+++ b/apps/SdkController/SdkControllerLib/src/com/android/tools/sdkcontroller/lib/EmulatorListener.java
@@ -22,6 +22,17 @@ package com.android.tools.sdkcontroller.lib;
* setEmulatorListener method of the EmulatorConnection class.
*/
public interface EmulatorListener {
+
+ /**
+ * Called as a side effect of constructing a new {@link EmulatorConnection}
+ * when emulator is bound with its communication socket.
+ *
+ * @param success True if the socket bind was successful.
+ * @param e Any exception thrown whilst trying to bind to the communication socket.
+ * Null if there's no exception (typically when {@code success==true}).
+ */
+ public void onEmulatorBindResult(boolean success, Exception e);
+
/**
* Called when emulator is connected. NOTE: This method is called from the
* I/O loop, so all communication with the emulator will be "on hold" until