blob: 87a5891df61fca6d0125703b504e1870bf1c1bd9 [file] [log] [blame]
Alexander Afanasyev8566f452012-12-10 15:21:51 -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 LIFETIME_STATS_POLICY_H_
22#define LIFETIME_STATS_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 lifetime stats policy
37 */
38struct lifetime_stats_policy_traits
39{
40 /// @brief Name that can be used to identify the policy (for NS-3 object model and logging)
41 static std::string GetName () { return "LifetimeStats"; }
42
43 struct policy_hook_type : public boost::intrusive::list_member_hook<> { Time timeWhenAdded; };
44
45 template<class Container>
46 struct container_hook
47 {
48 typedef boost::intrusive::member_hook< Container,
49 policy_hook_type,
50 &Container::policy_hook_ > type;
51 };
52
53 template<class Base,
54 class Container,
55 class Hook>
56 struct policy
57 {
58 typedef typename boost::intrusive::list< Container, Hook > policy_container;
59
60 static Time& get_time (typename Container::iterator item)
61 {
62 return static_cast<typename policy_container::value_traits::hook_type*>
63 (policy_container::value_traits::to_node_ptr(*item))->timeWhenAdded;
64 }
65
66 static const Time& get_time (typename Container::const_iterator item)
67 {
68 return static_cast<const typename policy_container::value_traits::hook_type*>
69 (policy_container::value_traits::to_node_ptr(*item))->timeWhenAdded;
70 }
71
72 class type : public policy_container
73 {
74 public:
75 typedef Container parent_trie;
76
77 type (Base &base)
78 : base_ (base)
79 , max_size_ (100)
80 , m_willRemoveEntry (0)
81 {
82 }
83
84 inline void
85 update (typename parent_trie::iterator item)
86 {
87 // do nothing. it's random policy
88 }
89
90 inline bool
91 insert (typename parent_trie::iterator item)
92 {
93 get_time (item) = Simulator::Now ();
94
95 policy_container::push_back (*item);
96 return true;
97 }
98
99 inline void
100 lookup (typename parent_trie::iterator item)
101 {
102 // do nothing. it's random policy
103 }
104
105 inline void
106 erase (typename parent_trie::iterator item)
107 {
108 Time lifetime = Simulator::Now () - get_time (item);
109
110 if (m_willRemoveEntry != 0)
111 {
112 (*m_willRemoveEntry) (item->payload (), lifetime);
113 }
114
115 policy_container::erase (policy_container::s_iterator_to (*item));
116 }
117
118 inline void
119 clear ()
120 {
121 policy_container::clear ();
122 }
123
124 inline void
125 set_max_size (size_t max_size)
126 {
127 max_size_ = max_size;
128 }
129
130 inline size_t
131 get_max_size () const
132 {
133 return max_size_;
134 }
135
136 void
137 set_traced_callback (TracedCallback< typename parent_trie::payload_traits::const_base_type, Time > *callback)
138 {
139 std::cout << Simulator::GetContext () << " " << callback << std::endl;
140 m_willRemoveEntry = callback;
141 }
142
143 private:
144 type () : base_(*((Base*)0)) { };
145
146 private:
147 Base &base_;
148 size_t max_size_;
149
150 TracedCallback< typename parent_trie::payload_traits::const_base_type, Time > *m_willRemoveEntry;
151 };
152 };
153};
154
155} // ndnSIM
156} // ndn
157} // ns3
158
159#endif // LIFETIME_STATS_POLICY_H