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