blob: 9eef231d9b6d69bd5bb3a9a057f4c69a1ee44a82 [file] [log] [blame]
Yingdi Yu1c4ba9a2014-08-27 19:05:49 -07001/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
2/*
Davide Pesavento1af79492023-09-22 15:45:21 -04003 * Copyright (c) 2012-2023 University of California, Los Angeles
Yingdi Yu1c4ba9a2014-08-27 19:05:49 -07004 *
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
27namespace chronosync {
28
Davide Pesavento1af79492023-09-22 15:45:21 -040029InterestTable::InterestTable(boost::asio::io_context& io)
Yingdi Yu1c4ba9a2014-08-27 19:05:49 -070030 : m_scheduler(io)
31{
32}
33
34InterestTable::~InterestTable()
35{
36 clear();
37}
38
Yingdi Yu53f5f042015-01-31 16:33:25 -080039void
Ashlesh Gawande08784d42017-09-06 23:40:21 -050040InterestTable::insert(const Interest& interest, ConstBufferPtr digest, bool isKnown/*=false*/)
Yingdi Yu1c4ba9a2014-08-27 19:05:49 -070041{
Yingdi Yu53f5f042015-01-31 16:33:25 -080042 erase(digest);
Yingdi Yu1c4ba9a2014-08-27 19:05:49 -070043
Ashlesh Gawande08784d42017-09-06 23:40:21 -050044 auto request = make_shared<UnsatisfiedInterest>(interest, digest, isKnown);
Yingdi Yu1c4ba9a2014-08-27 19:05:49 -070045
Ashlesh Gawande08784d42017-09-06 23:40:21 -050046 time::milliseconds entryLifetime = interest.getInterestLifetime();
Yingdi Yu1c4ba9a2014-08-27 19:05:49 -070047 if (entryLifetime < time::milliseconds::zero())
48 entryLifetime = ndn::DEFAULT_INTEREST_LIFETIME;
49
Davide Pesaventod057cf12019-03-20 23:38:40 -040050 request->expirationEvent = m_scheduler.schedule(entryLifetime, [=] { erase(digest); });
Yingdi Yu1c4ba9a2014-08-27 19:05:49 -070051
52 m_table.insert(request);
Yingdi Yu1c4ba9a2014-08-27 19:05:49 -070053}
54
Yingdi Yu906c2ea2014-10-31 11:24:50 -070055void
Ashlesh Gawande08784d42017-09-06 23:40:21 -050056InterestTable::erase(ConstBufferPtr digest)
Yingdi Yu1c4ba9a2014-08-27 19:05:49 -070057{
Ashlesh Gawande08784d42017-09-06 23:40:21 -050058 auto it = m_table.get<hashed>().find(digest);
59 while (it != m_table.get<hashed>().end()) {
Davide Pesaventod057cf12019-03-20 23:38:40 -040060 (*it)->expirationEvent.cancel();
Yingdi Yu1c4ba9a2014-08-27 19:05:49 -070061 m_table.erase(it);
Ashlesh Gawande08784d42017-09-06 23:40:21 -050062
63 it = m_table.get<hashed>().find(digest);
Yingdi Yu1c4ba9a2014-08-27 19:05:49 -070064 }
Yingdi Yu53f5f042015-01-31 16:33:25 -080065}
66
67bool
Ashlesh Gawande08784d42017-09-06 23:40:21 -050068InterestTable::has(ConstBufferPtr digest)
Yingdi Yu53f5f042015-01-31 16:33:25 -080069{
70 if (m_table.get<hashed>().find(digest) != m_table.get<hashed>().end())
71 return true;
72 else
73 return false;
Yingdi Yu1c4ba9a2014-08-27 19:05:49 -070074}
75
76size_t
77InterestTable::size() const
78{
79 return m_table.size();
80}
81
82void
83InterestTable::clear()
84{
Ashlesh Gawande08784d42017-09-06 23:40:21 -050085 for (const auto& item : m_table) {
Davide Pesaventod057cf12019-03-20 23:38:40 -040086 item->expirationEvent.cancel();
Yingdi Yu1c4ba9a2014-08-27 19:05:49 -070087 }
Yingdi Yu1c4ba9a2014-08-27 19:05:49 -070088 m_table.clear();
89}
90
Ashlesh Gawande08784d42017-09-06 23:40:21 -050091} // namespace chronosync