Gazebo Transport

API Reference

8.5.0
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
50 {
51  namespace transport
52  {
53  // Inline bracket to help doxygen filtering.
54  inline namespace IGNITION_TRANSPORT_VERSION_NAMESPACE {
55  //
56  // Forward declarations.
57  class Node;
58  class NodePrivate;
59 
61  class NodeSharedPrivate;
62 
66  class IGNITION_TRANSPORT_VISIBLE NodeShared
67  {
71  public: static NodeShared *Instance();
72 
74  public: void RunReceptionTask();
75 
87  public: bool Publish(const std::string &_topic,
88  char *_data,
89  const size_t _dataSize,
90  DeallocFunc *_ffn,
91  const std::string &_msgType);
92 
94  public: void RecvMsgUpdate();
95 
100  public: struct HandlerInfo
101  {
106 
111 
113  public: bool haveLocal;
114 
116  public: bool haveRaw;
117 
118  // Friendship. This allows HandlerInfo to be created by
119  // CheckHandlerInfo()
120  friend class NodeShared;
121 
122  // TODO(sloretz) private default constructor (visual studio 2017?)
123  };
124 
131  HandlerInfo CheckHandlerInfo(const std::string &_topic) const;
132 
141  public: struct SubscriberInfo : public HandlerInfo
142  {
144  // cppcheck-suppress unusedStructMember
145  public: bool haveRemote;
146 
147  // Friendship declaration
148  friend class NodeShared;
149 
150  // TODO(sloretz) private default constructor (visual studio 2017?)
151  };
152 
161  SubscriberInfo CheckSubscriberInfo(
162  const std::string &_topic,
163  const std::string &_msgType) const;
164 
172  public: void IGN_DEPRECATED(8.0) TriggerSubscriberCallbacks(
173  const std::string &_topic,
174  const std::string &_msgData,
175  const std::string &_msgType,
176  const HandlerInfo &_handlerInfo);
177 
184  public: void TriggerCallbacks(
185  const MessageInfo &_info,
186  const std::string &_msgData,
187  const HandlerInfo &_handlerInfo);
188 
192  public: void RecvControlUpdate();
193 
195  public: void RecvSrvRequest();
196 
198  public: void RecvSrvResponse();
199 
205  public: void SendPendingRemoteReqs(const std::string &_topic,
206  const std::string &_reqType,
207  const std::string &_repType);
208 
211  public: void OnNewConnection(const MessagePublisher &_pub);
212 
215  public: void OnNewDisconnection(const MessagePublisher &_pub);
216 
219  public: void OnNewSrvConnection(const ServicePublisher &_pub);
220 
223  public: void OnNewSrvDisconnection(const ServicePublisher &_pub);
224 
227  public: void OnNewRegistration(const MessagePublisher &_pub);
228 
231  public: void OnEndRegistration(const MessagePublisher &_pub);
232 
238  // there is at least one publisher.
241  public: bool TopicPublishers(const std::string &_topic,
242  SrvAddresses_M &_publishers) const;
243 
250  public: bool DiscoverService(const std::string &_topic) const;
251 
257  public: bool AdvertisePublisher(const ServicePublisher &_publisher);
258 
269  public: int RcvHwm();
270 
281  public: int SndHwm();
282 
289  public: void EnableStats(const std::string &_topic, bool _enable,
290  std::function<void(const TopicStatistics &_stats)> _cb);
291 
298  public: std::optional<TopicStatistics> TopicStats(
299  const std::string &_topic) const;
300 
302  protected: NodeShared();
303 
305  protected: virtual ~NodeShared();
306 
310  private: bool InitializeSockets();
311 
315 
317  public: Uuid responseReceiverId;
318 
320  public: Uuid replierId;
321 
322 #ifdef _WIN32
323 // Disable warning C4251 which is triggered by
324 // std::unique_ptr
325 #pragma warning(push)
326 #pragma warning(disable: 4251)
327 #endif
328  public: std::string pUuid;
330 
333 
335  public: mutable std::recursive_mutex mutex;
336 
338  public: static const int kMsgDiscPort = 10317;
339 
341  public: static const int kSrvDiscPort = 10318;
342 
344  private: TopicStorage<MessagePublisher> connections;
345 
347  private: std::vector<std::string> srvConnections;
348 
351 #ifdef _WIN32
352 #pragma warning(pop)
353 #endif
354 
360  {
367  public: bool HasSubscriber(
368  const std::string &_fullyQualifiedTopic,
369  const std::string &_msgType) const;
370 
376  public: bool HasSubscriber(
377  const std::string &_fullyQualifiedTopic) const;
378 
386  public: std::vector<std::string> NodeUuids(
387  const std::string &_fullyQualifiedTopic,
388  const std::string &_msgTypeName) const;
389 
397  public: bool RemoveHandlersForNode(
398  const std::string &_fullyQualifiedTopic,
399  const std::string &_nUuid);
400 
403 
408  };
409 
411 
412 #ifdef _WIN32
413 // Disable warning C4251 which is triggered by
414 // std::unique_ptr
415 #pragma warning(push)
416 #pragma warning(disable: 4251)
417 #endif
418  public: HandlerStorage<IRepHandler> repliers;
420 
423 
425  public: int verbose;
426 
429 
432 
435 
438 
441 
443  private: std::unique_ptr<NodeSharedPrivate> dataPtr;
444 #ifdef _WIN32
445 #pragma warning(pop)
446 #endif
447  private: friend Node;
448  private: friend NodePrivate;
449  };
450  }
451  }
452 }
453 #endif
std::string myReplierAddress
My replier service call address.
Definition: gz/transport/NodeShared.hh:437
HandlerStorage< ISubscriptionHandler > normal
Normal local subscriptions.
Definition: gz/transport/NodeShared.hh:402
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:105
Encapsulates statistics for a single topic. The set of statistics include:
Definition: gz/transport/TopicStatistics.hh:113
This struct wraps up the two different types of subscription handlers: normal (deserialized) and raw ...
Definition: gz/transport/NodeShared.hh:359
This struct provides information about the Subscribers of a Publisher. It should only be retrieved us...
Definition: gz/transport/NodeShared.hh:141
Definition: gz/transport/AdvertiseOptions.hh:28
HandlerInfo contains information about callback handlers which is useful for local publishers and mes...
Definition: gz/transport/NodeShared.hh:100
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:329
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:431
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.
static const int kSrvDiscPort
Port used by the service discovery layer.
Definition: gz/transport/NodeShared.hh:341
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:422
std::recursive_mutex mutex
Mutex to guarantee exclusive access between all threads.
Definition: gz/transport/NodeShared.hh:335
static const int kMsgDiscPort
Port used by the message discovery layer.
Definition: gz/transport/NodeShared.hh:338
void RecvSrvRequest()
Method in charge of receiving the service call requests.
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:222
std::thread threadReception
thread in charge of receiving and handling incoming messages.
Definition: gz/transport/NodeShared.hh:332
int verbose
Print activity to stdout.
Definition: gz/transport/NodeShared.hh:425
A portable class for representing a Universally Unique Identifier.
Definition: gz/transport/Uuid.hh:45
void RecvMsgUpdate()
Method in charge of receiving the topic updates.
void OnEndRegistration(const MessagePublisher &_pub)
Callback executed when a remote subscriber unregisters.
void TriggerSubscriberCallbacks(const std::string &_topic, const std::string &_msgData, const std::string &_msgType, const HandlerInfo &_handlerInfo)
Call the SubscriptionHandler callbacks (local and raw) for this NodeShared.
void OnNewConnection(const MessagePublisher &_pub)
Callback executed when the discovery detects new topics.
Uuid responseReceiverId
Response receiver socket identity.
Definition: gz/transport/NodeShared.hh:317
A class that allows a client to communicate with other peers. There are two main communication modes:...
Definition: gz/transport/Node.hh:95
STL class.
std::string myRequesterAddress
My requester service call address.
Definition: gz/transport/NodeShared.hh:434
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:110
A class that provides information about the message received.
Definition: gz/transport/MessageInfo.hh:37
HandlerWrapper localSubscribers
Definition: gz/transport/NodeShared.hh:410
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:428
bool haveRemote
True if this Publisher has any remote subscribers.
Definition: gz/transport/NodeShared.hh:145
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:407
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:350
STL namespace.
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:355
HandlerStorage< IRepHandler > repliers
Service call repliers.
Definition: gz/transport/NodeShared.hh:419
Uuid replierId
Replier socket identity.
Definition: gz/transport/NodeShared.hh:320
std::string hostAddr
IP address of this host.
Definition: gz/transport/NodeShared.hh:440
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...
Store address information about topics and provide convenient methods for adding new topics,...
Definition: gz/transport/TopicStorage.hh:41
bool haveLocal
True iff there are any standard local subscribers.
Definition: gz/transport/NodeShared.hh:113
void(void *_data, void *_hint) DeallocFunc
Definition: gz/transport/TransportTypes.hh:171
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:66
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:116
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:39
int SndHwm()
Get the capacity of the buffer (High Water Mark) that stores outgoing Ignition Transport messages....