blob: 684020722e83faf3627ab4465f4f053427670588 [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
Irfan Sheriff3ef889b2012-04-17 23:15:29 -070019import android.annotation.SdkConstant;
20import android.annotation.SdkConstant.SdkConstantType;
Irfan Sheriff7d024d32012-03-22 17:01:39 -070021import android.content.Context;
Irfan Sheriff7d024d32012-03-22 17:01:39 -070022import android.os.Handler;
Irfan Sheriff22af38c2012-05-03 16:44:27 -070023import android.os.HandlerThread;
Irfan Sheriff7d024d32012-03-22 17:01:39 -070024import android.os.Looper;
25import android.os.Message;
26import android.os.RemoteException;
27import android.os.Messenger;
Irfan Sheriff92784672012-04-13 12:15:41 -070028import android.text.TextUtils;
Irfan Sheriff7d024d32012-03-22 17:01:39 -070029import android.util.Log;
Irfan Sheriff22af38c2012-05-03 16:44:27 -070030import android.util.SparseArray;
31
32import java.util.concurrent.CountDownLatch;
Irfan Sheriff7d024d32012-03-22 17:01:39 -070033
34import com.android.internal.util.AsyncChannel;
35import com.android.internal.util.Protocol;
36
37/**
Irfan Sheriff92784672012-04-13 12:15:41 -070038 * The Network Service Discovery Manager class provides the API to discover services
39 * on a network. As an example, if device A and device B are connected over a Wi-Fi
40 * network, a game registered on device A can be discovered by a game on device
41 * B. Another example use case is an application discovering printers on the network.
42 *
43 * <p> The API currently supports DNS based service discovery and discovery is currently
Irfan Sheriff22af38c2012-05-03 16:44:27 -070044 * limited to a local network over Multicast DNS. DNS service discovery is described at
45 * http://files.dns-sd.org/draft-cheshire-dnsext-dns-sd.txt
Irfan Sheriff7d024d32012-03-22 17:01:39 -070046 *
47 * <p> The API is asynchronous and responses to requests from an application are on listener
Irfan Sheriff22af38c2012-05-03 16:44:27 -070048 * callbacks on a seperate thread.
Irfan Sheriff7d024d32012-03-22 17:01:39 -070049 *
Irfan Sheriff92784672012-04-13 12:15:41 -070050 * <p> There are three main operations the API supports - registration, discovery and resolution.
51 * <pre>
52 * Application start
53 * |
Irfan Sheriff22af38c2012-05-03 16:44:27 -070054 * |
55 * | onServiceRegistered()
56 * Register any local services /
57 * to be advertised with \
58 * registerService() onRegistrationFailed()
59 * |
60 * |
61 * discoverServices()
62 * |
63 * Maintain a list to track
64 * discovered services
65 * |
66 * |--------->
67 * | |
68 * | onServiceFound()
69 * | |
70 * | add service to list
71 * | |
72 * |<----------
73 * |
74 * |--------->
75 * | |
76 * | onServiceLost()
77 * | |
78 * | remove service from list
79 * | |
80 * |<----------
81 * |
82 * |
83 * | Connect to a service
84 * | from list ?
85 * |
86 * resolveService()
87 * |
88 * onServiceResolved()
89 * |
90 * Establish connection to service
91 * with the host and port information
Irfan Sheriff92784672012-04-13 12:15:41 -070092 *
93 * </pre>
94 * An application that needs to advertise itself over a network for other applications to
95 * discover it can do so with a call to {@link #registerService}. If Example is a http based
96 * application that can provide HTML data to peer services, it can register a name "Example"
97 * with service type "_http._tcp". A successful registration is notified with a callback to
Irfan Sheriff22af38c2012-05-03 16:44:27 -070098 * {@link RegistrationListener#onServiceRegistered} and a failure to register is notified
99 * over {@link RegistrationListener#onRegistrationFailed}
Irfan Sheriff92784672012-04-13 12:15:41 -0700100 *
101 * <p> A peer application looking for http services can initiate a discovery for "_http._tcp"
102 * with a call to {@link #discoverServices}. A service found is notified with a callback
Irfan Sheriff22af38c2012-05-03 16:44:27 -0700103 * to {@link DiscoveryListener#onServiceFound} and a service lost is notified on
104 * {@link DiscoveryListener#onServiceLost}.
Irfan Sheriff92784672012-04-13 12:15:41 -0700105 *
106 * <p> Once the peer application discovers the "Example" http srevice, and needs to receive data
107 * from the "Example" application, it can initiate a resolve with {@link #resolveService} to
108 * resolve the host and port details for the purpose of establishing a connection. A successful
Irfan Sheriff22af38c2012-05-03 16:44:27 -0700109 * resolve is notified on {@link ResolveListener#onServiceResolved} and a failure is notified
110 * on {@link ResolveListener#onResolveFailed}.
Irfan Sheriff92784672012-04-13 12:15:41 -0700111 *
112 * Applications can reserve for a service type at
113 * http://www.iana.org/form/ports-service. Existing services can be found at
114 * http://www.iana.org/assignments/service-names-port-numbers/service-names-port-numbers.xml
Irfan Sheriff7d024d32012-03-22 17:01:39 -0700115 *
116 * Get an instance of this class by calling {@link android.content.Context#getSystemService(String)
117 * Context.getSystemService(Context.NSD_SERVICE)}.
Irfan Sheriff7d024d32012-03-22 17:01:39 -0700118 *
Irfan Sheriff22af38c2012-05-03 16:44:27 -0700119 * {@see NsdServiceInfo}
Irfan Sheriff7d024d32012-03-22 17:01:39 -0700120 */
Irfan Sheriff22af38c2012-05-03 16:44:27 -0700121public final class NsdManager {
Irfan Sheriff7d024d32012-03-22 17:01:39 -0700122 private static final String TAG = "NsdManager";
123 INsdManager mService;
124
Irfan Sheriff3ef889b2012-04-17 23:15:29 -0700125 /**
126 * Broadcast intent action to indicate whether network service discovery is
127 * enabled or disabled. An extra {@link #EXTRA_NSD_STATE} provides the state
128 * information as int.
129 *
130 * @see #EXTRA_NSD_STATE
Irfan Sheriff3ef889b2012-04-17 23:15:29 -0700131 */
132 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
Irfan Sheriff6c07ba82012-04-17 23:23:42 -0700133 public static final String ACTION_NSD_STATE_CHANGED =
Irfan Sheriff3ef889b2012-04-17 23:15:29 -0700134 "android.net.nsd.STATE_CHANGED";
135
136 /**
137 * The lookup key for an int that indicates whether network service discovery is enabled
138 * or disabled. Retrieve it with {@link android.content.Intent#getIntExtra(String,int)}.
139 *
140 * @see #NSD_STATE_DISABLED
141 * @see #NSD_STATE_ENABLED
Irfan Sheriff3ef889b2012-04-17 23:15:29 -0700142 */
143 public static final String EXTRA_NSD_STATE = "nsd_state";
144
145 /**
146 * Network service discovery is disabled
147 *
Irfan Sheriff54ac7a52012-04-19 10:26:34 -0700148 * @see #ACTION_NSD_STATE_CHANGED
Irfan Sheriff3ef889b2012-04-17 23:15:29 -0700149 */
150 public static final int NSD_STATE_DISABLED = 1;
151
152 /**
153 * Network service discovery is enabled
154 *
Irfan Sheriff54ac7a52012-04-19 10:26:34 -0700155 * @see #ACTION_NSD_STATE_CHANGED
Irfan Sheriff3ef889b2012-04-17 23:15:29 -0700156 */
157 public static final int NSD_STATE_ENABLED = 2;
158
Irfan Sheriff7d024d32012-03-22 17:01:39 -0700159 private static final int BASE = Protocol.BASE_NSD_MANAGER;
160
161 /** @hide */
162 public static final int DISCOVER_SERVICES = BASE + 1;
163 /** @hide */
164 public static final int DISCOVER_SERVICES_STARTED = BASE + 2;
165 /** @hide */
166 public static final int DISCOVER_SERVICES_FAILED = BASE + 3;
167 /** @hide */
168 public static final int SERVICE_FOUND = BASE + 4;
169 /** @hide */
170 public static final int SERVICE_LOST = BASE + 5;
171
172 /** @hide */
173 public static final int STOP_DISCOVERY = BASE + 6;
174 /** @hide */
175 public static final int STOP_DISCOVERY_FAILED = BASE + 7;
176 /** @hide */
177 public static final int STOP_DISCOVERY_SUCCEEDED = BASE + 8;
178
179 /** @hide */
180 public static final int REGISTER_SERVICE = BASE + 9;
181 /** @hide */
182 public static final int REGISTER_SERVICE_FAILED = BASE + 10;
183 /** @hide */
184 public static final int REGISTER_SERVICE_SUCCEEDED = BASE + 11;
185
186 /** @hide */
Irfan Sheriff92784672012-04-13 12:15:41 -0700187 public static final int UNREGISTER_SERVICE = BASE + 12;
Irfan Sheriff7d024d32012-03-22 17:01:39 -0700188 /** @hide */
Irfan Sheriff92784672012-04-13 12:15:41 -0700189 public static final int UNREGISTER_SERVICE_FAILED = BASE + 13;
Irfan Sheriff7d024d32012-03-22 17:01:39 -0700190 /** @hide */
Irfan Sheriff92784672012-04-13 12:15:41 -0700191 public static final int UNREGISTER_SERVICE_SUCCEEDED = BASE + 14;
Irfan Sheriff7d024d32012-03-22 17:01:39 -0700192
193 /** @hide */
Irfan Sheriff92784672012-04-13 12:15:41 -0700194 public static final int RESOLVE_SERVICE = BASE + 18;
Irfan Sheriff817388e2012-04-11 14:52:19 -0700195 /** @hide */
Irfan Sheriff92784672012-04-13 12:15:41 -0700196 public static final int RESOLVE_SERVICE_FAILED = BASE + 19;
Irfan Sheriff817388e2012-04-11 14:52:19 -0700197 /** @hide */
Irfan Sheriff92784672012-04-13 12:15:41 -0700198 public static final int RESOLVE_SERVICE_SUCCEEDED = BASE + 20;
Irfan Sheriff817388e2012-04-11 14:52:19 -0700199
Irfan Sheriff92784672012-04-13 12:15:41 -0700200 /** @hide */
Irfan Sheriff3ef889b2012-04-17 23:15:29 -0700201 public static final int ENABLE = BASE + 24;
202 /** @hide */
203 public static final int DISABLE = BASE + 25;
204
Irfan Sheriff22af38c2012-05-03 16:44:27 -0700205 /** @hide */
206 public static final int NATIVE_DAEMON_EVENT = BASE + 26;
207
208 /** Dns based service discovery protocol */
209 public static final int PROTOCOL_DNS_SD = 0x0001;
210
211 private Context mContext;
212
213 private static final int INVALID_LISTENER_KEY = 0;
214 private int mListenerKey = 1;
215 private final SparseArray mListenerMap = new SparseArray();
216 private final SparseArray<NsdServiceInfo> mServiceMap = new SparseArray<NsdServiceInfo>();
217 private final Object mMapLock = new Object();
218
219 private final AsyncChannel mAsyncChannel = new AsyncChannel();
220 private ServiceHandler mHandler;
221 private final CountDownLatch mConnected = new CountDownLatch(1);
Irfan Sheriff3ef889b2012-04-17 23:15:29 -0700222
Irfan Sheriff7d024d32012-03-22 17:01:39 -0700223 /**
224 * Create a new Nsd instance. Applications use
225 * {@link android.content.Context#getSystemService Context.getSystemService()} to retrieve
226 * {@link android.content.Context#NSD_SERVICE Context.NSD_SERVICE}.
227 * @param service the Binder interface
228 * @hide - hide this because it takes in a parameter of type INsdManager, which
229 * is a system private class.
230 */
Irfan Sheriff22af38c2012-05-03 16:44:27 -0700231 public NsdManager(Context context, INsdManager service) {
Irfan Sheriff7d024d32012-03-22 17:01:39 -0700232 mService = service;
Irfan Sheriff22af38c2012-05-03 16:44:27 -0700233 mContext = context;
234 init();
Irfan Sheriff7d024d32012-03-22 17:01:39 -0700235 }
236
237 /**
Irfan Sheriff22af38c2012-05-03 16:44:27 -0700238 * Failures are passed with {@link RegistrationListener#onRegistrationFailed},
239 * {@link RegistrationListener#onUnregistrationFailed},
240 * {@link DiscoveryListener#onStartDiscoveryFailed},
241 * {@link DiscoveryListener#onStopDiscoveryFailed} or {@link ResolveListener#onResolveFailed}.
242 *
Irfan Sheriff7d024d32012-03-22 17:01:39 -0700243 * Indicates that the operation failed due to an internal error.
244 */
Irfan Sheriff22af38c2012-05-03 16:44:27 -0700245 public static final int FAILURE_INTERNAL_ERROR = 0;
Irfan Sheriff7d024d32012-03-22 17:01:39 -0700246
247 /**
Irfan Sheriff817388e2012-04-11 14:52:19 -0700248 * Indicates that the operation failed because it is already active.
249 */
Irfan Sheriff22af38c2012-05-03 16:44:27 -0700250 public static final int FAILURE_ALREADY_ACTIVE = 3;
Irfan Sheriff817388e2012-04-11 14:52:19 -0700251
252 /**
Irfan Sheriff22af38c2012-05-03 16:44:27 -0700253 * Indicates that the operation failed because the maximum outstanding
254 * requests from the applications have reached.
Irfan Sheriff817388e2012-04-11 14:52:19 -0700255 */
Irfan Sheriff22af38c2012-05-03 16:44:27 -0700256 public static final int FAILURE_MAX_LIMIT = 4;
Irfan Sheriff7d024d32012-03-22 17:01:39 -0700257
Irfan Sheriff92784672012-04-13 12:15:41 -0700258 /** Interface for callback invocation for service discovery */
Irfan Sheriff22af38c2012-05-03 16:44:27 -0700259 public interface DiscoveryListener {
Irfan Sheriff7d024d32012-03-22 17:01:39 -0700260
Irfan Sheriff22af38c2012-05-03 16:44:27 -0700261 public void onStartDiscoveryFailed(String serviceType, int errorCode);
Irfan Sheriff7d024d32012-03-22 17:01:39 -0700262
Irfan Sheriff22af38c2012-05-03 16:44:27 -0700263 public void onStopDiscoveryFailed(String serviceType, int errorCode);
Irfan Sheriff7d024d32012-03-22 17:01:39 -0700264
Irfan Sheriff22af38c2012-05-03 16:44:27 -0700265 public void onDiscoveryStarted(String serviceType);
Irfan Sheriff7d024d32012-03-22 17:01:39 -0700266
Irfan Sheriff22af38c2012-05-03 16:44:27 -0700267 public void onDiscoveryStopped(String serviceType);
268
269 public void onServiceFound(NsdServiceInfo serviceInfo);
270
271 public void onServiceLost(NsdServiceInfo serviceInfo);
Irfan Sheriff7d024d32012-03-22 17:01:39 -0700272
273 }
274
Irfan Sheriff92784672012-04-13 12:15:41 -0700275 /** Interface for callback invocation for service registration */
Irfan Sheriff22af38c2012-05-03 16:44:27 -0700276 public interface RegistrationListener {
Irfan Sheriff7d024d32012-03-22 17:01:39 -0700277
Irfan Sheriff22af38c2012-05-03 16:44:27 -0700278 public void onRegistrationFailed(NsdServiceInfo serviceInfo, int errorCode);
Irfan Sheriff7d024d32012-03-22 17:01:39 -0700279
Irfan Sheriff22af38c2012-05-03 16:44:27 -0700280 public void onUnregistrationFailed(NsdServiceInfo serviceInfo, int errorCode);
Irfan Sheriff7d024d32012-03-22 17:01:39 -0700281
Irfan Sheriff22af38c2012-05-03 16:44:27 -0700282 public void onServiceRegistered(NsdServiceInfo serviceInfo);
Irfan Sheriff7d024d32012-03-22 17:01:39 -0700283
Irfan Sheriff22af38c2012-05-03 16:44:27 -0700284 public void onServiceUnregistered(NsdServiceInfo serviceInfo);
Irfan Sheriff7d024d32012-03-22 17:01:39 -0700285 }
286
Irfan Sheriff92784672012-04-13 12:15:41 -0700287 /** Interface for callback invocation for service resolution */
Irfan Sheriff22af38c2012-05-03 16:44:27 -0700288 public interface ResolveListener {
Irfan Sheriff7d024d32012-03-22 17:01:39 -0700289
Irfan Sheriff22af38c2012-05-03 16:44:27 -0700290 public void onResolveFailed(NsdServiceInfo serviceInfo, int errorCode);
Irfan Sheriff7d024d32012-03-22 17:01:39 -0700291
Irfan Sheriff22af38c2012-05-03 16:44:27 -0700292 public void onServiceResolved(NsdServiceInfo serviceInfo);
Irfan Sheriff7d024d32012-03-22 17:01:39 -0700293 }
294
Irfan Sheriff22af38c2012-05-03 16:44:27 -0700295 private class ServiceHandler extends Handler {
296 ServiceHandler(Looper looper) {
297 super(looper);
Irfan Sheriff7d024d32012-03-22 17:01:39 -0700298 }
Irfan Sheriff7d024d32012-03-22 17:01:39 -0700299
Irfan Sheriff22af38c2012-05-03 16:44:27 -0700300 @Override
301 public void handleMessage(Message message) {
302 Object listener = getListener(message.arg2);
303 boolean listenerRemove = true;
304 switch (message.what) {
305 case AsyncChannel.CMD_CHANNEL_HALF_CONNECTED:
306 mAsyncChannel.sendMessage(AsyncChannel.CMD_CHANNEL_FULL_CONNECTION);
Irfan Sheriff22af38c2012-05-03 16:44:27 -0700307 break;
308 case AsyncChannel.CMD_CHANNEL_FULLY_CONNECTED:
Robert Greenwaltaf2eefb2013-05-02 15:45:32 -0700309 mConnected.countDown();
Irfan Sheriff22af38c2012-05-03 16:44:27 -0700310 break;
311 case AsyncChannel.CMD_CHANNEL_DISCONNECTED:
312 Log.e(TAG, "Channel lost");
313 break;
314 case DISCOVER_SERVICES_STARTED:
315 String s = ((NsdServiceInfo) message.obj).getServiceType();
316 ((DiscoveryListener) listener).onDiscoveryStarted(s);
317 // Keep listener until stop discovery
318 listenerRemove = false;
319 break;
320 case DISCOVER_SERVICES_FAILED:
321 ((DiscoveryListener) listener).onStartDiscoveryFailed(
322 getNsdService(message.arg2).getServiceType(), message.arg1);
323 break;
324 case SERVICE_FOUND:
325 ((DiscoveryListener) listener).onServiceFound((NsdServiceInfo) message.obj);
326 // Keep listener until stop discovery
327 listenerRemove = false;
328 break;
329 case SERVICE_LOST:
330 ((DiscoveryListener) listener).onServiceLost((NsdServiceInfo) message.obj);
331 // Keep listener until stop discovery
332 listenerRemove = false;
333 break;
334 case STOP_DISCOVERY_FAILED:
335 ((DiscoveryListener) listener).onStopDiscoveryFailed(
336 getNsdService(message.arg2).getServiceType(), message.arg1);
337 break;
338 case STOP_DISCOVERY_SUCCEEDED:
339 ((DiscoveryListener) listener).onDiscoveryStopped(
340 getNsdService(message.arg2).getServiceType());
341 break;
342 case REGISTER_SERVICE_FAILED:
343 ((RegistrationListener) listener).onRegistrationFailed(
344 getNsdService(message.arg2), message.arg1);
345 break;
346 case REGISTER_SERVICE_SUCCEEDED:
347 ((RegistrationListener) listener).onServiceRegistered(
348 (NsdServiceInfo) message.obj);
349 // Keep listener until unregister
350 listenerRemove = false;
351 break;
352 case UNREGISTER_SERVICE_FAILED:
353 ((RegistrationListener) listener).onUnregistrationFailed(
354 getNsdService(message.arg2), message.arg1);
355 break;
356 case UNREGISTER_SERVICE_SUCCEEDED:
357 ((RegistrationListener) listener).onServiceUnregistered(
358 getNsdService(message.arg2));
359 break;
360 case RESOLVE_SERVICE_FAILED:
361 ((ResolveListener) listener).onResolveFailed(
362 getNsdService(message.arg2), message.arg1);
363 break;
364 case RESOLVE_SERVICE_SUCCEEDED:
365 ((ResolveListener) listener).onServiceResolved((NsdServiceInfo) message.obj);
366 break;
367 default:
368 Log.d(TAG, "Ignored " + message);
369 break;
Irfan Sheriff7d024d32012-03-22 17:01:39 -0700370 }
Irfan Sheriff22af38c2012-05-03 16:44:27 -0700371 if (listenerRemove) {
372 removeListener(message.arg2);
Irfan Sheriff7d024d32012-03-22 17:01:39 -0700373 }
374 }
Irfan Sheriff92784672012-04-13 12:15:41 -0700375 }
376
Irfan Sheriff22af38c2012-05-03 16:44:27 -0700377 private int putListener(Object listener, NsdServiceInfo s) {
378 if (listener == null) return INVALID_LISTENER_KEY;
379 int key;
380 synchronized (mMapLock) {
381 do {
382 key = mListenerKey++;
383 } while (key == INVALID_LISTENER_KEY);
384 mListenerMap.put(key, listener);
385 mServiceMap.put(key, s);
386 }
387 return key;
388 }
389
390 private Object getListener(int key) {
391 if (key == INVALID_LISTENER_KEY) return null;
392 synchronized (mMapLock) {
393 return mListenerMap.get(key);
394 }
395 }
396
397 private NsdServiceInfo getNsdService(int key) {
398 synchronized (mMapLock) {
399 return mServiceMap.get(key);
400 }
401 }
402
403 private void removeListener(int key) {
404 if (key == INVALID_LISTENER_KEY) return;
405 synchronized (mMapLock) {
406 mListenerMap.remove(key);
407 mServiceMap.remove(key);
408 }
409 }
410
411 private int getListenerKey(Object listener) {
412 synchronized (mMapLock) {
413 int valueIndex = mListenerMap.indexOfValue(listener);
414 if (valueIndex != -1) {
415 return mListenerMap.keyAt(valueIndex);
416 }
417 }
418 return INVALID_LISTENER_KEY;
419 }
420
421
Irfan Sheriff7d024d32012-03-22 17:01:39 -0700422 /**
Irfan Sheriff22af38c2012-05-03 16:44:27 -0700423 * Initialize AsyncChannel
Irfan Sheriff7d024d32012-03-22 17:01:39 -0700424 */
Irfan Sheriff22af38c2012-05-03 16:44:27 -0700425 private void init() {
426 final Messenger messenger = getMessenger();
Irfan Sheriff7d024d32012-03-22 17:01:39 -0700427 if (messenger == null) throw new RuntimeException("Failed to initialize");
Irfan Sheriff22af38c2012-05-03 16:44:27 -0700428 HandlerThread t = new HandlerThread("NsdManager");
429 t.start();
430 mHandler = new ServiceHandler(t.getLooper());
431 mAsyncChannel.connect(mContext, mHandler, messenger);
432 try {
433 mConnected.await();
434 } catch (InterruptedException e) {
435 Log.e(TAG, "interrupted wait at init");
436 }
Irfan Sheriff7d024d32012-03-22 17:01:39 -0700437 }
438
439 /**
Irfan Sheriff92784672012-04-13 12:15:41 -0700440 * Register a service to be discovered by other services.
441 *
442 * <p> The function call immediately returns after sending a request to register service
443 * to the framework. The application is notified of a success to initiate
Irfan Sheriff22af38c2012-05-03 16:44:27 -0700444 * discovery through the callback {@link RegistrationListener#onServiceRegistered} or a failure
445 * through {@link RegistrationListener#onRegistrationFailed}.
Irfan Sheriff92784672012-04-13 12:15:41 -0700446 *
Irfan Sheriff22af38c2012-05-03 16:44:27 -0700447 * @param serviceInfo The service being registered
448 * @param protocolType The service discovery protocol
449 * @param listener The listener notifies of a successful registration and is used to
450 * unregister this service through a call on {@link #unregisterService}. Cannot be null.
Irfan Sheriff7d024d32012-03-22 17:01:39 -0700451 */
Irfan Sheriff22af38c2012-05-03 16:44:27 -0700452 public void registerService(NsdServiceInfo serviceInfo, int protocolType,
453 RegistrationListener listener) {
454 if (TextUtils.isEmpty(serviceInfo.getServiceName()) ||
455 TextUtils.isEmpty(serviceInfo.getServiceType())) {
Irfan Sheriff92784672012-04-13 12:15:41 -0700456 throw new IllegalArgumentException("Service name or type cannot be empty");
457 }
Irfan Sheriff22af38c2012-05-03 16:44:27 -0700458 if (serviceInfo.getPort() <= 0) {
Irfan Sheriff92784672012-04-13 12:15:41 -0700459 throw new IllegalArgumentException("Invalid port number");
460 }
Irfan Sheriff22af38c2012-05-03 16:44:27 -0700461 if (listener == null) {
462 throw new IllegalArgumentException("listener cannot be null");
463 }
464 if (protocolType != PROTOCOL_DNS_SD) {
465 throw new IllegalArgumentException("Unsupported protocol");
466 }
467 mAsyncChannel.sendMessage(REGISTER_SERVICE, 0, putListener(listener, serviceInfo),
468 serviceInfo);
Irfan Sheriff7d024d32012-03-22 17:01:39 -0700469 }
470
Irfan Sheriff92784672012-04-13 12:15:41 -0700471 /**
Irfan Sheriff22af38c2012-05-03 16:44:27 -0700472 * Unregister a service registered through {@link #registerService}. A successful
473 * unregister is notified to the application with a call to
474 * {@link RegistrationListener#onServiceUnregistered}.
475 *
476 * @param listener This should be the listener object that was passed to
477 * {@link #registerService}. It identifies the service that should be unregistered
478 * and notifies of a successful unregistration.
Irfan Sheriff92784672012-04-13 12:15:41 -0700479 */
Irfan Sheriff22af38c2012-05-03 16:44:27 -0700480 public void unregisterService(RegistrationListener listener) {
481 int id = getListenerKey(listener);
482 if (id == INVALID_LISTENER_KEY) {
483 throw new IllegalArgumentException("listener not registered");
484 }
485 if (listener == null) {
486 throw new IllegalArgumentException("listener cannot be null");
487 }
488 mAsyncChannel.sendMessage(UNREGISTER_SERVICE, 0, id);
Irfan Sheriff7d024d32012-03-22 17:01:39 -0700489 }
490
Irfan Sheriff92784672012-04-13 12:15:41 -0700491 /**
492 * Initiate service discovery to browse for instances of a service type. Service discovery
493 * consumes network bandwidth and will continue until the application calls
494 * {@link #stopServiceDiscovery}.
495 *
496 * <p> The function call immediately returns after sending a request to start service
497 * discovery to the framework. The application is notified of a success to initiate
Irfan Sheriff22af38c2012-05-03 16:44:27 -0700498 * discovery through the callback {@link DiscoveryListener#onDiscoveryStarted} or a failure
499 * through {@link DiscoveryListener#onStartDiscoveryFailed}.
Irfan Sheriff92784672012-04-13 12:15:41 -0700500 *
501 * <p> Upon successful start, application is notified when a service is found with
Irfan Sheriff22af38c2012-05-03 16:44:27 -0700502 * {@link DiscoveryListener#onServiceFound} or when a service is lost with
503 * {@link DiscoveryListener#onServiceLost}.
Irfan Sheriff92784672012-04-13 12:15:41 -0700504 *
505 * <p> Upon failure to start, service discovery is not active and application does
506 * not need to invoke {@link #stopServiceDiscovery}
507 *
Irfan Sheriff92784672012-04-13 12:15:41 -0700508 * @param serviceType The service type being discovered. Examples include "_http._tcp" for
509 * http services or "_ipp._tcp" for printers
Irfan Sheriff22af38c2012-05-03 16:44:27 -0700510 * @param protocolType The service discovery protocol
511 * @param listener The listener notifies of a successful discovery and is used
512 * to stop discovery on this serviceType through a call on {@link #stopServiceDiscovery}.
513 * Cannot be null.
Irfan Sheriff92784672012-04-13 12:15:41 -0700514 */
Irfan Sheriff22af38c2012-05-03 16:44:27 -0700515 public void discoverServices(String serviceType, int protocolType, DiscoveryListener listener) {
Irfan Sheriff92784672012-04-13 12:15:41 -0700516 if (listener == null) {
Irfan Sheriff22af38c2012-05-03 16:44:27 -0700517 throw new IllegalArgumentException("listener cannot be null");
Irfan Sheriff92784672012-04-13 12:15:41 -0700518 }
519 if (TextUtils.isEmpty(serviceType)) {
Irfan Sheriff22af38c2012-05-03 16:44:27 -0700520 throw new IllegalArgumentException("Service type cannot be empty");
Irfan Sheriff92784672012-04-13 12:15:41 -0700521 }
Irfan Sheriff22af38c2012-05-03 16:44:27 -0700522
523 if (protocolType != PROTOCOL_DNS_SD) {
524 throw new IllegalArgumentException("Unsupported protocol");
525 }
526
527 NsdServiceInfo s = new NsdServiceInfo();
Irfan Sheriff7d024d32012-03-22 17:01:39 -0700528 s.setServiceType(serviceType);
Irfan Sheriff22af38c2012-05-03 16:44:27 -0700529 mAsyncChannel.sendMessage(DISCOVER_SERVICES, 0, putListener(listener, s), s);
Irfan Sheriff7d024d32012-03-22 17:01:39 -0700530 }
531
Irfan Sheriff92784672012-04-13 12:15:41 -0700532 /**
533 * Stop service discovery initiated with {@link #discoverServices}. An active service
Irfan Sheriff22af38c2012-05-03 16:44:27 -0700534 * discovery is notified to the application with {@link DiscoveryListener#onDiscoveryStarted}
535 * and it stays active until the application invokes a stop service discovery. A successful
536 * stop is notified to with a call to {@link DiscoveryListener#onDiscoveryStopped}.
Irfan Sheriff92784672012-04-13 12:15:41 -0700537 *
Irfan Sheriff22af38c2012-05-03 16:44:27 -0700538 * <p> Upon failure to stop service discovery, application is notified through
539 * {@link DiscoveryListener#onStopDiscoveryFailed}.
Irfan Sheriff92784672012-04-13 12:15:41 -0700540 *
Irfan Sheriff22af38c2012-05-03 16:44:27 -0700541 * @param listener This should be the listener object that was passed to {@link #discoverServices}.
542 * It identifies the discovery that should be stopped and notifies of a successful stop.
Irfan Sheriff92784672012-04-13 12:15:41 -0700543 */
Irfan Sheriff22af38c2012-05-03 16:44:27 -0700544 public void stopServiceDiscovery(DiscoveryListener listener) {
545 int id = getListenerKey(listener);
546 if (id == INVALID_LISTENER_KEY) {
547 throw new IllegalArgumentException("service discovery not active on listener");
548 }
549 if (listener == null) {
550 throw new IllegalArgumentException("listener cannot be null");
551 }
552 mAsyncChannel.sendMessage(STOP_DISCOVERY, 0, id);
Irfan Sheriff7d024d32012-03-22 17:01:39 -0700553 }
554
Irfan Sheriff92784672012-04-13 12:15:41 -0700555 /**
556 * Resolve a discovered service. An application can resolve a service right before
557 * establishing a connection to fetch the IP and port details on which to setup
558 * the connection.
559 *
Irfan Sheriff22af38c2012-05-03 16:44:27 -0700560 * @param serviceInfo service to be resolved
Irfan Sheriff92784672012-04-13 12:15:41 -0700561 * @param listener to receive callback upon success or failure. Cannot be null.
562 */
Irfan Sheriff22af38c2012-05-03 16:44:27 -0700563 public void resolveService(NsdServiceInfo serviceInfo, ResolveListener listener) {
564 if (TextUtils.isEmpty(serviceInfo.getServiceName()) ||
565 TextUtils.isEmpty(serviceInfo.getServiceType())) {
Irfan Sheriff92784672012-04-13 12:15:41 -0700566 throw new IllegalArgumentException("Service name or type cannot be empty");
567 }
Irfan Sheriff22af38c2012-05-03 16:44:27 -0700568 if (listener == null) {
569 throw new IllegalArgumentException("listener cannot be null");
570 }
571 mAsyncChannel.sendMessage(RESOLVE_SERVICE, 0, putListener(listener, serviceInfo),
572 serviceInfo);
Irfan Sheriff817388e2012-04-11 14:52:19 -0700573 }
574
Irfan Sheriff3ef889b2012-04-17 23:15:29 -0700575 /** Internal use only @hide */
576 public void setEnabled(boolean enabled) {
577 try {
578 mService.setEnabled(enabled);
579 } catch (RemoteException e) { }
580 }
581
Irfan Sheriff7d024d32012-03-22 17:01:39 -0700582 /**
583 * Get a reference to NetworkService handler. This is used to establish
584 * an AsyncChannel communication with the service
585 *
586 * @return Messenger pointing to the NetworkService handler
587 */
588 private Messenger getMessenger() {
589 try {
590 return mService.getMessenger();
591 } catch (RemoteException e) {
592 return null;
593 }
594 }
595}