/* * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved * * Licensed under the Apache License, Version 2.0 (the License); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an AS IS BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ using System; using System.Threading; namespace Tizen.Network.Nsd { internal class SsdpInitializer { internal SsdpInitializer() { Globals.SsdpInitialize(); } ~SsdpInitializer() { int ret = Interop.Nsd.Ssdp.Deinitialize(); if (ret != (int)SsdpError.None) { Log.Error(Globals.LogTag, "Failed to deinitialize Ssdp, Error - " + (SsdpError)ret); } } } /// /// This class is used for managing local service registration and its properties using SSDP. /// /// 4 public class SsdpService : INsdService { private uint _serviceHandle; private string _target; private Interop.Nsd.Ssdp.ServiceRegisteredCallback _serviceRegisteredCallback; /// /// Constructor to create SsdpService instance that sets the target to a given value. /// /// 4 /// The SSDP local service's target. It may be a device type or a service type. /// http://tizen.org/feature/network.ssdp /// Thrown while setting this property when SSDP is not supported. /// Thrown when target is set to null. public SsdpService(string target) { _target = target; SsdpInitializeCreateService(); } internal SsdpService(uint service) { _serviceHandle = service; } internal void SsdpInitializeCreateService() { SsdpInitializer ssdpInit = Globals.s_threadSsd.Value; Log.Info(Globals.LogTag, "Initialize ThreadLocal instance = " + ssdpInit); int ret = Interop.Nsd.Ssdp.CreateService(_target, out _serviceHandle); if (ret != (int)SsdpError.None) { Log.Error(Globals.LogTag, "Failed to create a local Ssdp service handle, Error - " + (SsdpError)ret); NsdErrorFactory.ThrowSsdpException(ret); } } /// /// Unique Service Name of SSDP service. /// /// /// Set Usn for only unregistered service created locally. If service is already registered, Usn will not be set. /// In case of error, null will be returned during get and exception will be thrown during set. /// /// 4 /// http://tizen.org/feature/network.ssdp /// Thrown while setting this property when SSDP is not supported. /// Thrown when Usn value is set to null. /// Thrown while setting this property when any other error occurred. public string Usn { get { string usn; int ret = Interop.Nsd.Ssdp.GetUsn(_serviceHandle, out usn); if (ret != (int)SsdpError.None) { Log.Error(Globals.LogTag, "Failed to get usn of service, Error: " + (SsdpError)ret); return null; } return usn; } set { if (!Globals.s_threadSsd.IsValueCreated) { SsdpInitializeCreateService(); } int ret = Interop.Nsd.Ssdp.SetUsn(_serviceHandle, value); if (ret != (int)SsdpError.None) { Log.Error(Globals.LogTag, "Failed to set usn of service, Error: " + (SsdpError)ret); NsdErrorFactory.ThrowSsdpException(ret); } } } /// /// Target of SSDP service. /// /// /// It may be a device type or a service type specified in UPnP forum (http://upnp.org). /// In case of error, null will be returned. /// /// 4 public string Target { get { string target; int ret = Interop.Nsd.Ssdp.GetTarget(_serviceHandle, out target); if (ret != (int)SsdpError.None) { Log.Error(Globals.LogTag, "Failed to get target of service, Error: " + (SsdpError)ret); return null; } return target; } } /// /// URL of SSDP service. /// /// /// Set Url for only unregistered service created locally. If service is already registered, Url will not be set. /// In case of error, null will be returned during get and exception will be thrown during set. /// /// 4 /// http://tizen.org/feature/network.ssdp /// Thrown while setting this property when SSDP is not supported. /// Thrown when Url value is set to null. /// Thrown while setting this property when any other error occurred. public string Url { get { string url; int ret = Interop.Nsd.Ssdp.GetUrl(_serviceHandle, out url); if (ret != (int)SsdpError.None) { Log.Error(Globals.LogTag, "Failed to get url of Ssdp service, Error: " + (SsdpError)ret); return null; } return url; } set { if (!Globals.s_threadSsd.IsValueCreated) { SsdpInitializeCreateService(); } int ret = Interop.Nsd.Ssdp.SetUrl(_serviceHandle, value); if (ret != (int)SsdpError.None) { Log.Error(Globals.LogTag, "Failed to set url of Ssdp service, Error: " + (SsdpError)ret); NsdErrorFactory.ThrowSsdpException(ret); } } } /// /// Registers the SSDP local service for publishing. /// /// /// A service created locally must be passed. /// Url and Usn of the service must be set before RegisterService is called. /// /// 4 /// http://tizen.org/privilege/internet /// http://tizen.org/feature/network.ssdp /// Thrown when any other error occurred. /// Thrown when SSDP is not supported. /// Thrown when permission is denied. public void RegisterService() { if (!Globals.s_threadSsd.IsValueCreated) { SsdpInitializeCreateService(); } _serviceRegisteredCallback = (SsdpError result, uint service, IntPtr userData) => { }; int ret = Interop.Nsd.Ssdp.RegisterService(_serviceHandle, _serviceRegisteredCallback, IntPtr.Zero); if (ret != (int)SsdpError.None) { Log.Error(Globals.LogTag, "Failed to register the Ssdp local service, Error: " + (SsdpError)ret); NsdErrorFactory.ThrowSsdpException(ret); } } /// /// Deregisters the SSDP local service. /// /// /// A local service registered using RegisterService() must be passed. /// /// 4 /// http://tizen.org/feature/network.ssdp /// Thrown when any other error occurred. /// Thrown when SSDP is not supported. public void DeregisterService() { int ret = Interop.Nsd.Ssdp.DeregisterService(_serviceHandle); if (ret != (int)SsdpError.None) { Log.Error(Globals.LogTag, "Failed to deregister the Ssdp local service, Error: " + (SsdpError)ret); NsdErrorFactory.ThrowSsdpException(ret); } } #region IDisposable Support private bool _disposedValue = false; // To detect redundant calls private void Dispose(bool disposing) { if (!_disposedValue) { if (disposing) { if (_serviceHandle != 0) { int ret = Interop.Nsd.Ssdp.DestroyService(_serviceHandle); if (ret != (int)SsdpError.None) { Log.Error(Globals.LogTag, "Failed to destroy the local Ssdp service handle, Error - " + (SsdpError)ret); } } } _disposedValue = true; } } ~SsdpService() { Dispose(false); } /// /// Disposes the memory allocated to unmanaged resources. /// /// 4 public void Dispose() { Dispose(true); GC.SuppressFinalize(this); } #endregion } }