NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.3: 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-2017, 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 
34 NFD_LOG_INIT("UdpChannel");
35 
36 namespace ip = boost::asio::ip;
37 
39  time::nanoseconds idleTimeout)
40  : m_localEndpoint(localEndpoint)
41  , m_socket(getGlobalIoService())
42  , m_idleFaceTimeout(idleTimeout)
43 {
44  setUri(FaceUri(m_localEndpoint));
45  NFD_LOG_CHAN_INFO("Creating channel");
46 }
47 
48 void
49 UdpChannel::connect(const udp::Endpoint& remoteEndpoint,
50  ndn::nfd::FacePersistency persistency,
51  bool wantLpReliability,
52  const FaceCreatedCallback& onFaceCreated,
53  const FaceCreationFailedCallback& onConnectFailed)
54 {
55  shared_ptr<Face> face;
56  try {
57  face = createFace(remoteEndpoint, persistency, wantLpReliability).second;
58  }
59  catch (const boost::system::system_error& e) {
60  NFD_LOG_CHAN_DEBUG("Face creation for " << remoteEndpoint << " failed: " << e.what());
61  if (onConnectFailed)
62  onConnectFailed(504, std::string("Face creation failed: ") + e.what());
63  return;
64  }
65 
66  // Need to invoke the callback regardless of whether or not we had already
67  // created the face so that control responses and such can be sent
68  onFaceCreated(face);
69 }
70 
71 void
73  const FaceCreationFailedCallback& onFaceCreationFailed)
74 {
75  if (isListening()) {
76  NFD_LOG_CHAN_WARN("Already listening");
77  return;
78  }
79 
80  m_socket.open(m_localEndpoint.protocol());
81  m_socket.set_option(ip::udp::socket::reuse_address(true));
82  if (m_localEndpoint.address().is_v6()) {
83  m_socket.set_option(ip::v6_only(true));
84  }
85  m_socket.bind(m_localEndpoint);
86 
87  waitForNewPeer(onFaceCreated, onFaceCreationFailed);
88  NFD_LOG_CHAN_DEBUG("Started listening");
89 }
90 
91 void
92 UdpChannel::waitForNewPeer(const FaceCreatedCallback& onFaceCreated,
93  const FaceCreationFailedCallback& onReceiveFailed)
94 {
95  m_socket.async_receive_from(boost::asio::buffer(m_receiveBuffer), m_remoteEndpoint,
96  bind(&UdpChannel::handleNewPeer, this,
97  boost::asio::placeholders::error,
98  boost::asio::placeholders::bytes_transferred,
99  onFaceCreated, onReceiveFailed));
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) {
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  std::tie(isCreated, face) = createFace(m_remoteEndpoint, ndn::nfd::FACE_PERSISTENCY_ON_DEMAND,
123  false);
124  }
125  catch (const boost::system::system_error& e) {
126  NFD_LOG_CHAN_DEBUG("Face creation for " << m_remoteEndpoint << " failed: " << e.what());
127  if (onReceiveFailed)
128  onReceiveFailed(504, std::string("Face creation failed: ") + e.what());
129  return;
130  }
131 
132  if (isCreated)
133  onFaceCreated(face);
134  else
135  NFD_LOG_CHAN_DEBUG("Received datagram for existing face");
136 
137  // dispatch the datagram to the face for processing
138  auto* transport = static_cast<UnicastUdpTransport*>(face->getTransport());
139  transport->receiveDatagram(m_receiveBuffer.data(), nBytesReceived, error);
140 
141  waitForNewPeer(onFaceCreated, onReceiveFailed);
142 }
143 
144 std::pair<bool, shared_ptr<Face>>
145 UdpChannel::createFace(const udp::Endpoint& remoteEndpoint,
146  ndn::nfd::FacePersistency persistency,
147  bool wantLpReliability)
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 
163  options.reliabilityOptions.isEnabled = wantLpReliability;
164  auto linkService = make_unique<GenericLinkService>(options);
165  auto transport = make_unique<UnicastUdpTransport>(std::move(socket), persistency, m_idleFaceTimeout);
166  auto face = make_shared<Face>(std::move(linkService), std::move(transport));
167 
168  m_channelFaces[remoteEndpoint] = face;
169  connectFaceClosedSignal(*face, [this, remoteEndpoint] { m_channelFaces.erase(remoteEndpoint); });
170 
171  return {true, face};
172 }
173 
174 } // namespace face
175 } // namespace nfd
bool isListening() const override
Returns whether the channel is listening.
Definition: udp-channel.hpp:54
void setUri(const FaceUri &uri)
Definition: channel.cpp:34
LpReliability::Options reliabilityOptions
options for reliability
detail::SimulatorIo & getGlobalIoService()
Definition: global-io.cpp:48
void connectFaceClosedSignal(Face &face, const std::function< void()> &f)
invokes a callback when the face is closed
Definition: channel.cpp:40
bool isEnabled
enables link-layer reliability
void connect(const udp::Endpoint &remoteEndpoint, ndn::nfd::FacePersistency persistency, bool wantLpReliability, const FaceCreatedCallback &onFaceCreated, const FaceCreationFailedCallback &onConnectFailed)
Create a unicast UDP face toward remoteEndpoint.
Definition: udp-channel.cpp:49
A Transport that communicates on a unicast UDP socket.
void receiveDatagram(const uint8_t *buffer, size_t nBytesReceived, const boost::system::error_code &error)
Receive datagram, translate buffer into packet, deliver to parent class.
#define NFD_LOG_CHAN_DEBUG(msg)
Log a message at DEBUG level.
Definition: channel-log.hpp:49
UdpChannel(const udp::Endpoint &localEndpoint, time::nanoseconds idleTimeout)
Create a UDP channel on the given localEndpoint.
Definition: udp-channel.cpp:38
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
represents the underlying protocol and address used by a Face
Definition: face-uri.hpp:43
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
boost::asio::ip::udp::endpoint Endpoint
Options that control the behavior of GenericLinkService.
void listen(const FaceCreatedCallback &onFaceCreated, const FaceCreationFailedCallback &onFaceCreationFailed)
Start listening.
Definition: udp-channel.cpp:72
Catch-all error for socket component errors that don&#39;t fit in other categories.
Definition: base.hpp:83
#define NFD_LOG_INIT(name)
Definition: logger.hpp:34
function< void(const shared_ptr< Face > &newFace)> FaceCreatedCallback
Prototype for the callback that is invoked when a face is created (in response to an incoming connect...
Definition: channel.hpp:35