Ignition Transport

API Reference

10.0.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 
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
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:346
HandlerStorage< RawSubscriptionHandler > raw
Raw local subscriptions. Keeping these separate from localSubscriptions allows us to avoid an unneces...
Definition: NodeShared.hh:403
std::string myControlAddress
My pub/sub control address.
Definition: NodeShared.hh:427
HandlerInfo contains information about callback handlers which is useful for local publishers and mes...
Definition: NodeShared.hh:100
Uuid replierId
Replier socket identity.
Definition: NodeShared.hh:307
HandlerWrapper localSubscribers
Definition: NodeShared.hh:406
HandlerStorage< ISubscriptionHandler > normal
Normal local subscriptions.
Definition: NodeShared.hh:398
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:433
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:421
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
Uuid responseReceiverId
Response receiver socket identity.
Definition: NodeShared.hh:304
std::string hostAddr
IP address of this host.
Definition: NodeShared.hh:436
This struct wraps up the two different types of subscription handlers: normal (deserialized) and raw ...
Definition: NodeShared.hh:355
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:190
This class stores all the information about a service publisher.
Definition: Publisher.hh:314
std::recursive_mutex mutex
Mutex to guarantee exclusive access between all threads.
Definition: NodeShared.hh:322
std::thread threadReception
thread in charge of receiving and handling incoming messages.
Definition: NodeShared.hh:319
std::string myRequesterAddress
My requester service call address.
Definition: NodeShared.hh:430
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:418
std::string myAddress
My pub/sub address.
Definition: NodeShared.hh:424