blob: fc1d3bfb6b98911a897f0442d602d23cb4121bc9 [file] [log] [blame]
Zhenkai Zhu406f37a2012-03-05 20:23:20 -08001/* -*- 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 Bian3e1eb162012-04-03 16:59:32 -070019 * Chaoyi Bian <bcy@pku.edu.cn>
Zhenkai Zhu406f37a2012-03-05 20:23:20 -080020 * Alexander Afanasyev <alexander.afanasyev@ucla.edu>
21 */
Chaoyi Bian44fff0c2012-03-07 21:07:22 -080022
23#include "sync-interest-table.h"
Alexander Afanasyevce001692013-07-14 11:34:41 -070024#include "sync-logging.h"
Chaoyi Bian44fff0c2012-03-07 21:07:22 -080025
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -070026INIT_LOGGER ("SyncInterestTable");
27
Chaoyi Bian44fff0c2012-03-07 21:07:22 -080028namespace Sync
29{
30
Yingdi Yu6e1c9cd2014-03-25 10:26:54 -070031SyncInterestTable::SyncInterestTable (boost::asio::io_service& io, ndn::time::system_clock::Duration lifetime)
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -070032 : m_entryLifetime (lifetime)
Alexander Afanasyev531803b2014-02-05 15:57:35 -080033 , m_scheduler(io)
Chaoyi Bian44fff0c2012-03-07 21:07:22 -080034{
Alexander Afanasyev531803b2014-02-05 15:57:35 -080035 m_scheduler.schedulePeriodicEvent (ndn::time::seconds (4), ndn::time::seconds (4),
36 ndn::bind(&SyncInterestTable::expireInterests, this));
Chaoyi Bian44fff0c2012-03-07 21:07:22 -080037}
38
Alexander Afanasyeva022f3d2012-03-11 18:14:48 -070039SyncInterestTable::~SyncInterestTable ()
Chaoyi Bian44fff0c2012-03-07 21:07:22 -080040{
Chaoyi Bian44fff0c2012-03-07 21:07:22 -080041}
42
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -070043Interest
44SyncInterestTable::pop ()
Alexander Afanasyeva022f3d2012-03-11 18:14:48 -070045{
Alexander Afanasyev46eb5262012-05-10 16:30:35 -070046 if (m_table.size () == 0)
47 BOOST_THROW_EXCEPTION (Error::InterestTableIsEmpty ());
48
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -070049 Interest ret = *m_table.begin ();
50 m_table.erase (m_table.begin ());
51
52 return ret;
Zhenkai Zhu77169cb2012-03-08 16:02:52 -080053}
54
Alexander Afanasyeva022f3d2012-03-11 18:14:48 -070055bool
Yingdi Yu68bc51a2014-03-25 16:02:12 -070056SyncInterestTable::insert (DigestConstPtr digest, const std::string& name, bool unknownState/*=false*/)
Alexander Afanasyeva022f3d2012-03-11 18:14:48 -070057{
Alexander Afanasyev03793b42012-05-01 13:03:07 -070058 bool existent = false;
59
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -070060 InterestContainer::index<named>::type::iterator it = m_table.get<named> ().find (name);
Alexander Afanasyeva022f3d2012-03-11 18:14:48 -070061 if (it != m_table.end())
Alexander Afanasyev03793b42012-05-01 13:03:07 -070062 {
63 existent = true;
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -070064 m_table.erase (it);
Alexander Afanasyev03793b42012-05-01 13:03:07 -070065 }
Alexander Afanasyev2b6fbfb2012-05-24 10:57:14 -070066 m_table.insert (Interest (digest, name, unknownState));
Alexander Afanasyev03793b42012-05-01 13:03:07 -070067
68 return existent;
Zhenkai Zhu77169cb2012-03-08 16:02:52 -080069}
70
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -070071uint32_t
72SyncInterestTable::size () const
Alexander Afanasyeva022f3d2012-03-11 18:14:48 -070073{
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -070074 return m_table.size ();
75}
76
77bool
Yingdi Yu68bc51a2014-03-25 16:02:12 -070078SyncInterestTable::remove (const std::string& name)
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -070079{
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -070080 InterestContainer::index<named>::type::iterator item = m_table.get<named> ().find (name);
81 if (item != m_table.get<named> ().end ())
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -070082 {
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -070083 m_table.get<named> ().erase (name);
84 return true;
85 }
86
87 return false;
88}
89
90bool
91SyncInterestTable::remove (DigestConstPtr digest)
92{
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -070093 InterestContainer::index<hashed>::type::iterator item = m_table.get<hashed> ().find (digest);
94 if (item != m_table.get<hashed> ().end ())
95 {
96 m_table.get<hashed> ().erase (digest); // erase all records associated with the digest
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -070097 return true;
98 }
99 return false;
100}
101
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700102void SyncInterestTable::expireInterests ()
103{
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700104 uint32_t count = 0;
Yingdi Yu6e1c9cd2014-03-25 10:26:54 -0700105 ndn::time::system_clock::TimePoint expireTime = ndn::time::system_clock::now() - m_entryLifetime;
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700106
107 while (m_table.size () > 0)
Alexander Afanasyeva022f3d2012-03-11 18:14:48 -0700108 {
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700109 InterestContainer::index<timed>::type::iterator item = m_table.get<timed> ().begin ();
110
Alexander Afanasyev531803b2014-02-05 15:57:35 -0800111 if (item->m_time <= expireTime)
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700112 {
113 m_table.get<timed> ().erase (item);
114 count ++;
115 }
116 else
117 break;
Alexander Afanasyeva022f3d2012-03-11 18:14:48 -0700118 }
119}
120
Zhenkai Zhu77169cb2012-03-08 16:02:52 -0800121
122}