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 #include <zmq.hpp>
26 #ifdef _MSC_VER
27 #pragma warning(pop)
28 #endif
29 
30 #include <memory>
31 #include <mutex>
32 #include <string>
33 #include <thread>
34 #include <vector>
35 
45 
46 namespace ignition
47 {
48  namespace transport
49  {
54  {
58  public: static NodeShared *Instance();
59 
61  public: void RunReceptionTask();
62 
68  public: bool Publish(const std::string &_topic,
69  const std::string &_data,
70  const std::string &_msgType);
71 
73  public: void RecvMsgUpdate();
74 
77  public: void RecvControlUpdate();
78 
80  public: void RecvSrvRequest();
81 
83  public: void RecvSrvResponse();
84 
90  public: void SendPendingRemoteReqs(const std::string &_topic,
91  const std::string &_reqType,
92  const std::string &_repType);
93 
96  public: void OnNewConnection(const MessagePublisher &_pub);
97 
100  public: void OnNewDisconnection(const MessagePublisher &_pub);
101 
104  public: void OnNewSrvConnection(const ServicePublisher &_pub);
105 
108  public: void OnNewSrvDisconnection(const ServicePublisher &_pub);
109 
111  protected: NodeShared();
112 
114  protected: virtual ~NodeShared();
115 
119  private: bool InitializeSockets();
120 
122  public: static const int Timeout = 250;
123 
127 
130 
132  public: Uuid replierId;
133 
135  public: std::string pUuid;
136 
138  public: int timeout;
139 
141  public: std::thread threadReception;
142 
144  public: std::recursive_mutex mutex;
145 
147  public: bool exit;
148 
149 #ifdef _WIN32
150  public: bool threadReceptionExiting;
152 #endif
153 
155  private: const int kMsgDiscPort = 11317;
156 
158  private: const int kSrvDiscPort = 11318;
159 
161  private: std::mutex exitMutex;
162 
164  private: TopicStorage<MessagePublisher> connections;
165 
167  private: std::vector<std::string> srvConnections;
168 
171 
174 
177 
180 
182  public: int verbose;
183 
185  public: std::string myAddress;
186 
188  public: std::string myControlAddress;
189 
191  public: std::string myRequesterAddress;
192 
194  public: std::string myReplierAddress;
195 
197  public: std::string hostAddr;
198 
202 
205  public: std::unique_ptr<zmq::context_t> context;
206 
210 
212  public: std::unique_ptr<zmq::socket_t> publisher;
213 
215  public: std::unique_ptr<zmq::socket_t> subscriber;
216 
218  public: std::unique_ptr<zmq::socket_t> control;
219 
221  public: std::unique_ptr<zmq::socket_t> requester;
222 
224  public: std::unique_ptr<zmq::socket_t> responseReceiver;
225 
227  public: std::unique_ptr<zmq::socket_t> replier;
228 
232 
234  public: std::unique_ptr<MsgDiscovery> msgDiscovery;
235 
237  public: std::unique_ptr<SrvDiscovery> srvDiscovery;
238  };
239  }
240 }
241 #endif
std::thread threadReception
thread in charge of receiving and handling incoming messages.
Definition: NodeShared.hh:141
Uuid replierId
Replier socket identity.
Definition: NodeShared.hh:132
std::unique_ptr< zmq::socket_t > control
ZMQ socket to receive control updates (new connections, ...).
Definition: NodeShared.hh:218
std::string hostAddr
IP address of this host.
Definition: NodeShared.hh:197
std::unique_ptr< zmq::socket_t > responseReceiver
ZMQ socket for receiving service call responses.
Definition: NodeShared.hh:224
Uuid responseReceiverId
Response receiver socket identity.
Definition: NodeShared.hh:129
int timeout
Timeout used for receiving requests.
Definition: NodeShared.hh:138
std::string myControlAddress
My pub/sub control address.
Definition: NodeShared.hh:188
HandlerStorage< IReqHandler > requests
Pending service call requests.
Definition: NodeShared.hh:179
std::string myReplierAddress
My replier service call address.
Definition: NodeShared.hh:194
bool exit
When true, the reception thread will finish.
Definition: NodeShared.hh:147
TopicStorage< MessagePublisher > remoteSubscribers
Remote subscribers.
Definition: NodeShared.hh:170
std::unique_ptr< SrvDiscovery > srvDiscovery
Discovery service (services).
Definition: NodeShared.hh:237
std::unique_ptr< zmq::socket_t > replier
ZMQ socket to receive service call requests.
Definition: NodeShared.hh:227
ignition/transport/HandlerStorage.hh
Definition: HandlerStorage.hh:33
Private data for the Node class.
Definition: NodeShared.hh:53
A portable class for representing a Universally Unique Identifier.
Definition: Uuid.hh:41
std::unique_ptr< zmq::socket_t > subscriber
ZMQ socket to receive topic updates.
Definition: NodeShared.hh:215
std::unique_ptr< zmq::socket_t > requester
ZMQ socket for sending service call requests.
Definition: NodeShared.hh:221
std::string myAddress
My pub/sub address.
Definition: NodeShared.hh:185
std::unique_ptr< zmq::socket_t > publisher
ZMQ socket to send topic updates.
Definition: NodeShared.hh:212
#define IGNITION_TRANSPORT_VISIBLE
Use to represent "symbol visible" if supported.
Definition: Helpers.hh:55
ignition/transport/Publisher.hh
Definition: Publisher.hh:175
ignition/transport/Publisher.hh
Definition: Publisher.hh:264
HandlerStorage< ISubscriptionHandler > localSubscriptions
Subscriptions.
Definition: NodeShared.hh:173
std::unique_ptr< MsgDiscovery > msgDiscovery
Discovery service (messages).
Definition: NodeShared.hh:234
std::string myRequesterAddress
My requester service call address.
Definition: NodeShared.hh:191
std::unique_ptr< zmq::context_t > context
0MQ context.
Definition: NodeShared.hh:205
Definition: AdvertiseOptions.hh:25
Store address information about topics and provide convenient methods for adding new topics...
Definition: TopicStorage.hh:37
std::string pUuid
Process UUID.
Definition: NodeShared.hh:135
int verbose
Print activity to stdout.
Definition: NodeShared.hh:182
HandlerStorage< IRepHandler > repliers
Service call repliers.
Definition: NodeShared.hh:176
std::recursive_mutex mutex
Mutex to guarantee exclusive access between all threads.
Definition: NodeShared.hh:144