blob: 1587b9980d8f444de519441432324f441d07a594 [file] [log] [blame]
Alexander Afanasyeva9034b02014-01-26 18:32:02 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Eric Newberry2642cd22017-07-13 21:34:53 -04002/*
Eric Newberry0c841642018-01-17 15:01:00 -07003 * Copyright (c) 2014-2018, Regents of the University of California,
Junxiao Shi1e46be32015-01-08 20:18:05 -07004 * 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 Afanasyev9bcbc7c2014-04-06 19:37:37 -070010 *
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 Shi1e46be32015-01-08 20:18:05 -070024 */
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080025
Alexander Afanasyev613e2a92014-04-15 13:36:58 -070026#ifndef NFD_DAEMON_FACE_TCP_CHANNEL_HPP
27#define NFD_DAEMON_FACE_TCP_CHANNEL_HPP
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080028
Junxiao Shi61e3cc52014-03-03 20:40:28 -070029#include "channel.hpp"
Alexander Afanasyev18861802014-06-13 17:49:03 -070030#include "core/scheduler.hpp"
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080031
Alexander Afanasyev18bbf812014-01-29 01:40:23 -080032namespace nfd {
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080033
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +010034namespace tcp {
Junxiao Shi61e3cc52014-03-03 20:40:28 -070035typedef boost::asio::ip::tcp::endpoint Endpoint;
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +010036} // namespace tcp
37
Davide Pesavento8fd15e62017-04-06 19:58:54 -040038namespace face {
39
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -080040/**
41 * \brief Class implementing TCP-based channel to create faces
42 *
43 * Channel can create faces as a response to incoming TCP
44 * connections (TcpChannel::listen needs to be called for that
45 * to work) or explicitly after using TcpChannel::connect method.
46 */
Junxiao Shi61e3cc52014-03-03 20:40:28 -070047class TcpChannel : public Channel
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080048{
49public:
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -080050 /**
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -080051 * \brief Create TCP channel for the local endpoint
52 *
53 * To enable creation faces upon incoming connections,
54 * one needs to explicitly call TcpChannel::listen method.
55 */
Eric Newberry0c841642018-01-17 15:01:00 -070056 TcpChannel(const tcp::Endpoint& localEndpoint, bool wantCongestionMarking);
Junxiao Shi61e3cc52014-03-03 20:40:28 -070057
Davide Pesaventoc19408d2017-04-08 00:42:55 -040058 bool
59 isListening() const override
60 {
61 return m_acceptor.is_open();
62 }
63
64 size_t
65 size() const override
66 {
67 return m_channelFaces.size();
68 }
69
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -080070 /**
71 * \brief Enable listening on the local endpoint, accept connections,
72 * and create faces when remote host makes a connection
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -080073 * \param onFaceCreated Callback to notify successful creation of the face
74 * \param onAcceptFailed Callback to notify when channel fails (accept call
75 * returns an error)
76 * \param backlog The maximum length of the queue of pending incoming
77 * connections
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -080078 */
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080079 void
Alexander Afanasyev855a53c2014-01-27 12:03:00 -080080 listen(const FaceCreatedCallback& onFaceCreated,
Davide Pesavento47ab0292015-11-02 18:45:39 +010081 const FaceCreationFailedCallback& onAcceptFailed,
Alexander Afanasyev855a53c2014-01-27 12:03:00 -080082 int backlog = boost::asio::ip::tcp::acceptor::max_connections);
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080083
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -080084 /**
Davide Pesaventoc19408d2017-04-08 00:42:55 -040085 * \brief Create a face by establishing a TCP connection to \p remoteEndpoint
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -080086 */
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080087 void
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -080088 connect(const tcp::Endpoint& remoteEndpoint,
Davide Pesaventoa3c9ddb2017-04-10 22:15:24 -040089 ndn::nfd::FacePersistency persistency,
Eric Newberry2642cd22017-07-13 21:34:53 -040090 bool wantLocalFields,
91 bool wantLpReliability,
Alexander Afanasyev855a53c2014-01-27 12:03:00 -080092 const FaceCreatedCallback& onFaceCreated,
Davide Pesavento47ab0292015-11-02 18:45:39 +010093 const FaceCreationFailedCallback& onConnectFailed,
Davide Pesaventoc19408d2017-04-08 00:42:55 -040094 time::nanoseconds timeout = time::seconds(4));
Alexander Afanasyev53a6fd32014-03-23 00:00:04 -070095
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080096private:
Eric Newberry2642cd22017-07-13 21:34:53 -040097 struct ConnectParams
98 {
99 ndn::nfd::FacePersistency persistency;
100 bool wantLocalFields;
101 bool wantLpReliability;
102 };
103
104private:
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -0800105 void
Yukai Tu16aabbc2015-10-06 05:08:42 -0700106 createFace(boost::asio::ip::tcp::socket&& socket,
Davide Pesavento77911cc2017-04-08 22:12:30 -0400107 ndn::nfd::FacePersistency persistency,
Eric Newberry2642cd22017-07-13 21:34:53 -0400108 bool wantLocalFields,
109 bool wantLpReliability,
Eric Newberryf40551a2016-09-05 15:41:16 -0700110 const FaceCreatedCallback& onFaceCreated);
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800111
112 void
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100113 accept(const FaceCreatedCallback& onFaceCreated,
Davide Pesavento47ab0292015-11-02 18:45:39 +0100114 const FaceCreationFailedCallback& onAcceptFailed);
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800115
116 void
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100117 handleAccept(const boost::system::error_code& error,
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100118 const FaceCreatedCallback& onFaceCreated,
Davide Pesavento47ab0292015-11-02 18:45:39 +0100119 const FaceCreationFailedCallback& onAcceptFailed);
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -0800120
121 void
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100122 handleConnect(const boost::system::error_code& error,
Davide Pesavento77911cc2017-04-08 22:12:30 -0400123 const tcp::Endpoint& remoteEndpoint,
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100124 const shared_ptr<boost::asio::ip::tcp::socket>& socket,
Eric Newberry2642cd22017-07-13 21:34:53 -0400125 ConnectParams params,
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100126 const scheduler::EventId& connectTimeoutEvent,
127 const FaceCreatedCallback& onFaceCreated,
Davide Pesavento47ab0292015-11-02 18:45:39 +0100128 const FaceCreationFailedCallback& onConnectFailed);
Junxiao Shi61e3cc52014-03-03 20:40:28 -0700129
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -0800130 void
Davide Pesavento77911cc2017-04-08 22:12:30 -0400131 handleConnectTimeout(const tcp::Endpoint& remoteEndpoint,
132 const shared_ptr<boost::asio::ip::tcp::socket>& socket,
Davide Pesavento47ab0292015-11-02 18:45:39 +0100133 const FaceCreationFailedCallback& onConnectFailed);
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -0800134
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -0800135private:
Davide Pesavento77911cc2017-04-08 22:12:30 -0400136 const tcp::Endpoint m_localEndpoint;
Davide Pesavento292e5e12015-03-13 02:08:33 +0100137 boost::asio::ip::tcp::acceptor m_acceptor;
Davide Pesavento77911cc2017-04-08 22:12:30 -0400138 boost::asio::ip::tcp::socket m_socket;
139 std::map<tcp::Endpoint, shared_ptr<Face>> m_channelFaces;
Eric Newberry0c841642018-01-17 15:01:00 -0700140 bool m_wantCongestionMarking;
Alexander Afanasyeva9034b02014-01-26 18:32:02 -0800141};
142
Davide Pesavento8fd15e62017-04-06 19:58:54 -0400143} // namespace face
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800144} // namespace nfd
Junxiao Shi61e3cc52014-03-03 20:40:28 -0700145
Alexander Afanasyev613e2a92014-04-15 13:36:58 -0700146#endif // NFD_DAEMON_FACE_TCP_CHANNEL_HPP