blob: a9ed2654c92186b7c74e7d32dec4bf8aabe2e1d2 [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 Afanasyeva022f3d2012-03-11 18:14:48 -070033SyncInterestTable::SyncInterestTable ()
Chaoyi Bian44fff0c2012-03-07 21:07:22 -080034{
Alexander Afanasyev181d7e52012-04-09 13:54:11 -070035 m_scheduler.schedule (TIME_SECONDS (4),
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -070036 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 Afanasyevd3c501e2012-03-15 17:52:34 -070047 expireInterests ();
Alexander Afanasyeva022f3d2012-03-11 18:14:48 -070048 recursive_mutex::scoped_lock lock (m_mutex);
49
50 vector<string> entries;
51 for (unordered_map<string, time_t>::iterator it = m_table.begin();
52 it != m_table.end();
53 ++it)
54 {
55 entries.push_back(it->first);
56 }
57 m_table.clear ();
58
59 return entries;
Zhenkai Zhu77169cb2012-03-08 16:02:52 -080060}
61
Alexander Afanasyeva022f3d2012-03-11 18:14:48 -070062bool
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -070063SyncInterestTable::insert(const string &interest)
Alexander Afanasyeva022f3d2012-03-11 18:14:48 -070064{
65 recursive_mutex::scoped_lock lock (m_mutex);
66 TableContainer::iterator it = m_table.find (interest);
67 if (it != m_table.end())
68 m_table.erase(it);
69 time_t currentTime = time(0);
70 m_table.insert (make_pair(interest, currentTime));
Zhenkai Zhu77169cb2012-03-08 16:02:52 -080071}
72
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -070073uint32_t
74SyncInterestTable::size () const
Alexander Afanasyeva022f3d2012-03-11 18:14:48 -070075{
76 recursive_mutex::scoped_lock lock (m_mutex);
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -070077 return m_table.size ();
78}
79
80bool
81SyncInterestTable::remove (const std::string &interest)
82{
83 recursive_mutex::scoped_lock lock (m_mutex);
84 TableContainer::iterator item = m_table.find (interest);
85 if (item != m_table.end ())
86 {
87 m_table.erase (item);
88 return true;
89 }
90 return false;
91}
92
93
94void SyncInterestTable::expireInterests ()
95{
96 recursive_mutex::scoped_lock lock (m_mutex);
97
98 uint32_t count = 0;
Alexander Afanasyeva022f3d2012-03-11 18:14:48 -070099 time_t currentTime = time(0);
100 TableContainer::iterator it = m_table.begin ();
101 while (it != m_table.end())
102 {
103 time_t timestamp = it->second;
Alexander Afanasyevd3c501e2012-03-15 17:52:34 -0700104 _LOG_DEBUG ("expireInterests (): " << timestamp << ", " << currentTime);
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700105 if (currentTime - timestamp > m_checkPeriod)
106 {
Alexander Afanasyevd3c501e2012-03-15 17:52:34 -0700107 it = m_table.erase (it);
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700108 count ++;
109 }
Alexander Afanasyeva022f3d2012-03-11 18:14:48 -0700110 else
111 ++it;
112 }
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700113
114 _LOG_DEBUG ("expireInterests (): expired " << count);
115
Alexander Afanasyev181d7e52012-04-09 13:54:11 -0700116 m_scheduler.schedule (TIME_SECONDS (4),
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700117 bind (&SyncInterestTable::expireInterests, this),
118 0);
Alexander Afanasyeva022f3d2012-03-11 18:14:48 -0700119}
120
Zhenkai Zhu77169cb2012-03-08 16:02:52 -0800121
122}