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