blob: 0d7f98df8a147196ad3365fd28664d632f26aa01 [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 "sync-log.hpp"
25#include "ccnx-wrapper.hpp"
26#include "ccnx-selectors.hpp"
27#include "scheduler.hpp"
28#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 Afanasyevfc720362013-01-24 21:49:48 -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;
39 static const double WAIT; // seconds;
40 static const double RANDOM_PERCENT; // seconds;
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -080041
42public:
Zhenkai Zhub330aed2013-01-17 13:29:37 -080043 SyncCore(SyncLogPtr syncLog
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -070044 , const Ndnx::Name &userName
45 , const Ndnx::Name &localPrefix // routable name used by the local user
46 , const Ndnx::Name &syncPrefix // the prefix for the sync collection
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -080047 , const StateMsgCallback &callback // callback when state change is detected
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -070048 , Ndnx::NdnxWrapperPtr ndnx
Zhenkai Zhu95160102013-01-25 21:54:57 -080049 , double syncInterestInterval = -1.0);
Zhenkai Zhu8224bb62013-01-06 15:58:02 -080050 ~SyncCore();
51
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -080052 void
Alexander Afanasyevcbda9922013-01-22 11:21:12 -080053 localStateChanged ();
Zhenkai Zhu8224bb62013-01-06 15:58:02 -080054
Alexander Afanasyeva2fabcf2013-02-05 11:26:37 -080055 /**
56 * @brief Schedule an event to update local state with a small delay
57 *
58 * This call is preferred to localStateChanged if many local state updates
59 * are anticipated within a short period of time
60 */
61 void
62 localStateChangedDelayed ();
63
Alexander Afanasyev49a30d02013-01-21 21:38:48 -080064 void
65 updateLocalState (sqlite3_int64);
66
67// ------------------ only used in test -------------------------
68public:
69 HashPtr
70 root() const { return m_rootHash; }
71
72 sqlite3_int64
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -070073 seq (const Ndnx::Name &name);
Alexander Afanasyev49a30d02013-01-21 21:38:48 -080074
75private:
76 void
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -070077 handleInterest(const Ndnx::Name &name);
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -080078
79 void
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -070080 handleSyncData(const Ndnx::Name &name, Ndnx::PcoPtr content);
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -080081
82 void
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -070083 handleRecoverData(const Ndnx::Name &name, Ndnx::PcoPtr content);
Alexander Afanasyev49a30d02013-01-21 21:38:48 -080084
Zhenkai Zhuff4fa8a2013-01-28 22:02:40 -080085 void
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -070086 handleSyncInterestTimeout(const Ndnx::Name &name, const Ndnx::Closure &closure, Ndnx::Selectors selectors);
Alexander Afanasyev49a30d02013-01-21 21:38:48 -080087
Zhenkai Zhuff4fa8a2013-01-28 22:02:40 -080088 void
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -070089 handleRecoverInterestTimeout(const Ndnx::Name &name, const Ndnx::Closure &closure, Ndnx::Selectors selectors);
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -080090
91 void
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -070092 deregister(const Ndnx::Name &name);
Zhenkai Zhue851b952013-01-13 22:29:57 -080093
94 void
Alexander Afanasyev50526282013-01-26 13:43:57 -080095 recover(HashPtr hash);
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -080096
Alexander Afanasyevc507ac22013-01-21 16:01:58 -080097private:
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -080098 void
99 sendSyncInterest();
100
Zhenkai Zhue851b952013-01-13 22:29:57 -0800101 void
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -0700102 handleSyncInterest(const Ndnx::Name &name);
Zhenkai Zhue851b952013-01-13 22:29:57 -0800103
104 void
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -0700105 handleRecoverInterest(const Ndnx::Name &name);
Zhenkai Zhue851b952013-01-13 22:29:57 -0800106
107 void
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -0700108 handleStateData(const Ndnx::Bytes &content);
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800109
Alexander Afanasyevc507ac22013-01-21 16:01:58 -0800110private:
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -0700111 Ndnx::NdnxWrapperPtr m_ndnx;
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800112
Zhenkai Zhub330aed2013-01-17 13:29:37 -0800113 SyncLogPtr m_log;
Zhenkai Zhu66dc5a92013-01-08 21:41:15 -0800114 SchedulerPtr m_scheduler;
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800115 StateMsgCallback m_stateMsgCallback;
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800116
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -0700117 Ndnx::Name m_syncPrefix;
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800118 HashPtr m_rootHash;
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800119
Zhenkai Zhue573ae82013-01-15 13:15:52 -0800120 IntervalGeneratorPtr m_recoverWaitGenerator;
Zhenkai Zhu3b406592013-01-25 21:07:49 -0800121
122 TaskPtr m_sendSyncInterestTask;
Zhenkai Zhu95160102013-01-25 21:54:57 -0800123
124 double m_syncInterestInterval;
Zhenkai Zhu8224bb62013-01-06 15:58:02 -0800125};
126
127#endif // SYNC_CORE_H