blob: 6942124234b4e39e40029e82ae0bfa089a36c024 [file] [log] [blame]
Junxiao Shi65d00722014-02-17 10:50:20 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Junxiao Shi80f9fcd2016-07-23 02:48:36 +00003 * Copyright (c) 2014-2016, Regents of the University of California,
Junxiao Shi767cb332015-01-08 09:35:49 -07004 * Arizona Board of Regents,
5 * Colorado State University,
Junxiao Shi35353962015-01-08 09:13:47 -07006 * University Pierre & Marie Curie, Sorbonne University,
Junxiao Shi767cb332015-01-08 09:35:49 -07007 * Washington University in St. Louis,
8 * Beijing Institute of Technology,
9 * The University of Memphis.
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070010 *
11 * This file is part of NFD (Named Data Networking Forwarding Daemon).
12 * See AUTHORS.md for complete list of NFD authors and contributors.
13 *
14 * NFD is free software: you can redistribute it and/or modify it under the terms
15 * of the GNU General Public License as published by the Free Software Foundation,
16 * either version 3 of the License, or (at your option) any later version.
17 *
18 * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
19 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
20 * PURPOSE. See the GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License along with
23 * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
Junxiao Shi19838042014-06-21 00:34:01 -070024 */
Junxiao Shi65d00722014-02-17 10:50:20 -070025
Alexander Afanasyev613e2a92014-04-15 13:36:58 -070026#ifndef NFD_DAEMON_TABLE_MEASUREMENTS_HPP
27#define NFD_DAEMON_TABLE_MEASUREMENTS_HPP
Junxiao Shi65d00722014-02-17 10:50:20 -070028
29#include "measurements-entry.hpp"
HangZhangc85a23c2014-03-01 15:55:55 +080030#include "name-tree.hpp"
Junxiao Shi65d00722014-02-17 10:50:20 -070031
32namespace nfd {
33
HangZhangc85a23c2014-03-01 15:55:55 +080034namespace fib {
35class Entry;
Junxiao Shib30c7b02015-01-07 15:45:54 -070036} // namespace fib
HangZhangc85a23c2014-03-01 15:55:55 +080037
38namespace pit {
39class Entry;
Junxiao Shib30c7b02015-01-07 15:45:54 -070040} // namespace pit
HangZhangc85a23c2014-03-01 15:55:55 +080041
Junxiao Shib30c7b02015-01-07 15:45:54 -070042namespace measurements {
HangZhangc85a23c2014-03-01 15:55:55 +080043
Junxiao Shi80f9fcd2016-07-23 02:48:36 +000044/** \brief a predicate that accepts or rejects an entry
Junxiao Shib30c7b02015-01-07 15:45:54 -070045 */
46typedef std::function<bool(const Entry&)> EntryPredicate;
47
Junxiao Shi80f9fcd2016-07-23 02:48:36 +000048/** \brief an \p EntryPredicate that accepts any entry
Junxiao Shib30c7b02015-01-07 15:45:54 -070049 */
50class AnyEntry
51{
52public:
53 bool
Junxiao Shi80f9fcd2016-07-23 02:48:36 +000054 operator()(const Entry& entry) const
Junxiao Shib30c7b02015-01-07 15:45:54 -070055 {
56 return true;
57 }
58};
59
Junxiao Shi80f9fcd2016-07-23 02:48:36 +000060/** \brief an \p EntryPredicate that accepts an entry if it has StrategyInfo of type T
61 */
Junxiao Shib30c7b02015-01-07 15:45:54 -070062template<typename T>
63class EntryWithStrategyInfo
64{
65public:
66 bool
Junxiao Shi80f9fcd2016-07-23 02:48:36 +000067 operator()(const Entry& entry) const
Junxiao Shib30c7b02015-01-07 15:45:54 -070068 {
69 return entry.getStrategyInfo<T>() != nullptr;
70 }
71};
72
Junxiao Shib30c7b02015-01-07 15:45:54 -070073/** \brief represents the Measurements table
Junxiao Shi65d00722014-02-17 10:50:20 -070074 */
75class Measurements : noncopyable
76{
77public:
HangZhangc85a23c2014-03-01 15:55:55 +080078 explicit
79 Measurements(NameTree& nametree);
Junxiao Shi65d00722014-02-17 10:50:20 -070080
Junxiao Shi767cb332015-01-08 09:35:49 -070081 /** \brief find or insert a Measurements entry for \p name
Junxiao Shie368d992014-12-02 23:44:31 -070082 */
Junxiao Shi80f9fcd2016-07-23 02:48:36 +000083 Entry&
Junxiao Shi65d00722014-02-17 10:50:20 -070084 get(const Name& name);
85
Junxiao Shi767cb332015-01-08 09:35:49 -070086 /** \brief find or insert a Measurements entry for \p fibEntry.getPrefix()
Junxiao Shie368d992014-12-02 23:44:31 -070087 */
Junxiao Shi80f9fcd2016-07-23 02:48:36 +000088 Entry&
Junxiao Shi65d00722014-02-17 10:50:20 -070089 get(const fib::Entry& fibEntry);
90
Junxiao Shi767cb332015-01-08 09:35:49 -070091 /** \brief find or insert a Measurements entry for \p pitEntry.getName()
Junxiao Shie368d992014-12-02 23:44:31 -070092 */
Junxiao Shi80f9fcd2016-07-23 02:48:36 +000093 Entry&
Junxiao Shi65d00722014-02-17 10:50:20 -070094 get(const pit::Entry& pitEntry);
95
Junxiao Shie368d992014-12-02 23:44:31 -070096 /** \brief find or insert a Measurements entry for child's parent
97 * \retval nullptr if child is the root entry
Junxiao Shi65d00722014-02-17 10:50:20 -070098 */
Junxiao Shi80f9fcd2016-07-23 02:48:36 +000099 Entry*
100 getParent(const Entry& child);
Junxiao Shi65d00722014-02-17 10:50:20 -0700101
Junxiao Shi767cb332015-01-08 09:35:49 -0700102 /** \brief perform a longest prefix match for \p name
Junxiao Shie368d992014-12-02 23:44:31 -0700103 */
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000104 Entry*
Junxiao Shib30c7b02015-01-07 15:45:54 -0700105 findLongestPrefixMatch(const Name& name,
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000106 const EntryPredicate& pred = AnyEntry()) const;
Junxiao Shi65d00722014-02-17 10:50:20 -0700107
Junxiao Shi767cb332015-01-08 09:35:49 -0700108 /** \brief perform a longest prefix match for \p pitEntry.getName()
109 */
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000110 Entry*
Junxiao Shi767cb332015-01-08 09:35:49 -0700111 findLongestPrefixMatch(const pit::Entry& pitEntry,
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000112 const EntryPredicate& pred = AnyEntry()) const;
Junxiao Shi767cb332015-01-08 09:35:49 -0700113
Junxiao Shie368d992014-12-02 23:44:31 -0700114 /** \brief perform an exact match
115 */
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000116 Entry*
HangZhangc85a23c2014-03-01 15:55:55 +0800117 findExactMatch(const Name& name) const;
118
Junxiao Shi19838042014-06-21 00:34:01 -0700119 static time::nanoseconds
120 getInitialLifetime();
121
Junxiao Shie368d992014-12-02 23:44:31 -0700122 /** \brief extend lifetime of an entry
Junxiao Shi65d00722014-02-17 10:50:20 -0700123 *
124 * The entry will be kept until at least now()+lifetime.
125 */
126 void
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000127 extendLifetime(Entry& entry, const time::nanoseconds& lifetime);
HangZhangc85a23c2014-03-01 15:55:55 +0800128
129 size_t
130 size() const;
Junxiao Shi65d00722014-02-17 10:50:20 -0700131
132private:
133 void
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000134 cleanup(Entry& entry);
Junxiao Shi65d00722014-02-17 10:50:20 -0700135
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000136 Entry&
Junxiao Shie368d992014-12-02 23:44:31 -0700137 get(name_tree::Entry& nte);
HangZhangcb4fc832014-03-11 16:57:11 +0800138
Junxiao Shi811c0102016-08-10 04:12:45 +0000139 /** \tparam K Name or name_tree::Entry
Junxiao Shi767cb332015-01-08 09:35:49 -0700140 */
141 template<typename K>
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000142 Entry*
143 findLongestPrefixMatchImpl(const K& key, const EntryPredicate& pred) const;
Junxiao Shi767cb332015-01-08 09:35:49 -0700144
Junxiao Shi65d00722014-02-17 10:50:20 -0700145private:
HangZhangc85a23c2014-03-01 15:55:55 +0800146 NameTree& m_nameTree;
147 size_t m_nItems;
Junxiao Shi65d00722014-02-17 10:50:20 -0700148};
149
Junxiao Shi19838042014-06-21 00:34:01 -0700150inline time::nanoseconds
151Measurements::getInitialLifetime()
152{
153 return time::seconds(4);
154}
155
HangZhangc85a23c2014-03-01 15:55:55 +0800156inline size_t
157Measurements::size() const
158{
159 return m_nItems;
160}
161
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000162} // namespace measurements
163
164using measurements::Measurements;
165
Junxiao Shi65d00722014-02-17 10:50:20 -0700166} // namespace nfd
167
Alexander Afanasyev613e2a92014-04-15 13:36:58 -0700168#endif // NFD_DAEMON_TABLE_MEASUREMENTS_HPP