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 the 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 /// The constructor to create the 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.service_discovery.ssdp</feature>
55 /// <exception cref="NotSupportedException">Thrown while setting this property when SSDP is not supported.</exception>
56 /// <exception cref="ArgumentException">Thrown when the target is set to null.</exception>
57 public SsdpService(string target)
60 SsdpInitializeCreateService();
63 internal SsdpService(uint service)
65 _serviceHandle = service;
68 internal void SsdpInitializeCreateService()
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)
75 Log.Error(Globals.LogTag, "Failed to create a local Ssdp service handle, Error - " + (SsdpError)ret);
76 NsdErrorFactory.ThrowSsdpException(ret);
81 /// Unique Service Name of the SSDP service.
84 /// Set the USN for only an unregistered service created locally. If the service is already registered, the USN will not be set.
85 /// In case of an error, null will be returned during get and exception will be thrown during set.
87 /// <since_tizen> 4 </since_tizen>
88 /// <feature>http://tizen.org/feature/network.service_discovery.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>
97 int ret = Interop.Nsd.Ssdp.GetUsn(_serviceHandle, out usn);
98 if (ret != (int)SsdpError.None)
100 Log.Error(Globals.LogTag, "Failed to get usn of service, Error: " + (SsdpError)ret);
109 if (!Globals.s_threadSsd.IsValueCreated)
111 SsdpInitializeCreateService();
114 int ret = Interop.Nsd.Ssdp.SetUsn(_serviceHandle, value);
115 if (ret != (int)SsdpError.None)
117 Log.Error(Globals.LogTag, "Failed to set usn of service, Error: " + (SsdpError)ret);
118 NsdErrorFactory.ThrowSsdpException(ret);
124 /// Target of the SSDP service.
127 /// It may be a device type or a service type specified in the UPnP forum (http://upnp.org).
128 /// In case of an error, null will be returned.
130 /// <since_tizen> 4 </since_tizen>
136 int ret = Interop.Nsd.Ssdp.GetTarget(_serviceHandle, out target);
137 if (ret != (int)SsdpError.None)
139 Log.Error(Globals.LogTag, "Failed to get target of service, Error: " + (SsdpError)ret);
148 /// URL of the SSDP service.
151 /// Set the URL for only an unregistered service created locally. If the service is already registered, the URL will not be set.
152 /// In case of an error, null will be returned during get and exception will be thrown during set.
154 /// <since_tizen> 4 </since_tizen>
155 /// <feature>http://tizen.org/feature/network.service_discovery.ssdp</feature>
156 /// <exception cref="NotSupportedException">Thrown while setting this property when SSDP is not supported.</exception>
157 /// <exception cref="ArgumentException">Thrown when the URL value is set to null.</exception>
158 /// <exception cref="InvalidOperationException">Thrown while setting this property when any other error occurred.</exception>
164 int ret = Interop.Nsd.Ssdp.GetUrl(_serviceHandle, out url);
165 if (ret != (int)SsdpError.None)
167 Log.Error(Globals.LogTag, "Failed to get url of Ssdp service, Error: " + (SsdpError)ret);
176 if (!Globals.s_threadSsd.IsValueCreated)
178 SsdpInitializeCreateService();
181 int ret = Interop.Nsd.Ssdp.SetUrl(_serviceHandle, value);
182 if (ret != (int)SsdpError.None)
184 Log.Error(Globals.LogTag, "Failed to set url of Ssdp service, Error: " + (SsdpError)ret);
185 NsdErrorFactory.ThrowSsdpException(ret);
191 /// Registers the SSDP local service for publishing.
194 /// A service created locally must be passed.
195 /// URL and USN of the service must be set before the RegisterService is called.
197 /// <since_tizen> 4 </since_tizen>
198 /// <privilege>http://tizen.org/privilege/internet</privilege>
199 /// <feature>http://tizen.org/feature/network.service_discovery.ssdp</feature>
200 /// <exception cref="InvalidOperationException">Thrown when any other error occurred.</exception>
201 /// <exception cref="NotSupportedException">Thrown when the SSDP is not supported.</exception>
202 /// <exception cref="UnauthorizedAccessException">Thrown when the permission is denied.</exception>
203 public void RegisterService()
205 if (!Globals.s_threadSsd.IsValueCreated)
207 SsdpInitializeCreateService();
210 _serviceRegisteredCallback = (SsdpError result, uint service, IntPtr userData) =>
214 int ret = Interop.Nsd.Ssdp.RegisterService(_serviceHandle, _serviceRegisteredCallback, IntPtr.Zero);
215 if (ret != (int)SsdpError.None)
217 Log.Error(Globals.LogTag, "Failed to register the Ssdp local service, Error: " + (SsdpError)ret);
218 NsdErrorFactory.ThrowSsdpException(ret);
223 /// Deregisters the SSDP local service.
226 /// A local service registered using RegisterService() must be passed.
228 /// <since_tizen> 4 </since_tizen>
229 /// <feature>http://tizen.org/feature/network.service_discovery.ssdp</feature>
230 /// <exception cref="InvalidOperationException">Thrown when any other error occurred.</exception>
231 /// <exception cref="NotSupportedException">Thrown when the SSDP is not supported.</exception>
232 public void DeregisterService()
234 int ret = Interop.Nsd.Ssdp.DeregisterService(_serviceHandle);
235 if (ret != (int)SsdpError.None)
237 Log.Error(Globals.LogTag, "Failed to deregister the Ssdp local service, Error: " + (SsdpError)ret);
238 NsdErrorFactory.ThrowSsdpException(ret);
242 #region IDisposable Support
243 private bool _disposedValue = false; // To detect redundant calls
245 private void Dispose(bool disposing)
251 if (_serviceHandle != 0)
253 int ret = Interop.Nsd.Ssdp.DestroyService(_serviceHandle);
254 if (ret != (int)SsdpError.None)
256 Log.Error(Globals.LogTag, "Failed to destroy the local Ssdp service handle, Error - " + (SsdpError)ret);
261 _disposedValue = true;
266 /// Destroys the SsdpService object.
274 /// Disposes the memory allocated to unmanaged resources.
276 /// <since_tizen> 4 </since_tizen>
277 public void Dispose()
280 GC.SuppressFinalize(this);