moved zeroconf to publishZeroConf dir

This commit is contained in:
Johannes Pohl 2016-10-19 09:22:20 +02:00
parent 45c12c472f
commit ef6aa89222
7 changed files with 34 additions and 66 deletions

View file

@ -21,7 +21,7 @@ ifeq ($(TARGET), OPENWRT)
STRIP = echo
CXXFLAGS += -DNO_CPP11_STRING -DHAS_AVAHI -pthread
LDFLAGS = -lvorbis -lvorbisenc -logg -lFLAC -lavahi-client -lavahi-common -latomic
OBJ += publishAvahi.o
OBJ += publishZeroConf/publishAvahi.o
else ifeq ($(TARGET), FREEBSD)
@ -29,7 +29,7 @@ CXX = /usr/local/bin/g++
STRIP = echo
CXXFLAGS += -DNO_CPP11_STRING -DFREEBSD -DHAS_AVAHI -pthread
LDFLAGS = -lrt -lvorbis -lvorbisenc -logg -lFLAC -lavahi-client -lavahi-common -static-libgcc -static-libstdc++
OBJ += publishAvahi.o
OBJ += publishZeroConf/publishAvahi.o
else ifeq ($(TARGET), MACOS)
@ -37,7 +37,7 @@ CXX = /usr/bin/g++
STRIP = strip
CXXFLAGS += -DFREEBSD -DMACOS -DHAS_BONJOUR -Wno-deprecated -I/usr/local/include
LDFLAGS = -lvorbis -lvorbisenc -logg -lFLAC -L/usr/local/lib
OBJ += publishBonjour.o
OBJ += publishZeroConf/publishBonjour.o
else
@ -45,7 +45,7 @@ CXX = /usr/bin/g++
STRIP = strip
CXXFLAGS += -DHAS_AVAHI -pthread
LDFLAGS = -lrt -lvorbis -lvorbisenc -logg -lFLAC -lavahi-client -lavahi-common -static-libgcc -static-libstdc++
OBJ += publishAvahi.o
OBJ += publishZeroConf/publishAvahi.o
endif

View file

@ -38,19 +38,20 @@ PublishAvahi::PublishAvahi(const std::string& serviceName) : PublishmDNS(service
void PublishAvahi::publish(const std::vector<mDNSService>& services)
{
this->services = services;
services_ = services;
/* Allocate main loop object */
/// Allocate main loop object
if (!(simple_poll = avahi_simple_poll_new()))
{
///TODO: error handling
logE << "Failed to create simple poll object.\n";
}
/* Allocate a new client */
/// Allocate a new client
int error;
client_ = avahi_client_new(avahi_simple_poll_get(simple_poll), AVAHI_CLIENT_IGNORE_USER_CONFIG, client_callback, this, &error);
/* Check wether creating the client object succeeded */
/// Check wether creating the client object succeeded
if (!client_)
{
logE << "Failed to create client: " << avahi_strerror(error) << "\n";
@ -87,27 +88,26 @@ void PublishAvahi::entry_group_callback(AvahiEntryGroup *g, AvahiEntryGroupState
assert(g == group || group == NULL);
group = g;
/* Called whenever the entry group state changes */
/// Called whenever the entry group state changes
switch (state)
{
case AVAHI_ENTRY_GROUP_ESTABLISHED :
/* The entry group has been established successfully */
/// The entry group has been established successfully
logO << "Service '" << name << "' successfully established.\n";
break;
case AVAHI_ENTRY_GROUP_COLLISION : {
case AVAHI_ENTRY_GROUP_COLLISION :
{
char *n;
/* A service name collision with a remote service
* happened. Let's pick a new name */
/// A service name collision with a remote service happened. Let's pick a new name
n = avahi_alternative_service_name(name);
avahi_free(name);
name = n;
logO << "Service name collision, renaming service to '" << name << "'\n";
/* And recreate the services */
/// And recreate the services
static_cast<PublishAvahi*>(userdata)->create_services(avahi_entry_group_get_client(g));
break;
}
@ -116,7 +116,7 @@ void PublishAvahi::entry_group_callback(AvahiEntryGroup *g, AvahiEntryGroupState
logE << "Entry group failure: " << avahi_strerror(avahi_client_errno(avahi_entry_group_get_client(g))) << "\n";
/* Some kind of failure happened while we were registering our services */
/// Some kind of failure happened while we were registering our services
avahi_simple_poll_quit(simple_poll);
break;
@ -132,9 +132,7 @@ void PublishAvahi::create_services(AvahiClient *c)
int ret;
assert(c);
/* If this is the first time we're called, let's create a new
* entry group if necessary */
/// If this is the first time we're called, let's create a new entry group if necessary
if (!group)
{
if (!(group = avahi_entry_group_new(c, entry_group_callback, this)))
@ -143,54 +141,33 @@ void PublishAvahi::create_services(AvahiClient *c)
goto fail;
}
}
/* If the group is empty (either because it was just created, or
* because it was reset previously, add our entries. */
/// If the group is empty (either because it was just created, or because it was reset previously, add our entries.
if (avahi_entry_group_is_empty(group))
{
logO << "Adding service '" << name << "'\n";
/* Create some random TXT data */
snprintf(r, sizeof(r), "random=%i", rand());
/* We will now add two services and one subtype to the entry
* group. The two services have the same name, but differ in
* the service type (IPP vs. BSD LPR). Only services with the
* same name should be put in the same entry group. */
/* Add the service for IPP */
/* if ((ret = avahi_entry_group_add_service(group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, AVAHI_PUBLISH_UNIQUE, name, "_ipp._tcp", NULL, NULL, 651, "test=blah", r, NULL)) < 0)
/// We will now add two services and one subtype to the entry group
for (const auto& service: services)
{
if ((ret = avahi_entry_group_add_service(group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, AvahiPublishFlags(0), name, service.name_.c_str(), NULL, NULL, service.port_, NULL)) < 0)
{
if (ret == AVAHI_ERR_COLLISION)
goto collision;
fprintf(stderr, "Failed to add _ipp._tcp service: %s\n", avahi_strerror(ret));
goto fail;
}
*/
/* Add the same service for BSD LPR */
for (size_t n=0; n<services.size(); ++n)
{
if ((ret = avahi_entry_group_add_service(group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, AvahiPublishFlags(0), name, services[n].name_.c_str(), NULL, NULL, services[n].port_, NULL)) < 0)
{
if (ret == AVAHI_ERR_COLLISION)
goto collision;
logE << "Failed to add " << services[n].name_ << " service: " << avahi_strerror(ret) << "\n";
logE << "Failed to add " << service.name_ << " service: " << avahi_strerror(ret) << "\n";
goto fail;
}
}
/* Add an additional (hypothetic) subtype */
/// Add an additional (hypothetic) subtype
/* if ((ret = avahi_entry_group_add_service_subtype(group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, AvahiPublishFlags(0), name, "_printer._tcp", NULL, "_magic._sub._printer._tcp") < 0))
{
fprintf(stderr, "Failed to add subtype _magic._sub._printer._tcp: %s\n", avahi_strerror(ret));
goto fail;
}
*/
/* Tell the server to register the service */
/// Tell the server to register the service
if ((ret = avahi_entry_group_commit(group)) < 0)
{
logE << "Failed to commit entry group: " << avahi_strerror(ret) << "\n";
@ -202,8 +179,7 @@ void PublishAvahi::create_services(AvahiClient *c)
collision:
/* A service name collision with a local service happened. Let's
* pick a new name */
/// A service name collision with a local service happened. Let's pick a new name
n = avahi_alternative_service_name(name);
avahi_free(name);
name = n;
@ -224,14 +200,12 @@ void PublishAvahi::client_callback(AvahiClient *c, AvahiClientState state, AVAHI
{
assert(c);
/* Called whenever the client or server state changes */
/// Called whenever the client or server state changes
switch (state)
{
case AVAHI_CLIENT_S_RUNNING:
/* The server has startup successfully and registered its host
* name on the network, so it's time to create our services */
/// The server has startup successfully and registered its host name on the network, so it's time to create our services
static_cast<PublishAvahi*>(userdata)->create_services(c);
break;
@ -239,25 +213,20 @@ void PublishAvahi::client_callback(AvahiClient *c, AvahiClientState state, AVAHI
logE << "Client failure: " << avahi_strerror(avahi_client_errno(c)) << "\n";
avahi_simple_poll_quit(simple_poll);
break;
case AVAHI_CLIENT_S_COLLISION:
/* Let's drop our registered services. When the server is back
* in AVAHI_SERVER_RUNNING state we will register them
* again with the new host name. */
/// Let's drop our registered services. When the server is back
/// in AVAHI_SERVER_RUNNING state we will register them again with the new host name.
case AVAHI_CLIENT_S_REGISTERING:
/* The server records are now being established. This
* might be caused by a host name change. We need to wait
* for our own records to register until the host name is
* properly esatblished. */
/// The server records are now being established. This might be caused by a host name change. We need to wait
/// for our own records to register until the host name is properly esatblished.
if (group)
avahi_entry_group_reset(group);
break;
case AVAHI_CLIENT_CONNECTING:

View file

@ -52,7 +52,7 @@ private:
AvahiClient* client_;
std::thread pollThread_;
std::atomic<bool> active_;
std::vector<mDNSService> services;
std::vector<mDNSService> services_;
};

View file

@ -146,6 +146,7 @@ void PublishBonjour::publish(const std::vector<mDNSService>& services)
DNSServiceFlags flags = 0;
Opaque16 registerPort = { { static_cast<unsigned char>(service.port_ >> 8), static_cast<unsigned char>(service.port_ & 0xFF) } };
DNSServiceRef client = NULL;
// DNSServiceRegister(&client, flags, kDNSServiceInterfaceIndexAny, serviceName_.c_str(), service.name_.c_str(), NULL, NULL, registerPort.NotAnInteger, service.txt_.size(), service.txt_.empty()?NULL:service.txt_.c_str(), reg_reply, this);
DNSServiceRegister(&client, flags, kDNSServiceInterfaceIndexAny, serviceName_.c_str(), service.name_.c_str(), NULL, NULL, registerPort.NotAnInteger, 0, NULL, reg_reply, this);
clients.push_back(client);
}
@ -156,5 +157,3 @@ void PublishBonjour::publish(const std::vector<mDNSService>& services)

View file

@ -30,7 +30,7 @@
#include "encoder/encoderFactory.h"
#include "streamServer.h"
#if defined(HAS_AVAHI) || defined(HAS_BONJOUR)
#include "publishmDNS.h"
#include "publishZeroConf/publishmDNS.h"
#endif
#include "config.h"
#include "common/log.h"