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

View file

@ -38,19 +38,20 @@ PublishAvahi::PublishAvahi(const std::string& serviceName) : PublishmDNS(service
void PublishAvahi::publish(const std::vector<mDNSService>& services) 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())) if (!(simple_poll = avahi_simple_poll_new()))
{ {
///TODO: error handling
logE << "Failed to create simple poll object.\n"; logE << "Failed to create simple poll object.\n";
} }
/* Allocate a new client */ /// Allocate a new client
int error; int error;
client_ = avahi_client_new(avahi_simple_poll_get(simple_poll), AVAHI_CLIENT_IGNORE_USER_CONFIG, client_callback, this, &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_) if (!client_)
{ {
logE << "Failed to create client: " << avahi_strerror(error) << "\n"; 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); assert(g == group || group == NULL);
group = g; group = g;
/* Called whenever the entry group state changes */ /// Called whenever the entry group state changes
switch (state) switch (state)
{ {
case AVAHI_ENTRY_GROUP_ESTABLISHED : 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"; logO << "Service '" << name << "' successfully established.\n";
break; break;
case AVAHI_ENTRY_GROUP_COLLISION : { case AVAHI_ENTRY_GROUP_COLLISION :
{
char *n; char *n;
/* A service name collision with a remote service /// A service name collision with a remote service happened. Let's pick a new name
* happened. Let's pick a new name */
n = avahi_alternative_service_name(name); n = avahi_alternative_service_name(name);
avahi_free(name); avahi_free(name);
name = n; name = n;
logO << "Service name collision, renaming service to '" << 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)); static_cast<PublishAvahi*>(userdata)->create_services(avahi_entry_group_get_client(g));
break; 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"; 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); avahi_simple_poll_quit(simple_poll);
break; break;
@ -132,9 +132,7 @@ void PublishAvahi::create_services(AvahiClient *c)
int ret; int ret;
assert(c); assert(c);
/* If this is the first time we're called, let's create a new /// If this is the first time we're called, let's create a new entry group if necessary
* entry group if necessary */
if (!group) if (!group)
{ {
if (!(group = avahi_entry_group_new(c, entry_group_callback, this))) if (!(group = avahi_entry_group_new(c, entry_group_callback, this)))
@ -143,54 +141,33 @@ void PublishAvahi::create_services(AvahiClient *c)
goto fail; 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)) if (avahi_entry_group_is_empty(group))
{ {
logO << "Adding service '" << name << "'\n"; logO << "Adding service '" << name << "'\n";
/* Create some random TXT data */ /// We will now add two services and one subtype to the entry group
snprintf(r, sizeof(r), "random=%i", rand()); for (const auto& service: services)
/* 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)
{ {
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) if (ret == AVAHI_ERR_COLLISION)
goto 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; 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)) /* 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)); fprintf(stderr, "Failed to add subtype _magic._sub._printer._tcp: %s\n", avahi_strerror(ret));
goto fail; goto fail;
} }
*/ */
/* Tell the server to register the service */ /// Tell the server to register the service
if ((ret = avahi_entry_group_commit(group)) < 0) if ((ret = avahi_entry_group_commit(group)) < 0)
{ {
logE << "Failed to commit entry group: " << avahi_strerror(ret) << "\n"; logE << "Failed to commit entry group: " << avahi_strerror(ret) << "\n";
@ -202,8 +179,7 @@ void PublishAvahi::create_services(AvahiClient *c)
collision: collision:
/* A service name collision with a local service happened. Let's /// A service name collision with a local service happened. Let's pick a new name
* pick a new name */
n = avahi_alternative_service_name(name); n = avahi_alternative_service_name(name);
avahi_free(name); avahi_free(name);
name = n; name = n;
@ -224,14 +200,12 @@ void PublishAvahi::client_callback(AvahiClient *c, AvahiClientState state, AVAHI
{ {
assert(c); assert(c);
/* Called whenever the client or server state changes */ /// Called whenever the client or server state changes
switch (state) switch (state)
{ {
case AVAHI_CLIENT_S_RUNNING: case AVAHI_CLIENT_S_RUNNING:
/* The server has startup successfully and registered its host /// The server has startup successfully and registered its host name on the network, so it's time to create our services
* name on the network, so it's time to create our services */
static_cast<PublishAvahi*>(userdata)->create_services(c); static_cast<PublishAvahi*>(userdata)->create_services(c);
break; break;
@ -239,25 +213,20 @@ void PublishAvahi::client_callback(AvahiClient *c, AvahiClientState state, AVAHI
logE << "Client failure: " << avahi_strerror(avahi_client_errno(c)) << "\n"; logE << "Client failure: " << avahi_strerror(avahi_client_errno(c)) << "\n";
avahi_simple_poll_quit(simple_poll); avahi_simple_poll_quit(simple_poll);
break; break;
case AVAHI_CLIENT_S_COLLISION: case AVAHI_CLIENT_S_COLLISION:
/* Let's drop our registered services. When the server is back /// Let's drop our registered services. When the server is back
* in AVAHI_SERVER_RUNNING state we will register them /// in AVAHI_SERVER_RUNNING state we will register them again with the new host name.
* again with the new host name. */
case AVAHI_CLIENT_S_REGISTERING: case AVAHI_CLIENT_S_REGISTERING:
/* The server records are now being established. This /// The server records are now being established. This might be caused by a host name change. We need to wait
* 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.
* for our own records to register until the host name is
* properly esatblished. */
if (group) if (group)
avahi_entry_group_reset(group); avahi_entry_group_reset(group);
break; break;
case AVAHI_CLIENT_CONNECTING: case AVAHI_CLIENT_CONNECTING:

View file

@ -52,7 +52,7 @@ private:
AvahiClient* client_; AvahiClient* client_;
std::thread pollThread_; std::thread pollThread_;
std::atomic<bool> active_; 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; DNSServiceFlags flags = 0;
Opaque16 registerPort = { { static_cast<unsigned char>(service.port_ >> 8), static_cast<unsigned char>(service.port_ & 0xFF) } }; Opaque16 registerPort = { { static_cast<unsigned char>(service.port_ >> 8), static_cast<unsigned char>(service.port_ & 0xFF) } };
DNSServiceRef client = NULL; 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); DNSServiceRegister(&client, flags, kDNSServiceInterfaceIndexAny, serviceName_.c_str(), service.name_.c_str(), NULL, NULL, registerPort.NotAnInteger, 0, NULL, reg_reply, this);
clients.push_back(client); 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 "encoder/encoderFactory.h"
#include "streamServer.h" #include "streamServer.h"
#if defined(HAS_AVAHI) || defined(HAS_BONJOUR) #if defined(HAS_AVAHI) || defined(HAS_BONJOUR)
#include "publishmDNS.h" #include "publishZeroConf/publishmDNS.h"
#endif #endif
#include "config.h" #include "config.h"
#include "common/log.h" #include "common/log.h"