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