blob: bc5a9d25f56e21d09f921a15675c86de62877a34 [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
16Measurements::Measurements(boost::asio::io_service& ioService)
17 : m_scheduler(ioService)
18{
19}
20
21Measurements::~Measurements()
22{
23}
24
25shared_ptr<measurements::Entry>
26Measurements::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
41shared_ptr<measurements::Entry>
42Measurements::get(const fib::Entry& fibEntry)
43{
44 return this->get(fibEntry.getPrefix());
45}
46
47shared_ptr<measurements::Entry>
48Measurements::get(const pit::Entry& pitEntry)
49{
50 return this->get(pitEntry.getName());
51}
52
53shared_ptr<measurements::Entry>
54Measurements::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
73void
74Measurements::extendLifetime(measurements::Entry& entry, time::Duration lifetime)
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
83void
84Measurements::extendLifetimeInternal(
85 std::map<Name, shared_ptr<measurements::Entry> >::iterator it,
86 time::Duration lifetime)
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
101void
102Measurements::cleanup(std::map<Name, shared_ptr<measurements::Entry> >::iterator it)
103{
104 m_table.erase(it);
105}
106
107} // namespace nfd