blob: a24e1e86d4dd35660725b1cf1fc5ff32d1ee0ce7 [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{
Alexander Afanasyev03793b42012-05-01 13:03:07 -070065 bool existent = false;
66
Alexander Afanasyeva022f3d2012-03-11 18:14:48 -070067 recursive_mutex::scoped_lock lock (m_mutex);
68 TableContainer::iterator it = m_table.find (interest);
69 if (it != m_table.end())
Alexander Afanasyev03793b42012-05-01 13:03:07 -070070 {
71 existent = true;
72 m_table.erase(it);
73 }
Alexander Afanasyeva022f3d2012-03-11 18:14:48 -070074 time_t currentTime = time(0);
75 m_table.insert (make_pair(interest, currentTime));
Alexander Afanasyev03793b42012-05-01 13:03:07 -070076
77 return existent;
Zhenkai Zhu77169cb2012-03-08 16:02:52 -080078}
79
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -070080uint32_t
81SyncInterestTable::size () const
Alexander Afanasyeva022f3d2012-03-11 18:14:48 -070082{
83 recursive_mutex::scoped_lock lock (m_mutex);
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -070084 return m_table.size ();
85}
86
87bool
88SyncInterestTable::remove (const std::string &interest)
89{
90 recursive_mutex::scoped_lock lock (m_mutex);
91 TableContainer::iterator item = m_table.find (interest);
92 if (item != m_table.end ())
93 {
94 m_table.erase (item);
95 return true;
96 }
97 return false;
98}
99
100
101void SyncInterestTable::expireInterests ()
102{
103 recursive_mutex::scoped_lock lock (m_mutex);
104
105 uint32_t count = 0;
Alexander Afanasyeva022f3d2012-03-11 18:14:48 -0700106 time_t currentTime = time(0);
107 TableContainer::iterator it = m_table.begin ();
108 while (it != m_table.end())
109 {
110 time_t timestamp = it->second;
Alexander Afanasyevd3c501e2012-03-15 17:52:34 -0700111 _LOG_DEBUG ("expireInterests (): " << timestamp << ", " << currentTime);
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700112 if (currentTime - timestamp > m_checkPeriod)
113 {
Alexander Afanasyevd3c501e2012-03-15 17:52:34 -0700114 it = m_table.erase (it);
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700115 count ++;
116 }
Alexander Afanasyeva022f3d2012-03-11 18:14:48 -0700117 else
118 ++it;
119 }
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700120
121 _LOG_DEBUG ("expireInterests (): expired " << count);
122
Alexander Afanasyev181d7e52012-04-09 13:54:11 -0700123 m_scheduler.schedule (TIME_SECONDS (4),
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700124 bind (&SyncInterestTable::expireInterests, this),
125 0);
Alexander Afanasyeva022f3d2012-03-11 18:14:48 -0700126}
127
Zhenkai Zhu77169cb2012-03-08 16:02:52 -0800128
129}