blob: bc5f2c2bf4d49f0ad80bc20675f7b7d69691917b [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>
Zhenkai Zhu406f37a2012-03-05 20:23:20 -080032
Zhenkai Zhu406f37a2012-03-05 20:23:20 -080033namespace Sync {
34
35/**
36 * \ingroup sync
37 * @brief A table to keep unanswered Sync Interest
Zhenkai Zhu77169cb2012-03-08 16:02:52 -080038 * all access operation to the table should grab the
39 * mutex first
Zhenkai Zhu406f37a2012-03-05 20:23:20 -080040 */
41class SyncInterestTable
42{
Zhenkai Zhu406f37a2012-03-05 20:23:20 -080043public:
Alexander Afanasyeva022f3d2012-03-11 18:14:48 -070044 SyncInterestTable ();
45 ~SyncInterestTable ();
Zhenkai Zhu77169cb2012-03-08 16:02:52 -080046
Alexander Afanasyeva022f3d2012-03-11 18:14:48 -070047 /**
48 * @brief Insert an interest, if interest already exists, update the
49 * timestamp
50 */
51 bool insert (std::string interest);
Zhenkai Zhu01733f02012-03-06 13:56:26 -080052
Alexander Afanasyeva022f3d2012-03-11 18:14:48 -070053 /**
54 * @brief fetch all Interests and clear the table
55 */
56 std::vector<std::string>
57 fetchAll ();
Zhenkai Zhu406f37a2012-03-05 20:23:20 -080058
Zhenkai Zhu77169cb2012-03-08 16:02:52 -080059private:
Alexander Afanasyeva022f3d2012-03-11 18:14:48 -070060 /**
61 * @brief periodically called to expire Interest
62 */
63 void expireInterests ();
Zhenkai Zhu77169cb2012-03-08 16:02:52 -080064
Alexander Afanasyeva022f3d2012-03-11 18:14:48 -070065 void periodicCheck ();
Zhenkai Zhu77169cb2012-03-08 16:02:52 -080066
67private:
Alexander Afanasyeva022f3d2012-03-11 18:14:48 -070068 typedef boost::unordered_map<std::string, time_t> TableContainer;
69
70 static const int m_checkPeriod = 4;
71 TableContainer m_table; // pit entries
72
73 boost::thread m_thread; // thread to check every 4 sec
74 volatile bool m_running;
75 boost::recursive_mutex m_mutex;
Zhenkai Zhu77169cb2012-03-08 16:02:52 -080076
Zhenkai Zhu406f37a2012-03-05 20:23:20 -080077};
78
79} // Sync
80
Zhenkai Zhu1ac6f802012-03-06 17:40:27 -080081#endif // SYNC_INTEREST_TABLE_H