blob: 548ebe2fe7e5fcd7c9d487dfc9c4ec5da8625a45 [file] [log] [blame]
Zhenkai Zhu406f37a2012-03-05 20:23:20 -08001/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
2/*
Alexander Afanasyev8722d872014-07-02 13:00:29 -07003 * Copyright (c) 2012-2014 University of California, Los Angeles
Zhenkai Zhu406f37a2012-03-05 20:23:20 -08004 *
Alexander Afanasyev8722d872014-07-02 13:00:29 -07005 * This file is part of ChronoSync, synchronization library for distributed realtime
6 * applications for NDN.
Zhenkai Zhu406f37a2012-03-05 20:23:20 -08007 *
Alexander Afanasyev8722d872014-07-02 13:00:29 -07008 * ChronoSync is free software: you can redistribute it and/or modify it under the terms
9 * of the GNU General Public License as published by the Free Software Foundation, either
10 * version 3 of the License, or (at your option) any later version.
Zhenkai Zhu406f37a2012-03-05 20:23:20 -080011 *
Alexander Afanasyev8722d872014-07-02 13:00:29 -070012 * ChronoSync is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
13 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
Zhenkai Zhu406f37a2012-03-05 20:23:20 -080015 *
Alexander Afanasyev8722d872014-07-02 13:00:29 -070016 * You should have received a copy of the GNU General Public License along with
17 * ChronoSync, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
18 *
19 * @author Zhenkai Zhu <http://irl.cs.ucla.edu/~zhenkai/>
20 * @author Chaoyi Bian <bcy@pku.edu.cn>
21 * @author Alexander Afanasyev <http://lasr.cs.ucla.edu/afanasyev/index.html>
Zhenkai Zhu406f37a2012-03-05 20:23:20 -080022 */
Chaoyi Bian44fff0c2012-03-07 21:07:22 -080023
24#include "sync-interest-table.h"
Alexander Afanasyevce001692013-07-14 11:34:41 -070025#include "sync-logging.h"
Chaoyi Bian44fff0c2012-03-07 21:07:22 -080026
Yingdi Yu06a678a2014-08-01 17:07:08 -070027INIT_LOGGER ("SyncInterestTable")
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -070028
Chaoyi Bian44fff0c2012-03-07 21:07:22 -080029namespace Sync
30{
31
Yingdi Yu6e1c9cd2014-03-25 10:26:54 -070032SyncInterestTable::SyncInterestTable (boost::asio::io_service& io, ndn::time::system_clock::Duration lifetime)
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -070033 : m_entryLifetime (lifetime)
Alexander Afanasyev531803b2014-02-05 15:57:35 -080034 , m_scheduler(io)
Chaoyi Bian44fff0c2012-03-07 21:07:22 -080035{
Alexander Afanasyev7fe59832014-07-02 12:17:46 -070036 m_scheduler.scheduleEvent(ndn::time::seconds (4),
37 ndn::bind(&SyncInterestTable::expireInterests, this));
Chaoyi Bian44fff0c2012-03-07 21:07:22 -080038}
39
Alexander Afanasyeva022f3d2012-03-11 18:14:48 -070040SyncInterestTable::~SyncInterestTable ()
Chaoyi Bian44fff0c2012-03-07 21:07:22 -080041{
Chaoyi Bian44fff0c2012-03-07 21:07:22 -080042}
43
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -070044Interest
45SyncInterestTable::pop ()
Alexander Afanasyeva022f3d2012-03-11 18:14:48 -070046{
Alexander Afanasyev46eb5262012-05-10 16:30:35 -070047 if (m_table.size () == 0)
48 BOOST_THROW_EXCEPTION (Error::InterestTableIsEmpty ());
49
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -070050 Interest ret = *m_table.begin ();
51 m_table.erase (m_table.begin ());
52
53 return ret;
Zhenkai Zhu77169cb2012-03-08 16:02:52 -080054}
55
Alexander Afanasyeva022f3d2012-03-11 18:14:48 -070056bool
Yingdi Yu68bc51a2014-03-25 16:02:12 -070057SyncInterestTable::insert (DigestConstPtr digest, const std::string& name, bool unknownState/*=false*/)
Alexander Afanasyeva022f3d2012-03-11 18:14:48 -070058{
Alexander Afanasyev03793b42012-05-01 13:03:07 -070059 bool existent = false;
Yingdi Yu7c64e5c2014-04-30 14:06:37 -070060
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -070061 InterestContainer::index<named>::type::iterator it = m_table.get<named> ().find (name);
Alexander Afanasyeva022f3d2012-03-11 18:14:48 -070062 if (it != m_table.end())
Alexander Afanasyev03793b42012-05-01 13:03:07 -070063 {
64 existent = true;
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -070065 m_table.erase (it);
Alexander Afanasyev03793b42012-05-01 13:03:07 -070066 }
Alexander Afanasyev2b6fbfb2012-05-24 10:57:14 -070067 m_table.insert (Interest (digest, name, unknownState));
Alexander Afanasyev03793b42012-05-01 13:03:07 -070068
69 return existent;
Zhenkai Zhu77169cb2012-03-08 16:02:52 -080070}
71
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -070072uint32_t
73SyncInterestTable::size () const
Alexander Afanasyeva022f3d2012-03-11 18:14:48 -070074{
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -070075 return m_table.size ();
76}
77
78bool
Yingdi Yu68bc51a2014-03-25 16:02:12 -070079SyncInterestTable::remove (const std::string& name)
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -070080{
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -070081 InterestContainer::index<named>::type::iterator item = m_table.get<named> ().find (name);
82 if (item != m_table.get<named> ().end ())
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -070083 {
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -070084 m_table.get<named> ().erase (name);
85 return true;
86 }
87
88 return false;
89}
90
91bool
92SyncInterestTable::remove (DigestConstPtr digest)
93{
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -070094 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
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -070098 return true;
99 }
100 return false;
101}
102
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700103void SyncInterestTable::expireInterests ()
Yingdi Yu7c64e5c2014-04-30 14:06:37 -0700104{
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700105 uint32_t count = 0;
Yingdi Yu6e1c9cd2014-03-25 10:26:54 -0700106 ndn::time::system_clock::TimePoint expireTime = ndn::time::system_clock::now() - m_entryLifetime;
Yingdi Yu7c64e5c2014-04-30 14:06:37 -0700107
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700108 while (m_table.size () > 0)
Alexander Afanasyeva022f3d2012-03-11 18:14:48 -0700109 {
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700110 InterestContainer::index<timed>::type::iterator item = m_table.get<timed> ().begin ();
Yingdi Yu7c64e5c2014-04-30 14:06:37 -0700111
Alexander Afanasyev531803b2014-02-05 15:57:35 -0800112 if (item->m_time <= expireTime)
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700113 {
114 m_table.get<timed> ().erase (item);
115 count ++;
116 }
117 else
118 break;
Alexander Afanasyeva022f3d2012-03-11 18:14:48 -0700119 }
Alexander Afanasyev7fe59832014-07-02 12:17:46 -0700120
121 m_scheduler.scheduleEvent(ndn::time::seconds (4),
122 ndn::bind(&SyncInterestTable::expireInterests, this));
Alexander Afanasyeva022f3d2012-03-11 18:14:48 -0700123}
124
Zhenkai Zhu77169cb2012-03-08 16:02:52 -0800125
126}