NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.0: NDN, CCN, CCNx, content centric networks
API Documentation
transport.hpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
22 #ifndef NDN_TRANSPORT_TRANSPORT_HPP
23 #define NDN_TRANSPORT_TRANSPORT_HPP
24 
25 #include "../common.hpp"
26 #include "../encoding/block.hpp"
27 
28 #include <boost/system/error_code.hpp>
29 
30 // forward declaration
31 namespace boost {
32 namespace asio {
33 class io_service;
34 } // namespace asio
35 } // namespace boost
36 
37 namespace ndn {
38 
39 class Transport : noncopyable
40 {
41 public:
42  class Error : public std::runtime_error
43  {
44  public:
45  inline Error(const boost::system::error_code& code, const std::string& msg);
46  inline Error(const std::string& msg);
47  };
48 
49  typedef function<void (const Block& wire)> ReceiveCallback;
50  typedef function<void ()> ErrorCallback;
51 
52  inline
53  Transport();
54 
55  inline virtual
56  ~Transport();
57 
63  inline virtual void
64  connect(boost::asio::io_service& io_service,
65  const ReceiveCallback& receiveCallback);
66 
70  virtual void
71  close() = 0;
72 
78  virtual void
79  send(const Block& wire) = 0;
80 
87  virtual void
88  send(const Block& header, const Block& payload) = 0;
89 
90  virtual void
91  pause() = 0;
92 
93  virtual void
94  resume() = 0;
95 
96  inline bool
97  isConnected();
98 
99  inline bool
100  isExpectingData();
101 
102 protected:
103  inline void
104  receive(const Block& wire);
105 
106 protected:
107  boost::asio::io_service* m_ioService;
110  ReceiveCallback m_receiveCallback;
111 };
112 
113 inline
115  : m_ioService(0)
116  , m_isConnected(false)
117  , m_isExpectingData(false)
118 {
119 }
120 
121 inline
122 Transport::Error::Error(const boost::system::error_code& code, const std::string& msg)
123  : std::runtime_error(msg + (code.value() ? " (" + code.category().message(code.value()) + ")" : ""))
124 {
125 }
126 
127 inline
128 Transport::Error::Error(const std::string& msg)
129  : std::runtime_error(msg)
130 {
131 }
132 
133 inline
135 {
136 }
137 
138 inline void
139 Transport::connect(boost::asio::io_service& ioService,
140  const ReceiveCallback& receiveCallback)
141 {
142  m_ioService = &ioService;
143  m_receiveCallback = receiveCallback;
144 }
145 
146 inline bool
148 {
149  return m_isConnected;
150 }
151 
152 inline bool
154 {
155  return m_isExpectingData;
156 }
157 
158 inline void
160 {
161  m_receiveCallback(wire);
162 }
163 
164 } // namespace ndn
165 
166 #endif // NDN_TRANSPORT_TRANSPORT_HPP
virtual void connect(boost::asio::io_service &io_service, const ReceiveCallback &receiveCallback)
Connect transport.
Definition: transport.hpp:139
Copyright (c) 2011-2015 Regents of the University of California.
Copyright (c) 2013-2015 Regents of the University of California.
Error(const boost::system::error_code &code, const std::string &msg)
Definition: transport.hpp:122
STL namespace.
function< void()> ErrorCallback
Definition: transport.hpp:50
Class representing a wire element of NDN-TLV packet format.
Definition: block.hpp:43
ReceiveCallback m_receiveCallback
Definition: transport.hpp:110
bool m_isExpectingData
Definition: transport.hpp:109
function< void(const Block &wire)> ReceiveCallback
Definition: transport.hpp:49
boost::asio::io_service * m_ioService
Definition: transport.hpp:107
void receive(const Block &wire)
Definition: transport.hpp:159
bool isConnected()
Definition: transport.hpp:147
virtual ~Transport()
Definition: transport.hpp:134
bool isExpectingData()
Definition: transport.hpp:153