blob: bf5685b3c6db52111da7891209fd9bd8d0a77706 [file] [log] [blame]
Yingdi Yu1c4ba9a2014-08-27 19:05:49 -07001/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
2/*
Ashlesh Gawande08784d42017-09-06 23:40:21 -05003 * Copyright (c) 2012-2017 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#ifndef CHRONOSYNC_INTEREST_TABLE_HPP
26#define CHRONOSYNC_INTEREST_TABLE_HPP
27
28#include "interest-container.hpp"
29
Yingdi Yu1c4ba9a2014-08-27 19:05:49 -070030namespace chronosync {
31
32/**
33 * @brief A table to keep unsatisfied Sync Interest
34 */
35class InterestTable : noncopyable
36{
37public:
38 class Error : public std::runtime_error
39 {
40 public:
41 explicit
42 Error(const std::string& what)
43 : std::runtime_error(what)
44 {
45 }
46 };
47
Ashlesh Gawande08784d42017-09-06 23:40:21 -050048 using iterator = InterestContainer::iterator;
49 using const_iterator = InterestContainer::const_iterator;
Yingdi Yu1c4ba9a2014-08-27 19:05:49 -070050
51 explicit
52 InterestTable(boost::asio::io_service& io);
53
54 ~InterestTable();
55
56 /**
57 * @brief Insert an interest
58 *
59 * If the interest already exists in the table, the old interest will be replaced,
60 * and expire timer will be reset.
61 * Interests with the same name are counted as the same.
62 * This method assumes that the sync prefix of all interests are the same
63 * thus it only compares the digest part.
64 *
65 * @param interest Interest to insert.
66 * @param digest The value of the last digest component.
67 * @param isKnown false if the digest is an unknown digest.
Yingdi Yu1c4ba9a2014-08-27 19:05:49 -070068 */
Yingdi Yu53f5f042015-01-31 16:33:25 -080069 void
Ashlesh Gawande08784d42017-09-06 23:40:21 -050070 insert(const Interest& interest, ConstBufferPtr digest, bool isKnown = false);
Yingdi Yu1c4ba9a2014-08-27 19:05:49 -070071
Yingdi Yu53f5f042015-01-31 16:33:25 -080072 /// @brief check if an interest with the digest exists in the table
Yingdi Yu1c4ba9a2014-08-27 19:05:49 -070073 bool
Ashlesh Gawande08784d42017-09-06 23:40:21 -050074 has(ConstBufferPtr digest);
Yingdi Yu1c4ba9a2014-08-27 19:05:49 -070075
Yingdi Yu53f5f042015-01-31 16:33:25 -080076 /// @brief Delete interest by digest (e.g., when it was satisfied)
Yingdi Yu906c2ea2014-10-31 11:24:50 -070077 void
Ashlesh Gawande08784d42017-09-06 23:40:21 -050078 erase(ConstBufferPtr digest);
Yingdi Yu906c2ea2014-10-31 11:24:50 -070079
Yingdi Yu1c4ba9a2014-08-27 19:05:49 -070080 const_iterator
81 begin() const
82 {
83 return m_table.begin();
84 }
85
86 iterator
87 begin()
88 {
89 return m_table.begin();
90 }
91
92 const_iterator
93 end() const
94 {
95 return m_table.end();
96 }
97
98 iterator
99 end()
100 {
101 return m_table.end();
102 }
103
104 size_t
105 size() const;
106
107 void
108 clear();
109
110private:
111 ndn::Scheduler m_scheduler;
Yingdi Yu1c4ba9a2014-08-27 19:05:49 -0700112 InterestContainer m_table;
113};
114
115} // namespace chronosync
116
117#endif // CHRONOSYNC_INTEREST_TABLE_HPP