Merge packaging information to csproj
[platform/core/csapi/tizenfx.git] / src / Tizen.Network.Nsd / Tizen.Network.Nsd / NsdManager.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.Threading;
18
19 namespace Tizen.Network.Nsd
20 {
21     internal static class Globals
22     {
23         internal const string LogTag = "Tizen.Network.Nsd";
24
25         internal static void DnssdInitialize()
26         {
27             int ret = Interop.Nsd.Dnssd.Initialize();
28             if(ret!=(int)DnssdError.None)
29             {
30                 Log.Error(LogTag, "Failed to initialize Dnssd, Error - "+ (DnssdError)ret);
31                 NsdErrorFactory.ThrowDnssdException(ret);
32             }
33         }
34
35         internal static void SsdpInitialize()
36         {
37             int ret = Interop.Nsd.Ssdp.Initialize();
38             if (ret != (int)SsdpError.None)
39             {
40                 Log.Error(LogTag, "Failed to initialize Ssdp, Error - " + (SsdpError)ret);
41                 NsdErrorFactory.ThrowSsdpException(ret);
42             }
43         }
44
45         internal static ThreadLocal<DnssdInitializer> s_threadDns = new ThreadLocal<DnssdInitializer>(() =>
46        {
47            Log.Info(LogTag, "Inside Dnssd ThreadLocal delegate");
48            return new DnssdInitializer();
49        });
50
51         internal static ThreadLocal<SsdpInitializer> s_threadSsd = new ThreadLocal<SsdpInitializer>(() =>
52         {
53             Log.Info(LogTag, "Inside Ssdp ThreadLocal delegate");
54             return new SsdpInitializer();
55         });
56     }
57
58     /// <summary>
59     /// This class is used for managing local/network service registration and discovery using DNSSD/SSDP.
60     /// </summary>
61     public static class NsdManager
62     {
63         /// <summary>
64         /// Registers the DNSSD/SSDP local service for publishing.
65         /// </summary>
66         /// <remarks>
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.
69         /// </remarks>
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)
74         {
75             if (service.GetType() == typeof(DnssdService))
76             {
77                 DnssdService dnsService = (DnssdService)service;
78                 dnsService.RegisterService();
79             }
80
81             else if (service.GetType() == typeof(SsdpService))
82             {
83                 SsdpService ssdService = (SsdpService)service;
84                 ssdService.RegisterService();
85             }
86         }
87
88         /// <summary>
89         /// Deregisters the DNSSD/SSDP local service.
90         /// </summary>
91         /// <remarks>
92         /// A local service registered using RegisterService() must be passed.
93         /// </remarks>
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)
98         {
99             if (service.GetType() == typeof(DnssdService))
100             {
101                 DnssdService dnsService = (DnssdService)service;
102                 dnsService.DeregisterService();
103             }
104
105             else if (service.GetType() == typeof(SsdpService))
106             {
107                 SsdpService ssdService = (SsdpService)service;
108                 ssdService.DeregisterService();
109             }
110         }
111
112         /// <summary>
113         /// Starts browsing the DNSSD/SSDP remote service.
114         /// </summary>
115         /// <remarks>
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().
118         /// </remarks>
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)
123         {
124             if (browser.GetType() == typeof(DnssdBrowser))
125             {
126                 DnssdBrowser dnsBrowser = (DnssdBrowser)browser;
127                 dnsBrowser.StartDiscovery();
128             }
129
130             else if (browser.GetType() == typeof(SsdpBrowser))
131             {
132                 SsdpBrowser ssdBrowser = (SsdpBrowser)browser;
133                 ssdBrowser.StartDiscovery();
134             }
135         }
136
137         /// <summary>
138         /// Stops browsing the DNSSD/SSDP remote service.
139         /// </summary>
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)
144         {
145             if (browser.GetType() == typeof(DnssdBrowser))
146             {
147                 DnssdBrowser dnsBrowser = (DnssdBrowser)browser;
148                 dnsBrowser.StopDiscovery();
149             }
150
151             else if (browser.GetType() == typeof(SsdpBrowser))
152             {
153                 SsdpBrowser ssdBrowser = (SsdpBrowser)browser;
154                 ssdBrowser.StopDiscovery();
155             }
156         }
157     }
158 }