blob: 469e4e4dad90b28ba87afbbc87814c18cec3c6a4 [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 Afanasyevf3c03a92012-05-09 12:00:37 -070051 BOOST_ASSERT (m_table.size () != 0);
52 Interest ret = *m_table.begin ();
53 m_table.erase (m_table.begin ());
54
55 return ret;
Zhenkai Zhu77169cb2012-03-08 16:02:52 -080056}
57
Alexander Afanasyeva022f3d2012-03-11 18:14:48 -070058bool
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -070059SyncInterestTable::insert (DigestConstPtr digest, const string &name)
Alexander Afanasyeva022f3d2012-03-11 18:14:48 -070060{
Alexander Afanasyev03793b42012-05-01 13:03:07 -070061 bool existent = false;
62
Alexander Afanasyeva022f3d2012-03-11 18:14:48 -070063 recursive_mutex::scoped_lock lock (m_mutex);
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -070064 InterestContainer::index<named>::type::iterator it = m_table.get<named> ().find (name);
Alexander Afanasyeva022f3d2012-03-11 18:14:48 -070065 if (it != m_table.end())
Alexander Afanasyev03793b42012-05-01 13:03:07 -070066 {
67 existent = true;
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -070068 m_table.erase (it);
Alexander Afanasyev03793b42012-05-01 13:03:07 -070069 }
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -070070 m_table.insert (Interest (digest, name));
Alexander Afanasyev03793b42012-05-01 13:03:07 -070071
72 return existent;
Zhenkai Zhu77169cb2012-03-08 16:02:52 -080073}
74
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -070075uint32_t
76SyncInterestTable::size () const
Alexander Afanasyeva022f3d2012-03-11 18:14:48 -070077{
78 recursive_mutex::scoped_lock lock (m_mutex);
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -070079 return m_table.size ();
80}
81
82bool
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -070083SyncInterestTable::remove (const string &name)
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -070084{
85 recursive_mutex::scoped_lock lock (m_mutex);
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -070086
87 InterestContainer::index<named>::type::iterator item = m_table.get<named> ().find (name);
88 if (item != m_table.get<named> ().end ())
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -070089 {
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -070090 m_table.get<named> ().erase (name);
91 return true;
92 }
93
94 return false;
95}
96
97bool
98SyncInterestTable::remove (DigestConstPtr digest)
99{
100 recursive_mutex::scoped_lock lock (m_mutex);
101 InterestContainer::index<hashed>::type::iterator item = m_table.get<hashed> ().find (digest);
102 if (item != m_table.get<hashed> ().end ())
103 {
104 m_table.get<hashed> ().erase (digest); // erase all records associated with the digest
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700105 return true;
106 }
107 return false;
108}
109
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700110void SyncInterestTable::expireInterests ()
111{
112 recursive_mutex::scoped_lock lock (m_mutex);
113
114 uint32_t count = 0;
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700115 TimeAbsolute expireTime = TIME_NOW - m_entryLifetime;
116
117 while (m_table.size () > 0)
Alexander Afanasyeva022f3d2012-03-11 18:14:48 -0700118 {
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700119 InterestContainer::index<timed>::type::iterator item = m_table.get<timed> ().begin ();
120
121 if (item->m_time < expireTime)
122 {
123 m_table.get<timed> ().erase (item);
124 count ++;
125 }
126 else
127 break;
Alexander Afanasyeva022f3d2012-03-11 18:14:48 -0700128 }
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700129
130 _LOG_DEBUG ("expireInterests (): expired " << count);
131
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700132 m_scheduler.schedule (TIME_SECONDS (m_checkPeriod),
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700133 bind (&SyncInterestTable::expireInterests, this),
134 0);
Alexander Afanasyeva022f3d2012-03-11 18:14:48 -0700135}
136
Zhenkai Zhu77169cb2012-03-08 16:02:52 -0800137
138}