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.
17 using System.Threading;
19 namespace Tizen.Network.Nsd
21 internal static class Globals
23 internal const string LogTag = "Tizen.Network.Nsd";
25 internal static void DnssdInitialize()
27 int ret = Interop.Nsd.Dnssd.Initialize();
28 if(ret!=(int)DnssdError.None)
30 Log.Error(LogTag, "Failed to initialize Dnssd, Error - "+ (DnssdError)ret);
31 NsdErrorFactory.ThrowDnssdException(ret);
35 internal static void SsdpInitialize()
37 int ret = Interop.Nsd.Ssdp.Initialize();
38 if (ret != (int)SsdpError.None)
40 Log.Error(LogTag, "Failed to initialize Ssdp, Error - " + (SsdpError)ret);
41 NsdErrorFactory.ThrowSsdpException(ret);
45 internal static ThreadLocal<DnssdInitializer> s_threadDns = new ThreadLocal<DnssdInitializer>(() =>
47 Log.Info(LogTag, "Inside Dnssd ThreadLocal delegate");
48 return new DnssdInitializer();
51 internal static ThreadLocal<SsdpInitializer> s_threadSsd = new ThreadLocal<SsdpInitializer>(() =>
53 Log.Info(LogTag, "Inside Ssdp ThreadLocal delegate");
54 return new SsdpInitializer();
59 /// This class is used for managing local/network service registration and discovery using DNSSD/SSDP.
61 public static class NsdManager
64 /// Registers the DNSSD/SSDP local service for publishing.
67 /// A service created locally must be passed.
68 /// Name of the service must be set for DNSSD/SSDP both. Also, Port and Url must be set for DNSSD and SSDP respectively.
70 /// <param name="service">The DNSSD/SSDP service instance.</param>
71 /// <exception cref="NotSupportedException">Thrown when DNSSD/SSDP is not supported.</exception>
72 /// <exception cref="InvalidOperationException">Thrown when any other error occured.</exception>
73 public static void RegisterService(INsdService service)
75 if (service.GetType() == typeof(DnssdService))
77 DnssdService dnsService = (DnssdService)service;
78 dnsService.RegisterService();
81 else if (service.GetType() == typeof(SsdpService))
83 SsdpService ssdService = (SsdpService)service;
84 ssdService.RegisterService();
89 /// Deregisters the DNSSD/SSDP local service.
92 /// A local service registered using RegisterService() must be passed.
94 /// <param name="service">The DNSSD/SSDP service instance.</param>
95 /// <exception cref="NotSupportedException">Thrown when DNSSD/SSDP is not supported.</exception>
96 /// <exception cref="InvalidOperationException">Thrown when any other error occured.</exception>
97 public static void UnregisterService(INsdService service)
99 if (service.GetType() == typeof(DnssdService))
101 DnssdService dnsService = (DnssdService)service;
102 dnsService.DeregisterService();
105 else if (service.GetType() == typeof(SsdpService))
107 SsdpService ssdService = (SsdpService)service;
108 ssdService.DeregisterService();
113 /// Starts browsing the DNSSD/SSDP remote service.
116 /// If there are any services available, ServiceFound event will be invoked.
117 /// Application will keep browsing for available/unavailable services until it calls StopDiscovery().
119 /// <param name="browser">The DNSSD/SSDP browser instance.</param>
120 /// <exception cref="NotSupportedException">Thrown when DNSSD/SSDP is not supported.</exception>
121 /// <exception cref="InvalidOperationException">Thrown when any other error occured.</exception>
122 public static void StartDiscovery(INsdBrowser browser)
124 if (browser.GetType() == typeof(DnssdBrowser))
126 DnssdBrowser dnsBrowser = (DnssdBrowser)browser;
127 dnsBrowser.StartDiscovery();
130 else if (browser.GetType() == typeof(SsdpBrowser))
132 SsdpBrowser ssdBrowser = (SsdpBrowser)browser;
133 ssdBrowser.StartDiscovery();
138 /// Stops browsing the DNSSD/SSDP remote service.
140 /// <param name="browser">The DNSSD/SSDP browser instance.</param>
141 /// <exception cref="NotSupportedException">Thrown when DNSSD/SSDP is not supported.</exception>
142 /// <exception cref="InvalidOperationException">Thrown when any other error occured.</exception>
143 public static void StopDiscovery(INsdBrowser browser)
145 if (browser.GetType() == typeof(DnssdBrowser))
147 DnssdBrowser dnsBrowser = (DnssdBrowser)browser;
148 dnsBrowser.StopDiscovery();
151 else if (browser.GetType() == typeof(SsdpBrowser))
153 SsdpBrowser ssdBrowser = (SsdpBrowser)browser;
154 ssdBrowser.StopDiscovery();