Zhenkai Zhu | 8224bb6 | 2013-01-06 15:58:02 -0800 | [diff] [blame] | 1 | /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /* |
| 3 | * Copyright (c) 2013 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 | * Zhenkai Zhu <zhenkai@cs.ucla.edu> |
| 19 | * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu> |
| 20 | */ |
| 21 | |
| 22 | #ifndef SYNC_CORE_H |
| 23 | #define SYNC_CORE_H |
| 24 | |
| 25 | #include "sync-log.h" |
Alexander Afanasyev | abe952a | 2013-01-17 17:06:32 -0800 | [diff] [blame] | 26 | #include "ccnx-wrapper.h" |
| 27 | #include "scheduler.h" |
Alexander Afanasyev | abe952a | 2013-01-17 17:06:32 -0800 | [diff] [blame] | 28 | |
Zhenkai Zhu | 74dd53c | 2013-01-10 23:39:57 -0800 | [diff] [blame] | 29 | #include <boost/function.hpp> |
Zhenkai Zhu | 8224bb6 | 2013-01-06 15:58:02 -0800 | [diff] [blame] | 30 | |
| 31 | class SyncCore |
| 32 | { |
| 33 | public: |
Zhenkai Zhu | 74dd53c | 2013-01-10 23:39:57 -0800 | [diff] [blame] | 34 | typedef boost::function<void (const SyncStateMsgPtr & stateMsg) > StateMsgCallback; |
Alexander Afanasyev | dac8492 | 2013-01-20 23:32:17 -0800 | [diff] [blame] | 35 | // typedef map<Name, Name> YellowPage; |
Zhenkai Zhu | 74dd53c | 2013-01-10 23:39:57 -0800 | [diff] [blame] | 36 | typedef boost::shared_mutex Mutex; |
| 37 | typedef boost::shared_lock<Mutex> ReadLock; |
| 38 | typedef boost::unique_lock<Mutex> WriteLock; |
| 39 | |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame] | 40 | static const int FRESHNESS = 2; // seconds |
| 41 | static const string RECOVER; |
| 42 | static const double WAIT; // seconds; |
| 43 | static const double RANDOM_PERCENT; // seconds; |
Zhenkai Zhu | 74dd53c | 2013-01-10 23:39:57 -0800 | [diff] [blame] | 44 | |
| 45 | public: |
Zhenkai Zhu | b330aed | 2013-01-17 13:29:37 -0800 | [diff] [blame] | 46 | SyncCore(SyncLogPtr syncLog |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame^] | 47 | , const Ccnx::Name &userName |
| 48 | , const Ccnx::Name &localPrefix // routable name used by the local user |
| 49 | , const Ccnx::Name &syncPrefix // the prefix for the sync collection |
Zhenkai Zhu | 74dd53c | 2013-01-10 23:39:57 -0800 | [diff] [blame] | 50 | , const StateMsgCallback &callback // callback when state change is detected |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame^] | 51 | , Ccnx::CcnxWrapperPtr ccnx |
| 52 | , SchedulerPtr scheduler); |
Zhenkai Zhu | 8224bb6 | 2013-01-06 15:58:02 -0800 | [diff] [blame] | 53 | ~SyncCore(); |
| 54 | |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame^] | 55 | void |
| 56 | updateLocalState (sqlite3_int64); |
| 57 | |
| 58 | // ------------------ only used in test ------------------------- |
| 59 | public: |
| 60 | HashPtr |
| 61 | root() const { return m_rootHash; } |
| 62 | |
| 63 | sqlite3_int64 |
| 64 | seq (const Ccnx::Name &name); |
| 65 | |
| 66 | private: |
| 67 | void |
| 68 | handleInterest(const Ccnx::Name &name); |
Zhenkai Zhu | 74dd53c | 2013-01-10 23:39:57 -0800 | [diff] [blame] | 69 | |
| 70 | void |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame^] | 71 | handleSyncData(const Ccnx::Name &name, Ccnx::PcoPtr content); |
Zhenkai Zhu | 74dd53c | 2013-01-10 23:39:57 -0800 | [diff] [blame] | 72 | |
| 73 | void |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame^] | 74 | handleRecoverData(const Ccnx::Name &name, Ccnx::PcoPtr content); |
| 75 | |
| 76 | Ccnx::Closure::TimeoutCallbackReturnValue |
| 77 | handleSyncInterestTimeout(const Ccnx::Name &name); |
| 78 | |
| 79 | Ccnx::Closure::TimeoutCallbackReturnValue |
| 80 | handleRecoverInterestTimeout(const Ccnx::Name &name); |
Zhenkai Zhu | 74dd53c | 2013-01-10 23:39:57 -0800 | [diff] [blame] | 81 | |
| 82 | void |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame^] | 83 | deregister(const Ccnx::Name &name); |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame] | 84 | |
| 85 | void |
| 86 | recover(const HashPtr &hash); |
Zhenkai Zhu | 74dd53c | 2013-01-10 23:39:57 -0800 | [diff] [blame] | 87 | |
Alexander Afanasyev | c507ac2 | 2013-01-21 16:01:58 -0800 | [diff] [blame] | 88 | private: |
Zhenkai Zhu | 74dd53c | 2013-01-10 23:39:57 -0800 | [diff] [blame] | 89 | void |
| 90 | sendSyncInterest(); |
| 91 | |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame] | 92 | void |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame^] | 93 | handleSyncInterest(const Ccnx::Name &name); |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame] | 94 | |
| 95 | void |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame^] | 96 | handleRecoverInterest(const Ccnx::Name &name); |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame] | 97 | |
| 98 | void |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame^] | 99 | handleStateData(const Ccnx::Bytes &content); |
Zhenkai Zhu | 74dd53c | 2013-01-10 23:39:57 -0800 | [diff] [blame] | 100 | |
Alexander Afanasyev | c507ac2 | 2013-01-21 16:01:58 -0800 | [diff] [blame] | 101 | private: |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame^] | 102 | Ccnx::CcnxWrapperPtr m_ccnx; |
| 103 | |
Zhenkai Zhu | b330aed | 2013-01-17 13:29:37 -0800 | [diff] [blame] | 104 | SyncLogPtr m_log; |
Zhenkai Zhu | 66dc5a9 | 2013-01-08 21:41:15 -0800 | [diff] [blame] | 105 | SchedulerPtr m_scheduler; |
Zhenkai Zhu | 74dd53c | 2013-01-10 23:39:57 -0800 | [diff] [blame] | 106 | StateMsgCallback m_stateMsgCallback; |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame^] | 107 | |
| 108 | Ccnx::Name m_syncPrefix; |
Zhenkai Zhu | 74dd53c | 2013-01-10 23:39:57 -0800 | [diff] [blame] | 109 | HashPtr m_rootHash; |
Zhenkai Zhu | 74dd53c | 2013-01-10 23:39:57 -0800 | [diff] [blame] | 110 | |
Zhenkai Zhu | e573ae8 | 2013-01-15 13:15:52 -0800 | [diff] [blame] | 111 | IntervalGeneratorPtr m_recoverWaitGenerator; |
Zhenkai Zhu | 8224bb6 | 2013-01-06 15:58:02 -0800 | [diff] [blame] | 112 | }; |
| 113 | |
| 114 | #endif // SYNC_CORE_H |