NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.0: 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; -*- */
22 #include "face.hpp"
23 
24 #include "ns3/log.h"
25 NS_LOG_COMPONENT_DEFINE("ndn-cxx.Face");
26 
27 #include "detail/face-impl.hpp"
28 
29 #include "encoding/tlv.hpp"
30 #include "security/key-chain.hpp"
32 #include "util/time.hpp"
33 #include "util/random.hpp"
34 #include "util/face-uri.hpp"
35 
36 #include "ns3/ndnSIM/helper/ndn-stack-helper.hpp"
37 
38 namespace ndn {
39 
41  : m_impl(new Impl(*this))
42 {
43  construct();
44 }
45 
46 Face::Face(boost::asio::io_service&)
47  : m_impl(new Impl(*this))
48 {
49  construct();
50 }
51 
52 void
53 Face::construct()
54 {
55  static ::ndn::KeyChain keyChain("pib-dummy", "tpm-dummy");
56 
57  m_nfdController.reset(new nfd::Controller(*this, ns3::ndn::StackHelper::getKeyChain()));
58 }
59 
60 Face::~Face() = default;
61 
62 const PendingInterestId*
63 Face::expressInterest(const Interest& interest, const OnData& onData, const OnTimeout& onTimeout)
64 {
65  NS_LOG_INFO (">> Interest: " << interest.getName());
66 
67  shared_ptr<Interest> interestToExpress = make_shared<Interest>(interest);
68  m_impl->m_scheduler.scheduleEvent(time::seconds(0), [=] {
69  m_impl->asyncExpressInterest(interestToExpress, onData, onTimeout);
70  });
71 
72  return reinterpret_cast<const PendingInterestId*>(interestToExpress.get());
73 }
74 
75 const PendingInterestId*
77  const Interest& tmpl,
78  const OnData& onData, const OnTimeout& onTimeout/* = OnTimeout()*/)
79 {
80  return expressInterest(Interest(tmpl)
81  .setName(name)
82  .setNonce(0),
83  onData, onTimeout);
84 }
85 
86 void
87 Face::put(const Data& data)
88 {
89  NS_LOG_INFO (">> Data: " << data.getName());
90 
91  shared_ptr<const Data> dataPtr;
92  try {
93  dataPtr = data.shared_from_this();
94  }
95  catch (const bad_weak_ptr& e) {
96  NS_LOG_INFO("Face::put WARNING: the supplied Data should be created using make_shared<Data>()");
97  dataPtr = make_shared<Data>(data);
98  }
99 
100  m_impl->m_scheduler.scheduleEvent(time::seconds(0), [=] {
101  m_impl->asyncPutData(dataPtr);
102  });
103 }
104 
105 void
106 Face::removePendingInterest(const PendingInterestId* pendingInterestId)
107 {
108  m_impl->m_scheduler.scheduleEvent(time::seconds(0),
109  [=] {
110  m_impl->asyncRemovePendingInterest(pendingInterestId);
111  });
112 }
113 
114 size_t
116 {
117  return m_impl->m_pendingInterestTable.size();
118 }
119 
120 const RegisteredPrefixId*
122  const OnInterest& onInterest,
123  const RegisterPrefixFailureCallback& onFailure,
124  const security::SigningInfo& signingInfo,
125  uint64_t flags)
126 {
127  return setInterestFilter(interestFilter,
128  onInterest,
130  onFailure,
131  signingInfo,
132  flags);
133 }
134 
135 const RegisteredPrefixId*
137  const OnInterest& onInterest,
138  const RegisterPrefixSuccessCallback& onSuccess,
139  const RegisterPrefixFailureCallback& onFailure,
140  const security::SigningInfo& signingInfo,
141  uint64_t flags)
142 {
143  shared_ptr<InterestFilterRecord> filter =
144  make_shared<InterestFilterRecord>(interestFilter, onInterest);
145 
146  nfd::CommandOptions options;
147  options.setSigningInfo(signingInfo);
148 
149  return m_impl->registerPrefix(interestFilter.getPrefix(), filter,
150  onSuccess, onFailure,
151  flags, options);
152 }
153 
154 const InterestFilterId*
156  const OnInterest& onInterest)
157 {
158  NS_LOG_INFO("Set Interest Filter << " << interestFilter);
159 
160  shared_ptr<InterestFilterRecord> filter =
161  make_shared<InterestFilterRecord>(interestFilter, onInterest);
162 
163  m_impl->m_scheduler.scheduleEvent(time::seconds(0),
164  [=] { m_impl->asyncSetInterestFilter(filter); });
165 
166  return reinterpret_cast<const InterestFilterId*>(filter.get());
167 }
168 
169 #ifdef NDN_FACE_KEEP_DEPRECATED_REGISTRATION_SIGNING
170 
171 const RegisteredPrefixId*
173  const OnInterest& onInterest,
174  const RegisterPrefixSuccessCallback& onSuccess,
175  const RegisterPrefixFailureCallback& onFailure,
176  const IdentityCertificate& certificate,
177  uint64_t flags)
178 {
179  security::SigningInfo signingInfo;
180  if (!certificate.getName().empty()) {
181  signingInfo = signingByCertificate(certificate.getName());
182  }
183  return setInterestFilter(interestFilter, onInterest,
184  onSuccess, onFailure,
185  signingInfo, flags);
186 }
187 
188 const RegisteredPrefixId*
190  const OnInterest& onInterest,
191  const RegisterPrefixFailureCallback& onFailure,
192  const IdentityCertificate& certificate,
193  uint64_t flags)
194 {
195  security::SigningInfo signingInfo;
196  if (!certificate.getName().empty()) {
197  signingInfo = signingByCertificate(certificate.getName());
198  }
199  return setInterestFilter(interestFilter, onInterest,
200  onFailure, signingInfo, flags);
201 }
202 
203 const RegisteredPrefixId*
205  const OnInterest& onInterest,
206  const RegisterPrefixSuccessCallback& onSuccess,
207  const RegisterPrefixFailureCallback& onFailure,
208  const Name& identity,
209  uint64_t flags)
210 {
211  security::SigningInfo signingInfo = signingByIdentity(identity);
212 
213  return setInterestFilter(interestFilter, onInterest,
214  onSuccess, onFailure,
215  signingInfo, flags);
216 }
217 
218 const RegisteredPrefixId*
220  const OnInterest& onInterest,
221  const RegisterPrefixFailureCallback& onFailure,
222  const Name& identity,
223  uint64_t flags)
224 {
225  security::SigningInfo signingInfo = signingByIdentity(identity);
226 
227  return setInterestFilter(interestFilter, onInterest,
228  onFailure, signingInfo, flags);
229 }
230 
231 #endif // NDN_FACE_KEEP_DEPRECATED_REGISTRATION_SIGNING
232 
233 const RegisteredPrefixId*
235  const RegisterPrefixSuccessCallback& onSuccess,
236  const RegisterPrefixFailureCallback& onFailure,
237  const security::SigningInfo& signingInfo,
238  uint64_t flags)
239 {
240 
241  nfd::CommandOptions options;
242  options.setSigningInfo(signingInfo);
243 
244  return m_impl->registerPrefix(prefix, shared_ptr<InterestFilterRecord>(),
245  onSuccess, onFailure,
246  flags, options);
247 }
248 
249 #ifdef NDN_FACE_KEEP_DEPRECATED_REGISTRATION_SIGNING
250 
251 const RegisteredPrefixId*
253  const RegisterPrefixSuccessCallback& onSuccess,
254  const RegisterPrefixFailureCallback& onFailure,
255  const IdentityCertificate& certificate,
256  uint64_t flags)
257 {
258  security::SigningInfo signingInfo;
259  if (!certificate.getName().empty()) {
260  signingInfo = signingByCertificate(certificate.getName());
261  }
262  return registerPrefix(prefix, onSuccess,
263  onFailure, signingInfo, flags);
264 }
265 
266 const RegisteredPrefixId*
268  const RegisterPrefixSuccessCallback& onSuccess,
269  const RegisterPrefixFailureCallback& onFailure,
270  const Name& identity,
271  uint64_t flags)
272 {
273  security::SigningInfo signingInfo = signingByIdentity(identity);
274  return registerPrefix(prefix, onSuccess,
275  onFailure, signingInfo, flags);
276 }
277 #endif // NDN_FACE_KEEP_DEPRECATED_REGISTRATION_SIGNING
278 
279 void
280 Face::unsetInterestFilter(const RegisteredPrefixId* registeredPrefixId)
281 {
282  m_impl->m_scheduler.scheduleEvent(time::seconds(0), [=] {
283  m_impl->asyncUnregisterPrefix(registeredPrefixId,
286 }
287 
288 void
289 Face::unsetInterestFilter(const InterestFilterId* interestFilterId)
290 {
291  m_impl->m_scheduler.scheduleEvent(time::seconds(0), [=] {
292  m_impl->asyncUnsetInterestFilter(interestFilterId);
293  });
294 }
295 
296 void
297 Face::unregisterPrefix(const RegisteredPrefixId* registeredPrefixId,
298  const UnregisterPrefixSuccessCallback& onSuccess,
299  const UnregisterPrefixFailureCallback& onFailure)
300 {
301  m_impl->m_scheduler.scheduleEvent(time::seconds(0), [=] {
302  m_impl->asyncUnregisterPrefix(registeredPrefixId,onSuccess, onFailure);
303  });
304 }
305 
306 void
307 Face::processEvents(const time::milliseconds& timeout/* = time::milliseconds::zero()*/,
308  bool keepThread/* = false*/)
309 {
310 }
311 
312 void
314 {
315  m_impl->m_scheduler.scheduleEvent(time::seconds(0), [=] {
316  m_impl->m_pendingInterestTable.clear();
317  m_impl->m_registeredPrefixTable.clear();
318 
319  m_impl->m_nfdFace->close();
320  });
321 }
322 
323 } // namespace ndn
const Name & getName() const
Definition: interest.hpp:216
Copyright (c) 2011-2015 Regents of the University of California.
function< void(const std::string &)> UnregisterPrefixFailureCallback
Callback called when unregisterPrefix or unsetInterestFilter command fails.
Definition: face.hpp:95
represents an Interest packet
Definition: interest.hpp:45
function< void(const Interest &)> OnTimeout
Callback called when expressed Interest times out.
Definition: face.hpp:70
function< void(const InterestFilter &, const Interest &)> OnInterest
Callback called when incoming Interest matches the specified InterestFilter.
Definition: face.hpp:75
Signing parameters passed to KeyChain.
SigningInfo signingByCertificate(const Name &certName)
void unregisterPrefix(const RegisteredPrefixId *registeredPrefixId, const UnregisterPrefixSuccessCallback &onSuccess, const UnregisterPrefixFailureCallback &onFailure)
Unregister prefix from RIB.
Definition: face.cpp:297
const Name & getName() const
Get name of the Data packet.
Definition: data.hpp:343
Face()
Create a new Face using the default transport (UnixTransport)
Definition: face.cpp:40
contains options for ControlCommand execution
const RegisteredPrefixId * setInterestFilter(const InterestFilter &interestFilter, const OnInterest &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:121
void shutdown()
Shutdown face operations.
Definition: face.cpp:313
SigningInfo signingByIdentity(const Name &identity)
NFD Management protocol - ControlCommand client.
function< void(const Name &, const std::string &)> RegisterPrefixFailureCallback
Callback called when registerPrefix or setInterestFilter command fails.
Definition: face.hpp:85
function< void(const Name &)> RegisterPrefixSuccessCallback
Callback called when registerPrefix or setInterestFilter command succeeds.
Definition: face.hpp:80
Name abstraction to represent an absolute name.
Definition: name.hpp:46
const PendingInterestId * expressInterest(const Interest &interest, const OnData &onData, const OnTimeout &onTimeout=OnTimeout())
Express Interest.
Definition: face.cpp:63
void unsetInterestFilter(const RegisteredPrefixId *registeredPrefixId)
Remove the registered prefix entry with the registeredPrefixId.
Definition: face.cpp:280
CommandOptions & setSigningInfo(const security::SigningInfo &signingInfo)
sets signing parameters
function< void(const Interest &, Data &)> OnData
Callback called when expressed Interest gets satisfied with Data packet.
Definition: face.hpp:65
size_t getNPendingInterests() const
Get number of pending Interests.
Definition: face.cpp:115
bool empty() const
Check if name is emtpy.
Definition: name.hpp:398
function< void()> UnregisterPrefixSuccessCallback
Callback called when unregisterPrefix or unsetInterestFilter command succeeds.
Definition: face.hpp:90
static KeyChain & getKeyChain()
void processEvents(const time::milliseconds &timeout=time::milliseconds::zero(), bool keepThread=false)
Noop (kept for compatibility)
Definition: face.cpp:307
represents a Data packet
Definition: data.hpp:39
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:234
void removePendingInterest(const PendingInterestId *pendingInterestId)
Cancel previously expressed Interest.
Definition: face.cpp:106
void put(const Data &data)
Publish data packet.
Definition: face.cpp:87