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