Alexander Afanasyev | 7456b70 | 2013-02-01 22:41:48 -0800 | [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 SERIALIZED_POLICY_H_ |
| 22 | #define SERIALIZED_POLICY_H_ |
| 23 | |
| 24 | #include <boost/intrusive/options.hpp> |
| 25 | #include <boost/intrusive/set.hpp> |
| 26 | |
| 27 | namespace ns3 { |
| 28 | namespace ndn { |
| 29 | namespace ndnSIM { |
| 30 | |
| 31 | /** |
| 32 | * @brief Traits for Least Recently Used replacement policy |
| 33 | */ |
| 34 | struct serialized_size_policy_traits |
| 35 | { |
| 36 | /// @brief Name that can be used to identify the policy (for NS-3 object model and logging) |
| 37 | static std::string GetName () { return "SerializedSize"; } |
| 38 | |
| 39 | struct policy_hook_type : public boost::intrusive::set_member_hook<> { uint32_t size; }; |
| 40 | |
| 41 | template<class Container> |
| 42 | struct container_hook |
| 43 | { |
| 44 | typedef boost::intrusive::member_hook< Container, |
| 45 | policy_hook_type, |
| 46 | &Container::policy_hook_ > type; |
| 47 | }; |
| 48 | |
| 49 | template<class Base, |
| 50 | class Container, |
| 51 | class Hook> |
| 52 | struct policy |
| 53 | { |
| 54 | static uint32_t& get_size (typename Container::iterator item) |
| 55 | { |
| 56 | return static_cast<typename policy_container::value_traits::hook_type*> |
| 57 | (policy_container::value_traits::to_node_ptr(*item))->size; |
| 58 | } |
| 59 | |
| 60 | static const uint32_t& get_size (typename Container::const_iterator item) |
| 61 | { |
| 62 | return static_cast<const typename policy_container::value_traits::hook_type*> |
| 63 | (policy_container::value_traits::to_node_ptr(*item))->size; |
| 64 | } |
| 65 | |
| 66 | template<class Key> |
| 67 | struct MemberHookLess |
| 68 | { |
| 69 | bool operator () (const Key &a, const Key &b) const |
| 70 | { |
| 71 | return get_size (&a) < get_size (&b); |
| 72 | } |
| 73 | }; |
| 74 | |
| 75 | typedef typename boost::intrusive::multiset< Container, |
| 76 | boost::intrusive::compare< MemberHookLess< Container > >, |
| 77 | Hook > policy_container; |
| 78 | |
| 79 | // could be just typedef |
| 80 | class type : public policy_container |
| 81 | { |
| 82 | public: |
| 83 | typedef Container parent_trie; |
| 84 | |
| 85 | type (Base &base) |
| 86 | : base_ (base) |
| 87 | , max_size_ (10000) // size in bytes. Default ~10 kilobytes |
| 88 | , current_space_used_ (0) |
| 89 | { |
| 90 | } |
| 91 | |
| 92 | inline void |
| 93 | update (typename parent_trie::iterator item) |
| 94 | { |
| 95 | // in case size got changed |
| 96 | current_space_used_ -= get_size (item); |
| 97 | policy_container::erase (*item); |
| 98 | |
| 99 | get_order (item) = item->payload ()->GetInterest ()->GetSerializedSize (); |
| 100 | current_space_used_ += get_size (item); // this operation can violate policy constraint, so in some case |
| 101 | // it may be necessary to remove some other element |
| 102 | policy_container::insert (*item); |
| 103 | } |
| 104 | |
| 105 | inline bool |
| 106 | insert (typename parent_trie::iterator item) |
| 107 | { |
Alexander Afanasyev | 1fb9fed | 2013-02-01 23:18:04 -0800 | [diff] [blame] | 108 | uint32_t interestSize = item->payload ()->GetInterest ()->GetSerializedSize (); |
| 109 | |
| 110 | // can't use logging here |
| 111 | NS_LOG_DEBUG ("Number of entries: " << policy_container::size () |
| 112 | << ", space used: " << current_space_used_ |
| 113 | << ", name: " << item->payload ()->GetPrefix () |
| 114 | << ", interest size: " << interestSize); |
| 115 | |
| 116 | if (max_size_ != 0 && current_space_used_ + interestSize > max_size_) |
Alexander Afanasyev | 7456b70 | 2013-02-01 22:41:48 -0800 | [diff] [blame] | 117 | { |
Alexander Afanasyev | 1fb9fed | 2013-02-01 23:18:04 -0800 | [diff] [blame] | 118 | NS_LOG_DEBUG ("Rejecting PIT entry"); |
| 119 | |
Alexander Afanasyev | 7456b70 | 2013-02-01 22:41:48 -0800 | [diff] [blame] | 120 | // the current version just fails to add an element, but it also possible |
| 121 | // to remove the largest element (last element in multi_map policy container) |
| 122 | return false; |
| 123 | } |
| 124 | |
Alexander Afanasyev | 1fb9fed | 2013-02-01 23:18:04 -0800 | [diff] [blame] | 125 | get_size (item) = interestSize; |
| 126 | current_space_used_ += interestSize; |
Alexander Afanasyev | 7456b70 | 2013-02-01 22:41:48 -0800 | [diff] [blame] | 127 | |
| 128 | policy_container::insert (*item); |
| 129 | return true; |
| 130 | } |
| 131 | |
| 132 | inline void |
| 133 | lookup (typename parent_trie::iterator item) |
| 134 | { |
| 135 | // do nothing |
| 136 | } |
| 137 | |
| 138 | inline void |
| 139 | erase (typename parent_trie::iterator item) |
| 140 | { |
Alexander Afanasyev | 1fb9fed | 2013-02-01 23:18:04 -0800 | [diff] [blame] | 141 | NS_LOG_DEBUG ("Erasing entry with name: " << item->payload ()->GetPrefix ()); |
| 142 | |
Alexander Afanasyev | 7456b70 | 2013-02-01 22:41:48 -0800 | [diff] [blame] | 143 | current_space_used_ -= get_size (item); |
| 144 | policy_container::erase (policy_container::s_iterator_to (*item)); |
| 145 | } |
| 146 | |
| 147 | inline void |
| 148 | clear () |
| 149 | { |
| 150 | policy_container::clear (); |
| 151 | } |
| 152 | |
| 153 | inline void |
| 154 | set_max_size (size_t max_size) |
| 155 | { |
| 156 | max_size_ = max_size; |
| 157 | } |
| 158 | |
| 159 | inline size_t |
| 160 | get_max_size () const |
| 161 | { |
| 162 | return max_size_; |
| 163 | } |
| 164 | |
| 165 | inline uint32_t |
| 166 | get_current_space_used () const |
| 167 | { |
| 168 | return current_space_used_; |
| 169 | } |
| 170 | |
| 171 | private: |
| 172 | type () : base_(*((Base*)0)) { }; |
| 173 | |
| 174 | private: |
| 175 | Base &base_; |
| 176 | uint32_t max_size_; |
| 177 | uint32_t current_space_used_; |
| 178 | }; |
| 179 | }; |
| 180 | }; |
| 181 | |
| 182 | } // ndnSIM |
| 183 | } // ndn |
| 184 | } // ns3 |
| 185 | |
| 186 | #endif |