Gazebo Transport

API Reference

15.0.2
NodeShared.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 GZ_TRANSPORT_NODESHARED_HH_
19#define GZ_TRANSPORT_NODESHARED_HH_
20
21#ifdef _MSC_VER
22#pragma warning(push)
23#pragma warning(disable: 4251)
24#endif
25#include <google/protobuf/message.h>
26#ifdef _MSC_VER
27#pragma warning(pop)
28#endif
29
30#include <memory>
31#include <mutex>
32#include <optional>
33#include <string>
34#include <thread>
35#include <vector>
36#include <map>
37#include <unordered_set>
38
39#include "gz/transport/config.hh"
40#include "gz/transport/Export.hh"
48#include "gz/transport/Uuid.hh"
49
50namespace zenoh
51{
52 // Forward declaration.
53 class Session;
54}
55
56namespace gz::transport
57{
58 // Inline bracket to help doxygen filtering.
59 inline namespace GZ_TRANSPORT_VERSION_NAMESPACE {
60 //
61 // Forward declarations.
62 class IRepHandler;
63 class IReqHandler;
64 class Node;
65 class NodePrivate;
66
68 class NodeSharedPrivate;
69
73 class GZ_TRANSPORT_VISIBLE NodeShared
74 {
80 public: static NodeShared *Instance();
81
83 public: void RunReceptionTask();
84
96 public: bool Publish(const std::string &_topic,
97 char *_data,
98 const size_t _dataSize,
99 DeallocFunc *_ffn,
100 const std::string &_msgType);
101
103 public: void RecvMsgUpdate();
104
106 public: void SrvPublishThread();
107
112 public: struct HandlerInfo
113 {
118
123
125 public: bool haveLocal;
126
128 public: bool haveRaw;
129
130 // Friendship. This allows HandlerInfo to be created by
131 // CheckHandlerInfo()
132 friend class NodeShared;
133
134 // TODO(sloretz) private default constructor (visual studio 2017?)
135 };
136
144
153 public: struct SubscriberInfo : public HandlerInfo
154 {
156 // cppcheck-suppress unusedStructMember
157 public: bool haveRemote;
158
159 // Friendship declaration
160 friend class NodeShared;
161
162 // TODO(sloretz) private default constructor (visual studio 2017?)
163 };
164
174 const std::string &_topic,
175 const std::string &_msgType) const;
176
183 public: void TriggerCallbacks(
184 const MessageInfo &_info,
185 const std::string &_msgData,
186 const HandlerInfo &_handlerInfo);
187
189 public: void RecvSrvRequest();
190
192 public: void RecvSrvResponse();
193
199 public: void SendPendingRemoteReqs(const std::string &_topic,
200 const std::string &_reqType,
201 const std::string &_repType);
202
205 public: void OnNewConnection(const MessagePublisher &_pub);
206
209 public: void OnNewDisconnection(const MessagePublisher &_pub);
210
213 public: void OnNewSrvConnection(const ServicePublisher &_pub);
214
217 public: void OnNewSrvDisconnection(const ServicePublisher &_pub);
218
221 public: void OnNewRegistration(const MessagePublisher &_pub);
222
225 public: void OnEndRegistration(const MessagePublisher &_pub);
226
228 public: void OnSubscribers();
229
235 // there is at least one publisher.
238 public: bool TopicPublishers(const std::string &_topic,
239 SrvAddresses_M &_publishers) const;
240
247 public: bool DiscoverService(const std::string &_topic) const;
248
254 public: bool AdvertisePublisher(const ServicePublisher &_publisher);
255
266 public: int RcvHwm();
267
278 public: int SndHwm();
279
286 public: void EnableStats(const std::string &_topic, bool _enable,
287 std::function<void(const TopicStatistics &_stats)> _cb);
288
295 public: std::optional<TopicStatistics> TopicStats(
296 const std::string &_topic) const;
297
304 public: void AddGlobalRelay(const std::string& _relayAddress);
305
310
314
315#ifdef HAVE_ZENOH
318 public: std::shared_ptr<zenoh::Session> Session();
319#endif
320
329 public: bool Unsubscribe(const std::string &_topic,
330 const std::string &_nUuid,
331 const NodeOptions &_nOpt,
332 const std::string &_hUuid = "");
333
337 private: std::unordered_set<std::string> &TopicsSubscribed(
338 const std::string &_nUuid) const;
339
344 private: bool RemoveSubscribedTopic(const std::string &_topic,
345 const std::string &_nUuid);
346
352 public: bool RemoveHandlersFromPubQueue(const std::string &_topic,
353 const std::string &_nUuid);
354
362 public: bool RemoveHandlerFromPubQueue(const std::string &_topic,
363 const std::string &_nUuid,
364 const std::string &_hUuid);
365
373 public: bool SubscribeHelper(const std::string &_fullyQualifiedTopic,
374 const std::string &_nUuid);
375
379
382 public: std::string MyAddress() const;
383
387
391
395 protected: NodeShared();
396
398 protected: virtual ~NodeShared();
399
403 private: bool InitializeSockets();
404
408
411
414
415#ifdef _WIN32
416// Disable warning C4251 which is triggered by
417// std::unique_ptr
418#pragma warning(push)
419#pragma warning(disable: 4251)
420#endif
423
426
429
431 public: std::string discoveryIP = "239.255.0.7";
432
434 public: static const int kDefaultMsgDiscPort = 10317;
435
437 public: static const int kDefaultSrvDiscPort = 10318;
438
440 public: int msgDiscPort = kDefaultMsgDiscPort;
441
443 public: int srvDiscPort = kDefaultSrvDiscPort;
444
446 private: TopicStorage<MessagePublisher> connections;
447
449 private: std::vector<std::string> srvConnections;
450
453#ifdef _WIN32
454#pragma warning(pop)
455#endif
456
462 {
469 public: bool HasSubscriber(
470 const std::string &_fullyQualifiedTopic,
471 const std::string &_msgType) const;
472
478 public: bool HasSubscriber(
479 const std::string &_fullyQualifiedTopic) const;
480
488 const std::string &_fullyQualifiedTopic, const std::string &_nUuid)
489 const;
490
499 const std::string &_fullyQualifiedTopic,
500 const std::string &_msgTypeName) const;
501
510 const std::string &_fullyQualifiedTopic,
511 const std::string &_nUuid);
512
521 public: bool RemoveHandler(
522 const std::string &_fullyQualifiedTopic,
523 const std::string &_nUuid,
524 const std::string &_hUuid);
525
531 const std::string &_pUuid);
532
535
540 };
541
543
544#ifdef _WIN32
545// Disable warning C4251 which is triggered by
546// std::unique_ptr
547#pragma warning(push)
548#pragma warning(disable: 4251)
549#endif
552
555
557 public: int verbose;
558
561
564
567
570
573
575 private: std::unique_ptr<NodeSharedPrivate> dataPtr;
576#ifdef _WIN32
577#pragma warning(pop)
578#endif
579 private: friend Node;
580 private: friend NodePrivate;
581 };
582 }
583}
584#endif