Robot Raconteur Core C++ Library
Transport.h
Go to the documentation of this file.
1 
24 #pragma once
25 
27 #include "RobotRaconteur/Message.h"
30 #include <boost/date_time.hpp>
31 
32 #include "RobotRaconteur/Timer.h"
33 
34 #pragma warning(push)
35 #pragma warning(disable : 4996)
36 #include <boost/signals2.hpp>
37 
38 #ifdef ROBOTRACONTEUR_WINDOWS
39 #undef SendMessage
40 #endif
41 
42 namespace RobotRaconteur
43 {
44 
45 enum TransportListenerEventType
46 {
47  TransportListenerEventType_TransportClosed = 1,
48  TransportListenerEventType_TransportConnectionClosed
49 };
50 
51 class ROBOTRACONTEUR_CORE_API RobotRaconteurNode;
52 class ROBOTRACONTEUR_CORE_API Timer;
53 struct ROBOTRACONTEUR_CORE_API TimerEvent;
54 class ROBOTRACONTEUR_CORE_API AutoResetEvent;
55 class ROBOTRACONTEUR_CORE_API Transport;
56 
57 class ROBOTRACONTEUR_CORE_API ITransportTimeProvider
58 {
59  public:
60  virtual boost::posix_time::ptime NowUTC() = 0;
61 
62  virtual TimeSpec NowTimeSpec() = 0;
63 
64  virtual boost::posix_time::ptime NowNodeTime() = 0;
65 
66  virtual boost::posix_time::ptime NodeSyncTimeUTC() = 0;
67 
68  virtual TimeSpec NodeSyncTimeSpec() = 0;
69 
70  virtual RR_SHARED_PTR<Timer> CreateTimer(const boost::posix_time::time_duration& duration,
71  const boost::function<void(const TimerEvent&)>& handler,
72  bool oneshot = false) = 0;
73 
74  virtual RR_SHARED_PTR<Rate> CreateRate(double frequency) = 0;
75 
76  virtual void Sleep(const boost::posix_time::time_duration& duration) = 0;
77 
78  virtual RR_SHARED_PTR<AutoResetEvent> CreateAutoResetEvent() = 0;
79 
80  virtual ~ITransportTimeProvider() {}
81 };
82 
83 class ROBOTRACONTEUR_CORE_API ITransportConnection : boost::noncopyable
84 {
85  public:
86  virtual ~ITransportConnection() {}
87 
88  virtual void SendMessage(const RR_INTRUSIVE_PTR<Message>& m) = 0;
89 
90  virtual void AsyncSendMessage(
91  const RR_INTRUSIVE_PTR<Message>& m,
92  const boost::function<void(const RR_SHARED_PTR<RobotRaconteurException>&)>& handler) = 0;
93 
94  virtual void Close() = 0;
95 
96  virtual void CheckConnection(uint32_t endpoint) = 0;
97 
98  virtual uint32_t GetLocalEndpoint() = 0;
99 
100  virtual uint32_t GetRemoteEndpoint() = 0;
101 
102  virtual NodeID GetRemoteNodeID() = 0;
103 
104  virtual RR_SHARED_PTR<RobotRaconteurNode> GetNode() = 0;
105 
106  virtual bool CheckCapabilityActive(uint32_t flag) = 0;
107 
108  virtual RR_SHARED_PTR<Transport> GetTransport() = 0;
109 };
110 
111 class ROBOTRACONTEUR_CORE_API NodeDiscoveryInfo;
112 
119 class ROBOTRACONTEUR_CORE_API Transport : public IPeriodicCleanupTask, boost::noncopyable
120 {
121  public:
122  friend class RobotRaconteurNode;
123  friend class ITransport;
124  RR_OVIRTUAL ~Transport() RR_OVERRIDE {}
125 
126  protected:
127  RR_WEAK_PTR<RobotRaconteurNode> node;
128 
129  public:
130  Transport(const RR_SHARED_PTR<RobotRaconteurNode>& node);
131 
132  static boost::thread_specific_ptr<std::string> m_CurrentThreadTransportConnectionURL;
133 
134  static std::string GetCurrentTransportConnectionURL();
135 
136  static boost::thread_specific_ptr<RR_SHARED_PTR<ITransportConnection> > m_CurrentThreadTransport;
137 
138  static RR_SHARED_PTR<ITransportConnection> GetCurrentThreadTransport();
139 
140  // public event MessageHandler MessageReceivedEvent;
141 
142  uint32_t TransportID;
143 
144  virtual void CheckConnection(uint32_t endpoint) = 0;
145 
146  virtual bool IsClient() const = 0;
147 
148  virtual bool IsServer() const = 0;
149 
150  virtual std::string GetUrlSchemeString() const = 0;
151 
152  virtual std::vector<std::string> GetServerListenUrls() = 0;
153 
154  // public abstract string Scheme { get; }
155 
156  virtual bool CanConnectService(boost::string_ref url) = 0;
157 
158  virtual RR_SHARED_PTR<ITransportConnection> CreateTransportConnection(boost::string_ref url,
159  const RR_SHARED_PTR<Endpoint>& e) = 0;
160 
161  virtual void AsyncCreateTransportConnection(
162  boost::string_ref url, const RR_SHARED_PTR<Endpoint>& e,
163  boost::function<void(const RR_SHARED_PTR<ITransportConnection>&,
164  const RR_SHARED_PTR<RobotRaconteurException>&)>& handler) = 0;
165 
166  virtual void CloseTransportConnection(const RR_SHARED_PTR<Endpoint>& e) = 0;
167 
168  virtual void SendMessage(const RR_INTRUSIVE_PTR<Message>& m) = 0;
169 
170  virtual void AsyncSendMessage(
171  const RR_INTRUSIVE_PTR<Message>& m,
172  const boost::function<void(const RR_SHARED_PTR<RobotRaconteurException>&)>& handler) = 0;
173 
174  virtual void MessageReceived(const RR_INTRUSIVE_PTR<Message>& m) = 0;
175 
176  virtual RR_INTRUSIVE_PTR<Message> SpecialRequest(const RR_INTRUSIVE_PTR<Message>& m,
177  const RR_SHARED_PTR<ITransportConnection>& tc);
178 
179  virtual void Close();
180 
181  RR_OVIRTUAL void PeriodicCleanupTask() RR_OVERRIDE;
182 
183  virtual uint32_t TransportCapability(boost::string_ref name);
184 
185  // typedef void (*TransportListenerDelegate)(const RR_SHARED_PTR<Transport> &transport, TransportListenerEventType
186  // ev, const RR_SHARED_PTR<void> &parameter);
187 
188  boost::signals2::signal<void(const RR_SHARED_PTR<Transport>& transport, TransportListenerEventType ev,
189  const RR_SHARED_PTR<void>& parameter)>
190  TransportListeners;
191 
192  virtual RR_SHARED_PTR<RobotRaconteurNode> GetNode() const;
193 
194  virtual std::vector<NodeDiscoveryInfo> GetDetectedNodes(const std::vector<std::string>& schemes);
195 
196  virtual void AsyncGetDetectedNodes(
197  const std::vector<std::string>& schemes,
198  const boost::function<void(const RR_SHARED_PTR<std::vector<NodeDiscoveryInfo> >&)>& handler,
199  int32_t timeout = RR_TIMEOUT_INFINITE);
200 
201  protected:
202  virtual void LocalNodeServicesChanged();
203 
204  void FireTransportEventListener(const RR_SHARED_PTR<Transport>& shared_this, TransportListenerEventType ev,
205  const RR_SHARED_PTR<void>& parameter);
206 
207  void TransportConnectionClosed(uint32_t endpoint);
208 };
209 
210 struct ROBOTRACONTEUR_CORE_API ParseConnectionURLResult
211 {
212  ParseConnectionURLResult() : port(0) {}
213 
214  std::string scheme;
215  std::string host;
216  int32_t port;
217  std::string path;
218  NodeID nodeid;
219  std::string nodename;
220  std::string service;
221 };
222 
223 ROBOTRACONTEUR_CORE_API ParseConnectionURLResult ParseConnectionURL(boost::string_ref url);
224 
225 #ifndef ROBOTRACONTEUR_NO_CXX11_TEMPLATE_ALIASES
227 using TransportPtr = RR_SHARED_PTR<Transport>;
228 using ITransportConnectionPtr = RR_SHARED_PTR<ITransportConnection>;
229 #endif
230 
231 } // namespace RobotRaconteur
232 
233 #pragma warning(pop)
#define RR_TIMEOUT_INFINITE
Disable timeout for asynchronous operations.
Definition: RobotRaconteurConstants.h:566
boost::shared_ptr< Transport > TransportPtr
Convenience alias for Transport shared_ptr.
Definition: Transport.h:227
The central node implementation.
Definition: RobotRaconteurNode.h:132
Base class for transports.
Definition: Transport.h:120