blob: 199d731dfce3d1986ea97effeab034b0ab5b57bd [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>
19 * 卞超轶 Chaoyi Bian <bcy@pku.edu.cn>
20 * 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
Zhenkai Zhu406f37a2012-03-05 20:23:20 -080025#include <string>
Alexander Afanasyeva5858032012-03-09 15:55:10 -080026#include <vector>
Zhenkai Zhu77169cb2012-03-08 16:02:52 -080027#include <boost/unordered_map.hpp>
Zhenkai Zhu01733f02012-03-06 13:56:26 -080028#include <boost/unordered_set.hpp>
Zhenkai Zhu77169cb2012-03-08 16:02:52 -080029#include <boost/thread/recursive_mutex.hpp>
30#include <boost/thread/thread.hpp>
31#include <ctime>
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -070032#include "sync-scheduler.h"
Zhenkai Zhu406f37a2012-03-05 20:23:20 -080033
Zhenkai Zhu406f37a2012-03-05 20:23:20 -080034namespace Sync {
35
36/**
37 * \ingroup sync
38 * @brief A table to keep unanswered Sync Interest
Zhenkai Zhu77169cb2012-03-08 16:02:52 -080039 * all access operation to the table should grab the
40 * mutex first
Zhenkai Zhu406f37a2012-03-05 20:23:20 -080041 */
42class SyncInterestTable
43{
Zhenkai Zhu406f37a2012-03-05 20:23:20 -080044public:
Alexander Afanasyeva022f3d2012-03-11 18:14:48 -070045 SyncInterestTable ();
46 ~SyncInterestTable ();
Zhenkai Zhu77169cb2012-03-08 16:02:52 -080047
Alexander Afanasyeva022f3d2012-03-11 18:14:48 -070048 /**
49 * @brief Insert an interest, if interest already exists, update the
50 * timestamp
51 */
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -070052 bool
53 insert (const std::string &interest);
54
55 /**
56 * @brief Remove interest (e.g., when it was satisfied)
57 */
58 bool
59 remove (const std::string &interest);
Zhenkai Zhu01733f02012-03-06 13:56:26 -080060
Alexander Afanasyeva022f3d2012-03-11 18:14:48 -070061 /**
62 * @brief fetch all Interests and clear the table
63 */
64 std::vector<std::string>
65 fetchAll ();
Zhenkai Zhu406f37a2012-03-05 20:23:20 -080066
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -070067 uint32_t
68 size () const;
69
Zhenkai Zhu77169cb2012-03-08 16:02:52 -080070private:
Alexander Afanasyeva022f3d2012-03-11 18:14:48 -070071 /**
72 * @brief periodically called to expire Interest
73 */
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -070074 void
75 expireInterests ();
Zhenkai Zhu77169cb2012-03-08 16:02:52 -080076
77private:
Alexander Afanasyeva022f3d2012-03-11 18:14:48 -070078 typedef boost::unordered_map<std::string, time_t> TableContainer;
79
80 static const int m_checkPeriod = 4;
81 TableContainer m_table; // pit entries
Zhenkai Zhu77169cb2012-03-08 16:02:52 -080082
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -070083 Scheduler m_scheduler;
84 mutable boost::recursive_mutex m_mutex;
Zhenkai Zhu406f37a2012-03-05 20:23:20 -080085};
86
87} // Sync
88
Zhenkai Zhu1ac6f802012-03-06 17:40:27 -080089#endif // SYNC_INTEREST_TABLE_H