Zhenkai Zhu | 406f37a | 2012-03-05 20:23:20 -0800 | [diff] [blame] | 1 | /* -*- 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 Bian | 44fff0c | 2012-03-07 21:07:22 -0800 | [diff] [blame] | 22 | |
| 23 | #include "sync-interest-table.h" |
| 24 | |
| 25 | using namespace std; |
| 26 | using namespace boost; |
| 27 | |
| 28 | namespace Sync |
| 29 | { |
| 30 | |
| 31 | unordered_set<string> SyncInterestTable::fetchAll() |
| 32 | { |
Zhenkai Zhu | 77169cb | 2012-03-08 16:02:52 -0800 | [diff] [blame^] | 33 | 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 Bian | 44fff0c | 2012-03-07 21:07:22 -0800 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | bool SyncInterestTable::insert(string interest) |
| 46 | { |
Zhenkai Zhu | 77169cb | 2012-03-08 16:02:52 -0800 | [diff] [blame^] | 47 | 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 Bian | 44fff0c | 2012-03-07 21:07:22 -0800 | [diff] [blame] | 51 | } |
| 52 | |
Zhenkai Zhu | 77169cb | 2012-03-08 16:02:52 -0800 | [diff] [blame^] | 53 | void SyncInterestTable::SyncInterestTable() { |
| 54 | m_thread = thread(&SyncInterestTable::periodicCheck, this); |
| 55 | } |
| 56 | |
| 57 | void 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 | |
| 71 | void SyncInterestTable::periodicCheck() { |
| 72 | sleep(4); |
| 73 | expireInterest(); |
| 74 | } |
| 75 | |
| 76 | } |