Commit 871063da authored by Max Kellermann's avatar Max Kellermann

neighbor/Plugin: migrate from class Error to C++ exceptions

parent 135662d6
......@@ -461,10 +461,7 @@ int mpd_main(int argc, char *argv[])
#ifdef ENABLE_NEIGHBOR_PLUGINS
instance->neighbors = new NeighborGlue();
if (!instance->neighbors->Init(io_thread_get(), *instance, error)) {
LogError(error);
return EXIT_FAILURE;
}
instance->neighbors->Init(io_thread_get(), *instance);
if (instance->neighbors->IsEmpty()) {
delete instance->neighbors;
......@@ -563,9 +560,8 @@ try {
io_thread_start();
#ifdef ENABLE_NEIGHBOR_PLUGINS
if (instance->neighbors != nullptr &&
!instance->neighbors->Open(error))
FatalError(error);
if (instance->neighbors != nullptr)
instance->neighbors->Open();
#endif
ZeroconfInit(instance->event_loop);
......
......@@ -22,7 +22,6 @@
#include <forward_list>
class Error;
class NeighborListener;
struct NeighborInfo;
......@@ -54,8 +53,10 @@ public:
/**
* Start exploring the neighborhood.
*
* Throws std::runtime_error on error.
*/
virtual bool Open(Error &error) = 0;
virtual void Open() = 0;
/**
* Stop exploring.
......
......@@ -26,7 +26,6 @@
#include "config/ConfigGlobal.hxx"
#include "config/ConfigError.hxx"
#include "config/Block.hxx"
#include "util/Error.hxx"
#include "util/RuntimeError.hxx"
#include <stdexcept>
......@@ -40,63 +39,50 @@ NeighborGlue::~NeighborGlue() {}
static NeighborExplorer *
CreateNeighborExplorer(EventLoop &loop, NeighborListener &listener,
const ConfigBlock &block, Error &error)
const ConfigBlock &block)
{
const char *plugin_name = block.GetBlockValue("plugin");
if (plugin_name == nullptr) {
error.Set(config_domain,
"Missing \"plugin\" configuration");
return nullptr;
}
if (plugin_name == nullptr)
throw std::runtime_error("Missing \"plugin\" configuration");
const NeighborPlugin *plugin = GetNeighborPluginByName(plugin_name);
if (plugin == nullptr) {
error.Format(config_domain, "No such neighbor plugin: %s",
plugin_name);
return nullptr;
}
if (plugin == nullptr)
throw FormatRuntimeError("No such neighbor plugin: %s",
plugin_name);
return plugin->create(loop, listener, block, error);
return plugin->create(loop, listener, block);
}
bool
NeighborGlue::Init(EventLoop &loop, NeighborListener &listener, Error &error)
void
NeighborGlue::Init(EventLoop &loop, NeighborListener &listener)
{
for (const auto *block = config_get_block(ConfigBlockOption::NEIGHBORS);
block != nullptr; block = block->next) {
try {
auto *explorer =
CreateNeighborExplorer(loop, listener, *block,
error);
if (explorer == nullptr) {
error.FormatPrefix("Line %i: ", block->line);
return false;
}
CreateNeighborExplorer(loop, listener, *block);
explorers.emplace_front(explorer);
} catch (...) {
std::throw_with_nested(FormatRuntimeError("Line %i: ",
block->line));
}
}
return true;
}
bool
NeighborGlue::Open(Error &error)
void
NeighborGlue::Open()
{
for (auto i = explorers.begin(), end = explorers.end();
i != end; ++i) {
if (!i->explorer->Open(error)) {
try {
i->explorer->Open();
} catch (...) {
/* roll back */
for (auto k = explorers.begin(); k != i; ++k)
k->explorer->Close();
return false;
throw;
}
}
return true;
}
void
......
......@@ -27,7 +27,6 @@
#include <forward_list>
struct config_param;
class Error;
class EventLoop;
class NeighborExplorer;
class NeighborListener;
......@@ -60,9 +59,12 @@ public:
return explorers.empty();
}
bool Init(EventLoop &loop, NeighborListener &listener, Error &error);
/**
* Throws std::runtime_error on error.
*/
void Init(EventLoop &loop, NeighborListener &listener);
bool Open(Error &error);
void Open();
void Close();
/**
......
......@@ -21,7 +21,6 @@
#define MPD_NEIGHBOR_PLUGIN_HXX
struct ConfigBlock;
class Error;
class EventLoop;
class NeighborListener;
class NeighborExplorer;
......@@ -33,8 +32,7 @@ struct NeighborPlugin {
* Allocates and configures a #NeighborExplorer instance.
*/
NeighborExplorer *(*create)(EventLoop &loop, NeighborListener &listener,
const ConfigBlock &block,
Error &error);
const ConfigBlock &block);
};
#endif
......@@ -72,7 +72,7 @@ public:
:NeighborExplorer(_listener) {}
/* virtual methods from class NeighborExplorer */
virtual bool Open(Error &error) override;
void Open() override;
virtual void Close() override;
virtual List GetList() const override;
......@@ -82,12 +82,11 @@ private:
static void ThreadFunc(void *ctx);
};
bool
SmbclientNeighborExplorer::Open(gcc_unused Error &error)
void
SmbclientNeighborExplorer::Open()
{
quit = false;
thread.Start(ThreadFunc, this);
return true;
}
void
......@@ -270,8 +269,7 @@ SmbclientNeighborExplorer::ThreadFunc(void *ctx)
static NeighborExplorer *
smbclient_neighbor_create(gcc_unused EventLoop &loop,
NeighborListener &listener,
gcc_unused const ConfigBlock &block,
gcc_unused Error &error)
gcc_unused const ConfigBlock &block)
{
SmbclientInit();
......
......@@ -60,7 +60,7 @@ public:
:NeighborExplorer(_listener) {}
/* virtual methods from class NeighborExplorer */
virtual bool Open(Error &error) override;
void Open() override;
virtual void Close() override;
virtual List GetList() const override;
......@@ -70,8 +70,8 @@ private:
virtual void LostUPnP(const ContentDirectoryService &service) override;
};
bool
UpnpNeighborExplorer::Open(gcc_unused Error &error)
void
UpnpNeighborExplorer::Open()
{
UpnpClient_Handle handle;
UpnpClientGlobalInit(handle);
......@@ -85,8 +85,6 @@ UpnpNeighborExplorer::Open(gcc_unused Error &error)
UpnpClientGlobalFinish();
throw;
}
return true;
}
void
......@@ -130,8 +128,7 @@ UpnpNeighborExplorer::LostUPnP(const ContentDirectoryService &service)
static NeighborExplorer *
upnp_neighbor_create(gcc_unused EventLoop &loop,
NeighborListener &listener,
gcc_unused const ConfigBlock &block,
gcc_unused Error &error)
gcc_unused const ConfigBlock &block)
{
return new UpnpNeighborExplorer(listener);
}
......
......@@ -24,7 +24,6 @@
#include "neighbor/Glue.hxx"
#include "fs/Path.hxx"
#include "event/Loop.hxx"
#include "util/Error.hxx"
#include "Log.hxx"
#include <stdio.h>
......@@ -56,8 +55,6 @@ try {
/* read configuration file (mpd.conf) */
Error error;
config_global_init();
ReadConfigFile(config_path);
......@@ -69,17 +66,15 @@ try {
MyNeighborListener listener;
NeighborGlue neighbor;
if (!neighbor.Init(loop, listener, error) || !neighbor.Open(error)) {
LogError(error);
return EXIT_FAILURE;
}
neighbor.Init(loop, listener);
neighbor.Open();
/* run */
loop.Run();
neighbor.Close();
return EXIT_SUCCESS;
} catch (const std::exception &e) {
} catch (const std::exception &e) {
LogError(e);
return EXIT_FAILURE;
}
}
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