Yingdi Yu | 1c4ba9a | 2014-08-27 19:05:49 -0700 | [diff] [blame] | 1 | /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /* |
| 3 | * Copyright (c) 2012-2014 University of California, Los Angeles |
| 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 | |
| 29 | InterestTable::InterestTable(boost::asio::io_service& io) |
| 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 |
Yingdi Yu | 1c4ba9a | 2014-08-27 19:05:49 -0700 | [diff] [blame] | 40 | InterestTable::insert(shared_ptr<const Interest> interest, |
| 41 | ndn::ConstBufferPtr digest, |
| 42 | bool isKnown/*=false*/) |
| 43 | { |
Yingdi Yu | 53f5f04 | 2015-01-31 16:33:25 -0800 | [diff] [blame] | 44 | erase(digest); |
Yingdi Yu | 1c4ba9a | 2014-08-27 19:05:49 -0700 | [diff] [blame] | 45 | |
| 46 | UnsatisfiedInterestPtr request = |
Yingdi Yu | 906c2ea | 2014-10-31 11:24:50 -0700 | [diff] [blame] | 47 | make_shared<UnsatisfiedInterest>(interest, digest, isKnown); |
Yingdi Yu | 1c4ba9a | 2014-08-27 19:05:49 -0700 | [diff] [blame] | 48 | |
| 49 | time::milliseconds entryLifetime = interest->getInterestLifetime(); |
| 50 | if (entryLifetime < time::milliseconds::zero()) |
| 51 | entryLifetime = ndn::DEFAULT_INTEREST_LIFETIME; |
| 52 | |
| 53 | request->expirationEvent = |
Yingdi Yu | 906c2ea | 2014-10-31 11:24:50 -0700 | [diff] [blame] | 54 | m_scheduler.scheduleEvent(entryLifetime, |
Yingdi Yu | 53f5f04 | 2015-01-31 16:33:25 -0800 | [diff] [blame] | 55 | [=] () { erase(digest); }); |
Yingdi Yu | 1c4ba9a | 2014-08-27 19:05:49 -0700 | [diff] [blame] | 56 | |
| 57 | m_table.insert(request); |
Yingdi Yu | 1c4ba9a | 2014-08-27 19:05:49 -0700 | [diff] [blame] | 58 | } |
| 59 | |
Yingdi Yu | 906c2ea | 2014-10-31 11:24:50 -0700 | [diff] [blame] | 60 | void |
Yingdi Yu | 1c4ba9a | 2014-08-27 19:05:49 -0700 | [diff] [blame] | 61 | InterestTable::erase(ndn::ConstBufferPtr digest) |
| 62 | { |
| 63 | InterestContainer::index<hashed>::type::iterator it = m_table.get<hashed>().find(digest); |
| 64 | if (it != m_table.get<hashed>().end()) { |
| 65 | m_scheduler.cancelEvent((*it)->expirationEvent); |
| 66 | m_table.erase(it); |
Yingdi Yu | 1c4ba9a | 2014-08-27 19:05:49 -0700 | [diff] [blame] | 67 | } |
Yingdi Yu | 53f5f04 | 2015-01-31 16:33:25 -0800 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | bool |
| 71 | InterestTable::has(ndn::ConstBufferPtr digest) |
| 72 | { |
| 73 | if (m_table.get<hashed>().find(digest) != m_table.get<hashed>().end()) |
| 74 | return true; |
| 75 | else |
| 76 | return false; |
Yingdi Yu | 1c4ba9a | 2014-08-27 19:05:49 -0700 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | size_t |
| 80 | InterestTable::size() const |
| 81 | { |
| 82 | return m_table.size(); |
| 83 | } |
| 84 | |
| 85 | void |
| 86 | InterestTable::clear() |
| 87 | { |
| 88 | for (InterestContainer::iterator it = m_table.begin(); |
| 89 | it != m_table.end(); it++) { |
| 90 | m_scheduler.cancelEvent((*it)->expirationEvent); |
| 91 | } |
| 92 | |
| 93 | m_table.clear(); |
| 94 | } |
| 95 | |
| 96 | } |