All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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 
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 NewConnection = 6;
41  static const uint8_t EndConnection = 7;
42 
44  static const std::vector<std::string> MsgTypesStr =
45  {
46  "UNINITIALIZED", "ADVERTISE", "SUBSCRIBE", "UNADVERTISE", "HEARTBEAT",
47  "BYE", "NEW_CONNECTION", "END_CONNECTION"
48  };
49 
53  // of message (ADV, SUB, ... ) and optional flags.
55  {
57  public: Header() = default;
58 
64  public: Header(const uint16_t _version,
65  const std::string &_pUuid,
66  const uint8_t _type,
67  const uint16_t _flags = 0);
68 
70  public: virtual ~Header() = default;
71 
75  public: uint16_t Version() const;
76 
80  public: std::string PUuid() const;
81 
85  public: uint8_t Type() const;
86 
90  public: uint16_t Flags() const;
91 
95  public: void SetVersion(const uint16_t _version);
96 
100  public: void SetPUuid(const std::string &_pUuid);
101 
105  public: void SetType(const uint8_t _type);
106 
110  public: void SetFlags(const uint16_t _flags);
111 
114  public: int HeaderLength() const;
115 
121  public: size_t Pack(char *_buffer) const;
122 
125  public: size_t Unpack(const char *_buffer);
126 
130  public: friend std::ostream &operator<<(std::ostream &_out,
131  const Header &_header)
132  {
133  _out << "--------------------------------------\n"
134  << "Header:" << std::endl
135  << "\tVersion: " << _header.Version() << "\n"
136  << "\tProcess UUID: " << _header.PUuid() << "\n"
137  << "\tType: " << MsgTypesStr.at(_header.Type()) << "\n"
138  << "\tFlags: " << _header.Flags() << "\n";
139  return _out;
140  }
141 
143  private: uint16_t version = 0;
144 
146  private: std::string pUuid = "";
147 
149  private: uint8_t type = Uninitialized;
150 
152  private: uint16_t flags = 0;
153  };
154 
159  {
161  public: SubscriptionMsg() = default;
162 
166  public: SubscriptionMsg(const transport::Header &_header,
167  const std::string &_topic);
168 
172  public: transport::Header Header() const;
173 
177  public: std::string Topic() const;
178 
182  public: void SetHeader(const transport::Header &_header);
183 
187  public: void SetTopic(const std::string &_topic);
188 
191  public: size_t MsgLength() const;
192 
196  public: friend std::ostream &operator<<(std::ostream &_out,
197  const SubscriptionMsg &_msg)
198  {
199  _out << _msg.Header()
200  << "Body:" << std::endl
201  << "\tTopic: [" << _msg.Topic() << "]" << std::endl;
202 
203  return _out;
204  }
205 
209  public: size_t Pack(char *_buffer) const;
210 
214  public: size_t Unpack(const char *_buffer);
215 
217  private: transport::Header header;
218 
220  private: std::string topic = "";
221  };
222 
229 
231  {
233  public: AdvertiseMessage() = default;
234 
238  public: AdvertiseMessage(const Header &_header,
239  const T &_publisher)
240  : header(_header),
241  publisher(_publisher)
242  {
243  }
244 
248  public: transport::Header Header() const
249  {
250  return this->header;
251  }
252 
256  public: T& Publisher()
257  {
258  return this->publisher;
259  }
260 
264  public: void SetHeader(const transport::Header &_header)
265  {
266  this->header = _header;
267  }
268 
272  public: void SetPublisher(const T &_publisher)
273  {
274  this->publisher = _publisher;
275  }
276 
279  public: size_t MsgLength() const
280  {
281  return this->header.HeaderLength() + this->publisher.MsgLength();
282  }
283 
287  public: size_t Pack(char *_buffer) const
288  {
289  // Pack the common part of any advertise message.
290  size_t len = this->header.Pack(_buffer);
291  if (len == 0)
292  return 0;
293 
294  _buffer += len;
295 
296  // Pack the part of the publisher.
297  if (this->publisher.Pack(_buffer) == 0)
298  return 0;
299 
300  return this->MsgLength();
301  }
302 
306  public: size_t Unpack(const char *_buffer)
307  {
308  // Unpack the message publisher.
309  if (this->publisher.Unpack(_buffer) == 0)
310  return 0;
311 
312  return this->publisher.MsgLength();
313  }
314 
318  public: friend std::ostream &operator<<(std::ostream &_out,
319  const AdvertiseMessage &_msg)
320  {
321  _out << _msg.header << _msg.publisher;
322  return _out;
323  }
324 
326  private: transport::Header header;
327 
329  private: T publisher;
330  };
331  }
332 }
333 
334 #endif
std::string Topic() const
Get the topic.
uint16_t Version() const
Get the discovery protocol version.
AdvertiseMessage(const Header &_header, const T &_publisher)
Constructor.
Definition: Packet.hh:238
friend std::ostream & operator<<(std::ostream &_out, const AdvertiseMessage &_msg)
Stream insertion operator.
Definition: Packet.hh:318
static const std::vector< std::string > MsgTypesStr
Used for debugging the message type received/send.
Definition: Packet.hh:44
uint16_t Flags() const
Get the message flags.
friend std::ostream & operator<<(std::ostream &_out, const SubscriptionMsg &_msg)
Stream insertion operator.
Definition: Packet.hh:196
Header included in each discovery message containing the version of the discovery protocol...
Definition: Packet.hh:54
Subscription packet used in the discovery protocol for requesting information about a given topic...
Definition: Packet.hh:158
std::string PUuid() const
Get the process uuid.
transport::Header Header() const
Get the message header.
static const uint8_t NewConnection
Definition: Packet.hh:40
static const uint8_t HeartbeatType
Definition: Packet.hh:38
size_t Pack(char *_buffer) const
Serialize the advertise message.
Definition: Packet.hh:287
static const uint8_t ByeType
Definition: Packet.hh:39
static const uint8_t Uninitialized
Definition: Packet.hh:34
size_t Unpack(const char *_buffer)
Unserialize a stream of bytes into an AdvertiseMessage.
Definition: Packet.hh:306
void SetPublisher(const T &_publisher)
Set the publisher of this message.
Definition: Packet.hh:272
friend std::ostream & operator<<(std::ostream &_out, const Header &_header)
Stream insertion operator.
Definition: Packet.hh:130
static const uint8_t EndConnection
Definition: Packet.hh:41
static const uint8_t SubType
Definition: Packet.hh:36
transport::Header Header() const
Get the message header.
Definition: Packet.hh:248
Advertise packet used in the discovery protocol to broadcast information about the node advertising a...
Definition: Packet.hh:230
#define IGNITION_TRANSPORT_VISIBLE
Use to represent "symbol visible" if supported.
Definition: Helpers.hh:57
uint8_t Type() const
Get the message type.
size_t MsgLength() const
Get the total length of the message.
Definition: Packet.hh:279
static const uint8_t AdvType
Definition: Packet.hh:35
void SetHeader(const transport::Header &_header)
Set the header of the message.
Definition: Packet.hh:264
T & Publisher()
Get the publisher of this message.
Definition: Packet.hh:256
static const uint8_t UnadvType
Definition: Packet.hh:37