blob: 415b6415f035f670947c34d24c9d932451eb7fe1 [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>
20 * Alexander Afanasyev <alexander.afanasyev@ucla.edu>
21 */
22
23#include "sync-interest-table.h"
24#include "sync-logging.h"
25using namespace std;
26
27INIT_LOGGER ("SyncInterestTable");
28
29namespace Sync
30{
31
akmhoque05d5fcf2014-04-15 14:58:45 -050032SyncInterestTable::SyncInterestTable (boost::asio::io_service& io, ndn::time::system_clock::Duration lifetime)
akmhoque66e66182014-02-21 17:56:03 -060033 : m_entryLifetime (lifetime)
34 , m_scheduler(io)
35{
36 m_scheduler.schedulePeriodicEvent (ndn::time::seconds (4), ndn::time::seconds (4),
37 ndn::bind(&SyncInterestTable::expireInterests, this));
38}
39
40SyncInterestTable::~SyncInterestTable ()
41{
42}
43
44Interest
45SyncInterestTable::pop ()
46{
47 if (m_table.size () == 0)
48 BOOST_THROW_EXCEPTION (Error::InterestTableIsEmpty ());
49
50 Interest ret = *m_table.begin ();
51 m_table.erase (m_table.begin ());
52
53 return ret;
54}
55
56bool
57SyncInterestTable::insert (DigestConstPtr digest, const string &name, bool unknownState/*=false*/)
58{
59 bool existent = false;
60
61 InterestContainer::index<named>::type::iterator it = m_table.get<named> ().find (name);
62 if (it != m_table.end())
63 {
64 existent = true;
65 m_table.erase (it);
66 }
67 m_table.insert (Interest (digest, name, unknownState));
68
69 return existent;
70}
71
72uint32_t
73SyncInterestTable::size () const
74{
75 return m_table.size ();
76}
77
78bool
79SyncInterestTable::remove (const string &name)
80{
81 InterestContainer::index<named>::type::iterator item = m_table.get<named> ().find (name);
82 if (item != m_table.get<named> ().end ())
83 {
84 m_table.get<named> ().erase (name);
85 return true;
86 }
87
88 return false;
89}
90
91bool
92SyncInterestTable::remove (DigestConstPtr digest)
93{
94 InterestContainer::index<hashed>::type::iterator item = m_table.get<hashed> ().find (digest);
95 if (item != m_table.get<hashed> ().end ())
96 {
97 m_table.get<hashed> ().erase (digest); // erase all records associated with the digest
98 return true;
99 }
100 return false;
101}
102
103void SyncInterestTable::expireInterests ()
104{
105 uint32_t count = 0;
akmhoque05d5fcf2014-04-15 14:58:45 -0500106 ndn::time::system_clock::TimePoint expireTime = ndn::time::system_clock::now() - m_entryLifetime;
akmhoque66e66182014-02-21 17:56:03 -0600107
108 while (m_table.size () > 0)
109 {
110 InterestContainer::index<timed>::type::iterator item = m_table.get<timed> ().begin ();
111
112 if (item->m_time <= expireTime)
113 {
114 m_table.get<timed> ().erase (item);
115 count ++;
116 }
117 else
118 break;
119 }
120}
121
122
123}