Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Junxiao Shi | 5dd26c3 | 2014-07-20 23:15:14 -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 | * 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" |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 30 | |
| 31 | namespace nfd { |
| 32 | |
| 33 | namespace udp { |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 34 | typedef boost::asio::ip::udp::endpoint Endpoint; |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 35 | } // namespace udp |
| 36 | |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 37 | class UdpFace; |
| 38 | |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 39 | /** |
| 40 | * \brief Class implementing UDP-based channel to create faces |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 41 | */ |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 42 | class UdpChannel : public Channel |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 43 | { |
| 44 | public: |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 45 | /** |
| 46 | * \brief Exception of UdpChannel |
| 47 | */ |
| 48 | struct Error : public std::runtime_error |
| 49 | { |
| 50 | Error(const std::string& what) : runtime_error(what) {} |
| 51 | }; |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 52 | |
| 53 | /** |
| 54 | * \brief Create UDP channel for the local endpoint |
| 55 | * |
| 56 | * To enable creation of faces upon incoming connections, |
| 57 | * one needs to explicitly call UdpChannel::listen method. |
| 58 | * The created socket is bound to the localEndpoint. |
| 59 | * reuse_address option is set |
| 60 | * |
| 61 | * \throw UdpChannel::Error if bind on the socket fails |
| 62 | */ |
| 63 | UdpChannel(const udp::Endpoint& localEndpoint, |
Alexander Afanasyev | eb3197f | 2014-03-17 19:28:18 -0700 | [diff] [blame] | 64 | const time::seconds& timeout); |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 65 | |
| 66 | /** |
| 67 | * \brief Enable listening on the local endpoint, accept connections, |
| 68 | * and create faces when remote host makes a connection |
| 69 | * \param onFaceCreated Callback to notify successful creation of the face |
| 70 | * \param onAcceptFailed Callback to notify when channel fails |
| 71 | * |
Alexander Afanasyev | 5959b01 | 2014-06-02 19:18:12 +0300 | [diff] [blame] | 72 | * Once a face is created, if it doesn't send/receive anything for |
| 73 | * a period of time equal to timeout, it will be destroyed |
| 74 | * \todo this functionality has to be implemented |
| 75 | * |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 76 | * \throws UdpChannel::Error if called multiple times |
| 77 | */ |
| 78 | void |
| 79 | listen(const FaceCreatedCallback& onFaceCreated, |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 80 | const ConnectFailedCallback& onReceiveFailed); |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 81 | |
| 82 | /** |
| 83 | * \brief Create a face by establishing connection to remote endpoint |
| 84 | * |
| 85 | * \throw UdpChannel::Error if bind or connect on the socket fail |
| 86 | */ |
| 87 | void |
| 88 | connect(const udp::Endpoint& remoteEndpoint, |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 89 | const FaceCreatedCallback& onFaceCreated, |
| 90 | const ConnectFailedCallback& onConnectFailed); |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 91 | |
| 92 | /** |
| 93 | * \brief Get number of faces in the channel |
| 94 | */ |
| 95 | size_t |
| 96 | size() const; |
| 97 | |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 98 | bool |
| 99 | isListening() const; |
| 100 | |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 101 | private: |
| 102 | shared_ptr<UdpFace> |
| 103 | createFace(const shared_ptr<boost::asio::ip::udp::socket>& socket, |
Giulio Grassi | 69871f0 | 2014-03-09 16:14:44 +0100 | [diff] [blame] | 104 | const FaceCreatedCallback& onFaceCreated, |
Alexander Afanasyev | 355c066 | 2014-03-20 18:08:17 -0700 | [diff] [blame] | 105 | bool isOnDemand); |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 106 | |
| 107 | /** |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 108 | * \brief The channel has received a new packet from a remote |
| 109 | * endpoint that is not associated with any UdpFace yet |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 110 | */ |
| 111 | void |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 112 | handleNewPeer(const boost::system::error_code& error, |
| 113 | size_t nBytesReceived, |
| 114 | const FaceCreatedCallback& onFaceCreated, |
| 115 | const ConnectFailedCallback& onReceiveFailed); |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 116 | |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 117 | private: |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 118 | std::map<udp::Endpoint, shared_ptr<UdpFace>> m_channelFaces; |
| 119 | |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 120 | udp::Endpoint m_localEndpoint; |
| 121 | |
| 122 | /** |
| 123 | * \brief Endpoint used to store the information about the last new remote endpoint |
| 124 | */ |
| 125 | udp::Endpoint m_newRemoteEndpoint; |
| 126 | |
| 127 | /** |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 128 | * \brief Socket used to "accept" new communication |
| 129 | **/ |
| 130 | shared_ptr<boost::asio::ip::udp::socket> m_socket; |
| 131 | |
Junxiao Shi | 39cd633 | 2014-11-06 21:53:18 -0700 | [diff] [blame] | 132 | uint8_t m_inputBuffer[ndn::MAX_NDN_PACKET_SIZE]; |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 133 | |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 134 | /** |
| 135 | * \brief If true, it means the function listen has already been called |
| 136 | */ |
| 137 | bool m_isListening; |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 138 | |
Giulio Grassi | 69871f0 | 2014-03-09 16:14:44 +0100 | [diff] [blame] | 139 | /** |
Alexander Afanasyev | 355c066 | 2014-03-20 18:08:17 -0700 | [diff] [blame] | 140 | * \brief every time m_idleFaceTimeout expires all the idle (and on-demand) |
Giulio Grassi | 69871f0 | 2014-03-09 16:14:44 +0100 | [diff] [blame] | 141 | * faces will be removed |
| 142 | */ |
Alexander Afanasyev | eb3197f | 2014-03-17 19:28:18 -0700 | [diff] [blame] | 143 | time::seconds m_idleFaceTimeout; |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 144 | }; |
| 145 | |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 146 | inline bool |
| 147 | UdpChannel::isListening() const |
| 148 | { |
| 149 | return m_isListening; |
| 150 | } |
| 151 | |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 152 | } // namespace nfd |
| 153 | |
Alexander Afanasyev | 613e2a9 | 2014-04-15 13:36:58 -0700 | [diff] [blame] | 154 | #endif // NFD_DAEMON_FACE_UDP_CHANNEL_HPP |