Home | History | Annotate | Download | only in nsdchat
      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 
     40     public NsdHelper(Context context) {
     41         mContext = context;
     42         mNsdManager = (NsdManager) context.getSystemService(Context.NSD_SERVICE);
     43     }
     44 
     45     public void initializeNsd() {
     46         initializeResolveListener();
     47         initializeDiscoveryListener();
     48         initializeRegistrationListener();
     49 
     50         //mNsdManager.init(mContext.getMainLooper(), this);
     51 
     52     }
     53 
     54     public void initializeDiscoveryListener() {
     55         mDiscoveryListener = new NsdManager.DiscoveryListener() {
     56 
     57             @Override
     58             public void onDiscoveryStarted(String regType) {
     59                 Log.d(TAG, "Service discovery started");
     60             }
     61 
     62             @Override
     63             public void onServiceFound(NsdServiceInfo service) {
     64                 Log.d(TAG, "Service discovery success" + service);
     65                 if (!service.getServiceType().equals(SERVICE_TYPE)) {
     66                     Log.d(TAG, "Unknown Service Type: " + service.getServiceType());
     67                 } else if (service.getServiceName().equals(mServiceName)) {
     68                     Log.d(TAG, "Same machine: " + mServiceName);
     69                 } else if (service.getServiceName().contains(mServiceName)){
     70                     mNsdManager.resolveService(service, mResolveListener);
     71                 }
     72             }
     73 
     74             @Override
     75             public void onServiceLost(NsdServiceInfo service) {
     76                 Log.e(TAG, "service lost" + service);
     77                 if (mService == service) {
     78                     mService = null;
     79                 }
     80             }
     81 
     82             @Override
     83             public void onDiscoveryStopped(String serviceType) {
     84                 Log.i(TAG, "Discovery stopped: " + serviceType);
     85             }
     86 
     87             @Override
     88             public void onStartDiscoveryFailed(String serviceType, int errorCode) {
     89                 Log.e(TAG, "Discovery failed: Error code:" + errorCode);
     90                 mNsdManager.stopServiceDiscovery(this);
     91             }
     92 
     93             @Override
     94             public void onStopDiscoveryFailed(String serviceType, int errorCode) {
     95                 Log.e(TAG, "Discovery failed: Error code:" + errorCode);
     96                 mNsdManager.stopServiceDiscovery(this);
     97             }
     98         };
     99     }
    100 
    101     public void initializeResolveListener() {
    102         mResolveListener = new NsdManager.ResolveListener() {
    103 
    104             @Override
    105             public void onResolveFailed(NsdServiceInfo serviceInfo, int errorCode) {
    106                 Log.e(TAG, "Resolve failed" + errorCode);
    107             }
    108 
    109             @Override
    110             public void onServiceResolved(NsdServiceInfo serviceInfo) {
    111                 Log.e(TAG, "Resolve Succeeded. " + serviceInfo);
    112 
    113                 if (serviceInfo.getServiceName().equals(mServiceName)) {
    114                     Log.d(TAG, "Same IP.");
    115                     return;
    116                 }
    117                 mService = serviceInfo;
    118             }
    119         };
    120     }
    121 
    122     public void initializeRegistrationListener() {
    123         mRegistrationListener = new NsdManager.RegistrationListener() {
    124 
    125             @Override
    126             public void onServiceRegistered(NsdServiceInfo NsdServiceInfo) {
    127                 mServiceName = NsdServiceInfo.getServiceName();
    128             }
    129 
    130             @Override
    131             public void onRegistrationFailed(NsdServiceInfo arg0, int arg1) {
    132             }
    133 
    134             @Override
    135             public void onServiceUnregistered(NsdServiceInfo arg0) {
    136             }
    137 
    138             @Override
    139             public void onUnregistrationFailed(NsdServiceInfo serviceInfo, int errorCode) {
    140             }
    141 
    142         };
    143     }
    144 
    145     public void registerService(int port) {
    146         NsdServiceInfo serviceInfo  = new NsdServiceInfo();
    147         serviceInfo.setPort(port);
    148         serviceInfo.setServiceName(mServiceName);
    149         serviceInfo.setServiceType(SERVICE_TYPE);
    150 
    151         mNsdManager.registerService(
    152                 serviceInfo, NsdManager.PROTOCOL_DNS_SD, mRegistrationListener);
    153 
    154     }
    155 
    156     public void discoverServices() {
    157         mNsdManager.discoverServices(
    158                 SERVICE_TYPE, NsdManager.PROTOCOL_DNS_SD, mDiscoveryListener);
    159     }
    160 
    161     public void stopDiscovery() {
    162         mNsdManager.stopServiceDiscovery(mDiscoveryListener);
    163     }
    164 
    165     public NsdServiceInfo getChosenServiceInfo() {
    166         return mService;
    167     }
    168 
    169     public void tearDown() {
    170         mNsdManager.unregisterService(mRegistrationListener);
    171     }
    172 }
    173