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 | c3cc0b3 | 2012-12-12 18:41:20 -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 | c3cc0b3 | 2012-12-12 18:41:20 -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 | c3cc0b3 | 2012-12-12 18:41:20 -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 | c3cc0b3 | 2012-12-12 18:41:20 -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 | c3cc0b3 | 2012-12-12 18:41:20 -0800 | [diff] [blame] | 19 | |
| 20 | #ifndef FRESHNESS_POLICY_H_ |
| 21 | #define FRESHNESS_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 | c3cc0b3 | 2012-12-12 18:41:20 -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 freshness policy |
| 40 | */ |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 41 | struct freshness_policy_traits { |
Alexander Afanasyev | c3cc0b3 | 2012-12-12 18:41:20 -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 | c3cc0b3 | 2012-12-12 18:41:20 -0800 | [diff] [blame] | 45 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 46 | return "Freshness"; |
| 47 | } |
| 48 | |
| 49 | struct policy_hook_type : public boost::intrusive::set_member_hook<> { |
| 50 | Time timeWhenShouldExpire; |
Alexander Afanasyev | c3cc0b3 | 2012-12-12 18:41:20 -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 | static Time& |
| 62 | get_freshness(typename Container::iterator item) |
Alexander Afanasyev | c3cc0b3 | 2012-12-12 18:41:20 -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))->timeWhenShouldExpire; |
Alexander Afanasyev | c3cc0b3 | 2012-12-12 18:41:20 -0800 | [diff] [blame] | 66 | } |
Alexander Afanasyev | 41824bd | 2013-01-23 23:57:59 -0800 | [diff] [blame] | 67 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 68 | static const Time& |
| 69 | get_freshness(typename Container::const_iterator item) |
Alexander Afanasyev | c3cc0b3 | 2012-12-12 18:41:20 -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))->timeWhenShouldExpire; |
Alexander Afanasyev | c3cc0b3 | 2012-12-12 18:41:20 -0800 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | template<class Key> |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 76 | struct MemberHookLess { |
| 77 | bool |
| 78 | operator()(const Key& a, const Key& b) const |
Alexander Afanasyev | c3cc0b3 | 2012-12-12 18:41:20 -0800 | [diff] [blame] | 79 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 80 | return get_freshness(&a) < get_freshness(&b); |
Alexander Afanasyev | c3cc0b3 | 2012-12-12 18:41:20 -0800 | [diff] [blame] | 81 | } |
| 82 | }; |
| 83 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 84 | typedef boost::intrusive::multiset<Container, |
| 85 | boost::intrusive::compare<MemberHookLess<Container>>, |
| 86 | Hook> policy_container; |
Alexander Afanasyev | c3cc0b3 | 2012-12-12 18:41:20 -0800 | [diff] [blame] | 87 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 88 | class type : public policy_container { |
Alexander Afanasyev | c3cc0b3 | 2012-12-12 18:41:20 -0800 | [diff] [blame] | 89 | public: |
Alexander Afanasyev | 6978611 | 2012-12-13 11:53:36 -0800 | [diff] [blame] | 90 | 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] | 91 | typedef Container parent_trie; |
| 92 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 93 | type(Base& base) |
| 94 | : base_(base) |
| 95 | , max_size_(100) |
Alexander Afanasyev | c3cc0b3 | 2012-12-12 18:41:20 -0800 | [diff] [blame] | 96 | { |
| 97 | } |
| 98 | |
| 99 | inline void |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 100 | update(typename parent_trie::iterator item) |
Alexander Afanasyev | c3cc0b3 | 2012-12-12 18:41:20 -0800 | [diff] [blame] | 101 | { |
Alexander Afanasyev | 41824bd | 2013-01-23 23:57:59 -0800 | [diff] [blame] | 102 | // do nothing |
Alexander Afanasyev | c3cc0b3 | 2012-12-12 18:41:20 -0800 | [diff] [blame] | 103 | } |
Alexander Afanasyev | 41824bd | 2013-01-23 23:57:59 -0800 | [diff] [blame] | 104 | |
Alexander Afanasyev | c3cc0b3 | 2012-12-12 18:41:20 -0800 | [diff] [blame] | 105 | inline bool |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 106 | insert(typename parent_trie::iterator item) |
Alexander Afanasyev | c3cc0b3 | 2012-12-12 18:41:20 -0800 | [diff] [blame] | 107 | { |
Spyridon Mastorakis | de1f773 | 2014-12-05 22:43:34 -0800 | [diff] [blame] | 108 | time::milliseconds freshness = item->payload()->GetData()->getFreshnessPeriod(); |
| 109 | if (freshness > time::milliseconds::zero()) { |
| 110 | get_freshness(item) = Simulator::Now() + MilliSeconds(freshness.count()); |
Alexander Afanasyev | c3cc0b3 | 2012-12-12 18:41:20 -0800 | [diff] [blame] | 111 | |
Spyridon Mastorakis | de1f773 | 2014-12-05 22:43:34 -0800 | [diff] [blame] | 112 | // push item only if freshness is non zero. otherwise, this payload is not |
| 113 | // controlled by the policy. |
| 114 | // Note that .size() on this policy would return only the number of items with |
| 115 | // non-infinite freshness policy |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 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 | { |
Spyridon Mastorakis | de1f773 | 2014-12-05 22:43:34 -0800 | [diff] [blame] | 131 | time::milliseconds freshness = item->payload()->GetData()->getFreshnessPeriod(); |
| 132 | if (freshness > time::milliseconds::zero()) { |
| 133 | // erase only if freshness is positive (otherwise an item is not in the policy) |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 134 | policy_container::erase(policy_container::s_iterator_to(*item)); |
| 135 | } |
Alexander Afanasyev | c3cc0b3 | 2012-12-12 18:41:20 -0800 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | inline void |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 139 | clear() |
Alexander Afanasyev | c3cc0b3 | 2012-12-12 18:41:20 -0800 | [diff] [blame] | 140 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 141 | policy_container::clear(); |
Alexander Afanasyev | c3cc0b3 | 2012-12-12 18:41:20 -0800 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | inline void |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 145 | set_max_size(size_t max_size) |
Alexander Afanasyev | c3cc0b3 | 2012-12-12 18:41:20 -0800 | [diff] [blame] | 146 | { |
| 147 | max_size_ = max_size; |
| 148 | } |
| 149 | |
| 150 | inline size_t |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 151 | get_max_size() const |
Alexander Afanasyev | c3cc0b3 | 2012-12-12 18:41:20 -0800 | [diff] [blame] | 152 | { |
| 153 | return max_size_; |
| 154 | } |
| 155 | |
| 156 | private: |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 157 | type() |
| 158 | : base_(*((Base*)0)){}; |
Alexander Afanasyev | 41824bd | 2013-01-23 23:57:59 -0800 | [diff] [blame] | 159 | |
Alexander Afanasyev | c3cc0b3 | 2012-12-12 18:41:20 -0800 | [diff] [blame] | 160 | private: |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 161 | Base& base_; |
Alexander Afanasyev | c3cc0b3 | 2012-12-12 18:41:20 -0800 | [diff] [blame] | 162 | size_t max_size_; |
| 163 | }; |
| 164 | }; |
| 165 | }; |
| 166 | |
| 167 | } // ndnSIM |
| 168 | } // ndn |
| 169 | } // ns3 |
| 170 | |
Spyridon Mastorakis | 460f57c | 2014-12-17 00:44:14 -0800 | [diff] [blame] | 171 | /// @endcond |
| 172 | |
Alexander Afanasyev | c3cc0b3 | 2012-12-12 18:41:20 -0800 | [diff] [blame] | 173 | #endif // LIFETIME_STATS_POLICY_H |