blob: ed01018dba9d7e19640d4ea4bafa2692c86d6abb [file] [log] [blame]
Junxiao Shi65d00722014-02-17 10:50:20 -07001/* -*- 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
12namespace nfd {
13
14const time::Duration Measurements::s_defaultLifetime = time::seconds(4);
15
Junxiao Shic041ca32014-02-25 20:01:15 -070016Measurements::Measurements()
Junxiao Shi65d00722014-02-17 10:50:20 -070017{
18}
19
20Measurements::~Measurements()
21{
22}
23
24shared_ptr<measurements::Entry>
25Measurements::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
40shared_ptr<measurements::Entry>
41Measurements::get(const fib::Entry& fibEntry)
42{
43 return this->get(fibEntry.getPrefix());
44}
45
46shared_ptr<measurements::Entry>
47Measurements::get(const pit::Entry& pitEntry)
48{
49 return this->get(pitEntry.getName());
50}
51
52shared_ptr<measurements::Entry>
53Measurements::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
72void
Junxiao Shidbe71732014-02-21 22:23:28 -070073Measurements::extendLifetime(measurements::Entry& entry, const time::Duration& lifetime)
Junxiao Shi65d00722014-02-17 10:50:20 -070074{
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
82void
83Measurements::extendLifetimeInternal(
84 std::map<Name, shared_ptr<measurements::Entry> >::iterator it,
Junxiao Shidbe71732014-02-21 22:23:28 -070085 const time::Duration& lifetime)
Junxiao Shi65d00722014-02-17 10:50:20 -070086{
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 Shic041ca32014-02-25 20:01:15 -070094 scheduler::cancel(entry->m_cleanup);
Junxiao Shi65d00722014-02-17 10:50:20 -070095 entry->m_expiry = expiry;
Junxiao Shic041ca32014-02-25 20:01:15 -070096 entry->m_cleanup = scheduler::schedule(lifetime,
Junxiao Shi65d00722014-02-17 10:50:20 -070097 bind(&Measurements::cleanup, this, it));
98}
99
100void
101Measurements::cleanup(std::map<Name, shared_ptr<measurements::Entry> >::iterator it)
102{
103 m_table.erase(it);
104}
105
106} // namespace nfd