Yingdi Yu | 1c4ba9a | 2014-08-27 19:05:49 -0700 | [diff] [blame] | 1 | /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /* |
Davide Pesavento | 1af7949 | 2023-09-22 15:45:21 -0400 | [diff] [blame^] | 3 | * Copyright (c) 2012-2023 University of California, Los Angeles |
Yingdi Yu | 1c4ba9a | 2014-08-27 19:05:49 -0700 | [diff] [blame] | 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 | |
Davide Pesavento | 1af7949 | 2023-09-22 15:45:21 -0400 | [diff] [blame^] | 30 | #include <boost/asio/io_context.hpp> |
Davide Pesavento | 780e646 | 2021-02-08 20:58:12 -0500 | [diff] [blame] | 31 | |
Yingdi Yu | 1c4ba9a | 2014-08-27 19:05:49 -0700 | [diff] [blame] | 32 | namespace chronosync { |
| 33 | |
| 34 | /** |
Davide Pesavento | 1af7949 | 2023-09-22 15:45:21 -0400 | [diff] [blame^] | 35 | * @brief A table to keep unsatisfied Sync Interests. |
Yingdi Yu | 1c4ba9a | 2014-08-27 19:05:49 -0700 | [diff] [blame] | 36 | */ |
| 37 | class InterestTable : noncopyable |
| 38 | { |
| 39 | public: |
| 40 | class Error : public std::runtime_error |
| 41 | { |
| 42 | public: |
Davide Pesavento | 780e646 | 2021-02-08 20:58:12 -0500 | [diff] [blame] | 43 | using std::runtime_error::runtime_error; |
Yingdi Yu | 1c4ba9a | 2014-08-27 19:05:49 -0700 | [diff] [blame] | 44 | }; |
| 45 | |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 46 | using iterator = InterestContainer::iterator; |
| 47 | using const_iterator = InterestContainer::const_iterator; |
Yingdi Yu | 1c4ba9a | 2014-08-27 19:05:49 -0700 | [diff] [blame] | 48 | |
| 49 | explicit |
Davide Pesavento | 1af7949 | 2023-09-22 15:45:21 -0400 | [diff] [blame^] | 50 | InterestTable(boost::asio::io_context& io); |
Yingdi Yu | 1c4ba9a | 2014-08-27 19:05:49 -0700 | [diff] [blame] | 51 | |
| 52 | ~InterestTable(); |
| 53 | |
| 54 | /** |
| 55 | * @brief Insert an interest |
| 56 | * |
| 57 | * If the interest already exists in the table, the old interest will be replaced, |
| 58 | * and expire timer will be reset. |
| 59 | * Interests with the same name are counted as the same. |
| 60 | * This method assumes that the sync prefix of all interests are the same |
| 61 | * thus it only compares the digest part. |
| 62 | * |
| 63 | * @param interest Interest to insert. |
| 64 | * @param digest The value of the last digest component. |
| 65 | * @param isKnown false if the digest is an unknown digest. |
Yingdi Yu | 1c4ba9a | 2014-08-27 19:05:49 -0700 | [diff] [blame] | 66 | */ |
Yingdi Yu | 53f5f04 | 2015-01-31 16:33:25 -0800 | [diff] [blame] | 67 | void |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 68 | insert(const Interest& interest, ConstBufferPtr digest, bool isKnown = false); |
Yingdi Yu | 1c4ba9a | 2014-08-27 19:05:49 -0700 | [diff] [blame] | 69 | |
Yingdi Yu | 53f5f04 | 2015-01-31 16:33:25 -0800 | [diff] [blame] | 70 | /// @brief check if an interest with the digest exists in the table |
Yingdi Yu | 1c4ba9a | 2014-08-27 19:05:49 -0700 | [diff] [blame] | 71 | bool |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 72 | has(ConstBufferPtr digest); |
Yingdi Yu | 1c4ba9a | 2014-08-27 19:05:49 -0700 | [diff] [blame] | 73 | |
Yingdi Yu | 53f5f04 | 2015-01-31 16:33:25 -0800 | [diff] [blame] | 74 | /// @brief Delete interest by digest (e.g., when it was satisfied) |
Yingdi Yu | 906c2ea | 2014-10-31 11:24:50 -0700 | [diff] [blame] | 75 | void |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 76 | erase(ConstBufferPtr digest); |
Yingdi Yu | 906c2ea | 2014-10-31 11:24:50 -0700 | [diff] [blame] | 77 | |
Yingdi Yu | 1c4ba9a | 2014-08-27 19:05:49 -0700 | [diff] [blame] | 78 | const_iterator |
| 79 | begin() const |
| 80 | { |
| 81 | return m_table.begin(); |
| 82 | } |
| 83 | |
| 84 | iterator |
| 85 | begin() |
| 86 | { |
| 87 | return m_table.begin(); |
| 88 | } |
| 89 | |
| 90 | const_iterator |
| 91 | end() const |
| 92 | { |
| 93 | return m_table.end(); |
| 94 | } |
| 95 | |
| 96 | iterator |
| 97 | end() |
| 98 | { |
| 99 | return m_table.end(); |
| 100 | } |
| 101 | |
| 102 | size_t |
| 103 | size() const; |
| 104 | |
| 105 | void |
| 106 | clear(); |
| 107 | |
| 108 | private: |
| 109 | ndn::Scheduler m_scheduler; |
Yingdi Yu | 1c4ba9a | 2014-08-27 19:05:49 -0700 | [diff] [blame] | 110 | InterestContainer m_table; |
| 111 | }; |
| 112 | |
| 113 | } // namespace chronosync |
| 114 | |
| 115 | #endif // CHRONOSYNC_INTEREST_TABLE_HPP |