Irfan Sheriff | 7d024d3 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 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 android.net.nsd; |
| 18 | |
Hugo Benichi | ff3e6cc | 2017-05-02 13:36:28 +0900 | [diff] [blame] | 19 | import static com.android.internal.util.Preconditions.checkArgument; |
| 20 | import static com.android.internal.util.Preconditions.checkNotNull; |
| 21 | import static com.android.internal.util.Preconditions.checkStringNotEmpty; |
| 22 | |
Irfan Sheriff | 3ef889b | 2012-04-17 23:15:29 -0700 | [diff] [blame] | 23 | import android.annotation.SdkConstant; |
| 24 | import android.annotation.SdkConstant.SdkConstantType; |
Hugo Benichi | e062ae0 | 2017-07-31 20:35:58 +0900 | [diff] [blame] | 25 | import android.annotation.SystemService; |
Irfan Sheriff | 7d024d3 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 26 | import android.content.Context; |
Irfan Sheriff | 7d024d3 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 27 | import android.os.Handler; |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 28 | import android.os.HandlerThread; |
Irfan Sheriff | 7d024d3 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 29 | import android.os.Looper; |
| 30 | import android.os.Message; |
Irfan Sheriff | 7d024d3 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 31 | import android.os.Messenger; |
Hugo Benichi | e062ae0 | 2017-07-31 20:35:58 +0900 | [diff] [blame] | 32 | import android.os.RemoteException; |
Irfan Sheriff | 7d024d3 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 33 | import android.util.Log; |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 34 | import android.util.SparseArray; |
| 35 | |
Hugo Benichi | db8adb7 | 2017-04-17 15:27:52 +0900 | [diff] [blame] | 36 | import com.android.internal.annotations.VisibleForTesting; |
Irfan Sheriff | 7d024d3 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 37 | import com.android.internal.util.AsyncChannel; |
| 38 | import com.android.internal.util.Protocol; |
| 39 | |
Hugo Benichi | e062ae0 | 2017-07-31 20:35:58 +0900 | [diff] [blame] | 40 | import java.util.concurrent.CountDownLatch; |
| 41 | |
Irfan Sheriff | 7d024d3 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 42 | /** |
Irfan Sheriff | 9278467 | 2012-04-13 12:15:41 -0700 | [diff] [blame] | 43 | * The Network Service Discovery Manager class provides the API to discover services |
| 44 | * on a network. As an example, if device A and device B are connected over a Wi-Fi |
| 45 | * network, a game registered on device A can be discovered by a game on device |
| 46 | * B. Another example use case is an application discovering printers on the network. |
| 47 | * |
| 48 | * <p> The API currently supports DNS based service discovery and discovery is currently |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 49 | * limited to a local network over Multicast DNS. DNS service discovery is described at |
| 50 | * http://files.dns-sd.org/draft-cheshire-dnsext-dns-sd.txt |
Irfan Sheriff | 7d024d3 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 51 | * |
| 52 | * <p> The API is asynchronous and responses to requests from an application are on listener |
Hugo Benichi | 2183ba9 | 2017-04-05 14:06:11 +0900 | [diff] [blame] | 53 | * callbacks on a seperate internal thread. |
Irfan Sheriff | 7d024d3 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 54 | * |
Irfan Sheriff | 9278467 | 2012-04-13 12:15:41 -0700 | [diff] [blame] | 55 | * <p> There are three main operations the API supports - registration, discovery and resolution. |
| 56 | * <pre> |
| 57 | * Application start |
| 58 | * | |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 59 | * | |
| 60 | * | onServiceRegistered() |
| 61 | * Register any local services / |
| 62 | * to be advertised with \ |
| 63 | * registerService() onRegistrationFailed() |
| 64 | * | |
| 65 | * | |
| 66 | * discoverServices() |
| 67 | * | |
| 68 | * Maintain a list to track |
| 69 | * discovered services |
| 70 | * | |
| 71 | * |---------> |
| 72 | * | | |
| 73 | * | onServiceFound() |
| 74 | * | | |
| 75 | * | add service to list |
| 76 | * | | |
| 77 | * |<---------- |
| 78 | * | |
| 79 | * |---------> |
| 80 | * | | |
| 81 | * | onServiceLost() |
| 82 | * | | |
| 83 | * | remove service from list |
| 84 | * | | |
| 85 | * |<---------- |
| 86 | * | |
| 87 | * | |
| 88 | * | Connect to a service |
| 89 | * | from list ? |
| 90 | * | |
| 91 | * resolveService() |
| 92 | * | |
| 93 | * onServiceResolved() |
| 94 | * | |
| 95 | * Establish connection to service |
| 96 | * with the host and port information |
Irfan Sheriff | 9278467 | 2012-04-13 12:15:41 -0700 | [diff] [blame] | 97 | * |
| 98 | * </pre> |
| 99 | * An application that needs to advertise itself over a network for other applications to |
| 100 | * discover it can do so with a call to {@link #registerService}. If Example is a http based |
| 101 | * application that can provide HTML data to peer services, it can register a name "Example" |
| 102 | * with service type "_http._tcp". A successful registration is notified with a callback to |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 103 | * {@link RegistrationListener#onServiceRegistered} and a failure to register is notified |
| 104 | * over {@link RegistrationListener#onRegistrationFailed} |
Irfan Sheriff | 9278467 | 2012-04-13 12:15:41 -0700 | [diff] [blame] | 105 | * |
| 106 | * <p> A peer application looking for http services can initiate a discovery for "_http._tcp" |
| 107 | * with a call to {@link #discoverServices}. A service found is notified with a callback |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 108 | * to {@link DiscoveryListener#onServiceFound} and a service lost is notified on |
| 109 | * {@link DiscoveryListener#onServiceLost}. |
Irfan Sheriff | 9278467 | 2012-04-13 12:15:41 -0700 | [diff] [blame] | 110 | * |
Philip P. Moltmann | 29154b0 | 2016-04-18 16:23:06 -0700 | [diff] [blame] | 111 | * <p> Once the peer application discovers the "Example" http service, and either needs to read the |
| 112 | * attributes of the service or wants to receive data from the "Example" application, it can |
| 113 | * initiate a resolve with {@link #resolveService} to resolve the attributes, host, and port |
| 114 | * details. A successful resolve is notified on {@link ResolveListener#onServiceResolved} and a |
| 115 | * failure is notified on {@link ResolveListener#onResolveFailed}. |
Irfan Sheriff | 9278467 | 2012-04-13 12:15:41 -0700 | [diff] [blame] | 116 | * |
| 117 | * Applications can reserve for a service type at |
| 118 | * http://www.iana.org/form/ports-service. Existing services can be found at |
| 119 | * http://www.iana.org/assignments/service-names-port-numbers/service-names-port-numbers.xml |
Irfan Sheriff | 7d024d3 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 120 | * |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 121 | * {@see NsdServiceInfo} |
Irfan Sheriff | 7d024d3 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 122 | */ |
Jeff Sharkey | d86b8fe | 2017-06-02 17:36:26 -0600 | [diff] [blame] | 123 | @SystemService(Context.NSD_SERVICE) |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 124 | public final class NsdManager { |
Hugo Benichi | 2183ba9 | 2017-04-05 14:06:11 +0900 | [diff] [blame] | 125 | private static final String TAG = NsdManager.class.getSimpleName(); |
| 126 | private static final boolean DBG = false; |
Irfan Sheriff | 7d024d3 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 127 | |
Irfan Sheriff | 3ef889b | 2012-04-17 23:15:29 -0700 | [diff] [blame] | 128 | /** |
| 129 | * Broadcast intent action to indicate whether network service discovery is |
| 130 | * enabled or disabled. An extra {@link #EXTRA_NSD_STATE} provides the state |
| 131 | * information as int. |
| 132 | * |
| 133 | * @see #EXTRA_NSD_STATE |
Irfan Sheriff | 3ef889b | 2012-04-17 23:15:29 -0700 | [diff] [blame] | 134 | */ |
| 135 | @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) |
Hugo Benichi | 2183ba9 | 2017-04-05 14:06:11 +0900 | [diff] [blame] | 136 | public static final String ACTION_NSD_STATE_CHANGED = "android.net.nsd.STATE_CHANGED"; |
Irfan Sheriff | 3ef889b | 2012-04-17 23:15:29 -0700 | [diff] [blame] | 137 | |
| 138 | /** |
| 139 | * The lookup key for an int that indicates whether network service discovery is enabled |
| 140 | * or disabled. Retrieve it with {@link android.content.Intent#getIntExtra(String,int)}. |
| 141 | * |
| 142 | * @see #NSD_STATE_DISABLED |
| 143 | * @see #NSD_STATE_ENABLED |
Irfan Sheriff | 3ef889b | 2012-04-17 23:15:29 -0700 | [diff] [blame] | 144 | */ |
| 145 | public static final String EXTRA_NSD_STATE = "nsd_state"; |
| 146 | |
| 147 | /** |
| 148 | * Network service discovery is disabled |
| 149 | * |
Irfan Sheriff | 54ac7a5 | 2012-04-19 10:26:34 -0700 | [diff] [blame] | 150 | * @see #ACTION_NSD_STATE_CHANGED |
Irfan Sheriff | 3ef889b | 2012-04-17 23:15:29 -0700 | [diff] [blame] | 151 | */ |
| 152 | public static final int NSD_STATE_DISABLED = 1; |
| 153 | |
| 154 | /** |
| 155 | * Network service discovery is enabled |
| 156 | * |
Irfan Sheriff | 54ac7a5 | 2012-04-19 10:26:34 -0700 | [diff] [blame] | 157 | * @see #ACTION_NSD_STATE_CHANGED |
Irfan Sheriff | 3ef889b | 2012-04-17 23:15:29 -0700 | [diff] [blame] | 158 | */ |
| 159 | public static final int NSD_STATE_ENABLED = 2; |
| 160 | |
Irfan Sheriff | 7d024d3 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 161 | private static final int BASE = Protocol.BASE_NSD_MANAGER; |
| 162 | |
| 163 | /** @hide */ |
| 164 | public static final int DISCOVER_SERVICES = BASE + 1; |
| 165 | /** @hide */ |
| 166 | public static final int DISCOVER_SERVICES_STARTED = BASE + 2; |
| 167 | /** @hide */ |
| 168 | public static final int DISCOVER_SERVICES_FAILED = BASE + 3; |
| 169 | /** @hide */ |
| 170 | public static final int SERVICE_FOUND = BASE + 4; |
| 171 | /** @hide */ |
| 172 | public static final int SERVICE_LOST = BASE + 5; |
| 173 | |
| 174 | /** @hide */ |
| 175 | public static final int STOP_DISCOVERY = BASE + 6; |
| 176 | /** @hide */ |
| 177 | public static final int STOP_DISCOVERY_FAILED = BASE + 7; |
| 178 | /** @hide */ |
| 179 | public static final int STOP_DISCOVERY_SUCCEEDED = BASE + 8; |
| 180 | |
| 181 | /** @hide */ |
| 182 | public static final int REGISTER_SERVICE = BASE + 9; |
| 183 | /** @hide */ |
| 184 | public static final int REGISTER_SERVICE_FAILED = BASE + 10; |
| 185 | /** @hide */ |
| 186 | public static final int REGISTER_SERVICE_SUCCEEDED = BASE + 11; |
| 187 | |
| 188 | /** @hide */ |
Irfan Sheriff | 9278467 | 2012-04-13 12:15:41 -0700 | [diff] [blame] | 189 | public static final int UNREGISTER_SERVICE = BASE + 12; |
Irfan Sheriff | 7d024d3 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 190 | /** @hide */ |
Irfan Sheriff | 9278467 | 2012-04-13 12:15:41 -0700 | [diff] [blame] | 191 | public static final int UNREGISTER_SERVICE_FAILED = BASE + 13; |
Irfan Sheriff | 7d024d3 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 192 | /** @hide */ |
Irfan Sheriff | 9278467 | 2012-04-13 12:15:41 -0700 | [diff] [blame] | 193 | public static final int UNREGISTER_SERVICE_SUCCEEDED = BASE + 14; |
Irfan Sheriff | 7d024d3 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 194 | |
| 195 | /** @hide */ |
Irfan Sheriff | 9278467 | 2012-04-13 12:15:41 -0700 | [diff] [blame] | 196 | public static final int RESOLVE_SERVICE = BASE + 18; |
Irfan Sheriff | 817388e | 2012-04-11 14:52:19 -0700 | [diff] [blame] | 197 | /** @hide */ |
Irfan Sheriff | 9278467 | 2012-04-13 12:15:41 -0700 | [diff] [blame] | 198 | public static final int RESOLVE_SERVICE_FAILED = BASE + 19; |
Irfan Sheriff | 817388e | 2012-04-11 14:52:19 -0700 | [diff] [blame] | 199 | /** @hide */ |
Irfan Sheriff | 9278467 | 2012-04-13 12:15:41 -0700 | [diff] [blame] | 200 | public static final int RESOLVE_SERVICE_SUCCEEDED = BASE + 20; |
Irfan Sheriff | 817388e | 2012-04-11 14:52:19 -0700 | [diff] [blame] | 201 | |
Irfan Sheriff | 9278467 | 2012-04-13 12:15:41 -0700 | [diff] [blame] | 202 | /** @hide */ |
Irfan Sheriff | 3ef889b | 2012-04-17 23:15:29 -0700 | [diff] [blame] | 203 | public static final int ENABLE = BASE + 24; |
| 204 | /** @hide */ |
| 205 | public static final int DISABLE = BASE + 25; |
| 206 | |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 207 | /** @hide */ |
| 208 | public static final int NATIVE_DAEMON_EVENT = BASE + 26; |
| 209 | |
| 210 | /** Dns based service discovery protocol */ |
| 211 | public static final int PROTOCOL_DNS_SD = 0x0001; |
| 212 | |
Hugo Benichi | 2183ba9 | 2017-04-05 14:06:11 +0900 | [diff] [blame] | 213 | private static final SparseArray<String> EVENT_NAMES = new SparseArray<>(); |
| 214 | static { |
| 215 | EVENT_NAMES.put(DISCOVER_SERVICES, "DISCOVER_SERVICES"); |
| 216 | EVENT_NAMES.put(DISCOVER_SERVICES_STARTED, "DISCOVER_SERVICES_STARTED"); |
| 217 | EVENT_NAMES.put(DISCOVER_SERVICES_FAILED, "DISCOVER_SERVICES_FAILED"); |
| 218 | EVENT_NAMES.put(SERVICE_FOUND, "SERVICE_FOUND"); |
| 219 | EVENT_NAMES.put(SERVICE_LOST, "SERVICE_LOST"); |
| 220 | EVENT_NAMES.put(STOP_DISCOVERY, "STOP_DISCOVERY"); |
| 221 | EVENT_NAMES.put(STOP_DISCOVERY_FAILED, "STOP_DISCOVERY_FAILED"); |
| 222 | EVENT_NAMES.put(STOP_DISCOVERY_SUCCEEDED, "STOP_DISCOVERY_SUCCEEDED"); |
| 223 | EVENT_NAMES.put(REGISTER_SERVICE, "REGISTER_SERVICE"); |
| 224 | EVENT_NAMES.put(REGISTER_SERVICE_FAILED, "REGISTER_SERVICE_FAILED"); |
| 225 | EVENT_NAMES.put(REGISTER_SERVICE_SUCCEEDED, "REGISTER_SERVICE_SUCCEEDED"); |
| 226 | EVENT_NAMES.put(UNREGISTER_SERVICE, "UNREGISTER_SERVICE"); |
| 227 | EVENT_NAMES.put(UNREGISTER_SERVICE_FAILED, "UNREGISTER_SERVICE_FAILED"); |
| 228 | EVENT_NAMES.put(UNREGISTER_SERVICE_SUCCEEDED, "UNREGISTER_SERVICE_SUCCEEDED"); |
| 229 | EVENT_NAMES.put(RESOLVE_SERVICE, "RESOLVE_SERVICE"); |
| 230 | EVENT_NAMES.put(RESOLVE_SERVICE_FAILED, "RESOLVE_SERVICE_FAILED"); |
| 231 | EVENT_NAMES.put(RESOLVE_SERVICE_SUCCEEDED, "RESOLVE_SERVICE_SUCCEEDED"); |
| 232 | EVENT_NAMES.put(ENABLE, "ENABLE"); |
| 233 | EVENT_NAMES.put(DISABLE, "DISABLE"); |
| 234 | EVENT_NAMES.put(NATIVE_DAEMON_EVENT, "NATIVE_DAEMON_EVENT"); |
| 235 | } |
| 236 | |
| 237 | /** @hide */ |
| 238 | public static String nameOf(int event) { |
| 239 | String name = EVENT_NAMES.get(event); |
| 240 | if (name == null) { |
| 241 | return Integer.toString(event); |
| 242 | } |
| 243 | return name; |
| 244 | } |
| 245 | |
Hugo Benichi | e062ae0 | 2017-07-31 20:35:58 +0900 | [diff] [blame] | 246 | private static final int FIRST_LISTENER_KEY = 1; |
Hugo Benichi | ff3e6cc | 2017-05-02 13:36:28 +0900 | [diff] [blame] | 247 | |
Hugo Benichi | 2183ba9 | 2017-04-05 14:06:11 +0900 | [diff] [blame] | 248 | private final INsdManager mService; |
| 249 | private final Context mContext; |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 250 | |
Hugo Benichi | ff3e6cc | 2017-05-02 13:36:28 +0900 | [diff] [blame] | 251 | private int mListenerKey = FIRST_LISTENER_KEY; |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 252 | private final SparseArray mListenerMap = new SparseArray(); |
Hugo Benichi | 2183ba9 | 2017-04-05 14:06:11 +0900 | [diff] [blame] | 253 | private final SparseArray<NsdServiceInfo> mServiceMap = new SparseArray<>(); |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 254 | private final Object mMapLock = new Object(); |
| 255 | |
| 256 | private final AsyncChannel mAsyncChannel = new AsyncChannel(); |
| 257 | private ServiceHandler mHandler; |
| 258 | private final CountDownLatch mConnected = new CountDownLatch(1); |
Irfan Sheriff | 3ef889b | 2012-04-17 23:15:29 -0700 | [diff] [blame] | 259 | |
Irfan Sheriff | 7d024d3 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 260 | /** |
| 261 | * Create a new Nsd instance. Applications use |
| 262 | * {@link android.content.Context#getSystemService Context.getSystemService()} to retrieve |
| 263 | * {@link android.content.Context#NSD_SERVICE Context.NSD_SERVICE}. |
| 264 | * @param service the Binder interface |
| 265 | * @hide - hide this because it takes in a parameter of type INsdManager, which |
| 266 | * is a system private class. |
| 267 | */ |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 268 | public NsdManager(Context context, INsdManager service) { |
Irfan Sheriff | 7d024d3 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 269 | mService = service; |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 270 | mContext = context; |
| 271 | init(); |
Irfan Sheriff | 7d024d3 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | /** |
Hugo Benichi | ab5bdbf | 2017-04-28 15:31:10 +0900 | [diff] [blame] | 275 | * @hide |
| 276 | */ |
| 277 | @VisibleForTesting |
| 278 | public void disconnect() { |
| 279 | mAsyncChannel.disconnect(); |
Hugo Benichi | e062ae0 | 2017-07-31 20:35:58 +0900 | [diff] [blame] | 280 | mHandler.getLooper().quitSafely(); |
Hugo Benichi | ab5bdbf | 2017-04-28 15:31:10 +0900 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | /** |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 284 | * Failures are passed with {@link RegistrationListener#onRegistrationFailed}, |
| 285 | * {@link RegistrationListener#onUnregistrationFailed}, |
| 286 | * {@link DiscoveryListener#onStartDiscoveryFailed}, |
| 287 | * {@link DiscoveryListener#onStopDiscoveryFailed} or {@link ResolveListener#onResolveFailed}. |
| 288 | * |
Irfan Sheriff | 7d024d3 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 289 | * Indicates that the operation failed due to an internal error. |
| 290 | */ |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 291 | public static final int FAILURE_INTERNAL_ERROR = 0; |
Irfan Sheriff | 7d024d3 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 292 | |
| 293 | /** |
Irfan Sheriff | 817388e | 2012-04-11 14:52:19 -0700 | [diff] [blame] | 294 | * Indicates that the operation failed because it is already active. |
| 295 | */ |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 296 | public static final int FAILURE_ALREADY_ACTIVE = 3; |
Irfan Sheriff | 817388e | 2012-04-11 14:52:19 -0700 | [diff] [blame] | 297 | |
| 298 | /** |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 299 | * Indicates that the operation failed because the maximum outstanding |
| 300 | * requests from the applications have reached. |
Irfan Sheriff | 817388e | 2012-04-11 14:52:19 -0700 | [diff] [blame] | 301 | */ |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 302 | public static final int FAILURE_MAX_LIMIT = 4; |
Irfan Sheriff | 7d024d3 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 303 | |
Irfan Sheriff | 9278467 | 2012-04-13 12:15:41 -0700 | [diff] [blame] | 304 | /** Interface for callback invocation for service discovery */ |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 305 | public interface DiscoveryListener { |
Irfan Sheriff | 7d024d3 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 306 | |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 307 | public void onStartDiscoveryFailed(String serviceType, int errorCode); |
Irfan Sheriff | 7d024d3 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 308 | |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 309 | public void onStopDiscoveryFailed(String serviceType, int errorCode); |
Irfan Sheriff | 7d024d3 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 310 | |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 311 | public void onDiscoveryStarted(String serviceType); |
Irfan Sheriff | 7d024d3 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 312 | |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 313 | public void onDiscoveryStopped(String serviceType); |
| 314 | |
| 315 | public void onServiceFound(NsdServiceInfo serviceInfo); |
| 316 | |
| 317 | public void onServiceLost(NsdServiceInfo serviceInfo); |
Irfan Sheriff | 7d024d3 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 318 | } |
| 319 | |
Irfan Sheriff | 9278467 | 2012-04-13 12:15:41 -0700 | [diff] [blame] | 320 | /** Interface for callback invocation for service registration */ |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 321 | public interface RegistrationListener { |
Irfan Sheriff | 7d024d3 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 322 | |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 323 | public void onRegistrationFailed(NsdServiceInfo serviceInfo, int errorCode); |
Irfan Sheriff | 7d024d3 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 324 | |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 325 | public void onUnregistrationFailed(NsdServiceInfo serviceInfo, int errorCode); |
Irfan Sheriff | 7d024d3 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 326 | |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 327 | public void onServiceRegistered(NsdServiceInfo serviceInfo); |
Irfan Sheriff | 7d024d3 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 328 | |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 329 | public void onServiceUnregistered(NsdServiceInfo serviceInfo); |
Irfan Sheriff | 7d024d3 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 330 | } |
| 331 | |
Irfan Sheriff | 9278467 | 2012-04-13 12:15:41 -0700 | [diff] [blame] | 332 | /** Interface for callback invocation for service resolution */ |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 333 | public interface ResolveListener { |
Irfan Sheriff | 7d024d3 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 334 | |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 335 | public void onResolveFailed(NsdServiceInfo serviceInfo, int errorCode); |
Irfan Sheriff | 7d024d3 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 336 | |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 337 | public void onServiceResolved(NsdServiceInfo serviceInfo); |
Irfan Sheriff | 7d024d3 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 338 | } |
| 339 | |
Hugo Benichi | db8adb7 | 2017-04-17 15:27:52 +0900 | [diff] [blame] | 340 | @VisibleForTesting |
| 341 | class ServiceHandler extends Handler { |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 342 | ServiceHandler(Looper looper) { |
| 343 | super(looper); |
Irfan Sheriff | 7d024d3 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 344 | } |
Irfan Sheriff | 7d024d3 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 345 | |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 346 | @Override |
| 347 | public void handleMessage(Message message) { |
Hugo Benichi | ff3e6cc | 2017-05-02 13:36:28 +0900 | [diff] [blame] | 348 | final int what = message.what; |
| 349 | final int key = message.arg2; |
| 350 | switch (what) { |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 351 | case AsyncChannel.CMD_CHANNEL_HALF_CONNECTED: |
| 352 | mAsyncChannel.sendMessage(AsyncChannel.CMD_CHANNEL_FULL_CONNECTION); |
Dave Platt | 3fc376b | 2014-02-27 16:16:20 -0800 | [diff] [blame] | 353 | return; |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 354 | case AsyncChannel.CMD_CHANNEL_FULLY_CONNECTED: |
Robert Greenwalt | af2eefb | 2013-05-02 15:45:32 -0700 | [diff] [blame] | 355 | mConnected.countDown(); |
Dave Platt | 3fc376b | 2014-02-27 16:16:20 -0800 | [diff] [blame] | 356 | return; |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 357 | case AsyncChannel.CMD_CHANNEL_DISCONNECTED: |
| 358 | Log.e(TAG, "Channel lost"); |
Dave Platt | 3fc376b | 2014-02-27 16:16:20 -0800 | [diff] [blame] | 359 | return; |
| 360 | default: |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 361 | break; |
Dave Platt | 3fc376b | 2014-02-27 16:16:20 -0800 | [diff] [blame] | 362 | } |
Hugo Benichi | ff3e6cc | 2017-05-02 13:36:28 +0900 | [diff] [blame] | 363 | final Object listener; |
| 364 | final NsdServiceInfo ns; |
| 365 | synchronized (mMapLock) { |
| 366 | listener = mListenerMap.get(key); |
| 367 | ns = mServiceMap.get(key); |
| 368 | } |
Dave Platt | 3fc376b | 2014-02-27 16:16:20 -0800 | [diff] [blame] | 369 | if (listener == null) { |
| 370 | Log.d(TAG, "Stale key " + message.arg2); |
| 371 | return; |
| 372 | } |
Hugo Benichi | ff3e6cc | 2017-05-02 13:36:28 +0900 | [diff] [blame] | 373 | if (DBG) { |
| 374 | Log.d(TAG, "received " + nameOf(what) + " for key " + key + ", service " + ns); |
| 375 | } |
| 376 | switch (what) { |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 377 | case DISCOVER_SERVICES_STARTED: |
Dave Platt | 3fc376b | 2014-02-27 16:16:20 -0800 | [diff] [blame] | 378 | String s = getNsdServiceInfoType((NsdServiceInfo) message.obj); |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 379 | ((DiscoveryListener) listener).onDiscoveryStarted(s); |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 380 | break; |
| 381 | case DISCOVER_SERVICES_FAILED: |
Hugo Benichi | ff3e6cc | 2017-05-02 13:36:28 +0900 | [diff] [blame] | 382 | removeListener(key); |
Dave Platt | 3fc376b | 2014-02-27 16:16:20 -0800 | [diff] [blame] | 383 | ((DiscoveryListener) listener).onStartDiscoveryFailed(getNsdServiceInfoType(ns), |
| 384 | message.arg1); |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 385 | break; |
| 386 | case SERVICE_FOUND: |
| 387 | ((DiscoveryListener) listener).onServiceFound((NsdServiceInfo) message.obj); |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 388 | break; |
| 389 | case SERVICE_LOST: |
| 390 | ((DiscoveryListener) listener).onServiceLost((NsdServiceInfo) message.obj); |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 391 | break; |
| 392 | case STOP_DISCOVERY_FAILED: |
Hugo Benichi | 93f4591 | 2017-04-28 13:29:21 +0900 | [diff] [blame] | 393 | // TODO: failure to stop discovery should be internal and retried internally, as |
| 394 | // the effect for the client is indistinguishable from STOP_DISCOVERY_SUCCEEDED |
Hugo Benichi | ff3e6cc | 2017-05-02 13:36:28 +0900 | [diff] [blame] | 395 | removeListener(key); |
Dave Platt | 3fc376b | 2014-02-27 16:16:20 -0800 | [diff] [blame] | 396 | ((DiscoveryListener) listener).onStopDiscoveryFailed(getNsdServiceInfoType(ns), |
| 397 | message.arg1); |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 398 | break; |
| 399 | case STOP_DISCOVERY_SUCCEEDED: |
Hugo Benichi | ff3e6cc | 2017-05-02 13:36:28 +0900 | [diff] [blame] | 400 | removeListener(key); |
Dave Platt | 3fc376b | 2014-02-27 16:16:20 -0800 | [diff] [blame] | 401 | ((DiscoveryListener) listener).onDiscoveryStopped(getNsdServiceInfoType(ns)); |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 402 | break; |
| 403 | case REGISTER_SERVICE_FAILED: |
Hugo Benichi | ff3e6cc | 2017-05-02 13:36:28 +0900 | [diff] [blame] | 404 | removeListener(key); |
Dave Platt | 3fc376b | 2014-02-27 16:16:20 -0800 | [diff] [blame] | 405 | ((RegistrationListener) listener).onRegistrationFailed(ns, message.arg1); |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 406 | break; |
| 407 | case REGISTER_SERVICE_SUCCEEDED: |
| 408 | ((RegistrationListener) listener).onServiceRegistered( |
| 409 | (NsdServiceInfo) message.obj); |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 410 | break; |
| 411 | case UNREGISTER_SERVICE_FAILED: |
Hugo Benichi | ff3e6cc | 2017-05-02 13:36:28 +0900 | [diff] [blame] | 412 | removeListener(key); |
Dave Platt | 3fc376b | 2014-02-27 16:16:20 -0800 | [diff] [blame] | 413 | ((RegistrationListener) listener).onUnregistrationFailed(ns, message.arg1); |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 414 | break; |
| 415 | case UNREGISTER_SERVICE_SUCCEEDED: |
Hugo Benichi | 8c5eeb0 | 2017-04-28 11:01:22 +0900 | [diff] [blame] | 416 | // TODO: do not unregister listener until service is unregistered, or provide |
| 417 | // alternative way for unregistering ? |
Dave Platt | e7369bd | 2014-03-28 13:33:04 -0700 | [diff] [blame] | 418 | removeListener(message.arg2); |
Dave Platt | 3fc376b | 2014-02-27 16:16:20 -0800 | [diff] [blame] | 419 | ((RegistrationListener) listener).onServiceUnregistered(ns); |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 420 | break; |
| 421 | case RESOLVE_SERVICE_FAILED: |
Hugo Benichi | ff3e6cc | 2017-05-02 13:36:28 +0900 | [diff] [blame] | 422 | removeListener(key); |
Dave Platt | 3fc376b | 2014-02-27 16:16:20 -0800 | [diff] [blame] | 423 | ((ResolveListener) listener).onResolveFailed(ns, message.arg1); |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 424 | break; |
| 425 | case RESOLVE_SERVICE_SUCCEEDED: |
Hugo Benichi | ff3e6cc | 2017-05-02 13:36:28 +0900 | [diff] [blame] | 426 | removeListener(key); |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 427 | ((ResolveListener) listener).onServiceResolved((NsdServiceInfo) message.obj); |
| 428 | break; |
| 429 | default: |
| 430 | Log.d(TAG, "Ignored " + message); |
| 431 | break; |
Irfan Sheriff | 7d024d3 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 432 | } |
Irfan Sheriff | 7d024d3 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 433 | } |
Irfan Sheriff | 9278467 | 2012-04-13 12:15:41 -0700 | [diff] [blame] | 434 | } |
| 435 | |
Hugo Benichi | ff3e6cc | 2017-05-02 13:36:28 +0900 | [diff] [blame] | 436 | private int nextListenerKey() { |
| 437 | // Ensure mListenerKey >= FIRST_LISTENER_KEY; |
| 438 | mListenerKey = Math.max(FIRST_LISTENER_KEY, mListenerKey + 1); |
| 439 | return mListenerKey; |
| 440 | } |
| 441 | |
| 442 | // Assert that the listener is not in the map, then add it and returns its key |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 443 | private int putListener(Object listener, NsdServiceInfo s) { |
Hugo Benichi | ff3e6cc | 2017-05-02 13:36:28 +0900 | [diff] [blame] | 444 | checkListener(listener); |
| 445 | final int key; |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 446 | synchronized (mMapLock) { |
Dave Platt | e7369bd | 2014-03-28 13:33:04 -0700 | [diff] [blame] | 447 | int valueIndex = mListenerMap.indexOfValue(listener); |
Hugo Benichi | ff3e6cc | 2017-05-02 13:36:28 +0900 | [diff] [blame] | 448 | checkArgument(valueIndex == -1, "listener already in use"); |
| 449 | key = nextListenerKey(); |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 450 | mListenerMap.put(key, listener); |
| 451 | mServiceMap.put(key, s); |
| 452 | } |
| 453 | return key; |
| 454 | } |
| 455 | |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 456 | private void removeListener(int key) { |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 457 | synchronized (mMapLock) { |
| 458 | mListenerMap.remove(key); |
| 459 | mServiceMap.remove(key); |
| 460 | } |
| 461 | } |
| 462 | |
| 463 | private int getListenerKey(Object listener) { |
Hugo Benichi | ff3e6cc | 2017-05-02 13:36:28 +0900 | [diff] [blame] | 464 | checkListener(listener); |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 465 | synchronized (mMapLock) { |
| 466 | int valueIndex = mListenerMap.indexOfValue(listener); |
Hugo Benichi | ff3e6cc | 2017-05-02 13:36:28 +0900 | [diff] [blame] | 467 | checkArgument(valueIndex != -1, "listener not registered"); |
| 468 | return mListenerMap.keyAt(valueIndex); |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 469 | } |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 470 | } |
| 471 | |
Hugo Benichi | ff3e6cc | 2017-05-02 13:36:28 +0900 | [diff] [blame] | 472 | private static String getNsdServiceInfoType(NsdServiceInfo s) { |
Dave Platt | 3fc376b | 2014-02-27 16:16:20 -0800 | [diff] [blame] | 473 | if (s == null) return "?"; |
| 474 | return s.getServiceType(); |
| 475 | } |
| 476 | |
Irfan Sheriff | 7d024d3 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 477 | /** |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 478 | * Initialize AsyncChannel |
Irfan Sheriff | 7d024d3 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 479 | */ |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 480 | private void init() { |
| 481 | final Messenger messenger = getMessenger(); |
Hugo Benichi | ff3e6cc | 2017-05-02 13:36:28 +0900 | [diff] [blame] | 482 | if (messenger == null) { |
| 483 | fatal("Failed to obtain service Messenger"); |
| 484 | } |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 485 | HandlerThread t = new HandlerThread("NsdManager"); |
| 486 | t.start(); |
| 487 | mHandler = new ServiceHandler(t.getLooper()); |
| 488 | mAsyncChannel.connect(mContext, mHandler, messenger); |
| 489 | try { |
| 490 | mConnected.await(); |
| 491 | } catch (InterruptedException e) { |
Hugo Benichi | ff3e6cc | 2017-05-02 13:36:28 +0900 | [diff] [blame] | 492 | fatal("Interrupted wait at init"); |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 493 | } |
Irfan Sheriff | 7d024d3 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 494 | } |
| 495 | |
Hugo Benichi | ff3e6cc | 2017-05-02 13:36:28 +0900 | [diff] [blame] | 496 | private static void fatal(String msg) { |
| 497 | Log.e(TAG, msg); |
| 498 | throw new RuntimeException(msg); |
| 499 | } |
| 500 | |
Irfan Sheriff | 7d024d3 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 501 | /** |
Irfan Sheriff | 9278467 | 2012-04-13 12:15:41 -0700 | [diff] [blame] | 502 | * Register a service to be discovered by other services. |
| 503 | * |
| 504 | * <p> The function call immediately returns after sending a request to register service |
Dave Platt | e7369bd | 2014-03-28 13:33:04 -0700 | [diff] [blame] | 505 | * to the framework. The application is notified of a successful registration |
| 506 | * through the callback {@link RegistrationListener#onServiceRegistered} or a failure |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 507 | * through {@link RegistrationListener#onRegistrationFailed}. |
Irfan Sheriff | 9278467 | 2012-04-13 12:15:41 -0700 | [diff] [blame] | 508 | * |
Dave Platt | e7369bd | 2014-03-28 13:33:04 -0700 | [diff] [blame] | 509 | * <p> The application should call {@link #unregisterService} when the service |
| 510 | * registration is no longer required, and/or whenever the application is stopped. |
| 511 | * |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 512 | * @param serviceInfo The service being registered |
| 513 | * @param protocolType The service discovery protocol |
| 514 | * @param listener The listener notifies of a successful registration and is used to |
| 515 | * unregister this service through a call on {@link #unregisterService}. Cannot be null. |
Dave Platt | e7369bd | 2014-03-28 13:33:04 -0700 | [diff] [blame] | 516 | * Cannot be in use for an active service registration. |
Irfan Sheriff | 7d024d3 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 517 | */ |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 518 | public void registerService(NsdServiceInfo serviceInfo, int protocolType, |
| 519 | RegistrationListener listener) { |
Hugo Benichi | ff3e6cc | 2017-05-02 13:36:28 +0900 | [diff] [blame] | 520 | checkArgument(serviceInfo.getPort() > 0, "Invalid port number"); |
| 521 | checkServiceInfo(serviceInfo); |
| 522 | checkProtocol(protocolType); |
Dave Platt | e7369bd | 2014-03-28 13:33:04 -0700 | [diff] [blame] | 523 | int key = putListener(listener, serviceInfo); |
Dave Platt | e7369bd | 2014-03-28 13:33:04 -0700 | [diff] [blame] | 524 | mAsyncChannel.sendMessage(REGISTER_SERVICE, 0, key, serviceInfo); |
Irfan Sheriff | 7d024d3 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 525 | } |
| 526 | |
Irfan Sheriff | 9278467 | 2012-04-13 12:15:41 -0700 | [diff] [blame] | 527 | /** |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 528 | * Unregister a service registered through {@link #registerService}. A successful |
| 529 | * unregister is notified to the application with a call to |
| 530 | * {@link RegistrationListener#onServiceUnregistered}. |
| 531 | * |
| 532 | * @param listener This should be the listener object that was passed to |
| 533 | * {@link #registerService}. It identifies the service that should be unregistered |
Dave Platt | e7369bd | 2014-03-28 13:33:04 -0700 | [diff] [blame] | 534 | * and notifies of a successful or unsuccessful unregistration via the listener |
| 535 | * callbacks. In API versions 20 and above, the listener object may be used for |
| 536 | * another service registration once the callback has been called. In API versions <= 19, |
| 537 | * there is no entirely reliable way to know when a listener may be re-used, and a new |
| 538 | * listener should be created for each service registration request. |
Irfan Sheriff | 9278467 | 2012-04-13 12:15:41 -0700 | [diff] [blame] | 539 | */ |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 540 | public void unregisterService(RegistrationListener listener) { |
| 541 | int id = getListenerKey(listener); |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 542 | mAsyncChannel.sendMessage(UNREGISTER_SERVICE, 0, id); |
Irfan Sheriff | 7d024d3 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 543 | } |
| 544 | |
Irfan Sheriff | 9278467 | 2012-04-13 12:15:41 -0700 | [diff] [blame] | 545 | /** |
| 546 | * Initiate service discovery to browse for instances of a service type. Service discovery |
| 547 | * consumes network bandwidth and will continue until the application calls |
| 548 | * {@link #stopServiceDiscovery}. |
| 549 | * |
| 550 | * <p> The function call immediately returns after sending a request to start service |
| 551 | * discovery to the framework. The application is notified of a success to initiate |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 552 | * discovery through the callback {@link DiscoveryListener#onDiscoveryStarted} or a failure |
| 553 | * through {@link DiscoveryListener#onStartDiscoveryFailed}. |
Irfan Sheriff | 9278467 | 2012-04-13 12:15:41 -0700 | [diff] [blame] | 554 | * |
| 555 | * <p> Upon successful start, application is notified when a service is found with |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 556 | * {@link DiscoveryListener#onServiceFound} or when a service is lost with |
| 557 | * {@link DiscoveryListener#onServiceLost}. |
Irfan Sheriff | 9278467 | 2012-04-13 12:15:41 -0700 | [diff] [blame] | 558 | * |
| 559 | * <p> Upon failure to start, service discovery is not active and application does |
| 560 | * not need to invoke {@link #stopServiceDiscovery} |
| 561 | * |
Dave Platt | e7369bd | 2014-03-28 13:33:04 -0700 | [diff] [blame] | 562 | * <p> The application should call {@link #stopServiceDiscovery} when discovery of this |
| 563 | * service type is no longer required, and/or whenever the application is paused or |
| 564 | * stopped. |
| 565 | * |
Irfan Sheriff | 9278467 | 2012-04-13 12:15:41 -0700 | [diff] [blame] | 566 | * @param serviceType The service type being discovered. Examples include "_http._tcp" for |
| 567 | * http services or "_ipp._tcp" for printers |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 568 | * @param protocolType The service discovery protocol |
| 569 | * @param listener The listener notifies of a successful discovery and is used |
| 570 | * to stop discovery on this serviceType through a call on {@link #stopServiceDiscovery}. |
Dave Platt | e7369bd | 2014-03-28 13:33:04 -0700 | [diff] [blame] | 571 | * Cannot be null. Cannot be in use for an active service discovery. |
Irfan Sheriff | 9278467 | 2012-04-13 12:15:41 -0700 | [diff] [blame] | 572 | */ |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 573 | public void discoverServices(String serviceType, int protocolType, DiscoveryListener listener) { |
Hugo Benichi | ff3e6cc | 2017-05-02 13:36:28 +0900 | [diff] [blame] | 574 | checkStringNotEmpty(serviceType, "Service type cannot be empty"); |
| 575 | checkProtocol(protocolType); |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 576 | |
| 577 | NsdServiceInfo s = new NsdServiceInfo(); |
Irfan Sheriff | 7d024d3 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 578 | s.setServiceType(serviceType); |
Dave Platt | e7369bd | 2014-03-28 13:33:04 -0700 | [diff] [blame] | 579 | |
| 580 | int key = putListener(listener, s); |
Dave Platt | e7369bd | 2014-03-28 13:33:04 -0700 | [diff] [blame] | 581 | mAsyncChannel.sendMessage(DISCOVER_SERVICES, 0, key, s); |
Irfan Sheriff | 7d024d3 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 582 | } |
| 583 | |
Irfan Sheriff | 9278467 | 2012-04-13 12:15:41 -0700 | [diff] [blame] | 584 | /** |
Dave Platt | e7369bd | 2014-03-28 13:33:04 -0700 | [diff] [blame] | 585 | * Stop service discovery initiated with {@link #discoverServices}. An active service |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 586 | * discovery is notified to the application with {@link DiscoveryListener#onDiscoveryStarted} |
| 587 | * and it stays active until the application invokes a stop service discovery. A successful |
| 588 | * stop is notified to with a call to {@link DiscoveryListener#onDiscoveryStopped}. |
Irfan Sheriff | 9278467 | 2012-04-13 12:15:41 -0700 | [diff] [blame] | 589 | * |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 590 | * <p> Upon failure to stop service discovery, application is notified through |
| 591 | * {@link DiscoveryListener#onStopDiscoveryFailed}. |
Irfan Sheriff | 9278467 | 2012-04-13 12:15:41 -0700 | [diff] [blame] | 592 | * |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 593 | * @param listener This should be the listener object that was passed to {@link #discoverServices}. |
Dave Platt | e7369bd | 2014-03-28 13:33:04 -0700 | [diff] [blame] | 594 | * It identifies the discovery that should be stopped and notifies of a successful or |
| 595 | * unsuccessful stop. In API versions 20 and above, the listener object may be used for |
| 596 | * another service discovery once the callback has been called. In API versions <= 19, |
| 597 | * there is no entirely reliable way to know when a listener may be re-used, and a new |
| 598 | * listener should be created for each service discovery request. |
Irfan Sheriff | 9278467 | 2012-04-13 12:15:41 -0700 | [diff] [blame] | 599 | */ |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 600 | public void stopServiceDiscovery(DiscoveryListener listener) { |
| 601 | int id = getListenerKey(listener); |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 602 | mAsyncChannel.sendMessage(STOP_DISCOVERY, 0, id); |
Irfan Sheriff | 7d024d3 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 603 | } |
| 604 | |
Irfan Sheriff | 9278467 | 2012-04-13 12:15:41 -0700 | [diff] [blame] | 605 | /** |
| 606 | * Resolve a discovered service. An application can resolve a service right before |
| 607 | * establishing a connection to fetch the IP and port details on which to setup |
| 608 | * the connection. |
| 609 | * |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 610 | * @param serviceInfo service to be resolved |
Irfan Sheriff | 9278467 | 2012-04-13 12:15:41 -0700 | [diff] [blame] | 611 | * @param listener to receive callback upon success or failure. Cannot be null. |
Dave Platt | e7369bd | 2014-03-28 13:33:04 -0700 | [diff] [blame] | 612 | * Cannot be in use for an active service resolution. |
Irfan Sheriff | 9278467 | 2012-04-13 12:15:41 -0700 | [diff] [blame] | 613 | */ |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 614 | public void resolveService(NsdServiceInfo serviceInfo, ResolveListener listener) { |
Hugo Benichi | ff3e6cc | 2017-05-02 13:36:28 +0900 | [diff] [blame] | 615 | checkServiceInfo(serviceInfo); |
Dave Platt | e7369bd | 2014-03-28 13:33:04 -0700 | [diff] [blame] | 616 | int key = putListener(listener, serviceInfo); |
Dave Platt | e7369bd | 2014-03-28 13:33:04 -0700 | [diff] [blame] | 617 | mAsyncChannel.sendMessage(RESOLVE_SERVICE, 0, key, serviceInfo); |
Irfan Sheriff | 817388e | 2012-04-11 14:52:19 -0700 | [diff] [blame] | 618 | } |
| 619 | |
Irfan Sheriff | 3ef889b | 2012-04-17 23:15:29 -0700 | [diff] [blame] | 620 | /** Internal use only @hide */ |
| 621 | public void setEnabled(boolean enabled) { |
| 622 | try { |
| 623 | mService.setEnabled(enabled); |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 624 | } catch (RemoteException e) { |
| 625 | throw e.rethrowFromSystemServer(); |
| 626 | } |
Irfan Sheriff | 3ef889b | 2012-04-17 23:15:29 -0700 | [diff] [blame] | 627 | } |
| 628 | |
Irfan Sheriff | 7d024d3 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 629 | /** |
Hugo Benichi | ff3e6cc | 2017-05-02 13:36:28 +0900 | [diff] [blame] | 630 | * Get a reference to NsdService handler. This is used to establish |
Irfan Sheriff | 7d024d3 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 631 | * an AsyncChannel communication with the service |
| 632 | * |
Hugo Benichi | ff3e6cc | 2017-05-02 13:36:28 +0900 | [diff] [blame] | 633 | * @return Messenger pointing to the NsdService handler |
Irfan Sheriff | 7d024d3 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 634 | */ |
| 635 | private Messenger getMessenger() { |
| 636 | try { |
| 637 | return mService.getMessenger(); |
| 638 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 639 | throw e.rethrowFromSystemServer(); |
Irfan Sheriff | 7d024d3 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 640 | } |
| 641 | } |
Hugo Benichi | ff3e6cc | 2017-05-02 13:36:28 +0900 | [diff] [blame] | 642 | |
| 643 | private static void checkListener(Object listener) { |
| 644 | checkNotNull(listener, "listener cannot be null"); |
| 645 | } |
| 646 | |
| 647 | private static void checkProtocol(int protocolType) { |
| 648 | checkArgument(protocolType == PROTOCOL_DNS_SD, "Unsupported protocol"); |
| 649 | } |
| 650 | |
| 651 | private static void checkServiceInfo(NsdServiceInfo serviceInfo) { |
| 652 | checkNotNull(serviceInfo, "NsdServiceInfo cannot be null"); |
Hugo Benichi | e062ae0 | 2017-07-31 20:35:58 +0900 | [diff] [blame] | 653 | checkStringNotEmpty(serviceInfo.getServiceName(), "Service name cannot be empty"); |
Hugo Benichi | ff3e6cc | 2017-05-02 13:36:28 +0900 | [diff] [blame] | 654 | checkStringNotEmpty(serviceInfo.getServiceType(), "Service type cannot be empty"); |
| 655 | } |
Irfan Sheriff | 7d024d3 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 656 | } |