blob: 5b97f0ad7efb2859dc8b3e5690efd25ef552d52d [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
39bool
40InterestTable::insert(shared_ptr<const Interest> interest,
41 ndn::ConstBufferPtr digest,
42 bool isKnown/*=false*/)
43{
44 bool doesExist = erase(digest);
45
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,
55 [=] () { quiteErase(digest); });
Yingdi Yu1c4ba9a2014-08-27 19:05:49 -070056
57 m_table.insert(request);
58
59 return doesExist;
60}
61
Yingdi Yu906c2ea2014-10-31 11:24:50 -070062void
63InterestTable::quiteErase(ndn::ConstBufferPtr digest)
64{
65 erase(digest);
66}
67
Yingdi Yu1c4ba9a2014-08-27 19:05:49 -070068bool
69InterestTable::erase(ndn::ConstBufferPtr digest)
70{
71 InterestContainer::index<hashed>::type::iterator it = m_table.get<hashed>().find(digest);
72 if (it != m_table.get<hashed>().end()) {
73 m_scheduler.cancelEvent((*it)->expirationEvent);
74 m_table.erase(it);
75 return true;
76 }
77 return false;
78}
79
80size_t
81InterestTable::size() const
82{
83 return m_table.size();
84}
85
86void
87InterestTable::clear()
88{
89 for (InterestContainer::iterator it = m_table.begin();
90 it != m_table.end(); it++) {
91 m_scheduler.cancelEvent((*it)->expirationEvent);
92 }
93
94 m_table.clear();
95}
96
97}