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: |
Alexander Afanasyev | 6978611 | 2012-12-13 11:53:36 -0800 | [diff] [blame] | 85 | typedef policy policy_base; // to get access to get_order methods from outside |
Alexander Afanasyev | 9e96e36 | 2012-07-02 23:04:39 -0700 | [diff] [blame] | 86 | typedef Container parent_trie; |
| 87 | |
| 88 | type (Base &base) |
| 89 | : base_ (base) |
| 90 | , u_rand (0, std::numeric_limits<uint32_t>::max ()) |
| 91 | , max_size_ (100) |
| 92 | { |
| 93 | } |
| 94 | |
| 95 | inline void |
| 96 | update (typename parent_trie::iterator item) |
| 97 | { |
| 98 | // do nothing. it's random policy |
| 99 | } |
Alexander Afanasyev | 9a98970 | 2012-06-29 17:44:00 -0700 | [diff] [blame] | 100 | |
Alexander Afanasyev | 9e96e36 | 2012-07-02 23:04:39 -0700 | [diff] [blame] | 101 | inline bool |
| 102 | insert (typename parent_trie::iterator item) |
| 103 | { |
Alexander Afanasyev | 903062f | 2012-07-04 18:25:26 -0700 | [diff] [blame] | 104 | get_order (item) = u_rand.GetValue (); |
Alexander Afanasyev | 9a98970 | 2012-06-29 17:44:00 -0700 | [diff] [blame] | 105 | |
Alexander Afanasyev | bd6f3f4 | 2012-07-26 17:50:17 -0700 | [diff] [blame] | 106 | if (max_size_ != 0 && policy_container::size () >= max_size_) |
Alexander Afanasyev | 9e96e36 | 2012-07-02 23:04:39 -0700 | [diff] [blame] | 107 | { |
| 108 | if (MemberHookLess<Container>() (*item, *policy_container::begin ())) |
| 109 | { |
Alexander Afanasyev | 30cb117 | 2012-07-06 10:47:39 -0700 | [diff] [blame] | 110 | // std::cout << "Cannot add. Signaling fail\n"; |
Alexander Afanasyev | 9e96e36 | 2012-07-02 23:04:39 -0700 | [diff] [blame] | 111 | // just return false. Indicating that insert "failed" |
| 112 | return false; |
| 113 | } |
| 114 | else |
| 115 | { |
| 116 | // removing some random element |
| 117 | base_.erase (&(*policy_container::begin ())); |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | policy_container::insert (*item); |
| 122 | return true; |
| 123 | } |
Alexander Afanasyev | 9a98970 | 2012-06-29 17:44:00 -0700 | [diff] [blame] | 124 | |
Alexander Afanasyev | 9e96e36 | 2012-07-02 23:04:39 -0700 | [diff] [blame] | 125 | inline void |
| 126 | lookup (typename parent_trie::iterator item) |
| 127 | { |
| 128 | // do nothing. it's random policy |
| 129 | } |
Alexander Afanasyev | 9a98970 | 2012-06-29 17:44:00 -0700 | [diff] [blame] | 130 | |
Alexander Afanasyev | 9e96e36 | 2012-07-02 23:04:39 -0700 | [diff] [blame] | 131 | inline void |
| 132 | erase (typename parent_trie::iterator item) |
| 133 | { |
| 134 | policy_container::erase (policy_container::s_iterator_to (*item)); |
| 135 | } |
Alexander Afanasyev | 9a98970 | 2012-06-29 17:44:00 -0700 | [diff] [blame] | 136 | |
Alexander Afanasyev | 9e96e36 | 2012-07-02 23:04:39 -0700 | [diff] [blame] | 137 | inline void |
Alexander Afanasyev | 78057c3 | 2012-07-06 15:18:46 -0700 | [diff] [blame] | 138 | clear () |
| 139 | { |
| 140 | policy_container::clear (); |
| 141 | } |
| 142 | |
| 143 | inline void |
Alexander Afanasyev | 9e96e36 | 2012-07-02 23:04:39 -0700 | [diff] [blame] | 144 | set_max_size (size_t max_size) |
| 145 | { |
| 146 | max_size_ = max_size; |
| 147 | } |
Alexander Afanasyev | 9a98970 | 2012-06-29 17:44:00 -0700 | [diff] [blame] | 148 | |
Alexander Afanasyev | 9e96e36 | 2012-07-02 23:04:39 -0700 | [diff] [blame] | 149 | inline size_t |
| 150 | get_max_size () const |
| 151 | { |
| 152 | return max_size_; |
| 153 | } |
Alexander Afanasyev | 9a98970 | 2012-06-29 17:44:00 -0700 | [diff] [blame] | 154 | |
Alexander Afanasyev | 9e96e36 | 2012-07-02 23:04:39 -0700 | [diff] [blame] | 155 | private: |
Alexander Afanasyev | 903062f | 2012-07-04 18:25:26 -0700 | [diff] [blame] | 156 | type () : base_(*((Base*)0)) { }; |
| 157 | |
| 158 | private: |
Alexander Afanasyev | 9e96e36 | 2012-07-02 23:04:39 -0700 | [diff] [blame] | 159 | Base &base_; |
| 160 | ns3::UniformVariable u_rand; |
| 161 | size_t max_size_; |
| 162 | }; |
Alexander Afanasyev | 9a98970 | 2012-06-29 17:44:00 -0700 | [diff] [blame] | 163 | }; |
| 164 | }; |
| 165 | |
Alexander Afanasyev | 30cb117 | 2012-07-06 10:47:39 -0700 | [diff] [blame] | 166 | } // ndnSIM |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 167 | } // ndn |
Alexander Afanasyev | e77db79 | 2012-08-09 11:10:58 -0700 | [diff] [blame] | 168 | } // ns3 |
Alexander Afanasyev | 30cb117 | 2012-07-06 10:47:39 -0700 | [diff] [blame] | 169 | |
Alexander Afanasyev | 9a98970 | 2012-06-29 17:44:00 -0700 | [diff] [blame] | 170 | #endif // RANDOM_POLICY_H |