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 | { |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame^] | 22 | for (std::map<Name, shared_ptr<measurements::Entry> >::iterator it = m_table.begin(); |
| 23 | it != m_table.end(); ++it) { |
| 24 | shared_ptr<measurements::Entry> entry = it->second; |
| 25 | scheduler::cancel(entry->m_cleanup); |
| 26 | } |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 27 | } |
| 28 | |
| 29 | shared_ptr<measurements::Entry> |
| 30 | Measurements::get(const Name& name) |
| 31 | { |
| 32 | std::map<Name, shared_ptr<measurements::Entry> >::iterator it = m_table.find(name); |
| 33 | if (it != m_table.end()) { |
| 34 | return it->second; |
| 35 | } |
| 36 | |
| 37 | shared_ptr<measurements::Entry> entry = make_shared<measurements::Entry>(name); |
| 38 | std::pair<std::map<Name, shared_ptr<measurements::Entry> >::iterator, bool> pair = |
| 39 | m_table.insert(std::make_pair(name, entry)); |
| 40 | this->extendLifetimeInternal(pair.first, s_defaultLifetime); |
| 41 | |
| 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 | { |
| 60 | if (child->getName().size() == 0) { |
| 61 | return shared_ptr<measurements::Entry>(); |
| 62 | } |
| 63 | |
| 64 | return this->get(child->getName().getPrefix(-1)); |
| 65 | } |
| 66 | |
| 67 | //shared_ptr<fib::Entry> |
| 68 | //Measurements::findLongestPrefixMatch(const Name& name) const |
| 69 | //{ |
| 70 | //} |
| 71 | // |
| 72 | //shared_ptr<fib::Entry> |
| 73 | //Measurements::findExactMatch(const Name& name) const |
| 74 | //{ |
| 75 | //} |
| 76 | |
| 77 | void |
Junxiao Shi | dbe7173 | 2014-02-21 22:23:28 -0700 | [diff] [blame] | 78 | Measurements::extendLifetime(measurements::Entry& entry, const time::Duration& lifetime) |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 79 | { |
| 80 | std::map<Name, shared_ptr<measurements::Entry> >::iterator it = |
| 81 | m_table.find(entry.getName()); |
| 82 | BOOST_ASSERT(it != m_table.end()); |
| 83 | |
| 84 | this->extendLifetimeInternal(it, lifetime); |
| 85 | } |
| 86 | |
| 87 | void |
| 88 | Measurements::extendLifetimeInternal( |
| 89 | std::map<Name, shared_ptr<measurements::Entry> >::iterator it, |
Junxiao Shi | dbe7173 | 2014-02-21 22:23:28 -0700 | [diff] [blame] | 90 | const time::Duration& lifetime) |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 91 | { |
| 92 | shared_ptr<measurements::Entry>& entry = it->second; |
| 93 | |
| 94 | time::Point expiry = time::now() + lifetime; |
| 95 | if (entry->m_expiry >= expiry) { // has longer lifetime, not extending |
| 96 | return; |
| 97 | } |
| 98 | |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 99 | scheduler::cancel(entry->m_cleanup); |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 100 | entry->m_expiry = expiry; |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 101 | entry->m_cleanup = scheduler::schedule(lifetime, |
Junxiao Shi | 65d0072 | 2014-02-17 10:50:20 -0700 | [diff] [blame] | 102 | bind(&Measurements::cleanup, this, it)); |
| 103 | } |
| 104 | |
| 105 | void |
| 106 | Measurements::cleanup(std::map<Name, shared_ptr<measurements::Entry> >::iterator it) |
| 107 | { |
| 108 | m_table.erase(it); |
| 109 | } |
| 110 | |
| 111 | } // namespace nfd |