blob: 6fc258ece28112052517799bc1c8091ee4e7a43d [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>
Zhenkai Zhu77169cb2012-03-08 16:02:52 -080026#include <boost/unordered_map.hpp>
Zhenkai Zhu01733f02012-03-06 13:56:26 -080027#include <boost/unordered_set.hpp>
Zhenkai Zhu77169cb2012-03-08 16:02:52 -080028#include <boost/thread/recursive_mutex.hpp>
29#include <boost/thread/thread.hpp>
30#include <ctime>
Zhenkai Zhu406f37a2012-03-05 20:23:20 -080031
32/**
33 * \defgroup sync SYNC protocol
34 *
35 * Implementation of SYNC protocol
36 */
37namespace Sync {
38
39/**
40 * \ingroup sync
41 * @brief A table to keep unanswered Sync Interest
Zhenkai Zhu77169cb2012-03-08 16:02:52 -080042 * all access operation to the table should grab the
43 * mutex first
Zhenkai Zhu406f37a2012-03-05 20:23:20 -080044 */
45class SyncInterestTable
46{
Zhenkai Zhu406f37a2012-03-05 20:23:20 -080047public:
Zhenkai Zhu77169cb2012-03-08 16:02:52 -080048 SyncInterestTable();
49
Zhenkai Zhu01733f02012-03-06 13:56:26 -080050 /**
Zhenkai Zhu77169cb2012-03-08 16:02:52 -080051 * @brief Insert an interest, if interest already exists, update the
52 * timestamp
Zhenkai Zhu01733f02012-03-06 13:56:26 -080053 */
Zhenkai Zhu406f37a2012-03-05 20:23:20 -080054 bool insert(std::string interest);
Zhenkai Zhu01733f02012-03-06 13:56:26 -080055
56 /**
57 * @brief fetch all Interests and clear the table
58 */
Zhenkai Zhua5d06d72012-03-09 15:16:24 -080059 std::vector<std::string> fetchAll();
Zhenkai Zhu406f37a2012-03-05 20:23:20 -080060
Zhenkai Zhu77169cb2012-03-08 16:02:52 -080061private:
62 /**
63 * @brief periodically called to expire Interest
64 */
65 void expireInterests();
66
67 void periodicCheck();
68
69private:
Zhenkai Zhu075bd9d2012-03-08 16:06:45 -080070 static const int m_checkPeriod = 4;
Zhenkai Zhu77169cb2012-03-08 16:02:52 -080071 boost::unordered_map<std::string, time_t> m_table; // pit entries
72 boost::thread m_thread; // thread to check every 4 sec
73 boost::recursive_mutex m_mutex;
74
Zhenkai Zhu406f37a2012-03-05 20:23:20 -080075};
76
77} // Sync
78
Zhenkai Zhu1ac6f802012-03-06 17:40:27 -080079#endif // SYNC_INTEREST_TABLE_H