Gazebo Transport

API Reference

13.4.1
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"
50 #include "gz/transport/Uuid.hh"
51 
52 namespace gz::transport
53 {
54  // Inline bracket to help doxygen filtering.
55  inline namespace GZ_TRANSPORT_VERSION_NAMESPACE {
56  //
57  // Forward declarations.
58  class Node;
59  class NodePrivate;
60 
62  class NodeSharedPrivate;
63 
67  class GZ_TRANSPORT_VISIBLE NodeShared
68  {
72  public: static NodeShared *Instance();
73 
75  public: void RunReceptionTask();
76 
88  public: bool Publish(const std::string &_topic,
89  char *_data,
90  const size_t _dataSize,
91  DeallocFunc *_ffn,
92  const std::string &_msgType);
93 
95  public: void RecvMsgUpdate();
96 
101  public: struct HandlerInfo
102  {
107 
112 
114  public: bool haveLocal;
115 
117  public: bool haveRaw;
118 
119  // Friendship. This allows HandlerInfo to be created by
120  // CheckHandlerInfo()
121  friend class NodeShared;
122 
123  // TODO(sloretz) private default constructor (visual studio 2017?)
124  };
125 
133 
142  public: struct SubscriberInfo : public HandlerInfo
143  {
145  // cppcheck-suppress unusedStructMember
146  public: bool haveRemote;
147 
148  // Friendship declaration
149  friend class NodeShared;
150 
151  // TODO(sloretz) private default constructor (visual studio 2017?)
152  };
153 
163  const std::string &_topic,
164  const std::string &_msgType) const;
165 
172  public: void TriggerCallbacks(
173  const MessageInfo &_info,
174  const std::string &_msgData,
175  const HandlerInfo &_handlerInfo);
176 
178  public: void RecvSrvRequest();
179 
181  public: void RecvSrvResponse();
182 
188  public: void SendPendingRemoteReqs(const std::string &_topic,
189  const std::string &_reqType,
190  const std::string &_repType);
191 
194  public: void OnNewConnection(const MessagePublisher &_pub);
195 
198  public: void OnNewDisconnection(const MessagePublisher &_pub);
199 
202  public: void OnNewSrvConnection(const ServicePublisher &_pub);
203 
206  public: void OnNewSrvDisconnection(const ServicePublisher &_pub);
207 
210  public: void OnNewRegistration(const MessagePublisher &_pub);
211 
214  public: void OnEndRegistration(const MessagePublisher &_pub);
215 
217  public: void OnSubscribers();
218 
224  // there is at least one publisher.
227  public: bool TopicPublishers(const std::string &_topic,
228  SrvAddresses_M &_publishers) const;
229 
236  public: bool DiscoverService(const std::string &_topic) const;
237 
243  public: bool AdvertisePublisher(const ServicePublisher &_publisher);
244 
255  public: int RcvHwm();
256 
267  public: int SndHwm();
268 
275  public: void EnableStats(const std::string &_topic, bool _enable,
276  std::function<void(const TopicStatistics &_stats)> _cb);
277 
284  public: std::optional<TopicStatistics> TopicStats(
285  const std::string &_topic) const;
286 
293  public: void AddGlobalRelay(const std::string& _relayAddress);
294 
299 
308  public: bool Unsubscribe(const std::string &_topic,
309  const std::string &_nUuid,
310  const NodeOptions &_nOpt,
311  const std::string &_hUuid = "");
312 
316  private: std::unordered_set<std::string> &TopicsSubscribed(
317  const std::string &_nUuid) const;
318 
323  private: bool RemoveSubscribedTopic(const std::string &_topic,
324  const std::string &_nUuid);
325 
331  public: bool RemoveHandlersFromPubQueue(const std::string &_topic,
332  const std::string &_nUuid);
333 
341  public: bool RemoveHandlerFromPubQueue(const std::string &_topic,
342  const std::string &_nUuid,
343  const std::string &_hUuid);
344 
352  public: bool SubscribeHelper(const std::string &_fullyQualifiedTopic,
353  const std::string &_nUuid);
354 
356  protected: NodeShared();
357 
359  protected: virtual ~NodeShared();
360 
364  private: bool InitializeSockets();
365 
369 
372 
374  public: Uuid replierId;
375 
376 #ifdef _WIN32
377 // Disable warning C4251 which is triggered by
378 // std::unique_ptr
379 #pragma warning(push)
380 #pragma warning(disable: 4251)
381 #endif
384 
387 
389  public: mutable std::recursive_mutex mutex;
390 
392  public: std::string discoveryIP = "239.255.0.7";
393 
395  public: static const int kDefaultMsgDiscPort = 10317;
396 
398  public: static const int kDefaultSrvDiscPort = 10318;
399 
401  public: int msgDiscPort = kDefaultMsgDiscPort;
402 
404  public: int srvDiscPort = kDefaultSrvDiscPort;
405 
407  private: TopicStorage<MessagePublisher> connections;
408 
410  private: std::vector<std::string> srvConnections;
411 
414 #ifdef _WIN32
415 #pragma warning(pop)
416 #endif
417 
423  {
430  public: bool HasSubscriber(
431  const std::string &_fullyQualifiedTopic,
432  const std::string &_msgType) const;
433 
439  public: bool HasSubscriber(
440  const std::string &_fullyQualifiedTopic) const;
441 
448  public: bool HasSubscriberForNode(
449  const std::string &_fullyQualifiedTopic, const std::string &_nUuid)
450  const;
451 
460  const std::string &_fullyQualifiedTopic,
461  const std::string &_msgTypeName) const;
462 
470  public: bool RemoveHandlersForNode(
471  const std::string &_fullyQualifiedTopic,
472  const std::string &_nUuid);
473 
482  public: bool RemoveHandler(
483  const std::string &_fullyQualifiedTopic,
484  const std::string &_nUuid,
485  const std::string &_hUuid);
486 
492  const std::string &_pUuid);
493 
496 
501  };
502 
504 
505 #ifdef _WIN32
506 // Disable warning C4251 which is triggered by
507 // std::unique_ptr
508 #pragma warning(push)
509 #pragma warning(disable: 4251)
510 #endif
513 
516 
518  public: int verbose;
519 
522 
525 
528 
531 
534 
536  private: std::unique_ptr<NodeSharedPrivate> dataPtr;
537 #ifdef _WIN32
538 #pragma warning(pop)
539 #endif
540  private: friend Node;
541  private: friend NodePrivate;
542  };
543  }
544 }
545 #endif