blob: ef7193ad0d50ca84b87fb4ea8740a0b15ca3b8d3 [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 =
47 boost::make_shared<UnsatisfiedInterest>(interest, digest, isKnown);
48
49 time::milliseconds entryLifetime = interest->getInterestLifetime();
50 if (entryLifetime < time::milliseconds::zero())
51 entryLifetime = ndn::DEFAULT_INTEREST_LIFETIME;
52
53 request->expirationEvent =
54 m_scheduler.scheduleEvent(entryLifetime, ndn::bind(&InterestTable::erase, this, digest));
55
56 m_table.insert(request);
57
58 return doesExist;
59}
60
61bool
62InterestTable::erase(ndn::ConstBufferPtr digest)
63{
64 InterestContainer::index<hashed>::type::iterator it = m_table.get<hashed>().find(digest);
65 if (it != m_table.get<hashed>().end()) {
66 m_scheduler.cancelEvent((*it)->expirationEvent);
67 m_table.erase(it);
68 return true;
69 }
70 return false;
71}
72
73size_t
74InterestTable::size() const
75{
76 return m_table.size();
77}
78
79void
80InterestTable::clear()
81{
82 for (InterestContainer::iterator it = m_table.begin();
83 it != m_table.end(); it++) {
84 m_scheduler.cancelEvent((*it)->expirationEvent);
85 }
86
87 m_table.clear();
88}
89
90}