NS-3 based Named Data Networking (NDN) simulator
ndnSIM: NDN, CCN, CCNx, content centric networks
API Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Groups Pages
timeouts-policy.h
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2 /*
3  * Copyright (c) 2013, Regents of the University of California
4  * Alexander Afanasyev
5  *
6  * GNU v3.0 license, See the LICENSE file for more information
7  *
8  * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
9  */
10 
11 #ifndef NDN_NDNCXX_DETAIL_TIMEOUTS_POLICY_H_
12 #define NDN_NDNCXX_DETAIL_TIMEOUTS_POLICY_H_
13 
14 #include <boost/intrusive/options.hpp>
15 #include <boost/intrusive/list.hpp>
16 
17 #include <ns3/nstime.h>
18 #include <ns3/simulator.h>
19 
20 namespace ns3 {
21 namespace ndn {
22 namespace detail {
23 
28 {
30  static std::string GetName () { return "Timeouts"; }
31 
32  struct policy_hook_type : public boost::intrusive::set_member_hook<> { Time timeWhenShouldExpire; };
33 
34  template<class Container>
36  {
37  typedef boost::intrusive::member_hook< Container,
39  &Container::policy_hook_ > type;
40  };
41 
42  template<class Base,
43  class Container,
44  class Hook>
45  struct policy
46  {
47  static Time& get_timeout (typename Container::iterator item)
48  {
49  return static_cast<typename policy_container::value_traits::hook_type*>
50  (policy_container::value_traits::to_node_ptr(*item))->timeWhenShouldExpire;
51  }
52 
53  static const Time& get_timeout (typename Container::const_iterator item)
54  {
55  return static_cast<const typename policy_container::value_traits::hook_type*>
56  (policy_container::value_traits::to_node_ptr(*item))->timeWhenShouldExpire;
57  }
58 
59  template<class Key>
61  {
62  bool operator () (const Key &a, const Key &b) const
63  {
64  return get_timeout (&a) < get_timeout (&b);
65  }
66  };
67 
68  typedef boost::intrusive::multiset< Container,
69  boost::intrusive::compare< MemberHookLess< Container > >,
70  Hook > policy_container;
71 
72 
73  class type : public policy_container
74  {
75  public:
76  typedef policy policy_base; // to get access to get_timeout methods from outside
77  typedef Container parent_trie;
78 
79  type (Base &base)
80  : m_base (base)
81  {
82  }
83 
84  inline void
85  update (typename parent_trie::iterator item)
86  {
87  // do nothing
88  }
89 
90  inline bool
91  insert (typename parent_trie::iterator item)
92  {
93  Time timeout = item->payload ()->GetInterest ()->GetInterestLifetime ();
94  if (timeout.IsZero ()) timeout = Seconds (4.0);
95 
96  get_timeout (item) = Simulator::Now () + timeout;
97  policy_container::insert (*item);
98 
99  if (policy_container::s_iterator_to (*item) == policy_container::begin ())
100  {
101  if (m_timeoutEvent.IsRunning ())
102  {
103  Simulator::Remove (m_timeoutEvent); // just canceling would not clean up list of events
104  }
105 
106  m_timeoutEvent = Simulator::Schedule (timeout, &type::ProcessTimeoutEntry, this, item);
107  }
108 
109  return true;
110  }
111 
112  inline void
113  lookup (typename parent_trie::iterator item)
114  {
115  // do nothing. it's random policy
116  }
117 
118  inline void
119  erase (typename parent_trie::iterator item)
120  {
121  bool needRescheduling = false;
122  if (policy_container::s_iterator_to (*item) == policy_container::begin ())
123  {
124  if (m_timeoutEvent.IsRunning ())
125  {
126  Simulator::Remove (m_timeoutEvent); // just canceling would not clean up list of events
127  }
128  needRescheduling = true;
129  }
130 
131  // erase only if freshness is non zero (otherwise an item is not in the policy
132  policy_container::erase (policy_container::s_iterator_to (*item));
133 
134  if (needRescheduling && !policy_container::empty ())
135  {
136  Time timeout = get_timeout (&*policy_container::begin ()) - Simulator::Now ();
137  m_timeoutEvent = Simulator::Schedule (timeout, &type::ProcessTimeoutEntry, this, &*policy_container::begin ());
138  }
139  }
140 
141  inline void
142  clear ()
143  {
144  policy_container::clear ();
145  }
146 
147  inline void
148  ProcessTimeoutEntry (typename parent_trie::iterator item)
149  {
150  item->payload ()->ProcessOnTimeout (item->payload ()->GetInterest ());
151 
152  m_base.erase (item);
153  }
154 
155  private:
156  type () : m_base (*((Base*)0)) { };
157 
158  private:
159  Base &m_base;
160  EventId m_timeoutEvent;
161  };
162  };
163 };
164 
165 } // detail
166 } // ndn
167 } // ns3
168 
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)