blob: 7f1071761a3e6b427f29f3359058384789ffc2d2 [file] [log] [blame]
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2013-2016, Regents of the University of California.
Zhenkai Zhu8224bb62013-01-06 15:58:02 -08004 *
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -08005 * This file is part of ChronoShare, a decentralized file sharing application over NDN.
Zhenkai Zhu8224bb62013-01-06 15:58:02 -08006 *
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -08007 * 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 Zhu8224bb62013-01-06 15:58:02 -080010 *
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -080011 * 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 Zhu8224bb62013-01-06 15:58:02 -080014 *
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -080015 * 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 Zhu8224bb62013-01-06 15:58:02 -080019 */
20
21#ifndef SYNC_CORE_H
22#define SYNC_CORE_H
23
Alexander Afanasyevf4cde4e2016-12-25 13:42:57 -080024#include "ccnx-selectors.hpp"
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080025#include "ccnx-wrapper.hpp"
Alexander Afanasyevf4cde4e2016-12-25 13:42:57 -080026#include "scheduler.hpp"
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080027#include "sync-log.hpp"
Alexander Afanasyevf4cde4e2016-12-25 13:42:57 -080028#include "task.hpp"
Alexander Afanasyevabe952a2013-01-17 17:06:32 -080029
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -080030#include <boost/function.hpp>
Zhenkai Zhu8224bb62013-01-06 15:58:02 -080031
32class SyncCore
33{
34public:
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080035 typedef boost::function<void(SyncStateMsgPtr stateMsg)> StateMsgCallback;
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -080036
Zhenkai Zhue851b952013-01-13 22:29:57 -080037 static const int FRESHNESS = 2; // seconds
38 static const string RECOVER;
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080039 static const double WAIT; // seconds;
Zhenkai Zhue851b952013-01-13 22:29:57 -080040 static const double RANDOM_PERCENT; // seconds;
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -080041
42public:
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080043 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 Zhu8224bb62013-01-06 15:58:02 -080051 ~SyncCore();
52
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -080053 void
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080054 localStateChanged();
Zhenkai Zhu8224bb62013-01-06 15:58:02 -080055
Alexander Afanasyeva2fabcf2013-02-05 11:26:37 -080056 /**
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 Afanasyeveda3b7a2016-12-25 11:26:40 -080063 localStateChangedDelayed();
Alexander Afanasyeva2fabcf2013-02-05 11:26:37 -080064
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080065 void updateLocalState(sqlite3_int64);
Alexander Afanasyev49a30d02013-01-21 21:38:48 -080066
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080067 // ------------------ only used in test -------------------------
Alexander Afanasyev49a30d02013-01-21 21:38:48 -080068public:
69 HashPtr
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080070 root() const
71 {
72 return m_rootHash;
73 }
Alexander Afanasyev49a30d02013-01-21 21:38:48 -080074
75 sqlite3_int64
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080076 seq(const Ccnx::Name& name);
Alexander Afanasyev49a30d02013-01-21 21:38:48 -080077
78private:
79 void
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080080 handleInterest(const Ccnx::Name& name);
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -080081
82 void
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080083 handleSyncData(const Ccnx::Name& name, Ccnx::PcoPtr content);
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -080084
85 void
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080086 handleRecoverData(const Ccnx::Name& name, Ccnx::PcoPtr content);
Alexander Afanasyev49a30d02013-01-21 21:38:48 -080087
Zhenkai Zhuff4fa8a2013-01-28 22:02:40 -080088 void
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080089 handleSyncInterestTimeout(const Ccnx::Name& name, const Ccnx::Closure& closure,
90 Ccnx::Selectors selectors);
Alexander Afanasyev49a30d02013-01-21 21:38:48 -080091
Zhenkai Zhuff4fa8a2013-01-28 22:02:40 -080092 void
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080093 handleRecoverInterestTimeout(const Ccnx::Name& name, const Ccnx::Closure& closure,
94 Ccnx::Selectors selectors);
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -080095
96 void
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080097 deregister(const Ccnx::Name& name);
Zhenkai Zhue851b952013-01-13 22:29:57 -080098
99 void
Alexander Afanasyev50526282013-01-26 13:43:57 -0800100 recover(HashPtr hash);
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800101
Alexander Afanasyevc507ac22013-01-21 16:01:58 -0800102private:
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800103 void
104 sendSyncInterest();
105
Zhenkai Zhue851b952013-01-13 22:29:57 -0800106 void
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800107 handleSyncInterest(const Ccnx::Name& name);
Zhenkai Zhue851b952013-01-13 22:29:57 -0800108
109 void
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800110 handleRecoverInterest(const Ccnx::Name& name);
Zhenkai Zhue851b952013-01-13 22:29:57 -0800111
112 void
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800113 handleStateData(const Ccnx::Bytes& content);
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800114
Alexander Afanasyevc507ac22013-01-21 16:01:58 -0800115private:
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -0700116 Ndnx::NdnxWrapperPtr m_ndnx;
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800117
Zhenkai Zhub330aed2013-01-17 13:29:37 -0800118 SyncLogPtr m_log;
Zhenkai Zhu66dc5a92013-01-08 21:41:15 -0800119 SchedulerPtr m_scheduler;
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800120 StateMsgCallback m_stateMsgCallback;
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800121
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -0700122 Ndnx::Name m_syncPrefix;
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800123 HashPtr m_rootHash;
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800124
Zhenkai Zhue573ae82013-01-15 13:15:52 -0800125 IntervalGeneratorPtr m_recoverWaitGenerator;
Zhenkai Zhu3b406592013-01-25 21:07:49 -0800126
127 TaskPtr m_sendSyncInterestTask;
Zhenkai Zhu95160102013-01-25 21:54:57 -0800128
129 double m_syncInterestInterval;
Zhenkai Zhu8224bb62013-01-06 15:58:02 -0800130};
131
132#endif // SYNC_CORE_H