San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.server; |
| 18 | |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 19 | import android.net.LocalSocket; |
Jeff Sharkey | fa23c5a | 2011-08-09 21:44:24 -0700 | [diff] [blame] | 20 | import android.net.LocalSocketAddress; |
Chia-chi Yeh | e5750a3 | 2011-08-03 14:42:11 -0700 | [diff] [blame] | 21 | import android.os.Handler; |
| 22 | import android.os.HandlerThread; |
| 23 | import android.os.Message; |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 24 | import android.os.SystemClock; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 25 | import android.util.Slog; |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 26 | |
Jeff Sharkey | 31c6e48 | 2011-11-18 17:09:01 -0800 | [diff] [blame] | 27 | import com.google.android.collect.Lists; |
| 28 | |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 29 | import java.io.IOException; |
| 30 | import java.io.InputStream; |
| 31 | import java.io.OutputStream; |
Jeff Sharkey | ba2896e | 2011-11-30 18:13:54 -0800 | [diff] [blame] | 32 | import java.nio.charset.Charsets; |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 33 | import java.util.ArrayList; |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 34 | import java.util.concurrent.BlockingQueue; |
| 35 | import java.util.concurrent.LinkedBlockingQueue; |
| 36 | |
| 37 | /** |
Jeff Sharkey | 31c6e48 | 2011-11-18 17:09:01 -0800 | [diff] [blame] | 38 | * Generic connector class for interfacing with a native daemon which uses the |
| 39 | * {@code libsysutils} FrameworkListener protocol. |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 40 | */ |
Jeff Sharkey | fa23c5a | 2011-08-09 21:44:24 -0700 | [diff] [blame] | 41 | final class NativeDaemonConnector implements Runnable, Handler.Callback, Watchdog.Monitor { |
Jeff Sharkey | 31c6e48 | 2011-11-18 17:09:01 -0800 | [diff] [blame] | 42 | private static final boolean LOGD = false; |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 43 | |
Jeff Sharkey | 31c6e48 | 2011-11-18 17:09:01 -0800 | [diff] [blame] | 44 | private final String TAG; |
| 45 | |
| 46 | private String mSocket; |
| 47 | private OutputStream mOutputStream; |
| 48 | |
| 49 | private final BlockingQueue<NativeDaemonEvent> mResponseQueue; |
| 50 | |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 51 | private INativeDaemonConnectorCallbacks mCallbacks; |
Jeff Sharkey | 31c6e48 | 2011-11-18 17:09:01 -0800 | [diff] [blame] | 52 | private Handler mCallbackHandler; |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 53 | |
Jeff Sharkey | fa23c5a | 2011-08-09 21:44:24 -0700 | [diff] [blame] | 54 | /** Lock held whenever communicating with native daemon. */ |
Jeff Sharkey | 31c6e48 | 2011-11-18 17:09:01 -0800 | [diff] [blame] | 55 | private final Object mDaemonLock = new Object(); |
Jeff Sharkey | fa23c5a | 2011-08-09 21:44:24 -0700 | [diff] [blame] | 56 | |
Kenny Root | 961aa8c | 2010-03-22 18:02:45 -0700 | [diff] [blame] | 57 | private final int BUFFER_SIZE = 4096; |
| 58 | |
Jeff Sharkey | 31c6e48 | 2011-11-18 17:09:01 -0800 | [diff] [blame] | 59 | NativeDaemonConnector(INativeDaemonConnectorCallbacks callbacks, String socket, |
| 60 | int responseQueueSize, String logTag) { |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 61 | mCallbacks = callbacks; |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 62 | mSocket = socket; |
Jeff Sharkey | 31c6e48 | 2011-11-18 17:09:01 -0800 | [diff] [blame] | 63 | mResponseQueue = new LinkedBlockingQueue<NativeDaemonEvent>(responseQueueSize); |
| 64 | TAG = logTag != null ? logTag : "NativeDaemonConnector"; |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 65 | } |
| 66 | |
Chia-chi Yeh | e5750a3 | 2011-08-03 14:42:11 -0700 | [diff] [blame] | 67 | @Override |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 68 | public void run() { |
Chia-chi Yeh | e5750a3 | 2011-08-03 14:42:11 -0700 | [diff] [blame] | 69 | HandlerThread thread = new HandlerThread(TAG + ".CallbackHandler"); |
| 70 | thread.start(); |
| 71 | mCallbackHandler = new Handler(thread.getLooper(), this); |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 72 | |
| 73 | while (true) { |
| 74 | try { |
| 75 | listenToSocket(); |
| 76 | } catch (Exception e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 77 | Slog.e(TAG, "Error in NativeDaemonConnector", e); |
San Mehat | 4c27e0e | 2010-01-29 05:22:17 -0800 | [diff] [blame] | 78 | SystemClock.sleep(5000); |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 79 | } |
| 80 | } |
| 81 | } |
| 82 | |
Chia-chi Yeh | e5750a3 | 2011-08-03 14:42:11 -0700 | [diff] [blame] | 83 | @Override |
| 84 | public boolean handleMessage(Message msg) { |
| 85 | String event = (String) msg.obj; |
| 86 | try { |
| 87 | if (!mCallbacks.onEvent(msg.what, event, event.split(" "))) { |
| 88 | Slog.w(TAG, String.format( |
| 89 | "Unhandled event '%s'", event)); |
| 90 | } |
| 91 | } catch (Exception e) { |
| 92 | Slog.e(TAG, String.format( |
| 93 | "Error handling '%s'", event), e); |
| 94 | } |
| 95 | return true; |
| 96 | } |
| 97 | |
San Mehat | 4c27e0e | 2010-01-29 05:22:17 -0800 | [diff] [blame] | 98 | private void listenToSocket() throws IOException { |
Kenny Root | 961aa8c | 2010-03-22 18:02:45 -0700 | [diff] [blame] | 99 | LocalSocket socket = null; |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 100 | |
| 101 | try { |
| 102 | socket = new LocalSocket(); |
| 103 | LocalSocketAddress address = new LocalSocketAddress(mSocket, |
| 104 | LocalSocketAddress.Namespace.RESERVED); |
| 105 | |
| 106 | socket.connect(address); |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 107 | |
| 108 | InputStream inputStream = socket.getInputStream(); |
| 109 | mOutputStream = socket.getOutputStream(); |
| 110 | |
anga | 030bc88 | 2011-02-01 14:10:25 +0100 | [diff] [blame] | 111 | mCallbacks.onDaemonConnected(); |
| 112 | |
Kenny Root | 961aa8c | 2010-03-22 18:02:45 -0700 | [diff] [blame] | 113 | byte[] buffer = new byte[BUFFER_SIZE]; |
| 114 | int start = 0; |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 115 | |
| 116 | while (true) { |
Kenny Root | 961aa8c | 2010-03-22 18:02:45 -0700 | [diff] [blame] | 117 | int count = inputStream.read(buffer, start, BUFFER_SIZE - start); |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 118 | if (count < 0) break; |
| 119 | |
Kenny Root | 12da9d7 | 2010-09-02 22:18:14 -0700 | [diff] [blame] | 120 | // Add our starting point to the count and reset the start. |
| 121 | count += start; |
| 122 | start = 0; |
| 123 | |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 124 | for (int i = 0; i < count; i++) { |
| 125 | if (buffer[i] == 0) { |
Jeff Sharkey | ba2896e | 2011-11-30 18:13:54 -0800 | [diff] [blame] | 126 | final String rawEvent = new String( |
| 127 | buffer, start, i - start, Charsets.UTF_8); |
Jeff Sharkey | 31c6e48 | 2011-11-18 17:09:01 -0800 | [diff] [blame] | 128 | if (LOGD) Slog.d(TAG, "RCV <- " + rawEvent); |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 129 | |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 130 | try { |
Jeff Sharkey | 31c6e48 | 2011-11-18 17:09:01 -0800 | [diff] [blame] | 131 | final NativeDaemonEvent event = NativeDaemonEvent.parseRawEvent( |
| 132 | rawEvent); |
| 133 | if (event.isClassUnsolicited()) { |
Jeff Sharkey | ba2896e | 2011-11-30 18:13:54 -0800 | [diff] [blame] | 134 | // TODO: migrate to sending NativeDaemonEvent instances |
Jeff Sharkey | 31c6e48 | 2011-11-18 17:09:01 -0800 | [diff] [blame] | 135 | mCallbackHandler.sendMessage(mCallbackHandler.obtainMessage( |
| 136 | event.getCode(), event.getRawEvent())); |
Irfan Sheriff | 1cd94ef | 2011-01-16 14:31:55 -0800 | [diff] [blame] | 137 | } else { |
| 138 | try { |
| 139 | mResponseQueue.put(event); |
| 140 | } catch (InterruptedException ex) { |
Jeff Sharkey | 31c6e48 | 2011-11-18 17:09:01 -0800 | [diff] [blame] | 141 | Slog.e(TAG, "Failed to put response onto queue: " + ex); |
Irfan Sheriff | 1cd94ef | 2011-01-16 14:31:55 -0800 | [diff] [blame] | 142 | } |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 143 | } |
Jeff Sharkey | 31c6e48 | 2011-11-18 17:09:01 -0800 | [diff] [blame] | 144 | } catch (IllegalArgumentException e) { |
| 145 | Slog.w(TAG, "Problem parsing message: " + rawEvent, e); |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 146 | } |
Jeff Sharkey | 31c6e48 | 2011-11-18 17:09:01 -0800 | [diff] [blame] | 147 | |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 148 | start = i + 1; |
| 149 | } |
| 150 | } |
Kenny Root | 12da9d7 | 2010-09-02 22:18:14 -0700 | [diff] [blame] | 151 | |
| 152 | // We should end at the amount we read. If not, compact then |
| 153 | // buffer and read again. |
Kenny Root | 961aa8c | 2010-03-22 18:02:45 -0700 | [diff] [blame] | 154 | if (start != count) { |
| 155 | final int remaining = BUFFER_SIZE - start; |
| 156 | System.arraycopy(buffer, start, buffer, 0, remaining); |
| 157 | start = remaining; |
| 158 | } else { |
| 159 | start = 0; |
| 160 | } |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 161 | } |
| 162 | } catch (IOException ex) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 163 | Slog.e(TAG, "Communications error", ex); |
San Mehat | 4c27e0e | 2010-01-29 05:22:17 -0800 | [diff] [blame] | 164 | throw ex; |
| 165 | } finally { |
Jeff Sharkey | fa23c5a | 2011-08-09 21:44:24 -0700 | [diff] [blame] | 166 | synchronized (mDaemonLock) { |
San Mehat | 4c27e0e | 2010-01-29 05:22:17 -0800 | [diff] [blame] | 167 | if (mOutputStream != null) { |
| 168 | try { |
| 169 | mOutputStream.close(); |
| 170 | } catch (IOException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 171 | Slog.w(TAG, "Failed closing output stream", e); |
San Mehat | 4c27e0e | 2010-01-29 05:22:17 -0800 | [diff] [blame] | 172 | } |
| 173 | mOutputStream = null; |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 174 | } |
San Mehat | 4c27e0e | 2010-01-29 05:22:17 -0800 | [diff] [blame] | 175 | } |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 176 | |
San Mehat | 4c27e0e | 2010-01-29 05:22:17 -0800 | [diff] [blame] | 177 | try { |
| 178 | if (socket != null) { |
| 179 | socket.close(); |
| 180 | } |
| 181 | } catch (IOException ex) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 182 | Slog.w(TAG, "Failed closing socket", ex); |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 183 | } |
| 184 | } |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 185 | } |
| 186 | |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 187 | /** |
Jeff Sharkey | 31c6e48 | 2011-11-18 17:09:01 -0800 | [diff] [blame] | 188 | * Send command to daemon, escaping arguments as needed. |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 189 | * |
Jeff Sharkey | 31c6e48 | 2011-11-18 17:09:01 -0800 | [diff] [blame] | 190 | * @return the final command issued. |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 191 | */ |
Jeff Sharkey | 31c6e48 | 2011-11-18 17:09:01 -0800 | [diff] [blame] | 192 | private String sendCommandLocked(String cmd, Object... args) |
Jeff Sharkey | fa23c5a | 2011-08-09 21:44:24 -0700 | [diff] [blame] | 193 | throws NativeDaemonConnectorException { |
Jeff Sharkey | 31c6e48 | 2011-11-18 17:09:01 -0800 | [diff] [blame] | 194 | // TODO: eventually enforce that cmd doesn't contain arguments |
| 195 | if (cmd.indexOf('\0') >= 0) { |
| 196 | throw new IllegalArgumentException("unexpected command: " + cmd); |
Jeff Sharkey | b0aec07 | 2011-10-14 18:32:24 -0700 | [diff] [blame] | 197 | } |
| 198 | |
Jeff Sharkey | 31c6e48 | 2011-11-18 17:09:01 -0800 | [diff] [blame] | 199 | final StringBuilder builder = new StringBuilder(cmd); |
| 200 | for (Object arg : args) { |
| 201 | final String argString = String.valueOf(arg); |
| 202 | if (argString.indexOf('\0') >= 0) { |
| 203 | throw new IllegalArgumentException("unexpected argument: " + arg); |
Jeff Sharkey | fa23c5a | 2011-08-09 21:44:24 -0700 | [diff] [blame] | 204 | } |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 205 | |
Jeff Sharkey | 31c6e48 | 2011-11-18 17:09:01 -0800 | [diff] [blame] | 206 | builder.append(' '); |
| 207 | appendEscaped(builder, argString); |
| 208 | } |
| 209 | |
| 210 | final String unterminated = builder.toString(); |
| 211 | if (LOGD) Slog.d(TAG, "SND -> " + unterminated); |
| 212 | |
| 213 | builder.append('\0'); |
| 214 | |
| 215 | if (mOutputStream == null) { |
| 216 | throw new NativeDaemonConnectorException("missing output stream"); |
| 217 | } else { |
Jeff Sharkey | fa23c5a | 2011-08-09 21:44:24 -0700 | [diff] [blame] | 218 | try { |
Jeff Sharkey | ba2896e | 2011-11-30 18:13:54 -0800 | [diff] [blame] | 219 | mOutputStream.write(builder.toString().getBytes(Charsets.UTF_8)); |
Jeff Sharkey | 31c6e48 | 2011-11-18 17:09:01 -0800 | [diff] [blame] | 220 | } catch (IOException e) { |
| 221 | throw new NativeDaemonConnectorException("problem sending command", e); |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 222 | } |
| 223 | } |
| 224 | |
Jeff Sharkey | 31c6e48 | 2011-11-18 17:09:01 -0800 | [diff] [blame] | 225 | return unterminated; |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 226 | } |
San Mehat | deba693 | 2010-01-20 15:14:31 -0800 | [diff] [blame] | 227 | |
Jeff Sharkey | fa23c5a | 2011-08-09 21:44:24 -0700 | [diff] [blame] | 228 | /** |
Jeff Sharkey | ba2896e | 2011-11-30 18:13:54 -0800 | [diff] [blame] | 229 | * Issue the given command to the native daemon and return a single expected |
| 230 | * response. |
| 231 | * |
| 232 | * @throws NativeDaemonConnectorException when problem communicating with |
| 233 | * native daemon, or if the response matches |
| 234 | * {@link NativeDaemonEvent#isClassClientError()} or |
| 235 | * {@link NativeDaemonEvent#isClassServerError()}. |
San Mehat | deba693 | 2010-01-20 15:14:31 -0800 | [diff] [blame] | 236 | */ |
Jeff Sharkey | ba2896e | 2011-11-30 18:13:54 -0800 | [diff] [blame] | 237 | public NativeDaemonEvent execute(Command cmd) throws NativeDaemonConnectorException { |
| 238 | return execute(cmd.mCmd, cmd.mArguments.toArray()); |
| 239 | } |
| 240 | |
| 241 | /** |
| 242 | * Issue the given command to the native daemon and return a single expected |
| 243 | * response. |
| 244 | * |
| 245 | * @throws NativeDaemonConnectorException when problem communicating with |
| 246 | * native daemon, or if the response matches |
| 247 | * {@link NativeDaemonEvent#isClassClientError()} or |
| 248 | * {@link NativeDaemonEvent#isClassServerError()}. |
| 249 | */ |
| 250 | public NativeDaemonEvent execute(String cmd, Object... args) |
| 251 | throws NativeDaemonConnectorException { |
| 252 | final NativeDaemonEvent[] events = executeForList(cmd, args); |
| 253 | if (events.length != 1) { |
| 254 | throw new NativeDaemonConnectorException( |
| 255 | "Expected exactly one response, but received " + events.length); |
| 256 | } |
| 257 | return events[0]; |
| 258 | } |
| 259 | |
| 260 | /** |
| 261 | * Issue the given command to the native daemon and return any |
| 262 | * {@link NativeDaemonEvent#isClassContinue()} responses, including the |
| 263 | * final terminal response. |
| 264 | * |
| 265 | * @throws NativeDaemonConnectorException when problem communicating with |
| 266 | * native daemon, or if the response matches |
| 267 | * {@link NativeDaemonEvent#isClassClientError()} or |
| 268 | * {@link NativeDaemonEvent#isClassServerError()}. |
| 269 | */ |
| 270 | public NativeDaemonEvent[] executeForList(Command cmd) throws NativeDaemonConnectorException { |
| 271 | return executeForList(cmd.mCmd, cmd.mArguments.toArray()); |
| 272 | } |
| 273 | |
| 274 | /** |
| 275 | * Issue the given command to the native daemon and return any |
| 276 | * {@link NativeDaemonEvent#isClassContinue()} responses, including the |
| 277 | * final terminal response. |
| 278 | * |
| 279 | * @throws NativeDaemonConnectorException when problem communicating with |
| 280 | * native daemon, or if the response matches |
| 281 | * {@link NativeDaemonEvent#isClassClientError()} or |
| 282 | * {@link NativeDaemonEvent#isClassServerError()}. |
| 283 | */ |
| 284 | public NativeDaemonEvent[] executeForList(String cmd, Object... args) |
San Mehat | 4c27e0e | 2010-01-29 05:22:17 -0800 | [diff] [blame] | 285 | throws NativeDaemonConnectorException { |
Jeff Sharkey | 31c6e48 | 2011-11-18 17:09:01 -0800 | [diff] [blame] | 286 | synchronized (mDaemonLock) { |
| 287 | return executeLocked(cmd, args); |
| 288 | } |
| 289 | } |
San Mehat | deba693 | 2010-01-20 15:14:31 -0800 | [diff] [blame] | 290 | |
Jeff Sharkey | 31c6e48 | 2011-11-18 17:09:01 -0800 | [diff] [blame] | 291 | private NativeDaemonEvent[] executeLocked(String cmd, Object... args) |
| 292 | throws NativeDaemonConnectorException { |
| 293 | final ArrayList<NativeDaemonEvent> events = Lists.newArrayList(); |
San Mehat | deba693 | 2010-01-20 15:14:31 -0800 | [diff] [blame] | 294 | |
Jeff Sharkey | 31c6e48 | 2011-11-18 17:09:01 -0800 | [diff] [blame] | 295 | mResponseQueue.clear(); |
| 296 | |
| 297 | final String sentCommand = sendCommandLocked(cmd, args); |
| 298 | |
| 299 | NativeDaemonEvent event = null; |
| 300 | do { |
San Mehat | deba693 | 2010-01-20 15:14:31 -0800 | [diff] [blame] | 301 | try { |
Jeff Sharkey | 31c6e48 | 2011-11-18 17:09:01 -0800 | [diff] [blame] | 302 | event = mResponseQueue.take(); |
| 303 | } catch (InterruptedException e) { |
| 304 | Slog.w(TAG, "interrupted waiting for event line"); |
| 305 | continue; |
| 306 | } |
| 307 | events.add(event); |
| 308 | } while (event.isClassContinue()); |
| 309 | |
| 310 | if (event.isClassClientError()) { |
| 311 | throw new NativeDaemonArgumentException(sentCommand, event); |
| 312 | } |
| 313 | if (event.isClassServerError()) { |
| 314 | throw new NativeDaemonFailureException(sentCommand, event); |
| 315 | } |
| 316 | |
| 317 | return events.toArray(new NativeDaemonEvent[events.size()]); |
| 318 | } |
| 319 | |
| 320 | /** |
| 321 | * Issue a command to the native daemon and return the raw responses. |
| 322 | * |
| 323 | * @deprecated callers should move to {@link #execute(String, Object...)} |
| 324 | * which returns parsed {@link NativeDaemonEvent}. |
| 325 | */ |
| 326 | @Deprecated |
| 327 | public ArrayList<String> doCommand(String cmd) throws NativeDaemonConnectorException { |
| 328 | final ArrayList<String> rawEvents = Lists.newArrayList(); |
Jeff Sharkey | ba2896e | 2011-11-30 18:13:54 -0800 | [diff] [blame] | 329 | final NativeDaemonEvent[] events = executeForList(cmd); |
Jeff Sharkey | 31c6e48 | 2011-11-18 17:09:01 -0800 | [diff] [blame] | 330 | for (NativeDaemonEvent event : events) { |
| 331 | rawEvents.add(event.getRawEvent()); |
| 332 | } |
| 333 | return rawEvents; |
| 334 | } |
| 335 | |
| 336 | /** |
| 337 | * Issues a list command and returns the cooked list of all |
| 338 | * {@link NativeDaemonEvent#getMessage()} which match requested code. |
| 339 | */ |
Jeff Sharkey | ba2896e | 2011-11-30 18:13:54 -0800 | [diff] [blame] | 340 | @Deprecated |
Jeff Sharkey | 31c6e48 | 2011-11-18 17:09:01 -0800 | [diff] [blame] | 341 | public String[] doListCommand(String cmd, int expectedCode) |
| 342 | throws NativeDaemonConnectorException { |
| 343 | final ArrayList<String> list = Lists.newArrayList(); |
| 344 | |
Jeff Sharkey | ba2896e | 2011-11-30 18:13:54 -0800 | [diff] [blame] | 345 | final NativeDaemonEvent[] events = executeForList(cmd); |
Jeff Sharkey | 31c6e48 | 2011-11-18 17:09:01 -0800 | [diff] [blame] | 346 | for (int i = 0; i < events.length - 1; i++) { |
| 347 | final NativeDaemonEvent event = events[i]; |
| 348 | final int code = event.getCode(); |
| 349 | if (code == expectedCode) { |
| 350 | list.add(event.getMessage()); |
| 351 | } else { |
San Mehat | 4c27e0e | 2010-01-29 05:22:17 -0800 | [diff] [blame] | 352 | throw new NativeDaemonConnectorException( |
Jeff Sharkey | 31c6e48 | 2011-11-18 17:09:01 -0800 | [diff] [blame] | 353 | "unexpected list response " + code + " instead of " + expectedCode); |
San Mehat | deba693 | 2010-01-20 15:14:31 -0800 | [diff] [blame] | 354 | } |
| 355 | } |
Jeff Sharkey | 31c6e48 | 2011-11-18 17:09:01 -0800 | [diff] [blame] | 356 | |
| 357 | final NativeDaemonEvent finalEvent = events[events.length - 1]; |
| 358 | if (!finalEvent.isClassOk()) { |
| 359 | throw new NativeDaemonConnectorException("unexpected final event: " + finalEvent); |
| 360 | } |
| 361 | |
| 362 | return list.toArray(new String[list.size()]); |
| 363 | } |
| 364 | |
| 365 | /** |
| 366 | * Append the given argument to {@link StringBuilder}, escaping as needed, |
| 367 | * and surrounding with quotes when it contains spaces. |
| 368 | */ |
| 369 | // @VisibleForTesting |
| 370 | static void appendEscaped(StringBuilder builder, String arg) { |
| 371 | final boolean hasSpaces = arg.indexOf(' ') >= 0; |
| 372 | if (hasSpaces) { |
| 373 | builder.append('"'); |
| 374 | } |
| 375 | |
| 376 | final int length = arg.length(); |
| 377 | for (int i = 0; i < length; i++) { |
| 378 | final char c = arg.charAt(i); |
| 379 | |
| 380 | if (c == '"') { |
| 381 | builder.append("\\\""); |
| 382 | } else if (c == '\\') { |
| 383 | builder.append("\\\\"); |
| 384 | } else { |
| 385 | builder.append(c); |
| 386 | } |
| 387 | } |
| 388 | |
| 389 | if (hasSpaces) { |
| 390 | builder.append('"'); |
| 391 | } |
| 392 | } |
| 393 | |
| 394 | private static class NativeDaemonArgumentException extends NativeDaemonConnectorException { |
| 395 | public NativeDaemonArgumentException(String command, NativeDaemonEvent event) { |
| 396 | super(command, event); |
| 397 | } |
| 398 | |
| 399 | @Override |
| 400 | public IllegalArgumentException rethrowAsParcelableException() { |
| 401 | throw new IllegalArgumentException(getMessage(), this); |
| 402 | } |
| 403 | } |
| 404 | |
| 405 | private static class NativeDaemonFailureException extends NativeDaemonConnectorException { |
| 406 | public NativeDaemonFailureException(String command, NativeDaemonEvent event) { |
| 407 | super(command, event); |
| 408 | } |
San Mehat | deba693 | 2010-01-20 15:14:31 -0800 | [diff] [blame] | 409 | } |
Jeff Sharkey | fa23c5a | 2011-08-09 21:44:24 -0700 | [diff] [blame] | 410 | |
Jeff Sharkey | ba2896e | 2011-11-30 18:13:54 -0800 | [diff] [blame] | 411 | /** |
| 412 | * Command builder that handles argument list building. |
| 413 | */ |
| 414 | public static class Command { |
| 415 | private String mCmd; |
| 416 | private ArrayList<Object> mArguments = Lists.newArrayList(); |
| 417 | |
| 418 | public Command(String cmd, Object... args) { |
| 419 | mCmd = cmd; |
| 420 | for (Object arg : args) { |
| 421 | appendArg(arg); |
| 422 | } |
| 423 | } |
| 424 | |
| 425 | public Command appendArg(Object arg) { |
| 426 | mArguments.add(arg); |
| 427 | return this; |
| 428 | } |
| 429 | } |
| 430 | |
Jeff Sharkey | fa23c5a | 2011-08-09 21:44:24 -0700 | [diff] [blame] | 431 | /** {@inheritDoc} */ |
| 432 | public void monitor() { |
| 433 | synchronized (mDaemonLock) { } |
| 434 | } |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 435 | } |