Junxiao Shi | dbe7173 | 2014-02-21 22:23:28 -0700 | [diff] [blame] | 1 | /* -*- 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 | #ifndef NFD_TABLE_MEASUREMENTS_ACCESSOR_HPP |
| 8 | #define NFD_TABLE_MEASUREMENTS_ACCESSOR_HPP |
| 9 | |
| 10 | #include "measurements.hpp" |
| 11 | #include "fib.hpp" |
| 12 | |
| 13 | namespace nfd { |
| 14 | |
| 15 | namespace fw { |
| 16 | class Strategy; |
| 17 | } |
| 18 | |
| 19 | /** \brief allows Strategy to access portion of Measurements table under its namespace |
| 20 | */ |
| 21 | class MeasurementsAccessor : noncopyable |
| 22 | { |
| 23 | public: |
| 24 | MeasurementsAccessor(Measurements& measurements, Fib& fib, fw::Strategy* strategy); |
| 25 | |
| 26 | ~MeasurementsAccessor(); |
| 27 | |
| 28 | /// find or insert a Measurements entry for name |
| 29 | shared_ptr<measurements::Entry> |
| 30 | get(const Name& name); |
| 31 | |
| 32 | /// find or insert a Measurements entry for fibEntry->getPrefix() |
| 33 | shared_ptr<measurements::Entry> |
| 34 | get(const fib::Entry& fibEntry); |
| 35 | |
| 36 | /// find or insert a Measurements entry for pitEntry->getName() |
| 37 | shared_ptr<measurements::Entry> |
| 38 | get(const pit::Entry& pitEntry); |
| 39 | |
| 40 | /** \brief find or insert a Measurements entry for child's parent |
| 41 | * |
| 42 | * If child is the root entry, returns null. |
| 43 | */ |
| 44 | shared_ptr<measurements::Entry> |
| 45 | getParent(shared_ptr<measurements::Entry> child); |
| 46 | |
| 47 | // /// perform a longest prefix match |
| 48 | // shared_ptr<fib::Entry> |
| 49 | // findLongestPrefixMatch(const Name& name) const; |
| 50 | // |
| 51 | // /// perform an exact match |
| 52 | // shared_ptr<fib::Entry> |
| 53 | // findExactMatch(const Name& name) const; |
| 54 | |
| 55 | /** \brief extend lifetime of an entry |
| 56 | * |
| 57 | * The entry will be kept until at least now()+lifetime. |
| 58 | */ |
| 59 | void |
| 60 | extendLifetime(measurements::Entry& entry, const time::Duration& lifetime); |
| 61 | |
| 62 | private: |
| 63 | /** \brief perform access control to Measurements entry |
| 64 | * |
| 65 | * \return entry if strategy has access to namespace, otherwise 0 |
| 66 | */ |
| 67 | shared_ptr<measurements::Entry> |
| 68 | filter(const shared_ptr<measurements::Entry>& entry); |
| 69 | |
| 70 | private: |
| 71 | Measurements& m_measurements; |
| 72 | Fib& m_fib; |
| 73 | fw::Strategy* m_strategy; |
| 74 | }; |
| 75 | |
| 76 | inline shared_ptr<measurements::Entry> |
| 77 | MeasurementsAccessor::get(const Name& name) |
| 78 | { |
| 79 | return this->filter(m_measurements.get(name)); |
| 80 | } |
| 81 | |
| 82 | inline shared_ptr<measurements::Entry> |
| 83 | MeasurementsAccessor::get(const fib::Entry& fibEntry) |
| 84 | { |
| 85 | if (&fibEntry.getStrategy() == m_strategy) { |
| 86 | return m_measurements.get(fibEntry); |
| 87 | } |
| 88 | return shared_ptr<measurements::Entry>(); |
| 89 | } |
| 90 | |
| 91 | inline shared_ptr<measurements::Entry> |
| 92 | MeasurementsAccessor::get(const pit::Entry& pitEntry) |
| 93 | { |
| 94 | return this->filter(m_measurements.get(pitEntry)); |
| 95 | } |
| 96 | |
| 97 | inline shared_ptr<measurements::Entry> |
| 98 | MeasurementsAccessor::getParent(shared_ptr<measurements::Entry> child) |
| 99 | { |
| 100 | return this->filter(m_measurements.getParent(child)); |
| 101 | } |
| 102 | |
| 103 | //inline shared_ptr<fib::Entry> |
| 104 | //MeasurementsAccessor::findLongestPrefixMatch(const Name& name) const |
| 105 | //{ |
| 106 | // return this->filter(m_measurements.findLongestPrefixMatch(name)); |
| 107 | //} |
| 108 | // |
| 109 | //inline shared_ptr<fib::Entry> |
| 110 | //MeasurementsAccessor::findExactMatch(const Name& name) const |
| 111 | //{ |
| 112 | // return this->filter(m_measurements.findExactMatch(name)); |
| 113 | //} |
| 114 | |
| 115 | inline void |
| 116 | MeasurementsAccessor::extendLifetime(measurements::Entry& entry, const time::Duration& lifetime) |
| 117 | { |
| 118 | m_measurements.extendLifetime(entry, lifetime); |
| 119 | } |
| 120 | |
| 121 | } // namespace nfd |
| 122 | |
| 123 | #endif // NFD_TABLE_MEASUREMENTS_ACCESSOR_HPP |