blob: a5b107e24667f612a6d7114f9f492477c4e2ab45 [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
Junxiao Shi40631842014-03-01 13:52:37 -070026static inline bool
27predicate_NameTreeEntry_hasMeasurementsEntry(const name_tree::Entry& entry)
28{
29 return static_cast<bool>(entry.getMeasurementsEntry());
30}
31
Junxiao Shi65d00722014-02-17 10:50:20 -070032shared_ptr<measurements::Entry>
33Measurements::get(const Name& name)
34{
HangZhangc85a23c2014-03-01 15:55:55 +080035 shared_ptr<name_tree::Entry> nameTreeEntry = m_nameTree.lookup(name);
36 shared_ptr<measurements::Entry> entry = nameTreeEntry->getMeasurementsEntry();
37 if (static_cast<bool>(entry))
38 return entry;
39 entry = make_shared<measurements::Entry>(name);
40 nameTreeEntry->setMeasurementsEntry(entry);
41 m_nItems++;
Junxiao Shi65d00722014-02-17 10:50:20 -070042 return entry;
43}
44
45shared_ptr<measurements::Entry>
46Measurements::get(const fib::Entry& fibEntry)
47{
48 return this->get(fibEntry.getPrefix());
49}
50
51shared_ptr<measurements::Entry>
52Measurements::get(const pit::Entry& pitEntry)
53{
54 return this->get(pitEntry.getName());
55}
56
57shared_ptr<measurements::Entry>
58Measurements::getParent(shared_ptr<measurements::Entry> child)
59{
HangZhangc85a23c2014-03-01 15:55:55 +080060 BOOST_ASSERT(child);
61
Junxiao Shi65d00722014-02-17 10:50:20 -070062 if (child->getName().size() == 0) {
63 return shared_ptr<measurements::Entry>();
64 }
65
66 return this->get(child->getName().getPrefix(-1));
67}
68
HangZhangc85a23c2014-03-01 15:55:55 +080069shared_ptr<measurements::Entry>
70Measurements::findLongestPrefixMatch(const Name& name) const
Junxiao Shi65d00722014-02-17 10:50:20 -070071{
Junxiao Shi40631842014-03-01 13:52:37 -070072 shared_ptr<name_tree::Entry> nameTreeEntry =
73 m_nameTree.findLongestPrefixMatch(name, &predicate_NameTreeEntry_hasMeasurementsEntry);
74 if (static_cast<bool>(nameTreeEntry)) {
75 return nameTreeEntry->getMeasurementsEntry();
Junxiao Shi65d00722014-02-17 10:50:20 -070076 }
HangZhangc85a23c2014-03-01 15:55:55 +080077 return shared_ptr<measurements::Entry>();
78}
Junxiao Shi65d00722014-02-17 10:50:20 -070079
HangZhangc85a23c2014-03-01 15:55:55 +080080shared_ptr<measurements::Entry>
81Measurements::findExactMatch(const Name& name) const
82{
83 shared_ptr<name_tree::Entry> nameTreeEntry = m_nameTree.lookup(name);
84 if (static_cast<bool>(nameTreeEntry))
85 return nameTreeEntry->getMeasurementsEntry();
86 return shared_ptr<measurements::Entry>();
Junxiao Shi65d00722014-02-17 10:50:20 -070087}
88
89void
HangZhangc85a23c2014-03-01 15:55:55 +080090Measurements::extendLifetime(measurements::Entry& entry, const time::Duration lifetime)
Junxiao Shi65d00722014-02-17 10:50:20 -070091{
HangZhangc85a23c2014-03-01 15:55:55 +080092 shared_ptr<measurements::Entry> ret = this->findExactMatch(entry.getName());
93 if (static_cast<bool>(ret))
94 {
95 time::Point expiry = time::now() + lifetime;
96 if (ret->m_expiry >= expiry) // has longer lifetime, not extending
97 return;
98 scheduler::cancel(entry.m_cleanup);
99 entry.m_expiry = expiry;
100 entry.m_cleanup = scheduler::schedule(lifetime,
101 bind(&Measurements::cleanup, this, ret));
102 }
103}
104
105void
106Measurements::cleanup(shared_ptr<measurements::Entry> entry)
107{
108 BOOST_ASSERT(entry);
109
110 shared_ptr<name_tree::Entry> nameTreeEntry = m_nameTree.findExactMatch(entry->getName());
111 if (static_cast<bool>(nameTreeEntry))
112 {
113 nameTreeEntry->eraseMeasurementsEntry(
114 nameTreeEntry->getMeasurementsEntry());
115 m_nItems--;
116 }
117
Junxiao Shi65d00722014-02-17 10:50:20 -0700118}
119
120} // namespace nfd