NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.5: NDN, CCN, CCNx, content centric networks
API Documentation
tcp-channel.hpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2014-2018, 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.
10  *
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/>.
24  */
25 
26 #ifndef NFD_DAEMON_FACE_TCP_CHANNEL_HPP
27 #define NFD_DAEMON_FACE_TCP_CHANNEL_HPP
28 
29 #include "channel.hpp"
30 #include "core/scheduler.hpp"
31 
32 namespace nfd {
33 
34 namespace tcp {
35 typedef boost::asio::ip::tcp::endpoint Endpoint;
36 } // namespace tcp
37 
38 namespace face {
39 
40 using DetermineFaceScopeFromAddress = std::function<ndn::nfd::FaceScope(const boost::asio::ip::address& local,
41  const boost::asio::ip::address& remote)>;
42 
50 class TcpChannel : public Channel
51 {
52 public:
59  TcpChannel(const tcp::Endpoint& localEndpoint, bool wantCongestionMarking,
60  DetermineFaceScopeFromAddress determineFaceScope);
61 
62  bool
63  isListening() const override
64  {
65  return m_acceptor.is_open();
66  }
67 
68  size_t
69  size() const override
70  {
71  return m_channelFaces.size();
72  }
73 
83  void
84  listen(const FaceCreatedCallback& onFaceCreated,
85  const FaceCreationFailedCallback& onAcceptFailed,
86  int backlog = boost::asio::ip::tcp::acceptor::max_connections);
87 
91  void
92  connect(const tcp::Endpoint& remoteEndpoint,
93  const FaceParams& params,
94  const FaceCreatedCallback& onFaceCreated,
95  const FaceCreationFailedCallback& onConnectFailed,
96  time::nanoseconds timeout = 8_s);
97 
98 private:
99  void
100  createFace(boost::asio::ip::tcp::socket&& socket,
101  const FaceParams& params,
102  const FaceCreatedCallback& onFaceCreated);
103 
104  void
105  accept(const FaceCreatedCallback& onFaceCreated,
106  const FaceCreationFailedCallback& onAcceptFailed);
107 
108  void
109  handleAccept(const boost::system::error_code& error,
110  const FaceCreatedCallback& onFaceCreated,
111  const FaceCreationFailedCallback& onAcceptFailed);
112 
113  void
114  handleConnect(const boost::system::error_code& error,
115  const tcp::Endpoint& remoteEndpoint,
116  const shared_ptr<boost::asio::ip::tcp::socket>& socket,
117  const FaceParams& params,
118  const scheduler::EventId& connectTimeoutEvent,
119  const FaceCreatedCallback& onFaceCreated,
120  const FaceCreationFailedCallback& onConnectFailed);
121 
122  void
123  handleConnectTimeout(const tcp::Endpoint& remoteEndpoint,
124  const shared_ptr<boost::asio::ip::tcp::socket>& socket,
125  const FaceCreationFailedCallback& onConnectFailed);
126 
127 private:
128  const tcp::Endpoint m_localEndpoint;
129  boost::asio::ip::tcp::acceptor m_acceptor;
130  boost::asio::ip::tcp::socket m_socket;
131  std::map<tcp::Endpoint, shared_ptr<Face>> m_channelFaces;
132  bool m_wantCongestionMarking;
133  DetermineFaceScopeFromAddress m_determineFaceScope;
134 };
135 
136 } // namespace face
137 } // namespace nfd
138 
139 #endif // NFD_DAEMON_FACE_TCP_CHANNEL_HPP
represent a channel that communicates on a local endpoint
Definition: channel.hpp:52
bool isListening() const override
Returns whether the channel is listening.
Definition: tcp-channel.hpp:63
std::function< ndn::nfd::FaceScope(const boost::asio::ip::address &local, const boost::asio::ip::address &remote)> DetermineFaceScopeFromAddress
Definition: tcp-channel.hpp:41
std::function< void(uint32_t status, const std::string &reason)> FaceCreationFailedCallback
Prototype for the callback that is invoked when a face fails to be created.
Definition: channel.hpp:44
void listen(const FaceCreatedCallback &onFaceCreated, const FaceCreationFailedCallback &onAcceptFailed, int backlog=boost::asio::ip::tcp::acceptor::max_connections)
Enable listening on the local endpoint, accept connections, and create faces when remote host makes a...
Definition: tcp-channel.cpp:51
std::function< void(const shared_ptr< Face > &face)> FaceCreatedCallback
Prototype for the callback that is invoked when a face is created (in response to an incoming connect...
Definition: channel.hpp:40
size_t size() const override
Returns the number of faces in the channel.
Definition: tcp-channel.hpp:69
TcpChannel(const tcp::Endpoint &localEndpoint, bool wantCongestionMarking, DetermineFaceScopeFromAddress determineFaceScope)
Create TCP channel for the local endpoint.
Definition: tcp-channel.cpp:38
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-common.hpp:40
boost::asio::ip::tcp::endpoint Endpoint
Definition: tcp-channel.hpp:35
void connect(const tcp::Endpoint &remoteEndpoint, const FaceParams &params, const FaceCreatedCallback &onFaceCreated, const FaceCreationFailedCallback &onConnectFailed, time::nanoseconds timeout=8_s)
Create a face by establishing a TCP connection to remoteEndpoint.
Definition: tcp-channel.cpp:73
A handle of scheduled event.
Definition: scheduler.hpp:55
Class implementing TCP-based channel to create faces.
Definition: tcp-channel.hpp:50
Parameters used to set Transport properties or LinkService options on a newly created face.
Definition: channel.hpp:87