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"
29 #include "ndn-cxx/lp/nack.hpp"
31 
32 namespace ndn {
33 
37 class PendingInterestId;
38 
39 static_assert(sizeof(const PendingInterestId*) == sizeof(RecordId), "");
40 
45 {
46  APP,
47  FORWARDER
48 };
49 
50 std::ostream&
51 operator<<(std::ostream& os, PendingInterestOrigin origin)
52 {
53  switch (origin) {
55  return os << "app";
57  return os << "forwarder";
58  }
60 }
61 
65 class PendingInterest : public RecordBase<PendingInterest>
66 {
67 public:
74  PendingInterest(shared_ptr<const Interest> interest, const DataCallback& dataCallback,
75  const NackCallback& nackCallback, const TimeoutCallback& timeoutCallback,
77  : m_interest(std::move(interest))
78  , m_origin(PendingInterestOrigin::APP)
79  , m_dataCallback(dataCallback)
80  , m_nackCallback(nackCallback)
81  , m_timeoutCallback(timeoutCallback)
82  , m_nNotNacked(0)
83  {
84  scheduleTimeoutEvent(scheduler);
85  }
86 
90  PendingInterest(shared_ptr<const Interest> interest, Scheduler& scheduler)
91  : m_interest(std::move(interest))
92  , m_origin(PendingInterestOrigin::FORWARDER)
93  , m_nNotNacked(0)
94  {
95  scheduleTimeoutEvent(scheduler);
96  }
97 
98  shared_ptr<const Interest>
99  getInterest() const
100  {
101  return m_interest;
102  }
103 
105  getOrigin() const
106  {
107  return m_origin;
108  }
109 
115  void
117  {
118  ++m_nNotNacked;
119  }
120 
126  optional<lp::Nack>
127  recordNack(const lp::Nack& nack)
128  {
129  --m_nNotNacked;
130  BOOST_ASSERT(m_nNotNacked >= 0);
131 
132  if (!m_leastSevereNack || lp::isLessSevere(nack.getReason(), m_leastSevereNack->getReason())) {
133  m_leastSevereNack = nack;
134  }
135 
136  return m_nNotNacked > 0 ? nullopt : m_leastSevereNack;
137  }
138 
143  void
145  {
146  if (m_dataCallback != nullptr) {
147  m_dataCallback(*m_interest, data);
148  }
149  }
150 
155  void
157  {
158  if (m_nackCallback != nullptr) {
159  m_nackCallback(*m_interest, nack);
160  }
161  }
162 
163 private:
164  void
165  scheduleTimeoutEvent(Scheduler& scheduler)
166  {
167  m_timeoutEvent = scheduler.schedule(m_interest->getInterestLifetime(),
168  [=] { this->invokeTimeoutCallback(); });
169  }
170 
174  void
175  invokeTimeoutCallback()
176  {
177  if (m_timeoutCallback) {
178  m_timeoutCallback(*m_interest);
179  }
180 
181  deleteSelf();
182  }
183 
184 private:
185  shared_ptr<const Interest> m_interest;
186  PendingInterestOrigin m_origin;
187  DataCallback m_dataCallback;
188  NackCallback m_nackCallback;
189  TimeoutCallback m_timeoutCallback;
190  scheduler::ScopedEventId m_timeoutEvent;
191  int m_nNotNacked;
192  optional<lp::Nack> m_leastSevereNack;
193  std::function<void()> m_deleter;
194 };
195 
196 } // namespace ndn
197 
198 #endif // NDN_IMPL_PENDING_INTEREST_HPP
ndn::PendingInterest::PendingInterest
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.
Definition: pending-interest.hpp:74
ndn::PendingInterestOrigin::FORWARDER
@ FORWARDER
Interest was received from the forwarder via Transport.
nonstd::optional_lite::std11::move
T & move(T &t)
Definition: optional.hpp:421
ndn::PendingInterestOrigin::APP
@ APP
Interest was received from this app via Face::expressInterest API.
ndn::PendingInterest::recordForwarding
void recordForwarding()
Record that the Interest has been forwarded to one destination.
Definition: pending-interest.hpp:116
ndn::PendingInterest::invokeDataCallback
void invokeDataCallback(const Data &data)
Invoke the Data callback.
Definition: pending-interest.hpp:144
ndn::PendingInterest::invokeNackCallback
void invokeNackCallback(const lp::Nack &nack)
Invoke the Nack callback.
Definition: pending-interest.hpp:156
ndn::RecordId
uintptr_t RecordId
Definition: record-container.hpp:32
NDN_CXX_UNREACHABLE
#define NDN_CXX_UNREACHABLE
Definition: backports.hpp:72
ndn::PendingInterest::getOrigin
PendingInterestOrigin getOrigin() const
Definition: pending-interest.hpp:105
scheduler.hpp
ndn::PendingInterestOrigin
PendingInterestOrigin
Indicates where a pending Interest came from.
Definition: pending-interest.hpp:45
ndn::PendingInterest
Stores a pending Interest and associated callbacks.
Definition: pending-interest.hpp:66
ndn::lp::Nack::getReason
NackReason getReason() const
Definition: nack.hpp:90
ndn::lp::isLessSevere
bool isLessSevere(lp::NackReason x, lp::NackReason y)
compare NackReason for severity
Definition: nack-header.cpp:45
ndn::PendingInterest::recordNack
optional< lp::Nack > recordNack(const lp::Nack &nack)
Record an incoming Nack against a forwarded Interest.
Definition: pending-interest.hpp:127
ndn::DataCallback
function< void(const Interest &, const Data &)> DataCallback
Callback invoked when expressed Interest gets satisfied with a Data packet.
Definition: face.hpp:44
ndn::PendingInterest::PendingInterest
PendingInterest(shared_ptr< const Interest > interest, Scheduler &scheduler)
Construct a pending Interest record for an Interest from NFD.
Definition: pending-interest.hpp:90
nonstd::optional_lite::nullopt
const nullopt_t nullopt((nullopt_t::init()))
nack.hpp
ndn::Data
Represents a Data packet.
Definition: data.hpp:36
ndn::NackCallback
function< void(const Interest &, const lp::Nack &)> NackCallback
Callback invoked when Nack is sent in response to expressed Interest.
Definition: face.hpp:54
interest.hpp
record-container.hpp
data.hpp
ndn::scheduler
Definition: scheduler.cpp:27
face.hpp
ndn::lp::Nack
represents a Network Nack
Definition: nack.hpp:39
ndn::scheduler::Scheduler
Generic time-based scheduler.
Definition: scheduler.hpp:133
ndn::PendingInterest::getInterest
shared_ptr< const Interest > getInterest() const
Definition: pending-interest.hpp:99
ndn::operator<<
std::ostream & operator<<(std::ostream &os, const Data &data)
Definition: data.cpp:322
ndn::TimeoutCallback
function< void(const Interest &)> TimeoutCallback
Callback invoked when expressed Interest times out.
Definition: face.hpp:59
ndn::RecordBase
Template of PendingInterest, RegisteredPrefix, and InterestFilterRecord.
Definition: record-container.hpp:42
ndn::RecordBase< PendingInterest >::deleteSelf
void deleteSelf()
Delete this record from the container.
Definition: record-container.hpp:57
ndn
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-strategy-choice-helper.hpp:34
ndn::detail::ScopedCancelHandle< EventId >