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 
66  : m_impl(new Impl(*this))
67 {
68  construct(nullptr, ns3::ndn::StackHelper::getKeyChain());
69 }
70 
71 Face::Face(shared_ptr<Transport> transport)
72  : m_impl(new Impl(*this))
73 {
74  construct(transport, 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 shared_ptr<Transport>
84 Face::makeDefaultTransport()
85 {
86  ns3::Ptr<ns3::Node> node = ns3::NodeList::GetNode(ns3::Simulator::GetContext());
87  NS_ASSERT_MSG(node->GetObject<ns3::ndn::L3Protocol>() != 0,
88  "NDN stack should be installed on the node " << node);
89 
90  auto uri = ::nfd::FaceUri("ndnFace://" + boost::lexical_cast<std::string>(node->GetId()));
91 
93  serviceOpts.allowLocalFields = true;
94 
95  auto nfdFace = make_shared<::nfd::Face>(make_unique<::nfd::face::GenericLinkService>(serviceOpts),
96  make_unique<::nfd::face::InternalForwarderTransport>(uri, uri));
97  auto forwarderTransport = static_cast<::nfd::face::InternalForwarderTransport*>(nfdFace->getTransport());
98 
99  auto clientTransport = make_shared<::nfd::face::InternalClientTransport>();
100  clientTransport->connectToForwarder(forwarderTransport);
101 
102  node->GetObject<ns3::ndn::L3Protocol>()->addFace(nfdFace);;
103 
104  return clientTransport;
105 }
106 
107 void
108 Face::construct(shared_ptr<Transport> transport, KeyChain& keyChain)
109 {
110  if (transport == nullptr) {
111  transport = makeDefaultTransport();
112  }
113  BOOST_ASSERT(transport != nullptr);
114  m_transport = std::move(transport);
115 
116  m_nfdController = make_unique<nfd::Controller>(*this, keyChain);
117 
118  IO_CAPTURE_WEAK_IMPL(post) {
119  impl->ensureConnected(false);
121 }
122 
123 Face::~Face() = default;
124 
125 shared_ptr<Transport>
126 Face::getTransport()
127 {
128  return m_transport;
129 }
130 
131 const PendingInterestId*
133  const DataCallback& afterSatisfied,
134  const NackCallback& afterNacked,
135  const TimeoutCallback& afterTimeout)
136 {
137  shared_ptr<Interest> interest2 = make_shared<Interest>(interest);
138  interest2->getNonce();
139 
140  IO_CAPTURE_WEAK_IMPL(dispatch) {
141  impl->asyncExpressInterest(interest2, afterSatisfied, afterNacked, afterTimeout);
143 
144  return reinterpret_cast<const PendingInterestId*>(interest2.get());
145 }
146 
147 void
148 Face::removePendingInterest(const PendingInterestId* pendingInterestId)
149 {
150  IO_CAPTURE_WEAK_IMPL(post) {
151  impl->asyncRemovePendingInterest(pendingInterestId);
153 }
154 
155 void
157 {
158  IO_CAPTURE_WEAK_IMPL(post) {
159  impl->asyncRemoveAllPendingInterests();
161 }
162 
163 size_t
165 {
166  return m_impl->m_pendingInterestTable.size();
167 }
168 
169 void
171 {
172  IO_CAPTURE_WEAK_IMPL(dispatch) {
173  impl->asyncPutData(data);
175 }
176 
177 void
179 {
180  IO_CAPTURE_WEAK_IMPL(dispatch) {
181  impl->asyncPutNack(nack);
183 }
184 
185 const RegisteredPrefixId*
187  const InterestCallback& onInterest,
188  const RegisterPrefixFailureCallback& onFailure,
189  const security::SigningInfo& signingInfo,
190  uint64_t flags)
191 {
192  return setInterestFilter(interestFilter, onInterest, nullptr, onFailure, signingInfo, flags);
193 }
194 
195 const RegisteredPrefixId*
197  const InterestCallback& onInterest,
198  const RegisterPrefixSuccessCallback& onSuccess,
199  const RegisterPrefixFailureCallback& onFailure,
200  const security::SigningInfo& signingInfo,
201  uint64_t flags)
202 {
203  auto filter = make_shared<InterestFilterRecord>(interestFilter, onInterest);
204 
205  nfd::CommandOptions options;
206  options.setSigningInfo(signingInfo);
207 
208  return m_impl->registerPrefix(interestFilter.getPrefix(), filter,
209  onSuccess, onFailure, flags, options);
210 }
211 
212 const InterestFilterId*
214  const InterestCallback& onInterest)
215 {
216  auto filter = make_shared<InterestFilterRecord>(interestFilter, onInterest);
217 
218  IO_CAPTURE_WEAK_IMPL(post) {
219  impl->asyncSetInterestFilter(filter);
221 
222  return reinterpret_cast<const InterestFilterId*>(filter.get());
223 }
224 
225 const RegisteredPrefixId*
227  const RegisterPrefixSuccessCallback& onSuccess,
228  const RegisterPrefixFailureCallback& onFailure,
229  const security::SigningInfo& signingInfo,
230  uint64_t flags)
231 {
232  nfd::CommandOptions options;
233  options.setSigningInfo(signingInfo);
234 
235  return m_impl->registerPrefix(prefix, nullptr, onSuccess, onFailure, flags, options);
236 }
237 
238 void
239 Face::unsetInterestFilter(const RegisteredPrefixId* registeredPrefixId)
240 {
241  IO_CAPTURE_WEAK_IMPL(post) {
242  impl->asyncUnregisterPrefix(registeredPrefixId, nullptr, nullptr);
244 }
245 
246 void
247 Face::unsetInterestFilter(const InterestFilterId* interestFilterId)
248 {
249  IO_CAPTURE_WEAK_IMPL(post) {
250  impl->asyncUnsetInterestFilter(interestFilterId);
252 }
253 
254 void
255 Face::unregisterPrefix(const RegisteredPrefixId* registeredPrefixId,
256  const UnregisterPrefixSuccessCallback& onSuccess,
257  const UnregisterPrefixFailureCallback& onFailure)
258 {
259  IO_CAPTURE_WEAK_IMPL(post) {
260  impl->asyncUnregisterPrefix(registeredPrefixId, onSuccess, onFailure);
262 }
263 
264 void
265 Face::doProcessEvents(time::milliseconds timeout, bool keepThread)
266 {
267 }
268 
269 void
271 {
272  IO_CAPTURE_WEAK_IMPL(post) {
273  this->asyncShutdown();
275 }
276 
277 void
278 Face::asyncShutdown()
279 {
280  m_impl->m_pendingInterestTable.clear();
281  m_impl->m_registeredPrefixTable.clear();
282 
283  if (m_transport->isConnected())
284  m_transport->close();
285 }
286 
290 template<typename NetPkt>
291 static void
292 extractLpLocalFields(NetPkt& netPacket, const lp::Packet& lpPacket)
293 {
294  addTagFromField<lp::IncomingFaceIdTag, lp::IncomingFaceIdField>(netPacket, lpPacket);
295  addTagFromField<lp::CongestionMarkTag, lp::CongestionMarkField>(netPacket, lpPacket);
296 
297  if (lpPacket.has<lp::HopCountTagField>()) {
298  netPacket.setTag(make_shared<lp::HopCountTag>(lpPacket.get<lp::HopCountTagField>() + 1));
299  }
300 }
301 
302 void
303 Face::onReceiveElement(const Block& blockFromDaemon)
304 {
305  lp::Packet lpPacket(blockFromDaemon); // bare Interest/Data is a valid lp::Packet,
306  // no need to distinguish
307 
308  Buffer::const_iterator begin, end;
309  std::tie(begin, end) = lpPacket.get<lp::FragmentField>();
310  Block netPacket(&*begin, std::distance(begin, end));
311  switch (netPacket.type()) {
312  case tlv::Interest: {
313  auto interest = make_shared<Interest>(netPacket);
314  if (lpPacket.has<lp::NackField>()) {
315  auto nack = make_shared<lp::Nack>(std::move(*interest));
316  nack->setHeader(lpPacket.get<lp::NackField>());
317  extractLpLocalFields(*nack, lpPacket);
318  NDN_LOG_DEBUG(">N " << nack->getInterest() << '~' << nack->getHeader().getReason());
319  m_impl->nackPendingInterests(*nack);
320  }
321  else {
322  extractLpLocalFields(*interest, lpPacket);
323  NDN_LOG_DEBUG(">I " << *interest);
324  m_impl->processIncomingInterest(std::move(interest));
325  }
326  break;
327  }
328  case tlv::Data: {
329  auto data = make_shared<Data>(netPacket);
330  extractLpLocalFields(*data, lpPacket);
331  NDN_LOG_DEBUG(">D " << data->getName());
332  m_impl->satisfyPendingInterests(*data);
333  break;
334  }
335  }
336 }
337 
338 } // namespace ndn
Copyright (c) 2011-2015 Regents of the University of California.
virtual void doProcessEvents(time::milliseconds timeout, bool keepThread)
Definition: face.cpp:265
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:186
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:292
Signing parameters passed to KeyChain.
void unregisterPrefix(const RegisteredPrefixId *registeredPrefixId, const UnregisterPrefixSuccessCallback &onSuccess, const UnregisterPrefixFailureCallback &onFailure)
Unregister prefix from RIB.
Definition: face.cpp:255
represents a Network Nack
Definition: nack.hpp:40
Declare a field.
Definition: field-decl.hpp:178
void removeAllPendingInterests()
Cancel all previously expressed Interests.
Definition: face.cpp:156
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:164
void shutdown()
Shutdown face operations.
Definition: face.cpp:270
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:239
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.
Face(DummyIoService &ioService)
Create Face using default transport and given io_service.
Definition: face.cpp:65
#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:170
OversizedPacketError(char pktType, const Name &name, size_t wireSize)
Constructor.
Definition: face.cpp:55
std::string to_string(const V &v)
Definition: backports.hpp:107
const PendingInterestId * expressInterest(const Interest &interest, const DataCallback &afterSatisfied, const NackCallback &afterNacked, const TimeoutCallback &afterTimeout)
Express Interest.
Definition: face.cpp:132
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:226
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:148
const size_t MAX_NDN_PACKET_SIZE
practical limit of network layer packet size
Definition: tlv.hpp:39