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 | #include "interest-table.hpp" |
| 26 | |
| 27 | namespace chronosync { |
| 28 | |
Davide Pesavento | 1af7949 | 2023-09-22 15:45:21 -0400 | [diff] [blame] | 29 | InterestTable::InterestTable(boost::asio::io_context& io) |
Yingdi Yu | 1c4ba9a | 2014-08-27 19:05:49 -0700 | [diff] [blame] | 30 | : m_scheduler(io) |
| 31 | { |
| 32 | } |
| 33 | |
| 34 | InterestTable::~InterestTable() |
| 35 | { |
| 36 | clear(); |
| 37 | } |
| 38 | |
Yingdi Yu | 53f5f04 | 2015-01-31 16:33:25 -0800 | [diff] [blame] | 39 | void |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 40 | InterestTable::insert(const Interest& interest, ConstBufferPtr digest, bool isKnown/*=false*/) |
Yingdi Yu | 1c4ba9a | 2014-08-27 19:05:49 -0700 | [diff] [blame] | 41 | { |
Yingdi Yu | 53f5f04 | 2015-01-31 16:33:25 -0800 | [diff] [blame] | 42 | erase(digest); |
Yingdi Yu | 1c4ba9a | 2014-08-27 19:05:49 -0700 | [diff] [blame] | 43 | |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 44 | auto request = make_shared<UnsatisfiedInterest>(interest, digest, isKnown); |
Yingdi Yu | 1c4ba9a | 2014-08-27 19:05:49 -0700 | [diff] [blame] | 45 | |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 46 | time::milliseconds entryLifetime = interest.getInterestLifetime(); |
Yingdi Yu | 1c4ba9a | 2014-08-27 19:05:49 -0700 | [diff] [blame] | 47 | if (entryLifetime < time::milliseconds::zero()) |
| 48 | entryLifetime = ndn::DEFAULT_INTEREST_LIFETIME; |
| 49 | |
Davide Pesavento | d057cf1 | 2019-03-20 23:38:40 -0400 | [diff] [blame] | 50 | request->expirationEvent = m_scheduler.schedule(entryLifetime, [=] { erase(digest); }); |
Yingdi Yu | 1c4ba9a | 2014-08-27 19:05:49 -0700 | [diff] [blame] | 51 | |
| 52 | m_table.insert(request); |
Yingdi Yu | 1c4ba9a | 2014-08-27 19:05:49 -0700 | [diff] [blame] | 53 | } |
| 54 | |
Yingdi Yu | 906c2ea | 2014-10-31 11:24:50 -0700 | [diff] [blame] | 55 | void |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 56 | InterestTable::erase(ConstBufferPtr digest) |
Yingdi Yu | 1c4ba9a | 2014-08-27 19:05:49 -0700 | [diff] [blame] | 57 | { |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 58 | auto it = m_table.get<hashed>().find(digest); |
| 59 | while (it != m_table.get<hashed>().end()) { |
Davide Pesavento | d057cf1 | 2019-03-20 23:38:40 -0400 | [diff] [blame] | 60 | (*it)->expirationEvent.cancel(); |
Yingdi Yu | 1c4ba9a | 2014-08-27 19:05:49 -0700 | [diff] [blame] | 61 | m_table.erase(it); |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 62 | |
| 63 | it = m_table.get<hashed>().find(digest); |
Yingdi Yu | 1c4ba9a | 2014-08-27 19:05:49 -0700 | [diff] [blame] | 64 | } |
Yingdi Yu | 53f5f04 | 2015-01-31 16:33:25 -0800 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | bool |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 68 | InterestTable::has(ConstBufferPtr digest) |
Yingdi Yu | 53f5f04 | 2015-01-31 16:33:25 -0800 | [diff] [blame] | 69 | { |
| 70 | if (m_table.get<hashed>().find(digest) != m_table.get<hashed>().end()) |
| 71 | return true; |
| 72 | else |
| 73 | return false; |
Yingdi Yu | 1c4ba9a | 2014-08-27 19:05:49 -0700 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | size_t |
| 77 | InterestTable::size() const |
| 78 | { |
| 79 | return m_table.size(); |
| 80 | } |
| 81 | |
| 82 | void |
| 83 | InterestTable::clear() |
| 84 | { |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 85 | for (const auto& item : m_table) { |
Davide Pesavento | d057cf1 | 2019-03-20 23:38:40 -0400 | [diff] [blame] | 86 | item->expirationEvent.cancel(); |
Yingdi Yu | 1c4ba9a | 2014-08-27 19:05:49 -0700 | [diff] [blame] | 87 | } |
Yingdi Yu | 1c4ba9a | 2014-08-27 19:05:49 -0700 | [diff] [blame] | 88 | m_table.clear(); |
| 89 | } |
| 90 | |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 91 | } // namespace chronosync |