NativeDaemonConnector: Cleanup socket code and use a proper exception
Signed-off-by: San Mehat <san@google.com>
diff --git a/services/java/com/android/server/NativeDaemonConnector.java b/services/java/com/android/server/NativeDaemonConnector.java
index 92ba5f8..016aa52 100644
--- a/services/java/com/android/server/NativeDaemonConnector.java
+++ b/services/java/com/android/server/NativeDaemonConnector.java
@@ -28,7 +28,6 @@
 import java.io.InputStream;
 import java.io.OutputStream;
 import java.net.Socket;
-import java.lang.IllegalStateException;
 
 import java.util.List;
 import java.util.ArrayList;
@@ -82,12 +81,12 @@
                 listenToSocket();
             } catch (Exception e) {
                 Log.e(TAG, "Error in NativeDaemonConnector", e);
-                SystemClock.sleep(1000);
+                SystemClock.sleep(5000);
             }
         }
     }
 
-    private void listenToSocket() {
+    private void listenToSocket() throws IOException {
        LocalSocket socket = null;
 
         try {
@@ -143,31 +142,27 @@
             }
         } catch (IOException ex) {
             Log.e(TAG, "Communications error", ex);
-        }
-
-        synchronized (this) {
-            if (mOutputStream != null) {
-                try {
-                    mOutputStream.close();
-                } catch (IOException e) {
-                    Log.w(TAG, "Failed closing output stream", e);
+            throw ex;
+        } finally {
+            synchronized (this) {
+                if (mOutputStream != null) {
+                    try {
+                        mOutputStream.close();
+                    } catch (IOException e) {
+                        Log.w(TAG, "Failed closing output stream", e);
+                    }
+                    mOutputStream = null;
                 }
+            }
 
-                mOutputStream = null;
+            try {
+                if (socket != null) {
+                    socket.close();
+                }
+            } catch (IOException ex) {
+                Log.w(TAG, "Failed closing socket", ex);
             }
         }
-
-        try {
-            if (socket != null) {
-                socket.close();
-            }
-        } catch (IOException ex) {
-            Log.w(TAG, "Failed closing socket", ex);
-        }
-
-        Log.e(TAG, "Failed to connect to native daemon",
-                new IllegalStateException());
-        SystemClock.sleep(5000);
     }
 
     private void sendCommand(String command) {
@@ -204,7 +199,8 @@
     /**
      * Issue a command to the native daemon and return the responses
      */
-    public synchronized ArrayList<String> doCommand(String cmd) throws IllegalStateException {
+    public synchronized ArrayList<String> doCommand(String cmd)
+            throws NativeDaemonConnectorException  {
         sendCommand(cmd);
 
         ArrayList<String> response = new ArrayList<String>();
@@ -214,12 +210,12 @@
         while (!complete) {
             try {
                 String line = mResponseQueue.take();
-//                Log.d(TAG, "Removed off queue -> " + line);
+                Log.d(TAG, String.format("RSP -> {%s}", line));
                 String[] tokens = line.split(" ");
                 try {
                     code = Integer.parseInt(tokens[0]);
                 } catch (NumberFormatException nfe) {
-                    throw new IllegalStateException(
+                    throw new NativeDaemonConnectorException(
                             String.format("Invalid response from daemon (%s)", line));
                 }
 
@@ -233,7 +229,7 @@
 
         if (code >= ResponseCode.FailedRangeStart &&
                 code <= ResponseCode.FailedRangeEnd) {
-            throw new IllegalStateException(String.format(
+            throw new NativeDaemonConnectorException(code, String.format(
                                                "Command %s failed with code %d",
                                                 cmd, code));
         }
@@ -244,7 +240,7 @@
      * Issues a list command and returns the cooked list
      */
     public String[] doListCommand(String cmd, int expectedResponseCode)
-            throws IllegalStateException {
+            throws NativeDaemonConnectorException {
 
         ArrayList<String> rsp = doCommand(cmd);
         String[] rdata = new String[rsp.size()-1];
@@ -259,14 +255,15 @@
                 } else if (code == NativeDaemonConnector.ResponseCode.CommandOkay) {
                     return rdata;
                 } else {
-                    throw new IllegalStateException(
+                    throw new NativeDaemonConnectorException(
                             String.format("Expected list response %d, but got %d",
                                     expectedResponseCode, code));
                 }
             } catch (NumberFormatException nfe) {
-                throw new IllegalStateException(String.format("Error reading code '%s'", line));
+                throw new NativeDaemonConnectorException(
+                        String.format("Error reading code '%s'", line));
             }
         }
-        throw new IllegalStateException("Got an empty response");
+        throw new NativeDaemonConnectorException("Got an empty response");
     }
 }
diff --git a/services/java/com/android/server/NativeDaemonConnectorException.java b/services/java/com/android/server/NativeDaemonConnectorException.java
new file mode 100644
index 0000000..e60aaf8
--- /dev/null
+++ b/services/java/com/android/server/NativeDaemonConnectorException.java
@@ -0,0 +1,42 @@
+/*
+ * Copyright (C) 2006 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.
+ */
+
+package com.android.server;
+
+/**
+ * An exception that indicates there was an error with a NativeDaemonConnector operation
+ */
+public class NativeDaemonConnectorException extends RuntimeException
+{
+    private int mCode = -1;
+
+    public NativeDaemonConnectorException() {}
+
+    public NativeDaemonConnectorException(String error)
+    {
+        super(error);
+    }
+
+    public NativeDaemonConnectorException(int code, String error)
+    {
+        super(error);
+        mCode = code;
+    }
+
+    public int getCode() {
+        return mCode;
+    }
+}