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.
18 using System.Threading;
20 namespace Tizen.Network.Nsd
22 internal class SsdpInitializer
24 internal SsdpInitializer()
26 Globals.SsdpInitialize();
31 int ret = Interop.Nsd.Ssdp.Deinitialize();
32 if (ret != (int)SsdpError.None)
34 Log.Error(Globals.LogTag, "Failed to deinitialize Ssdp, Error - " + (SsdpError)ret);
40 /// This class is used for managing local service registration and its properties using SSDP.
42 /// <since_tizen> 4 </since_tizen>
43 public class SsdpService : INsdService
45 private uint _serviceHandle;
46 private string _target;
47 private Interop.Nsd.Ssdp.ServiceRegisteredCallback _serviceRegisteredCallback;
50 /// Constructor to create SsdpService instance that sets the target to a given value.
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 public SsdpService(string target)
58 SsdpInitializeCreateService();
61 internal SsdpService(uint service)
63 _serviceHandle = service;
66 internal void SsdpInitializeCreateService()
68 SsdpInitializer ssdpInit = Globals.s_threadSsd.Value;
69 Log.Info(Globals.LogTag, "Initialize ThreadLocal<SsdpInitializer> instance = " + ssdpInit);
70 int ret = Interop.Nsd.Ssdp.CreateService(_target, out _serviceHandle);
71 if (ret != (int)SsdpError.None)
73 Log.Error(Globals.LogTag, "Failed to create a local Ssdp service handle, Error - " + (SsdpError)ret);
74 NsdErrorFactory.ThrowSsdpException(ret);
79 /// Unique Service Name of SSDP service.
82 /// Set Usn for only unregistered service created locally. If service is already registered, Usn will not be set.
83 /// In case of error, null will be returned during get and exception will be thrown during set.
85 /// <since_tizen> 4 </since_tizen>
86 /// <feature>http://tizen.org/feature/network.ssdp</feature>
87 /// <exception cref="NotSupportedException">Thrown while setting this property when SSDP is not supported.</exception>
88 /// <exception cref="ArgumentException">Thrown when Usn value is set to null.</exception>
89 /// <exception cref="InvalidOperationException">Thrown while setting this property when any other error occurred.</exception>
95 int ret = Interop.Nsd.Ssdp.GetUsn(_serviceHandle, out usn);
96 if (ret != (int)SsdpError.None)
98 Log.Error(Globals.LogTag, "Failed to get usn of service, Error: " + (SsdpError)ret);
107 if (!Globals.s_threadSsd.IsValueCreated)
109 SsdpInitializeCreateService();
112 int ret = Interop.Nsd.Ssdp.SetUsn(_serviceHandle, value);
113 if (ret != (int)SsdpError.None)
115 Log.Error(Globals.LogTag, "Failed to set usn of service, Error: " + (SsdpError)ret);
116 NsdErrorFactory.ThrowSsdpException(ret);
122 /// Target of SSDP service.
125 /// It may be a device type or a service type specified in UPnP forum (http://upnp.org).
126 /// In case of error, null will be returned.
128 /// <since_tizen> 4 </since_tizen>
134 int ret = Interop.Nsd.Ssdp.GetTarget(_serviceHandle, out target);
135 if (ret != (int)SsdpError.None)
137 Log.Error(Globals.LogTag, "Failed to get target of service, Error: " + (SsdpError)ret);
146 /// URL of SSDP service.
149 /// Set Url for only unregistered service created locally. If service is already registered, Url will not be set.
150 /// In case of error, null will be returned during get and exception will be thrown during set.
152 /// <since_tizen> 4 </since_tizen>
153 /// <feature>http://tizen.org/feature/network.ssdp</feature>
154 /// <exception cref="NotSupportedException">Thrown while setting this property when SSDP is not supported.</exception>
155 /// <exception cref="ArgumentException">Thrown when Url value is set to null.</exception>
156 /// <exception cref="InvalidOperationException">Thrown while setting this property when any other error occurred.</exception>
162 int ret = Interop.Nsd.Ssdp.GetUrl(_serviceHandle, out url);
163 if (ret != (int)SsdpError.None)
165 Log.Error(Globals.LogTag, "Failed to get url of Ssdp service, Error: " + (SsdpError)ret);
174 if (!Globals.s_threadSsd.IsValueCreated)
176 SsdpInitializeCreateService();
179 int ret = Interop.Nsd.Ssdp.SetUrl(_serviceHandle, value);
180 if (ret != (int)SsdpError.None)
182 Log.Error(Globals.LogTag, "Failed to set url of Ssdp service, Error: " + (SsdpError)ret);
183 NsdErrorFactory.ThrowSsdpException(ret);
189 /// Registers the SSDP local service for publishing.
192 /// A service created locally must be passed.
193 /// Url and Usn of the service must be set before RegisterService is called.
195 /// <since_tizen> 4 </since_tizen>
196 /// <privilege>http://tizen.org/privilege/internet</privilege>
197 /// <feature>http://tizen.org/feature/network.ssdp</feature>
198 /// <exception cref="InvalidOperationException">Thrown when any other error occurred.</exception>
199 /// <exception cref="NotSupportedException">Thrown when SSDP is not supported.</exception>
200 /// <exception cref="UnauthorizedAccessException">Thrown when permission is denied.</exception>
201 public void RegisterService()
203 if (!Globals.s_threadSsd.IsValueCreated)
205 SsdpInitializeCreateService();
208 _serviceRegisteredCallback = (SsdpError result, uint service, IntPtr userData) =>
212 int ret = Interop.Nsd.Ssdp.RegisterService(_serviceHandle, _serviceRegisteredCallback, IntPtr.Zero);
213 if (ret != (int)SsdpError.None)
215 Log.Error(Globals.LogTag, "Failed to register the Ssdp local service, Error: " + (SsdpError)ret);
216 NsdErrorFactory.ThrowSsdpException(ret);
221 /// Deregisters the SSDP local service.
224 /// A local service registered using RegisterService() must be passed.
226 /// <since_tizen> 4 </since_tizen>
227 /// <feature>http://tizen.org/feature/network.ssdp</feature>
228 /// <exception cref="InvalidOperationException">Thrown when any other error occurred.</exception>
229 /// <exception cref="NotSupportedException">Thrown when SSDP is not supported.</exception>
230 public void DeregisterService()
232 int ret = Interop.Nsd.Ssdp.DeregisterService(_serviceHandle);
233 if (ret != (int)SsdpError.None)
235 Log.Error(Globals.LogTag, "Failed to deregister the Ssdp local service, Error: " + (SsdpError)ret);
236 NsdErrorFactory.ThrowSsdpException(ret);
240 #region IDisposable Support
241 private bool _disposedValue = false; // To detect redundant calls
243 protected virtual void Dispose(bool disposing)
249 if (_serviceHandle != 0)
251 int ret = Interop.Nsd.Ssdp.DestroyService(_serviceHandle);
252 if (ret != (int)SsdpError.None)
254 Log.Error(Globals.LogTag, "Failed to destroy the local Ssdp service handle, Error - " + (SsdpError)ret);
259 _disposedValue = true;
269 /// Disposes the memory allocated to unmanaged resources.
271 /// <since_tizen> 4 </since_tizen>
272 public void Dispose()
275 GC.SuppressFinalize(this);