Ignition Transport

API Reference

8.1.0
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 IGN_TRANSPORT_NODESHARED_HH_
19 #define IGN_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 "ignition/transport/config.hh"
38 #include "ignition/transport/Export.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
bool haveRaw
True iff there are any raw local subscribers.
Definition: NodeShared.hh:116
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: NodeShared.hh:105
bool haveLocal
True iff there are any standard local subscribers.
Definition: NodeShared.hh:113
bool haveRemote
True if this Publisher has any remote subscribers.
Definition: NodeShared.hh:145
TopicStorage< MessagePublisher > remoteSubscribers
Remote subscribers.
Definition: NodeShared.hh:350
HandlerStorage< RawSubscriptionHandler > raw
Raw local subscriptions. Keeping these separate from localSubscriptions allows us to avoid an unneces...
Definition: NodeShared.hh:407
std::string myControlAddress
My pub/sub control address.
Definition: NodeShared.hh:431
HandlerInfo contains information about callback handlers which is useful for local publishers and mes...
Definition: NodeShared.hh:100
STL namespace.
HandlerWrapper localSubscribers
Definition: NodeShared.hh:410
HandlerStorage< ISubscriptionHandler > normal
Normal local subscriptions.
Definition: NodeShared.hh:402
STL class.
Encapsulates statistics for a single topic. The set of statistics include:
Definition: TopicStatistics.hh:101
STL class.
Class to store and manage service call handlers.
Definition: HandlerStorage.hh:39
std::string myReplierAddress
My replier service call address.
Definition: NodeShared.hh:437
Private data for the Node class. This class should not be directly used. You should use the Node clas...
Definition: NodeShared.hh:66
int verbose
Print activity to stdout.
Definition: NodeShared.hh:425
A portable class for representing a Universally Unique Identifier.
Definition: Uuid.hh:45
A class that provides information about the message received.
Definition: MessageInfo.hh:37
std::string hostAddr
IP address of this host.
Definition: NodeShared.hh:440
This struct wraps up the two different types of subscription handlers: normal (deserialized) and raw ...
Definition: NodeShared.hh:359
A class that allows a client to communicate with other peers. There are two main communication modes:...
Definition: Node.hh:95
void(void *_data, void *_hint) DeallocFunc
Definition: TransportTypes.hh:171
This class stores all the information about a message publisher.
Definition: Publisher.hh:222
This class stores all the information about a service publisher.
Definition: Publisher.hh:355
std::recursive_mutex mutex
Mutex to guarantee exclusive access between all threads.
Definition: NodeShared.hh:335
std::thread threadReception
thread in charge of receiving and handling incoming messages.
Definition: NodeShared.hh:332
std::string myRequesterAddress
My requester service call address.
Definition: NodeShared.hh:434
Definition: AdvertiseOptions.hh:28
Store address information about topics and provide convenient methods for adding new topics...
Definition: TopicStorage.hh:41
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: NodeShared.hh:110
STL class.
This struct provides information about the Subscribers of a Publisher. It should only be retrieved us...
Definition: NodeShared.hh:141
HandlerStorage< IReqHandler > requests
Pending service call requests.
Definition: NodeShared.hh:422
std::string myAddress
My pub/sub address.
Definition: NodeShared.hh:428