[Nsd]Fixed ArgumentException and implemented IDisposable in INsdService
[platform/core/csapi/nsd.git] / Tizen.Network.Nsd / Tizen.Network.Nsd / SsdpBrowser.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 SSDP.
23     /// </summary>
24     /// <since_tizen> 4 </since_tizen>
25     public class SsdpBrowser : INsdBrowser
26     {
27         private string _target;
28         private uint _browserHandle;
29         private event EventHandler<SsdpServiceFoundEventArgs> _serviceFound;
30         private Interop.Nsd.Ssdp.ServiceFoundCallback _serviceFoundCallback;
31
32         /// <summary>
33         /// This event is raised when service has become available or unavailable during service discovery using SSDP.
34         /// </summary>
35         /// <since_tizen> 4 </since_tizen>
36         public event EventHandler<SsdpServiceFoundEventArgs> 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 SsdpBrowser class to create a SsdpBrowser instance for the given target.
51         /// </summary>
52         /// <since_tizen> 4 </since_tizen>
53         /// <param name="target">The target to browse for the service.</param>
54         /// <feature>http://tizen.org/feature/network.ssdp</feature>
55         /// <exception cref="ArgumentException">Thrown when target is null.</exception>
56         /// <exception cref="NotSupportedException">Thrown when SSDP is not supported.</exception>
57         public SsdpBrowser(string target)
58         {
59             SsdpInitializer ssdpInit = Globals.s_threadSsd.Value;
60             Log.Info(Globals.LogTag, "Initialize ThreadLocal<SsdpInitializer> instance = " + ssdpInit);
61             if (target == null)
62             {
63                 Log.Debug(Globals.LogTag, "target is null");
64                 NsdErrorFactory.ThrowSsdpException((int)SsdpError.InvalidParameter);
65             }
66
67             _target = target;
68         }
69
70         /// <summary>
71         /// Starts browsing the SSDP 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         /// <feature>http://tizen.org/feature/network.ssdp</feature>
79         /// <exception cref="InvalidOperationException">Thrown when any other error occured.</exception>
80         /// <exception cref="NotSupportedException">Thrown when SSDP is not supported.</exception>
81         public void StartDiscovery()
82         {
83             SsdpInitializer ssdpInit = Globals.s_threadSsd.Value;
84             Log.Info(Globals.LogTag, "Initialize ThreadLocal<SsdpInitializer> instance = " + ssdpInit);
85
86             _serviceFoundCallback = (SsdpServiceState state, uint service, IntPtr userData) =>
87             {
88                 if (_serviceFound != null)
89                 {
90                     Log.Info(Globals.LogTag, "Inside Service found callback");
91                     SsdpService ssdpService = new SsdpService(service);
92                     _serviceFound(null, new SsdpServiceFoundEventArgs(state, ssdpService));
93                 }
94             };
95
96             int ret = Interop.Nsd.Ssdp.StartBrowsing(_target, out _browserHandle, _serviceFoundCallback, IntPtr.Zero);
97             if (ret != (int)SsdpError.None)
98             {
99                 Log.Error(Globals.LogTag, "Failed to discover Ssdp remote service, Error - " + (SsdpError)ret);
100                 NsdErrorFactory.ThrowSsdpException(ret);
101             }
102         }
103
104         /// <summary>
105         /// Stops browsing the SSDP remote service.
106         /// </summary>
107         /// <since_tizen> 4 </since_tizen>
108         /// <privilege>http://tizen.org/privilege/internet</privilege>
109         /// <feature>http://tizen.org/feature/network.ssdp</feature>
110         /// <exception cref="InvalidOperationException">Thrown when any other error occured.</exception>
111         /// <exception cref="NotSupportedException">Thrown when SSDP is not supported.</exception>
112         /// <exception cref="UnauthorizedAccessException">Thrown when permission is denied.</exception>
113         public void StopDiscovery()
114         {
115             int ret = Interop.Nsd.Ssdp.StopBrowsing(_browserHandle);
116             if (ret != (int)SsdpError.None)
117             {
118                 Log.Error(Globals.LogTag, "Failed to stop discovery of Ssdp remote service, Error - " + (SsdpError)ret);
119                 NsdErrorFactory.ThrowSsdpException(ret);
120             }
121         }
122     }
123 }