NS-3 based Named Data Networking (NDN) simulator
ndnSIM: NDN, CCN, CCNx, content centric networks
API Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Groups Pages
serialized-size-policy.h
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 
35 {
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>
43  {
44  typedef boost::intrusive::member_hook< Container,
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>
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  if (item->payload ()->GetInterest ()->GetWire ())
100  {
101  policy::get_size (item) = item->payload ()->GetInterest ()->GetWire ()->GetSize ();
102  }
103  else
104  {
105  policy::get_size (item) = 0;
106  }
107  current_space_used_ += get_size (item); // this operation can violate policy constraint, so in some case
108  // it may be necessary to remove some other element
109  policy_container::insert (*item);
110  }
111 
112  inline bool
113  insert (typename parent_trie::iterator item)
114  {
115  uint32_t interestSize = 0;
116  if (item->payload ()->GetInterest ()->GetWire ())
117  {
118  interestSize = item->payload ()->GetInterest ()->GetWire ()->GetSize ();
119  }
120 
121  // can't use logging here
122  NS_LOG_DEBUG ("Number of entries: " << policy_container::size ()
123  << ", space used: " << current_space_used_
124  << ", name: " << item->payload ()->GetPrefix ()
125  << ", interest size: " << interestSize);
126 
127  if (max_size_ != 0 && current_space_used_ + interestSize > max_size_)
128  {
129  NS_LOG_DEBUG ("Rejecting PIT entry");
130 
131  // the current version just fails to add an element, but it also possible
132  // to remove the largest element (last element in multi_map policy container)
133  return false;
134  }
135 
136  policy::get_size (item) = interestSize;
137  current_space_used_ += interestSize;
138 
139  policy_container::insert (*item);
140  return true;
141  }
142 
143  inline void
144  lookup (typename parent_trie::iterator item)
145  {
146  // do nothing
147  }
148 
149  inline void
150  erase (typename parent_trie::iterator item)
151  {
152  NS_LOG_DEBUG ("Erasing entry with name: " << item->payload ()->GetPrefix ());
153 
154  current_space_used_ -= policy::get_size (item);
155  policy_container::erase (policy_container::s_iterator_to (*item));
156  }
157 
158  inline void
159  clear ()
160  {
161  policy_container::clear ();
162  }
163 
164  inline void
165  set_max_size (size_t max_size)
166  {
167  max_size_ = max_size;
168  }
169 
170  inline size_t
171  get_max_size () const
172  {
173  return max_size_;
174  }
175 
176  inline uint32_t
177  get_current_space_used () const
178  {
179  return current_space_used_;
180  }
181 
182  private:
183  type () : base_(*((Base*)0)) { };
184 
185  private:
186  Base &base_;
187  uint32_t max_size_;
188  uint32_t current_space_used_;
189  };
190  };
191 };
192 
193 } // ndnSIM
194 } // ndn
195 } // ns3
196 
197 #endif
static std::string GetName()
Name that can be used to identify the policy (for NS-3 object model and logging)
Traits for Least Recently Used replacement policy.