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