Gazebo Transport

API Reference

13.4.1
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 gz::transport
31 {
32  // Inline bracket to help doxygen filtering.
33  inline namespace GZ_TRANSPORT_VERSION_NAMESPACE {
34 
39  class GZ_TRANSPORT_VISIBLE Publisher
40  {
42  public: Publisher() = default;
43 
50  public: Publisher(const std::string &_topic,
51  const std::string &_addr,
52  const std::string &_pUuid,
53  const std::string &_nUuid,
54  const AdvertiseOptions &_opts);
55 
57  public: virtual ~Publisher() = default;
58 
62  public: std::string Topic() const;
63 
67  public: std::string Addr() const;
68 
72  public: std::string PUuid() const;
73 
77  public: std::string NUuid() const;
78 
82  public: virtual const AdvertiseOptions &Options() const;
83 
87  public: void SetTopic(const std::string &_topic);
88 
92  public: void SetAddr(const std::string &_addr);
93 
97  public: void SetPUuid(const std::string &_pUuid);
98 
102  public: void SetNUuid(const std::string &_nUuid);
103 
107  public: void SetOptions(const AdvertiseOptions &_opts);
108 
111  public: virtual void FillDiscovery(msgs::Discovery &_msg) const;
112 
115  public: virtual void SetFromDiscovery(const msgs::Discovery &_msg);
116 
122  public: bool operator==(const Publisher &_pub) const;
123 
129  public: bool operator!=(const Publisher &_pub) const;
130 
134  public: friend std::ostream &operator<<(std::ostream &_out,
135  const Publisher &_msg)
136  {
137  _out << "Publisher:" << std::endl
138  << "\tTopic: [" << _msg.Topic() << "]" << std::endl
139  << "\tAddress: " << _msg.Addr() << std::endl
140  << "\tProcess UUID: " << _msg.PUuid() << std::endl
141  << "\tNode UUID: " << _msg.NUuid() << std::endl
142  << _msg.Options();
143 
144  return _out;
145  }
146 
147 #ifdef _WIN32
148 // Disable warning C4251 which is triggered by
149 // std::string
150 #pragma warning(push)
151 #pragma warning(disable: 4251)
152 #endif
154  protected: std::string topic;
155 
157  protected: std::string addr;
158 
160  protected: std::string pUuid;
161 
163  protected: std::string nUuid;
164 #ifdef _WIN32
165 #pragma warning(pop)
166 #endif
167 
170  private: AdvertiseOptions opts;
171  };
172 
176  class GZ_TRANSPORT_VISIBLE MessagePublisher : public Publisher
177  {
179  public: MessagePublisher() = default;
180 
189  public: explicit MessagePublisher(const std::string &_topic,
190  const std::string &_addr,
191  const std::string &_ctrl,
192  const std::string &_pUuid,
193  const std::string &_nUuid,
194  const std::string &_msgTypeName,
195  const AdvertiseMessageOptions &_opts);
196 
198  public: virtual ~MessagePublisher() = default;
199 
204  public: std::string Ctrl() const;
205 
209  public: void SetCtrl(const std::string &_ctrl);
210 
213  public: std::string MsgTypeName() const;
214 
218  public: void SetMsgTypeName(const std::string &_msgTypeName);
219 
223  public: virtual const AdvertiseMessageOptions &Options() const;
224 
228  public: void SetOptions(const AdvertiseMessageOptions &_opts);
229 
232  public: virtual void FillDiscovery(msgs::Discovery &_msg) const final;
233 
236  public: virtual void SetFromDiscovery(const msgs::Discovery &_msg);
237 
241  public: friend std::ostream &operator<<(std::ostream &_out,
242  const MessagePublisher &_msg)
243  {
244  _out << "Publisher:" << std::endl
245  << "\tTopic: [" << _msg.Topic() << "]" << std::endl
246  << "\tAddress: " << _msg.Addr() << std::endl
247  << "\tProcess UUID: " << _msg.PUuid() << std::endl
248  << "\tNode UUID: " << _msg.NUuid() << std::endl
249  << "\tControl address: " << _msg.Ctrl() << std::endl
250  << "\tMessage type: " << _msg.MsgTypeName() << std::endl
251  << _msg.Options();
252  return _out;
253  }
254 
260  public: bool operator==(const MessagePublisher &_pub) const;
261 
267  public: bool operator!=(const MessagePublisher &_pub) const;
268 
269 #ifdef _WIN32
270 // Disable warning C4251 which is triggered by
271 // std::unique_ptr
272 #pragma warning(push)
273 #pragma warning(disable: 4251)
274 #endif
276  private: std::string ctrl;
277 
279  private: std::string msgTypeName;
280 #ifdef _WIN32
281 #pragma warning(pop)
282 #endif
283 
285  private: AdvertiseMessageOptions msgOpts;
286  };
287 
291  class GZ_TRANSPORT_VISIBLE ServicePublisher : public Publisher
292  {
294  public: ServicePublisher() = default;
295 
305  public: ServicePublisher(const std::string &_topic,
306  const std::string &_addr,
307  const std::string &_id,
308  const std::string &_pUuid,
309  const std::string &_nUuid,
310  const std::string &_reqType,
311  const std::string &_repType,
312  const AdvertiseServiceOptions &_opts);
313 
315  public: virtual ~ServicePublisher() = default;
316 
320  public: std::string SocketId() const;
321 
325  public: void SetSocketId(const std::string &_socketId);
326 
330  public: std::string ReqTypeName() const;
331 
335  public: std::string RepTypeName() const;
336 
340  public: void SetReqTypeName(const std::string &_reqTypeName);
341 
345  public: void SetRepTypeName(const std::string &_repTypeName);
346 
350  public: virtual const AdvertiseServiceOptions& Options() const;
351 
355  public: void SetOptions(const AdvertiseServiceOptions &_opts);
356 
359  public: virtual void FillDiscovery(msgs::Discovery &_msg) const final;
360 
364  public: virtual void SetFromDiscovery(const msgs::Discovery &_msg);
365 
369  public: friend std::ostream &operator<<(std::ostream &_out,
370  const ServicePublisher &_msg)
371  {
372  _out << "Publisher:" << std::endl
373  << "\tTopic: [" << _msg.Topic() << "]" << std::endl
374  << "\tAddress: " << _msg.Addr() << std::endl
375  << "\tProcess UUID: " << _msg.PUuid() << std::endl
376  << "\tNode UUID: " << _msg.NUuid() << std::endl
377  << "\tSocket ID: " << _msg.SocketId() << std::endl
378  << "\tRequest type: " << _msg.ReqTypeName() << std::endl
379  << "\tResponse type: " << _msg.RepTypeName() << std::endl
380  << _msg.Options();
381 
382  return _out;
383  }
384 
390  public: bool operator==(const ServicePublisher &_srv) const;
391 
397  public: bool operator!=(const ServicePublisher &_srv) const;
398 
399 #ifdef _WIN32
400 // Disable warning C4251 which is triggered by
401 // std::string
402 #pragma warning(push)
403 #pragma warning(disable: 4251)
404 #endif
406  private: std::string socketId;
407 
409  private: std::string reqTypeName;
410 
412  private: std::string repTypeName;
413 #ifdef _WIN32
414 #pragma warning(pop)
415 #endif
416 
418  private: AdvertiseServiceOptions srvOpts;
419  };
420  }
421 }
422 
423 #endif