blob: 0050ac642bc1c982c6e51da21a766496c575a1fb [file] [log] [blame]
Alexander Afanasyev7456b702013-02-01 22:41:48 -08001/* -*- 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
27namespace ns3 {
28namespace ndn {
29namespace ndnSIM {
30
31/**
32 * @brief Traits for Least Recently Used replacement policy
33 */
34struct 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 {
108 if (max_size_ != 0 && current_space_used_ >= max_size_)
109 {
110 // the current version just fails to add an element, but it also possible
111 // to remove the largest element (last element in multi_map policy container)
112 return false;
113 }
114
115 get_size (item) = item->payload ()->GetInterest ()->GetSerializedSize ();
116 current_space_used_ += get_size (item);
117
118 policy_container::insert (*item);
119 return true;
120 }
121
122 inline void
123 lookup (typename parent_trie::iterator item)
124 {
125 // do nothing
126 }
127
128 inline void
129 erase (typename parent_trie::iterator item)
130 {
131 current_space_used_ -= get_size (item);
132 policy_container::erase (policy_container::s_iterator_to (*item));
133 }
134
135 inline void
136 clear ()
137 {
138 policy_container::clear ();
139 }
140
141 inline void
142 set_max_size (size_t max_size)
143 {
144 max_size_ = max_size;
145 }
146
147 inline size_t
148 get_max_size () const
149 {
150 return max_size_;
151 }
152
153 inline uint32_t
154 get_current_space_used () const
155 {
156 return current_space_used_;
157 }
158
159 private:
160 type () : base_(*((Base*)0)) { };
161
162 private:
163 Base &base_;
164 uint32_t max_size_;
165 uint32_t current_space_used_;
166 };
167 };
168};
169
170} // ndnSIM
171} // ndn
172} // ns3
173
174#endif