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 | |
Zhenkai Zhu | 406f37a | 2012-03-05 20:23:20 -0800 | [diff] [blame] | 33 | namespace Sync { |
| 34 | |
| 35 | /** |
| 36 | * \ingroup sync |
| 37 | * @brief A table to keep unanswered Sync Interest |
Zhenkai Zhu | 77169cb | 2012-03-08 16:02:52 -0800 | [diff] [blame] | 38 | * all access operation to the table should grab the |
| 39 | * mutex first |
Zhenkai Zhu | 406f37a | 2012-03-05 20:23:20 -0800 | [diff] [blame] | 40 | */ |
| 41 | class SyncInterestTable |
| 42 | { |
Zhenkai Zhu | 406f37a | 2012-03-05 20:23:20 -0800 | [diff] [blame] | 43 | public: |
Alexander Afanasyev | a022f3d | 2012-03-11 18:14:48 -0700 | [diff] [blame^] | 44 | SyncInterestTable (); |
| 45 | ~SyncInterestTable (); |
Zhenkai Zhu | 77169cb | 2012-03-08 16:02:52 -0800 | [diff] [blame] | 46 | |
Alexander Afanasyev | a022f3d | 2012-03-11 18:14:48 -0700 | [diff] [blame^] | 47 | /** |
| 48 | * @brief Insert an interest, if interest already exists, update the |
| 49 | * timestamp |
| 50 | */ |
| 51 | bool insert (std::string interest); |
Zhenkai Zhu | 01733f0 | 2012-03-06 13:56:26 -0800 | [diff] [blame] | 52 | |
Alexander Afanasyev | a022f3d | 2012-03-11 18:14:48 -0700 | [diff] [blame^] | 53 | /** |
| 54 | * @brief fetch all Interests and clear the table |
| 55 | */ |
| 56 | std::vector<std::string> |
| 57 | fetchAll (); |
Zhenkai Zhu | 406f37a | 2012-03-05 20:23:20 -0800 | [diff] [blame] | 58 | |
Zhenkai Zhu | 77169cb | 2012-03-08 16:02:52 -0800 | [diff] [blame] | 59 | private: |
Alexander Afanasyev | a022f3d | 2012-03-11 18:14:48 -0700 | [diff] [blame^] | 60 | /** |
| 61 | * @brief periodically called to expire Interest |
| 62 | */ |
| 63 | void expireInterests (); |
Zhenkai Zhu | 77169cb | 2012-03-08 16:02:52 -0800 | [diff] [blame] | 64 | |
Alexander Afanasyev | a022f3d | 2012-03-11 18:14:48 -0700 | [diff] [blame^] | 65 | void periodicCheck (); |
Zhenkai Zhu | 77169cb | 2012-03-08 16:02:52 -0800 | [diff] [blame] | 66 | |
| 67 | private: |
Alexander Afanasyev | a022f3d | 2012-03-11 18:14:48 -0700 | [diff] [blame^] | 68 | 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 Zhu | 77169cb | 2012-03-08 16:02:52 -0800 | [diff] [blame] | 76 | |
Zhenkai Zhu | 406f37a | 2012-03-05 20:23:20 -0800 | [diff] [blame] | 77 | }; |
| 78 | |
| 79 | } // Sync |
| 80 | |
Zhenkai Zhu | 1ac6f80 | 2012-03-06 17:40:27 -0800 | [diff] [blame] | 81 | #endif // SYNC_INTEREST_TABLE_H |