LCOV - code coverage report
Current view: top level - daemon - messenger.cpp (source / functions) Coverage Total Hit
Test: coverage.info Lines: 100.0 % 65 65
Test Date: 2025-08-17 08:58:50 Functions: 100.0 % 6 6
Legend: Lines: hit not hit

            Line data    Source code
       1              : // Copyright (c) 2016-2025  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          105 :     , f_dispatcher(std::make_shared<ed::dispatcher>(this))
      80              : {
      81          105 :     set_name("messenger");
      82           35 :     set_dispatcher(f_dispatcher);
      83           35 :     add_fluid_settings_commands();
      84         1085 :     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_clock_stable)
      96           70 :             , ed::Callback(std::bind(&cluckd::msg_clock_stable, c, std::placeholders::_1))
      97              :         ),
      98              :         ed::define_match(
      99              :               ed::Expression(communicatord::g_name_communicatord_cmd_cluster_down)
     100           70 :             , ed::Callback(std::bind(&cluckd::msg_cluster_down, c, std::placeholders::_1))
     101              :         ),
     102              :         ed::define_match(
     103              :               ed::Expression(communicatord::g_name_communicatord_cmd_cluster_up)
     104           70 :             , ed::Callback(std::bind(&cluckd::msg_cluster_up, c, std::placeholders::_1))
     105              :         ),
     106              :         ed::define_match(
     107              :               ed::Expression(communicatord::g_name_communicatord_cmd_disconnected)
     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_hangup)
     112           70 :             , ed::Callback(std::bind(&cluckd::msg_server_gone, c, std::placeholders::_1))
     113              :         ),
     114              :         ed::define_match(
     115              :               ed::Expression(communicatord::g_name_communicatord_cmd_status)
     116           70 :             , ed::Callback(std::bind(&cluckd::msg_status, c, std::placeholders::_1))
     117              :             , ed::MatchFunc(&ed::one_to_one_callback_match)
     118              :             , ed::Priority(ed::dispatcher_match::DISPATCHER_MATCH_CALLBACK_PRIORITY)
     119              :         ),
     120              : 
     121              :         // cluck commands
     122              :         //
     123              :         ed::define_match(
     124              :               ed::Expression(cluck::g_name_cluck_cmd_activate_lock)
     125           70 :             , ed::Callback(std::bind(&cluckd::msg_activate_lock, c, std::placeholders::_1))
     126              :         ),
     127              :         ed::define_match(
     128              :               ed::Expression(cluck::g_name_cluck_cmd_add_ticket)
     129           70 :             , ed::Callback(std::bind(&cluckd::msg_add_ticket, c, std::placeholders::_1))
     130              :         ),
     131              :         ed::define_match(
     132              :               ed::Expression(cluck::g_name_cluck_cmd_drop_ticket)
     133           70 :             , ed::Callback(std::bind(&cluckd::msg_drop_ticket, c, std::placeholders::_1))
     134              :         ),
     135              :         ed::define_match(
     136              :               ed::Expression(cluck::g_name_cluck_cmd_get_max_ticket)
     137           70 :             , ed::Callback(std::bind(&cluckd::msg_get_max_ticket, c, std::placeholders::_1))
     138              :         ),
     139              :         ed::define_match(
     140              :               ed::Expression(cluck::g_name_cluck_cmd_info)
     141           70 :             , ed::Callback(std::bind(&cluckd::msg_info, c, std::placeholders::_1))
     142              :         ),
     143              :         ed::define_match(
     144              :               ed::Expression(cluck::g_name_cluck_cmd_list_tickets)
     145           70 :             , ed::Callback(std::bind(&cluckd::msg_list_tickets, c, std::placeholders::_1))
     146              :         ),
     147              :         ed::define_match(
     148              :               ed::Expression(cluck::g_name_cluck_cmd_lock)
     149           70 :             , ed::Callback(std::bind(&cluckd::msg_lock, c, std::placeholders::_1))
     150              :         ),
     151              :         ed::define_match(
     152              :               ed::Expression(cluck::g_name_cluck_cmd_lock_activated)
     153           70 :             , ed::Callback(std::bind(&cluckd::msg_lock_activated, c, std::placeholders::_1))
     154              :         ),
     155              :         ed::define_match(
     156              :               ed::Expression(cluck::g_name_cluck_cmd_lock_entered)
     157           70 :             , ed::Callback(std::bind(&cluckd::msg_lock_entered, c, std::placeholders::_1))
     158              :         ),
     159              :         ed::define_match(
     160              :               ed::Expression(cluck::g_name_cluck_cmd_lock_entering)
     161           70 :             , ed::Callback(std::bind(&cluckd::msg_lock_entering, c, std::placeholders::_1))
     162              :         ),
     163              :         ed::define_match(
     164              :               ed::Expression(cluck::g_name_cluck_cmd_lock_exiting)
     165           70 :             , ed::Callback(std::bind(&cluckd::msg_lock_exiting, c, std::placeholders::_1))
     166              :         ),
     167              :         ed::define_match(
     168              :               ed::Expression(cluck::g_name_cluck_cmd_lock_failed)
     169           70 :             , ed::Callback(std::bind(&cluckd::msg_lock_failed, c, std::placeholders::_1))
     170              :         ),
     171              :         ed::define_match(
     172              :               ed::Expression(cluck::g_name_cluck_cmd_lock_leaders)
     173           70 :             , ed::Callback(std::bind(&cluckd::msg_lock_leaders, c, std::placeholders::_1))
     174              :         ),
     175              :         ed::define_match(
     176              :               ed::Expression(cluck::g_name_cluck_cmd_lock_started)
     177           70 :             , ed::Callback(std::bind(&cluckd::msg_lock_started, c, std::placeholders::_1))
     178              :         ),
     179              :         ed::define_match(
     180              :               ed::Expression(cluck::g_name_cluck_cmd_lock_status)
     181           70 :             , ed::Callback(std::bind(&cluckd::msg_lock_status, c, std::placeholders::_1))
     182              :         ),
     183              :         ed::define_match(
     184              :               ed::Expression(cluck::g_name_cluck_cmd_lock_tickets)
     185           70 :             , ed::Callback(std::bind(&cluckd::msg_lock_tickets, c, std::placeholders::_1))
     186              :         ),
     187              :         ed::define_match(
     188              :               ed::Expression(cluck::g_name_cluck_cmd_max_ticket)
     189           70 :             , ed::Callback(std::bind(&cluckd::msg_max_ticket, c, std::placeholders::_1))
     190              :         ),
     191              :         ed::define_match(
     192              :               ed::Expression(cluck::g_name_cluck_cmd_ticket_added)
     193           70 :             , ed::Callback(std::bind(&cluckd::msg_ticket_added, c, std::placeholders::_1))
     194              :         ),
     195              :         ed::define_match(
     196              :               ed::Expression(cluck::g_name_cluck_cmd_ticket_ready)
     197           70 :             , ed::Callback(std::bind(&cluckd::msg_ticket_ready, c, std::placeholders::_1))
     198              :         ),
     199              :         ed::define_match(
     200              :               ed::Expression(cluck::g_name_cluck_cmd_unlock)
     201           70 :             , ed::Callback(std::bind(&cluckd::msg_unlock, c, std::placeholders::_1))
     202              :         ),
     203              :     });
     204           35 :     f_dispatcher->add_communicator_commands();
     205              : 
     206              :     // further dispatcher initialization
     207              :     //
     208              : #ifdef _DEBUG
     209           35 :     f_dispatcher->set_trace();
     210           35 :     f_dispatcher->set_show_matches();
     211              : #endif
     212         1960 : }
     213              : 
     214              : 
     215           35 : messenger::~messenger()
     216              : {
     217           35 : }
     218              : 
     219              : 
     220              : /** \brief Finish handling command line options.
     221              :  *
     222              :  * This function makes sure the fluid settings and communicator daemon
     223              :  * have a chance to check the command line options and act on it.
     224              :  */
     225           25 : void messenger::finish_parsing()
     226              : {
     227           25 :     process_fluid_settings_options();
     228           25 :     automatic_watch_initialization();
     229           25 : }
     230              : 
     231              : 
     232              : /** \brief Messenger received the READY message.
     233              :  *
     234              :  * Whenever we receive the READY message, we also receive our IP address
     235              :  * as the "my_address" parameter. This gets copied in the cluckd object.
     236              :  *
     237              :  * The function tells the fluid settings connection that the daemon
     238              :  * is ready.
     239              :  *
     240              :  * The cluck daemon uses the wall clock to synchronize the locks
     241              :  * between servers so it requests the clock status from the communicator
     242              :  * daemon. Once known to be stable, it's ready to start accepting locks.
     243              :  *
     244              :  * \param[in,out] msg  The READY message.
     245              :  */
     246           25 : void messenger::ready(ed::message & msg)
     247              : {
     248           25 :     fluid_settings_connection::ready(msg);
     249           25 :     f_cluckd->set_my_ip_address(get_my_address());
     250              : 
     251              :     // request the status of the system clock
     252              :     //
     253           25 :     ed::message clock_status;
     254           25 :     clock_status.reply_to(msg);
     255           75 :     clock_status.set_command(communicatord::g_name_communicatord_cmd_clock_status);
     256          125 :     clock_status.add_parameter(
     257              :               communicatord::g_name_communicatord_param_cache
     258              :             , communicatord::g_name_communicatord_value_no);
     259           25 :     send_message(clock_status);
     260           50 : }
     261              : 
     262              : 
     263              : /** \brief Let the server know STOP or QUITTING was sent to us.
     264              :  *
     265              :  * This STOP and QUITTING messages are currently managed through this
     266              :  * overridden virtual function.
     267              :  *
     268              :  * \param[in] quitting  Whether STOP (false) or QUITTING (true) was
     269              :  * received.
     270              :  */
     271           23 : void messenger::stop(bool quitting)
     272              : {
     273           23 :     f_cluckd->stop(quitting);
     274           23 : }
     275              : 
     276              : 
     277              : /** \brief Send the CLUSTER_STATUS to communicatord once ready.
     278              :  *
     279              :  * This function builds a message and sends it to communicatord.
     280              :  *
     281              :  * The CLUSTER_UP and CLUSTER_DOWN messages are sent only when that specific
     282              :  * event happen and until then we do not know what the state really is
     283              :  * (although we assume the worst and use CLUSTER_DOWN until we get a reply).
     284              :  *
     285              :  * \param[in] status  The status of the fluid settings object.
     286              :  * \param[in] name  The name of the changing parameter.
     287              :  * \param[in] value  The value of the parameter.
     288              :  */
     289           71 : void messenger::fluid_settings_changed(
     290              :       fluid_settings::fluid_settings_status_t status
     291              :     , std::string const & name
     292              :     , std::string const & value)
     293              : {
     294           71 :     snapdev::NOT_USED(name, value);
     295              : 
     296           71 :     if(status == fluid_settings::fluid_settings_status_t::FLUID_SETTINGS_STATUS_READY)
     297              :     {
     298              :         // now we're ready to start with cluckd
     299              :         //
     300           25 :         ed::message clusterstatus_message;
     301           75 :         clusterstatus_message.set_command(communicatord::g_name_communicatord_cmd_cluster_status);
     302           75 :         clusterstatus_message.set_service(communicatord::g_name_communicatord_service_communicatord);
     303           25 :         send_message(clusterstatus_message);
     304           25 :     }
     305           71 : }
     306              : 
     307              : 
     308              : 
     309              : } // namespace cluck_daemon
     310              : // vim: ts=4 sw=4 et
        

Generated by: LCOV version 2.0-1

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