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