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" |
Alexander Afanasyev | 860e6fe | 2012-03-15 17:30:31 -0700 | [diff] [blame] | 24 | #include "sync-log.h" |
Chaoyi Bian | 44fff0c | 2012-03-07 21:07:22 -0800 | [diff] [blame] | 25 | using namespace std; |
| 26 | using namespace boost; |
| 27 | |
Alexander Afanasyev | 860e6fe | 2012-03-15 17:30:31 -0700 | [diff] [blame] | 28 | INIT_LOGGER ("SyncInterestTable"); |
| 29 | |
Chaoyi Bian | 44fff0c | 2012-03-07 21:07:22 -0800 | [diff] [blame] | 30 | namespace Sync |
| 31 | { |
| 32 | |
Alexander Afanasyev | a022f3d | 2012-03-11 18:14:48 -0700 | [diff] [blame] | 33 | SyncInterestTable::SyncInterestTable () |
Chaoyi Bian | 44fff0c | 2012-03-07 21:07:22 -0800 | [diff] [blame] | 34 | { |
Alexander Afanasyev | d3c501e | 2012-03-15 17:52:34 -0700 | [diff] [blame] | 35 | m_scheduler.schedule (posix_time::seconds (4), |
Alexander Afanasyev | 860e6fe | 2012-03-15 17:30:31 -0700 | [diff] [blame] | 36 | bind (&SyncInterestTable::expireInterests, this), |
| 37 | 0); |
Chaoyi Bian | 44fff0c | 2012-03-07 21:07:22 -0800 | [diff] [blame] | 38 | } |
| 39 | |
Alexander Afanasyev | a022f3d | 2012-03-11 18:14:48 -0700 | [diff] [blame] | 40 | SyncInterestTable::~SyncInterestTable () |
Chaoyi Bian | 44fff0c | 2012-03-07 21:07:22 -0800 | [diff] [blame] | 41 | { |
Chaoyi Bian | 44fff0c | 2012-03-07 21:07:22 -0800 | [diff] [blame] | 42 | } |
| 43 | |
Alexander Afanasyev | a022f3d | 2012-03-11 18:14:48 -0700 | [diff] [blame] | 44 | vector<string> |
| 45 | SyncInterestTable::fetchAll () |
| 46 | { |
Alexander Afanasyev | d3c501e | 2012-03-15 17:52:34 -0700 | [diff] [blame] | 47 | expireInterests (); |
Alexander Afanasyev | a022f3d | 2012-03-11 18:14:48 -0700 | [diff] [blame] | 48 | recursive_mutex::scoped_lock lock (m_mutex); |
| 49 | |
| 50 | vector<string> entries; |
| 51 | for (unordered_map<string, time_t>::iterator it = m_table.begin(); |
| 52 | it != m_table.end(); |
| 53 | ++it) |
| 54 | { |
| 55 | entries.push_back(it->first); |
| 56 | } |
| 57 | m_table.clear (); |
| 58 | |
| 59 | return entries; |
Zhenkai Zhu | 77169cb | 2012-03-08 16:02:52 -0800 | [diff] [blame] | 60 | } |
| 61 | |
Alexander Afanasyev | a022f3d | 2012-03-11 18:14:48 -0700 | [diff] [blame] | 62 | bool |
Alexander Afanasyev | 860e6fe | 2012-03-15 17:30:31 -0700 | [diff] [blame] | 63 | SyncInterestTable::insert(const string &interest) |
Alexander Afanasyev | a022f3d | 2012-03-11 18:14:48 -0700 | [diff] [blame] | 64 | { |
| 65 | recursive_mutex::scoped_lock lock (m_mutex); |
| 66 | TableContainer::iterator it = m_table.find (interest); |
| 67 | if (it != m_table.end()) |
| 68 | m_table.erase(it); |
| 69 | time_t currentTime = time(0); |
| 70 | m_table.insert (make_pair(interest, currentTime)); |
Zhenkai Zhu | 77169cb | 2012-03-08 16:02:52 -0800 | [diff] [blame] | 71 | } |
| 72 | |
Alexander Afanasyev | 860e6fe | 2012-03-15 17:30:31 -0700 | [diff] [blame] | 73 | uint32_t |
| 74 | SyncInterestTable::size () const |
Alexander Afanasyev | a022f3d | 2012-03-11 18:14:48 -0700 | [diff] [blame] | 75 | { |
| 76 | recursive_mutex::scoped_lock lock (m_mutex); |
Alexander Afanasyev | 860e6fe | 2012-03-15 17:30:31 -0700 | [diff] [blame] | 77 | return m_table.size (); |
| 78 | } |
| 79 | |
| 80 | bool |
| 81 | SyncInterestTable::remove (const std::string &interest) |
| 82 | { |
| 83 | recursive_mutex::scoped_lock lock (m_mutex); |
| 84 | TableContainer::iterator item = m_table.find (interest); |
| 85 | if (item != m_table.end ()) |
| 86 | { |
| 87 | m_table.erase (item); |
| 88 | return true; |
| 89 | } |
| 90 | return false; |
| 91 | } |
| 92 | |
| 93 | |
| 94 | void SyncInterestTable::expireInterests () |
| 95 | { |
| 96 | recursive_mutex::scoped_lock lock (m_mutex); |
| 97 | |
| 98 | uint32_t count = 0; |
Alexander Afanasyev | a022f3d | 2012-03-11 18:14:48 -0700 | [diff] [blame] | 99 | time_t currentTime = time(0); |
| 100 | TableContainer::iterator it = m_table.begin (); |
| 101 | while (it != m_table.end()) |
| 102 | { |
| 103 | time_t timestamp = it->second; |
Alexander Afanasyev | d3c501e | 2012-03-15 17:52:34 -0700 | [diff] [blame] | 104 | _LOG_DEBUG ("expireInterests (): " << timestamp << ", " << currentTime); |
Alexander Afanasyev | 860e6fe | 2012-03-15 17:30:31 -0700 | [diff] [blame] | 105 | if (currentTime - timestamp > m_checkPeriod) |
| 106 | { |
Alexander Afanasyev | d3c501e | 2012-03-15 17:52:34 -0700 | [diff] [blame] | 107 | it = m_table.erase (it); |
Alexander Afanasyev | 860e6fe | 2012-03-15 17:30:31 -0700 | [diff] [blame] | 108 | count ++; |
| 109 | } |
Alexander Afanasyev | a022f3d | 2012-03-11 18:14:48 -0700 | [diff] [blame] | 110 | else |
| 111 | ++it; |
| 112 | } |
Alexander Afanasyev | 860e6fe | 2012-03-15 17:30:31 -0700 | [diff] [blame] | 113 | |
| 114 | _LOG_DEBUG ("expireInterests (): expired " << count); |
| 115 | |
Alexander Afanasyev | d3c501e | 2012-03-15 17:52:34 -0700 | [diff] [blame] | 116 | m_scheduler.schedule (posix_time::seconds (4), |
Alexander Afanasyev | 860e6fe | 2012-03-15 17:30:31 -0700 | [diff] [blame] | 117 | bind (&SyncInterestTable::expireInterests, this), |
| 118 | 0); |
Alexander Afanasyev | a022f3d | 2012-03-11 18:14:48 -0700 | [diff] [blame] | 119 | } |
| 120 | |
Zhenkai Zhu | 77169cb | 2012-03-08 16:02:52 -0800 | [diff] [blame] | 121 | |
| 122 | } |