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 | |
Zhenkai Zhu | a5d06d7 | 2012-03-09 15:16:24 -0800 | [diff] [blame] | 31 | vector<string> SyncInterestTable::fetchAll() |
Chaoyi Bian | 44fff0c | 2012-03-07 21:07:22 -0800 | [diff] [blame] | 32 | { |
Zhenkai Zhu | 075bd9d | 2012-03-08 16:06:45 -0800 | [diff] [blame] | 33 | expireInterests(); |
Zhenkai Zhu | 77169cb | 2012-03-08 16:02:52 -0800 | [diff] [blame] | 34 | |
| 35 | recursive_mutex::scoped_lock lock(m_mutex); |
Zhenkai Zhu | a5d06d7 | 2012-03-09 15:16:24 -0800 | [diff] [blame] | 36 | vector<string> entries; |
Zhenkai Zhu | 77169cb | 2012-03-08 16:02:52 -0800 | [diff] [blame] | 37 | for (unordered_map<string, time_t>::iterator it = m_table.begin(); it != |
| 38 | m_table.end(); ++it) { |
Zhenkai Zhu | a5d06d7 | 2012-03-09 15:16:24 -0800 | [diff] [blame] | 39 | entries.push_back(it->first); |
Zhenkai Zhu | 77169cb | 2012-03-08 16:02:52 -0800 | [diff] [blame] | 40 | } |
Zhenkai Zhu | a5d06d7 | 2012-03-09 15:16:24 -0800 | [diff] [blame] | 41 | m_table.clear(); |
Zhenkai Zhu | 77169cb | 2012-03-08 16:02:52 -0800 | [diff] [blame] | 42 | |
| 43 | return entries; |
Chaoyi Bian | 44fff0c | 2012-03-07 21:07:22 -0800 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | bool SyncInterestTable::insert(string interest) |
| 47 | { |
Zhenkai Zhu | 77169cb | 2012-03-08 16:02:52 -0800 | [diff] [blame] | 48 | recursive_mutex::scoped_lock lock(m_mutex); |
Zhenkai Zhu | bc7bfce | 2012-03-09 14:37:52 -0800 | [diff] [blame] | 49 | unordered_map<string, time_t>::iterator it = m_table.find(interest); |
| 50 | if (it != m_table.end()) |
| 51 | m_table.erase(it); |
Zhenkai Zhu | 77169cb | 2012-03-08 16:02:52 -0800 | [diff] [blame] | 52 | time_t currentTime = time(0); |
| 53 | m_table.insert(make_pair(interest, currentTime)); |
Chaoyi Bian | 44fff0c | 2012-03-07 21:07:22 -0800 | [diff] [blame] | 54 | } |
| 55 | |
Zhenkai Zhu | 075bd9d | 2012-03-08 16:06:45 -0800 | [diff] [blame] | 56 | SyncInterestTable::SyncInterestTable() { |
Zhenkai Zhu | 77169cb | 2012-03-08 16:02:52 -0800 | [diff] [blame] | 57 | m_thread = thread(&SyncInterestTable::periodicCheck, this); |
| 58 | } |
| 59 | |
| 60 | void 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 | |
| 74 | void SyncInterestTable::periodicCheck() { |
| 75 | sleep(4); |
Zhenkai Zhu | 075bd9d | 2012-03-08 16:06:45 -0800 | [diff] [blame] | 76 | expireInterests(); |
Zhenkai Zhu | 77169cb | 2012-03-08 16:02:52 -0800 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | } |