Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 1 | /* -*- 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" |
HangZhang | c85a23c | 2014-03-01 15:55:55 +0800 | [diff] [blame] | 8 | #include "name-tree.hpp" |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 9 | #include "pit-entry.hpp" |
HangZhang | c85a23c | 2014-03-01 15:55:55 +0800 | [diff] [blame] | 10 | #include "fib-entry.hpp" |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 11 | |
| 12 | namespace nfd { |
| 13 | |
| 14 | const time::Duration Measurements::s_defaultLifetime = time::seconds(4); |
| 15 | |
HangZhang | c85a23c | 2014-03-01 15:55:55 +0800 | [diff] [blame] | 16 | Measurements::Measurements(NameTree& nameTree) |
| 17 | : m_nameTree(nameTree) |
| 18 | , m_nItems(0) |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 19 | { |
| 20 | } |
| 21 | |
| 22 | Measurements::~Measurements() |
| 23 | { |
| 24 | } |
| 25 | |
Junxiao Shi | 4063184 | 2014-03-01 13:52:37 -0700 | [diff] [blame^] | 26 | static inline bool |
| 27 | predicate_NameTreeEntry_hasMeasurementsEntry(const name_tree::Entry& entry) |
| 28 | { |
| 29 | return static_cast<bool>(entry.getMeasurementsEntry()); |
| 30 | } |
| 31 | |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 32 | shared_ptr<measurements::Entry> |
| 33 | Measurements::get(const Name& name) |
| 34 | { |
HangZhang | c85a23c | 2014-03-01 15:55:55 +0800 | [diff] [blame] | 35 | shared_ptr<name_tree::Entry> nameTreeEntry = m_nameTree.lookup(name); |
| 36 | shared_ptr<measurements::Entry> entry = nameTreeEntry->getMeasurementsEntry(); |
| 37 | if (static_cast<bool>(entry)) |
| 38 | return entry; |
| 39 | entry = make_shared<measurements::Entry>(name); |
| 40 | nameTreeEntry->setMeasurementsEntry(entry); |
| 41 | m_nItems++; |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 42 | return entry; |
| 43 | } |
| 44 | |
| 45 | shared_ptr<measurements::Entry> |
| 46 | Measurements::get(const fib::Entry& fibEntry) |
| 47 | { |
| 48 | return this->get(fibEntry.getPrefix()); |
| 49 | } |
| 50 | |
| 51 | shared_ptr<measurements::Entry> |
| 52 | Measurements::get(const pit::Entry& pitEntry) |
| 53 | { |
| 54 | return this->get(pitEntry.getName()); |
| 55 | } |
| 56 | |
| 57 | shared_ptr<measurements::Entry> |
| 58 | Measurements::getParent(shared_ptr<measurements::Entry> child) |
| 59 | { |
HangZhang | c85a23c | 2014-03-01 15:55:55 +0800 | [diff] [blame] | 60 | BOOST_ASSERT(child); |
| 61 | |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 62 | if (child->getName().size() == 0) { |
| 63 | return shared_ptr<measurements::Entry>(); |
| 64 | } |
| 65 | |
| 66 | return this->get(child->getName().getPrefix(-1)); |
| 67 | } |
| 68 | |
HangZhang | c85a23c | 2014-03-01 15:55:55 +0800 | [diff] [blame] | 69 | shared_ptr<measurements::Entry> |
| 70 | Measurements::findLongestPrefixMatch(const Name& name) const |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 71 | { |
Junxiao Shi | 4063184 | 2014-03-01 13:52:37 -0700 | [diff] [blame^] | 72 | shared_ptr<name_tree::Entry> nameTreeEntry = |
| 73 | m_nameTree.findLongestPrefixMatch(name, &predicate_NameTreeEntry_hasMeasurementsEntry); |
| 74 | if (static_cast<bool>(nameTreeEntry)) { |
| 75 | return nameTreeEntry->getMeasurementsEntry(); |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 76 | } |
HangZhang | c85a23c | 2014-03-01 15:55:55 +0800 | [diff] [blame] | 77 | return shared_ptr<measurements::Entry>(); |
| 78 | } |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 79 | |
HangZhang | c85a23c | 2014-03-01 15:55:55 +0800 | [diff] [blame] | 80 | shared_ptr<measurements::Entry> |
| 81 | Measurements::findExactMatch(const Name& name) const |
| 82 | { |
| 83 | shared_ptr<name_tree::Entry> nameTreeEntry = m_nameTree.lookup(name); |
| 84 | if (static_cast<bool>(nameTreeEntry)) |
| 85 | return nameTreeEntry->getMeasurementsEntry(); |
| 86 | return shared_ptr<measurements::Entry>(); |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | void |
HangZhang | c85a23c | 2014-03-01 15:55:55 +0800 | [diff] [blame] | 90 | Measurements::extendLifetime(measurements::Entry& entry, const time::Duration lifetime) |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 91 | { |
HangZhang | c85a23c | 2014-03-01 15:55:55 +0800 | [diff] [blame] | 92 | shared_ptr<measurements::Entry> ret = this->findExactMatch(entry.getName()); |
| 93 | if (static_cast<bool>(ret)) |
| 94 | { |
| 95 | time::Point expiry = time::now() + lifetime; |
| 96 | if (ret->m_expiry >= expiry) // has longer lifetime, not extending |
| 97 | return; |
| 98 | scheduler::cancel(entry.m_cleanup); |
| 99 | entry.m_expiry = expiry; |
| 100 | entry.m_cleanup = scheduler::schedule(lifetime, |
| 101 | bind(&Measurements::cleanup, this, ret)); |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | void |
| 106 | Measurements::cleanup(shared_ptr<measurements::Entry> entry) |
| 107 | { |
| 108 | BOOST_ASSERT(entry); |
| 109 | |
| 110 | shared_ptr<name_tree::Entry> nameTreeEntry = m_nameTree.findExactMatch(entry->getName()); |
| 111 | if (static_cast<bool>(nameTreeEntry)) |
| 112 | { |
| 113 | nameTreeEntry->eraseMeasurementsEntry( |
| 114 | nameTreeEntry->getMeasurementsEntry()); |
| 115 | m_nItems--; |
| 116 | } |
| 117 | |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | } // namespace nfd |