Line data Source code
1 : // Copyright (c) 2012-2022 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
17 : // along with this program; if not, write to the Free Software
18 : // Foundation, Inc., 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/utils.h"
30 :
31 :
32 : // libaddr
33 : //
34 : #include "libaddr/addr.h"
35 :
36 :
37 :
38 : namespace ed
39 : {
40 :
41 :
42 :
43 : typedef int message_version_t;
44 :
45 : // this version defines the protocol version, it should really rarely
46 : // change if ever
47 : //
48 : constexpr message_version_t MESSAGE_VERSION = 1;
49 :
50 : constexpr char const MESSAGE_VERSION_NAME[] = "version";
51 :
52 :
53 :
54 42 : class message
55 : {
56 : public:
57 : typedef std::shared_ptr<message> pointer_t;
58 : typedef std::vector<message> vector_t;
59 : typedef string_map_t parameters_t;
60 :
61 : enum class format_t
62 : {
63 : MESSAGE_FORMAT_STRING,
64 : MESSAGE_FORMAT_JSON,
65 : };
66 :
67 : bool from_message(std::string const & msg);
68 : bool from_string(std::string const & msg);
69 : bool from_json(std::string const & msg);
70 : std::string to_message(format_t format = format_t::MESSAGE_FORMAT_STRING) const;
71 : std::string to_string() const;
72 : std::string to_json() const;
73 :
74 : std::string const & get_sent_from_server() const;
75 : void set_sent_from_server(std::string const & server);
76 : std::string const & get_sent_from_service() const;
77 : void set_sent_from_service(std::string const & service);
78 : std::string const & get_server() const;
79 : void set_server(std::string const & server);
80 : std::string const & get_service() const;
81 : void set_service(std::string const & service);
82 : void reply_to(message const & msg);
83 : std::string const & get_command() const;
84 : void set_command(std::string const & command);
85 : message_version_t get_message_version() const;
86 : bool check_version_parameter() const;
87 : void add_version_parameter();
88 : void add_parameter(std::string const & name, std::string const & value);
89 : void add_parameter(std::string const & name, std::int32_t value);
90 : void add_parameter(std::string const & name, std::uint32_t value);
91 : void add_parameter(std::string const & name, long long value);
92 : void add_parameter(std::string const & name, unsigned long long value);
93 : void add_parameter(std::string const & name, std::int64_t value);
94 : void add_parameter(std::string const & name, std::uint64_t value);
95 : void add_parameter(std::string const & name, addr::addr const & value, bool mask = false);
96 : bool has_parameter(std::string const & name) const;
97 : std::string get_parameter(std::string const & name) const;
98 : std::int64_t get_integer_parameter(std::string const & name) const;
99 : parameters_t const & get_all_parameters() const;
100 :
101 : template<typename T>
102 0 : void user_data(std::shared_ptr<T> data) { f_user_data = data; }
103 : template<typename T>
104 0 : std::shared_ptr<T> user_data() { return std::static_pointer_cast<T>(f_user_data); }
105 :
106 : private:
107 : std::string f_sent_from_server = std::string();
108 : std::string f_sent_from_service = std::string();
109 : std::string f_server = std::string();
110 : std::string f_service = std::string();
111 : std::string f_command = std::string();
112 : parameters_t f_parameters = parameters_t();
113 : mutable std::string f_cached_message = std::string();
114 : mutable std::string f_cached_json = std::string();
115 : std::shared_ptr<void> f_user_data = std::shared_ptr<void>();
116 : };
117 :
118 :
119 : void verify_message_name(
120 : std::string const & name
121 : , bool can_be_empty = false
122 : , bool can_be_lowercase = true);
123 :
124 :
125 :
126 : } // namespace ed
127 : // vim: ts=4 sw=4 et
|