NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.3: 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-2017 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_DETAIL_PENDING_INTEREST_HPP
23 #define NDN_DETAIL_PENDING_INTEREST_HPP
24 
25 #include "../data.hpp"
26 #include "../interest.hpp"
27 #include "../lp/nack.hpp"
28 #include "../util/scheduler-scoped-event-id.hpp"
29 
30 namespace ndn {
31 
36 {
37  APP,
38  FORWARDER
39 };
40 
41 std::ostream&
42 operator<<(std::ostream& os, PendingInterestOrigin origin)
43 {
44  switch (origin) {
46  return os << "app";
48  return os << "forwarder";
49  }
50  BOOST_ASSERT(false);
51  return os;
52 }
53 
57 class PendingInterest : noncopyable
58 {
59 public:
72  PendingInterest(shared_ptr<const Interest> interest,
73  const DataCallback& dataCallback,
74  const NackCallback& nackCallback,
75  const TimeoutCallback& timeoutCallback,
76  Scheduler& scheduler)
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_timeoutEvent(scheduler)
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_timeoutEvent(scheduler)
98  , m_nNotNacked(0)
99  {
100  scheduleTimeoutEvent(scheduler);
101  }
102 
106  shared_ptr<const Interest>
107  getInterest() const
108  {
109  return m_interest;
110  }
111 
113  getOrigin() const
114  {
115  return m_origin;
116  }
117 
123  void
125  {
126  ++m_nNotNacked;
127  }
128 
135  recordNack(const lp::Nack& nack)
136  {
137  --m_nNotNacked;
138  BOOST_ASSERT(m_nNotNacked >= 0);
139 
140  if (!m_leastSevereNack || lp::isLessSevere(nack.getReason(), m_leastSevereNack->getReason())) {
141  m_leastSevereNack = nack;
142  }
143 
144  return m_nNotNacked > 0 ? nullopt : m_leastSevereNack;
145  }
146 
151  void
153  {
154  if (m_dataCallback != nullptr) {
155  m_dataCallback(*m_interest, data);
156  }
157  }
158 
163  void
165  {
166  if (m_nackCallback != nullptr) {
167  m_nackCallback(*m_interest, nack);
168  }
169  }
170 
174  void
175  setDeleter(const std::function<void()>& deleter)
176  {
177  m_deleter = deleter;
178  }
179 
180 private:
181  void
182  scheduleTimeoutEvent(Scheduler& scheduler)
183  {
184  m_timeoutEvent = scheduler.scheduleEvent(m_interest->getInterestLifetime(),
185  [=] { this->invokeTimeoutCallback(); });
186  }
187 
191  void
192  invokeTimeoutCallback()
193  {
194  if (m_timeoutCallback) {
195  m_timeoutCallback(*m_interest);
196  }
197 
198  BOOST_ASSERT(m_deleter);
199  m_deleter();
200  }
201 
202 private:
203  shared_ptr<const Interest> m_interest;
204  PendingInterestOrigin m_origin;
205  DataCallback m_dataCallback;
206  NackCallback m_nackCallback;
207  TimeoutCallback m_timeoutCallback;
208  util::scheduler::ScopedEventId m_timeoutEvent;
209  int m_nNotNacked;
210  optional<lp::Nack> m_leastSevereNack;
211  std::function<void()> m_deleter;
212 };
213 
217 class PendingInterestId;
218 
223 {
224 public:
225  explicit
226  MatchPendingInterestId(const PendingInterestId* pendingInterestId)
227  : m_id(pendingInterestId)
228  {
229  }
230 
231  bool
232  operator()(const shared_ptr<const PendingInterest>& pendingInterest) const
233  {
234  return reinterpret_cast<const PendingInterestId*>(pendingInterest->getInterest().get()) == m_id;
235  }
236 
237 private:
238  const PendingInterestId* m_id;
239 };
240 
241 } // namespace ndn
242 
243 #endif // NDN_DETAIL_PENDING_INTEREST_HPP
constexpr nullopt_t nullopt
Copyright (c) 2011-2015 Regents of the University of California.
std::ostream & operator<<(std::ostream &os, const Data &data)
Definition: data.cpp:274
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.
STL namespace.
Functor to match PendingInterestId.
shared_ptr< const Interest > getInterest() const
Get the Interest.
represents a Network Nack
Definition: nack.hpp:40
NackReason getReason() const
Definition: nack.hpp:92
PendingInterestOrigin getOrigin() 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)
Event that is automatically cancelled upon destruction.
PendingInterestOrigin
Indicates where a pending Interest came from.
bool operator()(const shared_ptr< const PendingInterest > &pendingInterest) const
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:65
function< void(const Interest &, const lp::Nack &)> NackCallback
Callback invoked when Nack is sent in response to expressed Interest.
Definition: face.hpp:60
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.
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:55
void invokeDataCallback(const Data &data)
Invoke the Data callback.