blob: a05dba3b81543e0dd5e0f86ed47fd4a81e5ff639 [file] [log] [blame]
Zhenkai Zhu406f37a2012-03-05 20:23:20 -08001/* -*- 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>
Chaoyi Bian3e1eb162012-04-03 16:59:32 -070019 * Chaoyi Bian <bcy@pku.edu.cn>
Zhenkai Zhu406f37a2012-03-05 20:23:20 -080020 * Alexander Afanasyev <alexander.afanasyev@ucla.edu>
21 */
Chaoyi Bian44fff0c2012-03-07 21:07:22 -080022
23#include "sync-interest-table.h"
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -070024#include "sync-log.h"
Chaoyi Bian44fff0c2012-03-07 21:07:22 -080025using namespace std;
26using namespace boost;
27
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -070028INIT_LOGGER ("SyncInterestTable");
29
Chaoyi Bian44fff0c2012-03-07 21:07:22 -080030namespace Sync
31{
32
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -070033SyncInterestTable::SyncInterestTable (TimeDuration lifetime)
34 : m_entryLifetime (lifetime)
Chaoyi Bian44fff0c2012-03-07 21:07:22 -080035{
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -070036 m_scheduler.schedule (TIME_SECONDS (m_checkPeriod),
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -070037 bind (&SyncInterestTable::expireInterests, this),
38 0);
Chaoyi Bian44fff0c2012-03-07 21:07:22 -080039}
40
Alexander Afanasyeva022f3d2012-03-11 18:14:48 -070041SyncInterestTable::~SyncInterestTable ()
Chaoyi Bian44fff0c2012-03-07 21:07:22 -080042{
Chaoyi Bian44fff0c2012-03-07 21:07:22 -080043}
44
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -070045Interest
46SyncInterestTable::pop ()
Alexander Afanasyeva022f3d2012-03-11 18:14:48 -070047{
Alexander Afanasyevd3c501e2012-03-15 17:52:34 -070048 expireInterests ();
Alexander Afanasyeva022f3d2012-03-11 18:14:48 -070049 recursive_mutex::scoped_lock lock (m_mutex);
Alexander Afanasyeva022f3d2012-03-11 18:14:48 -070050
Alexander Afanasyev46eb5262012-05-10 16:30:35 -070051 if (m_table.size () == 0)
52 BOOST_THROW_EXCEPTION (Error::InterestTableIsEmpty ());
53
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -070054 Interest ret = *m_table.begin ();
55 m_table.erase (m_table.begin ());
56
57 return ret;
Zhenkai Zhu77169cb2012-03-08 16:02:52 -080058}
59
Alexander Afanasyeva022f3d2012-03-11 18:14:48 -070060bool
Alexander Afanasyev2b6fbfb2012-05-24 10:57:14 -070061SyncInterestTable::insert (DigestConstPtr digest, const string &name, bool unknownState/*=false*/)
Alexander Afanasyeva022f3d2012-03-11 18:14:48 -070062{
Alexander Afanasyev03793b42012-05-01 13:03:07 -070063 bool existent = false;
64
Alexander Afanasyeva022f3d2012-03-11 18:14:48 -070065 recursive_mutex::scoped_lock lock (m_mutex);
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -070066 InterestContainer::index<named>::type::iterator it = m_table.get<named> ().find (name);
Alexander Afanasyeva022f3d2012-03-11 18:14:48 -070067 if (it != m_table.end())
Alexander Afanasyev03793b42012-05-01 13:03:07 -070068 {
69 existent = true;
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -070070 m_table.erase (it);
Alexander Afanasyev03793b42012-05-01 13:03:07 -070071 }
Alexander Afanasyev2b6fbfb2012-05-24 10:57:14 -070072 m_table.insert (Interest (digest, name, unknownState));
Alexander Afanasyev03793b42012-05-01 13:03:07 -070073
74 return existent;
Zhenkai Zhu77169cb2012-03-08 16:02:52 -080075}
76
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -070077uint32_t
78SyncInterestTable::size () const
Alexander Afanasyeva022f3d2012-03-11 18:14:48 -070079{
80 recursive_mutex::scoped_lock lock (m_mutex);
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -070081 return m_table.size ();
82}
83
84bool
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -070085SyncInterestTable::remove (const string &name)
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -070086{
87 recursive_mutex::scoped_lock lock (m_mutex);
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -070088
89 InterestContainer::index<named>::type::iterator item = m_table.get<named> ().find (name);
90 if (item != m_table.get<named> ().end ())
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -070091 {
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -070092 m_table.get<named> ().erase (name);
93 return true;
94 }
95
96 return false;
97}
98
99bool
100SyncInterestTable::remove (DigestConstPtr digest)
101{
102 recursive_mutex::scoped_lock lock (m_mutex);
103 InterestContainer::index<hashed>::type::iterator item = m_table.get<hashed> ().find (digest);
104 if (item != m_table.get<hashed> ().end ())
105 {
106 m_table.get<hashed> ().erase (digest); // erase all records associated with the digest
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700107 return true;
108 }
109 return false;
110}
111
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700112void SyncInterestTable::expireInterests ()
113{
114 recursive_mutex::scoped_lock lock (m_mutex);
115
116 uint32_t count = 0;
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700117 TimeAbsolute expireTime = TIME_NOW - m_entryLifetime;
118
119 while (m_table.size () > 0)
Alexander Afanasyeva022f3d2012-03-11 18:14:48 -0700120 {
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700121 InterestContainer::index<timed>::type::iterator item = m_table.get<timed> ().begin ();
122
123 if (item->m_time < expireTime)
124 {
125 m_table.get<timed> ().erase (item);
126 count ++;
127 }
128 else
129 break;
Alexander Afanasyeva022f3d2012-03-11 18:14:48 -0700130 }
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700131
132 _LOG_DEBUG ("expireInterests (): expired " << count);
133
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700134 m_scheduler.schedule (TIME_SECONDS (m_checkPeriod),
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700135 bind (&SyncInterestTable::expireInterests, this),
136 0);
Alexander Afanasyeva022f3d2012-03-11 18:14:48 -0700137}
138
Zhenkai Zhu77169cb2012-03-08 16:02:52 -0800139
140}