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 | cd31abd | 2013-09-05 20:45:02 -0700 | [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 | cd31abd | 2013-09-05 20:45:02 -0700 | [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 | cd31abd | 2013-09-05 20:45:02 -0700 | [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 | cd31abd | 2013-09-05 20:45:02 -0700 | [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 | cd31abd | 2013-09-05 20:45:02 -0700 | [diff] [blame] | 19 | |
| 20 | #ifndef PROBABILITY_POLICY_H_ |
| 21 | #define PROBABILITY_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 | cd31abd | 2013-09-05 20:45:02 -0700 | [diff] [blame] | 25 | #include <boost/intrusive/options.hpp> |
| 26 | #include <boost/intrusive/list.hpp> |
| 27 | |
| 28 | #include <ns3/random-variable.h> |
| 29 | |
| 30 | namespace ns3 { |
| 31 | namespace ndn { |
| 32 | namespace ndnSIM { |
| 33 | |
| 34 | /** |
| 35 | * @brief Traits for freshness policy |
| 36 | */ |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 37 | struct probability_policy_traits { |
| 38 | static std::string |
| 39 | GetName() |
Alexander Afanasyev | cd31abd | 2013-09-05 20:45:02 -0700 | [diff] [blame] | 40 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 41 | return "ProbabilityImpl"; |
| 42 | } |
| 43 | |
| 44 | struct policy_hook_type : public boost::intrusive::list_member_hook<> { |
Alexander Afanasyev | cd31abd | 2013-09-05 20:45:02 -0700 | [diff] [blame] | 45 | }; |
| 46 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 47 | template<class Container> |
| 48 | struct container_hook { |
| 49 | typedef boost::intrusive::member_hook<Container, policy_hook_type, &Container::policy_hook_> |
| 50 | type; |
| 51 | }; |
| 52 | |
| 53 | template<class Base, class Container, class Hook> |
| 54 | struct policy { |
| 55 | typedef typename boost::intrusive::list<Container, Hook> policy_container; |
| 56 | |
| 57 | class type : public policy_container { |
Alexander Afanasyev | cd31abd | 2013-09-05 20:45:02 -0700 | [diff] [blame] | 58 | public: |
| 59 | typedef policy policy_base; // to get access to get_freshness methods from outside |
| 60 | typedef Container parent_trie; |
| 61 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 62 | type(Base& base) |
| 63 | : base_(base) |
| 64 | , max_size_(100) |
| 65 | , probability_(1.0) |
Alexander Afanasyev | cd31abd | 2013-09-05 20:45:02 -0700 | [diff] [blame] | 66 | { |
| 67 | } |
| 68 | |
| 69 | inline void |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 70 | update(typename parent_trie::iterator item) |
Alexander Afanasyev | cd31abd | 2013-09-05 20:45:02 -0700 | [diff] [blame] | 71 | { |
| 72 | } |
| 73 | |
| 74 | inline bool |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 75 | insert(typename parent_trie::iterator item) |
Alexander Afanasyev | cd31abd | 2013-09-05 20:45:02 -0700 | [diff] [blame] | 76 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 77 | if (ns3_rand_.GetValue() < probability_) { |
| 78 | policy_container::push_back(*item); |
Alexander Afanasyev | cd31abd | 2013-09-05 20:45:02 -0700 | [diff] [blame] | 79 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 80 | // allow caching |
| 81 | return true; |
| 82 | } |
| 83 | else { |
| 84 | // don't allow caching |
| 85 | return false; |
| 86 | } |
Alexander Afanasyev | cd31abd | 2013-09-05 20:45:02 -0700 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | inline void |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 90 | lookup(typename parent_trie::iterator item) |
Alexander Afanasyev | cd31abd | 2013-09-05 20:45:02 -0700 | [diff] [blame] | 91 | { |
| 92 | // do nothing. it's random policy |
| 93 | } |
| 94 | |
| 95 | inline void |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 96 | erase(typename parent_trie::iterator item) |
Alexander Afanasyev | cd31abd | 2013-09-05 20:45:02 -0700 | [diff] [blame] | 97 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 98 | policy_container::erase(policy_container::s_iterator_to(*item)); |
Alexander Afanasyev | cd31abd | 2013-09-05 20:45:02 -0700 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | inline void |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 102 | clear() |
Alexander Afanasyev | cd31abd | 2013-09-05 20:45:02 -0700 | [diff] [blame] | 103 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 104 | policy_container::clear(); |
Alexander Afanasyev | cd31abd | 2013-09-05 20:45:02 -0700 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | inline void |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 108 | set_max_size(size_t max_size) |
Alexander Afanasyev | cd31abd | 2013-09-05 20:45:02 -0700 | [diff] [blame] | 109 | { |
| 110 | max_size_ = max_size; |
| 111 | } |
| 112 | |
| 113 | inline size_t |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 114 | get_max_size() const |
Alexander Afanasyev | cd31abd | 2013-09-05 20:45:02 -0700 | [diff] [blame] | 115 | { |
| 116 | return max_size_; |
| 117 | } |
| 118 | |
| 119 | inline void |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 120 | set_probability(double probability) |
Alexander Afanasyev | cd31abd | 2013-09-05 20:45:02 -0700 | [diff] [blame] | 121 | { |
| 122 | probability_ = probability; |
| 123 | } |
| 124 | |
| 125 | inline double |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 126 | get_probability() const |
Alexander Afanasyev | cd31abd | 2013-09-05 20:45:02 -0700 | [diff] [blame] | 127 | { |
| 128 | return probability_; |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 129 | } |
Alexander Afanasyev | cd31abd | 2013-09-05 20:45:02 -0700 | [diff] [blame] | 130 | |
| 131 | private: |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 132 | type() |
| 133 | : base_(*((Base*)0)){}; |
Alexander Afanasyev | cd31abd | 2013-09-05 20:45:02 -0700 | [diff] [blame] | 134 | |
| 135 | private: |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 136 | Base& base_; |
Alexander Afanasyev | cd31abd | 2013-09-05 20:45:02 -0700 | [diff] [blame] | 137 | size_t max_size_; |
| 138 | double probability_; |
| 139 | UniformVariable ns3_rand_; |
| 140 | }; |
| 141 | }; |
| 142 | }; |
| 143 | |
| 144 | } // ndnSIM |
| 145 | } // ndn |
| 146 | } // ns3 |
| 147 | |
| 148 | #endif // PROBABILITY_POLICY_H |