dodo  0.0.1
A C++ library to create containerized Linux services
stomp.cpp
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.cpp
20  * Implements the dodo::network::stomp classes.
21  */
22 
24 #include "common/bytes.hpp"
25 #include "common/puts.hpp"
26 
27 #include <cstring>
28 
29 
31 
32  Frame::FrameMatch Frame::readCommand( const common::Bytes& frame, size_t &index, const common::Bytes& command ) const {
33 
34  enum State {
35  command_read,
36  endofline,
37  };
38 
39  State state = command_read;
40  size_t cmd_idx = 0;
41  while ( index < frame.getSize() ) {
42  switch ( state ) {
43  case command_read:
44  if ( cmd_idx == command.getSize() ) {
45  state = endofline;
46  } else {
47  if ( frame.getOctet(index) != command.getOctet(cmd_idx) ) return FrameMatch::NoMatch;
48  index++;
49  cmd_idx++;
50  }
51  case endofline:
52  if ( frame.getOctet(index) == '\r' ) {
53  index++;
54  } else if ( frame.getOctet(index) == '\n' ) {
55  return FrameMatch::FullMatch;
56  } else return FrameMatch::NoMatch;
57  }
58  }
60  }
61 
62  Frame::FrameMatch Connect::match( const common::Bytes& frame, std::list<std::string> &errors ) const {
63  size_t index = 0;
65  return match;
66  }
67 
68  void Connect::generate( common::Bytes& frame ) const {
69  frame.free();
70  frame.append( command_connect );
71  frame.append( eol );
73  frame.append( eol );
74  frame.append( { common::Puts() << "host:" << host_ } );
75  frame.append( eol );
76  if ( login_.length() > 0 ) {
77  frame.append( { common::Puts() << "login:" << login_ } );
78  frame.append( eol );
79  frame.append( { common::Puts() << "passcode:" << passcode_ } );
80  frame.append( eol );
81  }
82  if ( heartbeat_out_ms_ ) {
83  frame.append( { common::Puts() << "heart-beat:" << heartbeat_out_ms_ << "," << heartbeat_in_ms_ } );
84  frame.append( eol );
85  }
86 
87  frame.append( eol );
88  frame.append( 0 );
89  }
90 
91 }
dodo::network::protocol::stomp::Connect::host_
std::string host_
STOMP host to CONNECT.
Definition: stomp.hpp:182
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
stomp.hpp
dodo::common::Bytes::getOctet
Octet getOctet(size_t index) const
Return the Octet at index.
Definition: bytes.hpp:199
dodo::common::Bytes::free
void free()
Free and clear data.
Definition: bytes.cpp:56
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.
puts.hpp
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::Frame::FrameMatch::IncompleteMatch
@ IncompleteMatch
Match, but incomplete.
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::FrameMatch::NoMatch
@ NoMatch
Mismatch.
dodo::common::Bytes::append
void append(const Bytes &src)
Append another Bytes.
Definition: bytes.cpp:65
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
The STOMP 1.2 protocol (earlier versions not supported).
Definition: network.hpp:82
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::Frame::FrameMatch
FrameMatch
The manner in which frames match the STOMP protocol.
Definition: stomp.hpp:54
dodo::common::Puts
Helper class to write strings in stream format, eg.
Definition: puts.hpp:46
dodo::common::Bytes::getSize
size_t getSize() const
Return the array size.
Definition: bytes.hpp:192
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
bytes.hpp
dodo::network::protocol::stomp::Connect::generate
virtual void generate(common::Bytes &frame) const
Generate a STOMP (CONNECT) frame.
Definition: stomp.cpp:68