blob: 3a08fbdb04e7f538b3912be732c7cda196e48275 [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>
19 * 卞超轶 Chaoyi Bian <bcy@pku.edu.cn>
20 * 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 Afanasyeva022f3d2012-03-11 18:14:48 -070033SyncInterestTable::SyncInterestTable ()
Chaoyi Bian44fff0c2012-03-07 21:07:22 -080034{
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -070035 m_scheduler.schedule (posix_time::seconds (m_checkPeriod),
36 bind (&SyncInterestTable::expireInterests, this),
37 0);
Chaoyi Bian44fff0c2012-03-07 21:07:22 -080038}
39
Alexander Afanasyeva022f3d2012-03-11 18:14:48 -070040SyncInterestTable::~SyncInterestTable ()
Chaoyi Bian44fff0c2012-03-07 21:07:22 -080041{
Chaoyi Bian44fff0c2012-03-07 21:07:22 -080042}
43
Alexander Afanasyeva022f3d2012-03-11 18:14:48 -070044vector<string>
45SyncInterestTable::fetchAll ()
46{
Alexander Afanasyeva022f3d2012-03-11 18:14:48 -070047 recursive_mutex::scoped_lock lock (m_mutex);
48
49 vector<string> entries;
50 for (unordered_map<string, time_t>::iterator it = m_table.begin();
51 it != m_table.end();
52 ++it)
53 {
54 entries.push_back(it->first);
55 }
56 m_table.clear ();
57
58 return entries;
Zhenkai Zhu77169cb2012-03-08 16:02:52 -080059}
60
Alexander Afanasyeva022f3d2012-03-11 18:14:48 -070061bool
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -070062SyncInterestTable::insert(const string &interest)
Alexander Afanasyeva022f3d2012-03-11 18:14:48 -070063{
64 recursive_mutex::scoped_lock lock (m_mutex);
65 TableContainer::iterator it = m_table.find (interest);
66 if (it != m_table.end())
67 m_table.erase(it);
68 time_t currentTime = time(0);
69 m_table.insert (make_pair(interest, currentTime));
Zhenkai Zhu77169cb2012-03-08 16:02:52 -080070}
71
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -070072uint32_t
73SyncInterestTable::size () const
Alexander Afanasyeva022f3d2012-03-11 18:14:48 -070074{
75 recursive_mutex::scoped_lock lock (m_mutex);
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -070076 return m_table.size ();
77}
78
79bool
80SyncInterestTable::remove (const std::string &interest)
81{
82 recursive_mutex::scoped_lock lock (m_mutex);
83 TableContainer::iterator item = m_table.find (interest);
84 if (item != m_table.end ())
85 {
86 m_table.erase (item);
87 return true;
88 }
89 return false;
90}
91
92
93void SyncInterestTable::expireInterests ()
94{
95 recursive_mutex::scoped_lock lock (m_mutex);
96
97 uint32_t count = 0;
Alexander Afanasyeva022f3d2012-03-11 18:14:48 -070098 time_t currentTime = time(0);
99 TableContainer::iterator it = m_table.begin ();
100 while (it != m_table.end())
101 {
102 time_t timestamp = it->second;
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700103 if (currentTime - timestamp > m_checkPeriod)
104 {
105 it = m_table.erase(it);
106 count ++;
107 }
Alexander Afanasyeva022f3d2012-03-11 18:14:48 -0700108 else
109 ++it;
110 }
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700111
112 _LOG_DEBUG ("expireInterests (): expired " << count);
113
114 m_scheduler.schedule (posix_time::seconds (m_checkPeriod),
115 bind (&SyncInterestTable::expireInterests, this),
116 0);
Alexander Afanasyeva022f3d2012-03-11 18:14:48 -0700117}
118
Zhenkai Zhu77169cb2012-03-08 16:02:52 -0800119
120}