2 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 namespace Tizen.Network.Nsd
22 /// This class is used for managing network service discovery using DNSSD.
24 /// <since_tizen> 4 </since_tizen>
25 public class DnssdBrowser : INsdBrowser
27 private string _serviceType;
28 private uint _browserHandle;
29 private event EventHandler<DnssdServiceFoundEventArgs> _serviceFound;
30 private Interop.Nsd.Dnssd.ServiceFoundCallback _serviceFoundCallback;
33 /// This event is raised when a DNSSD service is found during service discovery.
35 /// <since_tizen> 4 </since_tizen>
36 public event EventHandler<DnssdServiceFoundEventArgs> ServiceFound
40 _serviceFound += value;
45 _serviceFound -= value;
50 /// A public constructor for DnssdBrowser class to create a DnssdBrowser instance for the given service type.
52 /// <param name="serviceType">The DNSSD service type</param>
53 /// <since_tizen> 4 </since_tizen>
54 /// <feature>http://tizen.org/feature/network.dnssd</feature>
55 /// <exception cref="ArgumentException">Thrown when serviceType is null.</exception>
56 /// <exception cref="NotSupportedException">Thrown when DNSSD is not supported.</exception>
57 public DnssdBrowser(string serviceType)
59 DnssdInitializer dnssdInit = Globals.s_threadDns.Value;
60 Log.Info(Globals.LogTag, "Initialize ThreadLocal<DnssdInitializer> instance = " + dnssdInit);
61 if(serviceType == null)
63 Log.Debug(Globals.LogTag, "serviceType is null");
64 NsdErrorFactory.ThrowDnssdException((int)DnssdError.InvalidParameter);
67 _serviceType = serviceType;
71 /// Starts browsing the DNSSD remote service.
74 /// If there are any services available, ServiceFound event will be invoked.
75 /// Application will keep browsing for available/unavailable services until it calls StopDiscovery().
77 /// <since_tizen> 4 </since_tizen>
78 /// <privilege>http://tizen.org/privilege/internet</privilege>
79 /// <feature>http://tizen.org/feature/network.dnssd</feature>
80 /// <exception cref="InvalidOperationException">Thrown when any other error occurred.</exception>
81 /// <exception cref="NotSupportedException">Thrown when DNSSD is not supported.</exception>
82 /// <exception cref="UnauthorizedAccessException">Thrown when permission is denied.</exception>
83 public void StartDiscovery()
85 DnssdInitializer dnssdInit = Globals.s_threadDns.Value;
86 Log.Info(Globals.LogTag, "Initialize ThreadLocal<DnssdInitializer> instance = " + dnssdInit);
88 _serviceFoundCallback = (DnssdServiceState state, uint service, IntPtr userData) =>
90 if (_serviceFound != null)
92 Log.Info(Globals.LogTag, "Inside Service found callback");
93 DnssdService dnsService = new DnssdService(service);
94 _serviceFound(null, new DnssdServiceFoundEventArgs(state, dnsService));
98 int ret = Interop.Nsd.Dnssd.StartBrowsing(_serviceType, out _browserHandle, _serviceFoundCallback, IntPtr.Zero);
99 if (ret != (int)DnssdError.None)
101 Log.Error(Globals.LogTag, "Failed to discover Dnssd remote service, Error - " + (DnssdError)ret);
102 NsdErrorFactory.ThrowDnssdException(ret);
107 /// Stops browsing the DNSSD remote service.
109 /// <since_tizen> 4 </since_tizen>
110 /// <feature>http://tizen.org/feature/network.dnssd</feature>
111 /// <exception cref="InvalidOperationException">Thrown when any other error occurred.</exception>
112 /// <exception cref="NotSupportedException">Thrown when DNSSD is not supported.</exception>
113 public void StopDiscovery()
115 int ret = Interop.Nsd.Dnssd.StopBrowsing(_browserHandle);
116 if (ret != (int)DnssdError.None)
118 Log.Error(Globals.LogTag, "Failed to stop discovery of Dnssd remote service, Error - " + (DnssdError)ret);
119 NsdErrorFactory.ThrowDnssdException(ret);