blob: 2e5451fbd1ac1ec1822547d218041cc1926cc4f2 [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 */
22
Zhenkai Zhu1ac6f802012-03-06 17:40:27 -080023#ifndef SYNC_INTEREST_TABLE_H
24#define SYNC_INTEREST_TABLE_H
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -070025
Zhenkai Zhu406f37a2012-03-05 20:23:20 -080026#include <string>
Alexander Afanasyeva5858032012-03-09 15:55:10 -080027#include <vector>
Zhenkai Zhu77169cb2012-03-08 16:02:52 -080028#include <boost/thread/recursive_mutex.hpp>
29#include <boost/thread/thread.hpp>
30#include <ctime>
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -070031#include "sync-scheduler.h"
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -070032#include "sync-digest.h"
33#include "sync-interest-container.h"
Zhenkai Zhu406f37a2012-03-05 20:23:20 -080034
Zhenkai Zhu406f37a2012-03-05 20:23:20 -080035namespace Sync {
36
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -070037
Zhenkai Zhu406f37a2012-03-05 20:23:20 -080038/**
39 * \ingroup sync
40 * @brief A table to keep unanswered Sync Interest
Zhenkai Zhu77169cb2012-03-08 16:02:52 -080041 * all access operation to the table should grab the
42 * mutex first
Zhenkai Zhu406f37a2012-03-05 20:23:20 -080043 */
44class SyncInterestTable
45{
Zhenkai Zhu406f37a2012-03-05 20:23:20 -080046public:
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -070047 SyncInterestTable (TimeDuration lifetime);
Alexander Afanasyeva022f3d2012-03-11 18:14:48 -070048 ~SyncInterestTable ();
Zhenkai Zhu77169cb2012-03-08 16:02:52 -080049
Alexander Afanasyeva022f3d2012-03-11 18:14:48 -070050 /**
51 * @brief Insert an interest, if interest already exists, update the
52 * timestamp
53 */
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -070054 bool
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -070055 insert (DigestConstPtr interest, const std::string &name);
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -070056
57 /**
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -070058 * @brief Remove interest by digest (e.g., when it was satisfied)
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -070059 */
60 bool
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -070061 remove (DigestConstPtr interest);
Zhenkai Zhu01733f02012-03-06 13:56:26 -080062
Alexander Afanasyeva022f3d2012-03-11 18:14:48 -070063 /**
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -070064 * @brief Remove interest by name (e.g., when it was satisfied)
Alexander Afanasyeva022f3d2012-03-11 18:14:48 -070065 */
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -070066 bool
67 remove (const std::string &name);
68
69 /**
70 * @brief pop a non-expired Interest from PIT
71 */
72 Interest
73 pop ();
Zhenkai Zhu406f37a2012-03-05 20:23:20 -080074
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -070075 uint32_t
76 size () const;
77
Zhenkai Zhu77169cb2012-03-08 16:02:52 -080078private:
Alexander Afanasyeva022f3d2012-03-11 18:14:48 -070079 /**
80 * @brief periodically called to expire Interest
81 */
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -070082 void
83 expireInterests ();
Zhenkai Zhu77169cb2012-03-08 16:02:52 -080084
85private:
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -070086 static const int m_checkPeriod = 4; // seconds
87
88 TimeDuration m_entryLifetime;
89 InterestContainer m_table;
Zhenkai Zhu77169cb2012-03-08 16:02:52 -080090
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -070091 Scheduler m_scheduler;
92 mutable boost::recursive_mutex m_mutex;
Zhenkai Zhu406f37a2012-03-05 20:23:20 -080093};
94
Alexander Afanasyev46eb5262012-05-10 16:30:35 -070095namespace Error {
96struct InterestTableIsEmpty : virtual boost::exception, virtual std::exception { };
97}
98
99
Zhenkai Zhu406f37a2012-03-05 20:23:20 -0800100} // Sync
101
Zhenkai Zhu1ac6f802012-03-06 17:40:27 -0800102#endif // SYNC_INTEREST_TABLE_H