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 com.example.android.nsdchat;
18 
19 import android.content.Context;
20 import android.net.nsd.NsdServiceInfo;
21 import android.net.nsd.NsdManager;
22 import android.util.Log;
23 
24 public class NsdHelper {
25 
26     Context mContext;
27 
28     NsdManager mNsdManager;
29     NsdManager.ResolveListener mResolveListener;
30     NsdManager.DiscoveryListener mDiscoveryListener;
31     NsdManager.RegistrationListener mRegistrationListener;
32 
33     public static final String SERVICE_TYPE = "_http._tcp.";
34 
35     public static final String TAG = "NsdHelper";
36     public String mServiceName = "NsdChat";
37 
38     NsdServiceInfo mService;
39 
NsdHelper(Context context)40     public NsdHelper(Context context) {
41         mContext = context;
42         mNsdManager = (NsdManager) context.getSystemService(Context.NSD_SERVICE);
43     }
44 
initializeNsd()45     public void initializeNsd() {
46         initializeResolveListener();
47 
48         //mNsdManager.init(mContext.getMainLooper(), this);
49 
50     }
51 
initializeDiscoveryListener()52     public void initializeDiscoveryListener() {
53         mDiscoveryListener = new NsdManager.DiscoveryListener() {
54 
55             @Override
56             public void onDiscoveryStarted(String regType) {
57                 Log.d(TAG, "Service discovery started");
58             }
59 
60             @Override
61             public void onServiceFound(NsdServiceInfo service) {
62                 Log.d(TAG, "Service discovery success" + service);
63                 if (!service.getServiceType().equals(SERVICE_TYPE)) {
64                     Log.d(TAG, "Unknown Service Type: " + service.getServiceType());
65                 } else if (service.getServiceName().equals(mServiceName)) {
66                     Log.d(TAG, "Same machine: " + mServiceName);
67                 } else if (service.getServiceName().contains(mServiceName)){
68                     mNsdManager.resolveService(service, mResolveListener);
69                 }
70             }
71 
72             @Override
73             public void onServiceLost(NsdServiceInfo service) {
74                 Log.e(TAG, "service lost" + service);
75                 if (mService == service) {
76                     mService = null;
77                 }
78             }
79 
80             @Override
81             public void onDiscoveryStopped(String serviceType) {
82                 Log.i(TAG, "Discovery stopped: " + serviceType);
83             }
84 
85             @Override
86             public void onStartDiscoveryFailed(String serviceType, int errorCode) {
87                 Log.e(TAG, "Discovery failed: Error code:" + errorCode);
88             }
89 
90             @Override
91             public void onStopDiscoveryFailed(String serviceType, int errorCode) {
92                 Log.e(TAG, "Discovery failed: Error code:" + errorCode);
93             }
94         };
95     }
96 
initializeResolveListener()97     public void initializeResolveListener() {
98         mResolveListener = new NsdManager.ResolveListener() {
99 
100             @Override
101             public void onResolveFailed(NsdServiceInfo serviceInfo, int errorCode) {
102                 Log.e(TAG, "Resolve failed" + errorCode);
103             }
104 
105             @Override
106             public void onServiceResolved(NsdServiceInfo serviceInfo) {
107                 Log.e(TAG, "Resolve Succeeded. " + serviceInfo);
108 
109                 if (serviceInfo.getServiceName().equals(mServiceName)) {
110                     Log.d(TAG, "Same IP.");
111                     return;
112                 }
113                 mService = serviceInfo;
114             }
115         };
116     }
117 
initializeRegistrationListener()118     public void initializeRegistrationListener() {
119         mRegistrationListener = new NsdManager.RegistrationListener() {
120 
121             @Override
122             public void onServiceRegistered(NsdServiceInfo NsdServiceInfo) {
123                 mServiceName = NsdServiceInfo.getServiceName();
124                 Log.d(TAG, "Service registered: " + mServiceName);
125             }
126 
127             @Override
128             public void onRegistrationFailed(NsdServiceInfo arg0, int arg1) {
129                 Log.d(TAG, "Service registration failed: " + arg1);
130             }
131 
132             @Override
133             public void onServiceUnregistered(NsdServiceInfo arg0) {
134                 Log.d(TAG, "Service unregistered: " + arg0.getServiceName());
135             }
136 
137             @Override
138             public void onUnregistrationFailed(NsdServiceInfo serviceInfo, int errorCode) {
139                 Log.d(TAG, "Service unregistration failed: " + errorCode);
140             }
141 
142         };
143     }
144 
registerService(int port)145     public void registerService(int port) {
146         tearDown();  // Cancel any previous registration request
147         initializeRegistrationListener();
148         NsdServiceInfo serviceInfo  = new NsdServiceInfo();
149         serviceInfo.setPort(port);
150         serviceInfo.setServiceName(mServiceName);
151         serviceInfo.setServiceType(SERVICE_TYPE);
152 
153         mNsdManager.registerService(
154                 serviceInfo, NsdManager.PROTOCOL_DNS_SD, mRegistrationListener);
155 
156     }
157 
discoverServices()158     public void discoverServices() {
159         stopDiscovery();  // Cancel any existing discovery request
160         initializeDiscoveryListener();
161         mNsdManager.discoverServices(
162                 SERVICE_TYPE, NsdManager.PROTOCOL_DNS_SD, mDiscoveryListener);
163     }
164 
stopDiscovery()165     public void stopDiscovery() {
166         if (mDiscoveryListener != null) {
167             try {
168                 mNsdManager.stopServiceDiscovery(mDiscoveryListener);
169             } finally {
170             }
171             mDiscoveryListener = null;
172         }
173     }
174 
getChosenServiceInfo()175     public NsdServiceInfo getChosenServiceInfo() {
176         return mService;
177     }
178 
tearDown()179     public void tearDown() {
180         if (mRegistrationListener != null) {
181             try {
182                 mNsdManager.unregisterService(mRegistrationListener);
183             } finally {
184             }
185             mRegistrationListener = null;
186         }
187     }
188 }
189