Robot Raconteur Core C++ Library
LocalTransport.h
Go to the documentation of this file.
1 
25 #include <boost/shared_array.hpp>
26 
27 #pragma once
28 
29 namespace RobotRaconteur
30 {
31 class ROBOTRACONTEUR_CORE_API LocalTransportConnection;
32 
33 namespace detail
34 {
35 class LocalTransportFDs;
36 class LocalTransportDiscovery;
37 class LocalTransport_socket;
38 class LocalTransport_acceptor;
39 } // namespace detail
40 
90 class ROBOTRACONTEUR_CORE_API LocalTransport : public Transport, public RR_ENABLE_SHARED_FROM_THIS<LocalTransport>
91 {
92  friend class LocalTransportConnection;
93 
94  private:
95  std::string socket_file_name;
96 
97  bool transportopen;
98 
99  public:
100  RR_UNORDERED_MAP<uint32_t, RR_SHARED_PTR<ITransportConnection> > TransportConnections;
101  boost::mutex TransportConnections_lock;
102 
116  LocalTransport(const RR_SHARED_PTR<RobotRaconteurNode>& node = RobotRaconteurNode::sp());
117 
118  RR_OVIRTUAL ~LocalTransport() RR_OVERRIDE;
119 
120  RR_OVIRTUAL bool IsServer() const RR_OVERRIDE;
121 
122  RR_OVIRTUAL bool IsClient() const RR_OVERRIDE;
123 
124  RR_OVIRTUAL std::string GetUrlSchemeString() const RR_OVERRIDE;
125 
126  RR_OVIRTUAL std::vector<std::string> GetServerListenUrls() RR_OVERRIDE;
127 
128  RR_OVIRTUAL void SendMessage(const RR_INTRUSIVE_PTR<Message>& m) RR_OVERRIDE;
129 
130  RR_OVIRTUAL void AsyncSendMessage(
131  const RR_INTRUSIVE_PTR<Message>& m,
132  const boost::function<void(const RR_SHARED_PTR<RobotRaconteurException>&)>& handler) RR_OVERRIDE;
133 
134  RR_OVIRTUAL void AsyncCreateTransportConnection(
135  boost::string_ref url, const RR_SHARED_PTR<Endpoint>& e,
136  boost::function<void(const RR_SHARED_PTR<ITransportConnection>&,
137  const RR_SHARED_PTR<RobotRaconteurException>&)>& callback) RR_OVERRIDE;
138 
139  RR_OVIRTUAL RR_SHARED_PTR<ITransportConnection> CreateTransportConnection(
140  boost::string_ref url, const RR_SHARED_PTR<Endpoint>& e) RR_OVERRIDE;
141 
142  RR_OVIRTUAL void CloseTransportConnection(const RR_SHARED_PTR<Endpoint>& e) RR_OVERRIDE;
143 
144  protected:
145  virtual void AsyncCreateTransportConnection2(
146  const RR_SHARED_PTR<detail::LocalTransport_socket>& socket, const std::string& noden,
147  const RR_SHARED_PTR<ITransportConnection>& transport, const RR_SHARED_PTR<RobotRaconteurException>& err,
148  boost::function<void(const RR_SHARED_PTR<ITransportConnection>&,
149  const RR_SHARED_PTR<RobotRaconteurException>&)>& callback);
150 
151  virtual void CloseTransportConnection_timed(const boost::system::error_code& err, const RR_SHARED_PTR<Endpoint>& e,
152  const RR_SHARED_PTR<void>& timer);
153 
154  public:
170  static bool IsLocalTransportSupported();
171 
182  virtual void StartClientAsNodeName(boost::string_ref name);
183 
202  virtual void StartServerAsNodeName(boost::string_ref name, bool public_ = false);
203 
217  virtual void StartServerAsNodeID(const NodeID& nodeid, bool public_ = false);
218 
219  RR_OVIRTUAL bool CanConnectService(boost::string_ref url) RR_OVERRIDE;
220 
221  RR_OVIRTUAL void Close() RR_OVERRIDE;
222 
223  RR_OVIRTUAL void CheckConnection(uint32_t endpoint) RR_OVERRIDE;
224 
225  RR_OVIRTUAL void PeriodicCleanupTask() RR_OVERRIDE;
226 
227  RR_OVIRTUAL uint32_t TransportCapability(boost::string_ref name) RR_OVERRIDE;
228 
229  RR_OVIRTUAL void MessageReceived(const RR_INTRUSIVE_PTR<Message>& m) RR_OVERRIDE;
230 
231  RR_OVIRTUAL void AsyncGetDetectedNodes(
232  const std::vector<std::string>& schemes,
233  const boost::function<void(const RR_SHARED_PTR<std::vector<NodeDiscoveryInfo> >&)>& handler,
234  int32_t timeout = RR_TIMEOUT_INFINITE) RR_OVERRIDE;
235 
237  virtual int32_t GetMaxMessageSize();
239  virtual void SetMaxMessageSize(int32_t size);
240 
242  virtual bool GetDisableMessage4();
244  virtual void SetDisableMessage4(bool d);
245 
247  virtual bool GetDisableStringTable();
249  virtual void SetDisableStringTable(bool d);
250 
252  virtual bool GetDisableAsyncMessageIO();
254  virtual void SetDisableAsyncMessageIO(bool d);
255 
262  virtual void EnableNodeDiscoveryListening();
263 
268  virtual void DisableNodeDiscoveryListening();
269 
270  template <typename T, typename F>
271  boost::signals2::connection AddCloseListener(const RR_SHARED_PTR<T>& t, const F& f)
272  {
273  boost::mutex::scoped_lock lock(closed_lock);
274  if (closed)
275  {
276  lock.unlock();
277  boost::bind(f, t)();
278  return boost::signals2::connection();
279  }
280 
281  return close_signal.connect(boost::signals2::signal<void()>::slot_type(boost::bind(f, t.get())).track(t));
282  }
283 
284  protected:
285  RR_OVIRTUAL void LocalNodeServicesChanged() RR_OVERRIDE;
286 
287  RR_SHARED_PTR<detail::LocalTransport_acceptor> acceptor;
288 
289  boost::mutex acceptor_lock;
290 
291  static void handle_accept(const RR_SHARED_PTR<LocalTransport>& parent,
292  const RR_SHARED_PTR<detail::LocalTransport_acceptor>& acceptor,
293  const RR_SHARED_PTR<detail::LocalTransport_socket>& socket,
294  const boost::system::error_code& error);
295 
296  virtual void register_transport(const RR_SHARED_PTR<ITransportConnection>& connection);
297  virtual void erase_transport(const RR_SHARED_PTR<ITransportConnection>& connection);
298 
299  boost::mutex fds_lock;
300  RR_SHARED_PTR<detail::LocalTransportFDs> fds;
301 
302  boost::mutex parameter_lock;
303  int32_t max_message_size;
304  bool disable_message4;
305  bool disable_string_table;
306  bool disable_async_message_io;
307 
308  RR_SHARED_PTR<detail::LocalTransportDiscovery> discovery;
309  boost::mutex discovery_lock;
310 
311  bool closed;
312  boost::mutex closed_lock;
313  boost::signals2::signal<void()> close_signal;
314 };
315 
321 class ROBOTRACONTEUR_CORE_API NodeNameAlreadyInUse : public std::runtime_error
322 {
323  public:
324  NodeNameAlreadyInUse() : runtime_error("Could not start server: NodeName already in use"){};
325 };
326 
331 class ROBOTRACONTEUR_CORE_API NodeIDAlreadyInUse : public std::runtime_error
332 {
333  public:
334  NodeIDAlreadyInUse() : runtime_error("Could not start server: NodeID already in use"){};
335 };
336 
337 #ifndef ROBOTRACONTEUR_NO_CXX11_TEMPLATE_ALIASES
339 using LocalTransportPtr = RR_SHARED_PTR<LocalTransport>;
340 #endif
341 } // namespace RobotRaconteur
boost::shared_ptr< LocalTransport > LocalTransportPtr
Convenience alias for LocalTransport shared_ptr.
Definition: LocalTransport.h:339
#define RR_TIMEOUT_INFINITE
Disable timeout for asynchronous operations.
Definition: RobotRaconteurConstants.h:566
Transport for communication between processes using UNIX domain sockets.
Definition: LocalTransport.h:91
LocalTransport(const boost::shared_ptr< RobotRaconteurNode > &node=RobotRaconteurNode::sp())
Construct a new LocalTransport.
Raw information used to announce and detect nodes.
Definition: Discovery.h:137
Exception thrown if the NodeID is already in use.
Definition: LocalTransport.h:332
NodeID UUID storage and generation.
Definition: NodeID.h:58
Exception thrown if the NodeName is already in use.
Definition: LocalTransport.h:322
Base class for Robot Raconteur exceptions.
Definition: Error.h:50
static boost::shared_ptr< RobotRaconteurNode > sp()
Singleton shared_ptr accessor.
Base class for transports.
Definition: Transport.h:120