blob: 8caf343ff3b799621de5f13a626b9bdadd8879ee [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{
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070022 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 Shi65d00722014-02-17 10:50:20 -070027}
28
29shared_ptr<measurements::Entry>
30Measurements::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
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{
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
77void
Junxiao Shidbe71732014-02-21 22:23:28 -070078Measurements::extendLifetime(measurements::Entry& entry, const time::Duration& lifetime)
Junxiao Shi65d00722014-02-17 10:50:20 -070079{
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
87void
88Measurements::extendLifetimeInternal(
89 std::map<Name, shared_ptr<measurements::Entry> >::iterator it,
Junxiao Shidbe71732014-02-21 22:23:28 -070090 const time::Duration& lifetime)
Junxiao Shi65d00722014-02-17 10:50:20 -070091{
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 Shic041ca32014-02-25 20:01:15 -070099 scheduler::cancel(entry->m_cleanup);
Junxiao Shi65d00722014-02-17 10:50:20 -0700100 entry->m_expiry = expiry;
Junxiao Shic041ca32014-02-25 20:01:15 -0700101 entry->m_cleanup = scheduler::schedule(lifetime,
Junxiao Shi65d00722014-02-17 10:50:20 -0700102 bind(&Measurements::cleanup, this, it));
103}
104
105void
106Measurements::cleanup(std::map<Name, shared_ptr<measurements::Entry> >::iterator it)
107{
108 m_table.erase(it);
109}
110
111} // namespace nfd