NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.0: 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 "../common.hpp"
26 #include "../interest.hpp"
27 #include "../data.hpp"
28 #include "../util/time.hpp"
29 #include "../util/scheduler.hpp"
30 #include "../util/scheduler-scoped-event-id.hpp"
31 
32 namespace ndn {
33 
34 class PendingInterest : noncopyable
35 {
36 public:
37  typedef function<void(const Interest&, Data&)> OnData;
38  typedef function<void(const Interest&)> OnTimeout;
39 
51  PendingInterest(shared_ptr<const Interest> interest, const OnData& onData,
52  const OnTimeout& onTimeout, Scheduler& scheduler)
53  : m_interest(interest)
54  , m_onData(onData)
55  , m_onTimeout(onTimeout)
56  , m_timeoutEvent(scheduler)
57  {
58  m_timeoutEvent =
59  scheduler.scheduleEvent(m_interest->getInterestLifetime() > time::milliseconds::zero() ?
60  m_interest->getInterestLifetime() :
62  bind(&PendingInterest::invokeTimeoutCallback, this));
63  }
64 
68  const Interest&
69  getInterest() const
70  {
71  return *m_interest;
72  }
73 
78  void
79  invokeDataCallback(const Data& data)
80  {
81  m_onData(*m_interest, const_cast<Data&>(data));
82  }
83 
87  void
88  setDeleter(const std::function<void()>& deleter)
89  {
90  m_deleter = deleter;
91  }
92 
93 private:
98  void
99  invokeTimeoutCallback()
100  {
101  if (m_onTimeout) {
102  m_onTimeout(*m_interest);
103  }
104 
105  BOOST_ASSERT(m_deleter);
106  m_deleter();
107  }
108 
109 private:
110  shared_ptr<const Interest> m_interest;
111  const OnData m_onData;
112  const OnTimeout m_onTimeout;
113  util::scheduler::ScopedEventId m_timeoutEvent;
114  std::function<void()> m_deleter;
115 };
116 
117 
118 class PendingInterestId;
119 
124 {
125 public:
126  explicit
127  MatchPendingInterestId(const PendingInterestId* pendingInterestId)
128  : m_id(pendingInterestId)
129  {
130  }
131 
132  bool
133  operator()(const shared_ptr<const PendingInterest>& pendingInterest) const
134  {
135  return (reinterpret_cast<const PendingInterestId*>(
136  &pendingInterest->getInterest()) == m_id);
137  }
138 private:
139  const PendingInterestId* m_id;
140 };
141 
142 
143 } // namespace ndn
144 
145 #endif // NDN_DETAIL_PENDING_INTEREST_HPP
Copyright (c) 2011-2015 Regents of the University of California.
void setDeleter(const std::function< void()> &deleter)
Set cleanup function to be called after interest times out.
represents an Interest packet
Definition: interest.hpp:45
Functor to match pending interests against PendingInterestId.
function< void(const Interest &)> OnTimeout
PendingInterest(shared_ptr< const Interest > interest, const OnData &onData, const OnTimeout &onTimeout, Scheduler &scheduler)
Create a new PitEntry and set the timeout based on the current time and the interest lifetime...
MatchPendingInterestId(const PendingInterestId *pendingInterestId)
Event that is automatically cancelled upon destruction.
bool operator()(const shared_ptr< const PendingInterest > &pendingInterest) const
function< void(const Interest &, Data &)> OnData
const time::milliseconds DEFAULT_INTEREST_LIFETIME
default value for InterestLifetime
Definition: interest.hpp:41
represents a Data packet
Definition: data.hpp:39
const Interest & getInterest() const
void invokeDataCallback(const Data &data)
invokes the DataCallback