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> |
HangZhang | cb4fc83 | 2014-03-11 16:57:11 +0800 | [diff] [blame] | 33 | Measurements::get(shared_ptr<name_tree::Entry> nameTreeEntry) |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 34 | { |
HangZhang | c85a23c | 2014-03-01 15:55:55 +0800 | [diff] [blame] | 35 | shared_ptr<measurements::Entry> entry = nameTreeEntry->getMeasurementsEntry(); |
| 36 | if (static_cast<bool>(entry)) |
| 37 | return entry; |
HangZhang | cb4fc83 | 2014-03-11 16:57:11 +0800 | [diff] [blame] | 38 | entry = make_shared<measurements::Entry>(nameTreeEntry->getPrefix()); |
HangZhang | c85a23c | 2014-03-01 15:55:55 +0800 | [diff] [blame] | 39 | nameTreeEntry->setMeasurementsEntry(entry); |
| 40 | m_nItems++; |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 41 | return entry; |
| 42 | } |
| 43 | |
| 44 | shared_ptr<measurements::Entry> |
HangZhang | cb4fc83 | 2014-03-11 16:57:11 +0800 | [diff] [blame] | 45 | Measurements::get(const Name& name) |
| 46 | { |
| 47 | shared_ptr<name_tree::Entry> nameTreeEntry = m_nameTree.lookup(name); |
| 48 | return get(nameTreeEntry); |
| 49 | } |
| 50 | |
| 51 | shared_ptr<measurements::Entry> |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 52 | Measurements::get(const fib::Entry& fibEntry) |
| 53 | { |
HangZhang | cb4fc83 | 2014-03-11 16:57:11 +0800 | [diff] [blame] | 54 | shared_ptr<name_tree::Entry> nameTreeEntry = m_nameTree.get(fibEntry); |
| 55 | |
| 56 | BOOST_ASSERT(static_cast<bool>(nameTreeEntry)); |
| 57 | |
| 58 | return get(nameTreeEntry); |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | shared_ptr<measurements::Entry> |
| 62 | Measurements::get(const pit::Entry& pitEntry) |
| 63 | { |
HangZhang | cb4fc83 | 2014-03-11 16:57:11 +0800 | [diff] [blame] | 64 | shared_ptr<name_tree::Entry> nameTreeEntry = m_nameTree.get(pitEntry); |
| 65 | |
| 66 | BOOST_ASSERT(static_cast<bool>(nameTreeEntry)); |
| 67 | |
| 68 | return get(nameTreeEntry); |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | shared_ptr<measurements::Entry> |
| 72 | Measurements::getParent(shared_ptr<measurements::Entry> child) |
| 73 | { |
HangZhang | c85a23c | 2014-03-01 15:55:55 +0800 | [diff] [blame] | 74 | BOOST_ASSERT(child); |
| 75 | |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 76 | if (child->getName().size() == 0) { |
| 77 | return shared_ptr<measurements::Entry>(); |
| 78 | } |
| 79 | |
| 80 | return this->get(child->getName().getPrefix(-1)); |
| 81 | } |
| 82 | |
HangZhang | c85a23c | 2014-03-01 15:55:55 +0800 | [diff] [blame] | 83 | shared_ptr<measurements::Entry> |
| 84 | Measurements::findLongestPrefixMatch(const Name& name) const |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 85 | { |
Junxiao Shi | 4063184 | 2014-03-01 13:52:37 -0700 | [diff] [blame] | 86 | 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 Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 90 | } |
HangZhang | c85a23c | 2014-03-01 15:55:55 +0800 | [diff] [blame] | 91 | return shared_ptr<measurements::Entry>(); |
| 92 | } |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 93 | |
HangZhang | c85a23c | 2014-03-01 15:55:55 +0800 | [diff] [blame] | 94 | shared_ptr<measurements::Entry> |
| 95 | Measurements::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 Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | void |
HangZhang | c85a23c | 2014-03-01 15:55:55 +0800 | [diff] [blame] | 104 | Measurements::extendLifetime(measurements::Entry& entry, const time::Duration lifetime) |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 105 | { |
HangZhang | c85a23c | 2014-03-01 15:55:55 +0800 | [diff] [blame] | 106 | shared_ptr<measurements::Entry> ret = this->findExactMatch(entry.getName()); |
| 107 | if (static_cast<bool>(ret)) |
| 108 | { |
| 109 | time::Point expiry = time::now() + lifetime; |
| 110 | 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 | |
| 119 | void |
| 120 | Measurements::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 | { |
HangZhang | cb4fc83 | 2014-03-11 16:57:11 +0800 | [diff] [blame] | 127 | nameTreeEntry->setMeasurementsEntry(shared_ptr<measurements::Entry>()); |
HangZhang | c85a23c | 2014-03-01 15:55:55 +0800 | [diff] [blame] | 128 | m_nItems--; |
| 129 | } |
HangZhang | cb4fc83 | 2014-03-11 16:57:11 +0800 | [diff] [blame] | 130 | |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | } // namespace nfd |