Alexander Afanasyev | 60a7b62 | 2014-12-20 17:04:07 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2011-2015 Regents of the University of California. |
Alexander Afanasyev | 8566f45 | 2012-12-10 15:21:51 -0800 | [diff] [blame] | 4 | * |
Alexander Afanasyev | 60a7b62 | 2014-12-20 17:04:07 -0800 | [diff] [blame] | 5 | * This file is part of ndnSIM. See AUTHORS for complete list of ndnSIM authors and |
| 6 | * contributors. |
Alexander Afanasyev | 8566f45 | 2012-12-10 15:21:51 -0800 | [diff] [blame] | 7 | * |
Alexander Afanasyev | 60a7b62 | 2014-12-20 17:04:07 -0800 | [diff] [blame] | 8 | * 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 Afanasyev | 8566f45 | 2012-12-10 15:21:51 -0800 | [diff] [blame] | 11 | * |
Alexander Afanasyev | 60a7b62 | 2014-12-20 17:04:07 -0800 | [diff] [blame] | 12 | * 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 Afanasyev | 8566f45 | 2012-12-10 15:21:51 -0800 | [diff] [blame] | 15 | * |
Alexander Afanasyev | 60a7b62 | 2014-12-20 17:04:07 -0800 | [diff] [blame] | 16 | * 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 Afanasyev | 8566f45 | 2012-12-10 15:21:51 -0800 | [diff] [blame] | 19 | |
| 20 | #ifndef LIFETIME_STATS_POLICY_H_ |
| 21 | #define LIFETIME_STATS_POLICY_H_ |
| 22 | |
Spyridon Mastorakis | 53e922f | 2014-10-17 17:29:26 -0700 | [diff] [blame] | 23 | #include "ns3/ndnSIM/model/ndn-common.hpp" |
| 24 | |
Alexander Afanasyev | 8566f45 | 2012-12-10 15:21:51 -0800 | [diff] [blame] | 25 | #include <boost/intrusive/options.hpp> |
| 26 | #include <boost/intrusive/list.hpp> |
| 27 | |
| 28 | #include <ns3/nstime.h> |
| 29 | #include <ns3/simulator.h> |
| 30 | #include <ns3/traced-callback.h> |
| 31 | |
| 32 | namespace ns3 { |
| 33 | namespace ndn { |
| 34 | namespace ndnSIM { |
| 35 | |
| 36 | /** |
| 37 | * @brief Traits for lifetime stats policy |
| 38 | */ |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 39 | struct lifetime_stats_policy_traits { |
Alexander Afanasyev | 8566f45 | 2012-12-10 15:21:51 -0800 | [diff] [blame] | 40 | /// @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] | 41 | static std::string |
| 42 | GetName() |
Alexander Afanasyev | 8566f45 | 2012-12-10 15:21:51 -0800 | [diff] [blame] | 43 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 44 | return "LifetimeStats"; |
| 45 | } |
| 46 | |
| 47 | struct policy_hook_type : public boost::intrusive::list_member_hook<> { |
| 48 | Time timeWhenAdded; |
Alexander Afanasyev | 8566f45 | 2012-12-10 15:21:51 -0800 | [diff] [blame] | 49 | }; |
| 50 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 51 | template<class Container> |
| 52 | struct container_hook { |
| 53 | typedef boost::intrusive::member_hook<Container, policy_hook_type, &Container::policy_hook_> |
| 54 | type; |
| 55 | }; |
| 56 | |
| 57 | template<class Base, class Container, class Hook> |
| 58 | struct policy { |
| 59 | typedef typename boost::intrusive::list<Container, Hook> policy_container; |
| 60 | |
| 61 | static Time& |
| 62 | get_time(typename Container::iterator item) |
Alexander Afanasyev | 8566f45 | 2012-12-10 15:21:51 -0800 | [diff] [blame] | 63 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 64 | return static_cast<typename policy_container::value_traits::hook_type*>( |
| 65 | policy_container::value_traits::to_node_ptr(*item))->timeWhenAdded; |
Alexander Afanasyev | 8566f45 | 2012-12-10 15:21:51 -0800 | [diff] [blame] | 66 | } |
| 67 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 68 | static const Time& |
| 69 | get_time(typename Container::const_iterator item) |
Alexander Afanasyev | 8566f45 | 2012-12-10 15:21:51 -0800 | [diff] [blame] | 70 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 71 | return static_cast<const typename policy_container::value_traits::hook_type*>( |
| 72 | policy_container::value_traits::to_node_ptr(*item))->timeWhenAdded; |
| 73 | } |
| 74 | |
| 75 | class type : public policy_container { |
Alexander Afanasyev | 8566f45 | 2012-12-10 15:21:51 -0800 | [diff] [blame] | 76 | public: |
Alexander Afanasyev | 6978611 | 2012-12-13 11:53:36 -0800 | [diff] [blame] | 77 | 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] | 78 | typedef Container parent_trie; |
| 79 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 80 | type(Base& base) |
| 81 | : base_(base) |
| 82 | , max_size_(100) |
| 83 | , m_willRemoveEntry(0) |
Alexander Afanasyev | 8566f45 | 2012-12-10 15:21:51 -0800 | [diff] [blame] | 84 | { |
| 85 | } |
| 86 | |
| 87 | inline void |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 88 | update(typename parent_trie::iterator item) |
Alexander Afanasyev | 8566f45 | 2012-12-10 15:21:51 -0800 | [diff] [blame] | 89 | { |
| 90 | // do nothing. it's random policy |
| 91 | } |
Alexander Afanasyev | 8566f45 | 2012-12-10 15:21:51 -0800 | [diff] [blame] | 92 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 93 | inline bool |
| 94 | insert(typename parent_trie::iterator item) |
| 95 | { |
| 96 | get_time(item) = Simulator::Now(); |
| 97 | |
| 98 | policy_container::push_back(*item); |
Alexander Afanasyev | 8566f45 | 2012-12-10 15:21:51 -0800 | [diff] [blame] | 99 | return true; |
| 100 | } |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 101 | |
Alexander Afanasyev | 8566f45 | 2012-12-10 15:21:51 -0800 | [diff] [blame] | 102 | inline void |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 103 | lookup(typename parent_trie::iterator item) |
Alexander Afanasyev | 8566f45 | 2012-12-10 15:21:51 -0800 | [diff] [blame] | 104 | { |
| 105 | // do nothing. it's random policy |
| 106 | } |
Alexander Afanasyev | 8566f45 | 2012-12-10 15:21:51 -0800 | [diff] [blame] | 107 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 108 | inline void |
| 109 | erase(typename parent_trie::iterator item) |
| 110 | { |
| 111 | Time lifetime = Simulator::Now() - get_time(item); |
| 112 | |
| 113 | if (m_willRemoveEntry != 0) { |
| 114 | (*m_willRemoveEntry)(item->payload(), lifetime); |
| 115 | } |
| 116 | |
| 117 | policy_container::erase(policy_container::s_iterator_to(*item)); |
Alexander Afanasyev | 8566f45 | 2012-12-10 15:21:51 -0800 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | inline void |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 121 | clear() |
Alexander Afanasyev | 8566f45 | 2012-12-10 15:21:51 -0800 | [diff] [blame] | 122 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 123 | policy_container::clear(); |
Alexander Afanasyev | 8566f45 | 2012-12-10 15:21:51 -0800 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | inline void |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 127 | set_max_size(size_t max_size) |
Alexander Afanasyev | 8566f45 | 2012-12-10 15:21:51 -0800 | [diff] [blame] | 128 | { |
| 129 | max_size_ = max_size; |
| 130 | } |
| 131 | |
| 132 | inline size_t |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 133 | get_max_size() const |
Alexander Afanasyev | 8566f45 | 2012-12-10 15:21:51 -0800 | [diff] [blame] | 134 | { |
| 135 | return max_size_; |
| 136 | } |
| 137 | |
| 138 | void |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 139 | set_traced_callback( |
| 140 | TracedCallback<typename parent_trie::payload_traits::const_base_type, Time>* callback) |
Alexander Afanasyev | 8566f45 | 2012-12-10 15:21:51 -0800 | [diff] [blame] | 141 | { |
Alexander Afanasyev | 8566f45 | 2012-12-10 15:21:51 -0800 | [diff] [blame] | 142 | m_willRemoveEntry = callback; |
| 143 | } |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 144 | |
Alexander Afanasyev | 8566f45 | 2012-12-10 15:21:51 -0800 | [diff] [blame] | 145 | private: |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 146 | type() |
| 147 | : base_(*((Base*)0)){}; |
| 148 | |
Alexander Afanasyev | 8566f45 | 2012-12-10 15:21:51 -0800 | [diff] [blame] | 149 | private: |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 150 | Base& base_; |
Alexander Afanasyev | 8566f45 | 2012-12-10 15:21:51 -0800 | [diff] [blame] | 151 | size_t max_size_; |
| 152 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 153 | TracedCallback<typename parent_trie::payload_traits::const_base_type, Time>* |
| 154 | m_willRemoveEntry; |
Alexander Afanasyev | 8566f45 | 2012-12-10 15:21:51 -0800 | [diff] [blame] | 155 | }; |
| 156 | }; |
| 157 | }; |
| 158 | |
| 159 | } // ndnSIM |
| 160 | } // ndn |
| 161 | } // ns3 |
| 162 | |
| 163 | #endif // LIFETIME_STATS_POLICY_H |