Refactor out a tf-remote-client jar.

Done to allow other tools to remotely communicate with a TF instance.

Bug 10919026

Change-Id: I510d8e54c2f915baaf7e707a6dafbe38ac2ee106
diff --git a/.classpath b/.classpath
index ae5e1f9..85476e0 100644
--- a/.classpath
+++ b/.classpath
@@ -9,5 +9,6 @@
 	<classpathentry exported="true" kind="var" path="TRADEFED_ROOT/out/host/common/obj/JAVA_LIBRARIES/junit_intermediates/javalib.jar" sourcepath="/TRADEFED_ROOT/external/junit/src"/>
 	<classpathentry exported="true" kind="var" path="TRADEFED_ROOT/prebuilts/misc/common/json/json-prebuilt.jar"/>
 	<classpathentry combineaccessrules="false" kind="src" path="/ddmlib"/>
+	<classpathentry combineaccessrules="false" kind="src" path="/tf-remote-client"/>
 	<classpathentry kind="output" path="bin"/>
 </classpath>
diff --git a/Android.mk b/Android.mk
index eedf707..63a3e83 100644
--- a/Android.mk
+++ b/Android.mk
@@ -29,7 +29,7 @@
 LOCAL_STATIC_JAVA_LIBRARIES := junit kxml2-2.3.0 guavalib jline-1.0 json-prebuilt
 # emmalib is only a runtime dependency if generating code coverage reporters,
 # not a compile time dependency
-LOCAL_JAVA_LIBRARIES := ddmlib-prebuilt emmalib tools-common-prebuilt
+LOCAL_JAVA_LIBRARIES := ddmlib-prebuilt emmalib tools-common-prebuilt tf-remote-client
 
 include $(BUILD_HOST_JAVA_LIBRARY)
 
@@ -85,7 +85,7 @@
 # Zip up the built files and dist it as google-tradefed.zip
 ifneq (,$(filter tradefed, $(TARGET_BUILD_APPS)))
 
-tradefed_dist_host_jars := tradefed tradefed-tests ddmlib-prebuilt tf-prod-tests emmalib loganalysis loganalysis-tests
+tradefed_dist_host_jars := tradefed tradefed-tests ddmlib-prebuilt tf-prod-tests emmalib loganalysis loganalysis-tests tf-remote-client
 tradefed_dist_host_jar_files := $(foreach m, $(tradefed_dist_host_jars), $(HOST_OUT_JAVA_LIBRARIES)/$(m).jar)
 
 tradefed_dist_host_exes := tradefed.sh
diff --git a/remote/.classpath b/remote/.classpath
new file mode 100644
index 0000000..72f62ef
--- /dev/null
+++ b/remote/.classpath
@@ -0,0 +1,8 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<classpath>
+	<classpathentry kind="src" path="src"/>
+	<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
+	<classpathentry exported="true" kind="var" path="TRADEFED_ROOT/prebuilts/misc/common/json/json-prebuilt.jar"/>
+	<classpathentry combineaccessrules="false" kind="src" path="/ddmlib"/>
+	<classpathentry kind="output" path="bin"/>
+</classpath>
diff --git a/remote/.project b/remote/.project
new file mode 100644
index 0000000..39f7e18
--- /dev/null
+++ b/remote/.project
@@ -0,0 +1,17 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<projectDescription>
+	<name>tf-remote-client</name>
+	<comment></comment>
+	<projects>
+	</projects>
+	<buildSpec>
+		<buildCommand>
+			<name>org.eclipse.jdt.core.javabuilder</name>
+			<arguments>
+			</arguments>
+		</buildCommand>
+	</buildSpec>
+	<natures>
+		<nature>org.eclipse.jdt.core.javanature</nature>
+	</natures>
+</projectDescription>
diff --git a/remote/Android.mk b/remote/Android.mk
new file mode 100644
index 0000000..67509e6
--- /dev/null
+++ b/remote/Android.mk
@@ -0,0 +1,39 @@
+# Copyright (C) 2013 The Android Open Source Project
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+LOCAL_PATH := $(call my-dir)
+
+include $(CLEAR_VARS)
+
+# Only compile source java files in this lib.
+LOCAL_SRC_FILES := $(call all-java-files-under, src)
+
+LOCAL_JAVACFLAGS += -g -Xlint
+
+LOCAL_MODULE := tf-remote-client
+
+LOCAL_MODULE_TAGS := optional
+# only depend on ddmlib for the Log class
+LOCAL_JAVA_LIBRARIES := json-prebuilt ddmlib-prebuilt
+
+include $(BUILD_HOST_JAVA_LIBRARY)
+
+# makefile rules to copy jars to HOST_OUT/tradefed
+# so tradefed.sh can automatically add to classpath
+DEST_JAR := $(HOST_OUT)/tradefed/$(LOCAL_MODULE).jar
+$(DEST_JAR): $(LOCAL_BUILT_MODULE)
+	$(copy-file-to-new-target)
+
+# this dependency ensure the above rule will be executed if jar is built
+$(LOCAL_INSTALLED_MODULE) : $(DEST_JAR)
diff --git a/src/com/android/tradefed/command/remote/AddCommandOp.java b/remote/src/com/android/tradefed/command/remote/AddCommandOp.java
similarity index 100%
rename from src/com/android/tradefed/command/remote/AddCommandOp.java
rename to remote/src/com/android/tradefed/command/remote/AddCommandOp.java
diff --git a/src/com/android/tradefed/command/remote/AllocateDeviceOp.java b/remote/src/com/android/tradefed/command/remote/AllocateDeviceOp.java
similarity index 100%
rename from src/com/android/tradefed/command/remote/AllocateDeviceOp.java
rename to remote/src/com/android/tradefed/command/remote/AllocateDeviceOp.java
diff --git a/src/com/android/tradefed/command/remote/CloseOp.java b/remote/src/com/android/tradefed/command/remote/CloseOp.java
similarity index 100%
rename from src/com/android/tradefed/command/remote/CloseOp.java
rename to remote/src/com/android/tradefed/command/remote/CloseOp.java
diff --git a/src/com/android/tradefed/command/remote/FreeDeviceOp.java b/remote/src/com/android/tradefed/command/remote/FreeDeviceOp.java
similarity index 100%
rename from src/com/android/tradefed/command/remote/FreeDeviceOp.java
rename to remote/src/com/android/tradefed/command/remote/FreeDeviceOp.java
diff --git a/src/com/android/tradefed/command/remote/RemoteClient.java b/remote/src/com/android/tradefed/command/remote/RemoteClient.java
similarity index 80%
rename from src/com/android/tradefed/command/remote/RemoteClient.java
rename to remote/src/com/android/tradefed/command/remote/RemoteClient.java
index 62fdab1..e61e3f3 100644
--- a/src/com/android/tradefed/command/remote/RemoteClient.java
+++ b/remote/src/com/android/tradefed/command/remote/RemoteClient.java
@@ -15,9 +15,8 @@
  */
 package com.android.tradefed.command.remote;
 
+import com.android.ddmlib.Log;
 import com.android.tradefed.command.remote.RemoteOperation.RemoteException;
-import com.android.tradefed.log.LogUtil.CLog;
-import com.android.tradefed.util.StreamUtil;
 
 import java.io.BufferedReader;
 import java.io.IOException;
@@ -34,6 +33,7 @@
  */
 public class RemoteClient {
 
+    private static final String TAG = RemoteClient.class.getSimpleName();
     private final Socket mSocket;
     private final PrintWriter mWriter;
     private final BufferedReader mReader;
@@ -47,7 +47,18 @@
      * @throws UnknownHostException
      */
     RemoteClient(int port) throws UnknownHostException, IOException {
-        String hostName = InetAddress.getLocalHost().getHostName();
+        this(InetAddress.getLocalHost().getHostName(), port);
+    }
+
+    /**
+     * Initialize the {@RemoteClient}, and instruct it to connect to the given hostname and port.
+     *
+     * @param hostname to connect to
+     * @param port the tcp/ip port number
+     * @throws IOException
+     * @throws UnknownHostException
+     */
+    RemoteClient(String hostName, int port) throws UnknownHostException, IOException {
         mSocket = new Socket(hostName, port);
         mWriter = new PrintWriter(mSocket.getOutputStream(), true);
         mReader = new BufferedReader(new InputStreamReader(mSocket.getInputStream()));
@@ -65,9 +76,12 @@
            String response = mReader.readLine();
            return response != null && Boolean.parseBoolean(response);
        } catch (RemoteException e) {
-           CLog.e("Failed to send remote commmand", e);
+           // TODO: convert to CLog once we have tf-common
+          Log.e(TAG, "Failed to send remote commmand");
+          Log.e(TAG, e);
        } catch (IOException e) {
-           CLog.e("Failed to send remote commmand", e);
+           Log.e(TAG, "Failed to send remote commmand");
+           Log.e(TAG, e);
        }
        return false;
     }
@@ -129,10 +143,12 @@
              try {
                 mSocket.close();
             } catch (IOException e) {
-                // ignore
+                Log.w(TAG, String.format("exception closing socket: %s", e.toString()));
             }
         }
-        StreamUtil.close(mWriter);
+        if (mWriter != null) {
+            mWriter.close();
+        }
     }
 }
 
diff --git a/src/com/android/tradefed/command/remote/RemoteOperation.java b/remote/src/com/android/tradefed/command/remote/RemoteOperation.java
similarity index 94%
rename from src/com/android/tradefed/command/remote/RemoteOperation.java
rename to remote/src/com/android/tradefed/command/remote/RemoteOperation.java
index 91f70f9..5306895 100644
--- a/src/com/android/tradefed/command/remote/RemoteOperation.java
+++ b/remote/src/com/android/tradefed/command/remote/RemoteOperation.java
@@ -15,7 +15,7 @@
  */
 package com.android.tradefed.command.remote;
 
-import com.android.tradefed.log.LogUtil.CLog;
+import com.android.ddmlib.Log;
 
 import org.json.JSONException;
 import org.json.JSONObject;
@@ -27,6 +27,7 @@
     private static final String TYPE = "type";
     private static final String VERSION = "version";
     static final int CURRENT_PROTOCOL_VERSION = 1;
+    private static final String TAG = RemoteOperation.class.getSimpleName();
 
     @SuppressWarnings("serial")
     static class RemoteException extends Exception {
@@ -109,7 +110,8 @@
              j.put(TYPE, getType().toString());
              packIntoJson(j);
          } catch (JSONException e) {
-             CLog.e("Failed to serialize RemoteOperation", e);
+             Log.e(TAG, "Failed to serialize RemoteOperation");
+             Log.e(TAG,  e);
          }
          return j.toString();
      }
diff --git a/tests/.classpath b/tests/.classpath
index ec3e475..de90d61 100644
--- a/tests/.classpath
+++ b/tests/.classpath
@@ -7,5 +7,6 @@
 	<classpathentry kind="src" path="res"/>
 	<classpathentry exported="true" kind="var" path="TRADEFED_ROOT/out/host/common/obj/JAVA_LIBRARIES/easymock_intermediates/javalib.jar" sourcepath="/TRADEFED_ROOT/external/easymock/src"/>
 	<classpathentry combineaccessrules="false" kind="src" path="/ddmlib"/>
+	<classpathentry combineaccessrules="false" kind="src" path="/tf-remote-client"/>
 	<classpathentry kind="output" path="bin"/>
 </classpath>
diff --git a/tests/Android.mk b/tests/Android.mk
index 31f1748..e39cc31 100644
--- a/tests/Android.mk
+++ b/tests/Android.mk
@@ -26,7 +26,7 @@
 LOCAL_MODULE := tradefed-tests
 LOCAL_MODULE_TAGS := optional
 LOCAL_STATIC_JAVA_LIBRARIES := easymock
-LOCAL_JAVA_LIBRARIES := tradefed ddmlib-prebuilt
+LOCAL_JAVA_LIBRARIES := tradefed ddmlib-prebuilt tf-remote-client
 
 include $(BUILD_HOST_JAVA_LIBRARY)