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