NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.5: NDN, CCN, CCNx, content centric networks
API Documentation
udp-channel.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2014-2019, 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 #include "udp-channel.hpp"
27 #include "face.hpp"
28 #include "generic-link-service.hpp"
30 #include "common/global.hpp"
31 
32 namespace nfd {
33 namespace face {
34 
36 
37 namespace ip = boost::asio::ip;
38 
40  time::nanoseconds idleTimeout,
41  bool wantCongestionMarking)
42  : m_localEndpoint(localEndpoint)
43  , m_socket(getGlobalIoService())
44  , m_idleFaceTimeout(idleTimeout)
45  , m_wantCongestionMarking(wantCongestionMarking)
46 {
47  setUri(FaceUri(m_localEndpoint));
48  NFD_LOG_CHAN_INFO("Creating channel");
49 }
50 
51 void
52 UdpChannel::connect(const udp::Endpoint& remoteEndpoint,
53  const FaceParams& params,
54  const FaceCreatedCallback& onFaceCreated,
55  const FaceCreationFailedCallback& onConnectFailed)
56 {
57  shared_ptr<Face> face;
58  try {
59  face = createFace(remoteEndpoint, params).second;
60  }
61  catch (const boost::system::system_error& e) {
62  NFD_LOG_CHAN_DEBUG("Face creation for " << remoteEndpoint << " failed: " << e.what());
63  if (onConnectFailed)
64  onConnectFailed(504, "Face creation failed: "s + e.what());
65  return;
66  }
67 
68  // Need to invoke the callback regardless of whether or not we had already
69  // created the face so that control responses and such can be sent
70  onFaceCreated(face);
71 }
72 
73 void
75  const FaceCreationFailedCallback& onFaceCreationFailed)
76 {
77  if (isListening()) {
78  NFD_LOG_CHAN_WARN("Already listening");
79  return;
80  }
81 
82  m_socket.open(m_localEndpoint.protocol());
83  m_socket.set_option(ip::udp::socket::reuse_address(true));
84  if (m_localEndpoint.address().is_v6()) {
85  m_socket.set_option(ip::v6_only(true));
86  }
87  m_socket.bind(m_localEndpoint);
88 
89  waitForNewPeer(onFaceCreated, onFaceCreationFailed);
90  NFD_LOG_CHAN_DEBUG("Started listening");
91 }
92 
93 void
94 UdpChannel::waitForNewPeer(const FaceCreatedCallback& onFaceCreated,
95  const FaceCreationFailedCallback& onReceiveFailed)
96 {
97  m_socket.async_receive_from(boost::asio::buffer(m_receiveBuffer), m_remoteEndpoint,
98  [=] (auto&&... args) {
99  this->handleNewPeer(std::forward<decltype(args)>(args)..., onFaceCreated, onReceiveFailed);
100  });
101 }
102 
103 void
104 UdpChannel::handleNewPeer(const boost::system::error_code& error,
105  size_t nBytesReceived,
106  const FaceCreatedCallback& onFaceCreated,
107  const FaceCreationFailedCallback& onReceiveFailed)
108 {
109  if (error) {
110  if (error != boost::asio::error::operation_aborted) {
111  NFD_LOG_CHAN_DEBUG("Receive failed: " << error.message());
112  if (onReceiveFailed)
113  onReceiveFailed(500, "Receive failed: " + error.message());
114  }
115  return;
116  }
117 
118  NFD_LOG_CHAN_TRACE("New peer " << m_remoteEndpoint);
119 
120  bool isCreated = false;
121  shared_ptr<Face> face;
122  try {
123  FaceParams params;
124  params.persistency = ndn::nfd::FACE_PERSISTENCY_ON_DEMAND;
125  std::tie(isCreated, face) = createFace(m_remoteEndpoint, params);
126  }
127  catch (const boost::system::system_error& e) {
128  NFD_LOG_CHAN_DEBUG("Face creation for " << m_remoteEndpoint << " failed: " << e.what());
129  if (onReceiveFailed)
130  onReceiveFailed(504, "Face creation failed: "s + e.what());
131  return;
132  }
133 
134  if (isCreated)
135  onFaceCreated(face);
136  else
137  NFD_LOG_CHAN_DEBUG("Received datagram for existing face");
138 
139  // dispatch the datagram to the face for processing
140  auto* transport = static_cast<UnicastUdpTransport*>(face->getTransport());
141  transport->receiveDatagram(m_receiveBuffer.data(), nBytesReceived, error);
142 
143  waitForNewPeer(onFaceCreated, onReceiveFailed);
144 }
145 
146 std::pair<bool, shared_ptr<Face>>
147 UdpChannel::createFace(const udp::Endpoint& remoteEndpoint,
148  const FaceParams& params)
149 {
150  auto it = m_channelFaces.find(remoteEndpoint);
151  if (it != m_channelFaces.end()) {
152  // we already have a face for this endpoint, so reuse it
153  NFD_LOG_CHAN_TRACE("Reusing existing face for " << remoteEndpoint);
154  return {false, it->second};
155  }
156 
157  // else, create a new face
158  ip::udp::socket socket(getGlobalIoService(), m_localEndpoint.protocol());
159  socket.set_option(ip::udp::socket::reuse_address(true));
160  socket.bind(m_localEndpoint);
161  socket.connect(remoteEndpoint);
162 
163  GenericLinkService::Options options;
164  options.allowFragmentation = true;
165  options.allowReassembly = true;
166  options.reliabilityOptions.isEnabled = params.wantLpReliability;
167 
168  if (boost::logic::indeterminate(params.wantCongestionMarking)) {
169  // Use default value for this channel if parameter is indeterminate
170  options.allowCongestionMarking = m_wantCongestionMarking;
171  }
172  else {
173  options.allowCongestionMarking = bool(params.wantCongestionMarking);
174  }
175 
176  if (params.baseCongestionMarkingInterval) {
177  options.baseCongestionMarkingInterval = *params.baseCongestionMarkingInterval;
178  }
179  if (params.defaultCongestionThreshold) {
180  options.defaultCongestionThreshold = *params.defaultCongestionThreshold;
181  }
182 
183  auto linkService = make_unique<GenericLinkService>(options);
184  auto transport = make_unique<UnicastUdpTransport>(std::move(socket), params.persistency,
185  m_idleFaceTimeout, params.mtu);
186  auto face = make_shared<Face>(std::move(linkService), std::move(transport));
187 
188  m_channelFaces[remoteEndpoint] = face;
189  connectFaceClosedSignal(*face, [this, remoteEndpoint] { m_channelFaces.erase(remoteEndpoint); });
190 
191  return {true, face};
192 }
193 
194 } // namespace face
195 } // namespace nfd
global.hpp
nonstd::optional_lite::std11::move
T & move(T &t)
Definition: optional.hpp:421
ndn::FaceUri
represents the underlying protocol and address used by a Face
Definition: face-uri.hpp:45
NFD_LOG_CHAN_DEBUG
#define NFD_LOG_CHAN_DEBUG(msg)
Log a message at DEBUG level.
Definition: channel-log.hpp:49
nfd::face::UdpChannel::connect
void connect(const udp::Endpoint &remoteEndpoint, const FaceParams &params, const FaceCreatedCallback &onFaceCreated, const FaceCreationFailedCallback &onConnectFailed)
Create a unicast UDP face toward remoteEndpoint.
Definition: udp-channel.cpp:52
nfd::getGlobalIoService
detail::SimulatorIo & getGlobalIoService()
Returns the global io_service instance for the calling thread.
Definition: global.cpp:49
nfd
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-common.hpp:40
NFD_LOG_CHAN_INFO
#define NFD_LOG_CHAN_INFO(msg)
Log a message at INFO level.
Definition: channel-log.hpp:52
udp-channel.hpp
NFD_LOG_CHAN_WARN
#define NFD_LOG_CHAN_WARN(msg)
Log a message at WARN level.
Definition: channel-log.hpp:55
nfd::face::UdpChannel::isListening
bool isListening() const override
Returns whether the channel is listening.
Definition: udp-channel.hpp:55
NFD_LOG_CHAN_TRACE
#define NFD_LOG_CHAN_TRACE(msg)
Log a message at TRACE level.
Definition: channel-log.hpp:46
nfd::face::connectFaceClosedSignal
void connectFaceClosedSignal(Face &face, std::function< void()> f)
Invokes a callback when a face is closed.
Definition: channel.cpp:41
nfd::face::FaceCreatedCallback
std::function< void(const shared_ptr< Face > &)> FaceCreatedCallback
Prototype for the callback that is invoked when a face is created (in response to an incoming connect...
Definition: channel.hpp:74
ndn::nfd::FACE_PERSISTENCY_ON_DEMAND
@ FACE_PERSISTENCY_ON_DEMAND
face is on-demand
Definition: nfd-constants.hpp:48
unicast-udp-transport.hpp
face.hpp
nfd::face::UdpChannel::listen
void listen(const FaceCreatedCallback &onFaceCreated, const FaceCreationFailedCallback &onFaceCreationFailed)
Start listening.
Definition: udp-channel.cpp:74
nfd::face::FaceParams
Parameters used to set Transport properties or LinkService options on a newly created face.
Definition: face-common.hpp:73
nfd::face::UdpChannel::UdpChannel
UdpChannel(const udp::Endpoint &localEndpoint, time::nanoseconds idleTimeout, bool wantCongestionMarking)
Create a UDP channel on the given localEndpoint.
Definition: udp-channel.cpp:39
nfd::udp::Endpoint
boost::asio::ip::udp::endpoint Endpoint
Definition: udp-protocol.hpp:34
nfd::face::Channel::setUri
void setUri(const FaceUri &uri)
Definition: channel.cpp:35
nfd::face::UdpChannel
Class implementing UDP-based channel to create faces.
Definition: udp-channel.hpp:41
nfd::face::FaceCreationFailedCallback
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:78
NFD_LOG_INIT
#define NFD_LOG_INIT(name)
Definition: logger.hpp:31