11 #ifndef NDN_NDNCXX_DETAIL_TIMEOUTS_POLICY_H_
12 #define NDN_NDNCXX_DETAIL_TIMEOUTS_POLICY_H_
14 #include <boost/intrusive/options.hpp>
15 #include <boost/intrusive/list.hpp>
17 #include <ns3/nstime.h>
18 #include <ns3/simulator.h>
30 static std::string
GetName () {
return "Timeouts"; }
32 struct policy_hook_type :
public boost::intrusive::set_member_hook<> { Time timeWhenShouldExpire; };
34 template<
class Container>
37 typedef boost::intrusive::member_hook< Container,
39 &Container::policy_hook_ > type;
47 static Time& get_timeout (
typename Container::iterator item)
49 return static_cast<typename policy_container::value_traits::hook_type*
>
50 (policy_container::value_traits::to_node_ptr(*item))->timeWhenShouldExpire;
53 static const Time& get_timeout (
typename Container::const_iterator item)
55 return static_cast<const typename policy_container::value_traits::hook_type*
>
56 (policy_container::value_traits::to_node_ptr(*item))->timeWhenShouldExpire;
62 bool operator () (
const Key &a,
const Key &b)
const
64 return get_timeout (&a) < get_timeout (&b);
68 typedef boost::intrusive::multiset< Container,
69 boost::intrusive::compare< MemberHookLess< Container > >,
70 Hook > policy_container;
73 class type :
public policy_container
77 typedef Container parent_trie;
85 update (
typename parent_trie::iterator item)
91 insert (
typename parent_trie::iterator item)
93 Time timeout = item->payload ()->GetInterest ()->GetInterestLifetime ();
94 if (timeout.IsZero ()) timeout = Seconds (4.0);
96 get_timeout (item) = Simulator::Now () + timeout;
97 policy_container::insert (*item);
99 if (policy_container::s_iterator_to (*item) == policy_container::begin ())
101 if (m_timeoutEvent.IsRunning ())
103 Simulator::Remove (m_timeoutEvent);
106 m_timeoutEvent = Simulator::Schedule (timeout, &type::ProcessTimeoutEntry,
this, item);
113 lookup (
typename parent_trie::iterator item)
119 erase (
typename parent_trie::iterator item)
121 bool needRescheduling =
false;
122 if (policy_container::s_iterator_to (*item) == policy_container::begin ())
124 if (m_timeoutEvent.IsRunning ())
126 Simulator::Remove (m_timeoutEvent);
128 needRescheduling =
true;
132 policy_container::erase (policy_container::s_iterator_to (*item));
134 if (needRescheduling && !policy_container::empty ())
136 Time timeout = get_timeout (&*policy_container::begin ()) - Simulator::Now ();
137 m_timeoutEvent = Simulator::Schedule (timeout, &type::ProcessTimeoutEntry,
this, &*policy_container::begin ());
144 policy_container::clear ();
148 ProcessTimeoutEntry (
typename parent_trie::iterator item)
150 item->payload ()->ProcessOnTimeout (item->payload ()->GetInterest ());
156 type () : m_base (*((Base*)0)) { };
160 EventId m_timeoutEvent;
169 #endif // NDN_NDNCXX_DETAIL_TIMEOUTS_STATS_POLICY_H
Traits for timeouts policy.
static std::string GetName()
Name that can be used to identify the policy (for NS-3 object model and logging)