Ignition Transport

API Reference

8.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 // This whole file is deprecated in version 8 of Ignition Transport. Please
31 // remove this file in Version 9.
32 
33 namespace ignition
34 {
35  namespace transport
36  {
37  // Inline bracket to help doxygen filtering.
38  inline namespace IGNITION_TRANSPORT_VERSION_NAMESPACE {
39  //
40  // Message types.
41  static const uint8_t Uninitialized = 0;
42  static const uint8_t AdvType = 1;
43  static const uint8_t SubType = 2;
44  static const uint8_t UnadvType = 3;
45  static const uint8_t HeartbeatType = 4;
46  static const uint8_t ByeType = 5;
47  static const uint8_t NewConnection = 6;
48  static const uint8_t EndConnection = 7;
49 
50  // Flag set when a discovery message is relayed.
51  static const uint16_t FlagRelay = 0b000000000000'0001;
52  // Flag set when we want to avoid to relay a discovery message.
53  // This is used to avoid loops.
54  static const uint16_t FlagNoRelay = 0b000000000000'0010;
55 
58  {
59  "UNINITIALIZED", "ADVERTISE", "SUBSCRIBE", "UNADVERTISE", "HEARTBEAT",
60  "BYE", "NEW_CONNECTION", "END_CONNECTION"
61  };
62 
69  class IGNITION_TRANSPORT_VISIBLE Header
70  {
72  public: IGN_DEPRECATED(8) Header() = default;
73 
79  public: IGN_DEPRECATED(8) Header(const uint16_t _version,
80  const std::string &_pUuid,
81  const uint8_t _type,
82  const uint16_t _flags = 0);
83 
85  public: virtual ~Header() = default;
86 
90  public: uint16_t Version() const;
91 
95  public: std::string PUuid() const;
96 
100  public: uint8_t Type() const;
101 
105  public: uint16_t Flags() const;
106 
110  public: void SetVersion(const uint16_t _version);
111 
115  public: void SetPUuid(const std::string &_pUuid);
116 
120  public: void SetType(const uint8_t _type);
121 
125  public: void SetFlags(const uint16_t _flags);
126 
129  public: int HeaderLength() const;
130 
136  public: size_t Pack(char *_buffer) const;
137 
140  public: size_t Unpack(const char *_buffer);
141 
145  public: friend std::ostream &operator<<(std::ostream &_out,
146  const Header &_header)
147  {
148  _out << "--------------------------------------\n"
149  << "Header:" << std::endl
150  << "\tVersion: " << _header.Version() << "\n"
151  << "\tProcess UUID: " << _header.PUuid() << "\n"
152  << "\tType: " << MsgTypesStr.at(_header.Type()) << "\n"
153  << "\tFlags: " << _header.Flags() << "\n";
154  return _out;
155  }
156 
158  private: uint16_t version = 0;
159 
160 #ifdef _WIN32
161 // Disable warning C4251 which is triggered by
162 // std::string
163 #pragma warning(push)
164 #pragma warning(disable: 4251)
165 #endif
166  private: std::string pUuid = "";
168 #ifdef _WIN32
169 #pragma warning(pop)
170 #endif
171 
173  private: uint8_t type = Uninitialized;
174 
176  private: uint16_t flags = 0;
177  };
178 
184  class IGNITION_TRANSPORT_VISIBLE SubscriptionMsg
185  {
187  public: IGN_DEPRECATED(8) SubscriptionMsg() = default;
188 
192  public: IGN_DEPRECATED(8) SubscriptionMsg(
193  const transport::Header &_header,
194  const std::string &_topic);
195 
199  public: transport::Header Header() const;
200 
204  public: std::string Topic() const;
205 
209  public: void SetHeader(const transport::Header &_header);
210 
214  public: void SetTopic(const std::string &_topic);
215 
218  public: size_t MsgLength() const;
219 
223  public: friend std::ostream &operator<<(std::ostream &_out,
224  const SubscriptionMsg &_msg)
225  {
226  _out << _msg.Header()
227  << "Body:" << std::endl
228  << "\tTopic: [" << _msg.Topic() << "]" << std::endl;
229 
230  return _out;
231  }
232 
236  public: size_t Pack(char *_buffer) const;
237 
241  public: size_t Unpack(const char *_buffer);
242 
244  private: transport::Header header;
245 
246 #ifdef _WIN32
247 // Disable warning C4251 which is triggered by std::string
248 #pragma warning(push)
249 #pragma warning(disable: 4251)
250 #endif
251  private: std::string topic = "";
253 #ifdef _WIN32
254 #pragma warning(pop)
255 #endif
256  };
257 
266  template <class T> class AdvertiseMessage
267  {
269  public: IGN_DEPRECATED(8) AdvertiseMessage() = default;
270 
274  public: IGN_DEPRECATED(8) AdvertiseMessage(const Header &_header,
275  const T &_publisher)
276  : header(_header),
277  publisher(_publisher)
278  {
279  }
280 
284  public: transport::Header Header() const
285  {
286  return this->header;
287  }
288 
292  public: T& Publisher()
293  {
294  return this->publisher;
295  }
296 
300  public: void SetHeader(const transport::Header &_header)
301  {
302  this->header = _header;
303  }
304 
308  public: void SetPublisher(const T &_publisher)
309  {
310  this->publisher = _publisher;
311  }
312 
315  public: size_t MsgLength() const
316  {
317  return this->header.HeaderLength() + this->publisher.MsgLength();
318  }
319 
323  public: size_t Pack(char *_buffer) const
324  {
325  // Pack the common part of any advertise message.
326  size_t len = this->header.Pack(_buffer);
327  if (len == 0)
328  return 0;
329 
330  _buffer += len;
331 
332  // Pack the part of the publisher.
333  if (this->publisher.Pack(_buffer) == 0)
334  return 0;
335 
336  return this->MsgLength();
337  }
338 
342  public: size_t Unpack(const char *_buffer)
343  {
344  // Unpack the message publisher.
345  if (this->publisher.Unpack(_buffer) == 0)
346  return 0;
347 
348  return this->publisher.MsgLength();
349  }
350 
353  public: void SetFromDiscovery(const msgs::Discovery &_msg)
354  {
355  this->publisher.SetFromDiscovery(_msg);
356  }
357 
361  public: friend std::ostream &operator<<(std::ostream &_out,
362  const AdvertiseMessage &_msg)
363  {
364  _out << _msg.header << _msg.publisher;
365  return _out;
366  }
367 
369  private: transport::Header header;
370 
372  private: T publisher;
373  };
374  }
375  }
376 }
377 
378 #endif
static const uint16_t FlagRelay
Definition: Packet.hh:51
friend std::ostream & operator<<(std::ostream &_out, const AdvertiseMessage &_msg)
Stream insertion operator.
Definition: Packet.hh:361
uint16_t Flags() const
Get the message flags.
friend std::ostream & operator<<(std::ostream &_out, const SubscriptionMsg &_msg)
Stream insertion operator.
Definition: Packet.hh:223
Header included in each discovery message containing the version of the discovery protocol...
Definition: Packet.hh:69
Subscription packet used in the discovery protocol for requesting information about a given topic...
Definition: Packet.hh:184
size_t Unpack(const char *_buffer)
Unserialize a stream of bytes into an AdvertiseMessage.
Definition: Packet.hh:342
static const uint8_t NewConnection
Definition: Packet.hh:47
T endl(T... args)
static const uint8_t HeartbeatType
Definition: Packet.hh:45
static const uint8_t SubType
Definition: Packet.hh:43
static const uint8_t ByeType
Definition: Packet.hh:46
void SetHeader(const transport::Header &_header)
Set the header of the message.
Definition: Packet.hh:300
size_t MsgLength() const
Get the total length of the message.
Definition: Packet.hh:315
static const uint8_t UnadvType
Definition: Packet.hh:44
STL class.
T at(T... args)
static const uint8_t EndConnection
Definition: Packet.hh:48
T & Publisher()
Get the publisher of this message.
Definition: Packet.hh:292
friend std::ostream & operator<<(std::ostream &_out, const Header &_header)
Stream insertion operator.
Definition: Packet.hh:145
uint8_t Type() const
Get the message type.
transport::Header Header() const
Get the message header.
static const uint8_t AdvType
Definition: Packet.hh:42
Advertise packet used in the discovery protocol to broadcast information about the node advertising a...
Definition: Packet.hh:266
void SetPublisher(const T &_publisher)
Set the publisher of this message.
Definition: Packet.hh:308
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:57
static const uint8_t Uninitialized
Definition: Packet.hh:41
void SetFromDiscovery(const msgs::Discovery &_msg)
Set from discovery message.
Definition: Packet.hh:353
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:323
transport::Header Header() const
Get the message header.
Definition: Packet.hh:284