blob: e361abadb177b47453aed9b5373f3c43a2669cdb [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"
HangZhangc85a23c2014-03-01 15:55:55 +08008#include "name-tree.hpp"
Junxiao Shi65d00722014-02-17 10:50:20 -07009#include "pit-entry.hpp"
HangZhangc85a23c2014-03-01 15:55:55 +080010#include "fib-entry.hpp"
Junxiao Shi65d00722014-02-17 10:50:20 -070011
12namespace nfd {
13
14const time::Duration Measurements::s_defaultLifetime = time::seconds(4);
15
HangZhangc85a23c2014-03-01 15:55:55 +080016Measurements::Measurements(NameTree& nameTree)
17 : m_nameTree(nameTree)
18 , m_nItems(0)
Junxiao Shi65d00722014-02-17 10:50:20 -070019{
20}
21
22Measurements::~Measurements()
23{
24}
25
26shared_ptr<measurements::Entry>
27Measurements::get(const Name& name)
28{
HangZhangc85a23c2014-03-01 15:55:55 +080029 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 Shi65d00722014-02-17 10:50:20 -070036 return entry;
37}
38
39shared_ptr<measurements::Entry>
40Measurements::get(const fib::Entry& fibEntry)
41{
42 return this->get(fibEntry.getPrefix());
43}
44
45shared_ptr<measurements::Entry>
46Measurements::get(const pit::Entry& pitEntry)
47{
48 return this->get(pitEntry.getName());
49}
50
51shared_ptr<measurements::Entry>
52Measurements::getParent(shared_ptr<measurements::Entry> child)
53{
HangZhangc85a23c2014-03-01 15:55:55 +080054 BOOST_ASSERT(child);
55
Junxiao Shi65d00722014-02-17 10:50:20 -070056 if (child->getName().size() == 0) {
57 return shared_ptr<measurements::Entry>();
58 }
59
60 return this->get(child->getName().getPrefix(-1));
61}
62
HangZhangc85a23c2014-03-01 15:55:55 +080063shared_ptr<measurements::Entry>
64Measurements::findLongestPrefixMatch(const Name& name) const
Junxiao Shi65d00722014-02-17 10:50:20 -070065{
HangZhangc85a23c2014-03-01 15:55:55 +080066 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 Shi65d00722014-02-17 10:50:20 -070072 }
HangZhangc85a23c2014-03-01 15:55:55 +080073 return shared_ptr<measurements::Entry>();
74}
Junxiao Shi65d00722014-02-17 10:50:20 -070075
HangZhangc85a23c2014-03-01 15:55:55 +080076shared_ptr<measurements::Entry>
77Measurements::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 Shi65d00722014-02-17 10:50:20 -070083}
84
85void
HangZhangc85a23c2014-03-01 15:55:55 +080086Measurements::extendLifetime(measurements::Entry& entry, const time::Duration lifetime)
Junxiao Shi65d00722014-02-17 10:50:20 -070087{
HangZhangc85a23c2014-03-01 15:55:55 +080088 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
101void
102Measurements::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 Shi65d00722014-02-17 10:50:20 -0700114}
115
116} // namespace nfd