LCOV - code coverage report
Current view: top level - daemon - messenger.cpp (source / functions) Hit Total Coverage
Test: coverage.info Lines: 59 59 100.0 %
Date: 2025-01-27 20:52:47 Functions: 6 7 85.7 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : // Copyright (c) 2016-2024  Made to Order Software Corp.  All Rights Reserved
       2             : //
       3             : // https://snapwebsites.org/project/cluck
       4             : // contact@m2osw.com
       5             : //
       6             : // This program is free software: you can redistribute it and/or modify
       7             : // it under the terms of the GNU General Public License as published by
       8             : // the Free Software Foundation, either version 3 of the License, or
       9             : // (at your option) any later version.
      10             : //
      11             : // This program is distributed in the hope that it will be useful,
      12             : // but WITHOUT ANY WARRANTY; without even the implied warranty of
      13             : // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      14             : // GNU General Public License for more details.
      15             : //
      16             : // You should have received a copy of the GNU General Public License
      17             : // along with this program.  If not, see <https://www.gnu.org/licenses/>.
      18             : 
      19             : 
      20             : // self
      21             : //
      22             : #include    "messenger.h"
      23             : 
      24             : #include    "cluckd.h"
      25             : 
      26             : 
      27             : // cluck
      28             : //
      29             : #include    <cluck/names.h>
      30             : 
      31             : 
      32             : // eventdispatcher
      33             : //
      34             : #include    <eventdispatcher/names.h>
      35             : 
      36             : 
      37             : // communicatord
      38             : //
      39             : #include    <communicatord/names.h>
      40             : 
      41             : 
      42             : // last include
      43             : //
      44             : #include    <snapdev/poison.h>
      45             : 
      46             : 
      47             : 
      48             : namespace cluck_daemon
      49             : {
      50             : 
      51             : 
      52             : /** \class messenger
      53             :  * \brief Handle messages from the communicatord.
      54             :  *
      55             :  * This class is an implementation of the TCP client message connection
      56             :  * so we can handle incoming messages. We actually use the fluid-settings
      57             :  * which itself uses the communicatord connection. All of the basic
      58             :  * communication messages used by the communicatord and fluid settings
      59             :  * are handled automatically.
      60             :  *
      61             :  * This class handles the lock messages.
      62             :  */
      63             : 
      64             : 
      65             : 
      66             : /** \brief The messenger initialization.
      67             :  *
      68             :  * The messenger is the cluck daemon connection to the communicator server.
      69             :  *
      70             :  * It sets up its dispatcher and calls cluckd functions whenever it
      71             :  * receives a message.
      72             :  *
      73             :  * \param[in] c  The cluck object we are listening for.
      74             :  * \param[in] opts  The options received from the command line.
      75             :  */
      76          35 : messenger::messenger(cluckd * c, advgetopt::getopt & opts)
      77             :     : fluid_settings_connection(opts, "cluckd")
      78          35 :     , f_cluckd(c)
      79          35 :     , f_dispatcher(std::make_shared<ed::dispatcher>(this))
      80             : {
      81          35 :     set_name("messenger");
      82          35 :     set_dispatcher(f_dispatcher);
      83          35 :     add_fluid_settings_commands();
      84        2730 :     f_dispatcher->add_matches({
      85             :         // eventdispatcher commands
      86             :         //
      87             :         ed::define_match(
      88             :               ed::Expression(ed::g_name_ed_cmd_absolutely)
      89          70 :             , ed::Callback(std::bind(&cluckd::msg_absolutely, c, std::placeholders::_1))
      90             :         ),
      91             : 
      92             :         // communicatord commands
      93             :         //
      94             :         ed::define_match(
      95             :               ed::Expression(communicatord::g_name_communicatord_cmd_cluster_down)
      96          70 :             , ed::Callback(std::bind(&cluckd::msg_cluster_down, c, std::placeholders::_1))
      97             :         ),
      98             :         ed::define_match(
      99             :               ed::Expression(communicatord::g_name_communicatord_cmd_cluster_up)
     100          70 :             , ed::Callback(std::bind(&cluckd::msg_cluster_up, c, std::placeholders::_1))
     101             :         ),
     102             :         ed::define_match(
     103             :               ed::Expression(communicatord::g_name_communicatord_cmd_disconnected)
     104          70 :             , ed::Callback(std::bind(&cluckd::msg_server_gone, c, std::placeholders::_1))
     105             :         ),
     106             :         ed::define_match(
     107             :               ed::Expression(communicatord::g_name_communicatord_cmd_hangup)
     108          70 :             , ed::Callback(std::bind(&cluckd::msg_server_gone, c, std::placeholders::_1))
     109             :         ),
     110             :         ed::define_match(
     111             :               ed::Expression(communicatord::g_name_communicatord_cmd_status)
     112          70 :             , ed::Callback(std::bind(&cluckd::msg_status, c, std::placeholders::_1))
     113             :             , ed::MatchFunc(&ed::one_to_one_callback_match)
     114             :             , ed::Priority(ed::dispatcher_match::DISPATCHER_MATCH_CALLBACK_PRIORITY)
     115             :         ),
     116             : 
     117             :         // cluck commands
     118             :         //
     119             :         ed::define_match(
     120             :               ed::Expression(cluck::g_name_cluck_cmd_activate_lock)
     121          70 :             , ed::Callback(std::bind(&cluckd::msg_activate_lock, c, std::placeholders::_1))
     122             :         ),
     123             :         ed::define_match(
     124             :               ed::Expression(cluck::g_name_cluck_cmd_add_ticket)
     125          70 :             , ed::Callback(std::bind(&cluckd::msg_add_ticket, c, std::placeholders::_1))
     126             :         ),
     127             :         ed::define_match(
     128             :               ed::Expression(cluck::g_name_cluck_cmd_drop_ticket)
     129          70 :             , ed::Callback(std::bind(&cluckd::msg_drop_ticket, c, std::placeholders::_1))
     130             :         ),
     131             :         ed::define_match(
     132             :               ed::Expression(cluck::g_name_cluck_cmd_get_max_ticket)
     133          70 :             , ed::Callback(std::bind(&cluckd::msg_get_max_ticket, c, std::placeholders::_1))
     134             :         ),
     135             :         ed::define_match(
     136             :               ed::Expression(cluck::g_name_cluck_cmd_info)
     137          70 :             , ed::Callback(std::bind(&cluckd::msg_info, c, std::placeholders::_1))
     138             :         ),
     139             :         ed::define_match(
     140             :               ed::Expression(cluck::g_name_cluck_cmd_list_tickets)
     141          70 :             , ed::Callback(std::bind(&cluckd::msg_list_tickets, c, std::placeholders::_1))
     142             :         ),
     143             :         ed::define_match(
     144             :               ed::Expression(cluck::g_name_cluck_cmd_lock)
     145          70 :             , ed::Callback(std::bind(&cluckd::msg_lock, c, std::placeholders::_1))
     146             :         ),
     147             :         ed::define_match(
     148             :               ed::Expression(cluck::g_name_cluck_cmd_lock_activated)
     149          70 :             , ed::Callback(std::bind(&cluckd::msg_lock_activated, c, std::placeholders::_1))
     150             :         ),
     151             :         ed::define_match(
     152             :               ed::Expression(cluck::g_name_cluck_cmd_lock_entered)
     153          70 :             , ed::Callback(std::bind(&cluckd::msg_lock_entered, c, std::placeholders::_1))
     154             :         ),
     155             :         ed::define_match(
     156             :               ed::Expression(cluck::g_name_cluck_cmd_lock_entering)
     157          70 :             , ed::Callback(std::bind(&cluckd::msg_lock_entering, c, std::placeholders::_1))
     158             :         ),
     159             :         ed::define_match(
     160             :               ed::Expression(cluck::g_name_cluck_cmd_lock_exiting)
     161          70 :             , ed::Callback(std::bind(&cluckd::msg_lock_exiting, c, std::placeholders::_1))
     162             :         ),
     163             :         ed::define_match(
     164             :               ed::Expression(cluck::g_name_cluck_cmd_lock_failed)
     165          70 :             , ed::Callback(std::bind(&cluckd::msg_lock_failed, c, std::placeholders::_1))
     166             :         ),
     167             :         ed::define_match(
     168             :               ed::Expression(cluck::g_name_cluck_cmd_lock_leaders)
     169          70 :             , ed::Callback(std::bind(&cluckd::msg_lock_leaders, c, std::placeholders::_1))
     170             :         ),
     171             :         ed::define_match(
     172             :               ed::Expression(cluck::g_name_cluck_cmd_lock_started)
     173          70 :             , ed::Callback(std::bind(&cluckd::msg_lock_started, c, std::placeholders::_1))
     174             :         ),
     175             :         ed::define_match(
     176             :               ed::Expression(cluck::g_name_cluck_cmd_lock_status)
     177          70 :             , ed::Callback(std::bind(&cluckd::msg_lock_status, c, std::placeholders::_1))
     178             :         ),
     179             :         ed::define_match(
     180             :               ed::Expression(cluck::g_name_cluck_cmd_lock_tickets)
     181          70 :             , ed::Callback(std::bind(&cluckd::msg_lock_tickets, c, std::placeholders::_1))
     182             :         ),
     183             :         ed::define_match(
     184             :               ed::Expression(cluck::g_name_cluck_cmd_max_ticket)
     185          70 :             , ed::Callback(std::bind(&cluckd::msg_max_ticket, c, std::placeholders::_1))
     186             :         ),
     187             :         ed::define_match(
     188             :               ed::Expression(cluck::g_name_cluck_cmd_ticket_added)
     189          70 :             , ed::Callback(std::bind(&cluckd::msg_ticket_added, c, std::placeholders::_1))
     190             :         ),
     191             :         ed::define_match(
     192             :               ed::Expression(cluck::g_name_cluck_cmd_ticket_ready)
     193          70 :             , ed::Callback(std::bind(&cluckd::msg_ticket_ready, c, std::placeholders::_1))
     194             :         ),
     195             :         ed::define_match(
     196             :               ed::Expression(cluck::g_name_cluck_cmd_unlock)
     197          70 :             , ed::Callback(std::bind(&cluckd::msg_unlock, c, std::placeholders::_1))
     198             :         ),
     199             :     });
     200          35 :     f_dispatcher->add_communicator_commands();
     201             : 
     202             :     // further dispatcher initialization
     203             :     //
     204             : #ifdef _DEBUG
     205          35 :     f_dispatcher->set_trace();
     206          35 :     f_dispatcher->set_show_matches();
     207             : #endif
     208          35 : }
     209             : 
     210             : 
     211          35 : messenger::~messenger()
     212             : {
     213          35 : }
     214             : 
     215             : 
     216             : /** \brief Finish handling command line options.
     217             :  *
     218             :  * This function makes sure the fluid settings and communicator daemon
     219             :  * have a chance to check the command line options and act on it.
     220             :  */
     221          25 : void messenger::finish_parsing()
     222             : {
     223          25 :     process_fluid_settings_options();
     224          25 :     automatic_watch_initialization();
     225          25 : }
     226             : 
     227             : 
     228             : /** \brief Messenger received the READY message.
     229             :  *
     230             :  * Whenever we receive the READY message, we also receive our IP address
     231             :  * as the "my_address" parameter. This gets copied in the cluckd object.
     232             :  *
     233             :  * \param[in,out] msg  The READY message.
     234             :  */
     235          25 : void messenger::ready(ed::message & msg)
     236             : {
     237          25 :     fluid_settings_connection::ready(msg);
     238          25 :     f_cluckd->set_my_ip_address(get_my_address());
     239          25 : }
     240             : 
     241             : 
     242             : /** \brief Let the server know STOP or QUITTING was sent to us.
     243             :  *
     244             :  * This STOP and QUITTING messages are currently managed through this
     245             :  * overridden virtual function.
     246             :  *
     247             :  * \param[in] quitting  Whether STOP (false) or QUITTING (true) was
     248             :  * received.
     249             :  */
     250          23 : void messenger::stop(bool quitting)
     251             : {
     252          23 :     f_cluckd->stop(quitting);
     253          23 : }
     254             : 
     255             : 
     256             : /** \brief Send the CLUSTER_STATUS to communicatord once ready.
     257             :  *
     258             :  * This function builds a message and sends it to communicatord.
     259             :  *
     260             :  * The CLUSTER_UP and CLUSTER_DOWN messages are sent only when that specific
     261             :  * event happen and until then we do not know what the state really is
     262             :  * (although we assume the worst and use CLUSTER_DOWN until we get a reply).
     263             :  *
     264             :  * \param[in] status  The status of the fluid settings object.
     265             :  * \param[in] name  The name of the changing parameter.
     266             :  * \param[in] value  The value of the parameter.
     267             :  */
     268          71 : void messenger::fluid_settings_changed(
     269             :       fluid_settings::fluid_settings_status_t status
     270             :     , std::string const & name
     271             :     , std::string const & value)
     272             : {
     273          71 :     snapdev::NOT_USED(name, value);
     274             : 
     275          71 :     if(status == fluid_settings::fluid_settings_status_t::FLUID_SETTINGS_STATUS_READY)
     276             :     {
     277             :         // now we're ready to start with cluckd
     278             :         //
     279          25 :         ed::message clusterstatus_message;
     280          25 :         clusterstatus_message.set_command(cluck::g_name_cluck_cmd_cluster_status);
     281          25 :         clusterstatus_message.set_service(communicatord::g_name_communicatord_service_communicatord);
     282          25 :         send_message(clusterstatus_message);
     283          25 :     }
     284          71 : }
     285             : 
     286             : 
     287             : 
     288             : } // namespace cluck_daemon
     289             : // vim: ts=4 sw=4 et

Generated by: LCOV version 1.14

Snap C++ | List of projects | List of versions