# HG changeset patch # User David Demelier # Date 1471462065 -7200 # Node ID 4dec71ea1326db2e44e52f882a17e40aea3debb9 # Parent 62807bf9a68c3490d86cb36b7332a5524facc9d5 Irccd: rename Service to Pollable diff -r 62807bf9a68c -r 4dec71ea1326 lib/irccd/CMakeSources.cmake --- a/lib/irccd/CMakeSources.cmake Wed Aug 17 20:41:25 2016 +0200 +++ b/lib/irccd/CMakeSources.cmake Wed Aug 17 21:27:45 2016 +0200 @@ -53,9 +53,9 @@ ${CMAKE_CURRENT_LIST_DIR}/plugin.hpp ${CMAKE_CURRENT_LIST_DIR}/plugin-dynlib.hpp ${CMAKE_CURRENT_LIST_DIR}/plugin-js.hpp + ${CMAKE_CURRENT_LIST_DIR}/pollable.hpp ${CMAKE_CURRENT_LIST_DIR}/rule.hpp ${CMAKE_CURRENT_LIST_DIR}/server.hpp - ${CMAKE_CURRENT_LIST_DIR}/service.hpp ${CMAKE_CURRENT_LIST_DIR}/service-command.hpp ${CMAKE_CURRENT_LIST_DIR}/service-interrupt.hpp ${CMAKE_CURRENT_LIST_DIR}/service-module.hpp diff -r 62807bf9a68c -r 4dec71ea1326 lib/irccd/connection.hpp --- a/lib/irccd/connection.hpp Wed Aug 17 20:41:25 2016 +0200 +++ b/lib/irccd/connection.hpp Wed Aug 17 21:27:45 2016 +0200 @@ -30,7 +30,7 @@ #include "net.hpp" #include "signals.hpp" -#include "service.hpp" +#include "pollable.hpp" namespace irccd { @@ -65,7 +65,7 @@ * | | * ------------------------------------+ */ -class Connection : public Service { +class Connection : public Pollable { public: /** * \brief The current connection state. diff -r 62807bf9a68c -r 4dec71ea1326 lib/irccd/irccd.hpp --- a/lib/irccd/irccd.hpp Wed Aug 17 20:41:25 2016 +0200 +++ b/lib/irccd/irccd.hpp Wed Aug 17 21:27:45 2016 +0200 @@ -41,9 +41,9 @@ class InterruptService; class ModuleService; class PluginService; +class Pollable; class RuleService; class ServerService; -class Service; class TransportService; /** @@ -65,7 +65,7 @@ std::shared_ptr m_ruleService; std::shared_ptr m_moduleService; std::shared_ptr m_pluginService; - std::vector> m_services; + std::vector> m_services; // Not copyable and not movable because services has references to irccd. Irccd(const Irccd &) = delete; @@ -85,7 +85,7 @@ * * \param service the service */ - inline void addService(std::shared_ptr service) + inline void addService(std::shared_ptr service) { m_services.push_back(std::move(service)); } diff -r 62807bf9a68c -r 4dec71ea1326 lib/irccd/pollable.hpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/irccd/pollable.hpp Wed Aug 17 21:27:45 2016 +0200 @@ -0,0 +1,115 @@ +/* + * pollable.hpp -- pollable object + * + * Copyright (c) 2013-2016 David Demelier + * + * 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_POLLABLE_HPP +#define IRCCD_POLLABLE_HPP + +/** + * \file service.hpp + * \brief Pollable object. + */ + +#include "net.hpp" +#include "util.hpp" + +namespace irccd { + +/** + * \brief Pollable object. + * + * This class can be used to prepare an object into a select(2) system call. + * + * The primary use case of these objects is to be polled in the main loop while + * being generic. + * + * To use the pollable objects: + * + * 1. Create two fd_set, one for input and one for output. Don't forget to + * initialize them using FD_ZERO. + * + * 2. For all of your pollable objects, call the prepare function and pass the + * input and output sets. The max handle is usually the pollable socket. + * + * 3. Do your select(2) call using the input, output and socket handle and your + * desired timeout. + * + * 4. For all of your pollable objects, call the sync function and pass the + * input and output sets. + * + * Pollable objects are usually implemented using asynchronous signals defined + * in signals.hpp file. + */ +class Pollable { +public: + /** + * Default constructor. + */ + Pollable() noexcept = default; + + /** + * Virtual destructor defaulted. + */ + virtual ~Pollable() noexcept = default; + + /** + * Prepare the input and output set. + * + * \param in the input set + * \param out the output set + * \param max the handle to update + */ + virtual void prepare(fd_set &in, fd_set &out, net::Handle &max) + { + util::unused(in, out, max); + } + + /** + * Synchronize with result sets. + * + * \param in the input set + * \param out the output set + */ + virtual void sync(fd_set &in, fd_set &out) + { + util::unused(in, out); + } + + /** + * Convenient function for polling events with a timeout. + * + * \param timeout the timeout in milliseconds + */ + virtual void poll(int timeout = -1) + { + fd_set in, out; + timeval tv = {0, timeout * 1000}; + + FD_ZERO(&in); + FD_ZERO(&out); + + net::Handle max = 0; + + prepare(in, out, max); + select(max + 1, &in, &out, nullptr, timeout < 0 ? nullptr : &tv); + sync(in, out); + } +}; + +} // !irccd + +#endif // !IRCCD_POLLABLE_HPP diff -r 62807bf9a68c -r 4dec71ea1326 lib/irccd/service-interrupt.hpp --- a/lib/irccd/service-interrupt.hpp Wed Aug 17 20:41:25 2016 +0200 +++ b/lib/irccd/service-interrupt.hpp Wed Aug 17 21:27:45 2016 +0200 @@ -24,7 +24,7 @@ * \brief Interrupt irccd event loop. */ -#include "service.hpp" +#include "pollable.hpp" namespace irccd { @@ -32,7 +32,7 @@ * \brief Interrupt irccd event loop. * \ingroup services */ -class InterruptService : public Service { +class InterruptService : public Pollable { private: net::TcpSocket m_in; net::TcpSocket m_out; diff -r 62807bf9a68c -r 4dec71ea1326 lib/irccd/service-server.hpp --- a/lib/irccd/service-server.hpp Wed Aug 17 20:41:25 2016 +0200 +++ b/lib/irccd/service-server.hpp Wed Aug 17 21:27:45 2016 +0200 @@ -29,7 +29,7 @@ #include #include "server.hpp" -#include "service.hpp" +#include "pollable.hpp" namespace irccd { @@ -39,7 +39,7 @@ * \brief Manage IRC servers. * \ingroup services */ -class ServerService : public Service { +class ServerService : public Pollable { private: Irccd &m_irccd; std::vector> m_servers; diff -r 62807bf9a68c -r 4dec71ea1326 lib/irccd/service-transport.hpp --- a/lib/irccd/service-transport.hpp Wed Aug 17 20:41:25 2016 +0200 +++ b/lib/irccd/service-transport.hpp Wed Aug 17 21:27:45 2016 +0200 @@ -26,7 +26,7 @@ #include -#include "service.hpp" +#include "pollable.hpp" namespace irccd { @@ -37,7 +37,7 @@ * \brief manage transport servers and clients. * \ingroup services */ -class TransportService : public Service { +class TransportService : public Pollable { private: Irccd &m_irccd; diff -r 62807bf9a68c -r 4dec71ea1326 lib/irccd/service.hpp --- a/lib/irccd/service.hpp Wed Aug 17 20:41:25 2016 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,105 +0,0 @@ -/* - * service.hpp -- selectable service - * - * Copyright (c) 2013-2016 David Demelier - * - * 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_SERVICE_HPP -#define IRCCD_SERVICE_HPP - -/** - * \file service.hpp - * \brief Selectable service. - */ - -/** - * \defgroup services Irccd services - * \brief Irccd services. - */ - -#include "net.hpp" -#include "sysconfig.hpp" -#include "util.hpp" - -namespace irccd { - -/** - * \brief Selectable service. - * - * This class can be used to prepare a set of sockets that will be selected by Irccd class. - * - * First, the function prepare is called, the user is responsible to fill the input and output set and adjust max accordingly. - * - * Second, after select has been called, sync is called. The user is responsible of checking which sockets are ready for input or output. - */ -class Service { -public: - /** - * Default constructor. - */ - Service() noexcept = default; - - /** - * Virtual destructor defaulted. - */ - virtual ~Service() noexcept = default; - - /** - * Prepare the input and output set. - * - * \param in the input set - * \param out the output set - * \param max the handle to update - */ - virtual void prepare(fd_set &in, fd_set &out, net::Handle &max) - { - util::unused(in, out, max); - } - - /** - * Synchronize with result sets. - * - * \param in the input set - * \param out the output set - */ - virtual void sync(fd_set &in, fd_set &out) - { - util::unused(in, out); - } - - /** - * Convenient function for polling events with a timeout. - * - * \param timeout the timeout in milliseconds - */ - virtual void poll(int timeout = -1) - { - fd_set in, out; - timeval tv = {0, timeout * 1000}; - - FD_ZERO(&in); - FD_ZERO(&out); - - net::Handle max = 0; - - prepare(in, out, max); - select(max + 1, &in, &out, nullptr, timeout < 0 ? nullptr : &tv); - sync(in, out); - } -}; - -} // !irccd - -#endif // !IRCCD_SERVICE_HPP