diff options
| -rw-r--r-- | services/java/com/android/server/NativeDaemonConnector.java | 59 | ||||
| -rw-r--r-- | services/java/com/android/server/NativeDaemonConnectorException.java | 42 |
2 files changed, 70 insertions, 31 deletions
diff --git a/services/java/com/android/server/NativeDaemonConnector.java b/services/java/com/android/server/NativeDaemonConnector.java index 92ba5f80d554..016aa52ac7a6 100644 --- a/services/java/com/android/server/NativeDaemonConnector.java +++ b/services/java/com/android/server/NativeDaemonConnector.java @@ -28,7 +28,6 @@ import java.io.IOException; 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 @@ final class NativeDaemonConnector implements Runnable { 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 @@ final class NativeDaemonConnector implements Runnable { } } 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(); + try { + if (socket != null) { + socket.close(); + } + } catch (IOException ex) { + Log.w(TAG, "Failed closing socket", ex); } - } 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 @@ final class NativeDaemonConnector implements Runnable { /** * 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 @@ final class NativeDaemonConnector implements Runnable { 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 @@ final class NativeDaemonConnector implements Runnable { 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 @@ final class NativeDaemonConnector implements Runnable { * 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 @@ final class NativeDaemonConnector implements Runnable { } 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 000000000000..e60aaf86858d --- /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; + } +} |