blob: 8963bd7d7641b8fa9de844615a5009505ee6f82f [file] [log] [blame]
Alexander Afanasyev60a7b622014-12-20 17:04:07 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2011-2015 Regents of the University of California.
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -08004 *
Alexander Afanasyev60a7b622014-12-20 17:04:07 -08005 * This file is part of ndnSIM. See AUTHORS for complete list of ndnSIM authors and
6 * contributors.
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -08007 *
Alexander Afanasyev60a7b622014-12-20 17:04:07 -08008 * ndnSIM is free software: you can redistribute it and/or modify it under the terms
9 * of the GNU General Public License as published by the Free Software Foundation,
10 * either version 3 of the License, or (at your option) any later version.
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -080011 *
Alexander Afanasyev60a7b622014-12-20 17:04:07 -080012 * ndnSIM is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
13 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -080015 *
Alexander Afanasyev60a7b622014-12-20 17:04:07 -080016 * You should have received a copy of the GNU General Public License along with
17 * ndnSIM, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
18 **/
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -080019
20#ifndef FRESHNESS_POLICY_H_
21#define FRESHNESS_POLICY_H_
22
Spyridon Mastorakis53e922f2014-10-17 17:29:26 -070023#include "ns3/ndnSIM/model/ndn-common.hpp"
24
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -080025#include <boost/intrusive/options.hpp>
26#include <boost/intrusive/list.hpp>
27
28#include <ns3/nstime.h>
29#include <ns3/simulator.h>
30#include <ns3/traced-callback.h>
31
32namespace ns3 {
33namespace ndn {
34namespace ndnSIM {
35
36/**
37 * @brief Traits for freshness policy
38 */
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080039struct freshness_policy_traits {
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -080040 /// @brief Name that can be used to identify the policy (for NS-3 object model and logging)
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080041 static std::string
42 GetName()
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -080043 {
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080044 return "Freshness";
45 }
46
47 struct policy_hook_type : public boost::intrusive::set_member_hook<> {
48 Time timeWhenShouldExpire;
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -080049 };
50
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080051 template<class Container>
52 struct container_hook {
53 typedef boost::intrusive::member_hook<Container, policy_hook_type, &Container::policy_hook_>
54 type;
55 };
56
57 template<class Base, class Container, class Hook>
58 struct policy {
59 static Time&
60 get_freshness(typename Container::iterator item)
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -080061 {
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080062 return static_cast<typename policy_container::value_traits::hook_type*>(
63 policy_container::value_traits::to_node_ptr(*item))->timeWhenShouldExpire;
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -080064 }
Alexander Afanasyev41824bd2013-01-23 23:57:59 -080065
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080066 static const Time&
67 get_freshness(typename Container::const_iterator item)
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -080068 {
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080069 return static_cast<const typename policy_container::value_traits::hook_type*>(
70 policy_container::value_traits::to_node_ptr(*item))->timeWhenShouldExpire;
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -080071 }
72
73 template<class Key>
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080074 struct MemberHookLess {
75 bool
76 operator()(const Key& a, const Key& b) const
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -080077 {
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080078 return get_freshness(&a) < get_freshness(&b);
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -080079 }
80 };
81
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080082 typedef boost::intrusive::multiset<Container,
83 boost::intrusive::compare<MemberHookLess<Container>>,
84 Hook> policy_container;
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -080085
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080086 class type : public policy_container {
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -080087 public:
Alexander Afanasyev69786112012-12-13 11:53:36 -080088 typedef policy policy_base; // to get access to get_freshness methods from outside
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -080089 typedef Container parent_trie;
90
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080091 type(Base& base)
92 : base_(base)
93 , max_size_(100)
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -080094 {
95 }
96
97 inline void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080098 update(typename parent_trie::iterator item)
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -080099 {
Alexander Afanasyev41824bd2013-01-23 23:57:59 -0800100 // do nothing
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -0800101 }
Alexander Afanasyev41824bd2013-01-23 23:57:59 -0800102
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -0800103 inline bool
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800104 insert(typename parent_trie::iterator item)
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -0800105 {
Spyridon Mastorakisde1f7732014-12-05 22:43:34 -0800106 time::milliseconds freshness = item->payload()->GetData()->getFreshnessPeriod();
107 if (freshness > time::milliseconds::zero()) {
108 get_freshness(item) = Simulator::Now() + MilliSeconds(freshness.count());
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -0800109
Spyridon Mastorakisde1f7732014-12-05 22:43:34 -0800110 // push item only if freshness is non zero. otherwise, this payload is not
111 // controlled by the policy.
112 // Note that .size() on this policy would return only the number of items with
113 // non-infinite freshness policy
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800114 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 {
Spyridon Mastorakisde1f7732014-12-05 22:43:34 -0800129 time::milliseconds freshness = item->payload()->GetData()->getFreshnessPeriod();
130 if (freshness > time::milliseconds::zero()) {
131 // erase only if freshness is positive (otherwise an item is not in the policy)
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800132 policy_container::erase(policy_container::s_iterator_to(*item));
133 }
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -0800134 }
135
136 inline void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800137 clear()
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -0800138 {
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800139 policy_container::clear();
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -0800140 }
141
142 inline void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800143 set_max_size(size_t max_size)
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -0800144 {
145 max_size_ = max_size;
146 }
147
148 inline size_t
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800149 get_max_size() const
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -0800150 {
151 return max_size_;
152 }
153
154 private:
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800155 type()
156 : base_(*((Base*)0)){};
Alexander Afanasyev41824bd2013-01-23 23:57:59 -0800157
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -0800158 private:
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800159 Base& base_;
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -0800160 size_t max_size_;
161 };
162 };
163};
164
165} // ndnSIM
166} // ndn
167} // ns3
168
169#endif // LIFETIME_STATS_POLICY_H