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