NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.5: NDN, CCN, CCNx, content centric networks
API Documentation
face.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2013-2017 Regents of the University of California.
4  *
5  * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
6  *
7  * ndn-cxx library is free software: you can redistribute it and/or modify it under the
8  * terms of the GNU Lesser General Public License as published by the Free Software
9  * Foundation, either version 3 of the License, or (at your option) any later version.
10  *
11  * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
12  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13  * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
14  *
15  * You should have received copies of the GNU General Public License and GNU Lesser
16  * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
17  * <http://www.gnu.org/licenses/>.
18  *
19  * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
20  */
21 
22 #include "face.hpp"
23 #include "detail/face-impl.hpp"
24 
25 #include "encoding/tlv.hpp"
26 #include "net/face-uri.hpp"
28 #include "util/random.hpp"
29 #include "util/time.hpp"
30 
31 #include "ns3/node-list.h"
32 #include "ns3/ndnSIM/helper/ndn-stack-helper.hpp"
33 #include "ns3/ndnSIM/NFD/daemon/face/generic-link-service.hpp"
34 #include "ns3/ndnSIM/NFD/daemon/face/internal-transport.hpp"
35 
36 // NDN_LOG_INIT(ndn.Face) is declared in face-impl.hpp
37 
38 // A callback scheduled through io.post and io.dispatch may be invoked after the face
39 // is destructed. To prevent this situation, these macros captures Face::m_impl as weak_ptr,
40 // and skips callback execution if the face has been destructed.
41 
42 #define IO_CAPTURE_WEAK_IMPL(OP) \
43  { \
44  weak_ptr<Impl> implWeak(m_impl); \
45  m_impl->m_scheduler.scheduleEvent(time::seconds(0), [=] { \
46  auto impl = implWeak.lock(); \
47  if (impl != nullptr) {
48 #define IO_CAPTURE_WEAK_IMPL_END \
49  } \
50  }); \
51  }
52 
53 namespace ndn {
54 
55 Face::OversizedPacketError::OversizedPacketError(char pktType, const Name& name, size_t wireSize)
56  : Error((pktType == 'I' ? "Interest " : pktType == 'D' ? "Data " : "Nack ") +
57  name.toUri() + " encodes into " + to_string(wireSize) + " octets, "
58  "exceeding the implementation limit of " + to_string(MAX_NDN_PACKET_SIZE) + " octets")
59  , pktType(pktType)
60  , name(name)
61  , wireSize(wireSize)
62 {
63 }
64 
65 Face::Face(shared_ptr<Transport> transport)
66  : m_impl(new Impl(*this))
67 {
68  construct(transport, ns3::ndn::StackHelper::getKeyChain());
69 }
70 
71 Face::Face(boost::asio::io_service& ioService)
72  : m_impl(new Impl(*this))
73 {
74  construct(nullptr, ns3::ndn::StackHelper::getKeyChain());
75 }
76 
77 Face::Face(shared_ptr<Transport> transport, KeyChain& keyChain)
78  : m_impl(new Impl(*this))
79 {
80  construct(std::move(transport), keyChain);
81 }
82 
83 Face::Face(shared_ptr<Transport> transport, boost::asio::io_service& ioService)
84  : m_impl(new Impl(*this))
85 {
86  construct(transport, ns3::ndn::StackHelper::getKeyChain());
87 }
88 
89 Face::Face(shared_ptr<Transport> transport, boost::asio::io_service& ioService, KeyChain& keyChain)
90  : m_impl(new Impl(*this))
91 {
92  construct(std::move(transport), keyChain);
93 }
94 
95 shared_ptr<Transport>
96 Face::makeDefaultTransport()
97 {
98  ns3::Ptr<ns3::Node> node = ns3::NodeList::GetNode(ns3::Simulator::GetContext());
99  NS_ASSERT_MSG(node->GetObject<ns3::ndn::L3Protocol>() != 0,
100  "NDN stack should be installed on the node " << node);
101 
102  auto uri = ::nfd::FaceUri("ndnFace://" + boost::lexical_cast<std::string>(node->GetId()));
103 
105  serviceOpts.allowLocalFields = true;
106 
107  auto nfdFace = make_shared<::nfd::Face>(make_unique<::nfd::face::GenericLinkService>(serviceOpts),
108  make_unique<::nfd::face::InternalForwarderTransport>(uri, uri));
109  auto forwarderTransport = static_cast<::nfd::face::InternalForwarderTransport*>(nfdFace->getTransport());
110 
111  auto clientTransport = make_shared<::nfd::face::InternalClientTransport>();
112  clientTransport->connectToForwarder(forwarderTransport);
113 
114  node->GetObject<ns3::ndn::L3Protocol>()->addFace(nfdFace);;
115 
116  return clientTransport;
117 }
118 
119 void
120 Face::construct(shared_ptr<Transport> transport, KeyChain& keyChain)
121 {
122  if (transport == nullptr) {
123  transport = makeDefaultTransport();
124  }
125  BOOST_ASSERT(transport != nullptr);
126  m_transport = std::move(transport);
127 
128  m_nfdController = make_unique<nfd::Controller>(*this, keyChain);
129 
130  IO_CAPTURE_WEAK_IMPL(post) {
131  impl->ensureConnected(false);
133 }
134 
135 Face::~Face() = default;
136 
137 shared_ptr<Transport>
138 Face::getTransport()
139 {
140  return m_transport;
141 }
142 
143 const PendingInterestId*
145  const DataCallback& afterSatisfied,
146  const NackCallback& afterNacked,
147  const TimeoutCallback& afterTimeout)
148 {
149  shared_ptr<Interest> interest2 = make_shared<Interest>(interest);
150  interest2->getNonce();
151 
152  IO_CAPTURE_WEAK_IMPL(dispatch) {
153  impl->asyncExpressInterest(interest2, afterSatisfied, afterNacked, afterTimeout);
155 
156  return reinterpret_cast<const PendingInterestId*>(interest2.get());
157 }
158 
159 void
160 Face::removePendingInterest(const PendingInterestId* pendingInterestId)
161 {
162  IO_CAPTURE_WEAK_IMPL(post) {
163  impl->asyncRemovePendingInterest(pendingInterestId);
165 }
166 
167 void
169 {
170  IO_CAPTURE_WEAK_IMPL(post) {
171  impl->asyncRemoveAllPendingInterests();
173 }
174 
175 size_t
177 {
178  return m_impl->m_pendingInterestTable.size();
179 }
180 
181 void
183 {
184  IO_CAPTURE_WEAK_IMPL(dispatch) {
185  impl->asyncPutData(data);
187 }
188 
189 void
191 {
192  IO_CAPTURE_WEAK_IMPL(dispatch) {
193  impl->asyncPutNack(nack);
195 }
196 
197 const RegisteredPrefixId*
199  const InterestCallback& onInterest,
200  const RegisterPrefixFailureCallback& onFailure,
201  const security::SigningInfo& signingInfo,
202  uint64_t flags)
203 {
204  return setInterestFilter(interestFilter, onInterest, nullptr, onFailure, signingInfo, flags);
205 }
206 
207 const RegisteredPrefixId*
209  const InterestCallback& onInterest,
210  const RegisterPrefixSuccessCallback& onSuccess,
211  const RegisterPrefixFailureCallback& onFailure,
212  const security::SigningInfo& signingInfo,
213  uint64_t flags)
214 {
215  auto filter = make_shared<InterestFilterRecord>(interestFilter, onInterest);
216 
217  nfd::CommandOptions options;
218  options.setSigningInfo(signingInfo);
219 
220  return m_impl->registerPrefix(interestFilter.getPrefix(), filter,
221  onSuccess, onFailure, flags, options);
222 }
223 
224 const InterestFilterId*
226  const InterestCallback& onInterest)
227 {
228  auto filter = make_shared<InterestFilterRecord>(interestFilter, onInterest);
229 
230  IO_CAPTURE_WEAK_IMPL(post) {
231  impl->asyncSetInterestFilter(filter);
233 
234  return reinterpret_cast<const InterestFilterId*>(filter.get());
235 }
236 
237 const RegisteredPrefixId*
239  const RegisterPrefixSuccessCallback& onSuccess,
240  const RegisterPrefixFailureCallback& onFailure,
241  const security::SigningInfo& signingInfo,
242  uint64_t flags)
243 {
244  nfd::CommandOptions options;
245  options.setSigningInfo(signingInfo);
246 
247  return m_impl->registerPrefix(prefix, nullptr, onSuccess, onFailure, flags, options);
248 }
249 
250 void
251 Face::unsetInterestFilter(const RegisteredPrefixId* registeredPrefixId)
252 {
253  IO_CAPTURE_WEAK_IMPL(post) {
254  impl->asyncUnregisterPrefix(registeredPrefixId, nullptr, nullptr);
256 }
257 
258 void
259 Face::unsetInterestFilter(const InterestFilterId* interestFilterId)
260 {
261  IO_CAPTURE_WEAK_IMPL(post) {
262  impl->asyncUnsetInterestFilter(interestFilterId);
264 }
265 
266 void
267 Face::unregisterPrefix(const RegisteredPrefixId* registeredPrefixId,
268  const UnregisterPrefixSuccessCallback& onSuccess,
269  const UnregisterPrefixFailureCallback& onFailure)
270 {
271  IO_CAPTURE_WEAK_IMPL(post) {
272  impl->asyncUnregisterPrefix(registeredPrefixId, onSuccess, onFailure);
274 }
275 
276 void
277 Face::doProcessEvents(time::milliseconds timeout, bool keepThread)
278 {
279 }
280 
281 void
283 {
284  IO_CAPTURE_WEAK_IMPL(post) {
285  this->asyncShutdown();
287 }
288 
289 void
290 Face::asyncShutdown()
291 {
292  m_impl->m_pendingInterestTable.clear();
293  m_impl->m_registeredPrefixTable.clear();
294 
295  if (m_transport->isConnected())
296  m_transport->close();
297 }
298 
302 template<typename NetPkt>
303 static void
304 extractLpLocalFields(NetPkt& netPacket, const lp::Packet& lpPacket)
305 {
306  addTagFromField<lp::IncomingFaceIdTag, lp::IncomingFaceIdField>(netPacket, lpPacket);
307  addTagFromField<lp::CongestionMarkTag, lp::CongestionMarkField>(netPacket, lpPacket);
308 
309  if (lpPacket.has<lp::HopCountTagField>()) {
310  netPacket.setTag(make_shared<lp::HopCountTag>(lpPacket.get<lp::HopCountTagField>() + 1));
311  }
312 }
313 
314 void
315 Face::onReceiveElement(const Block& blockFromDaemon)
316 {
317  lp::Packet lpPacket(blockFromDaemon); // bare Interest/Data is a valid lp::Packet,
318  // no need to distinguish
319 
320  Buffer::const_iterator begin, end;
321  std::tie(begin, end) = lpPacket.get<lp::FragmentField>();
322  Block netPacket(&*begin, std::distance(begin, end));
323  switch (netPacket.type()) {
324  case tlv::Interest: {
325  auto interest = make_shared<Interest>(netPacket);
326  if (lpPacket.has<lp::NackField>()) {
327  auto nack = make_shared<lp::Nack>(std::move(*interest));
328  nack->setHeader(lpPacket.get<lp::NackField>());
329  extractLpLocalFields(*nack, lpPacket);
330  NDN_LOG_DEBUG(">N " << nack->getInterest() << '~' << nack->getHeader().getReason());
331  m_impl->nackPendingInterests(*nack);
332  }
333  else {
334  extractLpLocalFields(*interest, lpPacket);
335  NDN_LOG_DEBUG(">I " << *interest);
336  m_impl->processIncomingInterest(std::move(interest));
337  }
338  break;
339  }
340  case tlv::Data: {
341  auto data = make_shared<Data>(netPacket);
342  extractLpLocalFields(*data, lpPacket);
343  NDN_LOG_DEBUG(">D " << data->getName());
344  m_impl->satisfyPendingInterests(*data);
345  break;
346  }
347  }
348 }
349 
350 } // namespace ndn
Copyright (c) 2011-2015 Regents of the University of California.
virtual void doProcessEvents(time::milliseconds timeout, bool keepThread)
Definition: face.cpp:277
The interface of signing key management.
Definition: key-chain.hpp:46
function< void(const std::string &)> UnregisterPrefixFailureCallback
Callback invoked when unregisterPrefix or unsetInterestFilter command fails.
Definition: face.hpp:85
virtual ~Face()
#define IO_CAPTURE_WEAK_IMPL_END
Definition: face.cpp:48
bool allowLocalFields
enables encoding of IncomingFaceId, and decoding of NextHopFaceId and CachePolicy ...
const RegisteredPrefixId * setInterestFilter(const InterestFilter &interestFilter, const InterestCallback &onInterest, const RegisterPrefixFailureCallback &onFailure, const security::SigningInfo &signingInfo=security::SigningInfo(), uint64_t flags=nfd::ROUTE_FLAG_CHILD_INHERIT)
Set InterestFilter to dispatch incoming matching interest to onInterest callback and register the fil...
Definition: face.cpp:198
declares the set of Interests a producer can serve, which starts with a name prefix, plus an optional regular expression
represents an Interest packet
Definition: interest.hpp:42
bool has() const
Definition: packet.hpp:78
static void extractLpLocalFields(NetPkt &netPacket, const lp::Packet &lpPacket)
extract local fields from NDNLPv2 packet and tag onto a network layer packet
Definition: face.cpp:304
Signing parameters passed to KeyChain.
void unregisterPrefix(const RegisteredPrefixId *registeredPrefixId, const UnregisterPrefixSuccessCallback &onSuccess, const UnregisterPrefixFailureCallback &onFailure)
Unregister prefix from RIB.
Definition: face.cpp:267
represents a Network Nack
Definition: nack.hpp:40
void removeAllPendingInterests()
Cancel all previously expressed Interests.
Definition: face.cpp:168
FIELD::ValueType get(size_t index=0) const
Definition: packet.hpp:101
#define NDN_LOG_DEBUG(expression)
Definition: logger.hpp:35
implements a forwarder-side transport that can be paired with another
contains options for ControlCommand execution
size_t getNPendingInterests() const
Get number of pending Interests.
Definition: face.cpp:176
void shutdown()
Shutdown face operations.
Definition: face.cpp:282
function< void(const Name &, const std::string &)> RegisterPrefixFailureCallback
Callback invoked when registerPrefix or setInterestFilter command fails.
Definition: face.hpp:75
function< void(const Name &)> RegisterPrefixSuccessCallback
Callback invoked when registerPrefix or setInterestFilter command succeeds.
Definition: face.hpp:70
Represents an absolute name.
Definition: name.hpp:42
represents the underlying protocol and address used by a Face
Definition: face-uri.hpp:44
void unsetInterestFilter(const RegisteredPrefixId *registeredPrefixId)
Remove the registered prefix entry with the registeredPrefixId.
Definition: face.cpp:251
CommandOptions & setSigningInfo(const security::SigningInfo &signingInfo)
sets signing parameters
Implementation network-layer of NDN stack.
function< void(const InterestFilter &, const Interest &)> InterestCallback
Callback invoked when incoming Interest matches the specified InterestFilter.
Definition: face.hpp:65
Options that control the behavior of GenericLinkService.
#define IO_CAPTURE_WEAK_IMPL(OP)
Definition: face.cpp:42
function< void()> UnregisterPrefixSuccessCallback
Callback invoked when unregisterPrefix or unsetInterestFilter command succeeds.
Definition: face.hpp:80
static KeyChain & getKeyChain()
void put(Data data)
Publish data packet.
Definition: face.cpp:182
OversizedPacketError(char pktType, const Name &name, size_t wireSize)
Constructor.
Definition: face.cpp:55
std::string to_string(const V &v)
Definition: backports.hpp:84
const PendingInterestId * expressInterest(const Interest &interest, const DataCallback &afterSatisfied, const NackCallback &afterNacked, const TimeoutCallback &afterTimeout)
Express Interest.
Definition: face.cpp:144
function< void(const Interest &)> TimeoutCallback
Callback invoked when expressed Interest times out.
Definition: face.hpp:60
function< void(const Interest &, const lp::Nack &)> NackCallback
Callback invoked when Nack is sent in response to expressed Interest.
Definition: face.hpp:55
Represents a Data packet.
Definition: data.hpp:35
const RegisteredPrefixId * registerPrefix(const Name &prefix, const RegisterPrefixSuccessCallback &onSuccess, const RegisterPrefixFailureCallback &onFailure, const security::SigningInfo &signingInfo=security::SigningInfo(), uint64_t flags=nfd::ROUTE_FLAG_CHILD_INHERIT)
Register prefix with the connected NDN forwarder.
Definition: face.cpp:238
Face(shared_ptr< Transport > transport=nullptr)
Create Face using given transport (or default transport if omitted)
Definition: face.cpp:65
ndn security v2 KeyChain
Definition: key-chain.cpp:70
function< void(const Interest &, const Data &)> DataCallback
Callback invoked when expressed Interest gets satisfied with a Data packet.
Definition: face.hpp:50
void removePendingInterest(const PendingInterestId *pendingInterestId)
Cancel previously expressed Interest.
Definition: face.cpp:160
const size_t MAX_NDN_PACKET_SIZE
practical limit of network layer packet size
Definition: tlv.hpp:39