lib/avahi/Publisher: make the service list dynamic

This allows editing the list of services at any time instead of
passing a constant list at construction time.  To do this, Service
instances are now caller-owned and managed in an IntrusiveList instead
of Publisher-owned in a std::forward_list.
This commit is contained in:
Max Kellermann
2024-01-17 18:10:28 +01:00
committed by Max Kellermann
parent b20b773189
commit 461da92064
5 changed files with 91 additions and 30 deletions

View File

@@ -4,7 +4,6 @@
#include "Helper.hxx"
#include "lib/avahi/Client.hxx"
#include "lib/avahi/ErrorHandler.hxx"
#include "lib/avahi/Service.hxx"
#include "lib/fmt/RuntimeError.hxx"
#include "Log.hxx"
@@ -29,12 +28,19 @@ static std::weak_ptr<SharedAvahiClient> shared_avahi_client;
inline
AvahiHelper::AvahiHelper(std::shared_ptr<SharedAvahiClient> _client,
const char *service_name,
std::forward_list<Avahi::Service> &&services)
const char *service_type, unsigned port)
:client(std::move(_client)),
publisher(client->client, service_name,
std::move(services), *client) {}
publisher(client->client, service_name, *client),
service(AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC,
service_type, port)
{
publisher.AddService(service);
}
AvahiHelper::~AvahiHelper() noexcept = default;
AvahiHelper::~AvahiHelper() noexcept
{
publisher.RemoveService(service);
}
std::unique_ptr<AvahiHelper>
AvahiInit(EventLoop &event_loop, const char *service_name,
@@ -49,11 +55,6 @@ AvahiInit(EventLoop &event_loop, const char *service_name,
shared_avahi_client = client =
std::make_shared<SharedAvahiClient>(event_loop);
std::forward_list<Avahi::Service> services;
services.emplace_front(AVAHI_IF_UNSPEC,
AVAHI_PROTO_UNSPEC,
service_type, port);
return std::make_unique<AvahiHelper>(std::move(client), service_name,
std::move(services));
service_type, port);
}

View File

@@ -4,6 +4,7 @@
#pragma once
#include "lib/avahi/Publisher.hxx"
#include "lib/avahi/Service.hxx"
#include <memory>
@@ -15,11 +16,12 @@ class SharedAvahiClient;
class AvahiHelper final {
std::shared_ptr<SharedAvahiClient> client;
Avahi::Publisher publisher;
Avahi::Service service;
public:
AvahiHelper(std::shared_ptr<SharedAvahiClient> _client,
const char *service_name,
std::forward_list<Avahi::Service> &&services);
const char *service_type, unsigned port);
~AvahiHelper() noexcept;
};