Ignition Transport

API Reference

6.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 <string>
32 #include <thread>
33 #include <vector>
34 #include <map>
35 
36 #include "ignition/transport/config.hh"
37 #include "ignition/transport/Export.hh"
46 
47 namespace ignition
48 {
49  namespace 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 
170  public: void TriggerSubscriberCallbacks(
171  const std::string &_topic,
172  const std::string &_msgData,
173  const std::string &_msgType,
174  const HandlerInfo &_handlerInfo);
175 
178  public: void RecvControlUpdate();
179 
181  public: void RecvSrvRequest();
182 
184  public: void RecvSrvResponse();
185 
191  public: void SendPendingRemoteReqs(const std::string &_topic,
192  const std::string &_reqType,
193  const std::string &_repType);
194 
197  public: void OnNewConnection(const MessagePublisher &_pub);
198 
201  public: void OnNewDisconnection(const MessagePublisher &_pub);
202 
205  public: void OnNewSrvConnection(const ServicePublisher &_pub);
206 
209  public: void OnNewSrvDisconnection(const ServicePublisher &_pub);
210 
216  // there is at least one publisher.
219  public: bool TopicPublishers(const std::string &_topic,
220  SrvAddresses_M &_publishers) const;
221 
228  public: bool DiscoverService(const std::string &_topic) const;
229 
235  public: bool AdvertisePublisher(const ServicePublisher &_publisher);
236 
238  protected: NodeShared();
239 
241  protected: virtual ~NodeShared();
242 
246  private: bool InitializeSockets();
247 
251 
254 
256  public: Uuid replierId;
257 
258 #ifdef _WIN32
259 // Disable warning C4251 which is triggered by
260 // std::unique_ptr
261 #pragma warning(push)
262 #pragma warning(disable: 4251)
263 #endif
264  public: std::string pUuid;
266 
269 
271  public: mutable std::recursive_mutex mutex;
272 
274  public: static const int kMsgDiscPort = 11317;
275 
277  public: static const int kSrvDiscPort = 11318;
278 
280  private: TopicStorage<MessagePublisher> connections;
281 
283  private: std::vector<std::string> srvConnections;
284 
287 #ifdef _WIN32
288 #pragma warning(pop)
289 #endif
290 
296  {
303  public: bool HasSubscriber(
304  const std::string &_fullyQualifiedTopic,
305  const std::string &_msgType) const;
306 
312  public: bool HasSubscriber(
313  const std::string &_fullyQualifiedTopic) const;
314 
322  public: std::vector<std::string> NodeUuids(
323  const std::string &_fullyQualifiedTopic,
324  const std::string &_msgTypeName) const;
325 
333  public: bool RemoveHandlersForNode(
334  const std::string &_fullyQualifiedTopic,
335  const std::string &_nUuid);
336 
339 
344  };
345 
347 
348 #ifdef _WIN32
349 // Disable warning C4251 which is triggered by
350 // std::unique_ptr
351 #pragma warning(push)
352 #pragma warning(disable: 4251)
353 #endif
354  public: HandlerStorage<IRepHandler> repliers;
356 
359 
361  public: int verbose;
362 
365 
368 
371 
374 
377 
379  private: std::unique_ptr<NodeSharedPrivate> dataPtr;
380 #ifdef _WIN32
381 #pragma warning(pop)
382 #endif
383  private: friend Node;
384  private: friend NodePrivate;
385  };
386  }
387  }
388 }
389 #endif
bool haveRaw
True iff there are any raw local subscribers.
Definition: NodeShared.hh:114
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:103
bool haveLocal
True iff there are any standard local subscribers.
Definition: NodeShared.hh:111
bool haveRemote
True if this Publisher has any remote subscribers.
Definition: NodeShared.hh:143
TopicStorage< MessagePublisher > remoteSubscribers
Remote subscribers.
Definition: NodeShared.hh:286
HandlerStorage< RawSubscriptionHandler > raw
Raw local subscriptions. Keeping these separate from localSubscriptions allows us to avoid an unneces...
Definition: NodeShared.hh:343
std::string myControlAddress
My pub/sub control address.
Definition: NodeShared.hh:367
HandlerInfo contains information about callback handlers which is useful for local publishers and mes...
Definition: NodeShared.hh:98
Uuid replierId
Replier socket identity.
Definition: NodeShared.hh:256
HandlerWrapper localSubscribers
Definition: NodeShared.hh:346
HandlerStorage< ISubscriptionHandler > normal
Normal local subscriptions.
Definition: NodeShared.hh:338
STL class.
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:373
Private data for the Node class. This class should not be directly used. You should use the Node clas...
Definition: NodeShared.hh:64
int verbose
Print activity to stdout.
Definition: NodeShared.hh:361
A portable class for representing a Universally Unique Identifier.
Definition: Uuid.hh:45
Uuid responseReceiverId
Response receiver socket identity.
Definition: NodeShared.hh:253
std::string hostAddr
IP address of this host.
Definition: NodeShared.hh:376
This struct wraps up the two different types of subscription handlers: normal (deserialized) and raw ...
Definition: NodeShared.hh:295
A class that allows a client to communicate with other peers. There are two main communication modes:...
Definition: Node.hh:69
void(void *_data, void *_hint) DeallocFunc
Definition: TransportTypes.hh:171
This class stores all the information about a message publisher.
Definition: Publisher.hh:212
This class stores all the information about a service publisher.
Definition: Publisher.hh:337
std::recursive_mutex mutex
Mutex to guarantee exclusive access between all threads.
Definition: NodeShared.hh:271
std::thread threadReception
thread in charge of receiving and handling incoming messages.
Definition: NodeShared.hh:268
std::string myRequesterAddress
My requester service call address.
Definition: NodeShared.hh:370
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:108
STL class.
This struct provides information about the Subscribers of a Publisher. It should only be retrieved us...
Definition: NodeShared.hh:139
HandlerStorage< IReqHandler > requests
Pending service call requests.
Definition: NodeShared.hh:358
std::string myAddress
My pub/sub address.
Definition: NodeShared.hh:364