Gazebo Transport

API Reference

12.2.1
gz/transport/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 
38 #include "gz/transport/config.hh"
39 #include "gz/transport/Export.hh"
48 #include "gz/transport/Uuid.hh"
49 
50 namespace gz
51 {
52  namespace 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 
132  HandlerInfo CheckHandlerInfo(const std::string &_topic) const;
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 
162  SubscriberInfo CheckSubscriberInfo(
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 
180  public: void RecvControlUpdate();
181 
183  public: void RecvSrvRequest();
184 
186  public: void RecvSrvResponse();
187 
193  public: void SendPendingRemoteReqs(const std::string &_topic,
194  const std::string &_reqType,
195  const std::string &_repType);
196 
199  public: void OnNewConnection(const MessagePublisher &_pub);
200 
203  public: void OnNewDisconnection(const MessagePublisher &_pub);
204 
207  public: void OnNewSrvConnection(const ServicePublisher &_pub);
208 
211  public: void OnNewSrvDisconnection(const ServicePublisher &_pub);
212 
215  public: void OnNewRegistration(const MessagePublisher &_pub);
216 
219  public: void OnEndRegistration(const MessagePublisher &_pub);
220 
226  // there is at least one publisher.
229  public: bool TopicPublishers(const std::string &_topic,
230  SrvAddresses_M &_publishers) const;
231 
238  public: bool DiscoverService(const std::string &_topic) const;
239 
245  public: bool AdvertisePublisher(const ServicePublisher &_publisher);
246 
257  public: int RcvHwm();
258 
269  public: int SndHwm();
270 
277  public: void EnableStats(const std::string &_topic, bool _enable,
278  std::function<void(const TopicStatistics &_stats)> _cb);
279 
286  public: std::optional<TopicStatistics> TopicStats(
287  const std::string &_topic) const;
288 
290  protected: NodeShared();
291 
293  protected: virtual ~NodeShared();
294 
298  private: bool InitializeSockets();
299 
303 
306 
308  public: Uuid replierId;
309 
310 #ifdef _WIN32
311 // Disable warning C4251 which is triggered by
312 // std::unique_ptr
313 #pragma warning(push)
314 #pragma warning(disable: 4251)
315 #endif
316  public: std::string pUuid;
318 
321 
323  public: mutable std::recursive_mutex mutex;
324 
326  public: std::string discoveryIP = "239.255.0.7";
327 
329  public: static const int kDefaultMsgDiscPort = 10317;
330 
332  public: static const int kDefaultSrvDiscPort = 10318;
333 
335  public: int msgDiscPort = kDefaultMsgDiscPort;
336 
338  public: int srvDiscPort = kDefaultSrvDiscPort;
339 
341  private: TopicStorage<MessagePublisher> connections;
342 
344  private: std::vector<std::string> srvConnections;
345 
348 #ifdef _WIN32
349 #pragma warning(pop)
350 #endif
351 
357  {
364  public: bool HasSubscriber(
365  const std::string &_fullyQualifiedTopic,
366  const std::string &_msgType) const;
367 
373  public: bool HasSubscriber(
374  const std::string &_fullyQualifiedTopic) const;
375 
383  public: std::vector<std::string> NodeUuids(
384  const std::string &_fullyQualifiedTopic,
385  const std::string &_msgTypeName) const;
386 
394  public: bool RemoveHandlersForNode(
395  const std::string &_fullyQualifiedTopic,
396  const std::string &_nUuid);
397 
400 
405  };
406 
408 
409 #ifdef _WIN32
410 // Disable warning C4251 which is triggered by
411 // std::unique_ptr
412 #pragma warning(push)
413 #pragma warning(disable: 4251)
414 #endif
415  public: HandlerStorage<IRepHandler> repliers;
417 
420 
422  public: int verbose;
423 
426 
429 
432 
435 
438 
440  private: std::unique_ptr<NodeSharedPrivate> dataPtr;
441 #ifdef _WIN32
442 #pragma warning(pop)
443 #endif
444  private: friend Node;
445  private: friend NodePrivate;
446  };
447  }
448  }
449 }
450 #endif