Ignition Transport

API Reference

6.0.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 
49  {
50  "UNINITIALIZED", "ADVERTISE", "SUBSCRIBE", "UNADVERTISE", "HEARTBEAT",
51  "BYE", "NEW_CONNECTION", "END_CONNECTION"
52  };
53 
57  // of message (ADV, SUB, ... ) and optional flags.
58  class IGNITION_TRANSPORT_VISIBLE Header
59  {
61  public: Header() = default;
62 
68  public: Header(const uint16_t _version,
69  const std::string &_pUuid,
70  const uint8_t _type,
71  const uint16_t _flags = 0);
72 
74  public: virtual ~Header() = default;
75 
79  public: uint16_t Version() const;
80 
84  public: std::string PUuid() const;
85 
89  public: uint8_t Type() const;
90 
94  public: uint16_t Flags() const;
95 
99  public: void SetVersion(const uint16_t _version);
100 
104  public: void SetPUuid(const std::string &_pUuid);
105 
109  public: void SetType(const uint8_t _type);
110 
114  public: void SetFlags(const uint16_t _flags);
115 
118  public: int HeaderLength() const;
119 
125  public: size_t Pack(char *_buffer) const;
126 
129  public: size_t Unpack(const char *_buffer);
130 
134  public: friend std::ostream &operator<<(std::ostream &_out,
135  const Header &_header)
136  {
137  _out << "--------------------------------------\n"
138  << "Header:" << std::endl
139  << "\tVersion: " << _header.Version() << "\n"
140  << "\tProcess UUID: " << _header.PUuid() << "\n"
141  << "\tType: " << MsgTypesStr.at(_header.Type()) << "\n"
142  << "\tFlags: " << _header.Flags() << "\n";
143  return _out;
144  }
145 
147  private: uint16_t version = 0;
148 
149 #ifdef _WIN32
150 // Disable warning C4251 which is triggered by
151 // std::string
152 #pragma warning(push)
153 #pragma warning(disable: 4251)
154 #endif
155  private: std::string pUuid = "";
157 #ifdef _WIN32
158 #pragma warning(pop)
159 #endif
160 
162  private: uint8_t type = Uninitialized;
163 
165  private: uint16_t flags = 0;
166  };
167 
171  class IGNITION_TRANSPORT_VISIBLE SubscriptionMsg
172  {
174  public: SubscriptionMsg() = default;
175 
179  public: SubscriptionMsg(const transport::Header &_header,
180  const std::string &_topic);
181 
185  public: transport::Header Header() const;
186 
190  public: std::string Topic() const;
191 
195  public: void SetHeader(const transport::Header &_header);
196 
200  public: void SetTopic(const std::string &_topic);
201 
204  public: size_t MsgLength() const;
205 
209  public: friend std::ostream &operator<<(std::ostream &_out,
210  const SubscriptionMsg &_msg)
211  {
212  _out << _msg.Header()
213  << "Body:" << std::endl
214  << "\tTopic: [" << _msg.Topic() << "]" << std::endl;
215 
216  return _out;
217  }
218 
222  public: size_t Pack(char *_buffer) const;
223 
227  public: size_t Unpack(const char *_buffer);
228 
230  private: transport::Header header;
231 
232 #ifdef _WIN32
233 // Disable warning C4251 which is triggered by
234 // std::string
235 #pragma warning(push)
236 #pragma warning(disable: 4251)
237 #endif
238  private: std::string topic = "";
240 #ifdef _WIN32
241 #pragma warning(pop)
242 #endif
243  };
244 
251 
252  template <class T> class AdvertiseMessage
253  {
255  public: AdvertiseMessage() = default;
256 
260  public: AdvertiseMessage(const Header &_header,
261  const T &_publisher)
262  : header(_header),
263  publisher(_publisher)
264  {
265  }
266 
270  public: transport::Header Header() const
271  {
272  return this->header;
273  }
274 
278  public: T& Publisher()
279  {
280  return this->publisher;
281  }
282 
286  public: void SetHeader(const transport::Header &_header)
287  {
288  this->header = _header;
289  }
290 
294  public: void SetPublisher(const T &_publisher)
295  {
296  this->publisher = _publisher;
297  }
298 
301  public: size_t MsgLength() const
302  {
303  return this->header.HeaderLength() + this->publisher.MsgLength();
304  }
305 
309  public: size_t Pack(char *_buffer) const
310  {
311  // Pack the common part of any advertise message.
312  size_t len = this->header.Pack(_buffer);
313  if (len == 0)
314  return 0;
315 
316  _buffer += len;
317 
318  // Pack the part of the publisher.
319  if (this->publisher.Pack(_buffer) == 0)
320  return 0;
321 
322  return this->MsgLength();
323  }
324 
328  public: size_t Unpack(const char *_buffer)
329  {
330  // Unpack the message publisher.
331  if (this->publisher.Unpack(_buffer) == 0)
332  return 0;
333 
334  return this->publisher.MsgLength();
335  }
336 
340  public: friend std::ostream &operator<<(std::ostream &_out,
341  const AdvertiseMessage &_msg)
342  {
343  _out << _msg.header << _msg.publisher;
344  return _out;
345  }
346 
348  private: transport::Header header;
349 
351  private: T publisher;
352  };
353  }
354  }
355 }
356 
357 #endif
friend std::ostream & operator<<(std::ostream &_out, const AdvertiseMessage &_msg)
Stream insertion operator.
Definition: Packet.hh:340
uint16_t Flags() const
Get the message flags.
friend std::ostream & operator<<(std::ostream &_out, const SubscriptionMsg &_msg)
Stream insertion operator.
Definition: Packet.hh:209
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:171
size_t Unpack(const char *_buffer)
Unserialize a stream of bytes into an AdvertiseMessage.
Definition: Packet.hh:328
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:286
size_t MsgLength() const
Get the total length of the message.
Definition: Packet.hh:301
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:278
friend std::ostream & operator<<(std::ostream &_out, const Header &_header)
Stream insertion operator.
Definition: Packet.hh:134
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:252
void SetPublisher(const T &_publisher)
Set the publisher of this message.
Definition: Packet.hh:294
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:48
static const uint8_t Uninitialized
Definition: Packet.hh:38
AdvertiseMessage(const Header &_header, const T &_publisher)
Constructor.
Definition: Packet.hh:260
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:309
transport::Header Header() const
Get the message header.
Definition: Packet.hh:270