blob: 7a7a394a6160cf73b7bc35b89a17bfe2a4fcf81b [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
31unordered_set<string> SyncInterestTable::fetchAll()
32{
Zhenkai Zhu77169cb2012-03-08 16:02:52 -080033 expireInterest();
34
35 recursive_mutex::scoped_lock lock(m_mutex);
36 unordered_set<string> entries;
37 for (unordered_map<string, time_t>::iterator it = m_table.begin(); it !=
38 m_table.end(); ++it) {
39 entries.insert(it->first);
40 }
41
42 return entries;
Chaoyi Bian44fff0c2012-03-07 21:07:22 -080043}
44
45bool SyncInterestTable::insert(string interest)
46{
Zhenkai Zhu77169cb2012-03-08 16:02:52 -080047 recursive_mutex::scoped_lock lock(m_mutex);
48 m_table.erase(m_table.find(interest));
49 time_t currentTime = time(0);
50 m_table.insert(make_pair(interest, currentTime));
Chaoyi Bian44fff0c2012-03-07 21:07:22 -080051}
52
Zhenkai Zhu77169cb2012-03-08 16:02:52 -080053void SyncInterestTable::SyncInterestTable() {
54 m_thread = thread(&SyncInterestTable::periodicCheck, this);
55}
56
57void SyncInterestTable::expireInterests() {
58 recursive_mutex::scoped_lock lock(m_mutex);
59 time_t currentTime = time(0);
60 unordered_map<string, time_t>::iterator it = m_table.begin();
61 while(it != m_table.end()) {
62 time_t timestamp = it->second;
63 if (currentTime - timestamp > m_checkPeriod) {
64 it = m_table.erase(it);
65 }
66 else
67 ++it;
68 }
69}
70
71void SyncInterestTable::periodicCheck() {
72 sleep(4);
73 expireInterest();
74}
75
76}