/*
* 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 DNS-SD.
///
/// 4
public class DnssdBrowser : INsdBrowser
{
private string _serviceType;
private uint _browserHandle;
private event EventHandler _serviceFound;
private Interop.Nsd.Dnssd.ServiceFoundCallback _serviceFoundCallback;
///
/// This event is raised when a DNS-SD service is found during the service discovery.
///
/// 4
public event EventHandler ServiceFound
{
add
{
_serviceFound += value;
}
remove
{
_serviceFound -= value;
}
}
///
/// A public constructor for the DnssdBrowser class to create a DnssdBrowser instance for the given service type.
///
/// The DNS-SD service type.
/// 4
/// http://tizen.org/feature/network.service_discovery.dnssd
/// Thrown when the serviceType is null.
/// Thrown when DNS-SD is not supported.
public DnssdBrowser(string serviceType)
{
DnssdInitializer dnssdInit = Globals.s_threadDns.Value;
Log.Info(Globals.LogTag, "Initialize ThreadLocal instance = " + dnssdInit);
if(serviceType == null)
{
Log.Debug(Globals.LogTag, "serviceType is null");
NsdErrorFactory.ThrowDnssdException((int)DnssdError.InvalidParameter);
}
_serviceType = serviceType;
}
///
/// Starts browsing the DNS-SD 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/privilege/internet
/// http://tizen.org/feature/network.service_discovery.dnssd
/// Thrown when any other error occurred.
/// Thrown when DNS-SD is not supported.
/// Thrown when the permission is denied.
public void StartDiscovery()
{
DnssdInitializer dnssdInit = Globals.s_threadDns.Value;
Log.Info(Globals.LogTag, "Initialize ThreadLocal instance = " + dnssdInit);
_serviceFoundCallback = (DnssdServiceState state, uint service, IntPtr userData) =>
{
if (_serviceFound != null)
{
Log.Info(Globals.LogTag, "Inside Service found callback");
DnssdService dnsService = new DnssdService(service);
_serviceFound(null, new DnssdServiceFoundEventArgs(state, dnsService));
}
};
int ret = Interop.Nsd.Dnssd.StartBrowsing(_serviceType, out _browserHandle, _serviceFoundCallback, IntPtr.Zero);
if (ret != (int)DnssdError.None)
{
Log.Error(Globals.LogTag, "Failed to discover Dnssd remote service, Error - " + (DnssdError)ret);
NsdErrorFactory.ThrowDnssdException(ret);
}
}
///
/// Stops browsing the DNS-SD remote service.
///
/// 4
/// http://tizen.org/feature/network.service_discovery.dnssd
/// Thrown when any other error occurred.
/// Thrown when DNS-SD is not supported.
public void StopDiscovery()
{
int ret = Interop.Nsd.Dnssd.StopBrowsing(_browserHandle);
if (ret != (int)DnssdError.None)
{
Log.Error(Globals.LogTag, "Failed to stop discovery of Dnssd remote service, Error - " + (DnssdError)ret);
NsdErrorFactory.ThrowDnssdException(ret);
}
}
}
}