Ignition Transport

API Reference

7.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 <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 
171  public: void TriggerSubscriberCallbacks(
172  const std::string &_topic,
173  const std::string &_msgData,
174  const std::string &_msgType,
175  const HandlerInfo &_handlerInfo);
176 
183  public: void TriggerCallbacks(
184  const MessageInfo &_info,
185  const std::string &_msgData,
186  const HandlerInfo &_handlerInfo);
187 
190  public: void RecvControlUpdate();
191 
193  public: void RecvSrvRequest();
194 
196  public: void RecvSrvResponse();
197 
203  public: void SendPendingRemoteReqs(const std::string &_topic,
204  const std::string &_reqType,
205  const std::string &_repType);
206 
209  public: void OnNewConnection(const MessagePublisher &_pub);
210 
213  public: void OnNewDisconnection(const MessagePublisher &_pub);
214 
217  public: void OnNewSrvConnection(const ServicePublisher &_pub);
218 
221  public: void OnNewSrvDisconnection(const ServicePublisher &_pub);
222 
228  // there is at least one publisher.
231  public: bool TopicPublishers(const std::string &_topic,
232  SrvAddresses_M &_publishers) const;
233 
240  public: bool DiscoverService(const std::string &_topic) const;
241 
247  public: bool AdvertisePublisher(const ServicePublisher &_publisher);
248 
250  protected: NodeShared();
251 
253  protected: virtual ~NodeShared();
254 
258  private: bool InitializeSockets();
259 
263 
266 
268  public: Uuid replierId;
269 
270 #ifdef _WIN32
271 // Disable warning C4251 which is triggered by
272 // std::unique_ptr
273 #pragma warning(push)
274 #pragma warning(disable: 4251)
275 #endif
276  public: std::string pUuid;
278 
281 
283  public: mutable std::recursive_mutex mutex;
284 
286  public: static const int kMsgDiscPort = 10317;
287 
289  public: static const int kSrvDiscPort = 10318;
290 
292  private: TopicStorage<MessagePublisher> connections;
293 
295  private: std::vector<std::string> srvConnections;
296 
299 #ifdef _WIN32
300 #pragma warning(pop)
301 #endif
302 
308  {
315  public: bool HasSubscriber(
316  const std::string &_fullyQualifiedTopic,
317  const std::string &_msgType) const;
318 
324  public: bool HasSubscriber(
325  const std::string &_fullyQualifiedTopic) const;
326 
334  public: std::vector<std::string> NodeUuids(
335  const std::string &_fullyQualifiedTopic,
336  const std::string &_msgTypeName) const;
337 
345  public: bool RemoveHandlersForNode(
346  const std::string &_fullyQualifiedTopic,
347  const std::string &_nUuid);
348 
351 
356  };
357 
359 
360 #ifdef _WIN32
361 // Disable warning C4251 which is triggered by
362 // std::unique_ptr
363 #pragma warning(push)
364 #pragma warning(disable: 4251)
365 #endif
366  public: HandlerStorage<IRepHandler> repliers;
368 
371 
373  public: int verbose;
374 
377 
380 
383 
386 
389 
391  private: std::unique_ptr<NodeSharedPrivate> dataPtr;
392 #ifdef _WIN32
393 #pragma warning(pop)
394 #endif
395  private: friend Node;
396  private: friend NodePrivate;
397  };
398  }
399  }
400 }
401 #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:298
HandlerStorage< RawSubscriptionHandler > raw
Raw local subscriptions. Keeping these separate from localSubscriptions allows us to avoid an unneces...
Definition: NodeShared.hh:355
std::string myControlAddress
My pub/sub control address.
Definition: NodeShared.hh:379
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:268
HandlerWrapper localSubscribers
Definition: NodeShared.hh:358
HandlerStorage< ISubscriptionHandler > normal
Normal local subscriptions.
Definition: NodeShared.hh:350
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:385
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:373
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:265
std::string hostAddr
IP address of this host.
Definition: NodeShared.hh:388
This struct wraps up the two different types of subscription handlers: normal (deserialized) and raw ...
Definition: NodeShared.hh:307
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:283
std::thread threadReception
thread in charge of receiving and handling incoming messages.
Definition: NodeShared.hh:280
std::string myRequesterAddress
My requester service call address.
Definition: NodeShared.hh:382
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:370
std::string myAddress
My pub/sub address.
Definition: NodeShared.hh:376