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