blob: efe7fef522f8c0073b3311213effba8c6144dcf2 [file] [log] [blame]
Yingdi Yu1c4ba9a2014-08-27 19:05:49 -07001/* -*- 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
27namespace chronosync {
28
29InterestTable::InterestTable(boost::asio::io_service& io)
30 : m_scheduler(io)
31{
32}
33
34InterestTable::~InterestTable()
35{
36 clear();
37}
38
Yingdi Yu53f5f042015-01-31 16:33:25 -080039void
Yingdi Yu1c4ba9a2014-08-27 19:05:49 -070040InterestTable::insert(shared_ptr<const Interest> interest,
41 ndn::ConstBufferPtr digest,
42 bool isKnown/*=false*/)
43{
Yingdi Yu53f5f042015-01-31 16:33:25 -080044 erase(digest);
Yingdi Yu1c4ba9a2014-08-27 19:05:49 -070045
46 UnsatisfiedInterestPtr request =
Yingdi Yu906c2ea2014-10-31 11:24:50 -070047 make_shared<UnsatisfiedInterest>(interest, digest, isKnown);
Yingdi Yu1c4ba9a2014-08-27 19:05:49 -070048
49 time::milliseconds entryLifetime = interest->getInterestLifetime();
50 if (entryLifetime < time::milliseconds::zero())
51 entryLifetime = ndn::DEFAULT_INTEREST_LIFETIME;
52
53 request->expirationEvent =
Yingdi Yu906c2ea2014-10-31 11:24:50 -070054 m_scheduler.scheduleEvent(entryLifetime,
Yingdi Yu53f5f042015-01-31 16:33:25 -080055 [=] () { erase(digest); });
Yingdi Yu1c4ba9a2014-08-27 19:05:49 -070056
57 m_table.insert(request);
Yingdi Yu1c4ba9a2014-08-27 19:05:49 -070058}
59
Yingdi Yu906c2ea2014-10-31 11:24:50 -070060void
Yingdi Yu1c4ba9a2014-08-27 19:05:49 -070061InterestTable::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 Yu1c4ba9a2014-08-27 19:05:49 -070067 }
Yingdi Yu53f5f042015-01-31 16:33:25 -080068}
69
70bool
71InterestTable::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 Yu1c4ba9a2014-08-27 19:05:49 -070077}
78
79size_t
80InterestTable::size() const
81{
82 return m_table.size();
83}
84
85void
86InterestTable::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}