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" |
| 8 | #include <algorithm> |
| 9 | #include "fib-entry.hpp" |
| 10 | #include "pit-entry.hpp" |
| 11 | |
| 12 | namespace nfd { |
| 13 | |
| 14 | const time::Duration Measurements::s_defaultLifetime = time::seconds(4); |
| 15 | |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 16 | Measurements::Measurements() |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 17 | { |
| 18 | } |
| 19 | |
| 20 | Measurements::~Measurements() |
| 21 | { |
| 22 | } |
| 23 | |
| 24 | shared_ptr<measurements::Entry> |
| 25 | Measurements::get(const Name& name) |
| 26 | { |
| 27 | std::map<Name, shared_ptr<measurements::Entry> >::iterator it = m_table.find(name); |
| 28 | if (it != m_table.end()) { |
| 29 | return it->second; |
| 30 | } |
| 31 | |
| 32 | shared_ptr<measurements::Entry> entry = make_shared<measurements::Entry>(name); |
| 33 | std::pair<std::map<Name, shared_ptr<measurements::Entry> >::iterator, bool> pair = |
| 34 | m_table.insert(std::make_pair(name, entry)); |
| 35 | this->extendLifetimeInternal(pair.first, s_defaultLifetime); |
| 36 | |
| 37 | return entry; |
| 38 | } |
| 39 | |
| 40 | shared_ptr<measurements::Entry> |
| 41 | Measurements::get(const fib::Entry& fibEntry) |
| 42 | { |
| 43 | return this->get(fibEntry.getPrefix()); |
| 44 | } |
| 45 | |
| 46 | shared_ptr<measurements::Entry> |
| 47 | Measurements::get(const pit::Entry& pitEntry) |
| 48 | { |
| 49 | return this->get(pitEntry.getName()); |
| 50 | } |
| 51 | |
| 52 | shared_ptr<measurements::Entry> |
| 53 | Measurements::getParent(shared_ptr<measurements::Entry> child) |
| 54 | { |
| 55 | if (child->getName().size() == 0) { |
| 56 | return shared_ptr<measurements::Entry>(); |
| 57 | } |
| 58 | |
| 59 | return this->get(child->getName().getPrefix(-1)); |
| 60 | } |
| 61 | |
| 62 | //shared_ptr<fib::Entry> |
| 63 | //Measurements::findLongestPrefixMatch(const Name& name) const |
| 64 | //{ |
| 65 | //} |
| 66 | // |
| 67 | //shared_ptr<fib::Entry> |
| 68 | //Measurements::findExactMatch(const Name& name) const |
| 69 | //{ |
| 70 | //} |
| 71 | |
| 72 | void |
Junxiao Shi | dbe7173 | 2014-02-21 22:23:28 -0700 | [diff] [blame] | 73 | Measurements::extendLifetime(measurements::Entry& entry, const time::Duration& lifetime) |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 74 | { |
| 75 | std::map<Name, shared_ptr<measurements::Entry> >::iterator it = |
| 76 | m_table.find(entry.getName()); |
| 77 | BOOST_ASSERT(it != m_table.end()); |
| 78 | |
| 79 | this->extendLifetimeInternal(it, lifetime); |
| 80 | } |
| 81 | |
| 82 | void |
| 83 | Measurements::extendLifetimeInternal( |
| 84 | std::map<Name, shared_ptr<measurements::Entry> >::iterator it, |
Junxiao Shi | dbe7173 | 2014-02-21 22:23:28 -0700 | [diff] [blame] | 85 | const time::Duration& lifetime) |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 86 | { |
| 87 | shared_ptr<measurements::Entry>& entry = it->second; |
| 88 | |
| 89 | time::Point expiry = time::now() + lifetime; |
| 90 | if (entry->m_expiry >= expiry) { // has longer lifetime, not extending |
| 91 | return; |
| 92 | } |
| 93 | |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 94 | scheduler::cancel(entry->m_cleanup); |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 95 | entry->m_expiry = expiry; |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 96 | entry->m_cleanup = scheduler::schedule(lifetime, |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 97 | bind(&Measurements::cleanup, this, it)); |
| 98 | } |
| 99 | |
| 100 | void |
| 101 | Measurements::cleanup(std::map<Name, shared_ptr<measurements::Entry> >::iterator it) |
| 102 | { |
| 103 | m_table.erase(it); |
| 104 | } |
| 105 | |
| 106 | } // namespace nfd |