cluck 1.0.1
The cluster lock service.
messenger.cpp
Go to the documentation of this file.
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// communicator
38//
39#include <communicator/names.h>
40
41
42// last include
43//
44#include <snapdev/poison.h>
45
46
47
48namespace cluck_daemon
49{
50
51
76messenger::messenger(cluckd * c, advgetopt::getopt & opts)
77 : fluid_settings_connection(opts, "cluckd")
78 , f_cluckd(c)
79{
80 set_name("cluck_messenger");
81 get_dispatcher()->add_matches({
82 // eventdispatcher commands
83 //
84 ed::define_match(
85 ed::Expression(ed::g_name_ed_cmd_absolutely)
86 , ed::Callback(std::bind(&cluckd::msg_absolutely, c, std::placeholders::_1))
87 ),
88
89 // communicator commands
90 //
91 ed::define_match(
92 ed::Expression(::communicator::g_name_communicator_cmd_clock_stable)
93 , ed::Callback(std::bind(&cluckd::msg_clock_stable, c, std::placeholders::_1))
94 ),
95 ed::define_match(
96 ed::Expression(::communicator::g_name_communicator_cmd_cluster_down)
97 , ed::Callback(std::bind(&cluckd::msg_cluster_down, c, std::placeholders::_1))
98 ),
99 ed::define_match(
100 ed::Expression(::communicator::g_name_communicator_cmd_cluster_up)
101 , ed::Callback(std::bind(&cluckd::msg_cluster_up, c, std::placeholders::_1))
102 ),
103 ed::define_match(
104 ed::Expression(::communicator::g_name_communicator_cmd_disconnected)
105 , ed::Callback(std::bind(&cluckd::msg_server_gone, c, std::placeholders::_1))
106 ),
107 ed::define_match(
108 ed::Expression(::communicator::g_name_communicator_cmd_hangup)
109 , ed::Callback(std::bind(&cluckd::msg_server_gone, c, std::placeholders::_1))
110 ),
111 ed::define_match(
112 ed::Expression(::communicator::g_name_communicator_cmd_status)
113 , ed::Callback(std::bind(&cluckd::msg_status, c, std::placeholders::_1))
114 , ed::MatchFunc(&ed::one_to_one_callback_match)
115 , ed::Priority(ed::dispatcher_match::DISPATCHER_MATCH_CALLBACK_PRIORITY)
116 ),
117
118 // cluck commands
119 //
120 ed::define_match(
121 ed::Expression(cluck::g_name_cluck_cmd_activate_lock)
122 , ed::Callback(std::bind(&cluckd::msg_activate_lock, c, std::placeholders::_1))
123 ),
124 ed::define_match(
125 ed::Expression(cluck::g_name_cluck_cmd_add_ticket)
126 , ed::Callback(std::bind(&cluckd::msg_add_ticket, c, std::placeholders::_1))
127 ),
128 ed::define_match(
129 ed::Expression(cluck::g_name_cluck_cmd_drop_ticket)
130 , ed::Callback(std::bind(&cluckd::msg_drop_ticket, c, std::placeholders::_1))
131 ),
132 ed::define_match(
133 ed::Expression(cluck::g_name_cluck_cmd_get_max_ticket)
134 , ed::Callback(std::bind(&cluckd::msg_get_max_ticket, c, std::placeholders::_1))
135 ),
136 ed::define_match(
137 ed::Expression(cluck::g_name_cluck_cmd_info)
138 , ed::Callback(std::bind(&cluckd::msg_info, c, std::placeholders::_1))
139 ),
140 ed::define_match(
141 ed::Expression(cluck::g_name_cluck_cmd_list_tickets)
142 , ed::Callback(std::bind(&cluckd::msg_list_tickets, c, std::placeholders::_1))
143 ),
144 ed::define_match(
145 ed::Expression(cluck::g_name_cluck_cmd_lock)
146 , ed::Callback(std::bind(&cluckd::msg_lock, c, std::placeholders::_1))
147 ),
148 ed::define_match(
149 ed::Expression(cluck::g_name_cluck_cmd_lock_activated)
150 , ed::Callback(std::bind(&cluckd::msg_lock_activated, c, std::placeholders::_1))
151 ),
152 ed::define_match(
153 ed::Expression(cluck::g_name_cluck_cmd_lock_entered)
154 , ed::Callback(std::bind(&cluckd::msg_lock_entered, c, std::placeholders::_1))
155 ),
156 ed::define_match(
157 ed::Expression(cluck::g_name_cluck_cmd_lock_entering)
158 , ed::Callback(std::bind(&cluckd::msg_lock_entering, c, std::placeholders::_1))
159 ),
160 ed::define_match(
161 ed::Expression(cluck::g_name_cluck_cmd_lock_exiting)
162 , ed::Callback(std::bind(&cluckd::msg_lock_exiting, c, std::placeholders::_1))
163 ),
164 ed::define_match(
165 ed::Expression(cluck::g_name_cluck_cmd_lock_failed)
166 , ed::Callback(std::bind(&cluckd::msg_lock_failed, c, std::placeholders::_1))
167 ),
168 ed::define_match(
169 ed::Expression(cluck::g_name_cluck_cmd_lock_leaders)
170 , ed::Callback(std::bind(&cluckd::msg_lock_leaders, c, std::placeholders::_1))
171 ),
172 ed::define_match(
173 ed::Expression(cluck::g_name_cluck_cmd_lock_started)
174 , ed::Callback(std::bind(&cluckd::msg_lock_started, c, std::placeholders::_1))
175 ),
176 ed::define_match(
177 ed::Expression(cluck::g_name_cluck_cmd_lock_status)
178 , ed::Callback(std::bind(&cluckd::msg_lock_status, c, std::placeholders::_1))
179 ),
180 ed::define_match(
181 ed::Expression(cluck::g_name_cluck_cmd_lock_tickets)
182 , ed::Callback(std::bind(&cluckd::msg_lock_tickets, c, std::placeholders::_1))
183 ),
184 ed::define_match(
185 ed::Expression(cluck::g_name_cluck_cmd_max_ticket)
186 , ed::Callback(std::bind(&cluckd::msg_max_ticket, c, std::placeholders::_1))
187 ),
188 ed::define_match(
189 ed::Expression(cluck::g_name_cluck_cmd_ticket_added)
190 , ed::Callback(std::bind(&cluckd::msg_ticket_added, c, std::placeholders::_1))
191 ),
192 ed::define_match(
193 ed::Expression(cluck::g_name_cluck_cmd_ticket_ready)
194 , ed::Callback(std::bind(&cluckd::msg_ticket_ready, c, std::placeholders::_1))
195 ),
196 ed::define_match(
197 ed::Expression(cluck::g_name_cluck_cmd_unlock)
198 , ed::Callback(std::bind(&cluckd::msg_unlock, c, std::placeholders::_1))
199 ),
200 });
201}
202
203
207
208
215{
216 process_fluid_settings_options();
217 automatic_watch_initialization();
218}
219
220
235void messenger::ready(ed::message & msg)
236{
237 fluid_settings_connection::ready(msg);
238 f_cluckd->set_my_ip_address(get_my_address());
239
240 // request the status of the system clock
241 //
242 ed::message clock_status;
243 clock_status.reply_to(msg);
244 clock_status.set_command(::communicator::g_name_communicator_cmd_clock_status);
245 clock_status.add_parameter(
246 ::communicator::g_name_communicator_param_cache
247 , ::communicator::g_name_communicator_value_no);
248 send_message(clock_status);
249}
250
251
260void messenger::stop(bool quitting)
261{
262 f_cluckd->stop(quitting);
263}
264
265
279 fluid_settings::fluid_settings_status_t status
280 , std::string const & name
281 , std::string const & value)
282{
283 fluid_settings_connection::fluid_settings_changed(status, name, value);
284
285 if(status == fluid_settings::fluid_settings_status_t::FLUID_SETTINGS_STATUS_READY)
286 {
287 // now we're ready to start with cluckd
288 //
289 ed::message clusterstatus_message;
290 clusterstatus_message.set_command(::communicator::g_name_communicator_cmd_cluster_status);
291 clusterstatus_message.set_service(::communicator::g_name_communicator_service_communicatord);
292 send_message(clusterstatus_message);
293 }
294}
295
296
297
298} // namespace cluck_daemon
299// vim: ts=4 sw=4 et
Class handling intercomputer locking.
Definition cluckd.h:48
void msg_get_max_ticket(ed::message &msg)
Search for the largest ticket.
Definition cluckd.cpp:2671
void msg_lock_status(ed::message &msg)
A service asked about the lock status.
Definition cluckd.cpp:3801
void msg_lock_entered(ed::message &msg)
Tell the specified ticket LOCK_ENTERED was received.
Definition cluckd.cpp:3135
void msg_max_ticket(ed::message &msg)
Got the largest ticket from another leader.
Definition cluckd.cpp:3978
void msg_add_ticket(ed::message &msg)
Add a ticket from another cluckd.
Definition cluckd.cpp:2254
void msg_ticket_ready(ed::message &msg)
Let other leaders know that the ticket is ready.
Definition cluckd.cpp:4199
void msg_cluster_down(ed::message &msg)
The communicatord lost too many connections.
Definition cluckd.cpp:2492
void msg_absolutely(ed::message &msg)
Lock the resource after confirmation that client is alive.
Definition cluckd.cpp:2109
void msg_activate_lock(ed::message &msg)
Acknowledge the ACTIVATE_LOCK with what we think is our first lock.
Definition cluckd.cpp:2193
void set_my_ip_address(addr::addr const &a)
Definition cluckd.cpp:407
void msg_unlock(ed::message &msg)
Unlock the resource.
Definition cluckd.cpp:4231
void msg_info(ed::message &msg)
Return a JSON with the state of this cluckd object.
Definition cluckd.cpp:790
void msg_lock_started(ed::message &msg)
Called whenever a cluck computer is acknowledging itself.
Definition cluckd.cpp:3655
void stop(bool quitting)
Called whenever we receive the STOP command or equivalent.
Definition cluckd.cpp:1284
void msg_lock_leaders(ed::message &msg)
The list of leaders.
Definition cluckd.cpp:3584
void msg_lock_activated(ed::message &msg)
Acknowledgement of the lock to activate.
Definition cluckd.cpp:3098
void msg_ticket_added(ed::message &msg)
Acknowledgement that the ticket was properly added.
Definition cluckd.cpp:4134
void msg_lock_entering(ed::message &msg)
Create an entering ticket.
Definition cluckd.cpp:3176
void msg_list_tickets(ed::message &msg)
Reply to the LIST_TICKETS message with the TICKET_LIST.
Definition cluckd.cpp:2707
void msg_lock_exiting(ed::message &msg)
Exit a ticket.
Definition cluckd.cpp:3353
void msg_server_gone(ed::message &msg)
Called whenever a remote connection is disconnected.
Definition cluckd.cpp:4021
void msg_lock(ed::message &msg)
Lock the named resource.
Definition cluckd.cpp:2753
void msg_clock_stable(ed::message &msg)
Message telling us whether the clock is stable.
Definition cluckd.cpp:2472
void msg_cluster_up(ed::message &msg)
Cluster is ready, send the LOCK_STARTED message.
Definition cluckd.cpp:2521
void msg_drop_ticket(ed::message &msg)
One of the cluckd processes asked for a ticket to be dropped.
Definition cluckd.cpp:2581
void msg_lock_failed(ed::message &msg)
Acknowledge a lock failure.
Definition cluckd.cpp:3468
void msg_lock_tickets(ed::message &msg)
Another cluckd is sending us its list of tickets.
Definition cluckd.cpp:3831
void msg_status(ed::message &msg)
With the STATUS message we know of new communicatord services.
Definition cluckd.cpp:4097
messenger(cluckd *c, advgetopt::getopt &opts)
The messenger initialization.
Definition messenger.cpp:76
virtual void ready(ed::message &msg)
Messenger received the READY message.
void finish_parsing()
Finish handling command line options.
virtual ~messenger() override
virtual void stop(bool quitting)
Let the server know STOP or QUITTING was sent to us.
virtual void fluid_settings_changed(fluid_settings::fluid_settings_status_t status, std::string const &name, std::string const &value) override
Send the CLUSTER_STATUS to communicatord once ready.
Daemon handling inter-computer locking.

This document is part of the Snap! Websites Project.

Copyright by Made to Order Software Corp.