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" |
| 28 | #include "interval-generator.h" |
| 29 | |
Zhenkai Zhu | 74dd53c | 2013-01-10 23:39:57 -0800 | [diff] [blame] | 30 | #include <boost/function.hpp> |
| 31 | #include <boost/thread/shared_mutex.hpp> |
Zhenkai Zhu | 8224bb6 | 2013-01-06 15:58:02 -0800 | [diff] [blame] | 32 | |
| 33 | using namespace std; |
| 34 | using namespace Ccnx; |
| 35 | |
| 36 | class SyncCore |
| 37 | { |
| 38 | public: |
Zhenkai Zhu | 74dd53c | 2013-01-10 23:39:57 -0800 | [diff] [blame] | 39 | typedef boost::function<void (const SyncStateMsgPtr & stateMsg) > StateMsgCallback; |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame] | 40 | typedef map<Name, Name> YellowPage; |
Zhenkai Zhu | 74dd53c | 2013-01-10 23:39:57 -0800 | [diff] [blame] | 41 | typedef boost::shared_mutex Mutex; |
| 42 | typedef boost::shared_lock<Mutex> ReadLock; |
| 43 | typedef boost::unique_lock<Mutex> WriteLock; |
| 44 | |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame] | 45 | static const int FRESHNESS = 2; // seconds |
| 46 | static const string RECOVER; |
| 47 | static const double WAIT; // seconds; |
| 48 | static const double RANDOM_PERCENT; // seconds; |
Zhenkai Zhu | 74dd53c | 2013-01-10 23:39:57 -0800 | [diff] [blame] | 49 | |
| 50 | public: |
Zhenkai Zhu | b330aed | 2013-01-17 13:29:37 -0800 | [diff] [blame] | 51 | SyncCore(SyncLogPtr syncLog |
| 52 | , const Name &userName |
Zhenkai Zhu | 74dd53c | 2013-01-10 23:39:57 -0800 | [diff] [blame] | 53 | , const Name &localPrefix // routable name used by the local user |
| 54 | , const Name &syncPrefix // the prefix for the sync collection |
| 55 | , const StateMsgCallback &callback // callback when state change is detected |
Zhenkai Zhu | b330aed | 2013-01-17 13:29:37 -0800 | [diff] [blame] | 56 | , const CcnxWrapperPtr &handle |
| 57 | , const SchedulerPtr &scheduler); |
Zhenkai Zhu | 8224bb6 | 2013-01-06 15:58:02 -0800 | [diff] [blame] | 58 | ~SyncCore(); |
| 59 | |
Zhenkai Zhu | 74dd53c | 2013-01-10 23:39:57 -0800 | [diff] [blame] | 60 | // some other code should call this fuction when local prefix |
| 61 | // changes; e.g. when wake up in another network |
| 62 | void |
| 63 | updateLocalPrefix(const Name &localPrefix); |
| 64 | |
| 65 | void |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame] | 66 | updateLocalState(sqlite3_int64); |
Zhenkai Zhu | 74dd53c | 2013-01-10 23:39:57 -0800 | [diff] [blame] | 67 | |
| 68 | Name |
| 69 | yp(const Name &name); |
| 70 | |
| 71 | void |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame] | 72 | handleInterest(const Name &name); |
Zhenkai Zhu | 74dd53c | 2013-01-10 23:39:57 -0800 | [diff] [blame] | 73 | |
| 74 | void |
| 75 | handleSyncData(const Name &name, const Bytes &content); |
| 76 | |
| 77 | void |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame] | 78 | handleRecoverData(const Name &name, const Bytes &content); |
| 79 | |
| 80 | Closure::TimeoutCallbackReturnValue |
| 81 | handleSyncInterestTimeout(const Name &name); |
| 82 | |
| 83 | Closure::TimeoutCallbackReturnValue |
| 84 | handleRecoverInterestTimeout(const Name &name); |
| 85 | |
| 86 | void |
| 87 | deregister(const Name &name); |
| 88 | |
| 89 | void |
| 90 | recover(const HashPtr &hash); |
Zhenkai Zhu | 74dd53c | 2013-01-10 23:39:57 -0800 | [diff] [blame] | 91 | |
Zhenkai Zhu | 9501b8b | 2013-01-17 12:37:00 -0800 | [diff] [blame] | 92 | // ------------------ only used in test ------------------------- |
Zhenkai Zhu | e573ae8 | 2013-01-15 13:15:52 -0800 | [diff] [blame] | 93 | HashPtr |
| 94 | root() { return m_rootHash; } |
| 95 | |
Zhenkai Zhu | 9501b8b | 2013-01-17 12:37:00 -0800 | [diff] [blame] | 96 | sqlite3_int64 |
| 97 | seq(const Name &name); |
| 98 | |
Zhenkai Zhu | 74dd53c | 2013-01-10 23:39:57 -0800 | [diff] [blame] | 99 | protected: |
| 100 | void |
| 101 | sendSyncInterest(); |
| 102 | |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame] | 103 | void |
| 104 | handleSyncInterest(const Name &name); |
| 105 | |
| 106 | void |
| 107 | handleRecoverInterest(const Name &name); |
| 108 | |
| 109 | void |
| 110 | handleStateData(const Bytes &content); |
| 111 | |
Zhenkai Zhu | 74dd53c | 2013-01-10 23:39:57 -0800 | [diff] [blame] | 112 | Name |
| 113 | constructSyncName(const HashPtr &hash); |
| 114 | |
| 115 | static void |
| 116 | msgToBytes(const SyncStateMsgPtr &msg, Bytes &bytes); |
| 117 | |
Zhenkai Zhu | 8224bb6 | 2013-01-06 15:58:02 -0800 | [diff] [blame] | 118 | protected: |
Zhenkai Zhu | b330aed | 2013-01-17 13:29:37 -0800 | [diff] [blame] | 119 | SyncLogPtr m_log; |
Zhenkai Zhu | 66dc5a9 | 2013-01-08 21:41:15 -0800 | [diff] [blame] | 120 | SchedulerPtr m_scheduler; |
Zhenkai Zhu | 74dd53c | 2013-01-10 23:39:57 -0800 | [diff] [blame] | 121 | StateMsgCallback m_stateMsgCallback; |
| 122 | Name m_userName; |
| 123 | Name m_localPrefix; |
| 124 | Name m_syncPrefix; |
| 125 | HashPtr m_rootHash; |
| 126 | YellowPage m_yp; |
| 127 | Mutex m_ypMutex; |
| 128 | CcnxWrapperPtr m_handle; |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame] | 129 | Closure *m_syncClosure; |
| 130 | Closure *m_recoverClosure; |
Zhenkai Zhu | 74dd53c | 2013-01-10 23:39:57 -0800 | [diff] [blame] | 131 | |
Zhenkai Zhu | e573ae8 | 2013-01-15 13:15:52 -0800 | [diff] [blame] | 132 | IntervalGeneratorPtr m_recoverWaitGenerator; |
| 133 | |
Zhenkai Zhu | 8224bb6 | 2013-01-06 15:58:02 -0800 | [diff] [blame] | 134 | }; |
| 135 | |
| 136 | #endif // SYNC_CORE_H |