Line data Source code
1 : // Copyright (c) 2012-2021 Made to Order Software Corp. All Rights Reserved
2 : //
3 : // https://snapwebsites.org/project/eventdispatcher
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 2 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 along
17 : // with this program; if not, write to the Free Software Foundation, Inc.,
18 : // 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 : #pragma once
20 :
21 : /** \file
22 : * \brief Event dispatch class.
23 : *
24 : * Class used to handle events.
25 : */
26 :
27 : // self
28 : //
29 : #include "eventdispatcher/message.h"
30 :
31 :
32 :
33 : namespace ed
34 : {
35 :
36 :
37 :
38 3 : class connection_with_send_message
39 : {
40 : public:
41 : virtual ~connection_with_send_message();
42 :
43 : // new callbacks
44 : virtual bool send_message(message const & msg, bool cache = false) = 0;
45 :
46 : virtual void msg_help(message & msg);
47 : virtual void msg_alive(message & msg);
48 : virtual void msg_leak(message & msg);
49 : virtual void msg_log(message & msg);
50 : virtual void msg_quitting(message & msg);
51 : virtual void msg_ready(message & msg);
52 : virtual void msg_restart(message & msg);
53 : virtual void msg_stop(message & msg);
54 : virtual void msg_log_unknown(message & msg);
55 : virtual void msg_reply_with_unknown(message & msg);
56 :
57 : virtual void help(string_list_t & commands);
58 : virtual void ready(message & msg);
59 : virtual void restart(message & msg);
60 : virtual void stop(bool quitting);
61 :
62 : bool get_last_send_status() const;
63 :
64 : /** \brief Broadcast a message to a set of connections.
65 : *
66 : * This function sends one message to all the connections found in a
67 : * container set. The type of the container is not specified. It can
68 : * be any container which works with `for(auto ...)`.
69 : *
70 : * Note that this function should only be used with connections that
71 : * do not automatically allow for broadcasting such as UDP. This is
72 : * useful to send messages to a plethora of connections such as TCP
73 : * or Unix sockets.
74 : *
75 : * \tparam C A connection container.
76 : *
77 : * \param[in] container A set of connections (vector, map, set, etc.)
78 : * \param[in] msg The message to broadcast.
79 : * \param[in] cache Whether to cache the message if the connection is not
80 : * currently opened.
81 : *
82 : * \return true if the send_message() succeeded on all connections, false
83 : * otherwise. If false was returned, you can use the get_last_send_status()
84 : * function to discover which connections failed.
85 : */
86 : template<class C>
87 : bool broadcast_message(C & container, message const & msg, bool cache = false)
88 : {
89 : bool result(true);
90 : for(auto c : container)
91 : {
92 : f_last_send_status = c->send_message(msg, cache);
93 : if(!f_last_send_status)
94 : {
95 : result = false;
96 : }
97 : }
98 : return result;
99 : }
100 :
101 : private:
102 : bool f_last_send_status = true;
103 : };
104 :
105 :
106 :
107 : } // namespace ed
108 : // vim: ts=4 sw=4 et
|