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; |
Lorenzo Colitti | 7421a01 | 2013-08-20 22:51:24 +0900 | [diff] [blame] | 21 | import android.os.Build; |
Chia-chi Yeh | e5750a3 | 2011-08-03 14:42:11 -0700 | [diff] [blame] | 22 | import android.os.Handler; |
Dianne Hackborn | 2ffa11e | 2014-04-21 15:56:18 -0700 | [diff] [blame] | 23 | import android.os.Looper; |
Chia-chi Yeh | e5750a3 | 2011-08-03 14:42:11 -0700 | [diff] [blame] | 24 | import android.os.Message; |
Dianne Hackborn | 77b987f | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 25 | import android.os.PowerManager; |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 26 | import android.os.SystemClock; |
Robert Greenwalt | 470fd72 | 2012-01-18 12:51:15 -0800 | [diff] [blame] | 27 | import android.util.LocalLog; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 28 | import android.util.Slog; |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 29 | |
Jeff Sharkey | 8b2c3a14 | 2012-11-12 11:45:05 -0800 | [diff] [blame] | 30 | import com.android.internal.annotations.VisibleForTesting; |
Jeff Sharkey | 31c6e48 | 2011-11-18 17:09:01 -0800 | [diff] [blame] | 31 | import com.google.android.collect.Lists; |
| 32 | |
Robert Greenwalt | 470fd72 | 2012-01-18 12:51:15 -0800 | [diff] [blame] | 33 | import java.io.FileDescriptor; |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 34 | import java.io.IOException; |
| 35 | import java.io.InputStream; |
| 36 | import java.io.OutputStream; |
Robert Greenwalt | 470fd72 | 2012-01-18 12:51:15 -0800 | [diff] [blame] | 37 | import java.io.PrintWriter; |
Elliott Hughes | d396a44 | 2013-06-28 16:24:48 -0700 | [diff] [blame] | 38 | import java.nio.charset.StandardCharsets; |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 39 | import java.util.ArrayList; |
Robert Greenwalt | 470007f | 2012-02-07 11:36:55 -0800 | [diff] [blame] | 40 | import java.util.concurrent.atomic.AtomicInteger; |
Robert Greenwalt | ef21599 | 2012-06-05 11:48:40 -0700 | [diff] [blame] | 41 | import java.util.concurrent.ArrayBlockingQueue; |
| 42 | import java.util.concurrent.BlockingQueue; |
| 43 | import java.util.concurrent.TimeUnit; |
Robert Greenwalt | 470007f | 2012-02-07 11:36:55 -0800 | [diff] [blame] | 44 | import java.util.LinkedList; |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 45 | |
| 46 | /** |
Jeff Sharkey | 31c6e48 | 2011-11-18 17:09:01 -0800 | [diff] [blame] | 47 | * Generic connector class for interfacing with a native daemon which uses the |
| 48 | * {@code libsysutils} FrameworkListener protocol. |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 49 | */ |
Jeff Sharkey | fa23c5a | 2011-08-09 21:44:24 -0700 | [diff] [blame] | 50 | final class NativeDaemonConnector implements Runnable, Handler.Callback, Watchdog.Monitor { |
Robert Greenwalt | 7f44ff8 | 2014-05-07 23:49:08 -0700 | [diff] [blame] | 51 | private final static boolean VDBG = false; |
| 52 | |
Jeff Sharkey | 31c6e48 | 2011-11-18 17:09:01 -0800 | [diff] [blame] | 53 | private final String TAG; |
| 54 | |
| 55 | private String mSocket; |
| 56 | private OutputStream mOutputStream; |
Robert Greenwalt | 470fd72 | 2012-01-18 12:51:15 -0800 | [diff] [blame] | 57 | private LocalLog mLocalLog; |
Jeff Sharkey | 31c6e48 | 2011-11-18 17:09:01 -0800 | [diff] [blame] | 58 | |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 59 | private volatile boolean mDebug = false; |
| 60 | |
Robert Greenwalt | 470007f | 2012-02-07 11:36:55 -0800 | [diff] [blame] | 61 | private final ResponseQueue mResponseQueue; |
Jeff Sharkey | 31c6e48 | 2011-11-18 17:09:01 -0800 | [diff] [blame] | 62 | |
Dianne Hackborn | 77b987f | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 63 | private final PowerManager.WakeLock mWakeLock; |
| 64 | |
Dianne Hackborn | 2ffa11e | 2014-04-21 15:56:18 -0700 | [diff] [blame] | 65 | private final Looper mLooper; |
| 66 | |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 67 | private INativeDaemonConnectorCallbacks mCallbacks; |
Jeff Sharkey | 31c6e48 | 2011-11-18 17:09:01 -0800 | [diff] [blame] | 68 | private Handler mCallbackHandler; |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 69 | |
Robert Greenwalt | 470007f | 2012-02-07 11:36:55 -0800 | [diff] [blame] | 70 | private AtomicInteger mSequenceNumber; |
| 71 | |
Jeff Sharkey | 14cbe52 | 2015-07-08 14:06:37 -0700 | [diff] [blame] | 72 | private static final long DEFAULT_TIMEOUT = 1 * 60 * 1000; /* 1 minute */ |
Robert Greenwalt | 5a0c320 | 2012-05-22 16:07:46 -0700 | [diff] [blame] | 73 | private static final long WARN_EXECUTE_DELAY_MS = 500; /* .5 sec */ |
Robert Greenwalt | 470007f | 2012-02-07 11:36:55 -0800 | [diff] [blame] | 74 | |
Jeff Sharkey | fa23c5a | 2011-08-09 21:44:24 -0700 | [diff] [blame] | 75 | /** Lock held whenever communicating with native daemon. */ |
Jeff Sharkey | 31c6e48 | 2011-11-18 17:09:01 -0800 | [diff] [blame] | 76 | private final Object mDaemonLock = new Object(); |
Jeff Sharkey | fa23c5a | 2011-08-09 21:44:24 -0700 | [diff] [blame] | 77 | |
Kenny Root | 961aa8c | 2010-03-22 18:02:45 -0700 | [diff] [blame] | 78 | private final int BUFFER_SIZE = 4096; |
| 79 | |
Jeff Sharkey | 31c6e48 | 2011-11-18 17:09:01 -0800 | [diff] [blame] | 80 | NativeDaemonConnector(INativeDaemonConnectorCallbacks callbacks, String socket, |
Dianne Hackborn | 77b987f | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 81 | int responseQueueSize, String logTag, int maxLogSize, PowerManager.WakeLock wl) { |
Dianne Hackborn | 2ffa11e | 2014-04-21 15:56:18 -0700 | [diff] [blame] | 82 | this(callbacks, socket, responseQueueSize, logTag, maxLogSize, wl, |
| 83 | FgThread.get().getLooper()); |
| 84 | } |
| 85 | |
| 86 | NativeDaemonConnector(INativeDaemonConnectorCallbacks callbacks, String socket, |
| 87 | int responseQueueSize, String logTag, int maxLogSize, PowerManager.WakeLock wl, |
| 88 | Looper looper) { |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 89 | mCallbacks = callbacks; |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 90 | mSocket = socket; |
Robert Greenwalt | 470007f | 2012-02-07 11:36:55 -0800 | [diff] [blame] | 91 | mResponseQueue = new ResponseQueue(responseQueueSize); |
Dianne Hackborn | 77b987f | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 92 | mWakeLock = wl; |
| 93 | if (mWakeLock != null) { |
| 94 | mWakeLock.setReferenceCounted(true); |
| 95 | } |
Dianne Hackborn | 2ffa11e | 2014-04-21 15:56:18 -0700 | [diff] [blame] | 96 | mLooper = looper; |
Robert Greenwalt | 470007f | 2012-02-07 11:36:55 -0800 | [diff] [blame] | 97 | mSequenceNumber = new AtomicInteger(0); |
Jeff Sharkey | 31c6e48 | 2011-11-18 17:09:01 -0800 | [diff] [blame] | 98 | TAG = logTag != null ? logTag : "NativeDaemonConnector"; |
Robert Greenwalt | 470fd72 | 2012-01-18 12:51:15 -0800 | [diff] [blame] | 99 | mLocalLog = new LocalLog(maxLogSize); |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 100 | } |
| 101 | |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 102 | /** |
| 103 | * Enable Set debugging mode, which causes messages to also be written to both |
| 104 | * {@link Slog} in addition to internal log. |
| 105 | */ |
| 106 | public void setDebug(boolean debug) { |
| 107 | mDebug = debug; |
| 108 | } |
| 109 | |
Chia-chi Yeh | e5750a3 | 2011-08-03 14:42:11 -0700 | [diff] [blame] | 110 | @Override |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 111 | public void run() { |
Dianne Hackborn | 2ffa11e | 2014-04-21 15:56:18 -0700 | [diff] [blame] | 112 | mCallbackHandler = new Handler(mLooper, this); |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 113 | |
| 114 | while (true) { |
| 115 | try { |
| 116 | listenToSocket(); |
| 117 | } catch (Exception e) { |
Robert Greenwalt | 470007f | 2012-02-07 11:36:55 -0800 | [diff] [blame] | 118 | loge("Error in NativeDaemonConnector: " + e); |
San Mehat | 4c27e0e | 2010-01-29 05:22:17 -0800 | [diff] [blame] | 119 | SystemClock.sleep(5000); |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 120 | } |
| 121 | } |
| 122 | } |
| 123 | |
Chia-chi Yeh | e5750a3 | 2011-08-03 14:42:11 -0700 | [diff] [blame] | 124 | @Override |
| 125 | public boolean handleMessage(Message msg) { |
| 126 | String event = (String) msg.obj; |
| 127 | try { |
Robert Greenwalt | 2d34b4a | 2012-04-20 13:08:02 -0700 | [diff] [blame] | 128 | if (!mCallbacks.onEvent(msg.what, event, NativeDaemonEvent.unescapeArgs(event))) { |
Robert Greenwalt | 470007f | 2012-02-07 11:36:55 -0800 | [diff] [blame] | 129 | log(String.format("Unhandled event '%s'", event)); |
Chia-chi Yeh | e5750a3 | 2011-08-03 14:42:11 -0700 | [diff] [blame] | 130 | } |
| 131 | } catch (Exception e) { |
Robert Greenwalt | 470007f | 2012-02-07 11:36:55 -0800 | [diff] [blame] | 132 | loge("Error handling '" + event + "': " + e); |
Dianne Hackborn | 77b987f | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 133 | } finally { |
Dianne Hackborn | 4590e52 | 2014-03-24 13:36:46 -0700 | [diff] [blame] | 134 | if (mCallbacks.onCheckHoldWakeLock(msg.what) && mWakeLock != null) { |
Dianne Hackborn | 77b987f | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 135 | mWakeLock.release(); |
| 136 | } |
Chia-chi Yeh | e5750a3 | 2011-08-03 14:42:11 -0700 | [diff] [blame] | 137 | } |
| 138 | return true; |
| 139 | } |
| 140 | |
Lorenzo Colitti | 7421a01 | 2013-08-20 22:51:24 +0900 | [diff] [blame] | 141 | private LocalSocketAddress determineSocketAddress() { |
| 142 | // If we're testing, set up a socket in a namespace that's accessible to test code. |
| 143 | // In order to ensure that unprivileged apps aren't able to impersonate native daemons on |
| 144 | // production devices, even if said native daemons ill-advisedly pick a socket name that |
| 145 | // starts with __test__, only allow this on debug builds. |
| 146 | if (mSocket.startsWith("__test__") && Build.IS_DEBUGGABLE) { |
| 147 | return new LocalSocketAddress(mSocket); |
| 148 | } else { |
| 149 | return new LocalSocketAddress(mSocket, LocalSocketAddress.Namespace.RESERVED); |
| 150 | } |
| 151 | } |
| 152 | |
San Mehat | 4c27e0e | 2010-01-29 05:22:17 -0800 | [diff] [blame] | 153 | private void listenToSocket() throws IOException { |
Kenny Root | 961aa8c | 2010-03-22 18:02:45 -0700 | [diff] [blame] | 154 | LocalSocket socket = null; |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 155 | |
| 156 | try { |
| 157 | socket = new LocalSocket(); |
Lorenzo Colitti | 7421a01 | 2013-08-20 22:51:24 +0900 | [diff] [blame] | 158 | LocalSocketAddress address = determineSocketAddress(); |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 159 | |
| 160 | socket.connect(address); |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 161 | |
| 162 | InputStream inputStream = socket.getInputStream(); |
Robert Greenwalt | 470007f | 2012-02-07 11:36:55 -0800 | [diff] [blame] | 163 | synchronized (mDaemonLock) { |
| 164 | mOutputStream = socket.getOutputStream(); |
| 165 | } |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 166 | |
anga | 030bc88 | 2011-02-01 14:10:25 +0100 | [diff] [blame] | 167 | mCallbacks.onDaemonConnected(); |
| 168 | |
Kenny Root | 961aa8c | 2010-03-22 18:02:45 -0700 | [diff] [blame] | 169 | byte[] buffer = new byte[BUFFER_SIZE]; |
| 170 | int start = 0; |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 171 | |
| 172 | while (true) { |
Kenny Root | 961aa8c | 2010-03-22 18:02:45 -0700 | [diff] [blame] | 173 | int count = inputStream.read(buffer, start, BUFFER_SIZE - start); |
Robert Greenwalt | 470007f | 2012-02-07 11:36:55 -0800 | [diff] [blame] | 174 | if (count < 0) { |
| 175 | loge("got " + count + " reading with start = " + start); |
| 176 | break; |
| 177 | } |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 178 | |
Kenny Root | 12da9d7 | 2010-09-02 22:18:14 -0700 | [diff] [blame] | 179 | // Add our starting point to the count and reset the start. |
| 180 | count += start; |
| 181 | start = 0; |
| 182 | |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 183 | for (int i = 0; i < count; i++) { |
| 184 | if (buffer[i] == 0) { |
Paul Lawrence | c38182f | 2014-11-11 12:23:22 -0800 | [diff] [blame] | 185 | // Note - do not log this raw message since it may contain |
| 186 | // sensitive data |
Jeff Sharkey | ba2896e | 2011-11-30 18:13:54 -0800 | [diff] [blame] | 187 | final String rawEvent = new String( |
Elliott Hughes | d396a44 | 2013-06-28 16:24:48 -0700 | [diff] [blame] | 188 | buffer, start, i - start, StandardCharsets.UTF_8); |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 189 | |
Dianne Hackborn | 77b987f | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 190 | boolean releaseWl = false; |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 191 | try { |
Jeff Sharkey | 31c6e48 | 2011-11-18 17:09:01 -0800 | [diff] [blame] | 192 | final NativeDaemonEvent event = NativeDaemonEvent.parseRawEvent( |
| 193 | rawEvent); |
Paul Lawrence | c38182f | 2014-11-11 12:23:22 -0800 | [diff] [blame] | 194 | |
| 195 | log("RCV <- {" + event + "}"); |
| 196 | |
Jeff Sharkey | 31c6e48 | 2011-11-18 17:09:01 -0800 | [diff] [blame] | 197 | if (event.isClassUnsolicited()) { |
Jeff Sharkey | ba2896e | 2011-11-30 18:13:54 -0800 | [diff] [blame] | 198 | // TODO: migrate to sending NativeDaemonEvent instances |
Dianne Hackborn | 4590e52 | 2014-03-24 13:36:46 -0700 | [diff] [blame] | 199 | if (mCallbacks.onCheckHoldWakeLock(event.getCode()) |
| 200 | && mWakeLock != null) { |
Dianne Hackborn | 77b987f | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 201 | mWakeLock.acquire(); |
| 202 | releaseWl = true; |
| 203 | } |
| 204 | if (mCallbackHandler.sendMessage(mCallbackHandler.obtainMessage( |
| 205 | event.getCode(), event.getRawEvent()))) { |
| 206 | releaseWl = false; |
| 207 | } |
Irfan Sheriff | 1cd94ef | 2011-01-16 14:31:55 -0800 | [diff] [blame] | 208 | } else { |
Robert Greenwalt | 470007f | 2012-02-07 11:36:55 -0800 | [diff] [blame] | 209 | mResponseQueue.add(event.getCmdNumber(), event); |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 210 | } |
Jeff Sharkey | 31c6e48 | 2011-11-18 17:09:01 -0800 | [diff] [blame] | 211 | } catch (IllegalArgumentException e) { |
Paul Lawrence | c38182f | 2014-11-11 12:23:22 -0800 | [diff] [blame] | 212 | log("Problem parsing message " + e); |
Dianne Hackborn | 77b987f | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 213 | } finally { |
| 214 | if (releaseWl) { |
| 215 | mWakeLock.acquire(); |
| 216 | } |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 217 | } |
Jeff Sharkey | 31c6e48 | 2011-11-18 17:09:01 -0800 | [diff] [blame] | 218 | |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 219 | start = i + 1; |
| 220 | } |
| 221 | } |
Paul Lawrence | c38182f | 2014-11-11 12:23:22 -0800 | [diff] [blame] | 222 | |
Robert Greenwalt | f0be1d89 | 2012-01-20 16:33:15 -0800 | [diff] [blame] | 223 | if (start == 0) { |
Paul Lawrence | c38182f | 2014-11-11 12:23:22 -0800 | [diff] [blame] | 224 | log("RCV incomplete"); |
Robert Greenwalt | f0be1d89 | 2012-01-20 16:33:15 -0800 | [diff] [blame] | 225 | } |
Kenny Root | 12da9d7 | 2010-09-02 22:18:14 -0700 | [diff] [blame] | 226 | |
| 227 | // We should end at the amount we read. If not, compact then |
| 228 | // buffer and read again. |
Kenny Root | 961aa8c | 2010-03-22 18:02:45 -0700 | [diff] [blame] | 229 | if (start != count) { |
| 230 | final int remaining = BUFFER_SIZE - start; |
| 231 | System.arraycopy(buffer, start, buffer, 0, remaining); |
| 232 | start = remaining; |
| 233 | } else { |
| 234 | start = 0; |
| 235 | } |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 236 | } |
| 237 | } catch (IOException ex) { |
Robert Greenwalt | 470007f | 2012-02-07 11:36:55 -0800 | [diff] [blame] | 238 | loge("Communications error: " + ex); |
San Mehat | 4c27e0e | 2010-01-29 05:22:17 -0800 | [diff] [blame] | 239 | throw ex; |
| 240 | } finally { |
Jeff Sharkey | fa23c5a | 2011-08-09 21:44:24 -0700 | [diff] [blame] | 241 | synchronized (mDaemonLock) { |
San Mehat | 4c27e0e | 2010-01-29 05:22:17 -0800 | [diff] [blame] | 242 | if (mOutputStream != null) { |
| 243 | try { |
Robert Greenwalt | 470007f | 2012-02-07 11:36:55 -0800 | [diff] [blame] | 244 | loge("closing stream for " + mSocket); |
San Mehat | 4c27e0e | 2010-01-29 05:22:17 -0800 | [diff] [blame] | 245 | mOutputStream.close(); |
| 246 | } catch (IOException e) { |
Robert Greenwalt | 470007f | 2012-02-07 11:36:55 -0800 | [diff] [blame] | 247 | loge("Failed closing output stream: " + e); |
San Mehat | 4c27e0e | 2010-01-29 05:22:17 -0800 | [diff] [blame] | 248 | } |
| 249 | mOutputStream = null; |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 250 | } |
San Mehat | 4c27e0e | 2010-01-29 05:22:17 -0800 | [diff] [blame] | 251 | } |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 252 | |
San Mehat | 4c27e0e | 2010-01-29 05:22:17 -0800 | [diff] [blame] | 253 | try { |
| 254 | if (socket != null) { |
| 255 | socket.close(); |
| 256 | } |
| 257 | } catch (IOException ex) { |
Robert Greenwalt | 470007f | 2012-02-07 11:36:55 -0800 | [diff] [blame] | 258 | loge("Failed closing socket: " + ex); |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 259 | } |
| 260 | } |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 261 | } |
| 262 | |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 263 | /** |
Jeff Sharkey | 56cd646 | 2013-06-07 15:09:15 -0700 | [diff] [blame] | 264 | * Wrapper around argument that indicates it's sensitive and shouldn't be |
| 265 | * logged. |
| 266 | */ |
| 267 | public static class SensitiveArg { |
| 268 | private final Object mArg; |
| 269 | |
| 270 | public SensitiveArg(Object arg) { |
| 271 | mArg = arg; |
| 272 | } |
| 273 | |
| 274 | @Override |
| 275 | public String toString() { |
| 276 | return String.valueOf(mArg); |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | /** |
Robert Greenwalt | 470007f | 2012-02-07 11:36:55 -0800 | [diff] [blame] | 281 | * Make command for daemon, escaping arguments as needed. |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 282 | */ |
Jeff Sharkey | 56cd646 | 2013-06-07 15:09:15 -0700 | [diff] [blame] | 283 | @VisibleForTesting |
| 284 | static void makeCommand(StringBuilder rawBuilder, StringBuilder logBuilder, int sequenceNumber, |
| 285 | String cmd, Object... args) { |
Jeff Sharkey | 31c6e48 | 2011-11-18 17:09:01 -0800 | [diff] [blame] | 286 | if (cmd.indexOf('\0') >= 0) { |
Jeff Sharkey | 7b4596f | 2013-02-25 10:55:29 -0800 | [diff] [blame] | 287 | throw new IllegalArgumentException("Unexpected command: " + cmd); |
| 288 | } |
| 289 | if (cmd.indexOf(' ') >= 0) { |
| 290 | throw new IllegalArgumentException("Arguments must be separate from command"); |
Jeff Sharkey | b0aec07 | 2011-10-14 18:32:24 -0700 | [diff] [blame] | 291 | } |
| 292 | |
Jeff Sharkey | 56cd646 | 2013-06-07 15:09:15 -0700 | [diff] [blame] | 293 | rawBuilder.append(sequenceNumber).append(' ').append(cmd); |
| 294 | logBuilder.append(sequenceNumber).append(' ').append(cmd); |
Jeff Sharkey | 31c6e48 | 2011-11-18 17:09:01 -0800 | [diff] [blame] | 295 | for (Object arg : args) { |
| 296 | final String argString = String.valueOf(arg); |
| 297 | if (argString.indexOf('\0') >= 0) { |
Jeff Sharkey | 7b4596f | 2013-02-25 10:55:29 -0800 | [diff] [blame] | 298 | throw new IllegalArgumentException("Unexpected argument: " + arg); |
Jeff Sharkey | fa23c5a | 2011-08-09 21:44:24 -0700 | [diff] [blame] | 299 | } |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 300 | |
Jeff Sharkey | 56cd646 | 2013-06-07 15:09:15 -0700 | [diff] [blame] | 301 | rawBuilder.append(' '); |
| 302 | logBuilder.append(' '); |
| 303 | |
| 304 | appendEscaped(rawBuilder, argString); |
| 305 | if (arg instanceof SensitiveArg) { |
| 306 | logBuilder.append("[scrubbed]"); |
| 307 | } else { |
| 308 | appendEscaped(logBuilder, argString); |
| 309 | } |
Jeff Sharkey | 31c6e48 | 2011-11-18 17:09:01 -0800 | [diff] [blame] | 310 | } |
Jeff Sharkey | 56cd646 | 2013-06-07 15:09:15 -0700 | [diff] [blame] | 311 | |
| 312 | rawBuilder.append('\0'); |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 313 | } |
San Mehat | deba693 | 2010-01-20 15:14:31 -0800 | [diff] [blame] | 314 | |
Jeff Sharkey | fa23c5a | 2011-08-09 21:44:24 -0700 | [diff] [blame] | 315 | /** |
Jeff Sharkey | ba2896e | 2011-11-30 18:13:54 -0800 | [diff] [blame] | 316 | * Issue the given command to the native daemon and return a single expected |
| 317 | * response. |
| 318 | * |
| 319 | * @throws NativeDaemonConnectorException when problem communicating with |
| 320 | * native daemon, or if the response matches |
| 321 | * {@link NativeDaemonEvent#isClassClientError()} or |
| 322 | * {@link NativeDaemonEvent#isClassServerError()}. |
San Mehat | deba693 | 2010-01-20 15:14:31 -0800 | [diff] [blame] | 323 | */ |
Jeff Sharkey | ba2896e | 2011-11-30 18:13:54 -0800 | [diff] [blame] | 324 | public NativeDaemonEvent execute(Command cmd) throws NativeDaemonConnectorException { |
| 325 | return execute(cmd.mCmd, cmd.mArguments.toArray()); |
| 326 | } |
| 327 | |
| 328 | /** |
| 329 | * Issue the given command to the native daemon and return a single expected |
Jeff Sharkey | 7b4596f | 2013-02-25 10:55:29 -0800 | [diff] [blame] | 330 | * response. Any arguments must be separated from base command so they can |
| 331 | * be properly escaped. |
Jeff Sharkey | ba2896e | 2011-11-30 18:13:54 -0800 | [diff] [blame] | 332 | * |
| 333 | * @throws NativeDaemonConnectorException when problem communicating with |
| 334 | * native daemon, or if the response matches |
| 335 | * {@link NativeDaemonEvent#isClassClientError()} or |
| 336 | * {@link NativeDaemonEvent#isClassServerError()}. |
| 337 | */ |
| 338 | public NativeDaemonEvent execute(String cmd, Object... args) |
| 339 | throws NativeDaemonConnectorException { |
Jeff Sharkey | 14cbe52 | 2015-07-08 14:06:37 -0700 | [diff] [blame] | 340 | return execute(DEFAULT_TIMEOUT, cmd, args); |
| 341 | } |
| 342 | |
| 343 | public NativeDaemonEvent execute(long timeoutMs, String cmd, Object... args) |
| 344 | throws NativeDaemonConnectorException { |
| 345 | final NativeDaemonEvent[] events = executeForList(timeoutMs, cmd, args); |
Jeff Sharkey | ba2896e | 2011-11-30 18:13:54 -0800 | [diff] [blame] | 346 | if (events.length != 1) { |
| 347 | throw new NativeDaemonConnectorException( |
| 348 | "Expected exactly one response, but received " + events.length); |
| 349 | } |
| 350 | return events[0]; |
| 351 | } |
| 352 | |
| 353 | /** |
| 354 | * Issue the given command to the native daemon and return any |
| 355 | * {@link NativeDaemonEvent#isClassContinue()} responses, including the |
| 356 | * final terminal response. |
| 357 | * |
| 358 | * @throws NativeDaemonConnectorException when problem communicating with |
| 359 | * native daemon, or if the response matches |
| 360 | * {@link NativeDaemonEvent#isClassClientError()} or |
| 361 | * {@link NativeDaemonEvent#isClassServerError()}. |
| 362 | */ |
| 363 | public NativeDaemonEvent[] executeForList(Command cmd) throws NativeDaemonConnectorException { |
| 364 | return executeForList(cmd.mCmd, cmd.mArguments.toArray()); |
| 365 | } |
| 366 | |
| 367 | /** |
| 368 | * Issue the given command to the native daemon and return any |
| 369 | * {@link NativeDaemonEvent#isClassContinue()} responses, including the |
Jeff Sharkey | 7b4596f | 2013-02-25 10:55:29 -0800 | [diff] [blame] | 370 | * final terminal response. Any arguments must be separated from base |
| 371 | * command so they can be properly escaped. |
Jeff Sharkey | ba2896e | 2011-11-30 18:13:54 -0800 | [diff] [blame] | 372 | * |
| 373 | * @throws NativeDaemonConnectorException when problem communicating with |
| 374 | * native daemon, or if the response matches |
| 375 | * {@link NativeDaemonEvent#isClassClientError()} or |
| 376 | * {@link NativeDaemonEvent#isClassServerError()}. |
| 377 | */ |
| 378 | public NativeDaemonEvent[] executeForList(String cmd, Object... args) |
San Mehat | 4c27e0e | 2010-01-29 05:22:17 -0800 | [diff] [blame] | 379 | throws NativeDaemonConnectorException { |
Jeff Sharkey | 14cbe52 | 2015-07-08 14:06:37 -0700 | [diff] [blame] | 380 | return executeForList(DEFAULT_TIMEOUT, cmd, args); |
Jeff Sharkey | 31c6e48 | 2011-11-18 17:09:01 -0800 | [diff] [blame] | 381 | } |
San Mehat | deba693 | 2010-01-20 15:14:31 -0800 | [diff] [blame] | 382 | |
Robert Greenwalt | 470007f | 2012-02-07 11:36:55 -0800 | [diff] [blame] | 383 | /** |
Jeff Sharkey | 7b4596f | 2013-02-25 10:55:29 -0800 | [diff] [blame] | 384 | * Issue the given command to the native daemon and return any {@linke |
| 385 | * NativeDaemonEvent@isClassContinue()} responses, including the final |
| 386 | * terminal response. Note that the timeout does not count time in deep |
| 387 | * sleep. Any arguments must be separated from base command so they can be |
| 388 | * properly escaped. |
Robert Greenwalt | 470007f | 2012-02-07 11:36:55 -0800 | [diff] [blame] | 389 | * |
| 390 | * @throws NativeDaemonConnectorException when problem communicating with |
| 391 | * native daemon, or if the response matches |
| 392 | * {@link NativeDaemonEvent#isClassClientError()} or |
| 393 | * {@link NativeDaemonEvent#isClassServerError()}. |
| 394 | */ |
Jeff Sharkey | 14cbe52 | 2015-07-08 14:06:37 -0700 | [diff] [blame] | 395 | public NativeDaemonEvent[] executeForList(long timeoutMs, String cmd, Object... args) |
Jeff Sharkey | 31c6e48 | 2011-11-18 17:09:01 -0800 | [diff] [blame] | 396 | throws NativeDaemonConnectorException { |
Robert Greenwalt | 5a0c320 | 2012-05-22 16:07:46 -0700 | [diff] [blame] | 397 | final long startTime = SystemClock.elapsedRealtime(); |
Robert Greenwalt | d192598 | 2012-03-12 15:37:40 -0700 | [diff] [blame] | 398 | |
Jeff Sharkey | 56cd646 | 2013-06-07 15:09:15 -0700 | [diff] [blame] | 399 | final ArrayList<NativeDaemonEvent> events = Lists.newArrayList(); |
Robert Greenwalt | d192598 | 2012-03-12 15:37:40 -0700 | [diff] [blame] | 400 | |
Jeff Sharkey | 56cd646 | 2013-06-07 15:09:15 -0700 | [diff] [blame] | 401 | final StringBuilder rawBuilder = new StringBuilder(); |
| 402 | final StringBuilder logBuilder = new StringBuilder(); |
| 403 | final int sequenceNumber = mSequenceNumber.incrementAndGet(); |
| 404 | |
| 405 | makeCommand(rawBuilder, logBuilder, sequenceNumber, cmd, args); |
| 406 | |
| 407 | final String rawCmd = rawBuilder.toString(); |
| 408 | final String logCmd = logBuilder.toString(); |
| 409 | |
Robert Greenwalt | d192598 | 2012-03-12 15:37:40 -0700 | [diff] [blame] | 410 | log("SND -> {" + logCmd + "}"); |
| 411 | |
Robert Greenwalt | d192598 | 2012-03-12 15:37:40 -0700 | [diff] [blame] | 412 | synchronized (mDaemonLock) { |
| 413 | if (mOutputStream == null) { |
| 414 | throw new NativeDaemonConnectorException("missing output stream"); |
| 415 | } else { |
| 416 | try { |
Elliott Hughes | b829283 | 2013-06-28 16:50:13 -0700 | [diff] [blame] | 417 | mOutputStream.write(rawCmd.getBytes(StandardCharsets.UTF_8)); |
Robert Greenwalt | d192598 | 2012-03-12 15:37:40 -0700 | [diff] [blame] | 418 | } catch (IOException e) { |
| 419 | throw new NativeDaemonConnectorException("problem sending command", e); |
| 420 | } |
| 421 | } |
| 422 | } |
Jeff Sharkey | 31c6e48 | 2011-11-18 17:09:01 -0800 | [diff] [blame] | 423 | |
| 424 | NativeDaemonEvent event = null; |
| 425 | do { |
Jeff Sharkey | 14cbe52 | 2015-07-08 14:06:37 -0700 | [diff] [blame] | 426 | event = mResponseQueue.remove(sequenceNumber, timeoutMs, logCmd); |
Robert Greenwalt | 470007f | 2012-02-07 11:36:55 -0800 | [diff] [blame] | 427 | if (event == null) { |
Robert Greenwalt | d192598 | 2012-03-12 15:37:40 -0700 | [diff] [blame] | 428 | loge("timed-out waiting for response to " + logCmd); |
Todd Kennedy | 8101ee6 | 2015-06-23 13:35:28 -0700 | [diff] [blame] | 429 | throw new NativeDaemonTimeoutException(logCmd, event); |
Jeff Sharkey | 31c6e48 | 2011-11-18 17:09:01 -0800 | [diff] [blame] | 430 | } |
Robert Greenwalt | 7f44ff8 | 2014-05-07 23:49:08 -0700 | [diff] [blame] | 431 | if (VDBG) log("RMV <- {" + event + "}"); |
Jeff Sharkey | 31c6e48 | 2011-11-18 17:09:01 -0800 | [diff] [blame] | 432 | events.add(event); |
| 433 | } while (event.isClassContinue()); |
| 434 | |
Robert Greenwalt | 5a0c320 | 2012-05-22 16:07:46 -0700 | [diff] [blame] | 435 | final long endTime = SystemClock.elapsedRealtime(); |
| 436 | if (endTime - startTime > WARN_EXECUTE_DELAY_MS) { |
| 437 | loge("NDC Command {" + logCmd + "} took too long (" + (endTime - startTime) + "ms)"); |
| 438 | } |
| 439 | |
Jeff Sharkey | 31c6e48 | 2011-11-18 17:09:01 -0800 | [diff] [blame] | 440 | if (event.isClassClientError()) { |
Robert Greenwalt | d192598 | 2012-03-12 15:37:40 -0700 | [diff] [blame] | 441 | throw new NativeDaemonArgumentException(logCmd, event); |
Jeff Sharkey | 31c6e48 | 2011-11-18 17:09:01 -0800 | [diff] [blame] | 442 | } |
| 443 | if (event.isClassServerError()) { |
Robert Greenwalt | d192598 | 2012-03-12 15:37:40 -0700 | [diff] [blame] | 444 | throw new NativeDaemonFailureException(logCmd, event); |
Jeff Sharkey | 31c6e48 | 2011-11-18 17:09:01 -0800 | [diff] [blame] | 445 | } |
| 446 | |
| 447 | return events.toArray(new NativeDaemonEvent[events.size()]); |
| 448 | } |
| 449 | |
| 450 | /** |
Jeff Sharkey | 31c6e48 | 2011-11-18 17:09:01 -0800 | [diff] [blame] | 451 | * Append the given argument to {@link StringBuilder}, escaping as needed, |
| 452 | * and surrounding with quotes when it contains spaces. |
| 453 | */ |
Jeff Sharkey | 8b2c3a14 | 2012-11-12 11:45:05 -0800 | [diff] [blame] | 454 | @VisibleForTesting |
Jeff Sharkey | 31c6e48 | 2011-11-18 17:09:01 -0800 | [diff] [blame] | 455 | static void appendEscaped(StringBuilder builder, String arg) { |
| 456 | final boolean hasSpaces = arg.indexOf(' ') >= 0; |
| 457 | if (hasSpaces) { |
| 458 | builder.append('"'); |
| 459 | } |
| 460 | |
| 461 | final int length = arg.length(); |
| 462 | for (int i = 0; i < length; i++) { |
| 463 | final char c = arg.charAt(i); |
| 464 | |
| 465 | if (c == '"') { |
| 466 | builder.append("\\\""); |
| 467 | } else if (c == '\\') { |
| 468 | builder.append("\\\\"); |
| 469 | } else { |
| 470 | builder.append(c); |
| 471 | } |
| 472 | } |
| 473 | |
| 474 | if (hasSpaces) { |
| 475 | builder.append('"'); |
| 476 | } |
| 477 | } |
| 478 | |
| 479 | private static class NativeDaemonArgumentException extends NativeDaemonConnectorException { |
| 480 | public NativeDaemonArgumentException(String command, NativeDaemonEvent event) { |
| 481 | super(command, event); |
| 482 | } |
| 483 | |
| 484 | @Override |
| 485 | public IllegalArgumentException rethrowAsParcelableException() { |
| 486 | throw new IllegalArgumentException(getMessage(), this); |
| 487 | } |
| 488 | } |
| 489 | |
| 490 | private static class NativeDaemonFailureException extends NativeDaemonConnectorException { |
| 491 | public NativeDaemonFailureException(String command, NativeDaemonEvent event) { |
| 492 | super(command, event); |
| 493 | } |
San Mehat | deba693 | 2010-01-20 15:14:31 -0800 | [diff] [blame] | 494 | } |
Jeff Sharkey | fa23c5a | 2011-08-09 21:44:24 -0700 | [diff] [blame] | 495 | |
Jeff Sharkey | ba2896e | 2011-11-30 18:13:54 -0800 | [diff] [blame] | 496 | /** |
Jeff Sharkey | 7b4596f | 2013-02-25 10:55:29 -0800 | [diff] [blame] | 497 | * Command builder that handles argument list building. Any arguments must |
| 498 | * be separated from base command so they can be properly escaped. |
Jeff Sharkey | ba2896e | 2011-11-30 18:13:54 -0800 | [diff] [blame] | 499 | */ |
| 500 | public static class Command { |
| 501 | private String mCmd; |
| 502 | private ArrayList<Object> mArguments = Lists.newArrayList(); |
| 503 | |
| 504 | public Command(String cmd, Object... args) { |
| 505 | mCmd = cmd; |
| 506 | for (Object arg : args) { |
| 507 | appendArg(arg); |
| 508 | } |
| 509 | } |
| 510 | |
| 511 | public Command appendArg(Object arg) { |
| 512 | mArguments.add(arg); |
| 513 | return this; |
| 514 | } |
| 515 | } |
| 516 | |
Jeff Sharkey | fa23c5a | 2011-08-09 21:44:24 -0700 | [diff] [blame] | 517 | /** {@inheritDoc} */ |
| 518 | public void monitor() { |
| 519 | synchronized (mDaemonLock) { } |
| 520 | } |
Robert Greenwalt | 470fd72 | 2012-01-18 12:51:15 -0800 | [diff] [blame] | 521 | |
| 522 | public void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 523 | mLocalLog.dump(fd, pw, args); |
Robert Greenwalt | 470007f | 2012-02-07 11:36:55 -0800 | [diff] [blame] | 524 | pw.println(); |
| 525 | mResponseQueue.dump(fd, pw, args); |
Robert Greenwalt | 470fd72 | 2012-01-18 12:51:15 -0800 | [diff] [blame] | 526 | } |
| 527 | |
| 528 | private void log(String logstring) { |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 529 | if (mDebug) Slog.d(TAG, logstring); |
Robert Greenwalt | 470fd72 | 2012-01-18 12:51:15 -0800 | [diff] [blame] | 530 | mLocalLog.log(logstring); |
| 531 | } |
Robert Greenwalt | 470007f | 2012-02-07 11:36:55 -0800 | [diff] [blame] | 532 | |
| 533 | private void loge(String logstring) { |
| 534 | Slog.e(TAG, logstring); |
| 535 | mLocalLog.log(logstring); |
| 536 | } |
| 537 | |
| 538 | private static class ResponseQueue { |
| 539 | |
Robert Greenwalt | ef21599 | 2012-06-05 11:48:40 -0700 | [diff] [blame] | 540 | private static class PendingCmd { |
Jeff Sharkey | 56cd646 | 2013-06-07 15:09:15 -0700 | [diff] [blame] | 541 | public final int cmdNum; |
| 542 | public final String logCmd; |
| 543 | |
Robert Greenwalt | ef21599 | 2012-06-05 11:48:40 -0700 | [diff] [blame] | 544 | public BlockingQueue<NativeDaemonEvent> responses = |
| 545 | new ArrayBlockingQueue<NativeDaemonEvent>(10); |
Robert Greenwalt | ef21599 | 2012-06-05 11:48:40 -0700 | [diff] [blame] | 546 | |
| 547 | // The availableResponseCount member is used to track when we can remove this |
| 548 | // instance from the ResponseQueue. |
| 549 | // This is used under the protection of a sync of the mPendingCmds object. |
| 550 | // A positive value means we've had more writers retreive this object while |
| 551 | // a negative value means we've had more readers. When we've had an equal number |
| 552 | // (it goes to zero) we can remove this object from the mPendingCmds list. |
| 553 | // Note that we may have more responses for this command (and more readers |
| 554 | // coming), but that would result in a new PendingCmd instance being created |
| 555 | // and added with the same cmdNum. |
| 556 | // Also note that when this goes to zero it just means a parity of readers and |
| 557 | // writers have retrieved this object - not that they are done using it. The |
| 558 | // responses queue may well have more responses yet to be read or may get more |
| 559 | // responses added to it. But all those readers/writers have retreived and |
| 560 | // hold references to this instance already so it can be removed from |
| 561 | // mPendingCmds queue. |
| 562 | public int availableResponseCount; |
Jeff Sharkey | 56cd646 | 2013-06-07 15:09:15 -0700 | [diff] [blame] | 563 | |
| 564 | public PendingCmd(int cmdNum, String logCmd) { |
| 565 | this.cmdNum = cmdNum; |
| 566 | this.logCmd = logCmd; |
| 567 | } |
Robert Greenwalt | 470007f | 2012-02-07 11:36:55 -0800 | [diff] [blame] | 568 | } |
| 569 | |
Robert Greenwalt | ef21599 | 2012-06-05 11:48:40 -0700 | [diff] [blame] | 570 | private final LinkedList<PendingCmd> mPendingCmds; |
Robert Greenwalt | 470007f | 2012-02-07 11:36:55 -0800 | [diff] [blame] | 571 | private int mMaxCount; |
| 572 | |
| 573 | ResponseQueue(int maxCount) { |
Robert Greenwalt | ef21599 | 2012-06-05 11:48:40 -0700 | [diff] [blame] | 574 | mPendingCmds = new LinkedList<PendingCmd>(); |
Robert Greenwalt | 470007f | 2012-02-07 11:36:55 -0800 | [diff] [blame] | 575 | mMaxCount = maxCount; |
| 576 | } |
| 577 | |
| 578 | public void add(int cmdNum, NativeDaemonEvent response) { |
Robert Greenwalt | ef21599 | 2012-06-05 11:48:40 -0700 | [diff] [blame] | 579 | PendingCmd found = null; |
| 580 | synchronized (mPendingCmds) { |
| 581 | for (PendingCmd pendingCmd : mPendingCmds) { |
| 582 | if (pendingCmd.cmdNum == cmdNum) { |
| 583 | found = pendingCmd; |
Robert Greenwalt | 470007f | 2012-02-07 11:36:55 -0800 | [diff] [blame] | 584 | break; |
| 585 | } |
| 586 | } |
| 587 | if (found == null) { |
| 588 | // didn't find it - make sure our queue isn't too big before adding |
Robert Greenwalt | ef21599 | 2012-06-05 11:48:40 -0700 | [diff] [blame] | 589 | while (mPendingCmds.size() >= mMaxCount) { |
Robert Greenwalt | 470007f | 2012-02-07 11:36:55 -0800 | [diff] [blame] | 590 | Slog.e("NativeDaemonConnector.ResponseQueue", |
Robert Greenwalt | ef21599 | 2012-06-05 11:48:40 -0700 | [diff] [blame] | 591 | "more buffered than allowed: " + mPendingCmds.size() + |
Robert Greenwalt | 470007f | 2012-02-07 11:36:55 -0800 | [diff] [blame] | 592 | " >= " + mMaxCount); |
| 593 | // let any waiter timeout waiting for this |
Robert Greenwalt | ef21599 | 2012-06-05 11:48:40 -0700 | [diff] [blame] | 594 | PendingCmd pendingCmd = mPendingCmds.remove(); |
Robert Greenwalt | 470007f | 2012-02-07 11:36:55 -0800 | [diff] [blame] | 595 | Slog.e("NativeDaemonConnector.ResponseQueue", |
Jeff Sharkey | 56cd646 | 2013-06-07 15:09:15 -0700 | [diff] [blame] | 596 | "Removing request: " + pendingCmd.logCmd + " (" + |
Robert Greenwalt | ef21599 | 2012-06-05 11:48:40 -0700 | [diff] [blame] | 597 | pendingCmd.cmdNum + ")"); |
Robert Greenwalt | 470007f | 2012-02-07 11:36:55 -0800 | [diff] [blame] | 598 | } |
Robert Greenwalt | ef21599 | 2012-06-05 11:48:40 -0700 | [diff] [blame] | 599 | found = new PendingCmd(cmdNum, null); |
| 600 | mPendingCmds.add(found); |
Robert Greenwalt | 470007f | 2012-02-07 11:36:55 -0800 | [diff] [blame] | 601 | } |
Robert Greenwalt | ef21599 | 2012-06-05 11:48:40 -0700 | [diff] [blame] | 602 | found.availableResponseCount++; |
| 603 | // if a matching remove call has already retrieved this we can remove this |
| 604 | // instance from our list |
| 605 | if (found.availableResponseCount == 0) mPendingCmds.remove(found); |
Robert Greenwalt | 470007f | 2012-02-07 11:36:55 -0800 | [diff] [blame] | 606 | } |
Robert Greenwalt | ef21599 | 2012-06-05 11:48:40 -0700 | [diff] [blame] | 607 | try { |
| 608 | found.responses.put(response); |
| 609 | } catch (InterruptedException e) { } |
Robert Greenwalt | 470007f | 2012-02-07 11:36:55 -0800 | [diff] [blame] | 610 | } |
| 611 | |
| 612 | // note that the timeout does not count time in deep sleep. If you don't want |
| 613 | // the device to sleep, hold a wakelock |
Jeff Sharkey | 14cbe52 | 2015-07-08 14:06:37 -0700 | [diff] [blame] | 614 | public NativeDaemonEvent remove(int cmdNum, long timeoutMs, String logCmd) { |
Robert Greenwalt | ef21599 | 2012-06-05 11:48:40 -0700 | [diff] [blame] | 615 | PendingCmd found = null; |
| 616 | synchronized (mPendingCmds) { |
| 617 | for (PendingCmd pendingCmd : mPendingCmds) { |
| 618 | if (pendingCmd.cmdNum == cmdNum) { |
| 619 | found = pendingCmd; |
| 620 | break; |
Robert Greenwalt | 470007f | 2012-02-07 11:36:55 -0800 | [diff] [blame] | 621 | } |
| 622 | } |
Robert Greenwalt | ef21599 | 2012-06-05 11:48:40 -0700 | [diff] [blame] | 623 | if (found == null) { |
Jeff Sharkey | 56cd646 | 2013-06-07 15:09:15 -0700 | [diff] [blame] | 624 | found = new PendingCmd(cmdNum, logCmd); |
Robert Greenwalt | ef21599 | 2012-06-05 11:48:40 -0700 | [diff] [blame] | 625 | mPendingCmds.add(found); |
Robert Greenwalt | 470007f | 2012-02-07 11:36:55 -0800 | [diff] [blame] | 626 | } |
Robert Greenwalt | ef21599 | 2012-06-05 11:48:40 -0700 | [diff] [blame] | 627 | found.availableResponseCount--; |
| 628 | // if a matching add call has already retrieved this we can remove this |
| 629 | // instance from our list |
| 630 | if (found.availableResponseCount == 0) mPendingCmds.remove(found); |
Robert Greenwalt | 470007f | 2012-02-07 11:36:55 -0800 | [diff] [blame] | 631 | } |
Robert Greenwalt | ef21599 | 2012-06-05 11:48:40 -0700 | [diff] [blame] | 632 | NativeDaemonEvent result = null; |
| 633 | try { |
| 634 | result = found.responses.poll(timeoutMs, TimeUnit.MILLISECONDS); |
| 635 | } catch (InterruptedException e) {} |
| 636 | if (result == null) { |
| 637 | Slog.e("NativeDaemonConnector.ResponseQueue", "Timeout waiting for response"); |
| 638 | } |
| 639 | return result; |
Robert Greenwalt | 470007f | 2012-02-07 11:36:55 -0800 | [diff] [blame] | 640 | } |
| 641 | |
| 642 | public void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 643 | pw.println("Pending requests:"); |
Robert Greenwalt | ef21599 | 2012-06-05 11:48:40 -0700 | [diff] [blame] | 644 | synchronized (mPendingCmds) { |
| 645 | for (PendingCmd pendingCmd : mPendingCmds) { |
Jeff Sharkey | 56cd646 | 2013-06-07 15:09:15 -0700 | [diff] [blame] | 646 | pw.println(" Cmd " + pendingCmd.cmdNum + " - " + pendingCmd.logCmd); |
Robert Greenwalt | 470007f | 2012-02-07 11:36:55 -0800 | [diff] [blame] | 647 | } |
| 648 | } |
| 649 | } |
| 650 | } |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 651 | } |