Alexander Afanasyev | 8566f45 | 2012-12-10 15:21:51 -0800 | [diff] [blame] | 1 | /* -*- 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 Mastorakis | 53e922f | 2014-10-17 17:29:26 -0700 | [diff] [blame] | 24 | #include "ns3/ndnSIM/model/ndn-common.hpp" |
| 25 | |
Alexander Afanasyev | 8566f45 | 2012-12-10 15:21:51 -0800 | [diff] [blame] | 26 | #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 | |
| 33 | namespace ns3 { |
| 34 | namespace ndn { |
| 35 | namespace ndnSIM { |
| 36 | |
| 37 | /** |
| 38 | * @brief Traits for lifetime stats policy |
| 39 | */ |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 40 | struct lifetime_stats_policy_traits { |
Alexander Afanasyev | 8566f45 | 2012-12-10 15:21:51 -0800 | [diff] [blame] | 41 | /// @brief Name that can be used to identify the policy (for NS-3 object model and logging) |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 42 | static std::string |
| 43 | GetName() |
Alexander Afanasyev | 8566f45 | 2012-12-10 15:21:51 -0800 | [diff] [blame] | 44 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 45 | return "LifetimeStats"; |
| 46 | } |
| 47 | |
| 48 | struct policy_hook_type : public boost::intrusive::list_member_hook<> { |
| 49 | Time timeWhenAdded; |
Alexander Afanasyev | 8566f45 | 2012-12-10 15:21:51 -0800 | [diff] [blame] | 50 | }; |
| 51 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 52 | 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 Afanasyev | 8566f45 | 2012-12-10 15:21:51 -0800 | [diff] [blame] | 64 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 65 | return static_cast<typename policy_container::value_traits::hook_type*>( |
| 66 | policy_container::value_traits::to_node_ptr(*item))->timeWhenAdded; |
Alexander Afanasyev | 8566f45 | 2012-12-10 15:21:51 -0800 | [diff] [blame] | 67 | } |
| 68 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 69 | static const Time& |
| 70 | get_time(typename Container::const_iterator item) |
Alexander Afanasyev | 8566f45 | 2012-12-10 15:21:51 -0800 | [diff] [blame] | 71 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 72 | 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 Afanasyev | 8566f45 | 2012-12-10 15:21:51 -0800 | [diff] [blame] | 77 | public: |
Alexander Afanasyev | 6978611 | 2012-12-13 11:53:36 -0800 | [diff] [blame] | 78 | typedef policy policy_base; // to get access to get_time methods from outside |
Alexander Afanasyev | 8566f45 | 2012-12-10 15:21:51 -0800 | [diff] [blame] | 79 | typedef Container parent_trie; |
| 80 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 81 | type(Base& base) |
| 82 | : base_(base) |
| 83 | , max_size_(100) |
| 84 | , m_willRemoveEntry(0) |
Alexander Afanasyev | 8566f45 | 2012-12-10 15:21:51 -0800 | [diff] [blame] | 85 | { |
| 86 | } |
| 87 | |
| 88 | inline void |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 89 | update(typename parent_trie::iterator item) |
Alexander Afanasyev | 8566f45 | 2012-12-10 15:21:51 -0800 | [diff] [blame] | 90 | { |
| 91 | // do nothing. it's random policy |
| 92 | } |
Alexander Afanasyev | 8566f45 | 2012-12-10 15:21:51 -0800 | [diff] [blame] | 93 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 94 | inline bool |
| 95 | insert(typename parent_trie::iterator item) |
| 96 | { |
| 97 | get_time(item) = Simulator::Now(); |
| 98 | |
| 99 | policy_container::push_back(*item); |
Alexander Afanasyev | 8566f45 | 2012-12-10 15:21:51 -0800 | [diff] [blame] | 100 | return true; |
| 101 | } |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 102 | |
Alexander Afanasyev | 8566f45 | 2012-12-10 15:21:51 -0800 | [diff] [blame] | 103 | inline void |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 104 | lookup(typename parent_trie::iterator item) |
Alexander Afanasyev | 8566f45 | 2012-12-10 15:21:51 -0800 | [diff] [blame] | 105 | { |
| 106 | // do nothing. it's random policy |
| 107 | } |
Alexander Afanasyev | 8566f45 | 2012-12-10 15:21:51 -0800 | [diff] [blame] | 108 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 109 | 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 Afanasyev | 8566f45 | 2012-12-10 15:21:51 -0800 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | inline void |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 122 | clear() |
Alexander Afanasyev | 8566f45 | 2012-12-10 15:21:51 -0800 | [diff] [blame] | 123 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 124 | policy_container::clear(); |
Alexander Afanasyev | 8566f45 | 2012-12-10 15:21:51 -0800 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | inline void |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 128 | set_max_size(size_t max_size) |
Alexander Afanasyev | 8566f45 | 2012-12-10 15:21:51 -0800 | [diff] [blame] | 129 | { |
| 130 | max_size_ = max_size; |
| 131 | } |
| 132 | |
| 133 | inline size_t |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 134 | get_max_size() const |
Alexander Afanasyev | 8566f45 | 2012-12-10 15:21:51 -0800 | [diff] [blame] | 135 | { |
| 136 | return max_size_; |
| 137 | } |
| 138 | |
| 139 | void |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 140 | set_traced_callback( |
| 141 | TracedCallback<typename parent_trie::payload_traits::const_base_type, Time>* callback) |
Alexander Afanasyev | 8566f45 | 2012-12-10 15:21:51 -0800 | [diff] [blame] | 142 | { |
Alexander Afanasyev | 8566f45 | 2012-12-10 15:21:51 -0800 | [diff] [blame] | 143 | m_willRemoveEntry = callback; |
| 144 | } |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 145 | |
Alexander Afanasyev | 8566f45 | 2012-12-10 15:21:51 -0800 | [diff] [blame] | 146 | private: |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 147 | type() |
| 148 | : base_(*((Base*)0)){}; |
| 149 | |
Alexander Afanasyev | 8566f45 | 2012-12-10 15:21:51 -0800 | [diff] [blame] | 150 | private: |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 151 | Base& base_; |
Alexander Afanasyev | 8566f45 | 2012-12-10 15:21:51 -0800 | [diff] [blame] | 152 | size_t max_size_; |
| 153 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 154 | TracedCallback<typename parent_trie::payload_traits::const_base_type, Time>* |
| 155 | m_willRemoveEntry; |
Alexander Afanasyev | 8566f45 | 2012-12-10 15:21:51 -0800 | [diff] [blame] | 156 | }; |
| 157 | }; |
| 158 | }; |
| 159 | |
| 160 | } // ndnSIM |
| 161 | } // ndn |
| 162 | } // ns3 |
| 163 | |
| 164 | #endif // LIFETIME_STATS_POLICY_H |