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_log(message & msg);
49 : virtual void msg_quitting(message & msg);
50 : virtual void msg_ready(message & msg);
51 : virtual void msg_restart(message & msg);
52 : virtual void msg_stop(message & msg);
53 : virtual void msg_log_unknown(message & msg);
54 : virtual void msg_reply_with_unknown(message & msg);
55 :
56 : virtual void help(string_list_t & commands);
57 : virtual void ready(message & msg);
58 : virtual void restart(message & msg);
59 : virtual void stop(bool quitting);
60 :
61 : bool get_last_send_status() const;
62 :
63 : /** \brief Broadcast a message to a set of connections.
64 : *
65 : * This function sends one message to all the connections found in a
66 : * container set. The type of the container is not specified. It can
67 : * be any container which works with `for(auto ...)`.
68 : *
69 : * Note that this function should only be used with connections that
70 : * do not automatically allow for broadcasting such as UDP. This is
71 : * useful to send messages to a plethora of connections such as TCP
72 : * or Unix sockets.
73 : *
74 : * \tparam C A connection container.
75 : *
76 : * \param[in] container A set of connections (vector, map, set, etc.)
77 : * \param[in] msg The message to broadcast.
78 : * \param[in] cache Whether to cache the message if the connection is not
79 : * currently opened.
80 : *
81 : * \return true if the send_message() succeeded on all connections, false
82 : * otherwise. If false was returned, you can use the get_last_send_status()
83 : * function to discover which connections failed.
84 : */
85 : template<class C>
86 : bool broadcast_message(C & container, message const & msg, bool cache = false)
87 : {
88 : bool result(true);
89 : for(auto c : container)
90 : {
91 : f_last_send_status = c->send_message(msg, cache);
92 : if(!f_last_send_status)
93 : {
94 : result = false;
95 : }
96 : }
97 : return result;
98 : }
99 :
100 : private:
101 : bool f_last_send_status = true;
102 : };
103 :
104 :
105 :
106 : } // namespace ed
107 : // vim: ts=4 sw=4 et
|