blob: 6e81618214427492e37bb66d322e96c9565d1291 [file] [log] [blame]
Alexander Afanasyev60a7b622014-12-20 17:04:07 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2011-2015 Regents of the University of California.
Alexander Afanasyev8566f452012-12-10 15:21:51 -08004 *
Alexander Afanasyev60a7b622014-12-20 17:04:07 -08005 * This file is part of ndnSIM. See AUTHORS for complete list of ndnSIM authors and
6 * contributors.
Alexander Afanasyev8566f452012-12-10 15:21:51 -08007 *
Alexander Afanasyev60a7b622014-12-20 17:04:07 -08008 * ndnSIM is free software: you can redistribute it and/or modify it under the terms
9 * of the GNU General Public License as published by the Free Software Foundation,
10 * either version 3 of the License, or (at your option) any later version.
Alexander Afanasyev8566f452012-12-10 15:21:51 -080011 *
Alexander Afanasyev60a7b622014-12-20 17:04:07 -080012 * ndnSIM is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
13 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
Alexander Afanasyev8566f452012-12-10 15:21:51 -080015 *
Alexander Afanasyev60a7b622014-12-20 17:04:07 -080016 * You should have received a copy of the GNU General Public License along with
17 * ndnSIM, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
18 **/
Alexander Afanasyev8566f452012-12-10 15:21:51 -080019
20#ifndef LIFETIME_STATS_POLICY_H_
21#define LIFETIME_STATS_POLICY_H_
22
Spyridon Mastorakis460f57c2014-12-17 00:44:14 -080023/// @cond include_hidden
24
Spyridon Mastorakis53e922f2014-10-17 17:29:26 -070025#include "ns3/ndnSIM/model/ndn-common.hpp"
26
Alexander Afanasyev8566f452012-12-10 15:21:51 -080027#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
34namespace ns3 {
35namespace ndn {
36namespace ndnSIM {
37
38/**
39 * @brief Traits for lifetime stats policy
40 */
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080041struct lifetime_stats_policy_traits {
Alexander Afanasyev8566f452012-12-10 15:21:51 -080042 /// @brief Name that can be used to identify the policy (for NS-3 object model and logging)
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080043 static std::string
44 GetName()
Alexander Afanasyev8566f452012-12-10 15:21:51 -080045 {
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080046 return "LifetimeStats";
47 }
48
49 struct policy_hook_type : public boost::intrusive::list_member_hook<> {
50 Time timeWhenAdded;
Alexander Afanasyev8566f452012-12-10 15:21:51 -080051 };
52
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080053 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)
Alexander Afanasyev8566f452012-12-10 15:21:51 -080065 {
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080066 return static_cast<typename policy_container::value_traits::hook_type*>(
67 policy_container::value_traits::to_node_ptr(*item))->timeWhenAdded;
Alexander Afanasyev8566f452012-12-10 15:21:51 -080068 }
69
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080070 static const Time&
71 get_time(typename Container::const_iterator item)
Alexander Afanasyev8566f452012-12-10 15:21:51 -080072 {
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080073 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 {
Alexander Afanasyev8566f452012-12-10 15:21:51 -080078 public:
Alexander Afanasyev69786112012-12-13 11:53:36 -080079 typedef policy policy_base; // to get access to get_time methods from outside
Alexander Afanasyev8566f452012-12-10 15:21:51 -080080 typedef Container parent_trie;
81
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080082 type(Base& base)
83 : base_(base)
84 , max_size_(100)
85 , m_willRemoveEntry(0)
Alexander Afanasyev8566f452012-12-10 15:21:51 -080086 {
87 }
88
89 inline void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080090 update(typename parent_trie::iterator item)
Alexander Afanasyev8566f452012-12-10 15:21:51 -080091 {
92 // do nothing. it's random policy
93 }
Alexander Afanasyev8566f452012-12-10 15:21:51 -080094
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080095 inline bool
96 insert(typename parent_trie::iterator item)
97 {
98 get_time(item) = Simulator::Now();
99
100 policy_container::push_back(*item);
Alexander Afanasyev8566f452012-12-10 15:21:51 -0800101 return true;
102 }
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800103
Alexander Afanasyev8566f452012-12-10 15:21:51 -0800104 inline void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800105 lookup(typename parent_trie::iterator item)
Alexander Afanasyev8566f452012-12-10 15:21:51 -0800106 {
107 // do nothing. it's random policy
108 }
Alexander Afanasyev8566f452012-12-10 15:21:51 -0800109
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800110 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));
Alexander Afanasyev8566f452012-12-10 15:21:51 -0800120 }
121
122 inline void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800123 clear()
Alexander Afanasyev8566f452012-12-10 15:21:51 -0800124 {
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800125 policy_container::clear();
Alexander Afanasyev8566f452012-12-10 15:21:51 -0800126 }
127
128 inline void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800129 set_max_size(size_t max_size)
Alexander Afanasyev8566f452012-12-10 15:21:51 -0800130 {
131 max_size_ = max_size;
132 }
133
134 inline size_t
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800135 get_max_size() const
Alexander Afanasyev8566f452012-12-10 15:21:51 -0800136 {
137 return max_size_;
138 }
139
140 void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800141 set_traced_callback(
142 TracedCallback<typename parent_trie::payload_traits::const_base_type, Time>* callback)
Alexander Afanasyev8566f452012-12-10 15:21:51 -0800143 {
Alexander Afanasyev8566f452012-12-10 15:21:51 -0800144 m_willRemoveEntry = callback;
145 }
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800146
Alexander Afanasyev8566f452012-12-10 15:21:51 -0800147 private:
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800148 type()
149 : base_(*((Base*)0)){};
150
Alexander Afanasyev8566f452012-12-10 15:21:51 -0800151 private:
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800152 Base& base_;
Alexander Afanasyev8566f452012-12-10 15:21:51 -0800153 size_t max_size_;
154
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800155 TracedCallback<typename parent_trie::payload_traits::const_base_type, Time>*
156 m_willRemoveEntry;
Alexander Afanasyev8566f452012-12-10 15:21:51 -0800157 };
158 };
159};
160
161} // ndnSIM
162} // ndn
163} // ns3
164
Spyridon Mastorakis460f57c2014-12-17 00:44:14 -0800165/// @endcond
166
Alexander Afanasyev8566f452012-12-10 15:21:51 -0800167#endif // LIFETIME_STATS_POLICY_H