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-2021 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_CXX_IMPL_PENDING_INTEREST_HPP
23 #define NDN_CXX_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 
38 {
39  APP,
40  FORWARDER
41 };
42 
43 std::ostream&
44 operator<<(std::ostream& os, PendingInterestOrigin origin)
45 {
46  switch (origin) {
48  return os << "app";
50  return os << "forwarder";
51  }
53 }
54 
58 class PendingInterest : public detail::RecordBase<PendingInterest>
59 {
60 public:
67  PendingInterest(shared_ptr<const Interest> interest, const DataCallback& dataCallback,
68  const NackCallback& nackCallback, const TimeoutCallback& timeoutCallback,
70  : m_interest(std::move(interest))
71  , m_origin(PendingInterestOrigin::APP)
72  , m_dataCallback(dataCallback)
73  , m_nackCallback(nackCallback)
74  , m_timeoutCallback(timeoutCallback)
75  {
76  scheduleTimeoutEvent(scheduler);
77  }
78 
82  PendingInterest(shared_ptr<const Interest> interest, Scheduler& scheduler)
83  : m_interest(std::move(interest))
84  , m_origin(PendingInterestOrigin::FORWARDER)
85  {
86  scheduleTimeoutEvent(scheduler);
87  }
88 
89  shared_ptr<const Interest>
90  getInterest() const
91  {
92  return m_interest;
93  }
94 
96  getOrigin() const
97  {
98  return m_origin;
99  }
100 
106  void
108  {
109  ++m_nNotNacked;
110  }
111 
117  optional<lp::Nack>
118  recordNack(const lp::Nack& nack)
119  {
120  --m_nNotNacked;
121  BOOST_ASSERT(m_nNotNacked >= 0);
122 
123  if (!m_leastSevereNack || lp::isLessSevere(nack.getReason(), m_leastSevereNack->getReason())) {
124  m_leastSevereNack = nack;
125  }
126 
127  return m_nNotNacked > 0 ? nullopt : m_leastSevereNack;
128  }
129 
134  void
136  {
137  if (m_dataCallback) {
138  m_dataCallback(*m_interest, data);
139  }
140  }
141 
146  void
148  {
149  if (m_nackCallback) {
150  m_nackCallback(*m_interest, nack);
151  }
152  }
153 
154 private:
155  void
156  scheduleTimeoutEvent(Scheduler& scheduler)
157  {
158  m_timeoutEvent = scheduler.schedule(m_interest->getInterestLifetime(),
159  [=] { this->invokeTimeoutCallback(); });
160  }
161 
165  void
166  invokeTimeoutCallback()
167  {
168  if (m_timeoutCallback) {
169  m_timeoutCallback(*m_interest);
170  }
171 
172  deleteSelf();
173  }
174 
175 private:
176  shared_ptr<const Interest> m_interest;
177  PendingInterestOrigin m_origin;
178  DataCallback m_dataCallback;
179  NackCallback m_nackCallback;
180  TimeoutCallback m_timeoutCallback;
181  scheduler::ScopedEventId m_timeoutEvent;
182  int m_nNotNacked = 0;
183  optional<lp::Nack> m_leastSevereNack;
184 };
185 
186 } // namespace ndn
187 
188 #endif // NDN_CXX_IMPL_PENDING_INTEREST_HPP
Copyright (c) 2011-2015 Regents of the University of California.
shared_ptr< const Interest > getInterest() const
std::ostream & operator<<(std::ostream &os, const Data &data)
Definition: data.cpp:376
optional< lp::Nack > recordNack(const lp::Nack &nack)
Record an incoming Nack against a forwarded Interest.
STL namespace.
EventId schedule(time::nanoseconds after, EventCallback callback)
Schedule a one-time event after the specified delay.
Definition: scheduler.cpp:96
represents a Network Nack
Definition: nack.hpp:38
NackReason getReason() const
Definition: nack.hpp:90
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.
Generic time-based scheduler.
Definition: scheduler.hpp:132
PendingInterestOrigin
Indicates where a pending Interest came from.
Interest was received from the forwarder via Transport.
#define NDN_CXX_UNREACHABLE
Definition: backports.hpp:138
Interest was received from this app via Face::expressInterest API.
function< void(const Interest &)> TimeoutCallback
Callback invoked when an expressed Interest times out.
Definition: face.hpp:60
function< void(const Interest &, const lp::Nack &)> NackCallback
Callback invoked when a Nack is received in response to an expressed Interest.
Definition: face.hpp:55
PendingInterestOrigin getOrigin() const
Represents a Data packet.
Definition: data.hpp:37
bool isLessSevere(lp::NackReason x, lp::NackReason y)
compare NackReason for severity
Definition: nack-header.cpp:45
Stores a pending Interest and associated callbacks.
Template of PendingInterest, RegisteredPrefix, and InterestFilterRecord.
const nullopt_t nullopt((nullopt_t::init()))
PendingInterest(shared_ptr< const Interest > interest, Scheduler &scheduler)
Construct a pending Interest record for an Interest from the forwarder.
function< void(const Interest &, const Data &)> DataCallback
Callback invoked when an expressed Interest is satisfied by a Data packet.
Definition: face.hpp:50
void invokeDataCallback(const Data &data)
Invoke the Data callback.