blob: 25c5fb08ad205e12e35d7e5168fb8ef581cc3123 [file] [log] [blame]
Alexander Afanasyeva9034b02014-01-26 18:32:02 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Junxiao Shi1e46be32015-01-08 20:18:05 -07003 * Copyright (c) 2014-2015, Regents of the University of California,
4 * 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 Afanasyev855a53c2014-01-27 12:03:00 -080030#include "tcp-face.hpp"
Alexander Afanasyev18861802014-06-13 17:49:03 -070031#include "core/scheduler.hpp"
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080032
Alexander Afanasyev18bbf812014-01-29 01:40:23 -080033namespace nfd {
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080034
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +010035namespace tcp {
Junxiao Shi61e3cc52014-03-03 20:40:28 -070036typedef boost::asio::ip::tcp::endpoint Endpoint;
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +010037} // namespace tcp
38
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -080039/**
40 * \brief Class implementing TCP-based channel to create faces
41 *
42 * Channel can create faces as a response to incoming TCP
43 * connections (TcpChannel::listen needs to be called for that
44 * to work) or explicitly after using TcpChannel::connect method.
45 */
Junxiao Shi61e3cc52014-03-03 20:40:28 -070046class TcpChannel : public Channel
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080047{
48public:
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -080049 /**
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -080050 * \brief Create TCP channel for the local endpoint
51 *
52 * To enable creation faces upon incoming connections,
53 * one needs to explicitly call TcpChannel::listen method.
54 */
Junxiao Shi61e3cc52014-03-03 20:40:28 -070055 explicit
56 TcpChannel(const tcp::Endpoint& localEndpoint);
57
58 virtual
59 ~TcpChannel();
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -080060
61 /**
62 * \brief Enable listening on the local endpoint, accept connections,
63 * and create faces when remote host makes a connection
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -080064 * \param onFaceCreated Callback to notify successful creation of the face
65 * \param onAcceptFailed Callback to notify when channel fails (accept call
66 * returns an error)
67 * \param backlog The maximum length of the queue of pending incoming
68 * connections
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -080069 */
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080070 void
Alexander Afanasyev855a53c2014-01-27 12:03:00 -080071 listen(const FaceCreatedCallback& onFaceCreated,
72 const ConnectFailedCallback& onAcceptFailed,
73 int backlog = boost::asio::ip::tcp::acceptor::max_connections);
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080074
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -080075 /**
76 * \brief Create a face by establishing connection to remote endpoint
77 */
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080078 void
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -080079 connect(const tcp::Endpoint& remoteEndpoint,
Alexander Afanasyev855a53c2014-01-27 12:03:00 -080080 const FaceCreatedCallback& onFaceCreated,
81 const ConnectFailedCallback& onConnectFailed,
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -070082 const time::seconds& timeout = time::seconds(4));
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080083
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -080084 /**
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -080085 * \brief Get number of faces in the channel
86 */
87 size_t
Junxiao Shi61e3cc52014-03-03 20:40:28 -070088 size() const;
89
Alexander Afanasyev53a6fd32014-03-23 00:00:04 -070090 bool
91 isListening() const;
92
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080093private:
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -080094 void
Alexander Afanasyeva16abd22014-01-31 18:12:29 -080095 createFace(const shared_ptr<boost::asio::ip::tcp::socket>& socket,
Alexander Afanasyev355c0662014-03-20 18:08:17 -070096 const FaceCreatedCallback& onFaceCreated,
97 bool isOnDemand);
Alexander Afanasyeva16abd22014-01-31 18:12:29 -080098
99 void
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800100 afterFaceFailed(tcp::Endpoint &endpoint);
101
102 void
Alexander Afanasyeva16abd22014-01-31 18:12:29 -0800103 handleSuccessfulAccept(const boost::system::error_code& error,
104 const shared_ptr<boost::asio::ip::tcp::socket>& socket,
105 const FaceCreatedCallback& onFaceCreated,
106 const ConnectFailedCallback& onConnectFailed);
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -0800107
108 void
109 handleSuccessfulConnect(const boost::system::error_code& error,
110 const shared_ptr<boost::asio::ip::tcp::socket>& socket,
Junxiao Shi1e46be32015-01-08 20:18:05 -0700111 const scheduler::EventId& connectTimeoutEvent,
Alexander Afanasyev855a53c2014-01-27 12:03:00 -0800112 const FaceCreatedCallback& onFaceCreated,
113 const ConnectFailedCallback& onConnectFailed);
Junxiao Shi61e3cc52014-03-03 20:40:28 -0700114
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -0800115 void
Alexander Afanasyev18861802014-06-13 17:49:03 -0700116 handleFailedConnect(const shared_ptr<boost::asio::ip::tcp::socket>& socket,
Alexander Afanasyev855a53c2014-01-27 12:03:00 -0800117 const ConnectFailedCallback& onConnectFailed);
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -0800118
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -0800119private:
Alexander Afanasyeva9034b02014-01-26 18:32:02 -0800120 tcp::Endpoint m_localEndpoint;
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -0800121
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800122 typedef std::map< tcp::Endpoint, shared_ptr<Face> > ChannelFaceMap;
Alexander Afanasyev334b7142014-01-28 12:59:39 -0800123 ChannelFaceMap m_channelFaces;
124
Alexander Afanasyev53a6fd32014-03-23 00:00:04 -0700125 bool m_isListening;
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -0800126 shared_ptr<boost::asio::ip::tcp::acceptor> m_acceptor;
Alexander Afanasyeva9034b02014-01-26 18:32:02 -0800127};
128
Alexander Afanasyev53a6fd32014-03-23 00:00:04 -0700129inline bool
130TcpChannel::isListening() const
131{
132 return m_isListening;
133}
134
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800135} // namespace nfd
Junxiao Shi61e3cc52014-03-03 20:40:28 -0700136
Alexander Afanasyev613e2a92014-04-15 13:36:58 -0700137#endif // NFD_DAEMON_FACE_TCP_CHANNEL_HPP