blob: 905f532443e5bb176ff9e0934284d93c2c15fcbf [file] [log] [blame]
Alexander Afanasyev8566f452012-12-10 15:21:51 -08001/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
2/*
3 * Copyright (c) 2012 University of California, Los Angeles
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation;
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 *
18 * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
19 */
20
21#ifndef LIFETIME_STATS_POLICY_H_
22#define LIFETIME_STATS_POLICY_H_
23
24#include <boost/intrusive/options.hpp>
25#include <boost/intrusive/list.hpp>
26
27#include <ns3/nstime.h>
28#include <ns3/simulator.h>
29#include <ns3/traced-callback.h>
30
31namespace ns3 {
32namespace ndn {
33namespace ndnSIM {
34
35/**
36 * @brief Traits for lifetime stats policy
37 */
38struct lifetime_stats_policy_traits
39{
40 /// @brief Name that can be used to identify the policy (for NS-3 object model and logging)
41 static std::string GetName () { return "LifetimeStats"; }
42
43 struct policy_hook_type : public boost::intrusive::list_member_hook<> { Time timeWhenAdded; };
44
45 template<class Container>
46 struct container_hook
47 {
48 typedef boost::intrusive::member_hook< Container,
49 policy_hook_type,
50 &Container::policy_hook_ > type;
51 };
52
53 template<class Base,
54 class Container,
55 class Hook>
56 struct policy
57 {
58 typedef typename boost::intrusive::list< Container, Hook > policy_container;
59
60 static Time& get_time (typename Container::iterator item)
61 {
62 return static_cast<typename policy_container::value_traits::hook_type*>
63 (policy_container::value_traits::to_node_ptr(*item))->timeWhenAdded;
64 }
65
66 static const Time& get_time (typename Container::const_iterator item)
67 {
68 return static_cast<const typename policy_container::value_traits::hook_type*>
69 (policy_container::value_traits::to_node_ptr(*item))->timeWhenAdded;
70 }
71
72 class type : public policy_container
73 {
74 public:
Alexander Afanasyev69786112012-12-13 11:53:36 -080075 typedef policy policy_base; // to get access to get_time methods from outside
Alexander Afanasyev8566f452012-12-10 15:21:51 -080076 typedef Container parent_trie;
77
78 type (Base &base)
79 : base_ (base)
80 , max_size_ (100)
81 , m_willRemoveEntry (0)
82 {
83 }
84
85 inline void
86 update (typename parent_trie::iterator item)
87 {
88 // do nothing. it's random policy
89 }
90
91 inline bool
92 insert (typename parent_trie::iterator item)
93 {
94 get_time (item) = Simulator::Now ();
95
96 policy_container::push_back (*item);
97 return true;
98 }
99
100 inline void
101 lookup (typename parent_trie::iterator item)
102 {
103 // do nothing. it's random policy
104 }
105
106 inline void
107 erase (typename parent_trie::iterator item)
108 {
109 Time lifetime = Simulator::Now () - get_time (item);
110
111 if (m_willRemoveEntry != 0)
112 {
113 (*m_willRemoveEntry) (item->payload (), lifetime);
114 }
115
116 policy_container::erase (policy_container::s_iterator_to (*item));
117 }
118
119 inline void
120 clear ()
121 {
122 policy_container::clear ();
123 }
124
125 inline void
126 set_max_size (size_t max_size)
127 {
128 max_size_ = max_size;
129 }
130
131 inline size_t
132 get_max_size () const
133 {
134 return max_size_;
135 }
136
137 void
138 set_traced_callback (TracedCallback< typename parent_trie::payload_traits::const_base_type, Time > *callback)
139 {
Alexander Afanasyev8566f452012-12-10 15:21:51 -0800140 m_willRemoveEntry = callback;
141 }
142
143 private:
144 type () : base_(*((Base*)0)) { };
145
146 private:
147 Base &base_;
148 size_t max_size_;
149
150 TracedCallback< typename parent_trie::payload_traits::const_base_type, Time > *m_willRemoveEntry;
151 };
152 };
153};
154
155} // ndnSIM
156} // ndn
157} // ns3
158
159#endif // LIFETIME_STATS_POLICY_H