Commit d52c7e7a authored by Max Kellermann's avatar Max Kellermann

output/httpd: throw C++ exception on init error

parent c4acccac
......@@ -150,9 +150,13 @@ private:
unsigned clients_max;
public:
HttpdOutput(EventLoop &_loop);
HttpdOutput(EventLoop &_loop, const ConfigBlock &block);
~HttpdOutput();
operator AudioOutput *() {
return &base;
}
#if CLANG_OR_GCC_VERSION(4,7)
constexpr
#endif
......@@ -162,21 +166,6 @@ public:
using DeferredMonitor::GetEventLoop;
bool Init(const ConfigBlock &block, Error &error);
bool Configure(const ConfigBlock &block, Error &error);
AudioOutput *InitAndConfigure(const ConfigBlock &block,
Error &error) {
if (!Init(block, error))
return nullptr;
if (!Configure(block, error))
return nullptr;
return &base;
}
void Bind();
void Unbind();
......
......@@ -32,6 +32,7 @@
#include "system/fd_util.h"
#include "IOThread.hxx"
#include "event/Call.hxx"
#include "util/RuntimeError.hxx"
#include "util/Error.hxx"
#include "util/Domain.hxx"
#include "util/DeleteDisposer.hxx"
......@@ -50,45 +51,12 @@
const Domain httpd_output_domain("httpd_output");
inline
HttpdOutput::HttpdOutput(EventLoop &_loop)
HttpdOutput::HttpdOutput(EventLoop &_loop, const ConfigBlock &block)
:ServerSocket(_loop), DeferredMonitor(_loop),
base(httpd_output_plugin),
base(httpd_output_plugin, block),
encoder(nullptr), unflushed_input(0),
metadata(nullptr)
{
}
HttpdOutput::~HttpdOutput()
{
if (metadata != nullptr)
metadata->Unref();
delete prepared_encoder;
}
inline void
HttpdOutput::Bind()
{
open = false;
BlockingCall(GetEventLoop(), [this](){
ServerSocket::Open();
});
}
inline void
HttpdOutput::Unbind()
{
assert(!open);
BlockingCall(GetEventLoop(), [this](){
ServerSocket::Close();
});
}
inline bool
HttpdOutput::Configure(const ConfigBlock &block, Error &error)
{
/* read configuration */
name = block.GetBlockValue("name", "Set name in config");
genre = block.GetBlockValue("genre", "Set genre in config");
......@@ -99,11 +67,8 @@ HttpdOutput::Configure(const ConfigBlock &block, Error &error)
const char *encoder_name =
block.GetBlockValue("encoder", "vorbis");
const auto encoder_plugin = encoder_plugin_get(encoder_name);
if (encoder_plugin == nullptr) {
error.Format(httpd_output_domain,
"No such encoder: %s", encoder_name);
return false;
}
if (encoder_plugin == nullptr)
throw FormatRuntimeError("No such encoder: %s", encoder_name);
clients_max = block.GetBlockValue("max_clients", 0u);
......@@ -123,31 +88,40 @@ HttpdOutput::Configure(const ConfigBlock &block, Error &error)
content_type = prepared_encoder->GetMimeType();
if (content_type == nullptr)
content_type = "application/octet-stream";
}
return true;
HttpdOutput::~HttpdOutput()
{
if (metadata != nullptr)
metadata->Unref();
delete prepared_encoder;
}
inline bool
HttpdOutput::Init(const ConfigBlock &block, Error &error)
inline void
HttpdOutput::Bind()
{
return base.Configure(block, error);
open = false;
BlockingCall(GetEventLoop(), [this](){
ServerSocket::Open();
});
}
static AudioOutput *
httpd_output_init(const ConfigBlock &block, Error &error)
inline void
HttpdOutput::Unbind()
{
HttpdOutput *httpd = new HttpdOutput(io_thread_get());
assert(!open);
try {
AudioOutput *result = httpd->InitAndConfigure(block, error);
if (result == nullptr)
delete httpd;
BlockingCall(GetEventLoop(), [this](){
ServerSocket::Close();
});
}
return result;
} catch (const std::runtime_error &e) {
delete httpd;
throw;
}
static AudioOutput *
httpd_output_init(const ConfigBlock &block, Error &)
{
return *new HttpdOutput(io_thread_get(), block);
}
static void
......
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