Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (C) 2014 Named Data Networking Project |
| 4 | * See COPYING for copyright and distribution information. |
| 5 | */ |
| 6 | |
| 7 | #ifndef NFD_FACE_DATAGRAM_FACE_HPP |
| 8 | #define NFD_FACE_DATAGRAM_FACE_HPP |
| 9 | |
| 10 | #include "face.hpp" |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 11 | #include "core/logger.hpp" |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 12 | |
| 13 | namespace nfd { |
| 14 | |
Davide Pesavento | d8d4d98 | 2014-03-21 18:47:58 +0100 | [diff] [blame] | 15 | template <class Protocol> |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 16 | class DatagramFace : public Face |
| 17 | { |
| 18 | public: |
Davide Pesavento | d8d4d98 | 2014-03-21 18:47:58 +0100 | [diff] [blame] | 19 | typedef Protocol protocol; |
Alexander Afanasyev | 355c066 | 2014-03-20 18:08:17 -0700 | [diff] [blame] | 20 | |
| 21 | /** \brief Construct datagram face |
| 22 | * |
| 23 | * \param uri FaceUri for the face |
| 24 | * \param socket Protocol-specific socket for the created face |
| 25 | * \param isOnDemand If true, the face can be closed after it remains |
Davide Pesavento | d8d4d98 | 2014-03-21 18:47:58 +0100 | [diff] [blame] | 26 | * unused for a certain amount of time |
Alexander Afanasyev | 355c066 | 2014-03-20 18:08:17 -0700 | [diff] [blame] | 27 | */ |
Alexander Afanasyev | a39b90b | 2014-03-05 15:31:00 +0000 | [diff] [blame] | 28 | DatagramFace(const FaceUri& uri, |
Giulio Grassi | 69871f0 | 2014-03-09 16:14:44 +0100 | [diff] [blame] | 29 | const shared_ptr<typename protocol::socket>& socket, |
Alexander Afanasyev | 355c066 | 2014-03-20 18:08:17 -0700 | [diff] [blame] | 30 | bool isOnDemand); |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 31 | |
| 32 | virtual |
| 33 | ~DatagramFace(); |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 34 | |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 35 | // from Face |
| 36 | virtual void |
| 37 | sendInterest(const Interest& interest); |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 38 | |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 39 | virtual void |
| 40 | sendData(const Data& data); |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 41 | |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 42 | virtual void |
| 43 | close(); |
| 44 | |
| 45 | void |
| 46 | handleSend(const boost::system::error_code& error, |
| 47 | const Block& wire); |
| 48 | |
| 49 | void |
| 50 | handleReceive(const boost::system::error_code& error, |
| 51 | size_t nBytesReceived); |
| 52 | |
Giulio Grassi | 69871f0 | 2014-03-09 16:14:44 +0100 | [diff] [blame] | 53 | /** |
| 54 | * \brief Set m_hasBeenUsedRecently to false |
| 55 | */ |
| 56 | void |
| 57 | resetRecentUsage(); |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 58 | |
Giulio Grassi | 69871f0 | 2014-03-09 16:14:44 +0100 | [diff] [blame] | 59 | bool |
| 60 | hasBeenUsedRecently() const; |
Alexander Afanasyev | 355c066 | 2014-03-20 18:08:17 -0700 | [diff] [blame] | 61 | |
| 62 | void |
| 63 | setOnDemand(bool isOnDemand); |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 64 | |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 65 | protected: |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 66 | void |
| 67 | receiveDatagram(const uint8_t* buffer, |
| 68 | size_t nBytesReceived, |
| 69 | const boost::system::error_code& error); |
| 70 | |
| 71 | void |
| 72 | keepFaceAliveUntilAllHandlersExecuted(const shared_ptr<Face>& face); |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 73 | |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 74 | void |
| 75 | closeSocket(); |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 76 | |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 77 | protected: |
| 78 | shared_ptr<typename protocol::socket> m_socket; |
| 79 | uint8_t m_inputBuffer[MAX_NDN_PACKET_SIZE]; |
| 80 | |
Giulio Grassi | 69871f0 | 2014-03-09 16:14:44 +0100 | [diff] [blame] | 81 | bool m_hasBeenUsedRecently; |
| 82 | |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 83 | NFD_LOG_INCLASS_DECLARE(); |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 84 | }; |
| 85 | |
| 86 | template <class T> |
| 87 | inline |
Alexander Afanasyev | a39b90b | 2014-03-05 15:31:00 +0000 | [diff] [blame] | 88 | DatagramFace<T>::DatagramFace(const FaceUri& uri, |
Giulio Grassi | 69871f0 | 2014-03-09 16:14:44 +0100 | [diff] [blame] | 89 | const shared_ptr<typename DatagramFace::protocol::socket>& socket, |
Alexander Afanasyev | 355c066 | 2014-03-20 18:08:17 -0700 | [diff] [blame] | 90 | bool isOnDemand) |
Alexander Afanasyev | a39b90b | 2014-03-05 15:31:00 +0000 | [diff] [blame] | 91 | : Face(uri) |
| 92 | , m_socket(socket) |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 93 | { |
Alexander Afanasyev | 355c066 | 2014-03-20 18:08:17 -0700 | [diff] [blame] | 94 | setOnDemand(isOnDemand); |
| 95 | |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 96 | m_socket->async_receive(boost::asio::buffer(m_inputBuffer, MAX_NDN_PACKET_SIZE), 0, |
| 97 | bind(&DatagramFace<T>::handleReceive, this, _1, _2)); |
| 98 | } |
| 99 | |
| 100 | template <class T> |
| 101 | inline |
| 102 | DatagramFace<T>::~DatagramFace() |
| 103 | { |
| 104 | } |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 105 | |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 106 | template <class T> |
| 107 | inline void |
| 108 | DatagramFace<T>::sendInterest(const Interest& interest) |
| 109 | { |
Alexander Afanasyev | 7e698e6 | 2014-03-07 16:48:35 +0000 | [diff] [blame] | 110 | this->onSendInterest(interest); |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 111 | m_socket->async_send(boost::asio::buffer(interest.wireEncode().wire(), |
| 112 | interest.wireEncode().size()), |
| 113 | bind(&DatagramFace<T>::handleSend, this, _1, interest.wireEncode())); |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 114 | |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 115 | // anything else should be done here? |
| 116 | } |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 117 | |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 118 | template <class T> |
| 119 | inline void |
| 120 | DatagramFace<T>::sendData(const Data& data) |
| 121 | { |
Alexander Afanasyev | 7e698e6 | 2014-03-07 16:48:35 +0000 | [diff] [blame] | 122 | this->onSendData(data); |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 123 | m_socket->async_send(boost::asio::buffer(data.wireEncode().wire(), |
| 124 | data.wireEncode().size()), |
| 125 | bind(&DatagramFace<T>::handleSend, this, _1, data.wireEncode())); |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 126 | |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 127 | // anything else should be done here? |
| 128 | } |
| 129 | |
| 130 | template <class T> |
| 131 | inline void |
| 132 | DatagramFace<T>::handleSend(const boost::system::error_code& error, |
| 133 | const Block& wire) |
| 134 | { |
| 135 | if (error != 0) { |
| 136 | if (error == boost::system::errc::operation_canceled) // when socket is closed by someone |
| 137 | return; |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 138 | |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 139 | if (!m_socket->is_open()) |
| 140 | { |
| 141 | onFail("Tunnel closed"); |
| 142 | return; |
| 143 | } |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 144 | |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 145 | NFD_LOG_WARN("[id:" << this->getId() |
| 146 | << ",endpoint:" << m_socket->local_endpoint() |
| 147 | << "] Send operation failed, closing socket: " |
| 148 | << error.category().message(error.value())); |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 149 | |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 150 | closeSocket(); |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 151 | |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 152 | if (error == boost::asio::error::eof) |
| 153 | { |
| 154 | onFail("Tunnel closed"); |
| 155 | } |
| 156 | else |
| 157 | { |
| 158 | onFail("Send operation failed, closing socket: " + |
| 159 | error.category().message(error.value())); |
| 160 | } |
| 161 | return; |
| 162 | } |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 163 | |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 164 | NFD_LOG_TRACE("[id:" << this->getId() |
| 165 | << ",endpoint:" << m_socket->local_endpoint() |
| 166 | << "] Successfully sent: " << wire.size() << " bytes"); |
| 167 | // do nothing (needed to retain validity of wire memory block |
| 168 | } |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 169 | |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 170 | template <class T> |
| 171 | inline void |
| 172 | DatagramFace<T>::close() |
| 173 | { |
| 174 | if (!m_socket->is_open()) |
| 175 | return; |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 176 | |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 177 | NFD_LOG_INFO("[id:" << this->getId() |
| 178 | << ",endpoint:" << m_socket->local_endpoint() |
| 179 | << "] Close tunnel"); |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 180 | |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 181 | closeSocket(); |
| 182 | onFail("Close tunnel"); |
| 183 | } |
| 184 | |
| 185 | template <class T> |
| 186 | inline void |
| 187 | DatagramFace<T>::handleReceive(const boost::system::error_code& error, |
| 188 | size_t nBytesReceived) |
| 189 | { |
Alexander Afanasyev | 7e698e6 | 2014-03-07 16:48:35 +0000 | [diff] [blame] | 190 | NFD_LOG_DEBUG("handleReceive: " << nBytesReceived); |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 191 | receiveDatagram(m_inputBuffer, nBytesReceived, error); |
Giulio Grassi | 69871f0 | 2014-03-09 16:14:44 +0100 | [diff] [blame] | 192 | if (m_socket->is_open()) |
| 193 | m_socket->async_receive(boost::asio::buffer(m_inputBuffer, MAX_NDN_PACKET_SIZE), 0, |
| 194 | bind(&DatagramFace<T>::handleReceive, this, _1, _2)); |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | template <class T> |
| 198 | inline void |
| 199 | DatagramFace<T>::receiveDatagram(const uint8_t* buffer, |
| 200 | size_t nBytesReceived, |
| 201 | const boost::system::error_code& error) |
| 202 | { |
| 203 | if (error != 0 || nBytesReceived == 0) { |
| 204 | if (error == boost::system::errc::operation_canceled) // when socket is closed by someone |
| 205 | return; |
| 206 | |
| 207 | // this should be unnecessary, but just in case |
| 208 | if (!m_socket->is_open()) |
| 209 | { |
| 210 | onFail("Tunnel closed"); |
| 211 | return; |
| 212 | } |
| 213 | |
| 214 | NFD_LOG_WARN("[id:" << this->getId() |
| 215 | << ",endpoint:" << m_socket->local_endpoint() |
| 216 | << "] Receive operation failed: " |
| 217 | << error.category().message(error.value())); |
| 218 | |
| 219 | closeSocket(); |
| 220 | |
| 221 | if (error == boost::asio::error::eof) |
| 222 | { |
| 223 | onFail("Tunnel closed"); |
| 224 | } |
| 225 | else |
| 226 | { |
| 227 | onFail("Receive operation failed, closing socket: " + |
| 228 | error.category().message(error.value())); |
| 229 | } |
| 230 | return; |
| 231 | } |
| 232 | |
| 233 | NFD_LOG_TRACE("[id:" << this->getId() |
| 234 | << ",endpoint:" << m_socket->local_endpoint() |
| 235 | << "] Received: " << nBytesReceived << " bytes"); |
| 236 | |
Alexander Afanasyev | 5a8d8d8 | 2014-03-21 14:08:41 -0700 | [diff] [blame] | 237 | Block element; |
| 238 | bool isOk = Block::fromBuffer(buffer, nBytesReceived, element); |
| 239 | if (!isOk) |
| 240 | { |
| 241 | NFD_LOG_WARN("[id:" << this->getId() |
| 242 | << ",endpoint:" << m_socket->local_endpoint() |
| 243 | << "] Failed to parse incoming packet"); |
| 244 | // This message won't extend the face lifetime |
| 245 | return; |
| 246 | } |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 247 | |
Alexander Afanasyev | 5a8d8d8 | 2014-03-21 14:08:41 -0700 | [diff] [blame] | 248 | if (element.size() != nBytesReceived) |
| 249 | { |
| 250 | NFD_LOG_WARN("[id:" << this->getId() |
| 251 | << ",endpoint:" << m_socket->local_endpoint() |
| 252 | << "] Received datagram size and decoded " |
| 253 | << "element size don't match"); |
| 254 | // This message won't extend the face lifetime |
| 255 | return; |
| 256 | } |
| 257 | |
| 258 | if (!this->decodeAndDispatchInput(element)) |
| 259 | { |
| 260 | NFD_LOG_WARN("[id:" << this->getId() |
| 261 | << ",endpoint:" << m_socket->local_endpoint() |
| 262 | << "] Received unrecognized block of type [" |
| 263 | << element.type() << "]"); |
| 264 | // This message won't extend the face lifetime |
| 265 | return; |
| 266 | } |
| 267 | |
Giulio Grassi | 69871f0 | 2014-03-09 16:14:44 +0100 | [diff] [blame] | 268 | m_hasBeenUsedRecently = true; |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | |
| 272 | template <class T> |
| 273 | inline void |
| 274 | DatagramFace<T>::keepFaceAliveUntilAllHandlersExecuted(const shared_ptr<Face>& face) |
| 275 | { |
| 276 | } |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 277 | |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 278 | template <class T> |
| 279 | inline void |
| 280 | DatagramFace<T>::closeSocket() |
| 281 | { |
Giulio Grassi | 69871f0 | 2014-03-09 16:14:44 +0100 | [diff] [blame] | 282 | NFD_LOG_DEBUG("closeSocket " << m_socket->local_endpoint()); |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 283 | boost::asio::io_service& io = m_socket->get_io_service(); |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 284 | |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 285 | // use the non-throwing variants and ignore errors, if any |
| 286 | boost::system::error_code error; |
| 287 | m_socket->shutdown(protocol::socket::shutdown_both, error); |
| 288 | m_socket->close(error); |
| 289 | // after this, handlers will be called with an error code |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 290 | |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 291 | // ensure that the Face object is alive at least until all pending |
| 292 | // handlers are dispatched |
| 293 | io.post(bind(&DatagramFace<T>::keepFaceAliveUntilAllHandlersExecuted, |
| 294 | this, this->shared_from_this())); |
| 295 | } |
| 296 | |
Giulio Grassi | 69871f0 | 2014-03-09 16:14:44 +0100 | [diff] [blame] | 297 | template <class T> |
| 298 | inline void |
Alexander Afanasyev | 355c066 | 2014-03-20 18:08:17 -0700 | [diff] [blame] | 299 | DatagramFace<T>::setOnDemand(bool isOnDemand) |
Giulio Grassi | 69871f0 | 2014-03-09 16:14:44 +0100 | [diff] [blame] | 300 | { |
Alexander Afanasyev | 355c066 | 2014-03-20 18:08:17 -0700 | [diff] [blame] | 301 | Face::setOnDemand(isOnDemand); |
Giulio Grassi | 69871f0 | 2014-03-09 16:14:44 +0100 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | template <class T> |
| 305 | inline void |
| 306 | DatagramFace<T>::resetRecentUsage() |
| 307 | { |
| 308 | m_hasBeenUsedRecently = false; |
| 309 | } |
| 310 | |
| 311 | template <class T> |
| 312 | inline bool |
| 313 | DatagramFace<T>::hasBeenUsedRecently() const |
| 314 | { |
| 315 | return m_hasBeenUsedRecently; |
| 316 | } |
| 317 | |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 318 | } // namespace nfd |
| 319 | |
| 320 | #endif // NFD_FACE_DATAGRAM_FACE_HPP |