blob: 535bf675cd0e54014cca8c1f9c5e25b6f6d27619 [file] [log] [blame]
Irfan Sheriff7d024d32012-03-22 17:01:39 -07001/*
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
17package android.net.nsd;
18
Hugo Benichiff3e6cc2017-05-02 13:36:28 +090019import static com.android.internal.util.Preconditions.checkArgument;
20import static com.android.internal.util.Preconditions.checkNotNull;
21import static com.android.internal.util.Preconditions.checkStringNotEmpty;
22
Irfan Sheriff3ef889b2012-04-17 23:15:29 -070023import android.annotation.SdkConstant;
24import android.annotation.SdkConstant.SdkConstantType;
Hugo Benichie062ae02017-07-31 20:35:58 +090025import android.annotation.SystemService;
Irfan Sheriff7d024d32012-03-22 17:01:39 -070026import android.content.Context;
Irfan Sheriff7d024d32012-03-22 17:01:39 -070027import android.os.Handler;
Irfan Sheriff22af38c2012-05-03 16:44:27 -070028import android.os.HandlerThread;
Irfan Sheriff7d024d32012-03-22 17:01:39 -070029import android.os.Looper;
30import android.os.Message;
Irfan Sheriff7d024d32012-03-22 17:01:39 -070031import android.os.Messenger;
Hugo Benichie062ae02017-07-31 20:35:58 +090032import android.os.RemoteException;
Irfan Sheriff7d024d32012-03-22 17:01:39 -070033import android.util.Log;
Irfan Sheriff22af38c2012-05-03 16:44:27 -070034import android.util.SparseArray;
35
Hugo Benichidb8adb72017-04-17 15:27:52 +090036import com.android.internal.annotations.VisibleForTesting;
Irfan Sheriff7d024d32012-03-22 17:01:39 -070037import com.android.internal.util.AsyncChannel;
38import com.android.internal.util.Protocol;
39
Hugo Benichie062ae02017-07-31 20:35:58 +090040import java.util.concurrent.CountDownLatch;
41
Irfan Sheriff7d024d32012-03-22 17:01:39 -070042/**
Irfan Sheriff92784672012-04-13 12:15:41 -070043 * 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 Sheriff22af38c2012-05-03 16:44:27 -070049 * 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 Sheriff7d024d32012-03-22 17:01:39 -070051 *
52 * <p> The API is asynchronous and responses to requests from an application are on listener
Hugo Benichi2183ba92017-04-05 14:06:11 +090053 * callbacks on a seperate internal thread.
Irfan Sheriff7d024d32012-03-22 17:01:39 -070054 *
Irfan Sheriff92784672012-04-13 12:15:41 -070055 * <p> There are three main operations the API supports - registration, discovery and resolution.
56 * <pre>
57 * Application start
58 * |
Irfan Sheriff22af38c2012-05-03 16:44:27 -070059 * |
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 Sheriff92784672012-04-13 12:15:41 -070097 *
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 Sheriff22af38c2012-05-03 16:44:27 -0700103 * {@link RegistrationListener#onServiceRegistered} and a failure to register is notified
104 * over {@link RegistrationListener#onRegistrationFailed}
Irfan Sheriff92784672012-04-13 12:15:41 -0700105 *
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 Sheriff22af38c2012-05-03 16:44:27 -0700108 * to {@link DiscoveryListener#onServiceFound} and a service lost is notified on
109 * {@link DiscoveryListener#onServiceLost}.
Irfan Sheriff92784672012-04-13 12:15:41 -0700110 *
Philip P. Moltmann29154b02016-04-18 16:23:06 -0700111 * <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 Sheriff92784672012-04-13 12:15:41 -0700116 *
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 Sheriff7d024d32012-03-22 17:01:39 -0700120 *
Irfan Sheriff22af38c2012-05-03 16:44:27 -0700121 * {@see NsdServiceInfo}
Irfan Sheriff7d024d32012-03-22 17:01:39 -0700122 */
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -0600123@SystemService(Context.NSD_SERVICE)
Irfan Sheriff22af38c2012-05-03 16:44:27 -0700124public final class NsdManager {
Hugo Benichi2183ba92017-04-05 14:06:11 +0900125 private static final String TAG = NsdManager.class.getSimpleName();
126 private static final boolean DBG = false;
Irfan Sheriff7d024d32012-03-22 17:01:39 -0700127
Irfan Sheriff3ef889b2012-04-17 23:15:29 -0700128 /**
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 Sheriff3ef889b2012-04-17 23:15:29 -0700134 */
135 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
Hugo Benichi2183ba92017-04-05 14:06:11 +0900136 public static final String ACTION_NSD_STATE_CHANGED = "android.net.nsd.STATE_CHANGED";
Irfan Sheriff3ef889b2012-04-17 23:15:29 -0700137
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 Sheriff3ef889b2012-04-17 23:15:29 -0700144 */
145 public static final String EXTRA_NSD_STATE = "nsd_state";
146
147 /**
148 * Network service discovery is disabled
149 *
Irfan Sheriff54ac7a52012-04-19 10:26:34 -0700150 * @see #ACTION_NSD_STATE_CHANGED
Irfan Sheriff3ef889b2012-04-17 23:15:29 -0700151 */
152 public static final int NSD_STATE_DISABLED = 1;
153
154 /**
155 * Network service discovery is enabled
156 *
Irfan Sheriff54ac7a52012-04-19 10:26:34 -0700157 * @see #ACTION_NSD_STATE_CHANGED
Irfan Sheriff3ef889b2012-04-17 23:15:29 -0700158 */
159 public static final int NSD_STATE_ENABLED = 2;
160
Irfan Sheriff7d024d32012-03-22 17:01:39 -0700161 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 Sheriff92784672012-04-13 12:15:41 -0700189 public static final int UNREGISTER_SERVICE = BASE + 12;
Irfan Sheriff7d024d32012-03-22 17:01:39 -0700190 /** @hide */
Irfan Sheriff92784672012-04-13 12:15:41 -0700191 public static final int UNREGISTER_SERVICE_FAILED = BASE + 13;
Irfan Sheriff7d024d32012-03-22 17:01:39 -0700192 /** @hide */
Irfan Sheriff92784672012-04-13 12:15:41 -0700193 public static final int UNREGISTER_SERVICE_SUCCEEDED = BASE + 14;
Irfan Sheriff7d024d32012-03-22 17:01:39 -0700194
195 /** @hide */
Irfan Sheriff92784672012-04-13 12:15:41 -0700196 public static final int RESOLVE_SERVICE = BASE + 18;
Irfan Sheriff817388e2012-04-11 14:52:19 -0700197 /** @hide */
Irfan Sheriff92784672012-04-13 12:15:41 -0700198 public static final int RESOLVE_SERVICE_FAILED = BASE + 19;
Irfan Sheriff817388e2012-04-11 14:52:19 -0700199 /** @hide */
Irfan Sheriff92784672012-04-13 12:15:41 -0700200 public static final int RESOLVE_SERVICE_SUCCEEDED = BASE + 20;
Irfan Sheriff817388e2012-04-11 14:52:19 -0700201
Irfan Sheriff92784672012-04-13 12:15:41 -0700202 /** @hide */
Irfan Sheriff3ef889b2012-04-17 23:15:29 -0700203 public static final int ENABLE = BASE + 24;
204 /** @hide */
205 public static final int DISABLE = BASE + 25;
206
Irfan Sheriff22af38c2012-05-03 16:44:27 -0700207 /** @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 Benichi2183ba92017-04-05 14:06:11 +0900213 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 Benichie062ae02017-07-31 20:35:58 +0900246 private static final int FIRST_LISTENER_KEY = 1;
Hugo Benichiff3e6cc2017-05-02 13:36:28 +0900247
Hugo Benichi2183ba92017-04-05 14:06:11 +0900248 private final INsdManager mService;
249 private final Context mContext;
Irfan Sheriff22af38c2012-05-03 16:44:27 -0700250
Hugo Benichiff3e6cc2017-05-02 13:36:28 +0900251 private int mListenerKey = FIRST_LISTENER_KEY;
Irfan Sheriff22af38c2012-05-03 16:44:27 -0700252 private final SparseArray mListenerMap = new SparseArray();
Hugo Benichi2183ba92017-04-05 14:06:11 +0900253 private final SparseArray<NsdServiceInfo> mServiceMap = new SparseArray<>();
Irfan Sheriff22af38c2012-05-03 16:44:27 -0700254 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 Sheriff3ef889b2012-04-17 23:15:29 -0700259
Irfan Sheriff7d024d32012-03-22 17:01:39 -0700260 /**
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 Sheriff22af38c2012-05-03 16:44:27 -0700268 public NsdManager(Context context, INsdManager service) {
Irfan Sheriff7d024d32012-03-22 17:01:39 -0700269 mService = service;
Irfan Sheriff22af38c2012-05-03 16:44:27 -0700270 mContext = context;
271 init();
Irfan Sheriff7d024d32012-03-22 17:01:39 -0700272 }
273
274 /**
Hugo Benichiab5bdbf2017-04-28 15:31:10 +0900275 * @hide
276 */
277 @VisibleForTesting
278 public void disconnect() {
279 mAsyncChannel.disconnect();
Hugo Benichie062ae02017-07-31 20:35:58 +0900280 mHandler.getLooper().quitSafely();
Hugo Benichiab5bdbf2017-04-28 15:31:10 +0900281 }
282
283 /**
Irfan Sheriff22af38c2012-05-03 16:44:27 -0700284 * 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 Sheriff7d024d32012-03-22 17:01:39 -0700289 * Indicates that the operation failed due to an internal error.
290 */
Irfan Sheriff22af38c2012-05-03 16:44:27 -0700291 public static final int FAILURE_INTERNAL_ERROR = 0;
Irfan Sheriff7d024d32012-03-22 17:01:39 -0700292
293 /**
Irfan Sheriff817388e2012-04-11 14:52:19 -0700294 * Indicates that the operation failed because it is already active.
295 */
Irfan Sheriff22af38c2012-05-03 16:44:27 -0700296 public static final int FAILURE_ALREADY_ACTIVE = 3;
Irfan Sheriff817388e2012-04-11 14:52:19 -0700297
298 /**
Irfan Sheriff22af38c2012-05-03 16:44:27 -0700299 * Indicates that the operation failed because the maximum outstanding
300 * requests from the applications have reached.
Irfan Sheriff817388e2012-04-11 14:52:19 -0700301 */
Irfan Sheriff22af38c2012-05-03 16:44:27 -0700302 public static final int FAILURE_MAX_LIMIT = 4;
Irfan Sheriff7d024d32012-03-22 17:01:39 -0700303
Irfan Sheriff92784672012-04-13 12:15:41 -0700304 /** Interface for callback invocation for service discovery */
Irfan Sheriff22af38c2012-05-03 16:44:27 -0700305 public interface DiscoveryListener {
Irfan Sheriff7d024d32012-03-22 17:01:39 -0700306
Irfan Sheriff22af38c2012-05-03 16:44:27 -0700307 public void onStartDiscoveryFailed(String serviceType, int errorCode);
Irfan Sheriff7d024d32012-03-22 17:01:39 -0700308
Irfan Sheriff22af38c2012-05-03 16:44:27 -0700309 public void onStopDiscoveryFailed(String serviceType, int errorCode);
Irfan Sheriff7d024d32012-03-22 17:01:39 -0700310
Irfan Sheriff22af38c2012-05-03 16:44:27 -0700311 public void onDiscoveryStarted(String serviceType);
Irfan Sheriff7d024d32012-03-22 17:01:39 -0700312
Irfan Sheriff22af38c2012-05-03 16:44:27 -0700313 public void onDiscoveryStopped(String serviceType);
314
315 public void onServiceFound(NsdServiceInfo serviceInfo);
316
317 public void onServiceLost(NsdServiceInfo serviceInfo);
Irfan Sheriff7d024d32012-03-22 17:01:39 -0700318 }
319
Irfan Sheriff92784672012-04-13 12:15:41 -0700320 /** Interface for callback invocation for service registration */
Irfan Sheriff22af38c2012-05-03 16:44:27 -0700321 public interface RegistrationListener {
Irfan Sheriff7d024d32012-03-22 17:01:39 -0700322
Irfan Sheriff22af38c2012-05-03 16:44:27 -0700323 public void onRegistrationFailed(NsdServiceInfo serviceInfo, int errorCode);
Irfan Sheriff7d024d32012-03-22 17:01:39 -0700324
Irfan Sheriff22af38c2012-05-03 16:44:27 -0700325 public void onUnregistrationFailed(NsdServiceInfo serviceInfo, int errorCode);
Irfan Sheriff7d024d32012-03-22 17:01:39 -0700326
Irfan Sheriff22af38c2012-05-03 16:44:27 -0700327 public void onServiceRegistered(NsdServiceInfo serviceInfo);
Irfan Sheriff7d024d32012-03-22 17:01:39 -0700328
Irfan Sheriff22af38c2012-05-03 16:44:27 -0700329 public void onServiceUnregistered(NsdServiceInfo serviceInfo);
Irfan Sheriff7d024d32012-03-22 17:01:39 -0700330 }
331
Irfan Sheriff92784672012-04-13 12:15:41 -0700332 /** Interface for callback invocation for service resolution */
Irfan Sheriff22af38c2012-05-03 16:44:27 -0700333 public interface ResolveListener {
Irfan Sheriff7d024d32012-03-22 17:01:39 -0700334
Irfan Sheriff22af38c2012-05-03 16:44:27 -0700335 public void onResolveFailed(NsdServiceInfo serviceInfo, int errorCode);
Irfan Sheriff7d024d32012-03-22 17:01:39 -0700336
Irfan Sheriff22af38c2012-05-03 16:44:27 -0700337 public void onServiceResolved(NsdServiceInfo serviceInfo);
Irfan Sheriff7d024d32012-03-22 17:01:39 -0700338 }
339
Hugo Benichidb8adb72017-04-17 15:27:52 +0900340 @VisibleForTesting
341 class ServiceHandler extends Handler {
Irfan Sheriff22af38c2012-05-03 16:44:27 -0700342 ServiceHandler(Looper looper) {
343 super(looper);
Irfan Sheriff7d024d32012-03-22 17:01:39 -0700344 }
Irfan Sheriff7d024d32012-03-22 17:01:39 -0700345
Irfan Sheriff22af38c2012-05-03 16:44:27 -0700346 @Override
347 public void handleMessage(Message message) {
Hugo Benichiff3e6cc2017-05-02 13:36:28 +0900348 final int what = message.what;
349 final int key = message.arg2;
350 switch (what) {
Irfan Sheriff22af38c2012-05-03 16:44:27 -0700351 case AsyncChannel.CMD_CHANNEL_HALF_CONNECTED:
352 mAsyncChannel.sendMessage(AsyncChannel.CMD_CHANNEL_FULL_CONNECTION);
Dave Platt3fc376b2014-02-27 16:16:20 -0800353 return;
Irfan Sheriff22af38c2012-05-03 16:44:27 -0700354 case AsyncChannel.CMD_CHANNEL_FULLY_CONNECTED:
Robert Greenwaltaf2eefb2013-05-02 15:45:32 -0700355 mConnected.countDown();
Dave Platt3fc376b2014-02-27 16:16:20 -0800356 return;
Irfan Sheriff22af38c2012-05-03 16:44:27 -0700357 case AsyncChannel.CMD_CHANNEL_DISCONNECTED:
358 Log.e(TAG, "Channel lost");
Dave Platt3fc376b2014-02-27 16:16:20 -0800359 return;
360 default:
Irfan Sheriff22af38c2012-05-03 16:44:27 -0700361 break;
Dave Platt3fc376b2014-02-27 16:16:20 -0800362 }
Hugo Benichiff3e6cc2017-05-02 13:36:28 +0900363 final Object listener;
364 final NsdServiceInfo ns;
365 synchronized (mMapLock) {
366 listener = mListenerMap.get(key);
367 ns = mServiceMap.get(key);
368 }
Dave Platt3fc376b2014-02-27 16:16:20 -0800369 if (listener == null) {
370 Log.d(TAG, "Stale key " + message.arg2);
371 return;
372 }
Hugo Benichiff3e6cc2017-05-02 13:36:28 +0900373 if (DBG) {
374 Log.d(TAG, "received " + nameOf(what) + " for key " + key + ", service " + ns);
375 }
376 switch (what) {
Irfan Sheriff22af38c2012-05-03 16:44:27 -0700377 case DISCOVER_SERVICES_STARTED:
Dave Platt3fc376b2014-02-27 16:16:20 -0800378 String s = getNsdServiceInfoType((NsdServiceInfo) message.obj);
Irfan Sheriff22af38c2012-05-03 16:44:27 -0700379 ((DiscoveryListener) listener).onDiscoveryStarted(s);
Irfan Sheriff22af38c2012-05-03 16:44:27 -0700380 break;
381 case DISCOVER_SERVICES_FAILED:
Hugo Benichiff3e6cc2017-05-02 13:36:28 +0900382 removeListener(key);
Dave Platt3fc376b2014-02-27 16:16:20 -0800383 ((DiscoveryListener) listener).onStartDiscoveryFailed(getNsdServiceInfoType(ns),
384 message.arg1);
Irfan Sheriff22af38c2012-05-03 16:44:27 -0700385 break;
386 case SERVICE_FOUND:
387 ((DiscoveryListener) listener).onServiceFound((NsdServiceInfo) message.obj);
Irfan Sheriff22af38c2012-05-03 16:44:27 -0700388 break;
389 case SERVICE_LOST:
390 ((DiscoveryListener) listener).onServiceLost((NsdServiceInfo) message.obj);
Irfan Sheriff22af38c2012-05-03 16:44:27 -0700391 break;
392 case STOP_DISCOVERY_FAILED:
Hugo Benichi93f45912017-04-28 13:29:21 +0900393 // 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 Benichiff3e6cc2017-05-02 13:36:28 +0900395 removeListener(key);
Dave Platt3fc376b2014-02-27 16:16:20 -0800396 ((DiscoveryListener) listener).onStopDiscoveryFailed(getNsdServiceInfoType(ns),
397 message.arg1);
Irfan Sheriff22af38c2012-05-03 16:44:27 -0700398 break;
399 case STOP_DISCOVERY_SUCCEEDED:
Hugo Benichiff3e6cc2017-05-02 13:36:28 +0900400 removeListener(key);
Dave Platt3fc376b2014-02-27 16:16:20 -0800401 ((DiscoveryListener) listener).onDiscoveryStopped(getNsdServiceInfoType(ns));
Irfan Sheriff22af38c2012-05-03 16:44:27 -0700402 break;
403 case REGISTER_SERVICE_FAILED:
Hugo Benichiff3e6cc2017-05-02 13:36:28 +0900404 removeListener(key);
Dave Platt3fc376b2014-02-27 16:16:20 -0800405 ((RegistrationListener) listener).onRegistrationFailed(ns, message.arg1);
Irfan Sheriff22af38c2012-05-03 16:44:27 -0700406 break;
407 case REGISTER_SERVICE_SUCCEEDED:
408 ((RegistrationListener) listener).onServiceRegistered(
409 (NsdServiceInfo) message.obj);
Irfan Sheriff22af38c2012-05-03 16:44:27 -0700410 break;
411 case UNREGISTER_SERVICE_FAILED:
Hugo Benichiff3e6cc2017-05-02 13:36:28 +0900412 removeListener(key);
Dave Platt3fc376b2014-02-27 16:16:20 -0800413 ((RegistrationListener) listener).onUnregistrationFailed(ns, message.arg1);
Irfan Sheriff22af38c2012-05-03 16:44:27 -0700414 break;
415 case UNREGISTER_SERVICE_SUCCEEDED:
Hugo Benichi8c5eeb02017-04-28 11:01:22 +0900416 // TODO: do not unregister listener until service is unregistered, or provide
417 // alternative way for unregistering ?
Dave Platte7369bd2014-03-28 13:33:04 -0700418 removeListener(message.arg2);
Dave Platt3fc376b2014-02-27 16:16:20 -0800419 ((RegistrationListener) listener).onServiceUnregistered(ns);
Irfan Sheriff22af38c2012-05-03 16:44:27 -0700420 break;
421 case RESOLVE_SERVICE_FAILED:
Hugo Benichiff3e6cc2017-05-02 13:36:28 +0900422 removeListener(key);
Dave Platt3fc376b2014-02-27 16:16:20 -0800423 ((ResolveListener) listener).onResolveFailed(ns, message.arg1);
Irfan Sheriff22af38c2012-05-03 16:44:27 -0700424 break;
425 case RESOLVE_SERVICE_SUCCEEDED:
Hugo Benichiff3e6cc2017-05-02 13:36:28 +0900426 removeListener(key);
Irfan Sheriff22af38c2012-05-03 16:44:27 -0700427 ((ResolveListener) listener).onServiceResolved((NsdServiceInfo) message.obj);
428 break;
429 default:
430 Log.d(TAG, "Ignored " + message);
431 break;
Irfan Sheriff7d024d32012-03-22 17:01:39 -0700432 }
Irfan Sheriff7d024d32012-03-22 17:01:39 -0700433 }
Irfan Sheriff92784672012-04-13 12:15:41 -0700434 }
435
Hugo Benichiff3e6cc2017-05-02 13:36:28 +0900436 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 Sheriff22af38c2012-05-03 16:44:27 -0700443 private int putListener(Object listener, NsdServiceInfo s) {
Hugo Benichiff3e6cc2017-05-02 13:36:28 +0900444 checkListener(listener);
445 final int key;
Irfan Sheriff22af38c2012-05-03 16:44:27 -0700446 synchronized (mMapLock) {
Dave Platte7369bd2014-03-28 13:33:04 -0700447 int valueIndex = mListenerMap.indexOfValue(listener);
Hugo Benichiff3e6cc2017-05-02 13:36:28 +0900448 checkArgument(valueIndex == -1, "listener already in use");
449 key = nextListenerKey();
Irfan Sheriff22af38c2012-05-03 16:44:27 -0700450 mListenerMap.put(key, listener);
451 mServiceMap.put(key, s);
452 }
453 return key;
454 }
455
Irfan Sheriff22af38c2012-05-03 16:44:27 -0700456 private void removeListener(int key) {
Irfan Sheriff22af38c2012-05-03 16:44:27 -0700457 synchronized (mMapLock) {
458 mListenerMap.remove(key);
459 mServiceMap.remove(key);
460 }
461 }
462
463 private int getListenerKey(Object listener) {
Hugo Benichiff3e6cc2017-05-02 13:36:28 +0900464 checkListener(listener);
Irfan Sheriff22af38c2012-05-03 16:44:27 -0700465 synchronized (mMapLock) {
466 int valueIndex = mListenerMap.indexOfValue(listener);
Hugo Benichiff3e6cc2017-05-02 13:36:28 +0900467 checkArgument(valueIndex != -1, "listener not registered");
468 return mListenerMap.keyAt(valueIndex);
Irfan Sheriff22af38c2012-05-03 16:44:27 -0700469 }
Irfan Sheriff22af38c2012-05-03 16:44:27 -0700470 }
471
Hugo Benichiff3e6cc2017-05-02 13:36:28 +0900472 private static String getNsdServiceInfoType(NsdServiceInfo s) {
Dave Platt3fc376b2014-02-27 16:16:20 -0800473 if (s == null) return "?";
474 return s.getServiceType();
475 }
476
Irfan Sheriff7d024d32012-03-22 17:01:39 -0700477 /**
Irfan Sheriff22af38c2012-05-03 16:44:27 -0700478 * Initialize AsyncChannel
Irfan Sheriff7d024d32012-03-22 17:01:39 -0700479 */
Irfan Sheriff22af38c2012-05-03 16:44:27 -0700480 private void init() {
481 final Messenger messenger = getMessenger();
Hugo Benichiff3e6cc2017-05-02 13:36:28 +0900482 if (messenger == null) {
483 fatal("Failed to obtain service Messenger");
484 }
Irfan Sheriff22af38c2012-05-03 16:44:27 -0700485 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 Benichiff3e6cc2017-05-02 13:36:28 +0900492 fatal("Interrupted wait at init");
Irfan Sheriff22af38c2012-05-03 16:44:27 -0700493 }
Irfan Sheriff7d024d32012-03-22 17:01:39 -0700494 }
495
Hugo Benichiff3e6cc2017-05-02 13:36:28 +0900496 private static void fatal(String msg) {
497 Log.e(TAG, msg);
498 throw new RuntimeException(msg);
499 }
500
Irfan Sheriff7d024d32012-03-22 17:01:39 -0700501 /**
Irfan Sheriff92784672012-04-13 12:15:41 -0700502 * 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 Platte7369bd2014-03-28 13:33:04 -0700505 * to the framework. The application is notified of a successful registration
506 * through the callback {@link RegistrationListener#onServiceRegistered} or a failure
Irfan Sheriff22af38c2012-05-03 16:44:27 -0700507 * through {@link RegistrationListener#onRegistrationFailed}.
Irfan Sheriff92784672012-04-13 12:15:41 -0700508 *
Dave Platte7369bd2014-03-28 13:33:04 -0700509 * <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 Sheriff22af38c2012-05-03 16:44:27 -0700512 * @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 Platte7369bd2014-03-28 13:33:04 -0700516 * Cannot be in use for an active service registration.
Irfan Sheriff7d024d32012-03-22 17:01:39 -0700517 */
Irfan Sheriff22af38c2012-05-03 16:44:27 -0700518 public void registerService(NsdServiceInfo serviceInfo, int protocolType,
519 RegistrationListener listener) {
Hugo Benichiff3e6cc2017-05-02 13:36:28 +0900520 checkArgument(serviceInfo.getPort() > 0, "Invalid port number");
521 checkServiceInfo(serviceInfo);
522 checkProtocol(protocolType);
Dave Platte7369bd2014-03-28 13:33:04 -0700523 int key = putListener(listener, serviceInfo);
Dave Platte7369bd2014-03-28 13:33:04 -0700524 mAsyncChannel.sendMessage(REGISTER_SERVICE, 0, key, serviceInfo);
Irfan Sheriff7d024d32012-03-22 17:01:39 -0700525 }
526
Irfan Sheriff92784672012-04-13 12:15:41 -0700527 /**
Irfan Sheriff22af38c2012-05-03 16:44:27 -0700528 * 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 Platte7369bd2014-03-28 13:33:04 -0700534 * 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 Sheriff92784672012-04-13 12:15:41 -0700539 */
Irfan Sheriff22af38c2012-05-03 16:44:27 -0700540 public void unregisterService(RegistrationListener listener) {
541 int id = getListenerKey(listener);
Irfan Sheriff22af38c2012-05-03 16:44:27 -0700542 mAsyncChannel.sendMessage(UNREGISTER_SERVICE, 0, id);
Irfan Sheriff7d024d32012-03-22 17:01:39 -0700543 }
544
Irfan Sheriff92784672012-04-13 12:15:41 -0700545 /**
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 Sheriff22af38c2012-05-03 16:44:27 -0700552 * discovery through the callback {@link DiscoveryListener#onDiscoveryStarted} or a failure
553 * through {@link DiscoveryListener#onStartDiscoveryFailed}.
Irfan Sheriff92784672012-04-13 12:15:41 -0700554 *
555 * <p> Upon successful start, application is notified when a service is found with
Irfan Sheriff22af38c2012-05-03 16:44:27 -0700556 * {@link DiscoveryListener#onServiceFound} or when a service is lost with
557 * {@link DiscoveryListener#onServiceLost}.
Irfan Sheriff92784672012-04-13 12:15:41 -0700558 *
559 * <p> Upon failure to start, service discovery is not active and application does
560 * not need to invoke {@link #stopServiceDiscovery}
561 *
Dave Platte7369bd2014-03-28 13:33:04 -0700562 * <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 Sheriff92784672012-04-13 12:15:41 -0700566 * @param serviceType The service type being discovered. Examples include "_http._tcp" for
567 * http services or "_ipp._tcp" for printers
Irfan Sheriff22af38c2012-05-03 16:44:27 -0700568 * @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 Platte7369bd2014-03-28 13:33:04 -0700571 * Cannot be null. Cannot be in use for an active service discovery.
Irfan Sheriff92784672012-04-13 12:15:41 -0700572 */
Irfan Sheriff22af38c2012-05-03 16:44:27 -0700573 public void discoverServices(String serviceType, int protocolType, DiscoveryListener listener) {
Hugo Benichiff3e6cc2017-05-02 13:36:28 +0900574 checkStringNotEmpty(serviceType, "Service type cannot be empty");
575 checkProtocol(protocolType);
Irfan Sheriff22af38c2012-05-03 16:44:27 -0700576
577 NsdServiceInfo s = new NsdServiceInfo();
Irfan Sheriff7d024d32012-03-22 17:01:39 -0700578 s.setServiceType(serviceType);
Dave Platte7369bd2014-03-28 13:33:04 -0700579
580 int key = putListener(listener, s);
Dave Platte7369bd2014-03-28 13:33:04 -0700581 mAsyncChannel.sendMessage(DISCOVER_SERVICES, 0, key, s);
Irfan Sheriff7d024d32012-03-22 17:01:39 -0700582 }
583
Irfan Sheriff92784672012-04-13 12:15:41 -0700584 /**
Dave Platte7369bd2014-03-28 13:33:04 -0700585 * Stop service discovery initiated with {@link #discoverServices}. An active service
Irfan Sheriff22af38c2012-05-03 16:44:27 -0700586 * 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 Sheriff92784672012-04-13 12:15:41 -0700589 *
Irfan Sheriff22af38c2012-05-03 16:44:27 -0700590 * <p> Upon failure to stop service discovery, application is notified through
591 * {@link DiscoveryListener#onStopDiscoveryFailed}.
Irfan Sheriff92784672012-04-13 12:15:41 -0700592 *
Irfan Sheriff22af38c2012-05-03 16:44:27 -0700593 * @param listener This should be the listener object that was passed to {@link #discoverServices}.
Dave Platte7369bd2014-03-28 13:33:04 -0700594 * 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 Sheriff92784672012-04-13 12:15:41 -0700599 */
Irfan Sheriff22af38c2012-05-03 16:44:27 -0700600 public void stopServiceDiscovery(DiscoveryListener listener) {
601 int id = getListenerKey(listener);
Irfan Sheriff22af38c2012-05-03 16:44:27 -0700602 mAsyncChannel.sendMessage(STOP_DISCOVERY, 0, id);
Irfan Sheriff7d024d32012-03-22 17:01:39 -0700603 }
604
Irfan Sheriff92784672012-04-13 12:15:41 -0700605 /**
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 Sheriff22af38c2012-05-03 16:44:27 -0700610 * @param serviceInfo service to be resolved
Irfan Sheriff92784672012-04-13 12:15:41 -0700611 * @param listener to receive callback upon success or failure. Cannot be null.
Dave Platte7369bd2014-03-28 13:33:04 -0700612 * Cannot be in use for an active service resolution.
Irfan Sheriff92784672012-04-13 12:15:41 -0700613 */
Irfan Sheriff22af38c2012-05-03 16:44:27 -0700614 public void resolveService(NsdServiceInfo serviceInfo, ResolveListener listener) {
Hugo Benichiff3e6cc2017-05-02 13:36:28 +0900615 checkServiceInfo(serviceInfo);
Dave Platte7369bd2014-03-28 13:33:04 -0700616 int key = putListener(listener, serviceInfo);
Dave Platte7369bd2014-03-28 13:33:04 -0700617 mAsyncChannel.sendMessage(RESOLVE_SERVICE, 0, key, serviceInfo);
Irfan Sheriff817388e2012-04-11 14:52:19 -0700618 }
619
Irfan Sheriff3ef889b2012-04-17 23:15:29 -0700620 /** Internal use only @hide */
621 public void setEnabled(boolean enabled) {
622 try {
623 mService.setEnabled(enabled);
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700624 } catch (RemoteException e) {
625 throw e.rethrowFromSystemServer();
626 }
Irfan Sheriff3ef889b2012-04-17 23:15:29 -0700627 }
628
Irfan Sheriff7d024d32012-03-22 17:01:39 -0700629 /**
Hugo Benichiff3e6cc2017-05-02 13:36:28 +0900630 * Get a reference to NsdService handler. This is used to establish
Irfan Sheriff7d024d32012-03-22 17:01:39 -0700631 * an AsyncChannel communication with the service
632 *
Hugo Benichiff3e6cc2017-05-02 13:36:28 +0900633 * @return Messenger pointing to the NsdService handler
Irfan Sheriff7d024d32012-03-22 17:01:39 -0700634 */
635 private Messenger getMessenger() {
636 try {
637 return mService.getMessenger();
638 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700639 throw e.rethrowFromSystemServer();
Irfan Sheriff7d024d32012-03-22 17:01:39 -0700640 }
641 }
Hugo Benichiff3e6cc2017-05-02 13:36:28 +0900642
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 Benichie062ae02017-07-31 20:35:58 +0900653 checkStringNotEmpty(serviceInfo.getServiceName(), "Service name cannot be empty");
Hugo Benichiff3e6cc2017-05-02 13:36:28 +0900654 checkStringNotEmpty(serviceInfo.getServiceType(), "Service type cannot be empty");
655 }
Irfan Sheriff7d024d32012-03-22 17:01:39 -0700656}