Commit 2d8ecd56 authored by Max Kellermann's avatar Max Kellermann

zeroconf: return a publisher object

parent 2059195a
......@@ -476,12 +476,15 @@ MainConfigured(const struct options &options, const ConfigData &raw_config)
};
#endif
#ifdef HAVE_ZEROCONF
std::unique_ptr<ZeroconfHelper> zeroconf;
try {
ZeroconfInit(raw_config, instance.event_loop);
zeroconf = ZeroconfInit(raw_config, instance.event_loop);
} catch (...) {
LogError(std::current_exception(),
"Zeroconf initialization failed");
}
#endif
#ifdef ENABLE_DATABASE
if (create_db) {
......@@ -543,7 +546,9 @@ MainConfigured(const struct options &options, const ConfigData &raw_config)
instance.BeginShutdownUpdate();
ZeroconfDeinit();
#ifdef HAVE_ZEROCONF
zeroconf.reset();
#endif
instance.BeginShutdownPartitions();
}
......
......@@ -18,6 +18,7 @@
*/
#include "Glue.hxx"
#include "Helper.hxx"
#include "config/Data.hxx"
#include "config/Option.hxx"
#include "Listen.hxx"
......@@ -53,31 +54,19 @@ static constexpr Domain zeroconf_domain("zeroconf");
#define DEFAULT_ZEROCONF_ENABLED 1
static int zeroconfEnabled;
#ifdef HAVE_AVAHI
static std::unique_ptr<AvahiHelper> avahi_helper;
#endif
#ifdef HAVE_BONJOUR
static std::unique_ptr<BonjourHelper> bonjour_helper;
#endif
void
std::unique_ptr<ZeroconfHelper>
ZeroconfInit(const ConfigData &config, [[maybe_unused]] EventLoop &loop)
{
const char *serviceName;
zeroconfEnabled = config.GetBool(ConfigOption::ZEROCONF_ENABLED,
DEFAULT_ZEROCONF_ENABLED);
if (!zeroconfEnabled)
return;
if (!config.GetBool(ConfigOption::ZEROCONF_ENABLED,
DEFAULT_ZEROCONF_ENABLED))
return nullptr;
if (listen_port <= 0) {
LogWarning(zeroconf_domain,
"No global port, disabling zeroconf");
zeroconfEnabled = false;
return;
return nullptr;
}
serviceName = config.GetString(ConfigOption::ZEROCONF_NAME,
......@@ -96,22 +85,12 @@ ZeroconfInit(const ConfigData &config, [[maybe_unused]] EventLoop &loop)
}
#ifdef HAVE_AVAHI
avahi_helper = AvahiInit(loop, serviceName, listen_port);
#endif
#ifdef HAVE_BONJOUR
bonjour_helper = BonjourInit(loop, serviceName, listen_port);
#endif
}
void
ZeroconfDeinit() noexcept
{
#ifdef HAVE_AVAHI
avahi_helper.reset();
return std::make_unique<ZeroconfHelper>(AvahiInit(loop, serviceName,
listen_port));
#endif
#ifdef HAVE_BONJOUR
bonjour_helper.reset();
return std::make_unique<ZeroconfHelper>(BonjourInit(loop, serviceName,
listen_port));
#endif
}
......@@ -20,32 +20,23 @@
#ifndef MPD_ZEROCONF_GLUE_HXX
#define MPD_ZEROCONF_GLUE_HXX
#include "Helper.hxx"
#include "config.h"
#include <memory>
struct ConfigData;
class EventLoop;
class ZeroconfHelper;
#ifdef HAVE_ZEROCONF
/**
* Throws on error.
*/
void
std::unique_ptr<ZeroconfHelper>
ZeroconfInit(const ConfigData &config, EventLoop &loop);
void
ZeroconfDeinit() noexcept;
#else /* ! HAVE_ZEROCONF */
static inline void
ZeroconfInit(const ConfigData &, EventLoop &)
{}
static inline void
ZeroconfDeinit() noexcept
{}
#endif /* ! HAVE_ZEROCONF */
#endif
/*
* Copyright 2003-2021 The Music Player Daemon Project
* http://www.musicpd.org
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "Helper.hxx"
#ifdef HAVE_AVAHI
#include "avahi/Helper.hxx"
#endif
#ifdef HAVE_BONJOUR
#include "Bonjour.hxx"
#endif
ZeroconfHelper::~ZeroconfHelper() noexcept = default;
/*
* Copyright 2003-2021 The Music Player Daemon Project
* http://www.musicpd.org
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#ifndef MPD_ZEROCONF_HELPER_HXX
#define MPD_ZEROCONF_HELPER_HXX
#include "config.h"
#include <memory>
class AvahiHelper;
class BonjourHelper;
class ZeroconfHelper final {
#ifdef HAVE_AVAHI
std::unique_ptr<AvahiHelper> helper;
#endif
#ifdef HAVE_BONJOUR
std::unique_ptr<BonjourHelper> helper;
#endif
public:
template<typename T>
ZeroconfHelper(T &&_helper) noexcept
:helper(std::forward<T>(_helper)) {}
~ZeroconfHelper() noexcept;
};
#endif
......@@ -36,6 +36,7 @@ if zeroconf_option == 'bonjour'
zeroconf = static_library(
'zeroconf_bonjour',
'Glue.cxx',
'Helper.cxx',
'Bonjour.cxx',
include_directories: inc,
dependencies: [
......@@ -60,6 +61,7 @@ else
zeroconf = static_library(
'zeroconf_bonjour',
'Glue.cxx',
'Helper.cxx',
include_directories: inc,
dependencies: [
avahi_dep,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment