/* * 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; namespace Tizen.Network.Nsd { /// /// This class is used for managing the network service discovery using SSDP. /// /// 4 public class SsdpBrowser : INsdBrowser { private string _target; private uint _browserHandle; private event EventHandler _serviceFound; private Interop.Nsd.Ssdp.ServiceFoundCallback _serviceFoundCallback; /// /// This event is raised when the service has become available or unavailable during a service discovery using SSDP. /// /// 4 public event EventHandler ServiceFound { add { _serviceFound += value; } remove { _serviceFound -= value; } } /// /// A public constructor for the SsdpBrowser class to create a SsdpBrowser instance for the given target. /// /// 4 /// The target to browse for the service. /// http://tizen.org/feature/network.service_discovery.ssdp /// Thrown when the target is null. /// Thrown when SSDP is not supported. public SsdpBrowser(string target) { SsdpInitializer ssdpInit = Globals.s_threadSsd.Value; Log.Info(Globals.LogTag, "Initialize ThreadLocal instance = " + ssdpInit); if (target == null) { Log.Debug(Globals.LogTag, "target is null"); NsdErrorFactory.ThrowSsdpException((int)SsdpError.InvalidParameter); } _target = target; } /// /// Starts browsing the SSDP remote service. /// /// /// If there are any services available, the ServiceFound event will be invoked. /// The application will keep browsing for the available or unavailable services until it calls StopDiscovery(). /// /// 4 /// http://tizen.org/feature/network.service_discovery.ssdp /// Thrown when any other error occured. /// Thrown when SSDP is not supported. public void StartDiscovery() { SsdpInitializer ssdpInit = Globals.s_threadSsd.Value; Log.Info(Globals.LogTag, "Initialize ThreadLocal instance = " + ssdpInit); _serviceFoundCallback = (SsdpServiceState state, uint service, IntPtr userData) => { if (_serviceFound != null) { Log.Info(Globals.LogTag, "Inside Service found callback"); SsdpService ssdpService = new SsdpService(service); _serviceFound(null, new SsdpServiceFoundEventArgs(state, ssdpService)); } }; int ret = Interop.Nsd.Ssdp.StartBrowsing(_target, out _browserHandle, _serviceFoundCallback, IntPtr.Zero); if (ret != (int)SsdpError.None) { Log.Error(Globals.LogTag, "Failed to discover Ssdp remote service, Error - " + (SsdpError)ret); NsdErrorFactory.ThrowSsdpException(ret); } } /// /// Stops browsing the SSDP remote service. /// /// 4 /// http://tizen.org/privilege/internet /// http://tizen.org/feature/network.service_discovery.ssdp /// Thrown when any other error occured. /// Thrown when SSDP is not supported. /// Thrown when the permission is denied. public void StopDiscovery() { int ret = Interop.Nsd.Ssdp.StopBrowsing(_browserHandle); if (ret != (int)SsdpError.None) { Log.Error(Globals.LogTag, "Failed to stop discovery of Ssdp remote service, Error - " + (SsdpError)ret); NsdErrorFactory.ThrowSsdpException(ret); } } } }