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; -*- */
22 #ifndef NDN_DETAIL_PENDING_INTEREST_HPP
23 #define NDN_DETAIL_PENDING_INTEREST_HPP
24 
25 #include "../interest.hpp"
26 #include "../data.hpp"
27 #include "../lp/nack.hpp"
28 #include "../util/scheduler-scoped-event-id.hpp"
29 
30 namespace ndn {
31 
35 class PendingInterest : noncopyable
36 {
37 public:
50  PendingInterest(shared_ptr<const Interest> interest,
51  const DataCallback& dataCallback,
52  const NackCallback& nackCallback,
53  const TimeoutCallback& timeoutCallback,
54  Scheduler& scheduler)
55  : m_interest(interest)
56  , m_dataCallback(dataCallback)
57  , m_nackCallback(nackCallback)
58  , m_timeoutCallback(timeoutCallback)
59  , m_timeoutEvent(scheduler)
60  {
61  m_timeoutEvent =
62  scheduler.scheduleEvent(m_interest->getInterestLifetime() > time::milliseconds::zero() ?
63  m_interest->getInterestLifetime() :
65  [=] { this->invokeTimeoutCallback(); });
66  }
67 
71  shared_ptr<const Interest>
72  getInterest() const
73  {
74  return m_interest;
75  }
76 
81  void
82  invokeDataCallback(const Data& data)
83  {
84  if (m_dataCallback != nullptr) {
85  m_dataCallback(*m_interest, data);
86  }
87  }
88 
93  void
95  {
96  if (m_nackCallback != nullptr) {
97  m_nackCallback(*m_interest, nack);
98  }
99  }
100 
104  void
105  setDeleter(const std::function<void()>& deleter)
106  {
107  m_deleter = deleter;
108  }
109 
110 private:
114  void
115  invokeTimeoutCallback()
116  {
117  if (m_timeoutCallback) {
118  m_timeoutCallback(*m_interest);
119  }
120 
121  BOOST_ASSERT(m_deleter);
122  m_deleter();
123  }
124 
125 private:
126  shared_ptr<const Interest> m_interest;
127  DataCallback m_dataCallback;
128  NackCallback m_nackCallback;
129  TimeoutCallback m_timeoutCallback;
130  util::scheduler::ScopedEventId m_timeoutEvent;
131  std::function<void()> m_deleter;
132 };
133 
137 class PendingInterestId;
138 
143 {
144 public:
145  explicit
146  MatchPendingInterestId(const PendingInterestId* pendingInterestId)
147  : m_id(pendingInterestId)
148  {
149  }
150 
151  bool
152  operator()(const shared_ptr<const PendingInterest>& pendingInterest) const
153  {
154  return reinterpret_cast<const PendingInterestId*>(pendingInterest->getInterest().get()) == m_id;
155  }
156 
157 private:
158  const PendingInterestId* m_id;
159 };
160 
161 } // namespace ndn
162 
163 #endif // NDN_DETAIL_PENDING_INTEREST_HPP
Copyright (c) 2011-2015 Regents of the University of California.
shared_ptr< const Interest > getInterest() const
void setDeleter(const std::function< void()> &deleter)
Set cleanup function to be invoked when Interest times out.
EventId scheduleEvent(const time::nanoseconds &after, const Event &event)
Schedule a one-time event after the specified delay.
Definition: scheduler.cpp:58
Functor to match PendingInterestId.
represents a Network Nack
Definition: nack.hpp:40
bool operator()(const shared_ptr< const PendingInterest > &pendingInterest) const
void invokeNackCallback(const lp::Nack &nack)
invokes the Nack callback
PendingInterest(shared_ptr< const Interest > interest, const DataCallback &dataCallback, const NackCallback &nackCallback, const TimeoutCallback &timeoutCallback, Scheduler &scheduler)
Construct a pending Interest record.
MatchPendingInterestId(const PendingInterestId *pendingInterestId)
Event that is automatically cancelled upon destruction.
const time::milliseconds DEFAULT_INTEREST_LIFETIME
default value for InterestLifetime
Definition: interest.hpp:38
function< void(const Interest &)> TimeoutCallback
Callback invoked when expressed Interest times out.
Definition: face.hpp:77
function< void(const Interest &, const lp::Nack &)> NackCallback
Callback invoked when Nack is sent in response to expressed Interest.
Definition: face.hpp:72
represents a Data packet
Definition: data.hpp:37
stores a pending Interest and associated callbacks
function< void(const Interest &, const Data &)> DataCallback
Callback invoked when expressed Interest gets satisfied with a Data packet.
Definition: face.hpp:67
void invokeDataCallback(const Data &data)
invokes the Data callback