blob: 6fbf7841483d385c3423044d2d75ea6409aeb924 [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 Afanasyevce001692013-07-14 11:34:41 -070024#include "sync-logging.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 Afanasyeva022f3d2012-03-11 18:14:48 -070048 recursive_mutex::scoped_lock lock (m_mutex);
Alexander Afanasyeva022f3d2012-03-11 18:14:48 -070049
Alexander Afanasyev46eb5262012-05-10 16:30:35 -070050 if (m_table.size () == 0)
51 BOOST_THROW_EXCEPTION (Error::InterestTableIsEmpty ());
52
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -070053 Interest ret = *m_table.begin ();
54 m_table.erase (m_table.begin ());
55
56 return ret;
Zhenkai Zhu77169cb2012-03-08 16:02:52 -080057}
58
Alexander Afanasyeva022f3d2012-03-11 18:14:48 -070059bool
Alexander Afanasyev2b6fbfb2012-05-24 10:57:14 -070060SyncInterestTable::insert (DigestConstPtr digest, const string &name, bool unknownState/*=false*/)
Alexander Afanasyeva022f3d2012-03-11 18:14:48 -070061{
Alexander Afanasyev03793b42012-05-01 13:03:07 -070062 bool existent = false;
63
Alexander Afanasyeva022f3d2012-03-11 18:14:48 -070064 recursive_mutex::scoped_lock lock (m_mutex);
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -070065 InterestContainer::index<named>::type::iterator it = m_table.get<named> ().find (name);
Alexander Afanasyeva022f3d2012-03-11 18:14:48 -070066 if (it != m_table.end())
Alexander Afanasyev03793b42012-05-01 13:03:07 -070067 {
68 existent = true;
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -070069 m_table.erase (it);
Alexander Afanasyev03793b42012-05-01 13:03:07 -070070 }
Alexander Afanasyev2b6fbfb2012-05-24 10:57:14 -070071 m_table.insert (Interest (digest, name, unknownState));
Alexander Afanasyev03793b42012-05-01 13:03:07 -070072
73 return existent;
Zhenkai Zhu77169cb2012-03-08 16:02:52 -080074}
75
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -070076uint32_t
77SyncInterestTable::size () const
Alexander Afanasyeva022f3d2012-03-11 18:14:48 -070078{
79 recursive_mutex::scoped_lock lock (m_mutex);
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -070080 return m_table.size ();
81}
82
83bool
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -070084SyncInterestTable::remove (const string &name)
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -070085{
86 recursive_mutex::scoped_lock lock (m_mutex);
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -070087
88 InterestContainer::index<named>::type::iterator item = m_table.get<named> ().find (name);
89 if (item != m_table.get<named> ().end ())
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -070090 {
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -070091 m_table.get<named> ().erase (name);
92 return true;
93 }
94
95 return false;
96}
97
98bool
99SyncInterestTable::remove (DigestConstPtr digest)
100{
101 recursive_mutex::scoped_lock lock (m_mutex);
102 InterestContainer::index<hashed>::type::iterator item = m_table.get<hashed> ().find (digest);
103 if (item != m_table.get<hashed> ().end ())
104 {
105 m_table.get<hashed> ().erase (digest); // erase all records associated with the digest
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700106 return true;
107 }
108 return false;
109}
110
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700111void SyncInterestTable::expireInterests ()
112{
113 recursive_mutex::scoped_lock lock (m_mutex);
114
115 uint32_t count = 0;
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700116 TimeAbsolute expireTime = TIME_NOW - m_entryLifetime;
117
118 while (m_table.size () > 0)
Alexander Afanasyeva022f3d2012-03-11 18:14:48 -0700119 {
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700120 InterestContainer::index<timed>::type::iterator item = m_table.get<timed> ().begin ();
121
122 if (item->m_time < expireTime)
123 {
124 m_table.get<timed> ().erase (item);
125 count ++;
126 }
127 else
128 break;
Alexander Afanasyeva022f3d2012-03-11 18:14:48 -0700129 }
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700130
131 _LOG_DEBUG ("expireInterests (): expired " << count);
132
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700133 m_scheduler.schedule (TIME_SECONDS (m_checkPeriod),
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700134 bind (&SyncInterestTable::expireInterests, this),
135 0);
Alexander Afanasyeva022f3d2012-03-11 18:14:48 -0700136}
137
Zhenkai Zhu77169cb2012-03-08 16:02:52 -0800138
139}