blob: f850708eb42e8dfbced02ffae56463efb7630a96 [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
33/**
34 * \defgroup sync SYNC protocol
35 *
36 * Implementation of SYNC protocol
37 */
38namespace Sync {
39
40/**
41 * \ingroup sync
42 * @brief A table to keep unanswered Sync Interest
Zhenkai Zhu77169cb2012-03-08 16:02:52 -080043 * all access operation to the table should grab the
44 * mutex first
Zhenkai Zhu406f37a2012-03-05 20:23:20 -080045 */
46class SyncInterestTable
47{
Zhenkai Zhu406f37a2012-03-05 20:23:20 -080048public:
Zhenkai Zhu77169cb2012-03-08 16:02:52 -080049 SyncInterestTable();
50
Zhenkai Zhu01733f02012-03-06 13:56:26 -080051 /**
Zhenkai Zhu77169cb2012-03-08 16:02:52 -080052 * @brief Insert an interest, if interest already exists, update the
53 * timestamp
Zhenkai Zhu01733f02012-03-06 13:56:26 -080054 */
Zhenkai Zhu406f37a2012-03-05 20:23:20 -080055 bool insert(std::string interest);
Zhenkai Zhu01733f02012-03-06 13:56:26 -080056
57 /**
58 * @brief fetch all Interests and clear the table
59 */
Zhenkai Zhua5d06d72012-03-09 15:16:24 -080060 std::vector<std::string> fetchAll();
Zhenkai Zhu406f37a2012-03-05 20:23:20 -080061
Zhenkai Zhu77169cb2012-03-08 16:02:52 -080062private:
63 /**
64 * @brief periodically called to expire Interest
65 */
66 void expireInterests();
67
68 void periodicCheck();
69
70private:
Zhenkai Zhu075bd9d2012-03-08 16:06:45 -080071 static const int m_checkPeriod = 4;
Zhenkai Zhu77169cb2012-03-08 16:02:52 -080072 boost::unordered_map<std::string, time_t> m_table; // pit entries
73 boost::thread m_thread; // thread to check every 4 sec
74 boost::recursive_mutex m_mutex;
75
Zhenkai Zhu406f37a2012-03-05 20:23:20 -080076};
77
78} // Sync
79
Zhenkai Zhu1ac6f802012-03-06 17:40:27 -080080#endif // SYNC_INTEREST_TABLE_H