Gazebo Transport

API Reference

8.5.0
gz/transport/Publisher.hh
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2015 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 GZ_TRANSPORT_PUBLISHER_HH_
19 #define GZ_TRANSPORT_PUBLISHER_HH_
20 
21 #include <gz/msgs/discovery.pb.h>
22 
23 #include <iostream>
24 #include <string>
25 
27 #include "gz/transport/config.hh"
28 #include "gz/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  // Forward declarations.
38  class MessagePublisherPrivate;
39 
44  class IGNITION_TRANSPORT_VISIBLE Publisher
45  {
47  public: Publisher() = default;
48 
55  public: Publisher(const std::string &_topic,
56  const std::string &_addr,
57  const std::string &_pUuid,
58  const std::string &_nUuid,
59  const AdvertiseOptions &_opts);
60 
63  public: Publisher(const Publisher &_other);
64 
66  public: virtual ~Publisher() = default;
67 
71  public: std::string Topic() const;
72 
76  public: std::string Addr() const;
77 
81  public: std::string PUuid() const;
82 
86  public: std::string NUuid() const;
87 
91  public: virtual const AdvertiseOptions &Options() const;
92 
96  public: void SetTopic(const std::string &_topic);
97 
101  public: void SetAddr(const std::string &_addr);
102 
106  public: void SetPUuid(const std::string &_pUuid);
107 
111  public: void SetNUuid(const std::string &_nUuid);
112 
116  public: void SetOptions(const AdvertiseOptions &_opts);
117 
123  public: virtual size_t IGN_DEPRECATED(8) Pack(char *_buffer) const;
124 
127  public: virtual size_t IGN_DEPRECATED(8) Unpack(const char *_buffer);
128 
131  public: virtual size_t IGN_DEPRECATED(8) MsgLength() const;
132 
135  public: virtual void FillDiscovery(msgs::Discovery &_msg) const;
136 
139  public: virtual void SetFromDiscovery(const msgs::Discovery &_msg);
140 
146  public: bool operator==(const Publisher &_pub) const;
147 
153  public: bool operator!=(const Publisher &_pub) const;
154 
158  public: Publisher &operator=(const Publisher &_other);
159 
163  public: friend std::ostream &operator<<(std::ostream &_out,
164  const Publisher &_msg)
165  {
166  _out << "Publisher:" << std::endl
167  << "\tTopic: [" << _msg.Topic() << "]" << std::endl
168  << "\tAddress: " << _msg.Addr() << std::endl
169  << "\tProcess UUID: " << _msg.PUuid() << std::endl
170  << "\tNode UUID: " << _msg.NUuid() << std::endl
171  << _msg.Options();
172 
173  return _out;
174  }
175 
179  protected: size_t IGN_DEPRECATED(8) PackInternal(char *_buffer) const;
180 
184  protected: size_t IGN_DEPRECATED(8) UnpackInternal(const char *_buffer);
185 
191  protected: size_t IGN_DEPRECATED(8) MsgLengthInternal() const;
192 
193 #ifdef _WIN32
194 // Disable warning C4251 which is triggered by
195 // std::string
196 #pragma warning(push)
197 #pragma warning(disable: 4251)
198 #endif
199  protected: std::string topic;
201 
203  protected: std::string addr;
204 
206  protected: std::string pUuid;
207 
209  protected: std::string nUuid;
210 #ifdef _WIN32
211 #pragma warning(pop)
212 #endif
213 
216  private: AdvertiseOptions opts;
217  };
218 
222  class IGNITION_TRANSPORT_VISIBLE MessagePublisher : public Publisher
223  {
225  public: MessagePublisher() = default;
226 
235  public: explicit MessagePublisher(const std::string &_topic,
236  const std::string &_addr,
237  const std::string &_ctrl,
238  const std::string &_pUuid,
239  const std::string &_nUuid,
240  const std::string &_msgTypeName,
241  const AdvertiseMessageOptions &_opts);
242 
245  public: MessagePublisher(const MessagePublisher &_other);
246 
248  public: virtual ~MessagePublisher() = default;
249 
250  // Documentation inherited.
251  public: virtual size_t IGN_DEPRECATED(8) Pack(char *_buffer) const;
252 
253  // Documentation inherited.
254  public: virtual size_t IGN_DEPRECATED(8) Unpack(const char *_buffer);
255 
256  // Documentation inherited.
257  public: virtual size_t IGN_DEPRECATED(8) MsgLength() const;
258 
263  public: std::string Ctrl() const;
264 
268  public: void SetCtrl(const std::string &_ctrl);
269 
272  public: std::string MsgTypeName() const;
273 
277  public: void SetMsgTypeName(const std::string &_msgTypeName);
278 
282  public: virtual const AdvertiseMessageOptions &Options() const;
283 
287  public: void SetOptions(const AdvertiseMessageOptions &_opts);
288 
291  public: virtual void FillDiscovery(msgs::Discovery &_msg) const final;
292 
295  public: virtual void SetFromDiscovery(const msgs::Discovery &_msg);
296 
300  public: friend std::ostream &operator<<(std::ostream &_out,
301  const MessagePublisher &_msg)
302  {
303  _out << "Publisher:" << std::endl
304  << "\tTopic: [" << _msg.Topic() << "]" << std::endl
305  << "\tAddress: " << _msg.Addr() << std::endl
306  << "\tProcess UUID: " << _msg.PUuid() << std::endl
307  << "\tNode UUID: " << _msg.NUuid() << std::endl
308  << "\tControl address: " << _msg.Ctrl() << std::endl
309  << "\tMessage type: " << _msg.MsgTypeName() << std::endl
310  << _msg.Options();
311  return _out;
312  }
313 
319  public: bool operator==(const MessagePublisher &_pub) const;
320 
326  public: bool operator!=(const MessagePublisher &_pub) const;
327 
331  public: MessagePublisher &operator=(const MessagePublisher &_other);
332 
333 #ifdef _WIN32
334 // Disable warning C4251 which is triggered by
335 // std::unique_ptr
336 #pragma warning(push)
337 #pragma warning(disable: 4251)
338 #endif
339  private: std::string ctrl;
341 
343  private: std::string msgTypeName;
344 #ifdef _WIN32
345 #pragma warning(pop)
346 #endif
347 
349  private: AdvertiseMessageOptions msgOpts;
350  };
351 
355  class IGNITION_TRANSPORT_VISIBLE ServicePublisher : public Publisher
356  {
358  public: ServicePublisher() = default;
359 
369  public: ServicePublisher(const std::string &_topic,
370  const std::string &_addr,
371  const std::string &_id,
372  const std::string &_pUuid,
373  const std::string &_nUuid,
374  const std::string &_reqType,
375  const std::string &_repType,
376  const AdvertiseServiceOptions &_opts);
377 
380  public: ServicePublisher(const ServicePublisher &_other);
381 
385  public: ServicePublisher &operator=(const ServicePublisher &_other)
386  = default;
387 
389  public: virtual ~ServicePublisher() = default;
390 
391  // Documentation inherited.
392  public: size_t IGN_DEPRECATED(8) Pack(char *_buffer) const;
393 
394  // Documentation inherited.
395  public: size_t IGN_DEPRECATED(8) Unpack(const char *_buffer);
396 
397  // Documentation inherited.
398  public: size_t IGN_DEPRECATED(8) MsgLength() const;
399 
403  public: std::string SocketId() const;
404 
408  public: void SetSocketId(const std::string &_socketId);
409 
413  public: std::string ReqTypeName() const;
414 
418  public: std::string RepTypeName() const;
419 
423  public: void SetReqTypeName(const std::string &_reqTypeName);
424 
428  public: void SetRepTypeName(const std::string &_repTypeName);
429 
433  public: virtual const AdvertiseServiceOptions& Options() const;
434 
438  public: void SetOptions(const AdvertiseServiceOptions &_opts);
439 
442  public: virtual void FillDiscovery(msgs::Discovery &_msg) const final;
443 
447  public: virtual void SetFromDiscovery(const msgs::Discovery &_msg);
448 
452  public: friend std::ostream &operator<<(std::ostream &_out,
453  const ServicePublisher &_msg)
454  {
455  _out << "Publisher:" << std::endl
456  << "\tTopic: [" << _msg.Topic() << "]" << std::endl
457  << "\tAddress: " << _msg.Addr() << std::endl
458  << "\tProcess UUID: " << _msg.PUuid() << std::endl
459  << "\tNode UUID: " << _msg.NUuid() << std::endl
460  << "\tSocket ID: " << _msg.SocketId() << std::endl
461  << "\tRequest type: " << _msg.ReqTypeName() << std::endl
462  << "\tResponse type: " << _msg.RepTypeName() << std::endl
463  << _msg.Options();
464 
465  return _out;
466  }
467 
473  public: bool operator==(const ServicePublisher &_srv) const;
474 
480  public: bool operator!=(const ServicePublisher &_srv) const;
481 
482 #ifdef _WIN32
483 // Disable warning C4251 which is triggered by
484 // std::string
485 #pragma warning(push)
486 #pragma warning(disable: 4251)
487 #endif
488  private: std::string socketId;
490 
492  private: std::string reqTypeName;
493 
495  private: std::string repTypeName;
496 #ifdef _WIN32
497 #pragma warning(pop)
498 #endif
499 
501  private: AdvertiseServiceOptions srvOpts;
502  };
503  }
504  }
505 }
506 
507 #endif
friend std::ostream & operator<<(std::ostream &_out, const Publisher &_msg)
Stream insertion operator.
Definition: gz/transport/Publisher.hh:163
bool operator!=(const MessagePublisher &_pub) const
Inequality operator. This function checks if the given message publisher does not have identical Topi...
bool operator==(const MessagePublisher &_pub) const
Equality operator. This function checks if the given message publisher has identical Topic,...
size_t PackInternal(char *_buffer) const
Serialize all fields except the advertise options. This is useful when we are serializing a derived c...
virtual const AdvertiseOptions & Options() const
Get the advertised options.
virtual size_t Pack(char *_buffer) const
Serialize the publisher. The caller has ownership of the buffer and is responsible for its [de]alloca...
Definition: gz/transport/AdvertiseOptions.hh:28
STL class.
std::string ReqTypeName() const
Get the name of the request's protobuf message advertised.
void SetReqTypeName(const std::string &_reqTypeName)
Set the name of the request's protobuf message advertised.
This class stores all the information about a publisher. It stores the topic name that publishes,...
Definition: gz/transport/Publisher.hh:44
virtual const AdvertiseServiceOptions & Options() const
Get the advertised options.
virtual ~ServicePublisher()=default
Destructor.
std::string addr
ZeroMQ address of the publisher.
Definition: gz/transport/Publisher.hh:203
virtual void SetFromDiscovery(const msgs::Discovery &_msg)
Populate a discovery message.
std::string PUuid() const
Get the process UUID of the publisher. return Process UUID.
size_t Unpack(const char *_buffer)
Unserialize the publisher.
virtual size_t Pack(char *_buffer) const
Serialize the publisher. The caller has ownership of the buffer and is responsible for its [de]alloca...
virtual void FillDiscovery(msgs::Discovery &_msg) const
Populate a discovery message.
virtual ~MessagePublisher()=default
Destructor.
size_t MsgLengthInternal() const
Get the total length of the message without counting the advertised options. This is useful when [un]...
bool operator==(const Publisher &_pub) const
Equality operator. This function checks if the given publisher has identical Topic,...
void SetNUuid(const std::string &_nUuid)
Set the node UUID of the publisher.
void SetCtrl(const std::string &_ctrl)
Set the ZeroMQ control address of the publisher.
This class stores all the information about a message publisher.
Definition: gz/transport/Publisher.hh:222
virtual void SetFromDiscovery(const msgs::Discovery &_msg)
Set data from a discovery message.
A class for customizing the publication options for a topic or service advertised....
Definition: gz/transport/AdvertiseOptions.hh:59
std::string Topic() const
Get the topic published by this publisher.
std::string Addr() const
Get the ZeroMQ address of the publisher.
std::string SocketId() const
Get the ZeroMQ socket ID used by this publisher.
friend std::ostream & operator<<(std::ostream &_out, const ServicePublisher &_msg)
Stream insertion operator.
Definition: gz/transport/Publisher.hh:452
bool operator!=(const ServicePublisher &_srv) const
Inequality operator. This function checks if the given service does not have identical Topic,...
A class for customizing the publication options for a topic advertised. E.g.: Set the rate of message...
Definition: gz/transport/AdvertiseOptions.hh:150
void SetMsgTypeName(const std::string &_msgTypeName)
Set the message type advertised by this publisher.
bool operator==(const ServicePublisher &_srv) const
Equality operator. This function checks if the given service has identical Topic, Addr,...
std::string topic
Topic name.
Definition: gz/transport/Publisher.hh:200
size_t MsgLength() const
Get the total length of the message.
std::string pUuid
Process UUID of the publisher.
Definition: gz/transport/Publisher.hh:206
STL class.
Publisher & operator=(const Publisher &_other)
Assignment operator.
std::string nUuid
Node UUID of the publisher.
Definition: gz/transport/Publisher.hh:209
void SetOptions(const AdvertiseServiceOptions &_opts)
Set the advertised options.
bool operator!=(const Publisher &_pub) const
Inequality operator. This function checks if the given publisher does not have identical Topic,...
void SetOptions(const AdvertiseOptions &_opts)
Set the advertised options.
std::string MsgTypeName() const
Get the message type advertised by this publisher.
virtual void FillDiscovery(msgs::Discovery &_msg) const final
Populate a discovery message.
size_t Pack(char *_buffer) const
Serialize the publisher. The caller has ownership of the buffer and is responsible for its [de]alloca...
MessagePublisher & operator=(const MessagePublisher &_other)
Assignment operator.
virtual ~Publisher()=default
Destructor.
ServicePublisher & operator=(const ServicePublisher &_other)=default
Assignment operator.
virtual size_t MsgLength() const
Get the total length of the message.
virtual const AdvertiseMessageOptions & Options() const
Get the advertised options.
void SetSocketId(const std::string &_socketId)
Set the ZeroMQ socket ID for this publisher.
Publisher()=default
Default constructor.
ServicePublisher()=default
Default constructor.
void SetOptions(const AdvertiseMessageOptions &_opts)
Set the advertised options.
virtual size_t Unpack(const char *_buffer)
Unserialize the publisher.
void SetTopic(const std::string &_topic)
Set the topic name published by this publisher.
void SetRepTypeName(const std::string &_repTypeName)
Set the name of the response's protobuf message advertised.
virtual void SetFromDiscovery(const msgs::Discovery &_msg)
Set data from a discovery message.
T endl(T... args)
void SetAddr(const std::string &_addr)
Set ZeroMQ address of the publisher.
size_t UnpackInternal(const char *_buffer)
Unserialize all fields except the advertise options. This is useful when we are unserializing a deriv...
This class stores all the information about a service publisher.
Definition: gz/transport/Publisher.hh:355
MessagePublisher()=default
Default constructor.
A class for customizing the publication options for a service advertised.
Definition: gz/transport/AdvertiseOptions.hh:247
virtual size_t Unpack(const char *_buffer)
Unserialize the publisher.
virtual size_t MsgLength() const
Get the total length of the message.
std::string Ctrl() const
Get the ZeroMQ control address. This address is used by the subscribers to notify the publisher about...
virtual void FillDiscovery(msgs::Discovery &_msg) const final
Populate a discovery message.
std::string RepTypeName() const
Get the name of the response's protobuf message advertised.
std::string NUuid() const
Get the node UUID of the publisher.
friend std::ostream & operator<<(std::ostream &_out, const MessagePublisher &_msg)
Stream insertion operator.
Definition: gz/transport/Publisher.hh:300
void SetPUuid(const std::string &_pUuid)
Set the process UUID of the publisher.