Yingdi Yu | 1c4ba9a | 2014-08-27 19:05:49 -0700 | [diff] [blame] | 1 | /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /* |
| 3 | * Copyright (c) 2012-2014 University of California, Los Angeles |
| 4 | * |
| 5 | * This file is part of ChronoSync, synchronization library for distributed realtime |
| 6 | * applications for NDN. |
| 7 | * |
| 8 | * ChronoSync is free software: you can redistribute it and/or modify it under the terms |
| 9 | * of the GNU General Public License as published by the Free Software Foundation, either |
| 10 | * version 3 of the License, or (at your option) any later version. |
| 11 | * |
| 12 | * ChronoSync is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 13 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 14 | * PURPOSE. See the GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License along with |
| 17 | * ChronoSync, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 18 | * |
| 19 | * @author Zhenkai Zhu <http://irl.cs.ucla.edu/~zhenkai/> |
| 20 | * @author Chaoyi Bian <bcy@pku.edu.cn> |
| 21 | * @author Alexander Afanasyev <http://lasr.cs.ucla.edu/afanasyev/index.html> |
| 22 | * @author Yingdi Yu <yingdi@cs.ucla.edu> |
| 23 | */ |
| 24 | |
| 25 | #ifndef CHRONOSYNC_INTEREST_TABLE_HPP |
| 26 | #define CHRONOSYNC_INTEREST_TABLE_HPP |
| 27 | |
| 28 | #include "interest-container.hpp" |
| 29 | |
| 30 | #include <ndn-cxx/util/scheduler.hpp> |
| 31 | |
| 32 | namespace chronosync { |
| 33 | |
| 34 | /** |
| 35 | * @brief A table to keep unsatisfied Sync Interest |
| 36 | */ |
| 37 | class InterestTable : noncopyable |
| 38 | { |
| 39 | public: |
| 40 | class Error : public std::runtime_error |
| 41 | { |
| 42 | public: |
| 43 | explicit |
| 44 | Error(const std::string& what) |
| 45 | : std::runtime_error(what) |
| 46 | { |
| 47 | } |
| 48 | }; |
| 49 | |
| 50 | typedef InterestContainer::iterator iterator; |
| 51 | typedef InterestContainer::const_iterator const_iterator; |
| 52 | |
| 53 | explicit |
| 54 | InterestTable(boost::asio::io_service& io); |
| 55 | |
| 56 | ~InterestTable(); |
| 57 | |
| 58 | /** |
| 59 | * @brief Insert an interest |
| 60 | * |
| 61 | * If the interest already exists in the table, the old interest will be replaced, |
| 62 | * and expire timer will be reset. |
| 63 | * Interests with the same name are counted as the same. |
| 64 | * This method assumes that the sync prefix of all interests are the same |
| 65 | * thus it only compares the digest part. |
| 66 | * |
| 67 | * @param interest Interest to insert. |
| 68 | * @param digest The value of the last digest component. |
| 69 | * @param isKnown false if the digest is an unknown digest. |
| 70 | * @return true if the same interest exists in the table before insertion. |
| 71 | */ |
| 72 | bool |
| 73 | insert(shared_ptr<const Interest> interest, |
| 74 | ndn::ConstBufferPtr digest, |
| 75 | bool isKnown = false); |
| 76 | |
| 77 | /** |
| 78 | * @brief Delete interest by digest (e.g., when it was satisfied) |
| 79 | * |
| 80 | * @return true if an interest with the digest exists in the table before deletion |
| 81 | */ |
| 82 | bool |
| 83 | erase(ndn::ConstBufferPtr digest); |
| 84 | |
| 85 | const_iterator |
| 86 | begin() const |
| 87 | { |
| 88 | return m_table.begin(); |
| 89 | } |
| 90 | |
| 91 | iterator |
| 92 | begin() |
| 93 | { |
| 94 | return m_table.begin(); |
| 95 | } |
| 96 | |
| 97 | const_iterator |
| 98 | end() const |
| 99 | { |
| 100 | return m_table.end(); |
| 101 | } |
| 102 | |
| 103 | iterator |
| 104 | end() |
| 105 | { |
| 106 | return m_table.end(); |
| 107 | } |
| 108 | |
| 109 | size_t |
| 110 | size() const; |
| 111 | |
| 112 | void |
| 113 | clear(); |
| 114 | |
| 115 | private: |
| 116 | ndn::Scheduler m_scheduler; |
| 117 | ndn::time::steady_clock::Duration m_entryLifetime; |
| 118 | ndn::time::steady_clock::Duration m_cleanPeriod; |
| 119 | |
| 120 | InterestContainer m_table; |
| 121 | }; |
| 122 | |
| 123 | } // namespace chronosync |
| 124 | |
| 125 | #endif // CHRONOSYNC_INTEREST_TABLE_HPP |