NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.3: NDN, CCN, CCNx, content centric networks
API Documentation
freshness-policy.hpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
20 #ifndef FRESHNESS_POLICY_H_
21 #define FRESHNESS_POLICY_H_
22 
24 
25 #include "ns3/ndnSIM/model/ndn-common.hpp"
26 
27 #include <boost/intrusive/options.hpp>
28 #include <boost/intrusive/list.hpp>
29 
30 #include <ns3/nstime.h>
31 #include <ns3/simulator.h>
32 #include <ns3/traced-callback.h>
33 
34 namespace ns3 {
35 namespace ndn {
36 namespace ndnSIM {
37 
41 struct freshness_policy_traits {
43  static std::string
44  GetName()
45  {
46  return "Freshness";
47  }
48 
49  struct policy_hook_type : public boost::intrusive::set_member_hook<> {
50  Time timeWhenShouldExpire;
51  };
52 
53  template<class Container>
54  struct container_hook {
55  typedef boost::intrusive::member_hook<Container, policy_hook_type, &Container::policy_hook_>
56  type;
57  };
58 
59  template<class Base, class Container, class Hook>
60  struct policy {
61  static Time&
62  get_freshness(typename Container::iterator item)
63  {
64  return static_cast<typename policy_container::value_traits::hook_type*>(
65  policy_container::value_traits::to_node_ptr(*item))->timeWhenShouldExpire;
66  }
67 
68  static const Time&
69  get_freshness(typename Container::const_iterator item)
70  {
71  return static_cast<const typename policy_container::value_traits::hook_type*>(
72  policy_container::value_traits::to_node_ptr(*item))->timeWhenShouldExpire;
73  }
74 
75  template<class Key>
76  struct MemberHookLess {
77  bool
78  operator()(const Key& a, const Key& b) const
79  {
80  return get_freshness(&a) < get_freshness(&b);
81  }
82  };
83 
84  typedef boost::intrusive::multiset<Container,
85  boost::intrusive::compare<MemberHookLess<Container>>,
86  Hook> policy_container;
87 
88  class type : public policy_container {
89  public:
90  typedef policy policy_base; // to get access to get_freshness methods from outside
91  typedef Container parent_trie;
92 
93  type(Base& base)
94  : base_(base)
95  , max_size_(100)
96  {
97  }
98 
99  inline void
100  update(typename parent_trie::iterator item)
101  {
102  // do nothing
103  }
104 
105  inline bool
106  insert(typename parent_trie::iterator item)
107  {
108  time::milliseconds freshness = item->payload()->GetData()->getFreshnessPeriod();
109  if (freshness > time::milliseconds::zero()) {
110  get_freshness(item) = Simulator::Now() + MilliSeconds(freshness.count());
111 
112  // push item only if freshness is non zero. otherwise, this payload is not
113  // controlled by the policy.
114  // Note that .size() on this policy would return only the number of items with
115  // non-infinite freshness policy
116  policy_container::insert(*item);
117  }
118 
119  return true;
120  }
121 
122  inline void
123  lookup(typename parent_trie::iterator item)
124  {
125  // do nothing. it's random policy
126  }
127 
128  inline void
129  erase(typename parent_trie::iterator item)
130  {
131  time::milliseconds freshness = item->payload()->GetData()->getFreshnessPeriod();
132  if (freshness > time::milliseconds::zero()) {
133  // erase only if freshness is positive (otherwise an item is not in the policy)
134  policy_container::erase(policy_container::s_iterator_to(*item));
135  }
136  }
137 
138  inline void
139  clear()
140  {
141  policy_container::clear();
142  }
143 
144  inline void
145  set_max_size(size_t max_size)
146  {
147  max_size_ = max_size;
148  }
149 
150  inline size_t
151  get_max_size() const
152  {
153  return max_size_;
154  }
155 
156  private:
157  type()
158  : base_(*((Base*)0)){};
159 
160  private:
161  Base& base_;
162  size_t max_size_;
163  };
164  };
165 };
166 
167 } // ndnSIM
168 } // ndn
169 } // ns3
170 
172 
173 #endif // LIFETIME_STATS_POLICY_H
Copyright (c) 2011-2015 Regents of the University of California.
boost::posix_time::time_duration milliseconds(long duration)
Definition: asio.hpp:117
Table::const_iterator iterator
Definition: cs-internal.hpp:41
Copyright (c) 2011-2015 Regents of the University of California.