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