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