NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.0: 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; -*- */
26 #include "udp-channel.hpp"
27 #include "udp-face.hpp"
28 #include "core/global-io.hpp"
29 
30 namespace nfd {
31 
32 NFD_LOG_INIT("UdpChannel");
33 
34 using namespace boost::asio;
35 
37  const time::seconds& timeout)
38  : m_localEndpoint(localEndpoint)
39  , m_socket(getGlobalIoService())
40  , m_idleFaceTimeout(timeout)
41 {
42  setUri(FaceUri(m_localEndpoint));
43 }
44 
45 void
47  const ConnectFailedCallback& onReceiveFailed)
48 {
49  if (isListening()) {
50  NFD_LOG_WARN("[" << m_localEndpoint << "] Already listening");
51  return;
52  }
53 
54  m_socket.open(m_localEndpoint.protocol());
55  m_socket.set_option(ip::udp::socket::reuse_address(true));
56  if (m_localEndpoint.address().is_v6())
57  m_socket.set_option(ip::v6_only(true));
58 
59  m_socket.bind(m_localEndpoint);
60  m_socket.async_receive_from(boost::asio::buffer(m_inputBuffer, ndn::MAX_NDN_PACKET_SIZE),
61  m_remoteEndpoint,
62  bind(&UdpChannel::handleNewPeer, this,
63  boost::asio::placeholders::error,
64  boost::asio::placeholders::bytes_transferred,
65  onFaceCreated, onReceiveFailed));
66 }
67 
68 void
69 UdpChannel::connect(const udp::Endpoint& remoteEndpoint,
70  ndn::nfd::FacePersistency persistency,
71  const FaceCreatedCallback& onFaceCreated,
72  const ConnectFailedCallback& onConnectFailed)
73 {
74  shared_ptr<UdpFace> face;
75  try {
76  face = createFace(remoteEndpoint, persistency).second;
77  }
78  catch (const boost::system::system_error& e) {
79  NFD_LOG_WARN("[" << m_localEndpoint << "] Connect failed: " << e.what());
80  if (onConnectFailed)
81  onConnectFailed(e.what());
82  return;
83  }
84 
85  // Need to invoke the callback regardless of whether or not we had already
86  // created the face so that control responses and such can be sent
87  onFaceCreated(face);
88 }
89 
90 size_t
92 {
93  return m_channelFaces.size();
94 }
95 
96 std::pair<bool, shared_ptr<UdpFace>>
97 UdpChannel::createFace(const udp::Endpoint& remoteEndpoint, ndn::nfd::FacePersistency persistency)
98 {
99  auto it = m_channelFaces.find(remoteEndpoint);
100  if (it != m_channelFaces.end()) {
101  // we already have a face for this endpoint, just reuse it
102  auto face = it->second;
103  // only on-demand -> persistent -> permanent transition is allowed
104  bool isTransitionAllowed = persistency != face->getPersistency() &&
105  (face->getPersistency() == ndn::nfd::FACE_PERSISTENCY_ON_DEMAND ||
106  persistency == ndn::nfd::FACE_PERSISTENCY_PERMANENT);
107  if (isTransitionAllowed) {
108  face->setPersistency(persistency);
109  }
110  return {false, face};
111  }
112 
113  // else, create a new face
114  ip::udp::socket socket(getGlobalIoService(), m_localEndpoint.protocol());
115  socket.set_option(ip::udp::socket::reuse_address(true));
116  socket.bind(m_localEndpoint);
117  socket.connect(remoteEndpoint);
118 
119  auto face = make_shared<UdpFace>(FaceUri(remoteEndpoint), FaceUri(m_localEndpoint),
120  std::move(socket), persistency, m_idleFaceTimeout);
121 
122  face->onFail.connectSingleShot([this, remoteEndpoint] (const std::string&) {
123  NFD_LOG_TRACE("Erasing " << remoteEndpoint << " from channel face map");
124  m_channelFaces.erase(remoteEndpoint);
125  });
126  m_channelFaces[remoteEndpoint] = face;
127 
128  return {true, face};
129 }
130 
131 void
132 UdpChannel::handleNewPeer(const boost::system::error_code& error,
133  size_t nBytesReceived,
134  const FaceCreatedCallback& onFaceCreated,
135  const ConnectFailedCallback& onReceiveFailed)
136 {
137  if (error) {
138  if (error == boost::asio::error::operation_aborted) // when the socket is closed by someone
139  return;
140 
141  NFD_LOG_DEBUG("[" << m_localEndpoint << "] Receive failed: " << error.message());
142  if (onReceiveFailed)
143  onReceiveFailed(error.message());
144  return;
145  }
146 
147  NFD_LOG_DEBUG("[" << m_localEndpoint << "] New peer " << m_remoteEndpoint);
148 
149  bool created;
150  shared_ptr<UdpFace> face;
151  try {
152  std::tie(created, face) = createFace(m_remoteEndpoint, ndn::nfd::FACE_PERSISTENCY_ON_DEMAND);
153  }
154  catch (const boost::system::system_error& e) {
155  NFD_LOG_WARN("[" << m_localEndpoint << "] Failed to create face for peer "
156  << m_remoteEndpoint << ": " << e.what());
157  if (onReceiveFailed)
158  onReceiveFailed(e.what());
159  return;
160  }
161 
162  if (created)
163  onFaceCreated(face);
164 
165  // dispatch the datagram to the face for processing
166  face->receiveDatagram(m_inputBuffer, nBytesReceived, error);
167 
168  m_socket.async_receive_from(boost::asio::buffer(m_inputBuffer, ndn::MAX_NDN_PACKET_SIZE),
169  m_remoteEndpoint,
170  bind(&UdpChannel::handleNewPeer, this,
171  boost::asio::placeholders::error,
172  boost::asio::placeholders::bytes_transferred,
173  onFaceCreated, onReceiveFailed));
174 }
175 
176 } // namespace nfd
UdpChannel(const udp::Endpoint &localEndpoint, const time::seconds &timeout)
Create UDP channel for the local endpoint.
Definition: udp-channel.cpp:36
#define NFD_LOG_DEBUG(expression)
Definition: logger.hpp:36
bool isListening() const
represents the underlying protocol and address used by a Face
Definition: face-uri.hpp:44
void connect(const udp::Endpoint &remoteEndpoint, ndn::nfd::FacePersistency persistency, const FaceCreatedCallback &onFaceCreated, const ConnectFailedCallback &onConnectFailed)
Create a face by establishing connection to remote endpoint.
Definition: udp-channel.cpp:69
void listen(const FaceCreatedCallback &onFaceCreated, const ConnectFailedCallback &onReceiveFailed)
Enable listening on the local endpoint, accept connections, and create faces when remote host makes a...
Definition: udp-channel.cpp:46
#define NFD_LOG_WARN(expression)
Definition: logger.hpp:39
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-common.hpp:38
void setUri(const FaceUri &uri)
Definition: channel.cpp:34
function< void(const std::string &reason)> ConnectFailedCallback
Prototype for the callback that is called when face is failed to get created.
Definition: channel.hpp:46
boost::asio::ip::udp::endpoint Endpoint
Definition: udp-channel.hpp:34
#define NFD_LOG_INIT(name)
Definition: logger.hpp:33
#define NFD_LOG_TRACE(expression)
Definition: logger.hpp:35
boost::asio::io_service & getGlobalIoService()
Definition: global-io.hpp:35
size_t size() const
Get number of faces in the channel.
Definition: udp-channel.cpp:91
function< void(const shared_ptr< Face > &newFace)> FaceCreatedCallback
Prototype for the callback called when face is created (as a response to incoming connection or after...
Definition: channel.hpp:41
const size_t MAX_NDN_PACKET_SIZE
practical limit of network layer packet size
Definition: tlv.hpp:39