NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.5: NDN, CCN, CCNx, content centric networks
API Documentation
pending-interest.hpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2013-2019 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 #ifndef NDN_IMPL_PENDING_INTEREST_HPP
23 #define NDN_IMPL_PENDING_INTEREST_HPP
24 
25 #include "ndn-cxx/data.hpp"
26 #include "ndn-cxx/face.hpp"
27 #include "ndn-cxx/interest.hpp"
28 #include "ndn-cxx/lp/nack.hpp"
30 
31 namespace ndn {
32 
37 {
38  APP,
39  FORWARDER
40 };
41 
42 std::ostream&
43 operator<<(std::ostream& os, PendingInterestOrigin origin)
44 {
45  switch (origin) {
47  return os << "app";
49  return os << "forwarder";
50  }
51  BOOST_ASSERT(false);
52  return os;
53 }
54 
58 class PendingInterest : noncopyable
59 {
60 public:
73  PendingInterest(shared_ptr<const Interest> interest,
74  const DataCallback& dataCallback,
75  const NackCallback& nackCallback,
76  const TimeoutCallback& timeoutCallback,
77  Scheduler& scheduler)
78  : m_interest(std::move(interest))
79  , m_origin(PendingInterestOrigin::APP)
80  , m_dataCallback(dataCallback)
81  , m_nackCallback(nackCallback)
82  , m_timeoutCallback(timeoutCallback)
83  , m_nNotNacked(0)
84  {
85  scheduleTimeoutEvent(scheduler);
86  }
87 
94  PendingInterest(shared_ptr<const Interest> interest, Scheduler& scheduler)
95  : m_interest(std::move(interest))
96  , m_origin(PendingInterestOrigin::FORWARDER)
97  , m_nNotNacked(0)
98  {
99  scheduleTimeoutEvent(scheduler);
100  }
101 
105  shared_ptr<const Interest>
106  getInterest() const
107  {
108  return m_interest;
109  }
110 
112  getOrigin() const
113  {
114  return m_origin;
115  }
116 
122  void
124  {
125  ++m_nNotNacked;
126  }
127 
133  optional<lp::Nack>
134  recordNack(const lp::Nack& nack)
135  {
136  --m_nNotNacked;
137  BOOST_ASSERT(m_nNotNacked >= 0);
138 
139  if (!m_leastSevereNack || lp::isLessSevere(nack.getReason(), m_leastSevereNack->getReason())) {
140  m_leastSevereNack = nack;
141  }
142 
143  return m_nNotNacked > 0 ? nullopt : m_leastSevereNack;
144  }
145 
150  void
152  {
153  if (m_dataCallback != nullptr) {
154  m_dataCallback(*m_interest, data);
155  }
156  }
157 
162  void
164  {
165  if (m_nackCallback != nullptr) {
166  m_nackCallback(*m_interest, nack);
167  }
168  }
169 
173  void
174  setDeleter(const std::function<void()>& deleter)
175  {
176  m_deleter = deleter;
177  }
178 
179 private:
180  void
181  scheduleTimeoutEvent(Scheduler& scheduler)
182  {
183  m_timeoutEvent = scheduler.scheduleEvent(m_interest->getInterestLifetime(),
184  [=] { this->invokeTimeoutCallback(); });
185  }
186 
190  void
191  invokeTimeoutCallback()
192  {
193  if (m_timeoutCallback) {
194  m_timeoutCallback(*m_interest);
195  }
196 
197  BOOST_ASSERT(m_deleter);
198  m_deleter();
199  }
200 
201 private:
202  shared_ptr<const Interest> m_interest;
203  PendingInterestOrigin m_origin;
204  DataCallback m_dataCallback;
205  NackCallback m_nackCallback;
206  TimeoutCallback m_timeoutCallback;
207  util::scheduler::ScopedEventId m_timeoutEvent;
208  int m_nNotNacked;
209  optional<lp::Nack> m_leastSevereNack;
210  std::function<void()> m_deleter;
211 };
212 
216 class PendingInterestId;
217 
222 {
223 public:
224  explicit
225  MatchPendingInterestId(const PendingInterestId* pendingInterestId)
226  : m_id(pendingInterestId)
227  {
228  }
229 
230  bool
231  operator()(const shared_ptr<const PendingInterest>& pendingInterest) const
232  {
233  return reinterpret_cast<const PendingInterestId*>(pendingInterest->getInterest().get()) == m_id;
234  }
235 
236 private:
237  const PendingInterestId* m_id;
238 };
239 
240 } // namespace ndn
241 
242 #endif // NDN_IMPL_PENDING_INTEREST_HPP
Copyright (c) 2011-2015 Regents of the University of California.
shared_ptr< const Interest > getInterest() const
Get the Interest.
std::ostream & operator<<(std::ostream &os, const Data &data)
Definition: data.cpp:322
void setDeleter(const std::function< void()> &deleter)
Set cleanup function to be invoked when Interest times out.
optional< lp::Nack > recordNack(const lp::Nack &nack)
Record an incoming Nack against a forwarded Interest.
Functor to match PendingInterestId.
EventId scheduleEvent(time::nanoseconds after, const EventCallback &callback)
Schedule a one-time event after the specified delay.
Definition: scheduler.cpp:103
represents a Network Nack
Definition: nack.hpp:38
NackReason getReason() const
Definition: nack.hpp:90
bool operator()(const shared_ptr< const PendingInterest > &pendingInterest) const
void invokeNackCallback(const lp::Nack &nack)
Invoke the Nack callback.
void recordForwarding()
Record that the Interest has been forwarded to one destination.
PendingInterest(shared_ptr< const Interest > interest, const DataCallback &dataCallback, const NackCallback &nackCallback, const TimeoutCallback &timeoutCallback, Scheduler &scheduler)
Construct a pending Interest record for an Interest from Face::expressInterest.
MatchPendingInterestId(const PendingInterestId *pendingInterestId)
PendingInterestOrigin
Indicates where a pending Interest came from.
Interest was received from the forwarder via Transport.
Interest was received from this app via Face::expressInterest API.
function< void(const Interest &)> TimeoutCallback
Callback invoked when expressed Interest times out.
Definition: face.hpp:63
function< void(const Interest &, const lp::Nack &)> NackCallback
Callback invoked when Nack is sent in response to expressed Interest.
Definition: face.hpp:58
PendingInterestOrigin getOrigin() const
Represents a Data packet.
Definition: data.hpp:35
bool isLessSevere(lp::NackReason x, lp::NackReason y)
compare NackReason for severity
Definition: nack-header.cpp:50
Stores a pending Interest and associated callbacks.
const nullopt_t nullopt((nullopt_t::init()))
PendingInterest(shared_ptr< const Interest > interest, Scheduler &scheduler)
Construct a pending Interest record for an Interest from NFD.
function< void(const Interest &, const Data &)> DataCallback
Callback invoked when expressed Interest gets satisfied with a Data packet.
Definition: face.hpp:53
void invokeDataCallback(const Data &data)
Invoke the Data callback.