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