All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
transport.hpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
7 #ifndef NDN_TRANSPORT_TRANSPORT_HPP
8 #define NDN_TRANSPORT_TRANSPORT_HPP
9 
10 #include "../common.hpp"
11 #include "../encoding/block.hpp"
12 
13 namespace ndn {
14 
15 class Transport
16 {
17 public:
18  class Error : public std::runtime_error
19  {
20  public:
21  inline Error(const boost::system::error_code& code, const std::string& msg);
22  inline Error(const std::string& msg);
23  };
24 
25  typedef ptr_lib::function<void (const Block& wire)> ReceiveCallback;
26  typedef ptr_lib::function<void ()> ErrorCallback;
27 
28  inline
29  Transport();
30 
31  inline virtual
32  ~Transport();
33 
39  inline virtual void
40  connect(boost::asio::io_service& io_service,
41  const ReceiveCallback& receiveCallback);
42 
46  virtual void
47  close() = 0;
48 
55  virtual void
56  send(const Block& wire) = 0;
57 
64  virtual void
65  send(const Block& header, const Block& payload) = 0;
66 
67  virtual void
68  pause() = 0;
69 
70  virtual void
71  resume() = 0;
72 
73  inline bool
74  isConnected();
75 
76  inline bool
78 
79 protected:
80  inline void
81  receive(const Block& wire);
82 
83 protected:
84  boost::asio::io_service* m_ioService;
88 };
89 
90 inline
92  : m_ioService(0)
93  , m_isConnected(false)
94  , m_isExpectingData(false)
95 {
96 }
97 
98 inline
99 Transport::Error::Error(const boost::system::error_code& code, const std::string& msg)
100  : std::runtime_error(msg + (code.value() ? " (" + code.category().message(code.value()) + ")" : ""))
101 {
102 }
103 
104 inline
105 Transport::Error::Error(const std::string& msg)
106  : std::runtime_error(msg)
107 {
108 }
109 
110 inline
112 {
113 }
114 
115 inline void
116 Transport::connect(boost::asio::io_service& ioService,
117  const ReceiveCallback& receiveCallback)
118 {
119  m_ioService = &ioService;
120  m_receiveCallback = receiveCallback;
121 }
122 
123 inline bool
125 {
126  return m_isConnected;
127 }
128 
129 inline bool
131 {
132  return m_isExpectingData;
133 }
134 
135 inline void
137 {
138  m_receiveCallback(wire);
139 }
140 
141 } // namespace ndn
142 
143 #endif // NDN_TRANSPORT_TRANSPORT_HPP
virtual void connect(boost::asio::io_service &io_service, const ReceiveCallback &receiveCallback)
Connect transport.
Definition: transport.hpp:116
ptr_lib::function< void(const Block &wire)> ReceiveCallback
Definition: transport.hpp:25
ptr_lib::function< void()> ErrorCallback
Definition: transport.hpp:26
Error(const boost::system::error_code &code, const std::string &msg)
Definition: transport.hpp:99
virtual void close()=0
Close the connection.
Class representing wire element of the NDN packet.
Definition: block.hpp:26
virtual void send(const Block &wire)=0
Set data to the host.
ReceiveCallback m_receiveCallback
Definition: transport.hpp:87
bool m_isExpectingData
Definition: transport.hpp:86
virtual void resume()=0
boost::asio::io_service * m_ioService
Definition: transport.hpp:84
void receive(const Block &wire)
Definition: transport.hpp:136
virtual void pause()=0
bool isConnected()
Definition: transport.hpp:124
bool m_isConnected
Definition: transport.hpp:85
virtual ~Transport()
Definition: transport.hpp:111
bool isExpectingData()
Definition: transport.hpp:130