[Nsd]Fixed ArgumentException and implemented IDisposable in INsdService
[platform/core/csapi/nsd.git] / Tizen.Network.Nsd / Tizen.Network.Nsd / DnssdBrowser.cs
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
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 using System;
18
19 namespace Tizen.Network.Nsd
20 {
21     /// <summary>
22     /// This class is used for managing network service discovery using DNSSD.
23     /// </summary>
24     /// <since_tizen> 4 </since_tizen>
25     public class DnssdBrowser : INsdBrowser
26     {
27         private string _serviceType;
28         private uint _browserHandle;
29         private event EventHandler<DnssdServiceFoundEventArgs> _serviceFound;
30         private Interop.Nsd.Dnssd.ServiceFoundCallback _serviceFoundCallback;
31
32         /// <summary>
33         /// This event is raised when a DNSSD service is found during service discovery.
34         /// </summary>
35         /// <since_tizen> 4 </since_tizen>
36         public event EventHandler<DnssdServiceFoundEventArgs> ServiceFound
37         {
38             add
39             {
40                 _serviceFound += value;
41             }
42
43             remove
44             {
45                 _serviceFound -= value;
46             }
47         }
48
49         /// <summary>
50         /// A public constructor for DnssdBrowser class to create a DnssdBrowser instance for the given service type.
51         /// </summary>
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)
58         {
59             DnssdInitializer dnssdInit = Globals.s_threadDns.Value;
60             Log.Info(Globals.LogTag, "Initialize ThreadLocal<DnssdInitializer> instance = " + dnssdInit);
61             if(serviceType == null)
62             {
63                 Log.Debug(Globals.LogTag, "serviceType is null");
64                 NsdErrorFactory.ThrowDnssdException((int)DnssdError.InvalidParameter);
65             }
66
67             _serviceType = serviceType;
68         }
69
70         /// <summary>
71         /// Starts browsing the DNSSD remote service.
72         /// </summary>
73         /// <remarks>
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().
76         /// </remarks>
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()
84         {
85             DnssdInitializer dnssdInit = Globals.s_threadDns.Value;
86             Log.Info(Globals.LogTag, "Initialize ThreadLocal<DnssdInitializer> instance = " + dnssdInit);
87
88             _serviceFoundCallback = (DnssdServiceState state, uint service, IntPtr userData) =>
89             {
90                 if (_serviceFound != null)
91                 {
92                     Log.Info(Globals.LogTag, "Inside Service found callback");
93                     DnssdService dnsService = new DnssdService(service);
94                     _serviceFound(null, new DnssdServiceFoundEventArgs(state, dnsService));
95                 }
96             };
97
98             int ret = Interop.Nsd.Dnssd.StartBrowsing(_serviceType, out _browserHandle, _serviceFoundCallback, IntPtr.Zero);
99             if (ret != (int)DnssdError.None)
100             {
101                 Log.Error(Globals.LogTag, "Failed to discover Dnssd remote service, Error - " + (DnssdError)ret);
102                 NsdErrorFactory.ThrowDnssdException(ret);
103             }
104         }
105
106         /// <summary>
107         /// Stops browsing the DNSSD remote service.
108         /// </summary>
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()
114         {
115             int ret = Interop.Nsd.Dnssd.StopBrowsing(_browserHandle);
116             if (ret != (int)DnssdError.None)
117             {
118                 Log.Error(Globals.LogTag, "Failed to stop discovery of Dnssd remote service, Error - " + (DnssdError)ret);
119                 NsdErrorFactory.ThrowDnssdException(ret);
120             }
121         }
122     }
123 }