Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Yukai Tu | 0a49d34 | 2015-09-13 12:54:22 +0800 | [diff] [blame] | 3 | * Copyright (c) 2014-2015, 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 | * The University of Memphis. |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 10 | * |
| 11 | * This file is part of NFD (Named Data Networking Forwarding Daemon). |
| 12 | * See AUTHORS.md for complete list of NFD authors and contributors. |
| 13 | * |
| 14 | * NFD is free software: you can redistribute it and/or modify it under the terms |
| 15 | * of the GNU General Public License as published by the Free Software Foundation, |
| 16 | * either version 3 of the License, or (at your option) any later version. |
| 17 | * |
| 18 | * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 19 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 20 | * PURPOSE. See the GNU General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU General Public License along with |
| 23 | * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
Junxiao Shi | 5dd26c3 | 2014-07-20 23:15:14 -0700 | [diff] [blame] | 24 | */ |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 25 | |
Alexander Afanasyev | 613e2a9 | 2014-04-15 13:36:58 -0700 | [diff] [blame] | 26 | #ifndef NFD_DAEMON_FACE_UDP_CHANNEL_HPP |
| 27 | #define NFD_DAEMON_FACE_UDP_CHANNEL_HPP |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 28 | |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 29 | #include "channel.hpp" |
Yukai Tu | 0a49d34 | 2015-09-13 12:54:22 +0800 | [diff] [blame] | 30 | #include "lp-face-wrapper.hpp" |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 31 | |
| 32 | namespace nfd { |
| 33 | |
| 34 | namespace udp { |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 35 | typedef boost::asio::ip::udp::endpoint Endpoint; |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 36 | } // namespace udp |
| 37 | |
| 38 | /** |
| 39 | * \brief Class implementing UDP-based channel to create faces |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 40 | */ |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 41 | class UdpChannel : public Channel |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 42 | { |
| 43 | public: |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 44 | /** |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 45 | * \brief Create UDP channel for the local endpoint |
| 46 | * |
| 47 | * To enable creation of faces upon incoming connections, |
| 48 | * one needs to explicitly call UdpChannel::listen method. |
| 49 | * The created socket is bound to the localEndpoint. |
| 50 | * reuse_address option is set |
| 51 | * |
| 52 | * \throw UdpChannel::Error if bind on the socket fails |
| 53 | */ |
| 54 | UdpChannel(const udp::Endpoint& localEndpoint, |
Alexander Afanasyev | eb3197f | 2014-03-17 19:28:18 -0700 | [diff] [blame] | 55 | const time::seconds& timeout); |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 56 | |
| 57 | /** |
| 58 | * \brief Enable listening on the local endpoint, accept connections, |
| 59 | * and create faces when remote host makes a connection |
| 60 | * \param onFaceCreated Callback to notify successful creation of the face |
Alexander Afanasyev | b755e9d | 2015-10-20 17:35:51 -0500 | [diff] [blame] | 61 | * \param onReceiveFailed Callback to notify when channel fails |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 62 | * |
Alexander Afanasyev | 5959b01 | 2014-06-02 19:18:12 +0300 | [diff] [blame] | 63 | * Once a face is created, if it doesn't send/receive anything for |
| 64 | * a period of time equal to timeout, it will be destroyed |
| 65 | * \todo this functionality has to be implemented |
| 66 | * |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 67 | * \throws UdpChannel::Error if called multiple times |
| 68 | */ |
| 69 | void |
| 70 | listen(const FaceCreatedCallback& onFaceCreated, |
Davide Pesavento | 47ab029 | 2015-11-02 18:45:39 +0100 | [diff] [blame] | 71 | const FaceCreationFailedCallback& onReceiveFailed); |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 72 | |
| 73 | /** |
| 74 | * \brief Create a face by establishing connection to remote endpoint |
| 75 | * |
| 76 | * \throw UdpChannel::Error if bind or connect on the socket fail |
| 77 | */ |
| 78 | void |
| 79 | connect(const udp::Endpoint& remoteEndpoint, |
Yukai Tu | 375dcb0 | 2015-08-01 13:04:43 +0800 | [diff] [blame] | 80 | ndn::nfd::FacePersistency persistency, |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 81 | const FaceCreatedCallback& onFaceCreated, |
Davide Pesavento | 47ab029 | 2015-11-02 18:45:39 +0100 | [diff] [blame] | 82 | const FaceCreationFailedCallback& onConnectFailed); |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 83 | |
| 84 | /** |
| 85 | * \brief Get number of faces in the channel |
| 86 | */ |
| 87 | size_t |
| 88 | size() const; |
| 89 | |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 90 | bool |
| 91 | isListening() const; |
| 92 | |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 93 | private: |
Yukai Tu | 0a49d34 | 2015-09-13 12:54:22 +0800 | [diff] [blame] | 94 | std::pair<bool, shared_ptr<face::LpFaceWrapper>> |
Yukai Tu | 375dcb0 | 2015-08-01 13:04:43 +0800 | [diff] [blame] | 95 | createFace(const udp::Endpoint& remoteEndpoint, ndn::nfd::FacePersistency persistency); |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 96 | |
| 97 | /** |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 98 | * \brief The channel has received a new packet from a remote |
| 99 | * endpoint that is not associated with any UdpFace yet |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 100 | */ |
| 101 | void |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 102 | handleNewPeer(const boost::system::error_code& error, |
| 103 | size_t nBytesReceived, |
| 104 | const FaceCreatedCallback& onFaceCreated, |
Davide Pesavento | 47ab029 | 2015-11-02 18:45:39 +0100 | [diff] [blame] | 105 | const FaceCreationFailedCallback& onReceiveFailed); |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 106 | |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 107 | private: |
Yukai Tu | 0a49d34 | 2015-09-13 12:54:22 +0800 | [diff] [blame] | 108 | std::map<udp::Endpoint, shared_ptr<face::LpFaceWrapper>> m_channelFaces; |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 109 | |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 110 | udp::Endpoint m_localEndpoint; |
| 111 | |
| 112 | /** |
Davide Pesavento | 292e5e1 | 2015-03-13 02:08:33 +0100 | [diff] [blame] | 113 | * \brief The latest peer that started communicating with us |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 114 | */ |
Davide Pesavento | 292e5e1 | 2015-03-13 02:08:33 +0100 | [diff] [blame] | 115 | udp::Endpoint m_remoteEndpoint; |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 116 | |
| 117 | /** |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 118 | * \brief Socket used to "accept" new communication |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 119 | */ |
Davide Pesavento | 292e5e1 | 2015-03-13 02:08:33 +0100 | [diff] [blame] | 120 | boost::asio::ip::udp::socket m_socket; |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 121 | |
Giulio Grassi | 69871f0 | 2014-03-09 16:14:44 +0100 | [diff] [blame] | 122 | /** |
Davide Pesavento | 292e5e1 | 2015-03-13 02:08:33 +0100 | [diff] [blame] | 123 | * \brief When this timeout expires, all idle on-demand faces will be closed |
Giulio Grassi | 69871f0 | 2014-03-09 16:14:44 +0100 | [diff] [blame] | 124 | */ |
Alexander Afanasyev | eb3197f | 2014-03-17 19:28:18 -0700 | [diff] [blame] | 125 | time::seconds m_idleFaceTimeout; |
Davide Pesavento | 292e5e1 | 2015-03-13 02:08:33 +0100 | [diff] [blame] | 126 | |
| 127 | uint8_t m_inputBuffer[ndn::MAX_NDN_PACKET_SIZE]; |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 128 | }; |
| 129 | |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 130 | inline bool |
| 131 | UdpChannel::isListening() const |
| 132 | { |
Davide Pesavento | 292e5e1 | 2015-03-13 02:08:33 +0100 | [diff] [blame] | 133 | return m_socket.is_open(); |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 134 | } |
| 135 | |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 136 | } // namespace nfd |
| 137 | |
Alexander Afanasyev | 613e2a9 | 2014-04-15 13:36:58 -0700 | [diff] [blame] | 138 | #endif // NFD_DAEMON_FACE_UDP_CHANNEL_HPP |