NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.0: NDN, CCN, CCNx, content centric networks
API Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
lifetime-stats-policy.hpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
20 #ifndef LIFETIME_STATS_POLICY_H_
21 #define LIFETIME_STATS_POLICY_H_
22 
24 
25 #include "ns3/ndnSIM/model/ndn-common.hpp"
26 
27 #include <boost/intrusive/options.hpp>
28 #include <boost/intrusive/list.hpp>
29 
30 #include <ns3/nstime.h>
31 #include <ns3/simulator.h>
32 #include <ns3/traced-callback.h>
33 
34 namespace ns3 {
35 namespace ndn {
36 namespace ndnSIM {
37 
41 struct lifetime_stats_policy_traits {
43  static std::string
44  GetName()
45  {
46  return "LifetimeStats";
47  }
48 
49  struct policy_hook_type : public boost::intrusive::list_member_hook<> {
50  Time timeWhenAdded;
51  };
52 
53  template<class Container>
54  struct container_hook {
55  typedef boost::intrusive::member_hook<Container, policy_hook_type, &Container::policy_hook_>
56  type;
57  };
58 
59  template<class Base, class Container, class Hook>
60  struct policy {
61  typedef typename boost::intrusive::list<Container, Hook> policy_container;
62 
63  static Time&
64  get_time(typename Container::iterator item)
65  {
66  return static_cast<typename policy_container::value_traits::hook_type*>(
67  policy_container::value_traits::to_node_ptr(*item))->timeWhenAdded;
68  }
69 
70  static const Time&
71  get_time(typename Container::const_iterator item)
72  {
73  return static_cast<const typename policy_container::value_traits::hook_type*>(
74  policy_container::value_traits::to_node_ptr(*item))->timeWhenAdded;
75  }
76 
77  class type : public policy_container {
78  public:
79  typedef policy policy_base; // to get access to get_time methods from outside
80  typedef Container parent_trie;
81 
82  type(Base& base)
83  : base_(base)
84  , max_size_(100)
85  , m_willRemoveEntry(0)
86  {
87  }
88 
89  inline void
90  update(typename parent_trie::iterator item)
91  {
92  // do nothing. it's random policy
93  }
94 
95  inline bool
96  insert(typename parent_trie::iterator item)
97  {
98  get_time(item) = Simulator::Now();
99 
100  policy_container::push_back(*item);
101  return true;
102  }
103 
104  inline void
105  lookup(typename parent_trie::iterator item)
106  {
107  // do nothing. it's random policy
108  }
109 
110  inline void
111  erase(typename parent_trie::iterator item)
112  {
113  Time lifetime = Simulator::Now() - get_time(item);
114 
115  if (m_willRemoveEntry != 0) {
116  (*m_willRemoveEntry)(item->payload(), lifetime);
117  }
118 
119  policy_container::erase(policy_container::s_iterator_to(*item));
120  }
121 
122  inline void
123  clear()
124  {
125  policy_container::clear();
126  }
127 
128  inline void
129  set_max_size(size_t max_size)
130  {
131  max_size_ = max_size;
132  }
133 
134  inline size_t
135  get_max_size() const
136  {
137  return max_size_;
138  }
139 
140  void
141  set_traced_callback(
142  TracedCallback<typename parent_trie::payload_traits::const_base_type, Time>* callback)
143  {
144  m_willRemoveEntry = callback;
145  }
146 
147  private:
148  type()
149  : base_(*((Base*)0)){};
150 
151  private:
152  Base& base_;
153  size_t max_size_;
154 
155  TracedCallback<typename parent_trie::payload_traits::const_base_type, Time>*
156  m_willRemoveEntry;
157  };
158  };
159 };
160 
161 } // ndnSIM
162 } // ndn
163 } // ns3
164 
166 
167 #endif // LIFETIME_STATS_POLICY_H