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_UDP_CHANNEL_HPP |
| 26 | #define NFD_DAEMON_FACE_UDP_CHANNEL_HPP |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 27 | |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 28 | #include "channel.hpp" |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 29 | #include "core/global-io.hpp" |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 30 | #include "udp-face.hpp" |
| 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 |
| 40 | * |
| 41 | * |
| 42 | */ |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 43 | class UdpChannel : public Channel |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 44 | { |
| 45 | public: |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 46 | /** |
| 47 | * \brief Exception of UdpChannel |
| 48 | */ |
| 49 | struct Error : public std::runtime_error |
| 50 | { |
| 51 | Error(const std::string& what) : runtime_error(what) {} |
| 52 | }; |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 53 | |
| 54 | /** |
| 55 | * \brief Create UDP channel for the local endpoint |
| 56 | * |
| 57 | * To enable creation of faces upon incoming connections, |
| 58 | * one needs to explicitly call UdpChannel::listen method. |
| 59 | * The created socket is bound to the localEndpoint. |
| 60 | * reuse_address option is set |
| 61 | * |
| 62 | * \throw UdpChannel::Error if bind on the socket fails |
| 63 | */ |
| 64 | UdpChannel(const udp::Endpoint& localEndpoint, |
Alexander Afanasyev | eb3197f | 2014-03-17 19:28:18 -0700 | [diff] [blame] | 65 | const time::seconds& timeout); |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 66 | |
| 67 | /** |
| 68 | * \brief Enable listening on the local endpoint, accept connections, |
| 69 | * and create faces when remote host makes a connection |
| 70 | * \param onFaceCreated Callback to notify successful creation of the face |
| 71 | * \param onAcceptFailed Callback to notify when channel fails |
| 72 | * |
Alexander Afanasyev | 5959b01 | 2014-06-02 19:18:12 +0300 | [diff] [blame] | 73 | * Once a face is created, if it doesn't send/receive anything for |
| 74 | * a period of time equal to timeout, it will be destroyed |
| 75 | * \todo this functionality has to be implemented |
| 76 | * |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 77 | * \throws UdpChannel::Error if called multiple times |
| 78 | */ |
| 79 | void |
| 80 | listen(const FaceCreatedCallback& onFaceCreated, |
| 81 | const ConnectFailedCallback& onAcceptFailed); |
| 82 | |
| 83 | /** |
| 84 | * \brief Create a face by establishing connection to remote endpoint |
| 85 | * |
| 86 | * \throw UdpChannel::Error if bind or connect on the socket fail |
| 87 | */ |
| 88 | void |
| 89 | connect(const udp::Endpoint& remoteEndpoint, |
Steve DiBenedetto | ca53ac6 | 2014-03-27 19:58:40 -0600 | [diff] [blame] | 90 | const FaceCreatedCallback& onFaceCreated, |
| 91 | const ConnectFailedCallback& onConnectFailed); |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 92 | /** |
| 93 | * \brief Create a face by establishing connection to the specified |
| 94 | * remote host and remote port |
| 95 | * |
| 96 | * This method will never block and will return immediately. All |
| 97 | * necessary hostname and port resolution and connection will happen |
| 98 | * in asynchronous mode. |
| 99 | * |
| 100 | * If connection cannot be established within specified timeout, it |
| 101 | * will be aborted. |
| 102 | */ |
| 103 | void |
| 104 | connect(const std::string& remoteHost, const std::string& remotePort, |
| 105 | const FaceCreatedCallback& onFaceCreated, |
| 106 | const ConnectFailedCallback& onConnectFailed); |
| 107 | |
| 108 | /** |
| 109 | * \brief Get number of faces in the channel |
| 110 | */ |
| 111 | size_t |
| 112 | size() const; |
| 113 | |
| 114 | private: |
| 115 | shared_ptr<UdpFace> |
| 116 | createFace(const shared_ptr<boost::asio::ip::udp::socket>& socket, |
Giulio Grassi | 69871f0 | 2014-03-09 16:14:44 +0100 | [diff] [blame] | 117 | const FaceCreatedCallback& onFaceCreated, |
Alexander Afanasyev | 355c066 | 2014-03-20 18:08:17 -0700 | [diff] [blame] | 118 | bool isOnDemand); |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 119 | void |
| 120 | afterFaceFailed(udp::Endpoint& endpoint); |
| 121 | |
| 122 | /** |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 123 | * \brief The UdpChannel has received a new pkt from a remote endpoint not yet |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 124 | * associated with any UdpFace |
| 125 | */ |
| 126 | void |
Alexander Afanasyev | f698028 | 2014-05-13 18:28:40 -0700 | [diff] [blame] | 127 | newPeer(const boost::system::error_code& error, std::size_t nBytesReceived); |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 128 | |
| 129 | void |
| 130 | handleEndpointResolution(const boost::system::error_code& error, |
| 131 | boost::asio::ip::udp::resolver::iterator remoteEndpoint, |
| 132 | const FaceCreatedCallback& onFaceCreated, |
| 133 | const ConnectFailedCallback& onConnectFailed, |
| 134 | const shared_ptr<boost::asio::ip::udp::resolver>& resolver); |
| 135 | |
| 136 | private: |
| 137 | udp::Endpoint m_localEndpoint; |
| 138 | |
| 139 | /** |
| 140 | * \brief Endpoint used to store the information about the last new remote endpoint |
| 141 | */ |
| 142 | udp::Endpoint m_newRemoteEndpoint; |
| 143 | |
| 144 | /** |
| 145 | * Callbacks for face creation. |
| 146 | * New communications are detected using async_receive_from. |
| 147 | * Its handler has a fixed signature. No space for the face callback |
| 148 | */ |
| 149 | FaceCreatedCallback onFaceCreatedNewPeerCallback; |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 150 | |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 151 | // @todo remove the onConnectFailedNewPeerCallback if it remains unused |
| 152 | ConnectFailedCallback onConnectFailedNewPeerCallback; |
| 153 | |
| 154 | /** |
| 155 | * \brief Socket used to "accept" new communication |
| 156 | **/ |
| 157 | shared_ptr<boost::asio::ip::udp::socket> m_socket; |
| 158 | |
| 159 | uint8_t m_inputBuffer[MAX_NDN_PACKET_SIZE]; |
| 160 | |
| 161 | typedef std::map< udp::Endpoint, shared_ptr<UdpFace> > ChannelFaceMap; |
| 162 | ChannelFaceMap m_channelFaces; |
| 163 | |
| 164 | /** |
| 165 | * \brief If true, it means the function listen has already been called |
| 166 | */ |
| 167 | bool m_isListening; |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 168 | |
Giulio Grassi | 69871f0 | 2014-03-09 16:14:44 +0100 | [diff] [blame] | 169 | /** |
Alexander Afanasyev | 355c066 | 2014-03-20 18:08:17 -0700 | [diff] [blame] | 170 | * \brief every time m_idleFaceTimeout expires all the idle (and on-demand) |
Giulio Grassi | 69871f0 | 2014-03-09 16:14:44 +0100 | [diff] [blame] | 171 | * faces will be removed |
| 172 | */ |
Alexander Afanasyev | eb3197f | 2014-03-17 19:28:18 -0700 | [diff] [blame] | 173 | time::seconds m_idleFaceTimeout; |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 174 | }; |
| 175 | |
| 176 | } // namespace nfd |
| 177 | |
Alexander Afanasyev | 613e2a9 | 2014-04-15 13:36:58 -0700 | [diff] [blame] | 178 | #endif // NFD_DAEMON_FACE_UDP_CHANNEL_HPP |