blob: 0ec430cbb6508a5de2d29a77371569b01d01cbb0 [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
Zhenkai Zhua5d06d72012-03-09 15:16:24 -080031vector<string> SyncInterestTable::fetchAll()
Chaoyi Bian44fff0c2012-03-07 21:07:22 -080032{
Zhenkai Zhu075bd9d2012-03-08 16:06:45 -080033 expireInterests();
Zhenkai Zhu77169cb2012-03-08 16:02:52 -080034
35 recursive_mutex::scoped_lock lock(m_mutex);
Zhenkai Zhua5d06d72012-03-09 15:16:24 -080036 vector<string> entries;
Zhenkai Zhu77169cb2012-03-08 16:02:52 -080037 for (unordered_map<string, time_t>::iterator it = m_table.begin(); it !=
38 m_table.end(); ++it) {
Zhenkai Zhua5d06d72012-03-09 15:16:24 -080039 entries.push_back(it->first);
Zhenkai Zhu77169cb2012-03-08 16:02:52 -080040 }
Zhenkai Zhua5d06d72012-03-09 15:16:24 -080041 m_table.clear();
Zhenkai Zhu77169cb2012-03-08 16:02:52 -080042
43 return entries;
Chaoyi Bian44fff0c2012-03-07 21:07:22 -080044}
45
46bool SyncInterestTable::insert(string interest)
47{
Zhenkai Zhu77169cb2012-03-08 16:02:52 -080048 recursive_mutex::scoped_lock lock(m_mutex);
Zhenkai Zhubc7bfce2012-03-09 14:37:52 -080049 unordered_map<string, time_t>::iterator it = m_table.find(interest);
50 if (it != m_table.end())
51 m_table.erase(it);
Zhenkai Zhu77169cb2012-03-08 16:02:52 -080052 time_t currentTime = time(0);
53 m_table.insert(make_pair(interest, currentTime));
Chaoyi Bian44fff0c2012-03-07 21:07:22 -080054}
55
Zhenkai Zhu075bd9d2012-03-08 16:06:45 -080056SyncInterestTable::SyncInterestTable() {
Zhenkai Zhu77169cb2012-03-08 16:02:52 -080057 m_thread = thread(&SyncInterestTable::periodicCheck, this);
58}
59
60void SyncInterestTable::expireInterests() {
61 recursive_mutex::scoped_lock lock(m_mutex);
62 time_t currentTime = time(0);
63 unordered_map<string, time_t>::iterator it = m_table.begin();
64 while(it != m_table.end()) {
65 time_t timestamp = it->second;
66 if (currentTime - timestamp > m_checkPeriod) {
67 it = m_table.erase(it);
68 }
69 else
70 ++it;
71 }
72}
73
74void SyncInterestTable::periodicCheck() {
75 sleep(4);
Zhenkai Zhu075bd9d2012-03-08 16:06:45 -080076 expireInterests();
Zhenkai Zhu77169cb2012-03-08 16:02:52 -080077}
78
79}