blob: 808d0f1d10ed63a64bcf4a7de9c6431a769035a7 [file] [log] [blame]
Alexander Afanasyeva9034b02014-01-26 18:32:02 -08001/* -*- 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 Shi61e3cc52014-03-03 20:40:28 -070010#include "channel.hpp"
Junxiao Shic041ca32014-02-25 20:01:15 -070011#include "core/time.hpp"
12#include <ndn-cpp-dev/util/monotonic_deadline_timer.hpp>
Alexander Afanasyev855a53c2014-01-27 12:03:00 -080013#include "tcp-face.hpp"
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080014
Alexander Afanasyev18bbf812014-01-29 01:40:23 -080015namespace nfd {
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080016
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +010017namespace tcp {
Junxiao Shi61e3cc52014-03-03 20:40:28 -070018typedef boost::asio::ip::tcp::endpoint Endpoint;
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +010019} // namespace tcp
20
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -080021/**
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 Shi61e3cc52014-03-03 20:40:28 -070028class TcpChannel : public Channel
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080029{
30public:
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -080031 /**
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -080032 * \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 Shi61e3cc52014-03-03 20:40:28 -070037 explicit
38 TcpChannel(const tcp::Endpoint& localEndpoint);
39
40 virtual
41 ~TcpChannel();
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -080042
43 /**
44 * \brief Enable listening on the local endpoint, accept connections,
45 * and create faces when remote host makes a connection
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -080046 * \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 Afanasyev8ad71ba2014-01-27 00:07:14 -080051 */
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080052 void
Alexander Afanasyev855a53c2014-01-27 12:03:00 -080053 listen(const FaceCreatedCallback& onFaceCreated,
54 const ConnectFailedCallback& onAcceptFailed,
55 int backlog = boost::asio::ip::tcp::acceptor::max_connections);
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080056
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -080057 /**
58 * \brief Create a face by establishing connection to remote endpoint
59 */
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080060 void
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -080061 connect(const tcp::Endpoint& remoteEndpoint,
Alexander Afanasyev855a53c2014-01-27 12:03:00 -080062 const FaceCreatedCallback& onFaceCreated,
63 const ConnectFailedCallback& onConnectFailed,
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -080064 const time::Duration& timeout = time::seconds(4));
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080065
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -080066 /**
67 * \brief Create a face by establishing connection to the specified
68 * remote host and remote port
69 *
Alexander Afanasyev855a53c2014-01-27 12:03:00 -080070 * This method will never block and will return immediately. All
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -080071 * 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 Afanasyev855a53c2014-01-27 12:03:00 -080079 const FaceCreatedCallback& onFaceCreated,
80 const ConnectFailedCallback& onConnectFailed,
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -080081 const time::Duration& timeout = time::seconds(4));
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -080082
83 /**
84 * \brief Get number of faces in the channel
85 */
86 size_t
Junxiao Shi61e3cc52014-03-03 20:40:28 -070087 size() const;
88
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080089private:
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -080090 void
Alexander Afanasyeva16abd22014-01-31 18:12:29 -080091 createFace(const shared_ptr<boost::asio::ip::tcp::socket>& socket,
92 const FaceCreatedCallback& onFaceCreated);
93
94 void
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -080095 afterFaceFailed(tcp::Endpoint &endpoint);
96
97 void
Alexander Afanasyeva16abd22014-01-31 18:12:29 -080098 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 Afanasyev8ad71ba2014-01-27 00:07:14 -0800102
103 void
104 handleSuccessfulConnect(const boost::system::error_code& error,
105 const shared_ptr<boost::asio::ip::tcp::socket>& socket,
Alexander Afanasyev855a53c2014-01-27 12:03:00 -0800106 const shared_ptr<boost::asio::monotonic_deadline_timer>& timer,
107 const FaceCreatedCallback& onFaceCreated,
108 const ConnectFailedCallback& onConnectFailed);
Junxiao Shi61e3cc52014-03-03 20:40:28 -0700109
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -0800110 void
111 handleFailedConnect(const boost::system::error_code& error,
112 const shared_ptr<boost::asio::ip::tcp::socket>& socket,
Alexander Afanasyev855a53c2014-01-27 12:03:00 -0800113 const shared_ptr<boost::asio::monotonic_deadline_timer>& timer,
114 const ConnectFailedCallback& onConnectFailed);
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -0800115
116 void
Davide Pesavento855bf292014-02-27 03:58:01 +0100117 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 Afanasyeva0a10fb2014-02-13 19:56:15 -0800124
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -0800125private:
Alexander Afanasyeva9034b02014-01-26 18:32:02 -0800126 tcp::Endpoint m_localEndpoint;
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -0800127
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800128 typedef std::map< tcp::Endpoint, shared_ptr<Face> > ChannelFaceMap;
Alexander Afanasyev334b7142014-01-28 12:59:39 -0800129 ChannelFaceMap m_channelFaces;
130
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -0800131 bool isListening;
132 shared_ptr<boost::asio::ip::tcp::acceptor> m_acceptor;
Alexander Afanasyeva9034b02014-01-26 18:32:02 -0800133};
134
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800135} // namespace nfd
Junxiao Shi61e3cc52014-03-03 20:40:28 -0700136
Alexander Afanasyeva9034b02014-01-26 18:32:02 -0800137#endif // NFD_FACE_TCP_CHANNEL_HPP