Gazebo Transport

API Reference

11.4.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, 0)
23 #endif
24 #include <google/protobuf/message.h>
25 #ifdef _MSC_VER
26 #pragma warning(pop)
27 #endif
28 
29 #include <memory>
30 #include <mutex>
31 #include <optional>
32 #include <string>
33 #include <thread>
34 #include <vector>
35 #include <map>
36 
37 #include "gz/transport/config.hh"
38 #include "gz/transport/Export.hh"
47 #include "gz/transport/Uuid.hh"
48 
49 namespace ignition::transport
50 {
51  // Inline bracket to help doxygen filtering.
52  inline namespace IGNITION_TRANSPORT_VERSION_NAMESPACE {
53  //
54  // Forward declarations.
55  class Node;
56  class NodePrivate;
57 
59  class NodeSharedPrivate;
60 
64  class IGNITION_TRANSPORT_VISIBLE NodeShared
65  {
69  public: static NodeShared *Instance();
70 
72  public: void RunReceptionTask();
73 
85  public: bool Publish(const std::string &_topic,
86  char *_data,
87  const size_t _dataSize,
88  DeallocFunc *_ffn,
89  const std::string &_msgType);
90 
92  public: void RecvMsgUpdate();
93 
98  public: struct HandlerInfo
99  {
104 
109 
111  public: bool haveLocal;
112 
114  public: bool haveRaw;
115 
116  // Friendship. This allows HandlerInfo to be created by
117  // CheckHandlerInfo()
118  friend class NodeShared;
119 
120  // TODO(sloretz) private default constructor (visual studio 2017?)
121  };
122 
129  HandlerInfo CheckHandlerInfo(const std::string &_topic) const;
130 
139  public: struct SubscriberInfo : public HandlerInfo
140  {
142  // cppcheck-suppress unusedStructMember
143  public: bool haveRemote;
144 
145  // Friendship declaration
146  friend class NodeShared;
147 
148  // TODO(sloretz) private default constructor (visual studio 2017?)
149  };
150 
159  SubscriberInfo CheckSubscriberInfo(
160  const std::string &_topic,
161  const std::string &_msgType) const;
162 
169  public: void TriggerCallbacks(
170  const MessageInfo &_info,
171  const std::string &_msgData,
172  const HandlerInfo &_handlerInfo);
173 
177  public: void RecvControlUpdate();
178 
180  public: void RecvSrvRequest();
181 
183  public: void RecvSrvResponse();
184 
190  public: void SendPendingRemoteReqs(const std::string &_topic,
191  const std::string &_reqType,
192  const std::string &_repType);
193 
196  public: void OnNewConnection(const MessagePublisher &_pub);
197 
200  public: void OnNewDisconnection(const MessagePublisher &_pub);
201 
204  public: void OnNewSrvConnection(const ServicePublisher &_pub);
205 
208  public: void OnNewSrvDisconnection(const ServicePublisher &_pub);
209 
212  public: void OnNewRegistration(const MessagePublisher &_pub);
213 
216  public: void OnEndRegistration(const MessagePublisher &_pub);
217 
223  // there is at least one publisher.
226  public: bool TopicPublishers(const std::string &_topic,
227  SrvAddresses_M &_publishers) const;
228 
235  public: bool DiscoverService(const std::string &_topic) const;
236 
242  public: bool AdvertisePublisher(const ServicePublisher &_publisher);
243 
254  public: int RcvHwm();
255 
266  public: int SndHwm();
267 
274  public: void EnableStats(const std::string &_topic, bool _enable,
275  std::function<void(const TopicStatistics &_stats)> _cb);
276 
283  public: std::optional<TopicStatistics> TopicStats(
284  const std::string &_topic) const;
285 
287  protected: NodeShared();
288 
290  protected: virtual ~NodeShared();
291 
295  private: bool InitializeSockets();
296 
300 
303 
305  public: Uuid replierId;
306 
307 #ifdef _WIN32
308 // Disable warning C4251 which is triggered by
309 // std::unique_ptr
310 #pragma warning(push)
311 #pragma warning(disable: 4251)
312 #endif
313  public: std::string pUuid;
315 
318 
320  public: mutable std::recursive_mutex mutex;
321 
323  public: std::string discoveryIP = "239.255.0.7";
324 
326  public: static const int kDefaultMsgDiscPort = 10317;
327 
329  public: static const int kDefaultSrvDiscPort = 10318;
330 
332  public: int msgDiscPort = kDefaultMsgDiscPort;
333 
335  public: int srvDiscPort = kDefaultSrvDiscPort;
336 
338  private: TopicStorage<MessagePublisher> connections;
339 
341  private: std::vector<std::string> srvConnections;
342 
345 #ifdef _WIN32
346 #pragma warning(pop)
347 #endif
348 
354  {
361  public: bool HasSubscriber(
362  const std::string &_fullyQualifiedTopic,
363  const std::string &_msgType) const;
364 
370  public: bool HasSubscriber(
371  const std::string &_fullyQualifiedTopic) const;
372 
380  public: std::vector<std::string> NodeUuids(
381  const std::string &_fullyQualifiedTopic,
382  const std::string &_msgTypeName) const;
383 
391  public: bool RemoveHandlersForNode(
392  const std::string &_fullyQualifiedTopic,
393  const std::string &_nUuid);
394 
397 
402  };
403 
405 
406 #ifdef _WIN32
407 // Disable warning C4251 which is triggered by
408 // std::unique_ptr
409 #pragma warning(push)
410 #pragma warning(disable: 4251)
411 #endif
412  public: HandlerStorage<IRepHandler> repliers;
414 
417 
419  public: int verbose;
420 
423 
426 
429 
432 
435 
437  private: std::unique_ptr<NodeSharedPrivate> dataPtr;
438 #ifdef _WIN32
439 #pragma warning(pop)
440 #endif
441  private: friend Node;
442  private: friend NodePrivate;
443  };
444  }
445 }
446 #endif
std::string myReplierAddress
My replier service call address.
Definition: gz/transport/NodeShared.hh:431
HandlerStorage< ISubscriptionHandler > normal
Normal local subscriptions.
Definition: gz/transport/NodeShared.hh:396
void OnNewDisconnection(const MessagePublisher &_pub)
Callback executed when the discovery detects disconnections.
std::map< std::string, ISubscriptionHandler_M > localHandlers
This is a map of the standard local callback handlers. The key is the topic name, and the value is an...
Definition: gz/transport/NodeShared.hh:103
Encapsulates statistics for a single topic. The set of statistics include:
Definition: gz/transport/TopicStatistics.hh:111
This struct wraps up the two different types of subscription handlers: normal (deserialized) and raw ...
Definition: gz/transport/NodeShared.hh:353
This struct provides information about the Subscribers of a Publisher. It should only be retrieved us...
Definition: gz/transport/NodeShared.hh:139
HandlerInfo contains information about callback handlers which is useful for local publishers and mes...
Definition: gz/transport/NodeShared.hh:98
STL class.
void OnNewSrvConnection(const ServicePublisher &_pub)
Callback executed when the discovery detects a new service call.
std::string pUuid
Process UUID.
Definition: gz/transport/NodeShared.hh:314
std::optional< TopicStatistics > TopicStats(const std::string &_topic) const
Get the current statistics for a topic. Statistics must have been enabled using the EnableStatistics ...
std::string myControlAddress
My pub/sub control address.
Definition: gz/transport/NodeShared.hh:425
void SendPendingRemoteReqs(const std::string &_topic, const std::string &_reqType, const std::string &_repType)
Try to send all the requests for a given service call and a pair of request/response types.
bool HasSubscriber(const std::string &_fullyQualifiedTopic, const std::string &_msgType) const
Returns true if this wrapper contains any subscriber that matches the given topic name and message ty...
void RecvSrvResponse()
Method in charge of receiving the service call responses.
bool DiscoverService(const std::string &_topic) const
Pass through to bool Discovery::Discover(const std::string &_topic) const.
bool AdvertisePublisher(const ServicePublisher &_publisher)
Pass through to bool Advertise(const Pub &_publisher)
HandlerStorage< IReqHandler > requests
Pending service call requests.
Definition: gz/transport/NodeShared.hh:416
std::string discoveryIP
Default IP address used by the message discovery layer.
Definition: gz/transport/NodeShared.hh:323
std::recursive_mutex mutex
Mutex to guarantee exclusive access between all threads.
Definition: gz/transport/NodeShared.hh:320
void RecvSrvRequest()
Method in charge of receiving the service call requests.
static const int kDefaultSrvDiscPort
Default port used by the service discovery layer.
Definition: gz/transport/NodeShared.hh:329
void OnNewSrvDisconnection(const ServicePublisher &_pub)
Callback executed when a service call is no longer available.
virtual ~NodeShared()
Destructor.
This class stores all the information about a message publisher.
Definition: gz/transport/Publisher.hh:176
std::thread threadReception
thread in charge of receiving and handling incoming messages.
Definition: gz/transport/NodeShared.hh:317
int verbose
Print activity to stdout.
Definition: gz/transport/NodeShared.hh:419
A portable class for representing a Universally Unique Identifier.
Definition: gz/transport/Uuid.hh:43
void RecvMsgUpdate()
Method in charge of receiving the topic updates.
void OnEndRegistration(const MessagePublisher &_pub)
Callback executed when a remote subscriber unregisters.
int srvDiscPort
Port used by the service discovery layer.
Definition: gz/transport/NodeShared.hh:335
void OnNewConnection(const MessagePublisher &_pub)
Callback executed when the discovery detects new topics.
Uuid responseReceiverId
Response receiver socket identity.
Definition: gz/transport/NodeShared.hh:302
A class that allows a client to communicate with other peers. There are two main communication modes:...
Definition: gz/transport/Node.hh:93
STL class.
std::string myRequesterAddress
My requester service call address.
Definition: gz/transport/NodeShared.hh:428
void RunReceptionTask()
Receive data and control messages.
std::map< std::string, RawSubscriptionHandler_M > rawHandlers
This is a map of the raw local callback handlers. The key is the topic name, and the value is another...
Definition: gz/transport/NodeShared.hh:108
A class that provides information about the message received.
Definition: gz/transport/MessageInfo.hh:35
HandlerWrapper localSubscribers
Definition: gz/transport/NodeShared.hh:404
bool TopicPublishers(const std::string &_topic, SrvAddresses_M &_publishers) const
Pass through to bool Publishers(const std::string &_topic, Addresses_M<Pub> &_publishers) const.
std::string myAddress
My pub/sub address.
Definition: gz/transport/NodeShared.hh:422
bool haveRemote
True if this Publisher has any remote subscribers.
Definition: gz/transport/NodeShared.hh:143
void TriggerCallbacks(const MessageInfo &_info, const std::string &_msgData, const HandlerInfo &_handlerInfo)
Call the SubscriptionHandler callbacks (local and raw) for this NodeShared.
HandlerStorage< RawSubscriptionHandler > raw
Raw local subscriptions. Keeping these separate from localSubscriptions allows us to avoid an unneces...
Definition: gz/transport/NodeShared.hh:401
void RecvControlUpdate()
Method in charge of receiving the control updates (when a new remote subscriber notifies its presence...
STL class.
int RcvHwm()
Get the capacity of the buffer (High Water Mark) that stores incoming Ignition Transport messages....
TopicStorage< MessagePublisher > remoteSubscribers
Remote subscribers.
Definition: gz/transport/NodeShared.hh:344
Definition: gz/transport/AdvertiseOptions.hh:28
bool RemoveHandlersForNode(const std::string &_fullyQualifiedTopic, const std::string &_nUuid)
Remove the handlers for the given topic name that belong to a specific node.
This class stores all the information about a service publisher.
Definition: gz/transport/Publisher.hh:291
HandlerStorage< IRepHandler > repliers
Service call repliers.
Definition: gz/transport/NodeShared.hh:413
Uuid replierId
Replier socket identity.
Definition: gz/transport/NodeShared.hh:305
std::string hostAddr
IP address of this host.
Definition: gz/transport/NodeShared.hh:434
std::vector< std::string > NodeUuids(const std::string &_fullyQualifiedTopic, const std::string &_msgTypeName) const
Get a set of node UUIDs for subscribers in this wrapper that match the topic and message type criteri...
static const int kDefaultMsgDiscPort
Default port used by the message discovery layer.
Definition: gz/transport/NodeShared.hh:326
Store address information about topics and provide convenient methods for adding new topics,...
Definition: gz/transport/TopicStorage.hh:39
bool haveLocal
True iff there are any standard local subscribers.
Definition: gz/transport/NodeShared.hh:111
void(void *_data, void *_hint) DeallocFunc
Definition: gz/transport/TransportTypes.hh:169
HandlerInfo CheckHandlerInfo(const std::string &_topic) const
Get information about the local and raw subscribers that are attached to this NodeShared.
bool Publish(const std::string &_topic, char *_data, const size_t _dataSize, DeallocFunc *_ffn, const std::string &_msgType)
Publish data.
Private data for the Node class. This class should not be directly used. You should use the Node clas...
Definition: gz/transport/NodeShared.hh:64
void OnNewRegistration(const MessagePublisher &_pub)
Callback executed when a remote subscriber connects.
void EnableStats(const std::string &_topic, bool _enable, std::function< void(const TopicStatistics &_stats)> _cb)
Turn topic statistics on or off.
SubscriberInfo CheckSubscriberInfo(const std::string &_topic, const std::string &_msgType) const
Get information about the nodes that are subscribed to the publishers of this NodeShared.
bool haveRaw
True iff there are any raw local subscribers.
Definition: gz/transport/NodeShared.hh:114
static NodeShared * Instance()
NodeShared is a singleton. This method gets the NodeShared instance shared between all the nodes.
Class to store and manage service call handlers.
Definition: gz/transport/HandlerStorage.hh:37
int msgDiscPort
Port used by the message discovery layer.
Definition: gz/transport/NodeShared.hh:332
int SndHwm()
Get the capacity of the buffer (High Water Mark) that stores outgoing Ignition Transport messages....