Ignition Transport

API Reference

4.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/Export.hh"
28
29namespace 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
45 {
46 "UNINITIALIZED", "ADVERTISE", "SUBSCRIBE", "UNADVERTISE", "HEARTBEAT",
47 "BYE", "NEW_CONNECTION", "END_CONNECTION"
48 };
49
53 // of message (ADV, SUB, ... ) and optional flags.
54 class IGNITION_TRANSPORT_VISIBLE Header
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
158 class IGNITION_TRANSPORT_VISIBLE SubscriptionMsg
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
230 template <class T> class AdvertiseMessage
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
T at(T... args)
Advertise packet used in the discovery protocol to broadcast information about the node advertising a...
Definition Packet.hh:231
AdvertiseMessage()=default
Constructor.
size_t MsgLength() const
Get the total length of the message.
Definition Packet.hh:279
void SetHeader(const transport::Header &_header)
Set the header of the message.
Definition Packet.hh:264
transport::Header Header() const
Get the message header.
Definition Packet.hh:248
friend std::ostream & operator<<(std::ostream &_out, const AdvertiseMessage &_msg)
Stream insertion operator.
Definition Packet.hh:318
T & Publisher()
Get the publisher of this message.
Definition Packet.hh:256
AdvertiseMessage(const Header &_header, const T &_publisher)
Constructor.
Definition Packet.hh:238
size_t Pack(char *_buffer) const
Serialize the advertise message.
Definition Packet.hh:287
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
Header included in each discovery message containing the version of the discovery protocol,...
Definition Packet.hh:55
friend std::ostream & operator<<(std::ostream &_out, const Header &_header)
Stream insertion operator.
Definition Packet.hh:130
Header(const uint16_t _version, const std::string &_pUuid, const uint8_t _type, const uint16_t _flags=0)
Constructor.
uint16_t Flags() const
Get the message flags.
Header()=default
Constructor.
int HeaderLength() const
Get the header length.
void SetVersion(const uint16_t _version)
Set the discovery protocol version.
uint8_t Type() const
Get the message type.
size_t Pack(char *_buffer) const
Serialize the header. The caller has ownership of the buffer and is responsible for its [de]allocatio...
uint16_t Version() const
Get the discovery protocol version.
virtual ~Header()=default
Destructor.
size_t Unpack(const char *_buffer)
Unserialize the header.
void SetType(const uint8_t _type)
Set the message type.
void SetPUuid(const std::string &_pUuid)
Set the process uuid.
void SetFlags(const uint16_t _flags)
Set the message flags.
std::string PUuid() const
Get the process uuid.
Subscription packet used in the discovery protocol for requesting information about a given topic.
Definition Packet.hh:159
size_t MsgLength() const
Get the total length of the message.
friend std::ostream & operator<<(std::ostream &_out, const SubscriptionMsg &_msg)
Stream insertion operator.
Definition Packet.hh:196
void SetHeader(const transport::Header &_header)
Set the header of the message.
std::string Topic() const
Get the topic.
transport::Header Header() const
Get the message header.
void SetTopic(const std::string &_topic)
Set the topic.
size_t Pack(char *_buffer) const
Serialize the subscription message.
SubscriptionMsg(const transport::Header &_header, const std::string &_topic)
Constructor.
size_t Unpack(const char *_buffer)
Unserialize a stream of bytes into a Sub.
SubscriptionMsg()=default
Constructor.
T endl(T... args)
static const uint8_t ByeType
Definition Packet.hh:39
static const uint8_t EndConnection
Definition Packet.hh:41
static const uint8_t Uninitialized
Definition Packet.hh:34
static const uint8_t UnadvType
Definition Packet.hh:37
static const uint8_t SubType
Definition Packet.hh:36
static const std::vector< std::string > MsgTypesStr
Used for debugging the message type received/send.
Definition Packet.hh:44
static const uint8_t HeartbeatType
Definition Packet.hh:38
static const uint8_t NewConnection
Definition Packet.hh:40
static const uint8_t AdvType
Definition Packet.hh:35
Definition AdvertiseOptions.hh:28