| /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */ |
| /* |
| * Copyright (c) 2012 University of California, Los Angeles |
| * |
| * This program is free software; you can redistribute it and/or modify |
| * it under the terms of the GNU General Public License version 2 as |
| * published by the Free Software Foundation; |
| * |
| * This program is distributed in the hope that it will be useful, |
| * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| * GNU General Public License for more details. |
| * |
| * You should have received a copy of the GNU General Public License |
| * along with this program; if not, write to the Free Software |
| * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| * |
| * Author: Zhenkai Zhu <zhenkai@cs.ucla.edu> |
| * 卞超轶 Chaoyi Bian <bcy@pku.edu.cn> |
| * Alexander Afanasyev <alexander.afanasyev@ucla.edu> |
| */ |
| |
| #include "sync-interest-table.h" |
| |
| using namespace std; |
| using namespace boost; |
| |
| namespace Sync |
| { |
| |
| vector<string> SyncInterestTable::fetchAll() |
| { |
| expireInterests(); |
| |
| recursive_mutex::scoped_lock lock(m_mutex); |
| vector<string> entries; |
| for (unordered_map<string, time_t>::iterator it = m_table.begin(); it != |
| m_table.end(); ++it) { |
| entries.push_back(it->first); |
| } |
| m_table.clear(); |
| |
| return entries; |
| } |
| |
| bool SyncInterestTable::insert(string interest) |
| { |
| recursive_mutex::scoped_lock lock(m_mutex); |
| unordered_map<string, time_t>::iterator it = m_table.find(interest); |
| if (it != m_table.end()) |
| m_table.erase(it); |
| time_t currentTime = time(0); |
| m_table.insert(make_pair(interest, currentTime)); |
| } |
| |
| SyncInterestTable::SyncInterestTable() { |
| m_thread = thread(&SyncInterestTable::periodicCheck, this); |
| } |
| |
| void SyncInterestTable::expireInterests() { |
| recursive_mutex::scoped_lock lock(m_mutex); |
| time_t currentTime = time(0); |
| unordered_map<string, time_t>::iterator it = m_table.begin(); |
| while(it != m_table.end()) { |
| time_t timestamp = it->second; |
| if (currentTime - timestamp > m_checkPeriod) { |
| it = m_table.erase(it); |
| } |
| else |
| ++it; |
| } |
| } |
| |
| void SyncInterestTable::periodicCheck() { |
| sleep(4); |
| expireInterests(); |
| } |
| |
| } |