Mercurial > irccd
changeset 536:623cb5d831d2
Irccd: split transport_service
author | David Demelier <markand@malikania.fr> |
---|---|
date | Fri, 17 Nov 2017 20:47:36 +0100 |
parents | 7e9bf74e0fd5 |
children | a4193cbce05d |
files | irccd/main.cpp libirccd/CMakeLists.txt libirccd/irccd/irccd.cpp libirccd/irccd/service.cpp libirccd/irccd/service.hpp libirccd/irccd/transport_service.cpp libirccd/irccd/transport_service.hpp |
diffstat | 7 files changed, 175 insertions(+), 120 deletions(-) [+] |
line wrap: on
line diff
--- a/irccd/main.cpp Fri Nov 17 21:01:23 2017 +0100 +++ b/irccd/main.cpp Fri Nov 17 20:47:36 2017 +0100 @@ -39,6 +39,7 @@ #include "service.hpp" #include "string_util.hpp" #include "system.hpp" +#include "transport_service.hpp" #include "config.hpp" #include "irccd.hpp"
--- a/libirccd/CMakeLists.txt Fri Nov 17 21:01:23 2017 +0100 +++ b/libirccd/CMakeLists.txt Fri Nov 17 20:47:36 2017 +0100 @@ -32,6 +32,7 @@ ${libirccd_SOURCE_DIR}/irccd/service.hpp ${libirccd_SOURCE_DIR}/irccd/transport_client.hpp ${libirccd_SOURCE_DIR}/irccd/transport_server.hpp + ${libirccd_SOURCE_DIR}/irccd/transport_service.hpp ) set( @@ -46,6 +47,7 @@ ${libirccd_SOURCE_DIR}/irccd/service.cpp ${libirccd_SOURCE_DIR}/irccd/transport_client.cpp ${libirccd_SOURCE_DIR}/irccd/transport_server.cpp + ${libirccd_SOURCE_DIR}/irccd/transport_service.cpp ) irccd_define_library(
--- a/libirccd/irccd/irccd.cpp Fri Nov 17 21:01:23 2017 +0100 +++ b/libirccd/irccd/irccd.cpp Fri Nov 17 20:47:36 2017 +0100 @@ -19,6 +19,7 @@ #include "irccd.hpp" #include "logger.hpp" #include "net_util.hpp" +#include "transport_service.hpp" #include "service.hpp" #include "util.hpp"
--- a/libirccd/irccd/service.cpp Fri Nov 17 21:01:23 2017 +0100 +++ b/libirccd/irccd/service.cpp Fri Nov 17 20:47:36 2017 +0100 @@ -27,8 +27,7 @@ #include "service.hpp" #include "string_util.hpp" #include "system.hpp" -#include "transport_client.hpp" -#include "transport_server.hpp" +#include "transport_service.hpp" using namespace std::string_literals; @@ -919,75 +918,4 @@ servers_.clear(); } -/* - * transport_service. - * ------------------------------------------------------------------ - */ - -void transport_service::handle_command(std::shared_ptr<transport_client> tc, const nlohmann::json& object) -{ - assert(object.is_object()); - - irccd_.post([=] (irccd&) { - auto name = object.find("command"); - if (name == object.end() || !name->is_string()) { - // TODO: send error. - log::warning("invalid command object"); - return; - } - - auto cmd = irccd_.commands().find(*name); - - if (!cmd) - tc->error(*name, "command does not exist"); - else { - try { - cmd->exec(irccd_, *tc, object); - } catch (const std::exception& ex) { - tc->error(cmd->name(), ex.what()); - } - } - }); -} - -void transport_service::do_accept(std::shared_ptr<transport_server> ts) -{ - ts->accept([this, ts] (auto client, auto code) { - if (code) { - log::warning() << "transport: " << code.message() << std::endl; - } else { - client->recv([this, client] (auto json, auto code) { - if (code) - log::warning() << "transport: " << code.message() << std::endl; - else - handle_command(client, json); - }); - } - - do_accept(ts); - }); -} - -transport_service::transport_service(irccd& irccd) noexcept - : irccd_(irccd) -{ -} - -void transport_service::add(std::shared_ptr<transport_server> ts) -{ - assert(ts); - - do_accept(ts); - servers_.push_back(std::move(ts)); -} - -void transport_service::broadcast(const nlohmann::json& json) -{ - assert(json.is_object()); - - for (const auto& servers : servers_) - for (const auto& client : servers->clients()) - client->send(json); -} - } // !irccd
--- a/libirccd/irccd/service.hpp Fri Nov 17 21:01:23 2017 +0100 +++ b/libirccd/irccd/service.hpp Fri Nov 17 20:47:36 2017 +0100 @@ -473,53 +473,6 @@ void clear() noexcept; }; -/* - * transport_service. - * ------------------------------------------------------------------ - */ - -class transport_server; -class transport_client; - -/** - * \brief manage transport servers and clients. - * \ingroup services - */ -class transport_service { -public: - using servers_t = std::vector<std::shared_ptr<transport_server>>; - -private: - irccd& irccd_; - servers_t servers_; - - void handle_command(std::shared_ptr<transport_client>, const nlohmann::json&); - void do_accept(std::shared_ptr<transport_server>); - -public: - /** - * Create the transport service. - * - * \param irccd the irccd instance - */ - transport_service(irccd& irccd) noexcept; - - /** - * Add a transport server. - * - * \param ts the transport server - */ - void add(std::shared_ptr<transport_server> ts); - - /** - * Send data to all clients. - * - * \pre object.is_object() - * \param object the json object - */ - void broadcast(const nlohmann::json& object); -}; - } // !irccd #endif // !IRCCD_SERVICE_HPP
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/libirccd/irccd/transport_service.cpp Fri Nov 17 20:47:36 2017 +0100 @@ -0,0 +1,97 @@ +/* + * transport_service.cpp -- transport service + * + * Copyright (c) 2013-2017 David Demelier <markand@malikania.fr> + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include <cassert> + +#include "command.hpp" +#include "irccd.hpp" +#include "logger.hpp" +#include "service.hpp" +#include "transport_client.hpp" +#include "transport_server.hpp" +#include "transport_service.hpp" + +namespace irccd { + +void transport_service::handle_command(std::shared_ptr<transport_client> tc, const nlohmann::json& object) +{ + assert(object.is_object()); + + irccd_.post([=] (irccd&) { + auto name = object.find("command"); + if (name == object.end() || !name->is_string()) { + // TODO: send error. + log::warning("invalid command object"); + return; + } + + auto cmd = irccd_.commands().find(*name); + + if (!cmd) + tc->error(*name, "command does not exist"); + else { + try { + cmd->exec(irccd_, *tc, object); + } catch (const std::exception& ex) { + tc->error(cmd->name(), ex.what()); + } + } + }); +} + +void transport_service::do_accept(std::shared_ptr<transport_server> ts) +{ + ts->accept([this, ts] (auto client, auto code) { + if (code) { + log::warning() << "transport: " << code.message() << std::endl; + } else { + client->recv([this, client] (auto json, auto code) { + if (code) + log::warning() << "transport: " << code.message() << std::endl; + else + handle_command(client, json); + }); + } + + do_accept(ts); + }); +} + +transport_service::transport_service(irccd& irccd) noexcept + : irccd_(irccd) +{ +} + +void transport_service::add(std::shared_ptr<transport_server> ts) +{ + assert(ts); + + do_accept(ts); + servers_.push_back(std::move(ts)); +} + +void transport_service::broadcast(const nlohmann::json& json) +{ + assert(json.is_object()); + + for (const auto& servers : servers_) + for (const auto& client : servers->clients()) + client->send(json); +} + +} // !irccd
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/libirccd/irccd/transport_service.hpp Fri Nov 17 20:47:36 2017 +0100 @@ -0,0 +1,73 @@ +/* + * transport_service.hpp -- transport service + * + * Copyright (c) 2013-2017 David Demelier <markand@malikania.fr> + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#ifndef IRCCD_TRANSPORT_SERVICE_HPP +#define IRCCD_TRANSPORT_SERVICE_HPP + +#include <memory> +#include <vector> + +#include <json.hpp> + +namespace irccd { + +class transport_client; +class transport_server; + +/** + * \brief manage transport servers and clients. + * \ingroup services + */ +class transport_service { +public: + using servers_t = std::vector<std::shared_ptr<transport_server>>; + +private: + irccd& irccd_; + servers_t servers_; + + void handle_command(std::shared_ptr<transport_client>, const nlohmann::json&); + void do_accept(std::shared_ptr<transport_server>); + +public: + /** + * Create the transport service. + * + * \param irccd the irccd instance + */ + transport_service(irccd& irccd) noexcept; + + /** + * Add a transport server. + * + * \param ts the transport server + */ + void add(std::shared_ptr<transport_server> ts); + + /** + * Send data to all clients. + * + * \pre object.is_object() + * \param object the json object + */ + void broadcast(const nlohmann::json& object); +}; + +} // !irccd + +#endif // !IRCCD_TRANSPORT_SERVICE_HPP