Packet.hh
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2014 Open Source Robotics Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16 */
17 
18 #ifndef __IGN_TRANSPORT_PACKET_HH_INCLUDED__
19 #define __IGN_TRANSPORT_PACKET_HH_INCLUDED__
20 
21 #include <cstdint>
22 #include <iostream>
23 #include <string>
24 #include <vector>
25 
28 
29 namespace ignition
30 {
31  namespace transport
32  {
33  // Message types.
34  static const uint8_t Uninitialized = 0;
35  static const uint8_t AdvType = 1;
36  static const uint8_t SubType = 2;
37  static const uint8_t UnadvType = 3;
38  static const uint8_t HeartbeatType = 4;
39  static const uint8_t ByeType = 5;
40  static const uint8_t AdvSrvType = 6;
41  static const uint8_t SubSrvType = 7;
42  static const uint8_t UnadvSrvType = 8;
43  static const uint8_t NewConnection = 9;
44  static const uint8_t EndConnection = 10;
45 
47  static const std::vector<std::string> MsgTypesStr =
48  {
49  "UNINITIALIZED", "ADVERTISE", "SUBSCRIBE", "UNADVERTISE", "HEARTBEAT",
50  "BYE", "ADV_SRV", "SUB_SRV", "UNADVERTISE_SRV", "NEW_CONNECTION",
51  "END_CONNECTION"
52  };
53 
57  // of message (ADV, SUB, ... ) and optional flags.
59  {
61  public: Header() = default;
62 
69  public: Header(const uint16_t _version,
70  const std::string &_pUuid,
71  const uint8_t _type,
72  const uint16_t _flags = 0);
73 
75  public: virtual ~Header() = default;
76 
80  public: uint16_t Version() const;
81 
85  public: std::string PUuid() const;
86 
90  public: uint8_t Type() const;
91 
95  public: uint16_t Flags() const;
96 
100  public: void SetVersion(const uint16_t _version);
101 
105  public: void SetPUuid(const std::string &_pUuid);
106 
110  public: void SetType(const uint8_t _type);
111 
115  public: void SetFlags(const uint16_t _flags);
116 
119  public: int HeaderLength() const;
120 
126  public: size_t Pack(char *_buffer) const;
127 
130  public: size_t Unpack(const char *_buffer);
131 
135  public: friend std::ostream &operator<<(std::ostream &_out,
136  const Header &_header)
137  {
138  _out << "--------------------------------------\n"
139  << "Header:" << std::endl
140  << "\tVersion: " << _header.Version() << "\n"
141  << "\tProcess UUID: " << _header.PUuid() << "\n"
142  << "\tType: " << MsgTypesStr.at(_header.Type()) << "\n"
143  << "\tFlags: " << _header.Flags() << "\n";
144  return _out;
145  }
146 
148  private: uint16_t version = 0;
149 
151  private: std::string pUuid = "";
152 
154  private: uint8_t type = Uninitialized;
155 
157  private: uint16_t flags = 0;
158  };
159 
164  {
166  public: SubscriptionMsg() = default;
167 
171  public: SubscriptionMsg(const transport::Header &_header,
172  const std::string &_topic);
173 
177  public: transport::Header Header() const;
178 
182  public: std::string Topic() const;
183 
187  public: void SetHeader(const transport::Header &_header);
188 
192  public: void SetTopic(const std::string &_topic);
193 
196  public: size_t MsgLength() const;
197 
201  public: friend std::ostream &operator<<(std::ostream &_out,
202  const SubscriptionMsg &_msg)
203  {
204  _out << _msg.Header()
205  << "Body:" << std::endl
206  << "\tTopic: [" << _msg.Topic() << "]" << std::endl;
207 
208  return _out;
209  }
210 
214  public: size_t Pack(char *_buffer) const;
215 
219  public: size_t Unpack(char *_buffer);
220 
222  private: transport::Header header;
223 
225  private: std::string topic = "";
226  };
227 
234 
235  template <class T> class IGNITION_VISIBLE AdvertiseMessage
236  {
238  public: AdvertiseMessage() = default;
239 
243  public: AdvertiseMessage(const Header &_header,
244  const T &_publisher)
245  : header(_header),
246  publisher(_publisher)
247  {
248  }
249 
253  public: transport::Header Header() const
254  {
255  return this->header;
256  }
257 
261  public: T& Publisher()
262  {
263  return this->publisher;
264  }
265 
269  public: void SetHeader(const transport::Header &_header)
270  {
271  this->header = _header;
272  }
273 
277  public: void SetPublisher(const T &_publisher)
278  {
279  this->publisher = _publisher;
280  }
281 
284  public: size_t MsgLength() const
285  {
286  return this->header.HeaderLength() + this->publisher.MsgLength();
287  }
288 
292  public: size_t Pack(char *_buffer) const
293  {
294  // Pack the common part of any advertise message.
295  size_t len = this->header.Pack(_buffer);
296  if (len == 0)
297  return 0;
298 
299  _buffer += len;
300 
301  // Pack the part of the publisher.
302  if (this->publisher.Pack(_buffer) == 0)
303  return 0;
304 
305  return this->MsgLength();
306  }
307 
311  public: size_t Unpack(char *_buffer)
312  {
313  // Unpack the message publisher.
314  if (this->publisher.Unpack(_buffer) == 0)
315  return 0;
316 
317  return this->publisher.MsgLength();
318  }
319 
323  public: friend std::ostream &operator<<(std::ostream &_out,
324  const AdvertiseMessage &_msg)
325  {
326  _out << _msg.header << _msg.publisher;
327  return _out;
328  }
329 
331  private: transport::Header header;
332 
334  private: T publisher;
335  };
336  }
337 }
338 
339 #endif
std::string Topic() const
Get the topic.
uint16_t Version() const
Get the discovery protocol version.
static const uint8_t AdvSrvType
Definition: Packet.hh:40
AdvertiseMessage(const Header &_header, const T &_publisher)
Constructor.
Definition: Packet.hh:243
friend std::ostream & operator<<(std::ostream &_out, const AdvertiseMessage &_msg)
Stream insertion operator.
Definition: Packet.hh:323
#define IGNITION_VISIBLE
Use to represent "symbol visible" if supported.
Definition: Helpers.hh:56
static const std::vector< std::string > MsgTypesStr
Used for debugging the message type received/send.
Definition: Packet.hh:47
uint16_t Flags() const
Get the message flags.
friend std::ostream & operator<<(std::ostream &_out, const SubscriptionMsg &_msg)
Stream insertion operator.
Definition: Packet.hh:201
Header included in each discovery message containing the version of the discovery protocol...
Definition: Packet.hh:58
Subscription packet used in the discovery protocol for requesting information about a given topic...
Definition: Packet.hh:163
std::string PUuid() const
Get the process uuid.
transport::Header Header() const
Get the message header.
static const uint8_t NewConnection
Definition: Packet.hh:43
static const uint8_t HeartbeatType
Definition: Packet.hh:38
size_t Pack(char *_buffer) const
Serialize the advertise message.
Definition: Packet.hh:292
static const uint8_t ByeType
Definition: Packet.hh:39
static const uint8_t Uninitialized
Definition: Packet.hh:34
size_t Unpack(char *_buffer)
Unserialize a stream of bytes into an AdvertiseMessage.
Definition: Packet.hh:311
void SetPublisher(const T &_publisher)
Set the publisher of this message.
Definition: Packet.hh:277
friend std::ostream & operator<<(std::ostream &_out, const Header &_header)
Stream insertion operator.
Definition: Packet.hh:135
static const uint8_t EndConnection
Definition: Packet.hh:44
static const uint8_t UnadvSrvType
Definition: Packet.hh:42
static const uint8_t SubType
Definition: Packet.hh:36
transport::Header Header() const
Get the message header.
Definition: Packet.hh:253
Advertise packet used in the discovery protocol to broadcast information about the node advertising a...
Definition: Packet.hh:235
uint8_t Type() const
Get the message type.
static const uint8_t SubSrvType
Definition: Packet.hh:41
size_t MsgLength() const
Get the total length of the message.
Definition: Packet.hh:284
Definition: AdvertiseOptions.hh:25
static const uint8_t AdvType
Definition: Packet.hh:35
void SetHeader(const transport::Header &_header)
Set the header of the message.
Definition: Packet.hh:269
T & Publisher()
Get the publisher of this message.
Definition: Packet.hh:261
static const uint8_t UnadvType
Definition: Packet.hh:37