blob: 999c61db618e4d7e6f73df120b208dcdd5e6613d [file] [log] [blame]
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -08001/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
2/*
3 * Copyright (c) 2012 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 FRESHNESS_POLICY_H_
22#define FRESHNESS_POLICY_H_
23
24#include <boost/intrusive/options.hpp>
25#include <boost/intrusive/list.hpp>
26
27#include <ns3/nstime.h>
28#include <ns3/simulator.h>
29#include <ns3/traced-callback.h>
30
31namespace ns3 {
32namespace ndn {
33namespace ndnSIM {
34
35/**
36 * @brief Traits for freshness policy
37 */
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080038struct freshness_policy_traits {
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -080039 /// @brief Name that can be used to identify the policy (for NS-3 object model and logging)
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080040 static std::string
41 GetName()
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -080042 {
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080043 return "Freshness";
44 }
45
46 struct policy_hook_type : public boost::intrusive::set_member_hook<> {
47 Time timeWhenShouldExpire;
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -080048 };
49
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080050 template<class Container>
51 struct container_hook {
52 typedef boost::intrusive::member_hook<Container, policy_hook_type, &Container::policy_hook_>
53 type;
54 };
55
56 template<class Base, class Container, class Hook>
57 struct policy {
58 static Time&
59 get_freshness(typename Container::iterator item)
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -080060 {
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080061 return static_cast<typename policy_container::value_traits::hook_type*>(
62 policy_container::value_traits::to_node_ptr(*item))->timeWhenShouldExpire;
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -080063 }
Alexander Afanasyev41824bd2013-01-23 23:57:59 -080064
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080065 static const Time&
66 get_freshness(typename Container::const_iterator item)
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -080067 {
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080068 return static_cast<const typename policy_container::value_traits::hook_type*>(
69 policy_container::value_traits::to_node_ptr(*item))->timeWhenShouldExpire;
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -080070 }
71
72 template<class Key>
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080073 struct MemberHookLess {
74 bool
75 operator()(const Key& a, const Key& b) const
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -080076 {
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080077 return get_freshness(&a) < get_freshness(&b);
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -080078 }
79 };
80
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080081 typedef boost::intrusive::multiset<Container,
82 boost::intrusive::compare<MemberHookLess<Container>>,
83 Hook> policy_container;
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -080084
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080085 class type : public policy_container {
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -080086 public:
Alexander Afanasyev69786112012-12-13 11:53:36 -080087 typedef policy policy_base; // to get access to get_freshness methods from outside
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -080088 typedef Container parent_trie;
89
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080090 type(Base& base)
91 : base_(base)
92 , max_size_(100)
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -080093 {
94 }
95
96 inline void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080097 update(typename parent_trie::iterator item)
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -080098 {
Alexander Afanasyev41824bd2013-01-23 23:57:59 -080099 // do nothing
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -0800100 }
Alexander Afanasyev41824bd2013-01-23 23:57:59 -0800101
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -0800102 inline bool
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800103 insert(typename parent_trie::iterator item)
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -0800104 {
105 // get_time (item) = Simulator::Now ();
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800106 Time freshness = item->payload()->GetData()->GetFreshness();
107 if (!freshness.IsZero()) {
108 get_freshness(item) = Simulator::Now() + freshness;
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -0800109
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800110 // push item only if freshness is non zero. otherwise, this payload is not controlled by
111 // the policy
112 // note that .size() on this policy would return only number of items with non-infinite
113 // freshness policy
114 policy_container::insert(*item);
115 }
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -0800116
117 return true;
118 }
Alexander Afanasyev41824bd2013-01-23 23:57:59 -0800119
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -0800120 inline void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800121 lookup(typename parent_trie::iterator item)
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -0800122 {
123 // do nothing. it's random policy
124 }
Alexander Afanasyev41824bd2013-01-23 23:57:59 -0800125
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -0800126 inline void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800127 erase(typename parent_trie::iterator item)
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -0800128 {
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800129 if (!item->payload()->GetData()->GetFreshness().IsZero()) {
130 // erase only if freshness is non zero (otherwise an item is not in the policy
131 policy_container::erase(policy_container::s_iterator_to(*item));
132 }
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -0800133 }
134
135 inline void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800136 clear()
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -0800137 {
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800138 policy_container::clear();
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -0800139 }
140
141 inline void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800142 set_max_size(size_t max_size)
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -0800143 {
144 max_size_ = max_size;
145 }
146
147 inline size_t
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800148 get_max_size() const
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -0800149 {
150 return max_size_;
151 }
152
153 private:
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800154 type()
155 : base_(*((Base*)0)){};
Alexander Afanasyev41824bd2013-01-23 23:57:59 -0800156
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -0800157 private:
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800158 Base& base_;
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -0800159 size_t max_size_;
160 };
161 };
162};
163
164} // ndnSIM
165} // ndn
166} // ns3
167
168#endif // LIFETIME_STATS_POLICY_H