Gazebo Transport

API Reference

15.1.0
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 <atomic>
31#include <memory>
32#include <mutex>
33#include <optional>
34#include <string>
35#include <thread>
36#include <vector>
37#include <map>
38#include <unordered_set>
39
40#include "gz/transport/config.hh"
41#include "gz/transport/Export.hh"
49#include "gz/transport/Uuid.hh"
50
51namespace zenoh
52{
53 // Forward declarations.
54 class Querier;
55 class Session;
56}
57
58namespace gz::transport
59{
60 // Inline bracket to help doxygen filtering.
61 inline namespace GZ_TRANSPORT_VERSION_NAMESPACE {
62 //
63 // Forward declarations.
64 class IRepHandler;
65 class IReqHandler;
66 class Node;
67 class NodePrivate;
68
70 class NodeSharedPrivate;
71
75 class GZ_TRANSPORT_VISIBLE NodeShared
76 {
82 public: static NodeShared *Instance();
83
85 public: void RunReceptionTask();
86
98 public: bool Publish(const std::string &_topic,
99 char *_data,
100 const size_t _dataSize,
101 DeallocFunc *_ffn,
102 const std::string &_msgType);
103
105 public: void RecvMsgUpdate();
106
108 public: void SrvPublishThread();
109
114 public: struct HandlerInfo
115 {
120
125
127 public: bool haveLocal;
128
130 public: bool haveRaw;
131
132 // Friendship. This allows HandlerInfo to be created by
133 // CheckHandlerInfo()
134 friend class NodeShared;
135
136 // TODO(sloretz) private default constructor (visual studio 2017?)
137 };
138
146
155 public: struct SubscriberInfo : public HandlerInfo
156 {
158 // cppcheck-suppress unusedStructMember
159 public: bool haveRemote;
160
161 // Friendship declaration
162 friend class NodeShared;
163
164 // TODO(sloretz) private default constructor (visual studio 2017?)
165 };
166
176 const std::string &_topic,
177 const std::string &_msgType) const;
178
185 public: void TriggerCallbacks(
186 const MessageInfo &_info,
187 const std::string &_msgData,
188 const HandlerInfo &_handlerInfo);
189
197 public: void TriggerCallbacks(
198 const MessageInfo &_info,
199 const char *_msgData,
200 std::size_t _msgSize,
201 const HandlerInfo &_handlerInfo);
202
204 public: void RecvSrvRequest();
205
207 public: void RecvSrvResponse();
208
214 public: void SendPendingRemoteReqs(const std::string &_topic,
215 const std::string &_reqType,
216 const std::string &_repType);
217
220 public: void OnNewConnection(const MessagePublisher &_pub);
221
224 public: void OnNewDisconnection(const MessagePublisher &_pub);
225
228 public: void OnNewSrvConnection(const ServicePublisher &_pub);
229
232 public: void OnNewSrvDisconnection(const ServicePublisher &_pub);
233
236 public: void OnNewRegistration(const MessagePublisher &_pub);
237
240 public: void OnEndRegistration(const MessagePublisher &_pub);
241
243 public: void OnSubscribers();
244
250 // there is at least one publisher.
253 public: bool TopicPublishers(const std::string &_topic,
254 SrvAddresses_M &_publishers) const;
255
262 public: bool DiscoverService(const std::string &_topic) const;
263
269 public: bool AdvertisePublisher(const ServicePublisher &_publisher);
270
281 public: int RcvHwm();
282
293 public: int SndHwm();
294
301 public: void EnableStats(const std::string &_topic, bool _enable,
302 std::function<void(const TopicStatistics &_stats)> _cb);
303
310 public: std::optional<TopicStatistics> TopicStats(
311 const std::string &_topic) const;
312
319 public: void AddGlobalRelay(const std::string& _relayAddress);
320
325
329
330#ifdef HAVE_ZENOH
333 public: std::shared_ptr<zenoh::Session> Session();
334
340 public: void EnsureZenohSubscription(const std::string &_topic);
341
345 public: void MaybeRemoveZenohSubscription(const std::string &_topic);
346
358 GetOrDeclareZenohQuerier(const std::string &_service);
359#endif
360
369 public: bool Unsubscribe(const std::string &_topic,
370 const std::string &_nUuid,
371 const NodeOptions &_nOpt,
372 const std::string &_hUuid = "");
373
377 private: std::unordered_set<std::string> &TopicsSubscribed(
378 const std::string &_nUuid) const;
379
384 private: bool RemoveSubscribedTopic(const std::string &_topic,
385 const std::string &_nUuid);
386
392 public: bool RemoveHandlersFromPubQueue(const std::string &_topic,
393 const std::string &_nUuid);
394
402 public: bool RemoveHandlerFromPubQueue(const std::string &_topic,
403 const std::string &_nUuid,
404 const std::string &_hUuid);
405
413 public: bool SubscribeHelper(const std::string &_fullyQualifiedTopic,
414 const std::string &_nUuid);
415
419
422 public: std::string MyAddress() const;
423
427
431
435 protected: NodeShared();
436
438 protected: virtual ~NodeShared();
439
443 private: bool InitializeSockets();
444
448
451
454
455#ifdef _WIN32
456// Disable warning C4251 which is triggered by
457// std::unique_ptr
458#pragma warning(push)
459#pragma warning(disable: 4251)
460#endif
463
466
469
471 public: std::string discoveryIP = "239.255.0.7";
472
474 public: static const int kDefaultMsgDiscPort = 10317;
475
477 public: static const int kDefaultSrvDiscPort = 10318;
478
480 public: int msgDiscPort = kDefaultMsgDiscPort;
481
483 public: int srvDiscPort = kDefaultSrvDiscPort;
484
486 private: TopicStorage<MessagePublisher> connections;
487
489 private: std::vector<std::string> srvConnections;
490
493#ifdef _WIN32
494#pragma warning(pop)
495#endif
496
502 {
509 public: bool HasSubscriber(
510 const std::string &_fullyQualifiedTopic,
511 const std::string &_msgType) const;
512
518 public: bool HasSubscriber(
519 const std::string &_fullyQualifiedTopic) const;
520
528 const std::string &_fullyQualifiedTopic, const std::string &_nUuid)
529 const;
530
539 const std::string &_fullyQualifiedTopic,
540 const std::string &_msgTypeName) const;
541
550 const std::string &_fullyQualifiedTopic,
551 const std::string &_nUuid);
552
561 public: bool RemoveHandler(
562 const std::string &_fullyQualifiedTopic,
563 const std::string &_nUuid,
564 const std::string &_hUuid);
565
571 const std::string &_pUuid);
572
575
580 };
581
583
584#ifdef _WIN32
585// Disable warning C4251 which is triggered by
586// std::unique_ptr
587#pragma warning(push)
588#pragma warning(disable: 4251)
589#endif
592
595
597 public: int verbose;
598
601
604
607
610
613
615 private: std::unique_ptr<NodeSharedPrivate> dataPtr;
616#ifdef _WIN32
617#pragma warning(pop)
618#endif
619 private: friend Node;
620 private: friend NodePrivate;
621 };
622 }
623}
624#endif