Alexander Afanasyev | c3cc0b3 | 2012-12-12 18:41:20 -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 FRESHNESS_POLICY_H_ |
| 22 | #define FRESHNESS_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 | c3cc0b3 | 2012-12-12 18:41:20 -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 freshness policy |
| 39 | */ |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 40 | struct freshness_policy_traits { |
Alexander Afanasyev | c3cc0b3 | 2012-12-12 18:41:20 -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 | c3cc0b3 | 2012-12-12 18:41:20 -0800 | [diff] [blame] | 44 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 45 | return "Freshness"; |
| 46 | } |
| 47 | |
| 48 | struct policy_hook_type : public boost::intrusive::set_member_hook<> { |
| 49 | Time timeWhenShouldExpire; |
Alexander Afanasyev | c3cc0b3 | 2012-12-12 18:41:20 -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 | static Time& |
| 61 | get_freshness(typename Container::iterator item) |
Alexander Afanasyev | c3cc0b3 | 2012-12-12 18:41:20 -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))->timeWhenShouldExpire; |
Alexander Afanasyev | c3cc0b3 | 2012-12-12 18:41:20 -0800 | [diff] [blame] | 65 | } |
Alexander Afanasyev | 41824bd | 2013-01-23 23:57:59 -0800 | [diff] [blame] | 66 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 67 | static const Time& |
| 68 | get_freshness(typename Container::const_iterator item) |
Alexander Afanasyev | c3cc0b3 | 2012-12-12 18:41:20 -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))->timeWhenShouldExpire; |
Alexander Afanasyev | c3cc0b3 | 2012-12-12 18:41:20 -0800 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | template<class Key> |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 75 | struct MemberHookLess { |
| 76 | bool |
| 77 | operator()(const Key& a, const Key& b) const |
Alexander Afanasyev | c3cc0b3 | 2012-12-12 18:41:20 -0800 | [diff] [blame] | 78 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 79 | return get_freshness(&a) < get_freshness(&b); |
Alexander Afanasyev | c3cc0b3 | 2012-12-12 18:41:20 -0800 | [diff] [blame] | 80 | } |
| 81 | }; |
| 82 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 83 | typedef boost::intrusive::multiset<Container, |
| 84 | boost::intrusive::compare<MemberHookLess<Container>>, |
| 85 | Hook> policy_container; |
Alexander Afanasyev | c3cc0b3 | 2012-12-12 18:41:20 -0800 | [diff] [blame] | 86 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 87 | class type : public policy_container { |
Alexander Afanasyev | c3cc0b3 | 2012-12-12 18:41:20 -0800 | [diff] [blame] | 88 | public: |
Alexander Afanasyev | 6978611 | 2012-12-13 11:53:36 -0800 | [diff] [blame] | 89 | typedef policy policy_base; // to get access to get_freshness methods from outside |
Alexander Afanasyev | c3cc0b3 | 2012-12-12 18:41:20 -0800 | [diff] [blame] | 90 | typedef Container parent_trie; |
| 91 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 92 | type(Base& base) |
| 93 | : base_(base) |
| 94 | , max_size_(100) |
Alexander Afanasyev | c3cc0b3 | 2012-12-12 18:41:20 -0800 | [diff] [blame] | 95 | { |
| 96 | } |
| 97 | |
| 98 | inline void |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 99 | update(typename parent_trie::iterator item) |
Alexander Afanasyev | c3cc0b3 | 2012-12-12 18:41:20 -0800 | [diff] [blame] | 100 | { |
Alexander Afanasyev | 41824bd | 2013-01-23 23:57:59 -0800 | [diff] [blame] | 101 | // do nothing |
Alexander Afanasyev | c3cc0b3 | 2012-12-12 18:41:20 -0800 | [diff] [blame] | 102 | } |
Alexander Afanasyev | 41824bd | 2013-01-23 23:57:59 -0800 | [diff] [blame] | 103 | |
Alexander Afanasyev | c3cc0b3 | 2012-12-12 18:41:20 -0800 | [diff] [blame] | 104 | inline bool |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 105 | insert(typename parent_trie::iterator item) |
Alexander Afanasyev | c3cc0b3 | 2012-12-12 18:41:20 -0800 | [diff] [blame] | 106 | { |
| 107 | // get_time (item) = Simulator::Now (); |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 108 | Time freshness = item->payload()->GetData()->GetFreshness(); |
| 109 | if (!freshness.IsZero()) { |
| 110 | get_freshness(item) = Simulator::Now() + freshness; |
Alexander Afanasyev | c3cc0b3 | 2012-12-12 18:41:20 -0800 | [diff] [blame] | 111 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 112 | // push item only if freshness is non zero. otherwise, this payload is not controlled by |
| 113 | // the policy |
| 114 | // note that .size() on this policy would return only number of items with non-infinite |
| 115 | // freshness policy |
| 116 | policy_container::insert(*item); |
| 117 | } |
Alexander Afanasyev | c3cc0b3 | 2012-12-12 18:41:20 -0800 | [diff] [blame] | 118 | |
| 119 | return true; |
| 120 | } |
Alexander Afanasyev | 41824bd | 2013-01-23 23:57:59 -0800 | [diff] [blame] | 121 | |
Alexander Afanasyev | c3cc0b3 | 2012-12-12 18:41:20 -0800 | [diff] [blame] | 122 | inline void |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 123 | lookup(typename parent_trie::iterator item) |
Alexander Afanasyev | c3cc0b3 | 2012-12-12 18:41:20 -0800 | [diff] [blame] | 124 | { |
| 125 | // do nothing. it's random policy |
| 126 | } |
Alexander Afanasyev | 41824bd | 2013-01-23 23:57:59 -0800 | [diff] [blame] | 127 | |
Alexander Afanasyev | c3cc0b3 | 2012-12-12 18:41:20 -0800 | [diff] [blame] | 128 | inline void |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 129 | erase(typename parent_trie::iterator item) |
Alexander Afanasyev | c3cc0b3 | 2012-12-12 18:41:20 -0800 | [diff] [blame] | 130 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 131 | if (!item->payload()->GetData()->GetFreshness().IsZero()) { |
| 132 | // erase only if freshness is non zero (otherwise an item is not in the policy |
| 133 | policy_container::erase(policy_container::s_iterator_to(*item)); |
| 134 | } |
Alexander Afanasyev | c3cc0b3 | 2012-12-12 18:41:20 -0800 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | inline void |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 138 | clear() |
Alexander Afanasyev | c3cc0b3 | 2012-12-12 18:41:20 -0800 | [diff] [blame] | 139 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 140 | policy_container::clear(); |
Alexander Afanasyev | c3cc0b3 | 2012-12-12 18:41:20 -0800 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | inline void |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 144 | set_max_size(size_t max_size) |
Alexander Afanasyev | c3cc0b3 | 2012-12-12 18:41:20 -0800 | [diff] [blame] | 145 | { |
| 146 | max_size_ = max_size; |
| 147 | } |
| 148 | |
| 149 | inline size_t |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 150 | get_max_size() const |
Alexander Afanasyev | c3cc0b3 | 2012-12-12 18:41:20 -0800 | [diff] [blame] | 151 | { |
| 152 | return max_size_; |
| 153 | } |
| 154 | |
| 155 | private: |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 156 | type() |
| 157 | : base_(*((Base*)0)){}; |
Alexander Afanasyev | 41824bd | 2013-01-23 23:57:59 -0800 | [diff] [blame] | 158 | |
Alexander Afanasyev | c3cc0b3 | 2012-12-12 18:41:20 -0800 | [diff] [blame] | 159 | private: |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 160 | Base& base_; |
Alexander Afanasyev | c3cc0b3 | 2012-12-12 18:41:20 -0800 | [diff] [blame] | 161 | size_t max_size_; |
| 162 | }; |
| 163 | }; |
| 164 | }; |
| 165 | |
| 166 | } // ndnSIM |
| 167 | } // ndn |
| 168 | } // ns3 |
| 169 | |
| 170 | #endif // LIFETIME_STATS_POLICY_H |