2023-03-06 14:42:04 +01:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
// Copyright The Music Player Daemon Project
|
2014-01-18 16:36:42 +01:00
|
|
|
|
|
|
|
#include "Glue.hxx"
|
|
|
|
#include "Registry.hxx"
|
|
|
|
#include "Explorer.hxx"
|
|
|
|
#include "NeighborPlugin.hxx"
|
|
|
|
#include "Info.hxx"
|
2018-07-17 23:11:33 +02:00
|
|
|
#include "config/Data.hxx"
|
2015-01-21 22:13:44 +01:00
|
|
|
#include "config/Block.hxx"
|
2022-11-28 21:58:21 +01:00
|
|
|
#include "lib/fmt/RuntimeError.hxx"
|
2016-09-05 11:29:10 +02:00
|
|
|
|
|
|
|
#include <stdexcept>
|
2014-01-18 16:36:42 +01:00
|
|
|
|
2020-02-01 13:47:16 +01:00
|
|
|
NeighborGlue::NeighborGlue() noexcept = default;
|
|
|
|
NeighborGlue::~NeighborGlue() noexcept = default;
|
2014-01-18 16:36:42 +01:00
|
|
|
|
2018-01-02 17:00:27 +01:00
|
|
|
static std::unique_ptr<NeighborExplorer>
|
2014-01-18 16:36:42 +01:00
|
|
|
CreateNeighborExplorer(EventLoop &loop, NeighborListener &listener,
|
2021-10-19 13:26:31 +02:00
|
|
|
const char *plugin_name,
|
2016-09-05 10:53:54 +02:00
|
|
|
const ConfigBlock &block)
|
2014-01-18 16:36:42 +01:00
|
|
|
{
|
|
|
|
const NeighborPlugin *plugin = GetNeighborPluginByName(plugin_name);
|
2016-09-05 10:53:54 +02:00
|
|
|
if (plugin == nullptr)
|
2024-04-16 11:06:34 +02:00
|
|
|
throw FmtRuntimeError("No such neighbor plugin: {:?}",
|
2022-11-28 21:58:21 +01:00
|
|
|
plugin_name);
|
2014-01-18 16:36:42 +01:00
|
|
|
|
2016-09-05 10:53:54 +02:00
|
|
|
return plugin->create(loop, listener, block);
|
2014-01-18 16:36:42 +01:00
|
|
|
}
|
|
|
|
|
2016-09-05 10:53:54 +02:00
|
|
|
void
|
2018-07-17 23:11:33 +02:00
|
|
|
NeighborGlue::Init(const ConfigData &config,
|
|
|
|
EventLoop &loop, NeighborListener &listener)
|
2014-01-18 16:36:42 +01:00
|
|
|
{
|
2022-07-12 21:02:49 +02:00
|
|
|
config.WithEach(ConfigBlockOption::NEIGHBORS, [&, this](const auto &block){
|
|
|
|
const char *plugin_name = block.GetBlockValue("plugin");
|
|
|
|
if (plugin_name == nullptr)
|
|
|
|
throw std::runtime_error("Missing \"plugin\" configuration");
|
2018-07-17 21:08:41 +02:00
|
|
|
|
2022-07-12 21:02:49 +02:00
|
|
|
explorers.emplace_front(plugin_name,
|
|
|
|
CreateNeighborExplorer(loop, listener,
|
|
|
|
plugin_name,
|
|
|
|
block));
|
|
|
|
});
|
2014-01-18 16:36:42 +01:00
|
|
|
}
|
|
|
|
|
2016-09-05 10:53:54 +02:00
|
|
|
void
|
|
|
|
NeighborGlue::Open()
|
2014-01-18 16:36:42 +01:00
|
|
|
{
|
|
|
|
for (auto i = explorers.begin(), end = explorers.end();
|
|
|
|
i != end; ++i) {
|
2016-09-05 10:53:54 +02:00
|
|
|
try {
|
|
|
|
i->explorer->Open();
|
|
|
|
} catch (...) {
|
2014-01-18 16:36:42 +01:00
|
|
|
/* roll back */
|
2014-01-26 13:41:31 +01:00
|
|
|
for (auto k = explorers.begin(); k != i; ++k)
|
2014-01-18 16:36:42 +01:00
|
|
|
k->explorer->Close();
|
2021-10-19 13:26:31 +02:00
|
|
|
|
2024-04-16 11:06:34 +02:00
|
|
|
std::throw_with_nested(FmtRuntimeError("Failed to open neighblor plugin {:?}",
|
2022-11-28 21:58:21 +01:00
|
|
|
i->name));
|
2014-01-18 16:36:42 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2018-01-02 16:58:14 +01:00
|
|
|
NeighborGlue::Close() noexcept
|
2014-01-18 16:36:42 +01:00
|
|
|
{
|
2020-01-31 20:09:15 -08:00
|
|
|
for (auto & explorer : explorers)
|
|
|
|
explorer.explorer->Close();
|
2014-01-18 16:36:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
NeighborGlue::List
|
2017-05-08 14:44:49 +02:00
|
|
|
NeighborGlue::GetList() const noexcept
|
2014-01-18 16:36:42 +01:00
|
|
|
{
|
|
|
|
List result;
|
|
|
|
|
|
|
|
for (const auto &i : explorers)
|
|
|
|
result.splice_after(result.before_begin(),
|
|
|
|
i.explorer->GetList());
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|