DDS  ver. 3.4
ProtocolMessage.h
Go to the documentation of this file.
1 // Copyright 2014 GSI, Inc. All rights reserved.
2 //
3 //
4 //
5 #ifndef __DDS__ProtocolMessage__
6 #define __DDS__ProtocolMessage__
7 // DDS
8 #include "def.h"
9 // STD
10 #include <cstring>
11 #include <memory>
12 // BOOST
13 #include <boost/crc.hpp>
14 
15 namespace dds
16 {
17  namespace protocol_api
18  {
19  // a very simple protocol
20  // |>>>>>>>>> HEADER <<<<|>>>>>>>>>>>>>>>> DATA <<<<<<<<<<<<<<<<<<|
21  //
22  // HEADER:
23  // - HEADER CHECKSUM (2 bytes): uint16_t
24  // - CMD (2 bytes): uint16_t
25  // - LEN (4 bytes): uint32_t
26  // - SENDER ID (8 bytes): uint64_t
27  //
28  // DATA
29  // DATA BLOCK (size of LEN) unsigned char
30  //
31  //----------------------------------------------------------------------
33  {
35  : m_crc(0)
36  , m_cmd(0)
37  , m_len(0)
38  , m_ID(0)
39  {
40  }
41 
42  bool isValid() const
43  {
44  return (getChecksum() == m_crc);
45  }
46  uint16_t getChecksum() const
47  {
48  // Calculate header's checksum.
49  // In order to do that, we of course should exclude m_crc from the calculation.
50  unsigned char const* buf = reinterpret_cast<unsigned char const*>(this);
51  boost::crc_16_type crc16;
52  crc16.process_bytes(buf + sizeof(uint16_t), sizeof(SMessageHeader) - sizeof(uint16_t));
53 
54  return crc16.checksum();
55  }
56 
57  void clear()
58  {
59  m_cmd = 0;
60  m_len = 0;
61  };
62 
63  // !!! IMPORTANT. The checksum member should be always on the top of the member's list !!!
64  // we exclude it from checksum calculations.
65  uint16_t m_crc;
67  uint16_t m_cmd;
68  uint32_t m_len;
71  uint64_t m_ID;
72  };
73 
74  //----------------------------------------------------------------------
75 
77  {
78  public:
80  typedef dataContainer_t::value_type data_t;
81  typedef std::shared_ptr<CProtocolMessage> protocolMessagePtr_t;
82 
83  enum
84  {
86  };
87  enum
88  {
90  };
91  /* enum
92  {
93  max_body_length = 512
94  };*/
95 
96  public:
98 
99  CProtocolMessage(uint16_t _cmd, const MiscCommon::BYTEVector_t& _data, uint64_t _ID);
100 
101  public:
102  void encode(uint16_t _cmd, const MiscCommon::BYTEVector_t& _data, uint64_t _ID)
103  {
104  _encode_message(_cmd, _data, _ID);
105  }
106 
107  void clear();
108  void resize(size_t _size); // FIXME: Used in tests to allocate memory for m_data.
109  const data_t* data() const;
110  data_t* data();
111  size_t length() const;
112  const data_t* body() const;
113  data_t* body();
114  size_t body_length() const;
115  bool decode_header();
116  const SMessageHeader& header() const;
117  std::string toString() const;
119  {
120  dataContainer_t buf(body(), body() + body_length());
121  return buf;
122  }
123 
124  private:
125  void _encode_message(uint16_t _cmd, const dataContainer_t& _data, uint64_t _ID);
126 
127  private:
128  dataContainer_t m_data;
129  SMessageHeader m_header;
130  };
131  } // namespace protocol_api
132 } // namespace dds
133 
134 #endif /* defined(__DDS__ProtocolMessage__) */
void clear()
Definition: ProtocolMessage.h:57
uint32_t m_len
Definition: ProtocolMessage.h:68
void clear()
Definition: ProtocolMessage.cpp:27
CProtocolMessage()
Definition: ProtocolMessage.cpp:16
uint16_t getChecksum() const
Definition: ProtocolMessage.h:46
std::string toString() const
Definition: ProtocolMessage.cpp:134
void resize(size_t _size)
Definition: ProtocolMessage.cpp:34
Definition: ProtocolMessage.h:76
uint16_t m_crc
Definition: ProtocolMessage.h:61
dataContainer_t bodyToContainer() const
Definition: ProtocolMessage.h:118
uint16_t m_cmd
Definition: ProtocolMessage.h:67
bool isValid() const
Definition: ProtocolMessage.h:42
Definition: AgentConnectionManager.h:13
const SMessageHeader & header() const
Definition: ProtocolMessage.cpp:129
void encode(uint16_t _cmd, const MiscCommon::BYTEVector_t &_data, uint64_t _ID)
Definition: ProtocolMessage.h:102
std::vector< unsigned char > BYTEVector_t
An STL vector of bytes.
Definition: def.h:127
size_t body_length() const
Definition: ProtocolMessage.cpp:64
const data_t * data() const
Definition: ProtocolMessage.cpp:39
const data_t * body() const
Definition: ProtocolMessage.cpp:54
MiscCommon::BYTEVector_t dataContainer_t
Definition: ProtocolMessage.h:79
uint64_t m_ID
Messages TO Commander: this is a sender ID (ID of the client's channel) Messages FROM Commander: this...
Definition: ProtocolMessage.h:71
SMessageHeader()
Definition: ProtocolMessage.h:34
dataContainer_t::value_type data_t
Definition: ProtocolMessage.h:80
size_t length() const
Definition: ProtocolMessage.cpp:49
Definition: ProtocolMessage.h:32
bool decode_header()
Definition: ProtocolMessage.cpp:75
std::shared_ptr< CProtocolMessage > protocolMessagePtr_t
Definition: ProtocolMessage.h:81