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> |
Chaoyi Bian | 3e1eb16 | 2012-04-03 16:59:32 -0700 | [diff] [blame] | 19 | * Chaoyi Bian <bcy@pku.edu.cn> |
Zhenkai Zhu | 406f37a | 2012-03-05 20:23:20 -0800 | [diff] [blame] | 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 | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 33 | SyncInterestTable::SyncInterestTable (TimeDuration lifetime) |
| 34 | : m_entryLifetime (lifetime) |
Chaoyi Bian | 44fff0c | 2012-03-07 21:07:22 -0800 | [diff] [blame] | 35 | { |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 36 | m_scheduler.schedule (TIME_SECONDS (m_checkPeriod), |
Alexander Afanasyev | 860e6fe | 2012-03-15 17:30:31 -0700 | [diff] [blame] | 37 | bind (&SyncInterestTable::expireInterests, this), |
| 38 | 0); |
Chaoyi Bian | 44fff0c | 2012-03-07 21:07:22 -0800 | [diff] [blame] | 39 | } |
| 40 | |
Alexander Afanasyev | a022f3d | 2012-03-11 18:14:48 -0700 | [diff] [blame] | 41 | SyncInterestTable::~SyncInterestTable () |
Chaoyi Bian | 44fff0c | 2012-03-07 21:07:22 -0800 | [diff] [blame] | 42 | { |
Chaoyi Bian | 44fff0c | 2012-03-07 21:07:22 -0800 | [diff] [blame] | 43 | } |
| 44 | |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 45 | Interest |
| 46 | SyncInterestTable::pop () |
Alexander Afanasyev | a022f3d | 2012-03-11 18:14:48 -0700 | [diff] [blame] | 47 | { |
Alexander Afanasyev | d3c501e | 2012-03-15 17:52:34 -0700 | [diff] [blame] | 48 | expireInterests (); |
Alexander Afanasyev | a022f3d | 2012-03-11 18:14:48 -0700 | [diff] [blame] | 49 | recursive_mutex::scoped_lock lock (m_mutex); |
Alexander Afanasyev | a022f3d | 2012-03-11 18:14:48 -0700 | [diff] [blame] | 50 | |
Alexander Afanasyev | 46eb526 | 2012-05-10 16:30:35 -0700 | [diff] [blame] | 51 | if (m_table.size () == 0) |
| 52 | BOOST_THROW_EXCEPTION (Error::InterestTableIsEmpty ()); |
| 53 | |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 54 | Interest ret = *m_table.begin (); |
| 55 | m_table.erase (m_table.begin ()); |
| 56 | |
| 57 | return ret; |
Zhenkai Zhu | 77169cb | 2012-03-08 16:02:52 -0800 | [diff] [blame] | 58 | } |
| 59 | |
Alexander Afanasyev | a022f3d | 2012-03-11 18:14:48 -0700 | [diff] [blame] | 60 | bool |
Alexander Afanasyev | 2b6fbfb | 2012-05-24 10:57:14 -0700 | [diff] [blame] | 61 | SyncInterestTable::insert (DigestConstPtr digest, const string &name, bool unknownState/*=false*/) |
Alexander Afanasyev | a022f3d | 2012-03-11 18:14:48 -0700 | [diff] [blame] | 62 | { |
Alexander Afanasyev | 03793b4 | 2012-05-01 13:03:07 -0700 | [diff] [blame] | 63 | bool existent = false; |
| 64 | |
Alexander Afanasyev | a022f3d | 2012-03-11 18:14:48 -0700 | [diff] [blame] | 65 | recursive_mutex::scoped_lock lock (m_mutex); |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 66 | InterestContainer::index<named>::type::iterator it = m_table.get<named> ().find (name); |
Alexander Afanasyev | a022f3d | 2012-03-11 18:14:48 -0700 | [diff] [blame] | 67 | if (it != m_table.end()) |
Alexander Afanasyev | 03793b4 | 2012-05-01 13:03:07 -0700 | [diff] [blame] | 68 | { |
| 69 | existent = true; |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 70 | m_table.erase (it); |
Alexander Afanasyev | 03793b4 | 2012-05-01 13:03:07 -0700 | [diff] [blame] | 71 | } |
Alexander Afanasyev | 2b6fbfb | 2012-05-24 10:57:14 -0700 | [diff] [blame] | 72 | m_table.insert (Interest (digest, name, unknownState)); |
Alexander Afanasyev | 03793b4 | 2012-05-01 13:03:07 -0700 | [diff] [blame] | 73 | |
| 74 | return existent; |
Zhenkai Zhu | 77169cb | 2012-03-08 16:02:52 -0800 | [diff] [blame] | 75 | } |
| 76 | |
Alexander Afanasyev | 860e6fe | 2012-03-15 17:30:31 -0700 | [diff] [blame] | 77 | uint32_t |
| 78 | SyncInterestTable::size () const |
Alexander Afanasyev | a022f3d | 2012-03-11 18:14:48 -0700 | [diff] [blame] | 79 | { |
| 80 | recursive_mutex::scoped_lock lock (m_mutex); |
Alexander Afanasyev | 860e6fe | 2012-03-15 17:30:31 -0700 | [diff] [blame] | 81 | return m_table.size (); |
| 82 | } |
| 83 | |
| 84 | bool |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 85 | SyncInterestTable::remove (const string &name) |
Alexander Afanasyev | 860e6fe | 2012-03-15 17:30:31 -0700 | [diff] [blame] | 86 | { |
| 87 | recursive_mutex::scoped_lock lock (m_mutex); |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 88 | |
| 89 | InterestContainer::index<named>::type::iterator item = m_table.get<named> ().find (name); |
| 90 | if (item != m_table.get<named> ().end ()) |
Alexander Afanasyev | 860e6fe | 2012-03-15 17:30:31 -0700 | [diff] [blame] | 91 | { |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 92 | m_table.get<named> ().erase (name); |
| 93 | return true; |
| 94 | } |
| 95 | |
| 96 | return false; |
| 97 | } |
| 98 | |
| 99 | bool |
| 100 | SyncInterestTable::remove (DigestConstPtr digest) |
| 101 | { |
| 102 | recursive_mutex::scoped_lock lock (m_mutex); |
| 103 | InterestContainer::index<hashed>::type::iterator item = m_table.get<hashed> ().find (digest); |
| 104 | if (item != m_table.get<hashed> ().end ()) |
| 105 | { |
| 106 | m_table.get<hashed> ().erase (digest); // erase all records associated with the digest |
Alexander Afanasyev | 860e6fe | 2012-03-15 17:30:31 -0700 | [diff] [blame] | 107 | return true; |
| 108 | } |
| 109 | return false; |
| 110 | } |
| 111 | |
Alexander Afanasyev | 860e6fe | 2012-03-15 17:30:31 -0700 | [diff] [blame] | 112 | void SyncInterestTable::expireInterests () |
| 113 | { |
| 114 | recursive_mutex::scoped_lock lock (m_mutex); |
| 115 | |
| 116 | uint32_t count = 0; |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 117 | TimeAbsolute expireTime = TIME_NOW - m_entryLifetime; |
| 118 | |
| 119 | while (m_table.size () > 0) |
Alexander Afanasyev | a022f3d | 2012-03-11 18:14:48 -0700 | [diff] [blame] | 120 | { |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 121 | InterestContainer::index<timed>::type::iterator item = m_table.get<timed> ().begin (); |
| 122 | |
| 123 | if (item->m_time < expireTime) |
| 124 | { |
| 125 | m_table.get<timed> ().erase (item); |
| 126 | count ++; |
| 127 | } |
| 128 | else |
| 129 | break; |
Alexander Afanasyev | a022f3d | 2012-03-11 18:14:48 -0700 | [diff] [blame] | 130 | } |
Alexander Afanasyev | 860e6fe | 2012-03-15 17:30:31 -0700 | [diff] [blame] | 131 | |
| 132 | _LOG_DEBUG ("expireInterests (): expired " << count); |
| 133 | |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 134 | m_scheduler.schedule (TIME_SECONDS (m_checkPeriod), |
Alexander Afanasyev | 860e6fe | 2012-03-15 17:30:31 -0700 | [diff] [blame] | 135 | bind (&SyncInterestTable::expireInterests, this), |
| 136 | 0); |
Alexander Afanasyev | a022f3d | 2012-03-11 18:14:48 -0700 | [diff] [blame] | 137 | } |
| 138 | |
Zhenkai Zhu | 77169cb | 2012-03-08 16:02:52 -0800 | [diff] [blame] | 139 | |
| 140 | } |