Zhenkai Zhu | 406f37a | 2012-03-05 20:23:20 -0800 | [diff] [blame] | 1 | /* -*- 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 Zhu | 1ac6f80 | 2012-03-06 17:40:27 -0800 | [diff] [blame] | 23 | #ifndef SYNC_INTEREST_TABLE_H |
| 24 | #define SYNC_INTEREST_TABLE_H |
Zhenkai Zhu | 406f37a | 2012-03-05 20:23:20 -0800 | [diff] [blame] | 25 | #include <string> |
Alexander Afanasyev | a585803 | 2012-03-09 15:55:10 -0800 | [diff] [blame^] | 26 | #include <vector> |
Zhenkai Zhu | 77169cb | 2012-03-08 16:02:52 -0800 | [diff] [blame] | 27 | #include <boost/unordered_map.hpp> |
Zhenkai Zhu | 01733f0 | 2012-03-06 13:56:26 -0800 | [diff] [blame] | 28 | #include <boost/unordered_set.hpp> |
Zhenkai Zhu | 77169cb | 2012-03-08 16:02:52 -0800 | [diff] [blame] | 29 | #include <boost/thread/recursive_mutex.hpp> |
| 30 | #include <boost/thread/thread.hpp> |
| 31 | #include <ctime> |
Zhenkai Zhu | 406f37a | 2012-03-05 20:23:20 -0800 | [diff] [blame] | 32 | |
| 33 | /** |
| 34 | * \defgroup sync SYNC protocol |
| 35 | * |
| 36 | * Implementation of SYNC protocol |
| 37 | */ |
| 38 | namespace Sync { |
| 39 | |
| 40 | /** |
| 41 | * \ingroup sync |
| 42 | * @brief A table to keep unanswered Sync Interest |
Zhenkai Zhu | 77169cb | 2012-03-08 16:02:52 -0800 | [diff] [blame] | 43 | * all access operation to the table should grab the |
| 44 | * mutex first |
Zhenkai Zhu | 406f37a | 2012-03-05 20:23:20 -0800 | [diff] [blame] | 45 | */ |
| 46 | class SyncInterestTable |
| 47 | { |
Zhenkai Zhu | 406f37a | 2012-03-05 20:23:20 -0800 | [diff] [blame] | 48 | public: |
Zhenkai Zhu | 77169cb | 2012-03-08 16:02:52 -0800 | [diff] [blame] | 49 | SyncInterestTable(); |
| 50 | |
Zhenkai Zhu | 01733f0 | 2012-03-06 13:56:26 -0800 | [diff] [blame] | 51 | /** |
Zhenkai Zhu | 77169cb | 2012-03-08 16:02:52 -0800 | [diff] [blame] | 52 | * @brief Insert an interest, if interest already exists, update the |
| 53 | * timestamp |
Zhenkai Zhu | 01733f0 | 2012-03-06 13:56:26 -0800 | [diff] [blame] | 54 | */ |
Zhenkai Zhu | 406f37a | 2012-03-05 20:23:20 -0800 | [diff] [blame] | 55 | bool insert(std::string interest); |
Zhenkai Zhu | 01733f0 | 2012-03-06 13:56:26 -0800 | [diff] [blame] | 56 | |
| 57 | /** |
| 58 | * @brief fetch all Interests and clear the table |
| 59 | */ |
Zhenkai Zhu | a5d06d7 | 2012-03-09 15:16:24 -0800 | [diff] [blame] | 60 | std::vector<std::string> fetchAll(); |
Zhenkai Zhu | 406f37a | 2012-03-05 20:23:20 -0800 | [diff] [blame] | 61 | |
Zhenkai Zhu | 77169cb | 2012-03-08 16:02:52 -0800 | [diff] [blame] | 62 | private: |
| 63 | /** |
| 64 | * @brief periodically called to expire Interest |
| 65 | */ |
| 66 | void expireInterests(); |
| 67 | |
| 68 | void periodicCheck(); |
| 69 | |
| 70 | private: |
Zhenkai Zhu | 075bd9d | 2012-03-08 16:06:45 -0800 | [diff] [blame] | 71 | static const int m_checkPeriod = 4; |
Zhenkai Zhu | 77169cb | 2012-03-08 16:02:52 -0800 | [diff] [blame] | 72 | 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 Zhu | 406f37a | 2012-03-05 20:23:20 -0800 | [diff] [blame] | 76 | }; |
| 77 | |
| 78 | } // Sync |
| 79 | |
Zhenkai Zhu | 1ac6f80 | 2012-03-06 17:40:27 -0800 | [diff] [blame] | 80 | #endif // SYNC_INTEREST_TABLE_H |