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