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 SSDP.
24 /// <since_tizen> 4 </since_tizen>
25 public class SsdpBrowser : INsdBrowser
27 private string _target;
28 private uint _browserHandle;
29 private event EventHandler<SsdpServiceFoundEventArgs> _serviceFound;
30 private Interop.Nsd.Ssdp.ServiceFoundCallback _serviceFoundCallback;
33 /// This event is raised when service has become available or unavailable during service discovery using SSDP.
35 /// <since_tizen> 4 </since_tizen>
36 public event EventHandler<SsdpServiceFoundEventArgs> ServiceFound
40 _serviceFound += value;
45 _serviceFound -= value;
50 /// A public constructor for SsdpBrowser class to create a SsdpBrowser instance for the given target.
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.service_discovery.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)
59 SsdpInitializer ssdpInit = Globals.s_threadSsd.Value;
60 Log.Info(Globals.LogTag, "Initialize ThreadLocal<SsdpInitializer> instance = " + ssdpInit);
63 Log.Debug(Globals.LogTag, "target is null");
64 NsdErrorFactory.ThrowSsdpException((int)SsdpError.InvalidParameter);
71 /// Starts browsing the SSDP 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 /// <feature>http://tizen.org/feature/network.service_discovery.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()
83 SsdpInitializer ssdpInit = Globals.s_threadSsd.Value;
84 Log.Info(Globals.LogTag, "Initialize ThreadLocal<SsdpInitializer> instance = " + ssdpInit);
86 _serviceFoundCallback = (SsdpServiceState state, uint service, IntPtr userData) =>
88 if (_serviceFound != null)
90 Log.Info(Globals.LogTag, "Inside Service found callback");
91 SsdpService ssdpService = new SsdpService(service);
92 _serviceFound(null, new SsdpServiceFoundEventArgs(state, ssdpService));
96 int ret = Interop.Nsd.Ssdp.StartBrowsing(_target, out _browserHandle, _serviceFoundCallback, IntPtr.Zero);
97 if (ret != (int)SsdpError.None)
99 Log.Error(Globals.LogTag, "Failed to discover Ssdp remote service, Error - " + (SsdpError)ret);
100 NsdErrorFactory.ThrowSsdpException(ret);
105 /// Stops browsing the SSDP remote service.
107 /// <since_tizen> 4 </since_tizen>
108 /// <privilege>http://tizen.org/privilege/internet</privilege>
109 /// <feature>http://tizen.org/feature/network.service_discovery.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()
115 int ret = Interop.Nsd.Ssdp.StopBrowsing(_browserHandle);
116 if (ret != (int)SsdpError.None)
118 Log.Error(Globals.LogTag, "Failed to stop discovery of Ssdp remote service, Error - " + (SsdpError)ret);
119 NsdErrorFactory.ThrowSsdpException(ret);