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