NativeDaemonConnector: Cleanup socket code and use a proper exception

Signed-off-by: San Mehat <san@google.com>
This commit is contained in:
San Mehat
2010-01-29 05:22:17 -08:00
parent f2b0adabbd
commit 0e0980b533
2 changed files with 71 additions and 32 deletions

View File

@@ -28,7 +28,6 @@ import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.net.Socket; import java.net.Socket;
import java.lang.IllegalStateException;
import java.util.List; import java.util.List;
import java.util.ArrayList; import java.util.ArrayList;
@@ -82,12 +81,12 @@ final class NativeDaemonConnector implements Runnable {
listenToSocket(); listenToSocket();
} catch (Exception e) { } catch (Exception e) {
Log.e(TAG, "Error in NativeDaemonConnector", 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; LocalSocket socket = null;
try { try {
@@ -143,8 +142,8 @@ final class NativeDaemonConnector implements Runnable {
} }
} catch (IOException ex) { } catch (IOException ex) {
Log.e(TAG, "Communications error", ex); Log.e(TAG, "Communications error", ex);
} throw ex;
} finally {
synchronized (this) { synchronized (this) {
if (mOutputStream != null) { if (mOutputStream != null) {
try { try {
@@ -152,7 +151,6 @@ final class NativeDaemonConnector implements Runnable {
} catch (IOException e) { } catch (IOException e) {
Log.w(TAG, "Failed closing output stream", e); Log.w(TAG, "Failed closing output stream", e);
} }
mOutputStream = null; mOutputStream = null;
} }
} }
@@ -164,10 +162,7 @@ final class NativeDaemonConnector implements Runnable {
} catch (IOException ex) { } catch (IOException ex) {
Log.w(TAG, "Failed closing socket", 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) { 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 * 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); sendCommand(cmd);
ArrayList<String> response = new ArrayList<String>(); ArrayList<String> response = new ArrayList<String>();
@@ -214,12 +210,12 @@ final class NativeDaemonConnector implements Runnable {
while (!complete) { while (!complete) {
try { try {
String line = mResponseQueue.take(); String line = mResponseQueue.take();
// Log.d(TAG, "Removed off queue -> " + line); Log.d(TAG, String.format("RSP -> {%s}", line));
String[] tokens = line.split(" "); String[] tokens = line.split(" ");
try { try {
code = Integer.parseInt(tokens[0]); code = Integer.parseInt(tokens[0]);
} catch (NumberFormatException nfe) { } catch (NumberFormatException nfe) {
throw new IllegalStateException( throw new NativeDaemonConnectorException(
String.format("Invalid response from daemon (%s)", line)); String.format("Invalid response from daemon (%s)", line));
} }
@@ -233,7 +229,7 @@ final class NativeDaemonConnector implements Runnable {
if (code >= ResponseCode.FailedRangeStart && if (code >= ResponseCode.FailedRangeStart &&
code <= ResponseCode.FailedRangeEnd) { code <= ResponseCode.FailedRangeEnd) {
throw new IllegalStateException(String.format( throw new NativeDaemonConnectorException(code, String.format(
"Command %s failed with code %d", "Command %s failed with code %d",
cmd, code)); cmd, code));
} }
@@ -244,7 +240,7 @@ final class NativeDaemonConnector implements Runnable {
* Issues a list command and returns the cooked list * Issues a list command and returns the cooked list
*/ */
public String[] doListCommand(String cmd, int expectedResponseCode) public String[] doListCommand(String cmd, int expectedResponseCode)
throws IllegalStateException { throws NativeDaemonConnectorException {
ArrayList<String> rsp = doCommand(cmd); ArrayList<String> rsp = doCommand(cmd);
String[] rdata = new String[rsp.size()-1]; String[] rdata = new String[rsp.size()-1];
@@ -259,14 +255,15 @@ final class NativeDaemonConnector implements Runnable {
} else if (code == NativeDaemonConnector.ResponseCode.CommandOkay) { } else if (code == NativeDaemonConnector.ResponseCode.CommandOkay) {
return rdata; return rdata;
} else { } else {
throw new IllegalStateException( throw new NativeDaemonConnectorException(
String.format("Expected list response %d, but got %d", String.format("Expected list response %d, but got %d",
expectedResponseCode, code)); expectedResponseCode, code));
} }
} catch (NumberFormatException nfe) { } 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");
} }
} }

View File

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