Release 4.0.0-preview1-00051
[platform/core/csapi/tizenfx.git] / src / Tizen.Network.Nsd / Tizen.Network.Nsd / SsdpService.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 using System.Threading;
19
20 namespace Tizen.Network.Nsd
21 {
22     internal class SsdpInitializer
23     {
24         internal SsdpInitializer()
25         {
26             Globals.SsdpInitialize();
27         }
28
29         ~SsdpInitializer()
30         {
31             int ret = Interop.Nsd.Ssdp.Deinitialize();
32             if (ret != (int)SsdpError.None)
33             {
34                 Log.Error(Globals.LogTag, "Failed to deinitialize Ssdp, Error - " + (SsdpError)ret);
35             }
36         }
37     }
38
39     /// <summary>
40     /// This class is used for managing local service registration and its properties using SSDP.
41     /// </summary>
42     /// <since_tizen> 4 </since_tizen>
43     public class SsdpService : INsdService
44     {
45         private uint _serviceHandle;
46         private string _target;
47         private Interop.Nsd.Ssdp.ServiceRegisteredCallback _serviceRegisteredCallback;
48
49         /// <summary>
50         /// Constructor to create SsdpService instance that sets the target to a given value.
51         /// </summary>
52         /// <since_tizen> 4 </since_tizen>
53         /// <param name="target">The SSDP local service's target. It may be a device type or a service type.</param>
54         /// <feature>http://tizen.org/feature/network.ssdp</feature>
55         /// <exception cref="NotSupportedException">Thrown while setting this property when SSDP is not supported.</exception>
56         /// <exception cref="ArgumentException">Thrown when target is set to null.</exception>
57         public SsdpService(string target)
58         {
59             _target = target;
60             SsdpInitializeCreateService();
61         }
62
63         internal SsdpService(uint service)
64         {
65             _serviceHandle = service;
66         }
67
68         internal void SsdpInitializeCreateService()
69         {
70             SsdpInitializer ssdpInit = Globals.s_threadSsd.Value;
71             Log.Info(Globals.LogTag, "Initialize ThreadLocal<SsdpInitializer> instance = " + ssdpInit);
72             int ret = Interop.Nsd.Ssdp.CreateService(_target, out _serviceHandle);
73             if (ret != (int)SsdpError.None)
74             {
75                 Log.Error(Globals.LogTag, "Failed to create a local Ssdp service handle, Error - " + (SsdpError)ret);
76                 NsdErrorFactory.ThrowSsdpException(ret);
77             }
78         }
79
80         /// <summary>
81         /// Unique Service Name of SSDP service.
82         /// </summary>
83         /// <remarks>
84         /// Set Usn for only unregistered service created locally. If service is already registered, Usn will not be set.
85         /// In case of error, null will be returned during get and exception will be thrown during set.
86         /// </remarks>
87         /// <since_tizen> 4 </since_tizen>
88         /// <feature>http://tizen.org/feature/network.ssdp</feature>
89         /// <exception cref="NotSupportedException">Thrown while setting this property when SSDP is not supported.</exception>
90         /// <exception cref="ArgumentException">Thrown when Usn value is set to null.</exception>
91         /// <exception cref="InvalidOperationException">Thrown while setting this property when any other error occurred.</exception>
92         public string Usn
93         {
94             get
95             {
96                 string usn;
97                 int ret = Interop.Nsd.Ssdp.GetUsn(_serviceHandle, out usn);
98                 if (ret != (int)SsdpError.None)
99                 {
100                     Log.Error(Globals.LogTag, "Failed to get usn of service, Error: " + (SsdpError)ret);
101                     return null;
102                 }
103
104                 return usn;
105             }
106
107             set
108             {
109                 if (!Globals.s_threadSsd.IsValueCreated)
110                 {
111                     SsdpInitializeCreateService();
112                 }
113
114                 int ret = Interop.Nsd.Ssdp.SetUsn(_serviceHandle, value);
115                 if (ret != (int)SsdpError.None)
116                 {
117                     Log.Error(Globals.LogTag, "Failed to set usn of service, Error: " + (SsdpError)ret);
118                     NsdErrorFactory.ThrowSsdpException(ret);
119                 }
120             }
121         }
122
123         /// <summary>
124         /// Target of SSDP service.
125         /// </summary>
126         /// <remarks>
127         /// It may be a device type or a service type specified in UPnP forum (http://upnp.org).
128         /// In case of error, null will be returned.
129         /// </remarks>
130         /// <since_tizen> 4 </since_tizen>
131         public string Target
132         {
133             get
134             {
135                 string target;
136                 int ret = Interop.Nsd.Ssdp.GetTarget(_serviceHandle, out target);
137                 if (ret != (int)SsdpError.None)
138                 {
139                     Log.Error(Globals.LogTag, "Failed to get target of service, Error: " + (SsdpError)ret);
140                     return null;
141                 }
142
143                 return target;
144             }
145         }
146
147         /// <summary>
148         /// URL of SSDP service.
149         /// </summary>
150         /// <remarks>
151         /// Set Url for only unregistered service created locally. If service is already registered, Url will not be set.
152         /// In case of error, null will be returned during get and exception will be thrown during set.
153         /// </remarks>
154         /// <since_tizen> 4 </since_tizen>
155         /// <feature>http://tizen.org/feature/network.ssdp</feature>
156         /// <exception cref="NotSupportedException">Thrown while setting this property when SSDP is not supported.</exception>
157         /// <exception cref="ArgumentException">Thrown when Url value is set to null.</exception>
158         /// <exception cref="InvalidOperationException">Thrown while setting this property when any other error occurred.</exception>
159         public string Url
160         {
161             get
162             {
163                 string url;
164                 int ret = Interop.Nsd.Ssdp.GetUrl(_serviceHandle, out url);
165                 if (ret != (int)SsdpError.None)
166                 {
167                     Log.Error(Globals.LogTag, "Failed to get url of Ssdp service, Error: " + (SsdpError)ret);
168                     return null;
169                 }
170
171                 return url;
172             }
173
174             set
175             {
176                 if (!Globals.s_threadSsd.IsValueCreated)
177                 {
178                     SsdpInitializeCreateService();
179                 }
180
181                 int ret = Interop.Nsd.Ssdp.SetUrl(_serviceHandle, value);
182                 if (ret != (int)SsdpError.None)
183                 {
184                     Log.Error(Globals.LogTag, "Failed to set url of Ssdp service, Error: " + (SsdpError)ret);
185                     NsdErrorFactory.ThrowSsdpException(ret);
186                 }
187             }
188         }
189
190         /// <summary>
191         /// Registers the SSDP local service for publishing.
192         /// </summary>
193         /// <remarks>
194         /// A service created locally must be passed.
195         /// Url and Usn of the service must be set before RegisterService is called.
196         /// </remarks>
197         /// <since_tizen> 4 </since_tizen>
198         /// <privilege>http://tizen.org/privilege/internet</privilege>
199         /// <feature>http://tizen.org/feature/network.ssdp</feature>
200         /// <exception cref="InvalidOperationException">Thrown when any other error occurred.</exception>
201         /// <exception cref="NotSupportedException">Thrown when SSDP is not supported.</exception>
202         /// <exception cref="UnauthorizedAccessException">Thrown when permission is denied.</exception>
203         public void RegisterService()
204         {
205             if (!Globals.s_threadSsd.IsValueCreated)
206             {
207                 SsdpInitializeCreateService();
208             }
209
210             _serviceRegisteredCallback = (SsdpError result, uint service, IntPtr userData) =>
211             {
212             };
213
214             int ret = Interop.Nsd.Ssdp.RegisterService(_serviceHandle, _serviceRegisteredCallback, IntPtr.Zero);
215             if (ret != (int)SsdpError.None)
216             {
217                 Log.Error(Globals.LogTag, "Failed to register the Ssdp local service, Error: " + (SsdpError)ret);
218                 NsdErrorFactory.ThrowSsdpException(ret);
219             }
220         }
221
222         /// <summary>
223         /// Deregisters the SSDP local service.
224         /// </summary>
225         /// <remarks>
226         /// A local service registered using RegisterService() must be passed.
227         /// </remarks>
228         /// <since_tizen> 4 </since_tizen>
229         /// <feature>http://tizen.org/feature/network.ssdp</feature>
230         /// <exception cref="InvalidOperationException">Thrown when any other error occurred.</exception>
231         /// <exception cref="NotSupportedException">Thrown when SSDP is not supported.</exception>
232         public void DeregisterService()
233         {
234             int ret = Interop.Nsd.Ssdp.DeregisterService(_serviceHandle);
235             if (ret != (int)SsdpError.None)
236             {
237                 Log.Error(Globals.LogTag, "Failed to deregister the Ssdp local service, Error: " + (SsdpError)ret);
238                 NsdErrorFactory.ThrowSsdpException(ret);
239             }
240         }
241
242         #region IDisposable Support
243         private bool _disposedValue = false; // To detect redundant calls
244
245         private void Dispose(bool disposing)
246         {
247             if (!_disposedValue)
248             {
249                 if (disposing)
250                 {
251                     if (_serviceHandle != 0)
252                     {
253                         int ret = Interop.Nsd.Ssdp.DestroyService(_serviceHandle);
254                         if (ret != (int)SsdpError.None)
255                         {
256                             Log.Error(Globals.LogTag, "Failed to destroy the local Ssdp service handle, Error - " + (SsdpError)ret);
257                         }
258                     }
259                 }
260
261                 _disposedValue = true;
262             }
263         }
264
265         ~SsdpService()
266         {
267             Dispose(false);
268         }
269
270         /// <summary>
271         /// Disposes the memory allocated to unmanaged resources.
272         /// </summary>
273         /// <since_tizen> 4 </since_tizen>
274         public void Dispose()
275         {
276             Dispose(true);
277             GC.SuppressFinalize(this);
278         }
279         #endregion
280     }
281 }