blob: dcc40fed2654d6b19f509675b797fb60e2fb5efa [file] [log] [blame]
Junxiao Shi65d00722014-02-17 10:50:20 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (C) 2014 Named Data Networking Project
4 * See COPYING for copyright and distribution information.
5 */
6
7#include "measurements.hpp"
HangZhangc85a23c2014-03-01 15:55:55 +08008#include "name-tree.hpp"
Junxiao Shi65d00722014-02-17 10:50:20 -07009#include "pit-entry.hpp"
HangZhangc85a23c2014-03-01 15:55:55 +080010#include "fib-entry.hpp"
Junxiao Shi65d00722014-02-17 10:50:20 -070011
12namespace nfd {
13
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -070014const time::nanoseconds Measurements::s_defaultLifetime = time::seconds(4);
Junxiao Shi65d00722014-02-17 10:50:20 -070015
HangZhangc85a23c2014-03-01 15:55:55 +080016Measurements::Measurements(NameTree& nameTree)
17 : m_nameTree(nameTree)
18 , m_nItems(0)
Junxiao Shi65d00722014-02-17 10:50:20 -070019{
20}
21
22Measurements::~Measurements()
23{
24}
25
Junxiao Shi40631842014-03-01 13:52:37 -070026static inline bool
27predicate_NameTreeEntry_hasMeasurementsEntry(const name_tree::Entry& entry)
28{
29 return static_cast<bool>(entry.getMeasurementsEntry());
30}
31
Junxiao Shi65d00722014-02-17 10:50:20 -070032shared_ptr<measurements::Entry>
HangZhangcb4fc832014-03-11 16:57:11 +080033Measurements::get(shared_ptr<name_tree::Entry> nameTreeEntry)
Junxiao Shi65d00722014-02-17 10:50:20 -070034{
HangZhangc85a23c2014-03-01 15:55:55 +080035 shared_ptr<measurements::Entry> entry = nameTreeEntry->getMeasurementsEntry();
36 if (static_cast<bool>(entry))
37 return entry;
HangZhangcb4fc832014-03-11 16:57:11 +080038 entry = make_shared<measurements::Entry>(nameTreeEntry->getPrefix());
HangZhangc85a23c2014-03-01 15:55:55 +080039 nameTreeEntry->setMeasurementsEntry(entry);
40 m_nItems++;
Junxiao Shi65d00722014-02-17 10:50:20 -070041 return entry;
42}
43
44shared_ptr<measurements::Entry>
HangZhangcb4fc832014-03-11 16:57:11 +080045Measurements::get(const Name& name)
46{
47 shared_ptr<name_tree::Entry> nameTreeEntry = m_nameTree.lookup(name);
48 return get(nameTreeEntry);
49}
50
51shared_ptr<measurements::Entry>
Junxiao Shi65d00722014-02-17 10:50:20 -070052Measurements::get(const fib::Entry& fibEntry)
53{
HangZhangcb4fc832014-03-11 16:57:11 +080054 shared_ptr<name_tree::Entry> nameTreeEntry = m_nameTree.get(fibEntry);
55
56 BOOST_ASSERT(static_cast<bool>(nameTreeEntry));
57
58 return get(nameTreeEntry);
Junxiao Shi65d00722014-02-17 10:50:20 -070059}
60
61shared_ptr<measurements::Entry>
62Measurements::get(const pit::Entry& pitEntry)
63{
HangZhangcb4fc832014-03-11 16:57:11 +080064 shared_ptr<name_tree::Entry> nameTreeEntry = m_nameTree.get(pitEntry);
65
66 BOOST_ASSERT(static_cast<bool>(nameTreeEntry));
67
68 return get(nameTreeEntry);
Junxiao Shi65d00722014-02-17 10:50:20 -070069}
70
71shared_ptr<measurements::Entry>
72Measurements::getParent(shared_ptr<measurements::Entry> child)
73{
HangZhangc85a23c2014-03-01 15:55:55 +080074 BOOST_ASSERT(child);
75
Junxiao Shi65d00722014-02-17 10:50:20 -070076 if (child->getName().size() == 0) {
77 return shared_ptr<measurements::Entry>();
78 }
79
80 return this->get(child->getName().getPrefix(-1));
81}
82
HangZhangc85a23c2014-03-01 15:55:55 +080083shared_ptr<measurements::Entry>
84Measurements::findLongestPrefixMatch(const Name& name) const
Junxiao Shi65d00722014-02-17 10:50:20 -070085{
Junxiao Shi40631842014-03-01 13:52:37 -070086 shared_ptr<name_tree::Entry> nameTreeEntry =
87 m_nameTree.findLongestPrefixMatch(name, &predicate_NameTreeEntry_hasMeasurementsEntry);
88 if (static_cast<bool>(nameTreeEntry)) {
89 return nameTreeEntry->getMeasurementsEntry();
Junxiao Shi65d00722014-02-17 10:50:20 -070090 }
HangZhangc85a23c2014-03-01 15:55:55 +080091 return shared_ptr<measurements::Entry>();
92}
Junxiao Shi65d00722014-02-17 10:50:20 -070093
HangZhangc85a23c2014-03-01 15:55:55 +080094shared_ptr<measurements::Entry>
95Measurements::findExactMatch(const Name& name) const
96{
97 shared_ptr<name_tree::Entry> nameTreeEntry = m_nameTree.lookup(name);
98 if (static_cast<bool>(nameTreeEntry))
99 return nameTreeEntry->getMeasurementsEntry();
100 return shared_ptr<measurements::Entry>();
Junxiao Shi65d00722014-02-17 10:50:20 -0700101}
102
103void
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700104Measurements::extendLifetime(measurements::Entry& entry, const time::nanoseconds& lifetime)
Junxiao Shi65d00722014-02-17 10:50:20 -0700105{
HangZhangc85a23c2014-03-01 15:55:55 +0800106 shared_ptr<measurements::Entry> ret = this->findExactMatch(entry.getName());
107 if (static_cast<bool>(ret))
108 {
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700109 time::steady_clock::TimePoint expiry = time::steady_clock::now() + lifetime;
HangZhangc85a23c2014-03-01 15:55:55 +0800110 if (ret->m_expiry >= expiry) // has longer lifetime, not extending
111 return;
112 scheduler::cancel(entry.m_cleanup);
113 entry.m_expiry = expiry;
114 entry.m_cleanup = scheduler::schedule(lifetime,
115 bind(&Measurements::cleanup, this, ret));
116 }
117}
118
119void
120Measurements::cleanup(shared_ptr<measurements::Entry> entry)
121{
122 BOOST_ASSERT(entry);
123
124 shared_ptr<name_tree::Entry> nameTreeEntry = m_nameTree.findExactMatch(entry->getName());
125 if (static_cast<bool>(nameTreeEntry))
126 {
HangZhangcb4fc832014-03-11 16:57:11 +0800127 nameTreeEntry->setMeasurementsEntry(shared_ptr<measurements::Entry>());
HangZhangc85a23c2014-03-01 15:55:55 +0800128 m_nItems--;
129 }
HangZhangcb4fc832014-03-11 16:57:11 +0800130
Junxiao Shi65d00722014-02-17 10:50:20 -0700131}
132
133} // namespace nfd