blob: db20bbb944c0f32a6b7b1cb0f5184dbb565619b7 [file] [log] [blame]
Zhenkai Zhu8224bb62013-01-06 15:58:02 -08001/* -*- 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 *
Alexander Afanasyev8e2104a2013-01-22 10:56:18 -080018 * Author: Zhenkai Zhu <zhenkai@cs.ucla.edu>
19 * Alexander Afanasyev <alexander.afanasyev@ucla.edu>
Zhenkai Zhu8224bb62013-01-06 15:58:02 -080020 */
21
22#ifndef SYNC_CORE_H
23#define SYNC_CORE_H
24
25#include "sync-log.h"
Alexander Afanasyevabe952a2013-01-17 17:06:32 -080026#include "ccnx-wrapper.h"
27#include "scheduler.h"
Alexander Afanasyevabe952a2013-01-17 17:06:32 -080028
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -080029#include <boost/function.hpp>
Zhenkai Zhu8224bb62013-01-06 15:58:02 -080030
31class SyncCore
32{
33public:
Alexander Afanasyevfc720362013-01-24 21:49:48 -080034 typedef boost::function<void (SyncStateMsgPtr stateMsg) > StateMsgCallback;
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -080035
Zhenkai Zhue851b952013-01-13 22:29:57 -080036 static const int FRESHNESS = 2; // seconds
37 static const string RECOVER;
38 static const double WAIT; // seconds;
39 static const double RANDOM_PERCENT; // seconds;
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -080040
41public:
Zhenkai Zhub330aed2013-01-17 13:29:37 -080042 SyncCore(SyncLogPtr syncLog
Alexander Afanasyev49a30d02013-01-21 21:38:48 -080043 , const Ccnx::Name &userName
44 , const Ccnx::Name &localPrefix // routable name used by the local user
45 , const Ccnx::Name &syncPrefix // the prefix for the sync collection
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -080046 , const StateMsgCallback &callback // callback when state change is detected
Alexander Afanasyevd94a8c62013-01-24 13:53:40 -080047 , Ccnx::CcnxWrapperPtr ccnx);
Zhenkai Zhu8224bb62013-01-06 15:58:02 -080048 ~SyncCore();
49
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -080050 void
Alexander Afanasyevcbda9922013-01-22 11:21:12 -080051 updateLocalPrefix (const Ccnx::Name &localPrefix);
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -080052
53 void
Alexander Afanasyevcbda9922013-01-22 11:21:12 -080054 localStateChanged ();
Zhenkai Zhu8224bb62013-01-06 15:58:02 -080055
Alexander Afanasyev49a30d02013-01-21 21:38:48 -080056 void
57 updateLocalState (sqlite3_int64);
58
59// ------------------ only used in test -------------------------
60public:
61 HashPtr
62 root() const { return m_rootHash; }
63
64 sqlite3_int64
65 seq (const Ccnx::Name &name);
66
67private:
68 void
69 handleInterest(const Ccnx::Name &name);
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -080070
71 void
Alexander Afanasyev49a30d02013-01-21 21:38:48 -080072 handleSyncData(const Ccnx::Name &name, Ccnx::PcoPtr content);
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -080073
74 void
Alexander Afanasyev49a30d02013-01-21 21:38:48 -080075 handleRecoverData(const Ccnx::Name &name, Ccnx::PcoPtr content);
76
77 Ccnx::Closure::TimeoutCallbackReturnValue
78 handleSyncInterestTimeout(const Ccnx::Name &name);
79
80 Ccnx::Closure::TimeoutCallbackReturnValue
81 handleRecoverInterestTimeout(const Ccnx::Name &name);
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -080082
83 void
Alexander Afanasyev49a30d02013-01-21 21:38:48 -080084 deregister(const Ccnx::Name &name);
Zhenkai Zhue851b952013-01-13 22:29:57 -080085
86 void
87 recover(const HashPtr &hash);
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -080088
Alexander Afanasyevc507ac22013-01-21 16:01:58 -080089private:
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -080090 void
91 sendSyncInterest();
92
Zhenkai Zhue851b952013-01-13 22:29:57 -080093 void
Alexander Afanasyev49a30d02013-01-21 21:38:48 -080094 handleSyncInterest(const Ccnx::Name &name);
Zhenkai Zhue851b952013-01-13 22:29:57 -080095
96 void
Alexander Afanasyev49a30d02013-01-21 21:38:48 -080097 handleRecoverInterest(const Ccnx::Name &name);
Zhenkai Zhue851b952013-01-13 22:29:57 -080098
99 void
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800100 handleStateData(const Ccnx::Bytes &content);
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800101
Alexander Afanasyevc507ac22013-01-21 16:01:58 -0800102private:
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800103 Ccnx::CcnxWrapperPtr m_ccnx;
104
Zhenkai Zhub330aed2013-01-17 13:29:37 -0800105 SyncLogPtr m_log;
Zhenkai Zhu66dc5a92013-01-08 21:41:15 -0800106 SchedulerPtr m_scheduler;
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800107 StateMsgCallback m_stateMsgCallback;
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800108
109 Ccnx::Name m_syncPrefix;
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800110 HashPtr m_rootHash;
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800111
Zhenkai Zhue573ae82013-01-15 13:15:52 -0800112 IntervalGeneratorPtr m_recoverWaitGenerator;
Zhenkai Zhu8224bb62013-01-06 15:58:02 -0800113};
114
115#endif // SYNC_CORE_H