NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.0: NDN, CCN, CCNx, content centric networks
API Documentation
face-impl.hpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
22 #ifndef NDN_DETAIL_FACE_IMPL_HPP
23 #define NDN_DETAIL_FACE_IMPL_HPP
24 
25 #include "../common.hpp"
26 #include "../face.hpp"
27 
28 #include "registered-prefix.hpp"
29 #include "pending-interest.hpp"
31 
32 #include "../util/scheduler.hpp"
33 #include "../util/config-file.hpp"
34 #include "../util/signal.hpp"
35 
36 #include "../transport/transport.hpp"
37 #include "../transport/unix-transport.hpp"
38 #include "../transport/tcp-transport.hpp"
39 
40 #include "../management/nfd-controller.hpp"
41 #include "../management/nfd-command-options.hpp"
42 
43 #include <ns3/ptr.h>
44 #include <ns3/node.h>
45 #include <ns3/node-list.h>
46 #include <ns3/ndnSIM/model/ndn-l3-protocol.hpp>
47 
48 #include "ns3/ndnSIM/NFD/daemon/face/local-face.hpp"
49 
50 namespace ndn {
51 
52 class Face::Impl : noncopyable
53 {
54 public:
55  typedef ContainerWithOnEmptySignal<shared_ptr<PendingInterest>> PendingInterestTable;
56  typedef std::list<shared_ptr<InterestFilterRecord> > InterestFilterTable;
57  typedef ContainerWithOnEmptySignal<shared_ptr<RegisteredPrefix>> RegisteredPrefixTable;
58 
59  class NfdFace : public ::nfd::LocalFace
60  {
61  public:
62  NfdFace(Impl& face, const ::nfd::FaceUri& localUri, const ::nfd::FaceUri& remoteUri)
63  : ::nfd::LocalFace(localUri, remoteUri)
64  , m_appFaceImpl(face)
65  {
66  }
67 
68  public: // from ::nfd::LocalFace
72  virtual void
73  sendInterest(const Interest& interest)
74  {
75  NS_LOG_DEBUG("<< Interest " << interest);
76  shared_ptr<const Interest> interestPtr = interest.shared_from_this();
77  m_appFaceImpl.m_scheduler.scheduleEvent(time::seconds(0), [this, interestPtr] {
78  m_appFaceImpl.processInterestFilters(*interestPtr);
79  });
80  }
81 
85  virtual void
86  sendData(const Data& data)
87  {
88  NS_LOG_DEBUG("<< Data " << data.getName());
89  shared_ptr<const Data> dataPtr = data.shared_from_this();
90  m_appFaceImpl.m_scheduler.scheduleEvent(time::seconds(0), [this, dataPtr] {
91  m_appFaceImpl.satisfyPendingInterests(*dataPtr);
92  });
93  }
94 
100  virtual void
102  {
103  this->fail("close");
104  }
105 
106  private:
107  friend class Impl;
108  Impl& m_appFaceImpl;
109  };
110 
112 
113  explicit
114  Impl(Face& face)
115  : m_face(face)
116  , m_scheduler(m_face.getIoService())
117  {
118  ns3::Ptr<ns3::Node> node = ns3::NodeList::GetNode(ns3::Simulator::GetContext());
119  NS_ASSERT_MSG(node->GetObject<ns3::ndn::L3Protocol>() != 0,
120  "NDN stack should be installed on the node " << node);
121 
122  auto uri = ::nfd::FaceUri("ndnFace://" + boost::lexical_cast<std::string>(node->GetId()));
123  m_nfdFace = make_shared<NfdFace>(*this, uri, uri);
124 
125  node->GetObject<ns3::ndn::L3Protocol>()->addFace(m_nfdFace);
126  }
127 
130 
131  void
133  {
134  for (auto entry = m_pendingInterestTable.begin(); entry != m_pendingInterestTable.end(); ) {
135  if ((*entry)->getInterest().matchesData(data)) {
136  shared_ptr<PendingInterest> matchedEntry = *entry;
137 
138  entry = m_pendingInterestTable.erase(entry);
139 
140  matchedEntry->invokeDataCallback(data);
141  }
142  else
143  ++entry;
144  }
145  }
146 
147  void
149  {
150  for (const auto& filter : m_interestFilterTable) {
151  if (filter->doesMatch(interest.getName())) {
152  filter->invokeInterestCallback(interest);
153  }
154  }
155  }
156 
159 
160  void
161  asyncExpressInterest(const shared_ptr<const Interest>& interest,
162  const OnData& onData, const OnTimeout& onTimeout)
163  {
164  auto entry =
165  m_pendingInterestTable.insert(make_shared<PendingInterest>(interest,
166  onData, onTimeout,
167  ref(m_scheduler))).first;
168  (*entry)->setDeleter([this, entry] { m_pendingInterestTable.erase(entry); });
169 
170  m_nfdFace->emitSignal(onReceiveInterest, *interest);
171  }
172 
173  void
174  asyncRemovePendingInterest(const PendingInterestId* pendingInterestId)
175  {
176  m_pendingInterestTable.remove_if(MatchPendingInterestId(pendingInterestId));
177  }
178 
179  void
180  asyncPutData(const shared_ptr<const Data>& data)
181  {
182  m_nfdFace->emitSignal(onReceiveData, *data);
183  }
184 
187 
188  void
189  asyncSetInterestFilter(const shared_ptr<InterestFilterRecord>& interestFilterRecord)
190  {
191  m_interestFilterTable.push_back(interestFilterRecord);
192  }
193 
194  void
195  asyncUnsetInterestFilter(const InterestFilterId* interestFilterId)
196  {
197  InterestFilterTable::iterator i = std::find_if(m_interestFilterTable.begin(),
198  m_interestFilterTable.end(),
199  MatchInterestFilterId(interestFilterId));
200  if (i != m_interestFilterTable.end())
201  {
202  m_interestFilterTable.erase(i);
203  }
204  }
205 
208 
209  const RegisteredPrefixId*
210  registerPrefix(const Name& prefix,
211  const shared_ptr<InterestFilterRecord>& filter,
212  const RegisterPrefixSuccessCallback& onSuccess,
213  const RegisterPrefixFailureCallback& onFailure,
214  uint64_t flags,
215  const nfd::CommandOptions& options)
216  {
217  using namespace nfd;
218 
219  ControlParameters params;
220  params.setName(prefix);
221  params.setFlags(flags);
222 
223  auto prefixToRegister = make_shared<RegisteredPrefix>(prefix, filter, options);
224 
225  m_face.m_nfdController->start<RibRegisterCommand>(params,
226  bind(&Impl::afterPrefixRegistered, this,
227  prefixToRegister, onSuccess),
228  bind(onFailure, prefixToRegister->getPrefix(), _2),
229  options);
230 
231  return reinterpret_cast<const RegisteredPrefixId*>(prefixToRegister.get());
232  }
233 
234  void
235  afterPrefixRegistered(const shared_ptr<RegisteredPrefix>& registeredPrefix,
236  const RegisterPrefixSuccessCallback& onSuccess)
237  {
238  m_registeredPrefixTable.insert(registeredPrefix);
239 
240  if (static_cast<bool>(registeredPrefix->getFilter())) {
241  // it was a combined operation
242  m_interestFilterTable.push_back(registeredPrefix->getFilter());
243  }
244 
245  if (static_cast<bool>(onSuccess)) {
246  onSuccess(registeredPrefix->getPrefix());
247  }
248  }
249 
250  void
251  asyncUnregisterPrefix(const RegisteredPrefixId* registeredPrefixId,
252  const UnregisterPrefixSuccessCallback& onSuccess,
253  const UnregisterPrefixFailureCallback& onFailure)
254  {
255  using namespace nfd;
256  auto i = std::find_if(m_registeredPrefixTable.begin(),
257  m_registeredPrefixTable.end(),
258  MatchRegisteredPrefixId(registeredPrefixId));
259  if (i != m_registeredPrefixTable.end()) {
260  RegisteredPrefix& record = **i;
261 
262  const shared_ptr<InterestFilterRecord>& filter = record.getFilter();
263 
264  if (filter != nullptr) {
265  // it was a combined operation
266  m_interestFilterTable.remove(filter);
267  }
268 
269  ControlParameters params;
270  params.setName(record.getPrefix());
271  m_face.m_nfdController->start<RibUnregisterCommand>(params,
272  bind(&Impl::finalizeUnregisterPrefix, this, i, onSuccess),
273  bind(onFailure, _2),
274  record.getCommandOptions());
275  }
276  else {
277  if (onFailure != nullptr) {
278  onFailure("Unrecognized PrefixId");
279  }
280  }
281 
282  // there cannot be two registered prefixes with the same id
283  }
284 
285  void
287  const UnregisterPrefixSuccessCallback& onSuccess)
288  {
289  m_registeredPrefixTable.erase(item);
290 
291  if (static_cast<bool>(onSuccess)) {
292  onSuccess();
293  }
294  }
295 
296 private:
297  Face& m_face;
298  util::Scheduler m_scheduler;
299 
300  PendingInterestTable m_pendingInterestTable;
301  InterestFilterTable m_interestFilterTable;
302  RegisteredPrefixTable m_registeredPrefixTable;
303 
304  shared_ptr<NfdFace> m_nfdFace;
305 
306  friend class Face;
307 };
308 
309 } // namespace ndn
310 
311 #endif // NDN_DETAIL_FACE_IMPL_HPP
NfdFace(Impl &face, const ::nfd::FaceUri &localUri, const ::nfd::FaceUri &remoteUri)
Definition: face-impl.hpp:62
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 parameters in a ControlCommand request or response
represents the underlying protocol and address used by a Face
Definition: face-uri.hpp:44
virtual void sendData(const Data &data)
Send Data towards application.
Definition: face-impl.hpp:86
ControlParameters & setFlags(uint64_t flags)
signal::Signal< Face, Data > onReceiveData
fires when a Data is received
Definition: face.hpp:83
represents an Interest packet
Definition: interest.hpp:45
function< void(const Interest &)> OnTimeout
Callback called when expressed Interest times out.
Definition: face.hpp:70
void asyncExpressInterest(const shared_ptr< const Interest > &interest, const OnData &onData, const OnTimeout &onTimeout)
Definition: face-impl.hpp:161
void asyncUnsetInterestFilter(const InterestFilterId *interestFilterId)
Definition: face-impl.hpp:195
represents a face
Definition: face.hpp:57
void asyncUnregisterPrefix(const RegisteredPrefixId *registeredPrefixId, const UnregisterPrefixSuccessCallback &onSuccess, const UnregisterPrefixFailureCallback &onFailure)
Definition: face-impl.hpp:251
const Name & getName() const
Get name of the Data packet.
Definition: data.hpp:343
Table::const_iterator iterator
Definition: cs-internal.hpp:41
void fail(const std::string &reason)
fail the face and raise onFail event if it&#39;s UP; otherwise do nothing
Definition: face.cpp:87
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-common.hpp:38
virtual void close()
Close the face.
Definition: face-impl.hpp:101
void asyncPutData(const shared_ptr< const Data > &data)
Definition: face-impl.hpp:180
void processInterestFilters(const Interest &interest)
Definition: face-impl.hpp:148
Impl(Face &face)
Definition: face-impl.hpp:114
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
boost::asio::io_service & getIoService()
Return nullptr (kept for compatibility)
Definition: face.hpp:548
Implementation network-layer of NDN stack.
void asyncSetInterestFilter(const shared_ptr< InterestFilterRecord > &interestFilterRecord)
Definition: face-impl.hpp:189
const RegisteredPrefixId * registerPrefix(const Name &prefix, const shared_ptr< InterestFilterRecord > &filter, const RegisterPrefixSuccessCallback &onSuccess, const RegisterPrefixFailureCallback &onFailure, uint64_t flags, const nfd::CommandOptions &options)
Definition: face-impl.hpp:210
virtual void sendInterest(const Interest &interest)
Send Interest towards application.
Definition: face-impl.hpp:73
ContainerWithOnEmptySignal< shared_ptr< RegisteredPrefix > > RegisteredPrefixTable
Definition: face-impl.hpp:57
function< void(const Interest &, Data &)> OnData
Callback called when expressed Interest gets satisfied with Data packet.
Definition: face.hpp:65
represents a face
Definition: local-face.hpp:40
std::list< shared_ptr< InterestFilterRecord > > InterestFilterTable
Definition: face-impl.hpp:56
ControlParameters & setName(const Name &name)
void satisfyPendingInterests(const Data &data)
Definition: face-impl.hpp:132
function< void()> UnregisterPrefixSuccessCallback
Callback called when unregisterPrefix or unsetInterestFilter command succeeds.
Definition: face.hpp:90
void afterPrefixRegistered(const shared_ptr< RegisteredPrefix > &registeredPrefix, const RegisterPrefixSuccessCallback &onSuccess)
Definition: face-impl.hpp:235
void asyncRemovePendingInterest(const PendingInterestId *pendingInterestId)
Definition: face-impl.hpp:174
signal::Signal< Face, Interest > onReceiveInterest
fires when an Interest is received
Definition: face.hpp:80
represents a Data packet
Definition: data.hpp:39
ContainerWithOnEmptySignal< shared_ptr< PendingInterest > > PendingInterestTable
Definition: face-impl.hpp:55
void finalizeUnregisterPrefix(RegisteredPrefixTable::iterator item, const UnregisterPrefixSuccessCallback &onSuccess)
Definition: face-impl.hpp:286