Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [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_TCP_CHANNEL_HPP |
| 8 | #define NFD_FACE_TCP_CHANNEL_HPP |
| 9 | |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 10 | #include "channel.hpp" |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 11 | #include <ndn-cpp-dev/util/monotonic_deadline_timer.hpp> |
Alexander Afanasyev | 855a53c | 2014-01-27 12:03:00 -0800 | [diff] [blame] | 12 | #include "tcp-face.hpp" |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 13 | |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 14 | namespace nfd { |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 15 | |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 16 | namespace tcp { |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 17 | typedef boost::asio::ip::tcp::endpoint Endpoint; |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 18 | } // namespace tcp |
| 19 | |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 20 | /** |
| 21 | * \brief Class implementing TCP-based channel to create faces |
| 22 | * |
| 23 | * Channel can create faces as a response to incoming TCP |
| 24 | * connections (TcpChannel::listen needs to be called for that |
| 25 | * to work) or explicitly after using TcpChannel::connect method. |
| 26 | */ |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 27 | class TcpChannel : public Channel |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 28 | { |
| 29 | public: |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 30 | /** |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 31 | * \brief Create TCP channel for the local endpoint |
| 32 | * |
| 33 | * To enable creation faces upon incoming connections, |
| 34 | * one needs to explicitly call TcpChannel::listen method. |
| 35 | */ |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 36 | explicit |
| 37 | TcpChannel(const tcp::Endpoint& localEndpoint); |
| 38 | |
| 39 | virtual |
| 40 | ~TcpChannel(); |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 41 | |
| 42 | /** |
| 43 | * \brief Enable listening on the local endpoint, accept connections, |
| 44 | * and create faces when remote host makes a connection |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 45 | * \param onFaceCreated Callback to notify successful creation of the face |
| 46 | * \param onAcceptFailed Callback to notify when channel fails (accept call |
| 47 | * returns an error) |
| 48 | * \param backlog The maximum length of the queue of pending incoming |
| 49 | * connections |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 50 | */ |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 51 | void |
Alexander Afanasyev | 855a53c | 2014-01-27 12:03:00 -0800 | [diff] [blame] | 52 | listen(const FaceCreatedCallback& onFaceCreated, |
| 53 | const ConnectFailedCallback& onAcceptFailed, |
| 54 | int backlog = boost::asio::ip::tcp::acceptor::max_connections); |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 55 | |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 56 | /** |
| 57 | * \brief Create a face by establishing connection to remote endpoint |
| 58 | */ |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 59 | void |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 60 | connect(const tcp::Endpoint& remoteEndpoint, |
Alexander Afanasyev | 855a53c | 2014-01-27 12:03:00 -0800 | [diff] [blame] | 61 | const FaceCreatedCallback& onFaceCreated, |
| 62 | const ConnectFailedCallback& onConnectFailed, |
Alexander Afanasyev | eb3197f | 2014-03-17 19:28:18 -0700 | [diff] [blame] | 63 | const time::seconds& timeout = time::seconds(4)); |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 64 | |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 65 | /** |
| 66 | * \brief Create a face by establishing connection to the specified |
| 67 | * remote host and remote port |
| 68 | * |
Alexander Afanasyev | 855a53c | 2014-01-27 12:03:00 -0800 | [diff] [blame] | 69 | * This method will never block and will return immediately. All |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 70 | * necessary hostname and port resolution and connection will happen |
| 71 | * in asynchronous mode. |
| 72 | * |
| 73 | * If connection cannot be established within specified timeout, it |
| 74 | * will be aborted. |
| 75 | */ |
| 76 | void |
| 77 | connect(const std::string& remoteHost, const std::string& remotePort, |
Alexander Afanasyev | 855a53c | 2014-01-27 12:03:00 -0800 | [diff] [blame] | 78 | const FaceCreatedCallback& onFaceCreated, |
| 79 | const ConnectFailedCallback& onConnectFailed, |
Alexander Afanasyev | eb3197f | 2014-03-17 19:28:18 -0700 | [diff] [blame] | 80 | const time::seconds& timeout = time::seconds(4)); |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 81 | |
| 82 | /** |
| 83 | * \brief Get number of faces in the channel |
| 84 | */ |
| 85 | size_t |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 86 | size() const; |
| 87 | |
Alexander Afanasyev | 53a6fd3 | 2014-03-23 00:00:04 -0700 | [diff] [blame] | 88 | bool |
| 89 | isListening() const; |
| 90 | |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 91 | private: |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 92 | void |
Alexander Afanasyev | a16abd2 | 2014-01-31 18:12:29 -0800 | [diff] [blame] | 93 | createFace(const shared_ptr<boost::asio::ip::tcp::socket>& socket, |
Alexander Afanasyev | 355c066 | 2014-03-20 18:08:17 -0700 | [diff] [blame] | 94 | const FaceCreatedCallback& onFaceCreated, |
| 95 | bool isOnDemand); |
Alexander Afanasyev | a16abd2 | 2014-01-31 18:12:29 -0800 | [diff] [blame] | 96 | |
| 97 | void |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 98 | afterFaceFailed(tcp::Endpoint &endpoint); |
| 99 | |
| 100 | void |
Alexander Afanasyev | a16abd2 | 2014-01-31 18:12:29 -0800 | [diff] [blame] | 101 | handleSuccessfulAccept(const boost::system::error_code& error, |
| 102 | const shared_ptr<boost::asio::ip::tcp::socket>& socket, |
| 103 | const FaceCreatedCallback& onFaceCreated, |
| 104 | const ConnectFailedCallback& onConnectFailed); |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 105 | |
| 106 | void |
| 107 | handleSuccessfulConnect(const boost::system::error_code& error, |
| 108 | const shared_ptr<boost::asio::ip::tcp::socket>& socket, |
Alexander Afanasyev | eb3197f | 2014-03-17 19:28:18 -0700 | [diff] [blame] | 109 | const shared_ptr<ndn::monotonic_deadline_timer>& timer, |
Alexander Afanasyev | 855a53c | 2014-01-27 12:03:00 -0800 | [diff] [blame] | 110 | const FaceCreatedCallback& onFaceCreated, |
| 111 | const ConnectFailedCallback& onConnectFailed); |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 112 | |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 113 | void |
| 114 | handleFailedConnect(const boost::system::error_code& error, |
| 115 | const shared_ptr<boost::asio::ip::tcp::socket>& socket, |
Alexander Afanasyev | eb3197f | 2014-03-17 19:28:18 -0700 | [diff] [blame] | 116 | const shared_ptr<ndn::monotonic_deadline_timer>& timer, |
Alexander Afanasyev | 855a53c | 2014-01-27 12:03:00 -0800 | [diff] [blame] | 117 | const ConnectFailedCallback& onConnectFailed); |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 118 | |
| 119 | void |
Davide Pesavento | 855bf29 | 2014-02-27 03:58:01 +0100 | [diff] [blame] | 120 | handleEndpointResolution(const boost::system::error_code& error, |
| 121 | boost::asio::ip::tcp::resolver::iterator remoteEndpoint, |
| 122 | const shared_ptr<boost::asio::ip::tcp::socket>& socket, |
Alexander Afanasyev | eb3197f | 2014-03-17 19:28:18 -0700 | [diff] [blame] | 123 | const shared_ptr<ndn::monotonic_deadline_timer>& timer, |
Davide Pesavento | 855bf29 | 2014-02-27 03:58:01 +0100 | [diff] [blame] | 124 | const FaceCreatedCallback& onFaceCreated, |
| 125 | const ConnectFailedCallback& onConnectFailed, |
| 126 | const shared_ptr<boost::asio::ip::tcp::resolver>& resolver); |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 127 | |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 128 | private: |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 129 | tcp::Endpoint m_localEndpoint; |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 130 | |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 131 | typedef std::map< tcp::Endpoint, shared_ptr<Face> > ChannelFaceMap; |
Alexander Afanasyev | 334b714 | 2014-01-28 12:59:39 -0800 | [diff] [blame] | 132 | ChannelFaceMap m_channelFaces; |
| 133 | |
Alexander Afanasyev | 53a6fd3 | 2014-03-23 00:00:04 -0700 | [diff] [blame] | 134 | bool m_isListening; |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 135 | shared_ptr<boost::asio::ip::tcp::acceptor> m_acceptor; |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 136 | }; |
| 137 | |
Alexander Afanasyev | 53a6fd3 | 2014-03-23 00:00:04 -0700 | [diff] [blame] | 138 | inline bool |
| 139 | TcpChannel::isListening() const |
| 140 | { |
| 141 | return m_isListening; |
| 142 | } |
| 143 | |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 144 | } // namespace nfd |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 145 | |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 146 | #endif // NFD_FACE_TCP_CHANNEL_HPP |