2014-01-18 16:36:42 +01:00
|
|
|
/*
|
2017-01-03 20:48:59 +01:00
|
|
|
* Copyright 2003-2017 The Music Player Daemon Project
|
2014-01-18 16:36:42 +01:00
|
|
|
* 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 "config.h"
|
|
|
|
#include "Glue.hxx"
|
|
|
|
#include "Registry.hxx"
|
|
|
|
#include "Explorer.hxx"
|
|
|
|
#include "NeighborPlugin.hxx"
|
|
|
|
#include "Info.hxx"
|
2018-07-16 19:50:07 +02:00
|
|
|
#include "config/Global.hxx"
|
|
|
|
#include "config/Domain.hxx"
|
2015-01-21 22:13:44 +01:00
|
|
|
#include "config/Block.hxx"
|
2016-09-05 11:29:10 +02:00
|
|
|
#include "util/RuntimeError.hxx"
|
|
|
|
|
|
|
|
#include <stdexcept>
|
2014-01-18 16:36:42 +01:00
|
|
|
|
2018-01-02 17:00:27 +01:00
|
|
|
NeighborGlue::NeighborGlue() noexcept {}
|
2018-01-02 16:58:14 +01:00
|
|
|
NeighborGlue::~NeighborGlue() noexcept {}
|
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,
|
2016-09-05 10:53:54 +02:00
|
|
|
const ConfigBlock &block)
|
2014-01-18 16:36:42 +01:00
|
|
|
{
|
2015-01-21 22:13:44 +01:00
|
|
|
const char *plugin_name = block.GetBlockValue("plugin");
|
2016-09-05 10:53:54 +02:00
|
|
|
if (plugin_name == nullptr)
|
|
|
|
throw std::runtime_error("Missing \"plugin\" configuration");
|
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)
|
|
|
|
throw FormatRuntimeError("No such neighbor plugin: %s",
|
|
|
|
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
|
|
|
|
NeighborGlue::Init(EventLoop &loop, NeighborListener &listener)
|
2014-01-18 16:36:42 +01:00
|
|
|
{
|
2015-01-21 22:13:44 +01:00
|
|
|
for (const auto *block = config_get_block(ConfigBlockOption::NEIGHBORS);
|
|
|
|
block != nullptr; block = block->next) {
|
2018-07-17 21:08:41 +02:00
|
|
|
block->SetUsed();
|
|
|
|
|
2016-09-05 11:29:10 +02:00
|
|
|
try {
|
2018-01-02 17:00:27 +01:00
|
|
|
explorers.emplace_front(CreateNeighborExplorer(loop,
|
|
|
|
listener,
|
|
|
|
*block));
|
2016-09-05 11:29:10 +02:00
|
|
|
} catch (...) {
|
|
|
|
std::throw_with_nested(FormatRuntimeError("Line %i: ",
|
|
|
|
block->line));
|
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();
|
2016-09-05 10:53:54 +02:00
|
|
|
throw;
|
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
|
|
|
{
|
|
|
|
for (auto i = explorers.begin(), end = explorers.end(); i != end; ++i)
|
|
|
|
i->explorer->Close();
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|