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 | |
Alexander Afanasyev | a022f3d | 2012-03-11 18:14:48 -0700 | [diff] [blame^] | 31 | SyncInterestTable::SyncInterestTable () |
| 32 | : m_running (true) |
Chaoyi Bian | 44fff0c | 2012-03-07 21:07:22 -0800 | [diff] [blame] | 33 | { |
Alexander Afanasyev | a022f3d | 2012-03-11 18:14:48 -0700 | [diff] [blame^] | 34 | m_thread = thread (&SyncInterestTable::periodicCheck, this); |
Chaoyi Bian | 44fff0c | 2012-03-07 21:07:22 -0800 | [diff] [blame] | 35 | } |
| 36 | |
Alexander Afanasyev | a022f3d | 2012-03-11 18:14:48 -0700 | [diff] [blame^] | 37 | SyncInterestTable::~SyncInterestTable () |
Chaoyi Bian | 44fff0c | 2012-03-07 21:07:22 -0800 | [diff] [blame] | 38 | { |
Alexander Afanasyev | a022f3d | 2012-03-11 18:14:48 -0700 | [diff] [blame^] | 39 | // cout << "request interrupt: " << this_thread::get_id () << endl; |
| 40 | m_running = false; |
| 41 | m_thread.interrupt (); |
| 42 | m_thread.join (); |
Chaoyi Bian | 44fff0c | 2012-03-07 21:07:22 -0800 | [diff] [blame] | 43 | } |
| 44 | |
Alexander Afanasyev | a022f3d | 2012-03-11 18:14:48 -0700 | [diff] [blame^] | 45 | vector<string> |
| 46 | SyncInterestTable::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 Zhu | 77169cb | 2012-03-08 16:02:52 -0800 | [diff] [blame] | 62 | } |
| 63 | |
Alexander Afanasyev | a022f3d | 2012-03-11 18:14:48 -0700 | [diff] [blame^] | 64 | bool |
| 65 | SyncInterestTable::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 Zhu | 77169cb | 2012-03-08 16:02:52 -0800 | [diff] [blame] | 73 | } |
| 74 | |
Alexander Afanasyev | a022f3d | 2012-03-11 18:14:48 -0700 | [diff] [blame^] | 75 | void 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 | |
| 91 | void 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 Zhu | 77169cb | 2012-03-08 16:02:52 -0800 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | } |