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 :
33 : namespace ed
34 : {
35 :
36 :
37 :
38 : typedef int message_version_t;
39 :
40 : // this version defines the protocol version, it should really rarely
41 : // change if ever
42 : //
43 : constexpr message_version_t MESSAGE_VERSION = 1;
44 :
45 : constexpr char const MESSAGE_VERSION_NAME[] = "version";
46 :
47 :
48 :
49 44 : class message
50 : {
51 : public:
52 : typedef std::shared_ptr<message> pointer_t;
53 : typedef std::vector<message> vector_t;
54 : typedef string_map_t parameters_t;
55 :
56 : enum class format_t
57 : {
58 : MESSAGE_FORMAT_STRING,
59 : MESSAGE_FORMAT_JSON,
60 : };
61 :
62 : bool from_message(std::string const & msg);
63 : bool from_string(std::string const & msg);
64 : bool from_json(std::string const & msg);
65 : std::string to_message(format_t format = format_t::MESSAGE_FORMAT_STRING) const;
66 : std::string to_string() const;
67 : std::string to_json() const;
68 :
69 : std::string const & get_sent_from_server() const;
70 : void set_sent_from_server(std::string const & server);
71 : std::string const & get_sent_from_service() const;
72 : void set_sent_from_service(std::string const & service);
73 : std::string const & get_server() const;
74 : void set_server(std::string const & server);
75 : std::string const & get_service() const;
76 : void set_service(std::string const & service);
77 : void reply_to(message const & msg);
78 : std::string const & get_command() const;
79 : void set_command(std::string const & command);
80 : message_version_t get_message_version() const;
81 : bool check_version_parameter() const;
82 : void add_version_parameter();
83 : void add_parameter(std::string const & name, std::string const & value);
84 : void add_parameter(std::string const & name, std::int32_t value);
85 : void add_parameter(std::string const & name, std::uint32_t value);
86 : void add_parameter(std::string const & name, long long value);
87 : void add_parameter(std::string const & name, unsigned long long value);
88 : void add_parameter(std::string const & name, std::int64_t value);
89 : void add_parameter(std::string const & name, std::uint64_t value);
90 : bool has_parameter(std::string const & name) const;
91 : std::string get_parameter(std::string const & name) const;
92 : std::int64_t get_integer_parameter(std::string const & name) const;
93 : parameters_t const & get_all_parameters() const;
94 :
95 : private:
96 : std::string f_sent_from_server = std::string();
97 : std::string f_sent_from_service = std::string();
98 : std::string f_server = std::string();
99 : std::string f_service = std::string();
100 : std::string f_command = std::string();
101 : parameters_t f_parameters = parameters_t();
102 : mutable std::string f_cached_message = std::string();
103 : mutable std::string f_cached_json = std::string();
104 : };
105 :
106 :
107 : void verify_message_name(
108 : std::string const & name
109 : , bool can_be_empty = false
110 : , bool can_be_lowercase = true);
111 :
112 :
113 :
114 : } // namespace ed
115 : // vim: ts=4 sw=4 et
|