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