akmhoque | 66e6618 | 2014-02-21 17:56:03 -0600 | [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 | */ |
| 22 | |
| 23 | #include "sync-interest-table.h" |
| 24 | #include "sync-logging.h" |
| 25 | using namespace std; |
| 26 | |
| 27 | INIT_LOGGER ("SyncInterestTable"); |
| 28 | |
| 29 | namespace Sync |
| 30 | { |
| 31 | |
akmhoque | 05d5fcf | 2014-04-15 14:58:45 -0500 | [diff] [blame^] | 32 | SyncInterestTable::SyncInterestTable (boost::asio::io_service& io, ndn::time::system_clock::Duration lifetime) |
akmhoque | 66e6618 | 2014-02-21 17:56:03 -0600 | [diff] [blame] | 33 | : m_entryLifetime (lifetime) |
| 34 | , m_scheduler(io) |
| 35 | { |
| 36 | m_scheduler.schedulePeriodicEvent (ndn::time::seconds (4), ndn::time::seconds (4), |
| 37 | ndn::bind(&SyncInterestTable::expireInterests, this)); |
| 38 | } |
| 39 | |
| 40 | SyncInterestTable::~SyncInterestTable () |
| 41 | { |
| 42 | } |
| 43 | |
| 44 | Interest |
| 45 | SyncInterestTable::pop () |
| 46 | { |
| 47 | if (m_table.size () == 0) |
| 48 | BOOST_THROW_EXCEPTION (Error::InterestTableIsEmpty ()); |
| 49 | |
| 50 | Interest ret = *m_table.begin (); |
| 51 | m_table.erase (m_table.begin ()); |
| 52 | |
| 53 | return ret; |
| 54 | } |
| 55 | |
| 56 | bool |
| 57 | SyncInterestTable::insert (DigestConstPtr digest, const string &name, bool unknownState/*=false*/) |
| 58 | { |
| 59 | bool existent = false; |
| 60 | |
| 61 | InterestContainer::index<named>::type::iterator it = m_table.get<named> ().find (name); |
| 62 | if (it != m_table.end()) |
| 63 | { |
| 64 | existent = true; |
| 65 | m_table.erase (it); |
| 66 | } |
| 67 | m_table.insert (Interest (digest, name, unknownState)); |
| 68 | |
| 69 | return existent; |
| 70 | } |
| 71 | |
| 72 | uint32_t |
| 73 | SyncInterestTable::size () const |
| 74 | { |
| 75 | return m_table.size (); |
| 76 | } |
| 77 | |
| 78 | bool |
| 79 | SyncInterestTable::remove (const string &name) |
| 80 | { |
| 81 | InterestContainer::index<named>::type::iterator item = m_table.get<named> ().find (name); |
| 82 | if (item != m_table.get<named> ().end ()) |
| 83 | { |
| 84 | m_table.get<named> ().erase (name); |
| 85 | return true; |
| 86 | } |
| 87 | |
| 88 | return false; |
| 89 | } |
| 90 | |
| 91 | bool |
| 92 | SyncInterestTable::remove (DigestConstPtr digest) |
| 93 | { |
| 94 | InterestContainer::index<hashed>::type::iterator item = m_table.get<hashed> ().find (digest); |
| 95 | if (item != m_table.get<hashed> ().end ()) |
| 96 | { |
| 97 | m_table.get<hashed> ().erase (digest); // erase all records associated with the digest |
| 98 | return true; |
| 99 | } |
| 100 | return false; |
| 101 | } |
| 102 | |
| 103 | void SyncInterestTable::expireInterests () |
| 104 | { |
| 105 | uint32_t count = 0; |
akmhoque | 05d5fcf | 2014-04-15 14:58:45 -0500 | [diff] [blame^] | 106 | ndn::time::system_clock::TimePoint expireTime = ndn::time::system_clock::now() - m_entryLifetime; |
akmhoque | 66e6618 | 2014-02-21 17:56:03 -0600 | [diff] [blame] | 107 | |
| 108 | while (m_table.size () > 0) |
| 109 | { |
| 110 | InterestContainer::index<timed>::type::iterator item = m_table.get<timed> ().begin (); |
| 111 | |
| 112 | if (item->m_time <= expireTime) |
| 113 | { |
| 114 | m_table.get<timed> ().erase (item); |
| 115 | count ++; |
| 116 | } |
| 117 | else |
| 118 | break; |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | |
| 123 | } |