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
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 
171  public: void TriggerCallbacks(
172  const MessageInfo &_info,
173  const std::string &_msgData,
174  const HandlerInfo &_handlerInfo);
175 
179  public: void RecvControlUpdate();
180 
182  public: void RecvSrvRequest();
183 
185  public: void RecvSrvResponse();
186 
192  public: void SendPendingRemoteReqs(const std::string &_topic,
193  const std::string &_reqType,
194  const std::string &_repType);
195 
198  public: void OnNewConnection(const MessagePublisher &_pub);
199 
202  public: void OnNewDisconnection(const MessagePublisher &_pub);
203 
206  public: void OnNewSrvConnection(const ServicePublisher &_pub);
207 
210  public: void OnNewSrvDisconnection(const ServicePublisher &_pub);
211 
214  public: void OnNewRegistration(const MessagePublisher &_pub);
215 
218  public: void OnEndRegistration(const MessagePublisher &_pub);
219 
225  // there is at least one publisher.
228  public: bool TopicPublishers(const std::string &_topic,
229  SrvAddresses_M &_publishers) const;
230 
237  public: bool DiscoverService(const std::string &_topic) const;
238 
244  public: bool AdvertisePublisher(const ServicePublisher &_publisher);
245 
256  public: int RcvHwm();
257 
268  public: int SndHwm();
269 
276  public: void EnableStats(const std::string &_topic, bool _enable,
277  std::function<void(const TopicStatistics &_stats)> _cb);
278 
285  public: std::optional<TopicStatistics> TopicStats(
286  const std::string &_topic) const;
287 
289  protected: NodeShared();
290 
292  protected: virtual ~NodeShared();
293 
297  private: bool InitializeSockets();
298 
302 
305 
307  public: Uuid replierId;
308 
309 #ifdef _WIN32
310 // Disable warning C4251 which is triggered by
311 // std::unique_ptr
312 #pragma warning(push)
313 #pragma warning(disable: 4251)
314 #endif
315  public: std::string pUuid;
317 
320 
322  public: mutable std::recursive_mutex mutex;
323 
325  public: std::string discoveryIP = "239.255.0.7";
326 
328  public: static const int kDefaultMsgDiscPort = 10317;
329 
331  public: static const int kDefaultSrvDiscPort = 10318;
332 
334  public: int msgDiscPort = kDefaultMsgDiscPort;
335 
337  public: int srvDiscPort = kDefaultSrvDiscPort;
338 
340  private: TopicStorage<MessagePublisher> connections;
341 
343  private: std::vector<std::string> srvConnections;
344 
347 #ifdef _WIN32
348 #pragma warning(pop)
349 #endif
350 
356  {
363  public: bool HasSubscriber(
364  const std::string &_fullyQualifiedTopic,
365  const std::string &_msgType) const;
366 
372  public: bool HasSubscriber(
373  const std::string &_fullyQualifiedTopic) const;
374 
382  public: std::vector<std::string> NodeUuids(
383  const std::string &_fullyQualifiedTopic,
384  const std::string &_msgTypeName) const;
385 
393  public: bool RemoveHandlersForNode(
394  const std::string &_fullyQualifiedTopic,
395  const std::string &_nUuid);
396 
399 
404  };
405 
407 
408 #ifdef _WIN32
409 // Disable warning C4251 which is triggered by
410 // std::unique_ptr
411 #pragma warning(push)
412 #pragma warning(disable: 4251)
413 #endif
414  public: HandlerStorage<IRepHandler> repliers;
416 
419 
421  public: int verbose;
422 
425 
428 
431 
434 
437 
439  private: std::unique_ptr<NodeSharedPrivate> dataPtr;
440 #ifdef _WIN32
441 #pragma warning(pop)
442 #endif
443  private: friend Node;
444  private: friend NodePrivate;
445  };
446  }
447  }
448 }
449 #endif
std::string myReplierAddress
My replier service call address.
Definition: gz/transport/NodeShared.hh:433
HandlerStorage< ISubscriptionHandler > normal
Normal local subscriptions.
Definition: gz/transport/NodeShared.hh:398
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:355
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:316
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:427
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:418
std::string discoveryIP
Default IP address used by the message discovery layer.
Definition: gz/transport/NodeShared.hh:325
std::recursive_mutex mutex
Mutex to guarantee exclusive access between all threads.
Definition: gz/transport/NodeShared.hh:322
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:331
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:181
std::thread threadReception
thread in charge of receiving and handling incoming messages.
Definition: gz/transport/NodeShared.hh:319
int verbose
Print activity to stdout.
Definition: gz/transport/NodeShared.hh:421
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.
int srvDiscPort
Port used by the service discovery layer.
Definition: gz/transport/NodeShared.hh:337
void OnNewConnection(const MessagePublisher &_pub)
Callback executed when the discovery detects new topics.
Uuid responseReceiverId
Response receiver socket identity.
Definition: gz/transport/NodeShared.hh:304
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:430
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:406
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:424
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:403
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:346
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:296
HandlerStorage< IRepHandler > repliers
Service call repliers.
Definition: gz/transport/NodeShared.hh:415
Uuid replierId
Replier socket identity.
Definition: gz/transport/NodeShared.hh:307
std::string hostAddr
IP address of this host.
Definition: gz/transport/NodeShared.hh:436
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:328
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 msgDiscPort
Port used by the message discovery layer.
Definition: gz/transport/NodeShared.hh:334
int SndHwm()
Get the capacity of the buffer (High Water Mark) that stores outgoing Ignition Transport messages....