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