changeset 247:4dec71ea1326

Irccd: rename Service to Pollable
author David Demelier <markand@malikania.fr>
date Wed, 17 Aug 2016 21:27:45 +0200
parents 62807bf9a68c
children b8d09c237835
files lib/irccd/CMakeSources.cmake lib/irccd/connection.hpp lib/irccd/irccd.hpp lib/irccd/pollable.hpp lib/irccd/service-interrupt.hpp lib/irccd/service-server.hpp lib/irccd/service-transport.hpp lib/irccd/service.hpp
diffstat 8 files changed, 127 insertions(+), 117 deletions(-) [+]
line wrap: on
line diff
--- 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
--- 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.
--- 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<RuleService> m_ruleService;
     std::shared_ptr<ModuleService> m_moduleService;
     std::shared_ptr<PluginService> m_pluginService;
-    std::vector<std::shared_ptr<Service>> m_services;
+    std::vector<std::shared_ptr<Pollable>> 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> service)
+    inline void addService(std::shared_ptr<Pollable> service)
     {
         m_services.push_back(std::move(service));
     }
--- /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 <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_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
--- 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;
--- 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 <string>
 
 #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<std::shared_ptr<Server>> m_servers;
--- 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 <json.hpp>
 
-#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;
 
--- 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 <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_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