blob: 3b84ac2fd1c689343cfcf068e7242f812098c6c5 [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 Afanasyev8566f452012-12-10 15:21:51 -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 Afanasyev8566f452012-12-10 15:21:51 -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 Afanasyev8566f452012-12-10 15:21:51 -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 Afanasyev8566f452012-12-10 15:21:51 -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 Afanasyev8566f452012-12-10 15:21:51 -080019
20#ifndef NDN_CONTENT_STORE_WITH_STATS_H_
21#define NDN_CONTENT_STORE_WITH_STATS_H_
22
Spyridon Mastorakis53e922f2014-10-17 17:29:26 -070023#include "ns3/ndnSIM/model/ndn-common.hpp"
Alexander Afanasyev8566f452012-12-10 15:21:51 -080024
Alexander Afanasyev0c395372014-12-20 15:54:02 -080025#include "content-store-impl.hpp"
26
27#include "../../utils/trie/multi-policy.hpp"
28#include "custom-policies/lifetime-stats-policy.hpp"
Alexander Afanasyev8566f452012-12-10 15:21:51 -080029
30namespace ns3 {
31namespace ndn {
32namespace cs {
33
Alexander Afanasyev79206512013-07-27 16:49:12 -070034/**
35 * @ingroup ndn-cs
36 * @brief Special content store realization that provides ability to track stats of CS operations
37 */
Alexander Afanasyev8566f452012-12-10 15:21:51 -080038template<class Policy>
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080039class ContentStoreWithStats
40 : public ContentStoreImpl<ndnSIM::
41 multi_policy_traits<boost::mpl::
42 vector2<Policy,
43 ndnSIM::
44 lifetime_stats_policy_traits>>> {
Alexander Afanasyev8566f452012-12-10 15:21:51 -080045public:
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080046 typedef ContentStoreImpl<ndnSIM::
47 multi_policy_traits<boost::mpl::
48 vector2<Policy,
49 ndnSIM::lifetime_stats_policy_traits>>>
50 super;
Alexander Afanasyev8566f452012-12-10 15:21:51 -080051
Alexander Afanasyevbe82da52013-04-09 05:43:14 -070052 typedef typename super::policy_container::template index<1>::type lifetime_stats_container;
53
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080054 ContentStoreWithStats()
Alexander Afanasyev8566f452012-12-10 15:21:51 -080055 {
56 // connect traceback to the policy
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080057 super::getPolicy().template get<1>().set_traced_callback(&m_willRemoveEntry);
Alexander Afanasyev8566f452012-12-10 15:21:51 -080058 }
Alexander Afanasyev7456b702013-02-01 22:41:48 -080059
Alexander Afanasyev8566f452012-12-10 15:21:51 -080060 static TypeId
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080061 GetTypeId();
Alexander Afanasyev7456b702013-02-01 22:41:48 -080062
Alexander Afanasyevbe82da52013-04-09 05:43:14 -070063 virtual inline void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080064 Print(std::ostream& os) const;
Alexander Afanasyevbe82da52013-04-09 05:43:14 -070065
Alexander Afanasyevd6453cd2015-08-20 21:45:36 -070066public:
67 typedef void (*RemoveCsEntryCallback)(Ptr<const Entry>, Time);
68
Alexander Afanasyev8566f452012-12-10 15:21:51 -080069private:
70 static LogComponent g_log; ///< @brief Logging variable
71
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080072 /// @brief trace of for entry removal: first parameter is pointer to the CS entry, second is how
73 /// long entry was in the cache
74 TracedCallback<Ptr<const Entry>, Time> m_willRemoveEntry;
Alexander Afanasyev8566f452012-12-10 15:21:51 -080075};
76
77//////////////////////////////////////////
78////////// Implementation ////////////////
79//////////////////////////////////////////
80
Alexander Afanasyev8566f452012-12-10 15:21:51 -080081template<class Policy>
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080082LogComponent ContentStoreWithStats<Policy>::g_log = LogComponent(("ndn.cs.Stats."
Alexander Afanasyevd6453cd2015-08-20 21:45:36 -070083 + Policy::GetName()).c_str(), __FILE__);
Alexander Afanasyev8566f452012-12-10 15:21:51 -080084
85template<class Policy>
86TypeId
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080087ContentStoreWithStats<Policy>::GetTypeId()
Alexander Afanasyev8566f452012-12-10 15:21:51 -080088{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080089 static TypeId tid =
90 TypeId(("ns3::ndn::cs::Stats::" + Policy::GetName()).c_str())
91 .SetGroupName("Ndn")
92 .SetParent<super>()
93 .template AddConstructor<ContentStoreWithStats<Policy>>()
Alexander Afanasyev8566f452012-12-10 15:21:51 -080094
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080095 .AddTraceSource("WillRemoveEntry",
96 "Trace called just before content store entry will be removed",
Alexander Afanasyevd6453cd2015-08-20 21:45:36 -070097 MakeTraceSourceAccessor(&ContentStoreWithStats<Policy>::m_willRemoveEntry),
98 "ns3::ndn::cs::ContentStoreWithStats::RemoveCsEntryCallback")
Alexander Afanasyev8566f452012-12-10 15:21:51 -080099
100 // trace stuff here
101 ;
102
103 return tid;
104}
105
Alexander Afanasyevbe82da52013-04-09 05:43:14 -0700106template<class Policy>
107void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800108ContentStoreWithStats<Policy>::Print(std::ostream& os) const
Alexander Afanasyevbe82da52013-04-09 05:43:14 -0700109{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800110 // const freshness_policy_container &freshness = this->getPolicy ().template
111 // get<freshness_policy_container> ();
Alexander Afanasyevbe82da52013-04-09 05:43:14 -0700112
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800113 for (typename super::policy_container::const_iterator item = this->getPolicy().begin();
114 item != this->getPolicy().end(); item++) {
115 Time alive = lifetime_stats_container::policy_base::get_time(&(*item)) - Simulator::Now();
116 os << item->payload()->GetName() << "(alive: " << alive.ToDouble(Time::S) << "s)" << std::endl;
117 }
Alexander Afanasyevbe82da52013-04-09 05:43:14 -0700118}
119
Alexander Afanasyev8566f452012-12-10 15:21:51 -0800120} // namespace cs
121} // namespace ndn
122} // namespace ns3
123
124#endif // NDN_CONTENT_STORE_IMPL_H_