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