move stuff in common, clang-format, update copyright

This commit is contained in:
Guillaume Meunier
2022-11-13 11:00:04 +01:00
parent 294c4f81ba
commit 4baf2b17eb
62 changed files with 2291 additions and 2846 deletions
+64 -58
View File
@@ -1,26 +1,25 @@
#include "avahi_publisher.h"
#include <avahi-common/simple-watch.h>
#include <avahi-client/client.h>
#include <avahi-common/error.h>
#include <avahi-common/alternative.h>
#include <avahi-common/malloc.h>
#include <avahi-client/publish.h>
#include <avahi-common/alternative.h>
#include <avahi-common/error.h>
#include <avahi-common/malloc.h>
#include <avahi-common/simple-watch.h>
#include <iostream>
#include <string.h>
void
avahi_publisher::alt_name()
void avahi_publisher::alt_name()
{
char *new_name = avahi_alternative_service_name(name);
char * new_name = avahi_alternative_service_name(name);
avahi_free(name);
name = new_name;
}
void
avahi_publisher::create_service(AvahiClient *client)
void avahi_publisher::create_service(AvahiClient * client)
{
if (!entry_group) {
if (!entry_group)
{
entry_group = avahi_entry_group_new(client, avahi_entry_group_callback, nullptr);
}
avahi_entry_group_reset(entry_group);
@@ -29,73 +28,83 @@ avahi_publisher::create_service(AvahiClient *client)
name = avahi_strdup("WiVRn");
int ret;
do {
ret = avahi_entry_group_add_service(entry_group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC,
(AvahiPublishFlags)0, name, type.c_str(), nullptr, nullptr, port,
nullptr);
if (ret == AVAHI_ERR_COLLISION) {
do
{
ret = avahi_entry_group_add_service(entry_group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, (AvahiPublishFlags)0, name, type.c_str(), nullptr, nullptr, port, nullptr);
if (ret == AVAHI_ERR_COLLISION)
{
alt_name();
}
} while (ret == AVAHI_ERR_COLLISION);
if (ret < 0) {
if (ret < 0)
{
std::cerr << "Cannot add service: " << avahi_strerror(ret) << std::endl;
}
ret = avahi_entry_group_commit(entry_group);
if (ret < 0) {
if (ret < 0)
{
std::cerr << "Cannot commit entry group: " << avahi_strerror(ret) << std::endl;
} else {
}
else
{
std::cout << "Service published: " << name << std::endl;
}
}
void
avahi_publisher::avahi_entry_group_callback(
AvahiEntryGroup *g,
AvahiEntryGroupState state /**< The new state of the entry group */,
void *userdata /* The arbitrary user data pointer originally passed to avahi_entry_group_new()*/)
void avahi_publisher::avahi_entry_group_callback(
AvahiEntryGroup * g,
AvahiEntryGroupState state /**< The new state of the entry group */,
void * userdata /* The arbitrary user data pointer originally passed to avahi_entry_group_new()*/)
{
avahi_publisher *self = (avahi_publisher *)userdata;
avahi_publisher * self = (avahi_publisher *)userdata;
switch (state) {
case AVAHI_ENTRY_GROUP_ESTABLISHED:
case AVAHI_ENTRY_GROUP_FAILURE:
case AVAHI_ENTRY_GROUP_REGISTERING:
case AVAHI_ENTRY_GROUP_UNCOMMITED: break;
case AVAHI_ENTRY_GROUP_COLLISION:
self->alt_name();
self->create_service(avahi_entry_group_get_client(g));
switch (state)
{
case AVAHI_ENTRY_GROUP_ESTABLISHED:
case AVAHI_ENTRY_GROUP_FAILURE:
case AVAHI_ENTRY_GROUP_REGISTERING:
case AVAHI_ENTRY_GROUP_UNCOMMITED:
break;
case AVAHI_ENTRY_GROUP_COLLISION:
self->alt_name();
self->create_service(avahi_entry_group_get_client(g));
break;
break;
}
}
void
avahi_publisher::client_callback(AvahiClient *s,
AvahiClientState state /**< The new state of the client */,
void *userdata /**< The user data that was passed to avahi_client_new() */)
void avahi_publisher::client_callback(AvahiClient * s,
AvahiClientState state /**< The new state of the client */,
void * userdata /**< The user data that was passed to avahi_client_new() */)
{
avahi_publisher *self = (avahi_publisher *)userdata;
switch (state) {
case AVAHI_CLIENT_CONNECTING:
case AVAHI_CLIENT_FAILURE: break;
avahi_publisher * self = (avahi_publisher *)userdata;
switch (state)
{
case AVAHI_CLIENT_CONNECTING:
case AVAHI_CLIENT_FAILURE:
break;
case AVAHI_CLIENT_S_COLLISION:
case AVAHI_CLIENT_S_REGISTERING: avahi_entry_group_reset(self->entry_group); break;
case AVAHI_CLIENT_S_COLLISION:
case AVAHI_CLIENT_S_REGISTERING:
avahi_entry_group_reset(self->entry_group);
break;
case AVAHI_CLIENT_S_RUNNING: self->create_service(s); break;
case AVAHI_CLIENT_S_RUNNING:
self->create_service(s);
break;
}
}
avahi_publisher::avahi_publisher(const char *name, std::string type, int port)
: name(avahi_strdup(name)), type(std::move(type)), port(port)
avahi_publisher::avahi_publisher(const char * name, std::string type, int port) :
name(avahi_strdup(name)), type(std::move(type)), port(port)
{
avahi_poll = avahi_simple_poll_new();
int error;
avahi_client =
avahi_client_new(avahi_simple_poll_get(avahi_poll), (AvahiClientFlags)0, &client_callback, this, &error);
avahi_client_new(avahi_simple_poll_get(avahi_poll), (AvahiClientFlags)0, &client_callback, this, &error);
if (!avahi_client)
throw std::runtime_error(std::string("Cannot create avahi client: ") + avahi_strerror(error));
@@ -108,26 +117,23 @@ avahi_publisher::~avahi_publisher()
avahi_simple_poll_free(avahi_poll);
}
AvahiWatch *
avahi_publisher::watch_new(int fd,
AvahiWatchEvent event,
void (*callback)(AvahiWatch *w, int fd, AvahiWatchEvent event, void *userdata),
void *userdata)
AvahiWatch * avahi_publisher::watch_new(int fd,
AvahiWatchEvent event,
void (*callback)(AvahiWatch * w, int fd, AvahiWatchEvent event, void * userdata),
void * userdata)
{
AvahiWatch *watch = avahi_simple_poll_get(avahi_poll)
->watch_new(avahi_simple_poll_get(avahi_poll), fd, event, callback, userdata);
AvahiWatch * watch = avahi_simple_poll_get(avahi_poll)
->watch_new(avahi_simple_poll_get(avahi_poll), fd, event, callback, userdata);
return watch;
}
void
avahi_publisher::watch_free(AvahiWatch *watch)
void avahi_publisher::watch_free(AvahiWatch * watch)
{
avahi_simple_poll_get(avahi_poll)->watch_free(watch);
}
bool
avahi_publisher::iterate(int sleep_time)
bool avahi_publisher::iterate(int sleep_time)
{
int r = avahi_simple_poll_iterate(avahi_poll, sleep_time);