blob: 1102ef1fe032cd336d1e590152b8b0cc6279e3c9 [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 */
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080038struct lifetime_stats_policy_traits {
Alexander Afanasyev8566f452012-12-10 15:21:51 -080039 /// @brief Name that can be used to identify the policy (for NS-3 object model and logging)
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080040 static std::string
41 GetName()
Alexander Afanasyev8566f452012-12-10 15:21:51 -080042 {
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080043 return "LifetimeStats";
44 }
45
46 struct policy_hook_type : public boost::intrusive::list_member_hook<> {
47 Time timeWhenAdded;
Alexander Afanasyev8566f452012-12-10 15:21:51 -080048 };
49
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080050 template<class Container>
51 struct container_hook {
52 typedef boost::intrusive::member_hook<Container, policy_hook_type, &Container::policy_hook_>
53 type;
54 };
55
56 template<class Base, class Container, class Hook>
57 struct policy {
58 typedef typename boost::intrusive::list<Container, Hook> policy_container;
59
60 static Time&
61 get_time(typename Container::iterator item)
Alexander Afanasyev8566f452012-12-10 15:21:51 -080062 {
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080063 return static_cast<typename policy_container::value_traits::hook_type*>(
64 policy_container::value_traits::to_node_ptr(*item))->timeWhenAdded;
Alexander Afanasyev8566f452012-12-10 15:21:51 -080065 }
66
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080067 static const Time&
68 get_time(typename Container::const_iterator item)
Alexander Afanasyev8566f452012-12-10 15:21:51 -080069 {
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080070 return static_cast<const typename policy_container::value_traits::hook_type*>(
71 policy_container::value_traits::to_node_ptr(*item))->timeWhenAdded;
72 }
73
74 class type : public policy_container {
Alexander Afanasyev8566f452012-12-10 15:21:51 -080075 public:
Alexander Afanasyev69786112012-12-13 11:53:36 -080076 typedef policy policy_base; // to get access to get_time methods from outside
Alexander Afanasyev8566f452012-12-10 15:21:51 -080077 typedef Container parent_trie;
78
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080079 type(Base& base)
80 : base_(base)
81 , max_size_(100)
82 , m_willRemoveEntry(0)
Alexander Afanasyev8566f452012-12-10 15:21:51 -080083 {
84 }
85
86 inline void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080087 update(typename parent_trie::iterator item)
Alexander Afanasyev8566f452012-12-10 15:21:51 -080088 {
89 // do nothing. it's random policy
90 }
Alexander Afanasyev8566f452012-12-10 15:21:51 -080091
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080092 inline bool
93 insert(typename parent_trie::iterator item)
94 {
95 get_time(item) = Simulator::Now();
96
97 policy_container::push_back(*item);
Alexander Afanasyev8566f452012-12-10 15:21:51 -080098 return true;
99 }
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800100
Alexander Afanasyev8566f452012-12-10 15:21:51 -0800101 inline void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800102 lookup(typename parent_trie::iterator item)
Alexander Afanasyev8566f452012-12-10 15:21:51 -0800103 {
104 // do nothing. it's random policy
105 }
Alexander Afanasyev8566f452012-12-10 15:21:51 -0800106
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800107 inline void
108 erase(typename parent_trie::iterator item)
109 {
110 Time lifetime = Simulator::Now() - get_time(item);
111
112 if (m_willRemoveEntry != 0) {
113 (*m_willRemoveEntry)(item->payload(), lifetime);
114 }
115
116 policy_container::erase(policy_container::s_iterator_to(*item));
Alexander Afanasyev8566f452012-12-10 15:21:51 -0800117 }
118
119 inline void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800120 clear()
Alexander Afanasyev8566f452012-12-10 15:21:51 -0800121 {
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800122 policy_container::clear();
Alexander Afanasyev8566f452012-12-10 15:21:51 -0800123 }
124
125 inline void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800126 set_max_size(size_t max_size)
Alexander Afanasyev8566f452012-12-10 15:21:51 -0800127 {
128 max_size_ = max_size;
129 }
130
131 inline size_t
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800132 get_max_size() const
Alexander Afanasyev8566f452012-12-10 15:21:51 -0800133 {
134 return max_size_;
135 }
136
137 void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800138 set_traced_callback(
139 TracedCallback<typename parent_trie::payload_traits::const_base_type, Time>* callback)
Alexander Afanasyev8566f452012-12-10 15:21:51 -0800140 {
Alexander Afanasyev8566f452012-12-10 15:21:51 -0800141 m_willRemoveEntry = callback;
142 }
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800143
Alexander Afanasyev8566f452012-12-10 15:21:51 -0800144 private:
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800145 type()
146 : base_(*((Base*)0)){};
147
Alexander Afanasyev8566f452012-12-10 15:21:51 -0800148 private:
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800149 Base& base_;
Alexander Afanasyev8566f452012-12-10 15:21:51 -0800150 size_t max_size_;
151
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800152 TracedCallback<typename parent_trie::payload_traits::const_base_type, Time>*
153 m_willRemoveEntry;
Alexander Afanasyev8566f452012-12-10 15:21:51 -0800154 };
155 };
156};
157
158} // ndnSIM
159} // ndn
160} // ns3
161
162#endif // LIFETIME_STATS_POLICY_H