Zhenkai Zhu | 406f37a | 2012-03-05 20:23:20 -0800 | [diff] [blame] | 1 | /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /* |
Alexander Afanasyev | 8722d87 | 2014-07-02 13:00:29 -0700 | [diff] [blame] | 3 | * Copyright (c) 2012-2014 University of California, Los Angeles |
Zhenkai Zhu | 406f37a | 2012-03-05 20:23:20 -0800 | [diff] [blame] | 4 | * |
Alexander Afanasyev | 8722d87 | 2014-07-02 13:00:29 -0700 | [diff] [blame] | 5 | * This file is part of ChronoSync, synchronization library for distributed realtime |
| 6 | * applications for NDN. |
Zhenkai Zhu | 406f37a | 2012-03-05 20:23:20 -0800 | [diff] [blame] | 7 | * |
Alexander Afanasyev | 8722d87 | 2014-07-02 13:00:29 -0700 | [diff] [blame] | 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. |
Zhenkai Zhu | 406f37a | 2012-03-05 20:23:20 -0800 | [diff] [blame] | 11 | * |
Alexander Afanasyev | 8722d87 | 2014-07-02 13:00:29 -0700 | [diff] [blame] | 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. |
Zhenkai Zhu | 406f37a | 2012-03-05 20:23:20 -0800 | [diff] [blame] | 15 | * |
Alexander Afanasyev | 8722d87 | 2014-07-02 13:00:29 -0700 | [diff] [blame] | 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> |
Zhenkai Zhu | 406f37a | 2012-03-05 20:23:20 -0800 | [diff] [blame] | 22 | */ |
| 23 | |
Zhenkai Zhu | 1ac6f80 | 2012-03-06 17:40:27 -0800 | [diff] [blame] | 24 | #ifndef SYNC_INTEREST_TABLE_H |
| 25 | #define SYNC_INTEREST_TABLE_H |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 26 | |
Yingdi Yu | 7c64e5c | 2014-04-30 14:06:37 -0700 | [diff] [blame] | 27 | #include <ndn-cxx/util/scheduler.hpp> |
Alexander Afanasyev | 531803b | 2014-02-05 15:57:35 -0800 | [diff] [blame] | 28 | |
Zhenkai Zhu | 406f37a | 2012-03-05 20:23:20 -0800 | [diff] [blame] | 29 | #include <string> |
Alexander Afanasyev | a585803 | 2012-03-09 15:55:10 -0800 | [diff] [blame] | 30 | #include <vector> |
Alexander Afanasyev | 531803b | 2014-02-05 15:57:35 -0800 | [diff] [blame] | 31 | |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 32 | #include "sync-digest.h" |
| 33 | #include "sync-interest-container.h" |
Zhenkai Zhu | 406f37a | 2012-03-05 20:23:20 -0800 | [diff] [blame] | 34 | |
Zhenkai Zhu | 406f37a | 2012-03-05 20:23:20 -0800 | [diff] [blame] | 35 | namespace Sync { |
| 36 | |
| 37 | /** |
| 38 | * \ingroup sync |
| 39 | * @brief A table to keep unanswered Sync Interest |
Yingdi Yu | 7c64e5c | 2014-04-30 14:06:37 -0700 | [diff] [blame] | 40 | * all access operation to the table should grab the |
Zhenkai Zhu | 77169cb | 2012-03-08 16:02:52 -0800 | [diff] [blame] | 41 | * mutex first |
Zhenkai Zhu | 406f37a | 2012-03-05 20:23:20 -0800 | [diff] [blame] | 42 | */ |
| 43 | class SyncInterestTable |
| 44 | { |
Zhenkai Zhu | 406f37a | 2012-03-05 20:23:20 -0800 | [diff] [blame] | 45 | public: |
Yingdi Yu | 6e1c9cd | 2014-03-25 10:26:54 -0700 | [diff] [blame] | 46 | SyncInterestTable (boost::asio::io_service& io, ndn::time::system_clock::Duration lifetime); |
Alexander Afanasyev | a022f3d | 2012-03-11 18:14:48 -0700 | [diff] [blame] | 47 | ~SyncInterestTable (); |
Zhenkai Zhu | 77169cb | 2012-03-08 16:02:52 -0800 | [diff] [blame] | 48 | |
Alexander Afanasyev | a022f3d | 2012-03-11 18:14:48 -0700 | [diff] [blame] | 49 | /** |
| 50 | * @brief Insert an interest, if interest already exists, update the |
| 51 | * timestamp |
| 52 | */ |
Alexander Afanasyev | 860e6fe | 2012-03-15 17:30:31 -0700 | [diff] [blame] | 53 | bool |
Alexander Afanasyev | 2b6fbfb | 2012-05-24 10:57:14 -0700 | [diff] [blame] | 54 | insert (DigestConstPtr interest, const std::string &name, bool unknownState=false); |
Alexander Afanasyev | 860e6fe | 2012-03-15 17:30:31 -0700 | [diff] [blame] | 55 | |
| 56 | /** |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 57 | * @brief Remove interest by digest (e.g., when it was satisfied) |
Alexander Afanasyev | 860e6fe | 2012-03-15 17:30:31 -0700 | [diff] [blame] | 58 | */ |
| 59 | bool |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 60 | remove (DigestConstPtr interest); |
Zhenkai Zhu | 01733f0 | 2012-03-06 13:56:26 -0800 | [diff] [blame] | 61 | |
Alexander Afanasyev | a022f3d | 2012-03-11 18:14:48 -0700 | [diff] [blame] | 62 | /** |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 63 | * @brief Remove interest by name (e.g., when it was satisfied) |
Alexander Afanasyev | a022f3d | 2012-03-11 18:14:48 -0700 | [diff] [blame] | 64 | */ |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 65 | bool |
| 66 | remove (const std::string &name); |
Yingdi Yu | 7c64e5c | 2014-04-30 14:06:37 -0700 | [diff] [blame] | 67 | |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 68 | /** |
| 69 | * @brief pop a non-expired Interest from PIT |
| 70 | */ |
| 71 | Interest |
| 72 | pop (); |
Zhenkai Zhu | 406f37a | 2012-03-05 20:23:20 -0800 | [diff] [blame] | 73 | |
Alexander Afanasyev | 860e6fe | 2012-03-15 17:30:31 -0700 | [diff] [blame] | 74 | uint32_t |
| 75 | size () const; |
| 76 | |
Zhenkai Zhu | 77169cb | 2012-03-08 16:02:52 -0800 | [diff] [blame] | 77 | private: |
Alexander Afanasyev | a022f3d | 2012-03-11 18:14:48 -0700 | [diff] [blame] | 78 | /** |
| 79 | * @brief periodically called to expire Interest |
| 80 | */ |
Alexander Afanasyev | 860e6fe | 2012-03-15 17:30:31 -0700 | [diff] [blame] | 81 | void |
| 82 | expireInterests (); |
Zhenkai Zhu | 77169cb | 2012-03-08 16:02:52 -0800 | [diff] [blame] | 83 | |
| 84 | private: |
Yingdi Yu | 6e1c9cd | 2014-03-25 10:26:54 -0700 | [diff] [blame] | 85 | ndn::time::system_clock::Duration m_entryLifetime; |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 86 | InterestContainer m_table; |
Zhenkai Zhu | 77169cb | 2012-03-08 16:02:52 -0800 | [diff] [blame] | 87 | |
Alexander Afanasyev | 531803b | 2014-02-05 15:57:35 -0800 | [diff] [blame] | 88 | ndn::Scheduler m_scheduler; |
Zhenkai Zhu | 406f37a | 2012-03-05 20:23:20 -0800 | [diff] [blame] | 89 | }; |
| 90 | |
Alexander Afanasyev | 46eb526 | 2012-05-10 16:30:35 -0700 | [diff] [blame] | 91 | namespace Error { |
| 92 | struct InterestTableIsEmpty : virtual boost::exception, virtual std::exception { }; |
| 93 | } |
| 94 | |
Zhenkai Zhu | 406f37a | 2012-03-05 20:23:20 -0800 | [diff] [blame] | 95 | } // Sync |
| 96 | |
Zhenkai Zhu | 1ac6f80 | 2012-03-06 17:40:27 -0800 | [diff] [blame] | 97 | #endif // SYNC_INTEREST_TABLE_H |