dodo  0.0.1
A C++ library to create containerized Linux services
stomp.hpp
Go to the documentation of this file.
1 /*
2  * This file is part of the dodo library (https://github.com/jmspit/dodo).
3  * Copyright (c) 2019 Jan-Marten Spit.
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, version 3.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 /**
19  * @file stomp.hpp
20  * Defines the dodo::network::protocol::Stomp classes.
21  */
22 
23 #ifndef network_protocol_stomp_hpp
24 #define network_protocol_stomp_hpp
25 
26 #include <common/bytes.hpp>
27 
28 #include <list>
29 #include <string>
30 
32 
33  /**
34  * A generic STOMP 1.2 frame.
35  */
36  class Frame {
37  public:
38 
39  /** EOL sequence - use only \n as the protocl allows it and the extra \n serves no purpose. */
40  const common::Bytes eol{ "\n" };
41 
42  /** CONNECT command. */
43  const common::Bytes command_connect{ "STOMP" };
44 
45  /** CONNECTED command. */
46  const common::Bytes command_connected{ "CONNECTED" };
47 
48  /** STOMP 1.2 accept header. */
49  const common::Bytes header_accept_version_1_2{ "accept-version:1.2" };
50 
51  /**
52  * The manner in which frames match the STOMP protocol.
53  */
54  enum class FrameMatch {
55  NoMatch, /**< Mismatch */
56  IncompleteMatch, /**< Match, but incomplete */
57  FullMatch /**< Complete match */
58  };
59 
60  /**
61  * STOMP versions. Only the latest 1.2 version is supported.
62  */
63  enum class Version {
64  v1_2 /**< STOMP 1.2 */
65  };
66 
67  /**
68  * Checks how the data matches a frame specification.
69  * @param frame The frame / common::Bytes to match against.
70  * @param errors If returning FrameMatch::NoMatch, one or more errors.
71  * @return FrameMatch::IncompleteMatch if the frame matches but is incomplete,
72  * FrameMatch::FullMatch if it matches completely.
73  */
74  virtual FrameMatch match( const common::Bytes& frame, std::list<std::string> &errors ) const = 0;
75 
76  /**
77  * Generate a frame.
78  * @param frame The generation destination, which is overwritten.
79  */
80  virtual void generate( common::Bytes& frame ) const = 0;
81 
82  protected:
83 
84  /**
85  * Read a command from a frame.
86  * @param frame The (incomplete) frame
87  * @param index The index into fame (0 for commands).
88  * @param command The command to read.
89  * @return FrameMatch::IncompleteMatch if the frame matches but is incomplete,
90  * FrameMatch::FullMatch if it matches completely.
91  */
92  FrameMatch readCommand( const common::Bytes& frame, size_t &index, const common::Bytes& command ) const;
93 
94  };
95 
96  /**
97  * A STOMP CONNECT frame.
98  */
99  class Connect : public Frame {
100  public:
101 
102  /**
103  * Constructor.
104  * @param version The STOMP protocol version.
105  */
106  Connect( const Version &version = Version::v1_2 ) : Frame(),
107  host_(""),
108  login_(""),
109  passcode_(""),
110  version_(version),
112  heartbeat_in_ms_(0) {}
113 
114 
115  /**
116  * Checks how the data matches a frame specification.
117  * @param frame The frame / common::Bytes to match against.
118  * @param errors If returning FrameMatch::NoMatch, one or more errors.
119  * @return FrameMatch::IncompleteMatch if the frame matches but is incomplete,
120  * FrameMatch::FullMatch if it matches completely.
121  */
122  virtual FrameMatch match( const common::Bytes& frame, std::list<std::string> &errors ) const;
123 
124  /**
125  * Generate a STOMP (CONNECT) frame.
126  * @param frame The generation destination, which is overwritten.
127  */
128  virtual void generate( common::Bytes& frame ) const;
129 
130  /**
131  * Return the STOMP host.
132  * @return The host.
133  */
134  std::string getHost() const { return host_; };
135 
136  /**
137  * Set the STOMP host.
138  * @param host The host to set.
139  */
140  void setHost( const std::string &host ) { host_ = host; };
141 
142  /**
143  * Return the login.
144  * @return The login.
145  */
146  std::string getLogin() const { return login_; };
147 
148  /**
149  * Set the login.
150  * @param login The login to set.
151  */
152  void setLogin( const std::string &login ) { login_ = login; };
153 
154  /**
155  * Return the passcode.
156  * @return The passcode.
157  */
158  std::string getPasscode() const { return passcode_; };
159 
160  /**
161  * Set the login.
162  * @param passcode The passcode to set.
163  */
164  void setPasscode( const std::string &passcode ) { passcode_ = passcode; }
165 
166 
167  /**
168  * Return the outgoing heartbeat in milliseconds.
169  * @return the outgoing heartbeat interval in milliseconds.
170  */
171  size_t getHeartbeatOut() const { return heartbeat_out_ms_; }
172 
173  /**
174  * Set the heartbeat.
175  * @param out outgoing heartbeat interval in milliseconds.
176  * @param in incoming heartbeat interval in milliseconds.
177  */
178  void setHeartbeat( size_t out, size_t in ) { heartbeat_out_ms_ = out; heartbeat_in_ms_ = in; }
179 
180  protected:
181  /** STOMP host to CONNECT. */
182  std::string host_;
183 
184  /** Optional login. */
185  std::string login_;
186 
187  /** Optional passcode (required if login is not empty) */
188  std::string passcode_;
189 
190  /** STOMP protocol version required in CONNECT handshake. */
192 
193  /** Offered outgoing heartbeat delay in milliseconds. */
195 
196  /** Desired incoming heartbeat delay in milliseconds. */
198  };
199 
200  /**
201  * A STOMP CONNECTED frame.
202  */
203  class Connected : public Frame {
204  public:
205 
206  /**
207  * Constructor.
208  * @param version The STOMP protocol version.
209  */
210  Connected( const Version &version ) :
211  version_(version),
214  {}
215 
216  /**
217  * Generate a CONNECTED frame.
218  * @param frame The generation destination, which is overwritten.
219  */
220  virtual void generate( common::Bytes& frame ) const;
221 
222  protected:
223 
224  /** STOMP protocol version required in CONNECT handshake. */
226 
227  /** Offered outgoing heartbeat delay in milliseconds. */
229 
230  /** Desired incoming heartbeat delay in milliseconds. */
232 
233  /** The session id. */
234  std::string session_id_;
235 
236  /** The server id. */
237  std::string server_;
238 
239 
240  };
241 
242 }
243 
244 #endif
dodo::network::protocol::stomp::Connect::host_
std::string host_
STOMP host to CONNECT.
Definition: stomp.hpp:182
dodo::network::protocol::stomp::Connected::heartbeat_in_ms_
size_t heartbeat_in_ms_
Desired incoming heartbeat delay in milliseconds.
Definition: stomp.hpp:231
dodo::network::protocol::stomp::Connect::match
virtual FrameMatch match(const common::Bytes &frame, std::list< std::string > &errors) const
Checks how the data matches a frame specification.
Definition: stomp.cpp:62
dodo::network::protocol::stomp::Connected::heartbeat_out_ms_
size_t heartbeat_out_ms_
Offered outgoing heartbeat delay in milliseconds.
Definition: stomp.hpp:228
dodo::network::protocol::stomp::Frame::match
virtual FrameMatch match(const common::Bytes &frame, std::list< std::string > &errors) const =0
Checks how the data matches a frame specification.
dodo::network::protocol::stomp::Connect::version_
Version version_
STOMP protocol version required in CONNECT handshake.
Definition: stomp.hpp:191
dodo::network::protocol::stomp::Connect::heartbeat_in_ms_
size_t heartbeat_in_ms_
Desired incoming heartbeat delay in milliseconds.
Definition: stomp.hpp:197
dodo::network::protocol::stomp::Frame::FrameMatch::FullMatch
@ FullMatch
Complete match.
dodo::network::protocol::stomp::Connected::Connected
Connected(const Version &version)
Constructor.
Definition: stomp.hpp:210
dodo::network::protocol::stomp::Frame::command_connect
const common::Bytes command_connect
CONNECT command.
Definition: stomp.hpp:43
dodo::common::Bytes
An array of Octets with size elements.
Definition: bytes.hpp:44
dodo::network::protocol::stomp::Connect::getHost
std::string getHost() const
Return the STOMP host.
Definition: stomp.hpp:134
dodo::network::protocol::stomp::Connect::getLogin
std::string getLogin() const
Return the login.
Definition: stomp.hpp:146
dodo::network::protocol::stomp::Frame::FrameMatch::IncompleteMatch
@ IncompleteMatch
Match, but incomplete.
dodo::network::protocol::stomp::Frame::Version::v1_2
@ v1_2
STOMP 1.2.
dodo::network::protocol::stomp::Frame::command_connected
const common::Bytes command_connected
CONNECTED command.
Definition: stomp.hpp:46
dodo::network::protocol::stomp::Connect
A STOMP CONNECT frame.
Definition: stomp.hpp:99
dodo::network::protocol::stomp::Frame::eol
const common::Bytes eol
EOL sequence - use only as the protocl allows it and the extra serves no purpose.
Definition: stomp.hpp:40
dodo::network::protocol::stomp::Frame::Version
Version
STOMP versions.
Definition: stomp.hpp:63
dodo::network::protocol::stomp::Frame::FrameMatch::NoMatch
@ NoMatch
Mismatch.
dodo::network::protocol::stomp::Connected::session_id_
std::string session_id_
The session id.
Definition: stomp.hpp:234
dodo::network::protocol::stomp::Connect::passcode_
std::string passcode_
Optional passcode (required if login is not empty)
Definition: stomp.hpp:188
dodo::network::protocol::stomp::Connect::login_
std::string login_
Optional login.
Definition: stomp.hpp:185
dodo::network::protocol::stomp::Connect::Connect
Connect(const Version &version=Version::v1_2)
Constructor.
Definition: stomp.hpp:106
dodo::network::protocol::stomp::Connect::getPasscode
std::string getPasscode() const
Return the passcode.
Definition: stomp.hpp:158
dodo::network::protocol::stomp
The STOMP 1.2 protocol (earlier versions not supported).
Definition: network.hpp:82
dodo::network::protocol::stomp::Connect::setHeartbeat
void setHeartbeat(size_t out, size_t in)
Set the heartbeat.
Definition: stomp.hpp:178
dodo::network::protocol::stomp::Connected::server_
std::string server_
The server id.
Definition: stomp.hpp:237
dodo::network::protocol::stomp::Frame::readCommand
FrameMatch readCommand(const common::Bytes &frame, size_t &index, const common::Bytes &command) const
Read a command from a frame.
Definition: stomp.cpp:32
dodo::network::protocol::stomp::Connect::heartbeat_out_ms_
size_t heartbeat_out_ms_
Offered outgoing heartbeat delay in milliseconds.
Definition: stomp.hpp:194
dodo::network::protocol::stomp::Connected
A STOMP CONNECTED frame.
Definition: stomp.hpp:203
dodo::network::protocol::stomp::Frame
A generic STOMP 1.2 frame.
Definition: stomp.hpp:36
dodo::network::protocol::stomp::Frame::FrameMatch
FrameMatch
The manner in which frames match the STOMP protocol.
Definition: stomp.hpp:54
dodo::network::protocol::stomp::Connected::version_
Version version_
STOMP protocol version required in CONNECT handshake.
Definition: stomp.hpp:225
dodo::network::protocol::stomp::Connect::setLogin
void setLogin(const std::string &login)
Set the login.
Definition: stomp.hpp:152
dodo::network::protocol::stomp::Connect::setHost
void setHost(const std::string &host)
Set the STOMP host.
Definition: stomp.hpp:140
dodo::network::protocol::stomp::Connect::setPasscode
void setPasscode(const std::string &passcode)
Set the login.
Definition: stomp.hpp:164
dodo::network::protocol::stomp::Connected::generate
virtual void generate(common::Bytes &frame) const
Generate a CONNECTED frame.
dodo::network::protocol::stomp::Frame::header_accept_version_1_2
const common::Bytes header_accept_version_1_2
STOMP 1.2 accept header.
Definition: stomp.hpp:49
dodo::network::protocol::stomp::Connect::getHeartbeatOut
size_t getHeartbeatOut() const
Return the outgoing heartbeat in milliseconds.
Definition: stomp.hpp:171
bytes.hpp
dodo::network::protocol::stomp::Connect::generate
virtual void generate(common::Bytes &frame) const
Generate a STOMP (CONNECT) frame.
Definition: stomp.cpp:68
dodo::network::protocol::stomp::Frame::generate
virtual void generate(common::Bytes &frame) const =0
Generate a frame.