Ignition Transport

API Reference

7.1.0
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_
19 #define IGN_TRANSPORT_PACKET_HH_
20 
21 #include <cstdint>
22 #include <iostream>
23 #include <string>
24 #include <vector>
25 
26 #include "ignition/transport/config.hh"
27 #include "ignition/transport/Export.hh"
29 
30 namespace ignition
31 {
32  namespace transport
33  {
34  // Inline bracket to help doxygen filtering.
35  inline namespace IGNITION_TRANSPORT_VERSION_NAMESPACE {
36  //
37  // Message types.
38  static const uint8_t Uninitialized = 0;
39  static const uint8_t AdvType = 1;
40  static const uint8_t SubType = 2;
41  static const uint8_t UnadvType = 3;
42  static const uint8_t HeartbeatType = 4;
43  static const uint8_t ByeType = 5;
44  static const uint8_t NewConnection = 6;
45  static const uint8_t EndConnection = 7;
46 
47  // Flag set when a discovery message is relayed.
48  static const uint16_t FlagRelay = 0b000000000000'0001;
49  // Flag set when we want to avoid to relay a discovery message.
50  // This is used to avoid loops.
51  static const uint16_t FlagNoRelay = 0b000000000000'0010;
52 
55  {
56  "UNINITIALIZED", "ADVERTISE", "SUBSCRIBE", "UNADVERTISE", "HEARTBEAT",
57  "BYE", "NEW_CONNECTION", "END_CONNECTION"
58  };
59 
63  // of message (ADV, SUB, ... ) and optional flags.
64  class IGNITION_TRANSPORT_VISIBLE Header
65  {
67  public: Header() = default;
68 
74  public: Header(const uint16_t _version,
75  const std::string &_pUuid,
76  const uint8_t _type,
77  const uint16_t _flags = 0);
78 
80  public: virtual ~Header() = default;
81 
85  public: uint16_t Version() const;
86 
90  public: std::string PUuid() const;
91 
95  public: uint8_t Type() const;
96 
100  public: uint16_t Flags() const;
101 
105  public: void SetVersion(const uint16_t _version);
106 
110  public: void SetPUuid(const std::string &_pUuid);
111 
115  public: void SetType(const uint8_t _type);
116 
120  public: void SetFlags(const uint16_t _flags);
121 
124  public: int HeaderLength() const;
125 
131  public: size_t Pack(char *_buffer) const;
132 
135  public: size_t Unpack(const char *_buffer);
136 
140  public: friend std::ostream &operator<<(std::ostream &_out,
141  const Header &_header)
142  {
143  _out << "--------------------------------------\n"
144  << "Header:" << std::endl
145  << "\tVersion: " << _header.Version() << "\n"
146  << "\tProcess UUID: " << _header.PUuid() << "\n"
147  << "\tType: " << MsgTypesStr.at(_header.Type()) << "\n"
148  << "\tFlags: " << _header.Flags() << "\n";
149  return _out;
150  }
151 
153  private: uint16_t version = 0;
154 
155 #ifdef _WIN32
156 // Disable warning C4251 which is triggered by
157 // std::string
158 #pragma warning(push)
159 #pragma warning(disable: 4251)
160 #endif
161  private: std::string pUuid = "";
163 #ifdef _WIN32
164 #pragma warning(pop)
165 #endif
166 
168  private: uint8_t type = Uninitialized;
169 
171  private: uint16_t flags = 0;
172  };
173 
177  class IGNITION_TRANSPORT_VISIBLE SubscriptionMsg
178  {
180  public: SubscriptionMsg() = default;
181 
185  public: SubscriptionMsg(const transport::Header &_header,
186  const std::string &_topic);
187 
191  public: transport::Header Header() const;
192 
196  public: std::string Topic() const;
197 
201  public: void SetHeader(const transport::Header &_header);
202 
206  public: void SetTopic(const std::string &_topic);
207 
210  public: size_t MsgLength() const;
211 
215  public: friend std::ostream &operator<<(std::ostream &_out,
216  const SubscriptionMsg &_msg)
217  {
218  _out << _msg.Header()
219  << "Body:" << std::endl
220  << "\tTopic: [" << _msg.Topic() << "]" << std::endl;
221 
222  return _out;
223  }
224 
228  public: size_t Pack(char *_buffer) const;
229 
233  public: size_t Unpack(const char *_buffer);
234 
236  private: transport::Header header;
237 
238 #ifdef _WIN32
239 // Disable warning C4251 which is triggered by
240 // std::string
241 #pragma warning(push)
242 #pragma warning(disable: 4251)
243 #endif
244  private: std::string topic = "";
246 #ifdef _WIN32
247 #pragma warning(pop)
248 #endif
249  };
250 
257 
258  template <class T> class AdvertiseMessage
259  {
261  public: AdvertiseMessage() = default;
262 
266  public: AdvertiseMessage(const Header &_header,
267  const T &_publisher)
268  : header(_header),
269  publisher(_publisher)
270  {
271  }
272 
276  public: transport::Header Header() const
277  {
278  return this->header;
279  }
280 
284  public: T& Publisher()
285  {
286  return this->publisher;
287  }
288 
292  public: void SetHeader(const transport::Header &_header)
293  {
294  this->header = _header;
295  }
296 
300  public: void SetPublisher(const T &_publisher)
301  {
302  this->publisher = _publisher;
303  }
304 
307  public: size_t MsgLength() const
308  {
309  return this->header.HeaderLength() + this->publisher.MsgLength();
310  }
311 
315  public: size_t Pack(char *_buffer) const
316  {
317  // Pack the common part of any advertise message.
318  size_t len = this->header.Pack(_buffer);
319  if (len == 0)
320  return 0;
321 
322  _buffer += len;
323 
324  // Pack the part of the publisher.
325  if (this->publisher.Pack(_buffer) == 0)
326  return 0;
327 
328  return this->MsgLength();
329  }
330 
334  public: size_t Unpack(const char *_buffer)
335  {
336  // Unpack the message publisher.
337  if (this->publisher.Unpack(_buffer) == 0)
338  return 0;
339 
340  return this->publisher.MsgLength();
341  }
342 
346  public: friend std::ostream &operator<<(std::ostream &_out,
347  const AdvertiseMessage &_msg)
348  {
349  _out << _msg.header << _msg.publisher;
350  return _out;
351  }
352 
354  private: transport::Header header;
355 
357  private: T publisher;
358  };
359  }
360  }
361 }
362 
363 #endif
static const uint16_t FlagRelay
Definition: Packet.hh:48
friend std::ostream & operator<<(std::ostream &_out, const AdvertiseMessage &_msg)
Stream insertion operator.
Definition: Packet.hh:346
uint16_t Flags() const
Get the message flags.
friend std::ostream & operator<<(std::ostream &_out, const SubscriptionMsg &_msg)
Stream insertion operator.
Definition: Packet.hh:215
Header included in each discovery message containing the version of the discovery protocol...
Definition: Packet.hh:64
Subscription packet used in the discovery protocol for requesting information about a given topic...
Definition: Packet.hh:177
size_t Unpack(const char *_buffer)
Unserialize a stream of bytes into an AdvertiseMessage.
Definition: Packet.hh:334
static const uint8_t NewConnection
Definition: Packet.hh:44
T endl(T... args)
static const uint8_t HeartbeatType
Definition: Packet.hh:42
static const uint8_t SubType
Definition: Packet.hh:40
static const uint8_t ByeType
Definition: Packet.hh:43
void SetHeader(const transport::Header &_header)
Set the header of the message.
Definition: Packet.hh:292
size_t MsgLength() const
Get the total length of the message.
Definition: Packet.hh:307
static const uint8_t UnadvType
Definition: Packet.hh:41
STL class.
T at(T... args)
static const uint8_t EndConnection
Definition: Packet.hh:45
T & Publisher()
Get the publisher of this message.
Definition: Packet.hh:284
friend std::ostream & operator<<(std::ostream &_out, const Header &_header)
Stream insertion operator.
Definition: Packet.hh:140
uint8_t Type() const
Get the message type.
transport::Header Header() const
Get the message header.
static const uint8_t AdvType
Definition: Packet.hh:39
Advertise packet used in the discovery protocol to broadcast information about the node advertising a...
Definition: Packet.hh:258
void SetPublisher(const T &_publisher)
Set the publisher of this message.
Definition: Packet.hh:300
std::string PUuid() const
Get the process uuid.
static const std::vector< std::string > MsgTypesStr
Used for debugging the message type received/send.
Definition: Packet.hh:54
static const uint8_t Uninitialized
Definition: Packet.hh:38
AdvertiseMessage(const Header &_header, const T &_publisher)
Constructor.
Definition: Packet.hh:266
Definition: AdvertiseOptions.hh:28
std::string Topic() const
Get the topic.
STL class.
uint16_t Version() const
Get the discovery protocol version.
size_t Pack(char *_buffer) const
Serialize the advertise message.
Definition: Packet.hh:315
transport::Header Header() const
Get the message header.
Definition: Packet.hh:276