Robot Raconteur Core C++ Library
IntraTransport.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 IntraTransportConnection;
32 
55 class ROBOTRACONTEUR_CORE_API IntraTransport : public Transport, public RR_ENABLE_SHARED_FROM_THIS<IntraTransport>
56 {
57  friend class IntraTransportConnection;
58 
59  private:
60  static std::list<RR_WEAK_PTR<IntraTransport> > peer_transports;
61  static boost::mutex peer_transports_lock;
62 
63  bool transportopen;
64  bool is_server;
65 
66  public:
67  RR_UNORDERED_MAP<uint32_t, RR_SHARED_PTR<ITransportConnection> > TransportConnections;
68  boost::mutex TransportConnections_lock;
69 
83  IntraTransport(const RR_SHARED_PTR<RobotRaconteurNode>& node = RobotRaconteurNode::sp());
84 
85  RR_OVIRTUAL ~IntraTransport() RR_OVERRIDE;
86 
87  RR_OVIRTUAL bool IsServer() const RR_OVERRIDE;
88 
89  RR_OVIRTUAL bool IsClient() const RR_OVERRIDE;
90 
91  RR_OVIRTUAL std::string GetUrlSchemeString() const RR_OVERRIDE;
92 
93  RR_OVIRTUAL std::vector<std::string> GetServerListenUrls() RR_OVERRIDE;
94 
95  RR_OVIRTUAL void SendMessage(const RR_INTRUSIVE_PTR<Message>& m) RR_OVERRIDE;
96 
97  RR_OVIRTUAL void AsyncSendMessage(
98  const RR_INTRUSIVE_PTR<Message>& m,
99  const boost::function<void(const RR_SHARED_PTR<RobotRaconteurException>&)>& handler) RR_OVERRIDE;
100 
101  RR_OVIRTUAL void AsyncCreateTransportConnection(
102  boost::string_ref url, const RR_SHARED_PTR<Endpoint>& e,
103  boost::function<void(const RR_SHARED_PTR<ITransportConnection>&,
104  const RR_SHARED_PTR<RobotRaconteurException>&)>& handler) RR_OVERRIDE;
105 
106  RR_OVIRTUAL RR_SHARED_PTR<ITransportConnection> CreateTransportConnection(
107  boost::string_ref url, const RR_SHARED_PTR<Endpoint>& e) RR_OVERRIDE;
108 
109  RR_OVIRTUAL void CloseTransportConnection(const RR_SHARED_PTR<Endpoint>& e) RR_OVERRIDE;
110 
115  virtual void StartServer();
116 
121  virtual void StartClient();
122 
123  protected:
124  virtual void CloseTransportConnection_timed(const boost::system::error_code& err, const RR_SHARED_PTR<Endpoint>& e,
125  const RR_SHARED_PTR<void>& timer);
126 
127  public:
128  RR_OVIRTUAL bool CanConnectService(boost::string_ref url) RR_OVERRIDE;
129 
130  RR_OVIRTUAL void Close() RR_OVERRIDE;
131 
132  RR_OVIRTUAL void CheckConnection(uint32_t endpoint) RR_OVERRIDE;
133 
134  RR_OVIRTUAL void PeriodicCleanupTask() RR_OVERRIDE;
135 
136  RR_OVIRTUAL uint32_t TransportCapability(boost::string_ref name) RR_OVERRIDE;
137 
138  RR_OVIRTUAL void MessageReceived(const RR_INTRUSIVE_PTR<Message>& m) RR_OVERRIDE;
139 
140  RR_OVIRTUAL void AsyncGetDetectedNodes(
141  const std::vector<std::string>& schemes,
142  const boost::function<void(const RR_SHARED_PTR<std::vector<NodeDiscoveryInfo> >&)>& handler,
143  int32_t timeout = RR_TIMEOUT_INFINITE) RR_OVERRIDE;
144 
145  template <typename T, typename F>
146  boost::signals2::connection AddCloseListener(const RR_SHARED_PTR<T>& t, const F& f)
147  {
148  boost::mutex::scoped_lock lock(closed_lock);
149  if (closed)
150  {
151  lock.unlock();
152  boost::bind(f, t)();
153  return boost::signals2::connection();
154  }
155 
156  return close_signal.connect(boost::signals2::signal<void()>::slot_type(boost::bind(f, t.get())).track(t));
157  }
158 
159  bool TryGetNodeInfo(NodeID& node_id, std::string& node_name, std::string& service_nonce);
160 
161  RR_OVIRTUAL void LocalNodeServicesChanged() RR_OVERRIDE;
162 
163  void SendNodeDiscovery();
164 
165  void DiscoverAllNodes();
166 
167  protected:
168  virtual void register_transport(const RR_SHARED_PTR<ITransportConnection>& connection);
169  virtual void erase_transport(const RR_SHARED_PTR<ITransportConnection>& connection);
170 
171  void NodeDetected(const NodeDiscoveryInfo& info);
172 
173  boost::mutex init_lock;
174  bool is_init;
175 
176  void Init();
177 
178  bool closed;
179  boost::mutex closed_lock;
180  boost::signals2::signal<void()> close_signal;
181 };
182 
183 #ifndef ROBOTRACONTEUR_NO_CXX11_TEMPLATE_ALIASES
185 using IntraTransportPtr = RR_SHARED_PTR<IntraTransport>;
186 #endif
187 
188 } // namespace RobotRaconteur
boost::shared_ptr< IntraTransport > IntraTransportPtr
Convenience alias for IntraTransport shared_ptr.
Definition: IntraTransport.h:185
#define RR_TIMEOUT_INFINITE
Disable timeout for asynchronous operations.
Definition: RobotRaconteurConstants.h:566
Transport for intra-process communication.
Definition: IntraTransport.h:56
IntraTransport(const boost::shared_ptr< RobotRaconteurNode > &node=RobotRaconteurNode::sp())
Construct a new IntraTransport.
Raw information used to announce and detect nodes.
Definition: Discovery.h:137
NodeID UUID storage and generation.
Definition: NodeID.h:58
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