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