Alexander Afanasyev | fa2f662 | 2016-12-25 12:28:00 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2013-2016, Regents of the University of California. |
Zhenkai Zhu | 8224bb6 | 2013-01-06 15:58:02 -0800 | [diff] [blame] | 4 | * |
Alexander Afanasyev | fa2f662 | 2016-12-25 12:28:00 -0800 | [diff] [blame] | 5 | * This file is part of ChronoShare, a decentralized file sharing application over NDN. |
Zhenkai Zhu | 8224bb6 | 2013-01-06 15:58:02 -0800 | [diff] [blame] | 6 | * |
Alexander Afanasyev | fa2f662 | 2016-12-25 12:28:00 -0800 | [diff] [blame] | 7 | * ChronoShare is free software: you can redistribute it and/or modify it under the terms |
| 8 | * of the GNU General Public License as published by the Free Software Foundation, either |
| 9 | * version 3 of the License, or (at your option) any later version. |
Zhenkai Zhu | 8224bb6 | 2013-01-06 15:58:02 -0800 | [diff] [blame] | 10 | * |
Alexander Afanasyev | fa2f662 | 2016-12-25 12:28:00 -0800 | [diff] [blame] | 11 | * ChronoShare is distributed in the hope that it will be useful, but WITHOUT ANY |
| 12 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 13 | * PARTICULAR PURPOSE. See the GNU General Public License for more details. |
Zhenkai Zhu | 8224bb6 | 2013-01-06 15:58:02 -0800 | [diff] [blame] | 14 | * |
Alexander Afanasyev | fa2f662 | 2016-12-25 12:28:00 -0800 | [diff] [blame] | 15 | * You should have received copies of the GNU General Public License along with |
| 16 | * ChronoShare, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 17 | * |
| 18 | * See AUTHORS.md for complete list of ChronoShare authors and contributors. |
Zhenkai Zhu | 8224bb6 | 2013-01-06 15:58:02 -0800 | [diff] [blame] | 19 | */ |
| 20 | |
| 21 | #ifndef SYNC_CORE_H |
| 22 | #define SYNC_CORE_H |
| 23 | |
Alexander Afanasyev | f4cde4e | 2016-12-25 13:42:57 -0800 | [diff] [blame] | 24 | #include "ccnx-selectors.hpp" |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 25 | #include "ccnx-wrapper.hpp" |
Alexander Afanasyev | f4cde4e | 2016-12-25 13:42:57 -0800 | [diff] [blame] | 26 | #include "scheduler.hpp" |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 27 | #include "sync-log.hpp" |
Alexander Afanasyev | f4cde4e | 2016-12-25 13:42:57 -0800 | [diff] [blame] | 28 | #include "task.hpp" |
Alexander Afanasyev | abe952a | 2013-01-17 17:06:32 -0800 | [diff] [blame] | 29 | |
Zhenkai Zhu | 74dd53c | 2013-01-10 23:39:57 -0800 | [diff] [blame] | 30 | #include <boost/function.hpp> |
Zhenkai Zhu | 8224bb6 | 2013-01-06 15:58:02 -0800 | [diff] [blame] | 31 | |
| 32 | class SyncCore |
| 33 | { |
| 34 | public: |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 35 | typedef boost::function<void(SyncStateMsgPtr stateMsg)> StateMsgCallback; |
Zhenkai Zhu | 74dd53c | 2013-01-10 23:39:57 -0800 | [diff] [blame] | 36 | |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame] | 37 | static const int FRESHNESS = 2; // seconds |
| 38 | static const string RECOVER; |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 39 | static const double WAIT; // seconds; |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame] | 40 | static const double RANDOM_PERCENT; // seconds; |
Zhenkai Zhu | 74dd53c | 2013-01-10 23:39:57 -0800 | [diff] [blame] | 41 | |
| 42 | public: |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 43 | SyncCore(SyncLogPtr syncLog, const Ccnx::Name& userName, |
| 44 | const Ccnx::Name& localPrefix // routable name used by the local user |
| 45 | , |
| 46 | const Ccnx::Name& syncPrefix // the prefix for the sync collection |
| 47 | , |
| 48 | const StateMsgCallback& callback // callback when state change is detected |
| 49 | , |
| 50 | Ccnx::CcnxWrapperPtr ccnx, 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 | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 54 | localStateChanged(); |
Zhenkai Zhu | 8224bb6 | 2013-01-06 15:58:02 -0800 | [diff] [blame] | 55 | |
Alexander Afanasyev | a2fabcf | 2013-02-05 11:26:37 -0800 | [diff] [blame] | 56 | /** |
| 57 | * @brief Schedule an event to update local state with a small delay |
| 58 | * |
| 59 | * This call is preferred to localStateChanged if many local state updates |
| 60 | * are anticipated within a short period of time |
| 61 | */ |
| 62 | void |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 63 | localStateChangedDelayed(); |
Alexander Afanasyev | a2fabcf | 2013-02-05 11:26:37 -0800 | [diff] [blame] | 64 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 65 | void updateLocalState(sqlite3_int64); |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 66 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 67 | // ------------------ only used in test ------------------------- |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 68 | public: |
| 69 | HashPtr |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 70 | root() const |
| 71 | { |
| 72 | return m_rootHash; |
| 73 | } |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 74 | |
| 75 | sqlite3_int64 |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 76 | seq(const Ccnx::Name& name); |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 77 | |
| 78 | private: |
| 79 | void |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 80 | handleInterest(const Ccnx::Name& name); |
Zhenkai Zhu | 74dd53c | 2013-01-10 23:39:57 -0800 | [diff] [blame] | 81 | |
| 82 | void |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 83 | handleSyncData(const Ccnx::Name& name, Ccnx::PcoPtr content); |
Zhenkai Zhu | 74dd53c | 2013-01-10 23:39:57 -0800 | [diff] [blame] | 84 | |
| 85 | void |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 86 | handleRecoverData(const Ccnx::Name& name, Ccnx::PcoPtr content); |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 87 | |
Zhenkai Zhu | ff4fa8a | 2013-01-28 22:02:40 -0800 | [diff] [blame] | 88 | void |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 89 | handleSyncInterestTimeout(const Ccnx::Name& name, const Ccnx::Closure& closure, |
| 90 | Ccnx::Selectors selectors); |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 91 | |
Zhenkai Zhu | ff4fa8a | 2013-01-28 22:02:40 -0800 | [diff] [blame] | 92 | void |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 93 | handleRecoverInterestTimeout(const Ccnx::Name& name, const Ccnx::Closure& closure, |
| 94 | Ccnx::Selectors selectors); |
Zhenkai Zhu | 74dd53c | 2013-01-10 23:39:57 -0800 | [diff] [blame] | 95 | |
| 96 | void |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 97 | deregister(const Ccnx::Name& name); |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame] | 98 | |
| 99 | void |
Alexander Afanasyev | 5052628 | 2013-01-26 13:43:57 -0800 | [diff] [blame] | 100 | recover(HashPtr hash); |
Zhenkai Zhu | 74dd53c | 2013-01-10 23:39:57 -0800 | [diff] [blame] | 101 | |
Alexander Afanasyev | c507ac2 | 2013-01-21 16:01:58 -0800 | [diff] [blame] | 102 | private: |
Zhenkai Zhu | 74dd53c | 2013-01-10 23:39:57 -0800 | [diff] [blame] | 103 | void |
| 104 | sendSyncInterest(); |
| 105 | |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame] | 106 | void |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 107 | handleSyncInterest(const Ccnx::Name& name); |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame] | 108 | |
| 109 | void |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 110 | handleRecoverInterest(const Ccnx::Name& name); |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame] | 111 | |
| 112 | void |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 113 | handleStateData(const Ccnx::Bytes& content); |
Zhenkai Zhu | 74dd53c | 2013-01-10 23:39:57 -0800 | [diff] [blame] | 114 | |
Alexander Afanasyev | c507ac2 | 2013-01-21 16:01:58 -0800 | [diff] [blame] | 115 | private: |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame] | 116 | Ndnx::NdnxWrapperPtr m_ndnx; |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 117 | |
Zhenkai Zhu | b330aed | 2013-01-17 13:29:37 -0800 | [diff] [blame] | 118 | SyncLogPtr m_log; |
Zhenkai Zhu | 66dc5a9 | 2013-01-08 21:41:15 -0800 | [diff] [blame] | 119 | SchedulerPtr m_scheduler; |
Zhenkai Zhu | 74dd53c | 2013-01-10 23:39:57 -0800 | [diff] [blame] | 120 | StateMsgCallback m_stateMsgCallback; |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 121 | |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame] | 122 | Ndnx::Name m_syncPrefix; |
Zhenkai Zhu | 74dd53c | 2013-01-10 23:39:57 -0800 | [diff] [blame] | 123 | HashPtr m_rootHash; |
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; |
Zhenkai Zhu | 3b40659 | 2013-01-25 21:07:49 -0800 | [diff] [blame] | 126 | |
| 127 | TaskPtr m_sendSyncInterestTask; |
Zhenkai Zhu | 9516010 | 2013-01-25 21:54:57 -0800 | [diff] [blame] | 128 | |
| 129 | double m_syncInterestInterval; |
Zhenkai Zhu | 8224bb6 | 2013-01-06 15:58:02 -0800 | [diff] [blame] | 130 | }; |
| 131 | |
| 132 | #endif // SYNC_CORE_H |