blob: 1fc1552909be98ab20c414acee7550cbafc7676c [file] [log] [blame]
akmhoque66e66182014-02-21 17:56:03 -06001/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
2/*
3 * Copyright (c) 2012 University of California, Los Angeles
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation;
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 *
18 * Author: Zhenkai Zhu <zhenkai@cs.ucla.edu>
19 * Chaoyi Bian <bcy@pku.edu.cn>
Alexander Afanasyeva7cc43e2014-06-19 14:11:16 -070020 * Alexander Afanasyev <alexander.afanasyev@ucla.edu>
akmhoque66e66182014-02-21 17:56:03 -060021 */
22
23#include "sync-interest-table.h"
24#include "sync-logging.h"
25using namespace std;
26
Alexander Afanasyeva7cc43e2014-06-19 14:11:16 -070027INIT_LOGGER("SyncInterestTable");
akmhoque66e66182014-02-21 17:56:03 -060028
29namespace Sync
30{
31
Alexander Afanasyeva7cc43e2014-06-19 14:11:16 -070032SyncInterestTable::SyncInterestTable(boost::asio::io_service& io, ndn::time::system_clock::Duration lifetime)
33 : m_entryLifetime(lifetime)
akmhoque66e66182014-02-21 17:56:03 -060034 , m_scheduler(io)
35{
Alexander Afanasyeva7cc43e2014-06-19 14:11:16 -070036 m_scheduler.scheduleEvent(ndn::time::seconds(4),
37 ndn::bind(&SyncInterestTable::expireInterests, this));
akmhoque66e66182014-02-21 17:56:03 -060038}
39
Alexander Afanasyeva7cc43e2014-06-19 14:11:16 -070040SyncInterestTable::~SyncInterestTable()
akmhoque66e66182014-02-21 17:56:03 -060041{
42}
43
44Interest
Alexander Afanasyeva7cc43e2014-06-19 14:11:16 -070045SyncInterestTable::pop()
akmhoque66e66182014-02-21 17:56:03 -060046{
Alexander Afanasyeva7cc43e2014-06-19 14:11:16 -070047 if (m_table.size() == 0)
48 BOOST_THROW_EXCEPTION(Error::InterestTableIsEmpty());
akmhoque66e66182014-02-21 17:56:03 -060049
Alexander Afanasyeva7cc43e2014-06-19 14:11:16 -070050 Interest ret = *m_table.begin();
51 m_table.erase(m_table.begin());
akmhoque66e66182014-02-21 17:56:03 -060052
53 return ret;
54}
55
56bool
Alexander Afanasyeva7cc43e2014-06-19 14:11:16 -070057SyncInterestTable::insert(DigestConstPtr digest, const string &name, bool unknownState/*=false*/)
akmhoque66e66182014-02-21 17:56:03 -060058{
59 bool existent = false;
Alexander Afanasyeva7cc43e2014-06-19 14:11:16 -070060
61 InterestContainer::index<named>::type::iterator it = m_table.get<named>().find(name);
akmhoque66e66182014-02-21 17:56:03 -060062 if (it != m_table.end())
63 {
64 existent = true;
Alexander Afanasyeva7cc43e2014-06-19 14:11:16 -070065 m_table.erase(it);
akmhoque66e66182014-02-21 17:56:03 -060066 }
Alexander Afanasyeva7cc43e2014-06-19 14:11:16 -070067 m_table.insert(Interest(digest, name, unknownState));
akmhoque66e66182014-02-21 17:56:03 -060068
69 return existent;
70}
71
72uint32_t
Alexander Afanasyeva7cc43e2014-06-19 14:11:16 -070073SyncInterestTable::size() const
akmhoque66e66182014-02-21 17:56:03 -060074{
Alexander Afanasyeva7cc43e2014-06-19 14:11:16 -070075 return m_table.size();
akmhoque66e66182014-02-21 17:56:03 -060076}
77
78bool
Alexander Afanasyeva7cc43e2014-06-19 14:11:16 -070079SyncInterestTable::remove(const string &name)
akmhoque66e66182014-02-21 17:56:03 -060080{
Alexander Afanasyeva7cc43e2014-06-19 14:11:16 -070081 InterestContainer::index<named>::type::iterator item = m_table.get<named>().find(name);
82 if (item != m_table.get<named>().end())
akmhoque66e66182014-02-21 17:56:03 -060083 {
Alexander Afanasyeva7cc43e2014-06-19 14:11:16 -070084 m_table.get<named>().erase(name);
akmhoque66e66182014-02-21 17:56:03 -060085 return true;
86 }
87
88 return false;
89}
90
91bool
Alexander Afanasyeva7cc43e2014-06-19 14:11:16 -070092SyncInterestTable::remove(DigestConstPtr digest)
akmhoque66e66182014-02-21 17:56:03 -060093{
Alexander Afanasyeva7cc43e2014-06-19 14:11:16 -070094 InterestContainer::index<hashed>::type::iterator item = m_table.get<hashed>().find(digest);
95 if (item != m_table.get<hashed>().end())
akmhoque66e66182014-02-21 17:56:03 -060096 {
Alexander Afanasyeva7cc43e2014-06-19 14:11:16 -070097 m_table.get<hashed>().erase(digest); // erase all records associated with the digest
akmhoque66e66182014-02-21 17:56:03 -060098 return true;
99 }
100 return false;
101}
102
Alexander Afanasyeva7cc43e2014-06-19 14:11:16 -0700103void SyncInterestTable::expireInterests()
104{
akmhoque66e66182014-02-21 17:56:03 -0600105 uint32_t count = 0;
akmhoque05d5fcf2014-04-15 14:58:45 -0500106 ndn::time::system_clock::TimePoint expireTime = ndn::time::system_clock::now() - m_entryLifetime;
Alexander Afanasyeva7cc43e2014-06-19 14:11:16 -0700107
108 while (m_table.size() > 0)
akmhoque66e66182014-02-21 17:56:03 -0600109 {
Alexander Afanasyeva7cc43e2014-06-19 14:11:16 -0700110 InterestContainer::index<timed>::type::iterator item = m_table.get<timed>().begin();
111
akmhoque66e66182014-02-21 17:56:03 -0600112 if (item->m_time <= expireTime)
113 {
Alexander Afanasyeva7cc43e2014-06-19 14:11:16 -0700114 m_table.get<timed>().erase(item);
akmhoque66e66182014-02-21 17:56:03 -0600115 count ++;
116 }
117 else
118 break;
119 }
Alexander Afanasyeva7cc43e2014-06-19 14:11:16 -0700120
121 m_scheduler.scheduleEvent(ndn::time::seconds(4),
122 ndn::bind(&SyncInterestTable::expireInterests, this));
akmhoque66e66182014-02-21 17:56:03 -0600123}
124
125
126}