blob: b9f6484299da6d9541034fadd8e2e05185dd36ee [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"
24
25using namespace std;
26using namespace boost;
27
28namespace Sync
29{
30
Alexander Afanasyeva022f3d2012-03-11 18:14:48 -070031SyncInterestTable::SyncInterestTable ()
32 : m_running (true)
Chaoyi Bian44fff0c2012-03-07 21:07:22 -080033{
Alexander Afanasyeva022f3d2012-03-11 18:14:48 -070034 m_thread = thread (&SyncInterestTable::periodicCheck, this);
Chaoyi Bian44fff0c2012-03-07 21:07:22 -080035}
36
Alexander Afanasyeva022f3d2012-03-11 18:14:48 -070037SyncInterestTable::~SyncInterestTable ()
Chaoyi Bian44fff0c2012-03-07 21:07:22 -080038{
Alexander Afanasyeva022f3d2012-03-11 18:14:48 -070039 // cout << "request interrupt: " << this_thread::get_id () << endl;
40 m_running = false;
41 m_thread.interrupt ();
42 m_thread.join ();
Chaoyi Bian44fff0c2012-03-07 21:07:22 -080043}
44
Alexander Afanasyeva022f3d2012-03-11 18:14:48 -070045vector<string>
46SyncInterestTable::fetchAll ()
47{
48 expireInterests();
49
50 recursive_mutex::scoped_lock lock (m_mutex);
51
52 vector<string> entries;
53 for (unordered_map<string, time_t>::iterator it = m_table.begin();
54 it != m_table.end();
55 ++it)
56 {
57 entries.push_back(it->first);
58 }
59 m_table.clear ();
60
61 return entries;
Zhenkai Zhu77169cb2012-03-08 16:02:52 -080062}
63
Alexander Afanasyeva022f3d2012-03-11 18:14:48 -070064bool
65SyncInterestTable::insert(string interest)
66{
67 recursive_mutex::scoped_lock lock (m_mutex);
68 TableContainer::iterator it = m_table.find (interest);
69 if (it != m_table.end())
70 m_table.erase(it);
71 time_t currentTime = time(0);
72 m_table.insert (make_pair(interest, currentTime));
Zhenkai Zhu77169cb2012-03-08 16:02:52 -080073}
74
Alexander Afanasyeva022f3d2012-03-11 18:14:48 -070075void SyncInterestTable::expireInterests()
76{
77 recursive_mutex::scoped_lock lock (m_mutex);
78 time_t currentTime = time(0);
79 TableContainer::iterator it = m_table.begin ();
80 while (it != m_table.end())
81 {
82 time_t timestamp = it->second;
83 if (currentTime - timestamp > m_checkPeriod) {
84 it = m_table.erase(it);
85 }
86 else
87 ++it;
88 }
89}
90
91void SyncInterestTable::periodicCheck ()
92{
93 while (m_running)
94 {
95 try
96 {
97 // cout << "enterSleep: " << this_thread::get_id () << endl;
98
99 this_thread::sleep (posix_time::seconds(4));
100 expireInterests ();
101 }
102 catch (boost::thread_interrupted e)
103 {
104 // should I just assign m_running = false here?
105
106 // cout << "interrupted: " << this_thread::get_id () << endl;
107 // do nothing
108 }
109 }
Zhenkai Zhu77169cb2012-03-08 16:02:52 -0800110}
111
112}