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
30namespace gz
31{
32 namespace transport
33 {
34 // Inline bracket to help doxygen filtering.
35 inline namespace GZ_TRANSPORT_VERSION_NAMESPACE {
36
41 class GZ_TRANSPORT_VISIBLE Publisher
42 {
44 public: Publisher() = default;
45
52 public: Publisher(const std::string &_topic,
53 const std::string &_addr,
54 const std::string &_pUuid,
55 const std::string &_nUuid,
56 const AdvertiseOptions &_opts);
57
59 public: virtual ~Publisher() = default;
60
64 public: std::string Topic() const;
65
69 public: std::string Addr() const;
70
74 public: std::string PUuid() const;
75
79 public: std::string NUuid() const;
80
84 public: virtual const AdvertiseOptions &Options() const;
85
89 public: void SetTopic(const std::string &_topic);
90
94 public: void SetAddr(const std::string &_addr);
95
99 public: void SetPUuid(const std::string &_pUuid);
100
104 public: void SetNUuid(const std::string &_nUuid);
105
109 public: void SetOptions(const AdvertiseOptions &_opts);
110
113 public: virtual void FillDiscovery(msgs::Discovery &_msg) const;
114
117 public: virtual void SetFromDiscovery(const msgs::Discovery &_msg);
118
124 public: bool operator==(const Publisher &_pub) const;
125
131 public: bool operator!=(const Publisher &_pub) const;
132
136 public: friend std::ostream &operator<<(std::ostream &_out,
137 const Publisher &_msg)
138 {
139 _out << "Publisher:" << std::endl
140 << "\tTopic: [" << _msg.Topic() << "]" << std::endl
141 << "\tAddress: " << _msg.Addr() << std::endl
142 << "\tProcess UUID: " << _msg.PUuid() << std::endl
143 << "\tNode UUID: " << _msg.NUuid() << std::endl
144 << _msg.Options();
145
146 return _out;
147 }
148
149#ifdef _WIN32
150// Disable warning C4251 which is triggered by
151// std::string
152#pragma warning(push)
153#pragma warning(disable: 4251)
154#endif
156 protected: std::string topic;
157
159 protected: std::string addr;
160
162 protected: std::string pUuid;
163
165 protected: std::string nUuid;
166#ifdef _WIN32
167#pragma warning(pop)
168#endif
169
172 private: AdvertiseOptions opts;
173 };
174
178 class GZ_TRANSPORT_VISIBLE MessagePublisher : public Publisher
179 {
181 public: MessagePublisher() = default;
182
191 public: explicit MessagePublisher(const std::string &_topic,
192 const std::string &_addr,
193 const std::string &_ctrl,
194 const std::string &_pUuid,
195 const std::string &_nUuid,
196 const std::string &_msgTypeName,
197 const AdvertiseMessageOptions &_opts);
198
200 public: virtual ~MessagePublisher() = default;
201
206 public: std::string Ctrl() const;
207
211 public: void SetCtrl(const std::string &_ctrl);
212
215 public: std::string MsgTypeName() const;
216
220 public: void SetMsgTypeName(const std::string &_msgTypeName);
221
225 public: virtual const AdvertiseMessageOptions &Options() const;
226
230 public: void SetOptions(const AdvertiseMessageOptions &_opts);
231
234 public: virtual void FillDiscovery(msgs::Discovery &_msg) const final;
235
238 public: virtual void SetFromDiscovery(const msgs::Discovery &_msg);
239
243 public: friend std::ostream &operator<<(std::ostream &_out,
244 const MessagePublisher &_msg)
245 {
246 _out << "Publisher:" << std::endl
247 << "\tTopic: [" << _msg.Topic() << "]" << std::endl
248 << "\tAddress: " << _msg.Addr() << std::endl
249 << "\tProcess UUID: " << _msg.PUuid() << std::endl
250 << "\tNode UUID: " << _msg.NUuid() << std::endl
251 << "\tControl address: " << _msg.Ctrl() << std::endl
252 << "\tMessage type: " << _msg.MsgTypeName() << std::endl
253 << _msg.Options();
254 return _out;
255 }
256
262 public: bool operator==(const MessagePublisher &_pub) const;
263
269 public: bool operator!=(const MessagePublisher &_pub) const;
270
271#ifdef _WIN32
272// Disable warning C4251 which is triggered by
273// std::unique_ptr
274#pragma warning(push)
275#pragma warning(disable: 4251)
276#endif
278 private: std::string ctrl;
279
281 private: std::string msgTypeName;
282#ifdef _WIN32
283#pragma warning(pop)
284#endif
285
287 private: AdvertiseMessageOptions msgOpts;
288 };
289
293 class GZ_TRANSPORT_VISIBLE ServicePublisher : public Publisher
294 {
296 public: ServicePublisher() = default;
297
307 public: ServicePublisher(const std::string &_topic,
308 const std::string &_addr,
309 const std::string &_id,
310 const std::string &_pUuid,
311 const std::string &_nUuid,
312 const std::string &_reqType,
313 const std::string &_repType,
314 const AdvertiseServiceOptions &_opts);
315
317 public: virtual ~ServicePublisher() = default;
318
322 public: std::string SocketId() const;
323
327 public: void SetSocketId(const std::string &_socketId);
328
332 public: std::string ReqTypeName() const;
333
337 public: std::string RepTypeName() const;
338
342 public: void SetReqTypeName(const std::string &_reqTypeName);
343
347 public: void SetRepTypeName(const std::string &_repTypeName);
348
352 public: virtual const AdvertiseServiceOptions& Options() const;
353
357 public: void SetOptions(const AdvertiseServiceOptions &_opts);
358
361 public: virtual void FillDiscovery(msgs::Discovery &_msg) const final;
362
366 public: virtual void SetFromDiscovery(const msgs::Discovery &_msg);
367
371 public: friend std::ostream &operator<<(std::ostream &_out,
372 const ServicePublisher &_msg)
373 {
374 _out << "Publisher:" << std::endl
375 << "\tTopic: [" << _msg.Topic() << "]" << std::endl
376 << "\tAddress: " << _msg.Addr() << std::endl
377 << "\tProcess UUID: " << _msg.PUuid() << std::endl
378 << "\tNode UUID: " << _msg.NUuid() << std::endl
379 << "\tSocket ID: " << _msg.SocketId() << std::endl
380 << "\tRequest type: " << _msg.ReqTypeName() << std::endl
381 << "\tResponse type: " << _msg.RepTypeName() << std::endl
382 << _msg.Options();
383
384 return _out;
385 }
386
392 public: bool operator==(const ServicePublisher &_srv) const;
393
399 public: bool operator!=(const ServicePublisher &_srv) const;
400
401#ifdef _WIN32
402// Disable warning C4251 which is triggered by
403// std::string
404#pragma warning(push)
405#pragma warning(disable: 4251)
406#endif
408 private: std::string socketId;
409
411 private: std::string reqTypeName;
412
414 private: std::string repTypeName;
415#ifdef _WIN32
416#pragma warning(pop)
417#endif
418
420 private: AdvertiseServiceOptions srvOpts;
421 };
422 }
423 }
424}
425
426#endif