Gazebo Transport

API Reference

15.0.0~pre1
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 {
78 public: static NodeShared *Instance();
79
81 public: void RunReceptionTask();
82
94 public: bool Publish(const std::string &_topic,
95 char *_data,
96 const size_t _dataSize,
97 DeallocFunc *_ffn,
98 const std::string &_msgType);
99
101 public: void RecvMsgUpdate();
102
104 public: void SrvPublishThread();
105
110 public: struct HandlerInfo
111 {
116
121
123 public: bool haveLocal;
124
126 public: bool haveRaw;
127
128 // Friendship. This allows HandlerInfo to be created by
129 // CheckHandlerInfo()
130 friend class NodeShared;
131
132 // TODO(sloretz) private default constructor (visual studio 2017?)
133 };
134
142
151 public: struct SubscriberInfo : public HandlerInfo
152 {
154 // cppcheck-suppress unusedStructMember
155 public: bool haveRemote;
156
157 // Friendship declaration
158 friend class NodeShared;
159
160 // TODO(sloretz) private default constructor (visual studio 2017?)
161 };
162
172 const std::string &_topic,
173 const std::string &_msgType) const;
174
181 public: void TriggerCallbacks(
182 const MessageInfo &_info,
183 const std::string &_msgData,
184 const HandlerInfo &_handlerInfo);
185
187 public: void RecvSrvRequest();
188
190 public: void RecvSrvResponse();
191
197 public: void SendPendingRemoteReqs(const std::string &_topic,
198 const std::string &_reqType,
199 const std::string &_repType);
200
203 public: void OnNewConnection(const MessagePublisher &_pub);
204
207 public: void OnNewDisconnection(const MessagePublisher &_pub);
208
211 public: void OnNewSrvConnection(const ServicePublisher &_pub);
212
215 public: void OnNewSrvDisconnection(const ServicePublisher &_pub);
216
219 public: void OnNewRegistration(const MessagePublisher &_pub);
220
223 public: void OnEndRegistration(const MessagePublisher &_pub);
224
226 public: void OnSubscribers();
227
233 // there is at least one publisher.
236 public: bool TopicPublishers(const std::string &_topic,
237 SrvAddresses_M &_publishers) const;
238
245 public: bool DiscoverService(const std::string &_topic) const;
246
252 public: bool AdvertisePublisher(const ServicePublisher &_publisher);
253
264 public: int RcvHwm();
265
276 public: int SndHwm();
277
284 public: void EnableStats(const std::string &_topic, bool _enable,
285 std::function<void(const TopicStatistics &_stats)> _cb);
286
293 public: std::optional<TopicStatistics> TopicStats(
294 const std::string &_topic) const;
295
302 public: void AddGlobalRelay(const std::string& _relayAddress);
303
308
312
313#ifdef HAVE_ZENOH
316 public: std::shared_ptr<zenoh::Session> Session();
317#endif
318
327 public: bool Unsubscribe(const std::string &_topic,
328 const std::string &_nUuid,
329 const NodeOptions &_nOpt,
330 const std::string &_hUuid = "");
331
335 private: std::unordered_set<std::string> &TopicsSubscribed(
336 const std::string &_nUuid) const;
337
342 private: bool RemoveSubscribedTopic(const std::string &_topic,
343 const std::string &_nUuid);
344
350 public: bool RemoveHandlersFromPubQueue(const std::string &_topic,
351 const std::string &_nUuid);
352
360 public: bool RemoveHandlerFromPubQueue(const std::string &_topic,
361 const std::string &_nUuid,
362 const std::string &_hUuid);
363
371 public: bool SubscribeHelper(const std::string &_fullyQualifiedTopic,
372 const std::string &_nUuid);
373
377
380 public: std::string MyAddress() const;
381
385
389
391 protected: NodeShared();
392
394 protected: virtual ~NodeShared();
395
399 private: bool InitializeSockets();
400
404
407
410
411#ifdef _WIN32
412// Disable warning C4251 which is triggered by
413// std::unique_ptr
414#pragma warning(push)
415#pragma warning(disable: 4251)
416#endif
419
422
425
427 public: std::string discoveryIP = "239.255.0.7";
428
430 public: static const int kDefaultMsgDiscPort = 10317;
431
433 public: static const int kDefaultSrvDiscPort = 10318;
434
436 public: int msgDiscPort = kDefaultMsgDiscPort;
437
439 public: int srvDiscPort = kDefaultSrvDiscPort;
440
442 private: TopicStorage<MessagePublisher> connections;
443
445 private: std::vector<std::string> srvConnections;
446
449#ifdef _WIN32
450#pragma warning(pop)
451#endif
452
458 {
465 public: bool HasSubscriber(
466 const std::string &_fullyQualifiedTopic,
467 const std::string &_msgType) const;
468
474 public: bool HasSubscriber(
475 const std::string &_fullyQualifiedTopic) const;
476
484 const std::string &_fullyQualifiedTopic, const std::string &_nUuid)
485 const;
486
495 const std::string &_fullyQualifiedTopic,
496 const std::string &_msgTypeName) const;
497
506 const std::string &_fullyQualifiedTopic,
507 const std::string &_nUuid);
508
517 public: bool RemoveHandler(
518 const std::string &_fullyQualifiedTopic,
519 const std::string &_nUuid,
520 const std::string &_hUuid);
521
527 const std::string &_pUuid);
528
531
536 };
537
539
540#ifdef _WIN32
541// Disable warning C4251 which is triggered by
542// std::unique_ptr
543#pragma warning(push)
544#pragma warning(disable: 4251)
545#endif
548
551
553 public: int verbose;
554
557
560
563
566
569
571 private: std::unique_ptr<NodeSharedPrivate> dataPtr;
572#ifdef _WIN32
573#pragma warning(pop)
574#endif
575 private: friend Node;
576 private: friend NodePrivate;
577 };
578 }
579}
580#endif