Alexander Afanasyev | 9a98970 | 2012-06-29 17:44:00 -0700 | [diff] [blame] | 1 | /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /* |
| 3 | * Copyright (c) 2011 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 RANDOM_POLICY_H_ |
| 22 | #define RANDOM_POLICY_H_ |
| 23 | |
| 24 | #include "ns3/random-variable.h" |
| 25 | |
Alexander Afanasyev | 30cb117 | 2012-07-06 10:47:39 -0700 | [diff] [blame] | 26 | #include <boost/intrusive/options.hpp> |
| 27 | #include <boost/intrusive/set.hpp> |
| 28 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 29 | namespace ns3 { |
| 30 | namespace ndn { |
| 31 | namespace ndnSIM { |
Alexander Afanasyev | 30cb117 | 2012-07-06 10:47:39 -0700 | [diff] [blame] | 32 | |
Alexander Afanasyev | 30f60e3 | 2012-07-10 14:21:16 -0700 | [diff] [blame] | 33 | /** |
| 34 | * @brief Traits for random replacement policy |
| 35 | */ |
Alexander Afanasyev | 9a98970 | 2012-06-29 17:44:00 -0700 | [diff] [blame] | 36 | struct random_policy_traits |
| 37 | { |
Alexander Afanasyev | 991a0cc | 2012-12-10 11:38:19 -0800 | [diff] [blame] | 38 | /// @brief Name that can be used to identify the policy (for NS-3 object model and logging) |
| 39 | static std::string GetName () { return "Random"; } |
| 40 | |
Alexander Afanasyev | 30cb117 | 2012-07-06 10:47:39 -0700 | [diff] [blame] | 41 | struct policy_hook_type : public boost::intrusive::set_member_hook<> { uint32_t randomOrder; }; |
Alexander Afanasyev | 9a98970 | 2012-06-29 17:44:00 -0700 | [diff] [blame] | 42 | |
Alexander Afanasyev | 9e96e36 | 2012-07-02 23:04:39 -0700 | [diff] [blame] | 43 | template<class Container> |
| 44 | struct container_hook |
Alexander Afanasyev | 9a98970 | 2012-06-29 17:44:00 -0700 | [diff] [blame] | 45 | { |
Alexander Afanasyev | 30cb117 | 2012-07-06 10:47:39 -0700 | [diff] [blame] | 46 | typedef boost::intrusive::member_hook< Container, |
| 47 | policy_hook_type, |
| 48 | &Container::policy_hook_ > type; |
Alexander Afanasyev | 9e96e36 | 2012-07-02 23:04:39 -0700 | [diff] [blame] | 49 | }; |
| 50 | |
| 51 | template<class Base, |
| 52 | class Container, |
| 53 | class Hook> |
| 54 | struct policy |
| 55 | { |
Alexander Afanasyev | 903062f | 2012-07-04 18:25:26 -0700 | [diff] [blame] | 56 | static uint32_t& get_order (typename Container::iterator item) |
| 57 | { |
| 58 | return static_cast<typename policy_container::value_traits::hook_type*> |
| 59 | (policy_container::value_traits::to_node_ptr(*item))->randomOrder; |
| 60 | } |
| 61 | |
| 62 | static const uint32_t& get_order (typename Container::const_iterator item) |
| 63 | { |
| 64 | return static_cast<const typename policy_container::value_traits::hook_type*> |
| 65 | (policy_container::value_traits::to_node_ptr(*item))->randomOrder; |
| 66 | } |
| 67 | |
Alexander Afanasyev | 9e96e36 | 2012-07-02 23:04:39 -0700 | [diff] [blame] | 68 | template<class Key> |
| 69 | struct MemberHookLess |
Alexander Afanasyev | 9a98970 | 2012-06-29 17:44:00 -0700 | [diff] [blame] | 70 | { |
Alexander Afanasyev | 9e96e36 | 2012-07-02 23:04:39 -0700 | [diff] [blame] | 71 | bool operator () (const Key &a, const Key &b) const |
| 72 | { |
Alexander Afanasyev | 903062f | 2012-07-04 18:25:26 -0700 | [diff] [blame] | 73 | return get_order (&a) < get_order (&b); |
Alexander Afanasyev | 9e96e36 | 2012-07-02 23:04:39 -0700 | [diff] [blame] | 74 | } |
| 75 | }; |
| 76 | |
Alexander Afanasyev | ad82e25 | 2012-07-29 23:51:21 -0700 | [diff] [blame] | 77 | typedef boost::intrusive::multiset< Container, |
Alexander Afanasyev | 30cb117 | 2012-07-06 10:47:39 -0700 | [diff] [blame] | 78 | boost::intrusive::compare< MemberHookLess< Container > >, |
| 79 | Hook > policy_container; |
Alexander Afanasyev | 9e96e36 | 2012-07-02 23:04:39 -0700 | [diff] [blame] | 80 | |
| 81 | // could be just typedef |
| 82 | class type : public policy_container |
| 83 | { |
| 84 | public: |
| 85 | typedef Container parent_trie; |
| 86 | |
| 87 | type (Base &base) |
| 88 | : base_ (base) |
| 89 | , u_rand (0, std::numeric_limits<uint32_t>::max ()) |
| 90 | , max_size_ (100) |
| 91 | { |
| 92 | } |
| 93 | |
| 94 | inline void |
| 95 | update (typename parent_trie::iterator item) |
| 96 | { |
| 97 | // do nothing. it's random policy |
| 98 | } |
Alexander Afanasyev | 9a98970 | 2012-06-29 17:44:00 -0700 | [diff] [blame] | 99 | |
Alexander Afanasyev | 9e96e36 | 2012-07-02 23:04:39 -0700 | [diff] [blame] | 100 | inline bool |
| 101 | insert (typename parent_trie::iterator item) |
| 102 | { |
Alexander Afanasyev | 903062f | 2012-07-04 18:25:26 -0700 | [diff] [blame] | 103 | get_order (item) = u_rand.GetValue (); |
Alexander Afanasyev | 9a98970 | 2012-06-29 17:44:00 -0700 | [diff] [blame] | 104 | |
Alexander Afanasyev | bd6f3f4 | 2012-07-26 17:50:17 -0700 | [diff] [blame] | 105 | if (max_size_ != 0 && policy_container::size () >= max_size_) |
Alexander Afanasyev | 9e96e36 | 2012-07-02 23:04:39 -0700 | [diff] [blame] | 106 | { |
| 107 | if (MemberHookLess<Container>() (*item, *policy_container::begin ())) |
| 108 | { |
Alexander Afanasyev | 30cb117 | 2012-07-06 10:47:39 -0700 | [diff] [blame] | 109 | // std::cout << "Cannot add. Signaling fail\n"; |
Alexander Afanasyev | 9e96e36 | 2012-07-02 23:04:39 -0700 | [diff] [blame] | 110 | // just return false. Indicating that insert "failed" |
| 111 | return false; |
| 112 | } |
| 113 | else |
| 114 | { |
| 115 | // removing some random element |
| 116 | base_.erase (&(*policy_container::begin ())); |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | policy_container::insert (*item); |
| 121 | return true; |
| 122 | } |
Alexander Afanasyev | 9a98970 | 2012-06-29 17:44:00 -0700 | [diff] [blame] | 123 | |
Alexander Afanasyev | 9e96e36 | 2012-07-02 23:04:39 -0700 | [diff] [blame] | 124 | inline void |
| 125 | lookup (typename parent_trie::iterator item) |
| 126 | { |
| 127 | // do nothing. it's random policy |
| 128 | } |
Alexander Afanasyev | 9a98970 | 2012-06-29 17:44:00 -0700 | [diff] [blame] | 129 | |
Alexander Afanasyev | 9e96e36 | 2012-07-02 23:04:39 -0700 | [diff] [blame] | 130 | inline void |
| 131 | erase (typename parent_trie::iterator item) |
| 132 | { |
| 133 | policy_container::erase (policy_container::s_iterator_to (*item)); |
| 134 | } |
Alexander Afanasyev | 9a98970 | 2012-06-29 17:44:00 -0700 | [diff] [blame] | 135 | |
Alexander Afanasyev | 9e96e36 | 2012-07-02 23:04:39 -0700 | [diff] [blame] | 136 | inline void |
Alexander Afanasyev | 78057c3 | 2012-07-06 15:18:46 -0700 | [diff] [blame] | 137 | clear () |
| 138 | { |
| 139 | policy_container::clear (); |
| 140 | } |
| 141 | |
| 142 | inline void |
Alexander Afanasyev | 9e96e36 | 2012-07-02 23:04:39 -0700 | [diff] [blame] | 143 | set_max_size (size_t max_size) |
| 144 | { |
| 145 | max_size_ = max_size; |
| 146 | } |
Alexander Afanasyev | 9a98970 | 2012-06-29 17:44:00 -0700 | [diff] [blame] | 147 | |
Alexander Afanasyev | 9e96e36 | 2012-07-02 23:04:39 -0700 | [diff] [blame] | 148 | inline size_t |
| 149 | get_max_size () const |
| 150 | { |
| 151 | return max_size_; |
| 152 | } |
Alexander Afanasyev | 9a98970 | 2012-06-29 17:44:00 -0700 | [diff] [blame] | 153 | |
Alexander Afanasyev | 9e96e36 | 2012-07-02 23:04:39 -0700 | [diff] [blame] | 154 | private: |
Alexander Afanasyev | 903062f | 2012-07-04 18:25:26 -0700 | [diff] [blame] | 155 | type () : base_(*((Base*)0)) { }; |
| 156 | |
| 157 | private: |
Alexander Afanasyev | 9e96e36 | 2012-07-02 23:04:39 -0700 | [diff] [blame] | 158 | Base &base_; |
| 159 | ns3::UniformVariable u_rand; |
| 160 | size_t max_size_; |
| 161 | }; |
Alexander Afanasyev | 9a98970 | 2012-06-29 17:44:00 -0700 | [diff] [blame] | 162 | }; |
| 163 | }; |
| 164 | |
Alexander Afanasyev | 30cb117 | 2012-07-06 10:47:39 -0700 | [diff] [blame] | 165 | } // ndnSIM |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 166 | } // ndn |
Alexander Afanasyev | e77db79 | 2012-08-09 11:10:58 -0700 | [diff] [blame] | 167 | } // ns3 |
Alexander Afanasyev | 30cb117 | 2012-07-06 10:47:39 -0700 | [diff] [blame] | 168 | |
Alexander Afanasyev | 9a98970 | 2012-06-29 17:44:00 -0700 | [diff] [blame] | 169 | #endif // RANDOM_POLICY_H |