blob: 1abf39d3959a32b8986f2fdd38a33a4455644243 [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
Spyridon Mastorakis53e922f2014-10-17 17:29:26 -070024#include "ns3/ndnSIM/model/ndn-common.hpp"
25
Alexander Afanasyev8566f452012-12-10 15:21:51 -080026#include <boost/intrusive/options.hpp>
27#include <boost/intrusive/list.hpp>
28
29#include <ns3/nstime.h>
30#include <ns3/simulator.h>
31#include <ns3/traced-callback.h>
32
33namespace ns3 {
34namespace ndn {
35namespace ndnSIM {
36
37/**
38 * @brief Traits for lifetime stats policy
39 */
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080040struct lifetime_stats_policy_traits {
Alexander Afanasyev8566f452012-12-10 15:21:51 -080041 /// @brief Name that can be used to identify the policy (for NS-3 object model and logging)
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080042 static std::string
43 GetName()
Alexander Afanasyev8566f452012-12-10 15:21:51 -080044 {
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080045 return "LifetimeStats";
46 }
47
48 struct policy_hook_type : public boost::intrusive::list_member_hook<> {
49 Time timeWhenAdded;
Alexander Afanasyev8566f452012-12-10 15:21:51 -080050 };
51
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080052 template<class Container>
53 struct container_hook {
54 typedef boost::intrusive::member_hook<Container, policy_hook_type, &Container::policy_hook_>
55 type;
56 };
57
58 template<class Base, class Container, class Hook>
59 struct policy {
60 typedef typename boost::intrusive::list<Container, Hook> policy_container;
61
62 static Time&
63 get_time(typename Container::iterator item)
Alexander Afanasyev8566f452012-12-10 15:21:51 -080064 {
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080065 return static_cast<typename policy_container::value_traits::hook_type*>(
66 policy_container::value_traits::to_node_ptr(*item))->timeWhenAdded;
Alexander Afanasyev8566f452012-12-10 15:21:51 -080067 }
68
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080069 static const Time&
70 get_time(typename Container::const_iterator item)
Alexander Afanasyev8566f452012-12-10 15:21:51 -080071 {
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080072 return static_cast<const typename policy_container::value_traits::hook_type*>(
73 policy_container::value_traits::to_node_ptr(*item))->timeWhenAdded;
74 }
75
76 class type : public policy_container {
Alexander Afanasyev8566f452012-12-10 15:21:51 -080077 public:
Alexander Afanasyev69786112012-12-13 11:53:36 -080078 typedef policy policy_base; // to get access to get_time methods from outside
Alexander Afanasyev8566f452012-12-10 15:21:51 -080079 typedef Container parent_trie;
80
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080081 type(Base& base)
82 : base_(base)
83 , max_size_(100)
84 , m_willRemoveEntry(0)
Alexander Afanasyev8566f452012-12-10 15:21:51 -080085 {
86 }
87
88 inline void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080089 update(typename parent_trie::iterator item)
Alexander Afanasyev8566f452012-12-10 15:21:51 -080090 {
91 // do nothing. it's random policy
92 }
Alexander Afanasyev8566f452012-12-10 15:21:51 -080093
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080094 inline bool
95 insert(typename parent_trie::iterator item)
96 {
97 get_time(item) = Simulator::Now();
98
99 policy_container::push_back(*item);
Alexander Afanasyev8566f452012-12-10 15:21:51 -0800100 return true;
101 }
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800102
Alexander Afanasyev8566f452012-12-10 15:21:51 -0800103 inline void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800104 lookup(typename parent_trie::iterator item)
Alexander Afanasyev8566f452012-12-10 15:21:51 -0800105 {
106 // do nothing. it's random policy
107 }
Alexander Afanasyev8566f452012-12-10 15:21:51 -0800108
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800109 inline void
110 erase(typename parent_trie::iterator item)
111 {
112 Time lifetime = Simulator::Now() - get_time(item);
113
114 if (m_willRemoveEntry != 0) {
115 (*m_willRemoveEntry)(item->payload(), lifetime);
116 }
117
118 policy_container::erase(policy_container::s_iterator_to(*item));
Alexander Afanasyev8566f452012-12-10 15:21:51 -0800119 }
120
121 inline void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800122 clear()
Alexander Afanasyev8566f452012-12-10 15:21:51 -0800123 {
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800124 policy_container::clear();
Alexander Afanasyev8566f452012-12-10 15:21:51 -0800125 }
126
127 inline void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800128 set_max_size(size_t max_size)
Alexander Afanasyev8566f452012-12-10 15:21:51 -0800129 {
130 max_size_ = max_size;
131 }
132
133 inline size_t
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800134 get_max_size() const
Alexander Afanasyev8566f452012-12-10 15:21:51 -0800135 {
136 return max_size_;
137 }
138
139 void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800140 set_traced_callback(
141 TracedCallback<typename parent_trie::payload_traits::const_base_type, Time>* callback)
Alexander Afanasyev8566f452012-12-10 15:21:51 -0800142 {
Alexander Afanasyev8566f452012-12-10 15:21:51 -0800143 m_willRemoveEntry = callback;
144 }
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800145
Alexander Afanasyev8566f452012-12-10 15:21:51 -0800146 private:
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800147 type()
148 : base_(*((Base*)0)){};
149
Alexander Afanasyev8566f452012-12-10 15:21:51 -0800150 private:
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800151 Base& base_;
Alexander Afanasyev8566f452012-12-10 15:21:51 -0800152 size_t max_size_;
153
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800154 TracedCallback<typename parent_trie::payload_traits::const_base_type, Time>*
155 m_willRemoveEntry;
Alexander Afanasyev8566f452012-12-10 15:21:51 -0800156 };
157 };
158};
159
160} // ndnSIM
161} // ndn
162} // ns3
163
164#endif // LIFETIME_STATS_POLICY_H