blob: 7cf43682d9525b4a3a14bfccad2f956d48f2bcf8 [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"
Zhenkai Zhuff4fa8a2013-01-28 22:02:40 -080027#include "ccnx-selectors.h"
Alexander Afanasyevabe952a2013-01-17 17:06:32 -080028#include "scheduler.h"
Zhenkai Zhu3b406592013-01-25 21:07:49 -080029#include "task.h"
Alexander Afanasyevabe952a2013-01-17 17:06:32 -080030
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -080031#include <boost/function.hpp>
Zhenkai Zhu8224bb62013-01-06 15:58:02 -080032
33class SyncCore
34{
35public:
Alexander Afanasyevfc720362013-01-24 21:49:48 -080036 typedef boost::function<void (SyncStateMsgPtr stateMsg) > StateMsgCallback;
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -080037
Zhenkai Zhue851b952013-01-13 22:29:57 -080038 static const int FRESHNESS = 2; // seconds
39 static const string RECOVER;
40 static const double WAIT; // seconds;
41 static const double RANDOM_PERCENT; // seconds;
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -080042
43public:
Zhenkai Zhub330aed2013-01-17 13:29:37 -080044 SyncCore(SyncLogPtr syncLog
Alexander Afanasyev49a30d02013-01-21 21:38:48 -080045 , const Ccnx::Name &userName
46 , const Ccnx::Name &localPrefix // routable name used by the local user
47 , const Ccnx::Name &syncPrefix // the prefix for the sync collection
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -080048 , const StateMsgCallback &callback // callback when state change is detected
Zhenkai Zhu95160102013-01-25 21:54:57 -080049 , Ccnx::CcnxWrapperPtr ccnx
50 , double syncInterestInterval = -1.0);
Zhenkai Zhu8224bb62013-01-06 15:58:02 -080051 ~SyncCore();
52
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -080053 void
Alexander Afanasyevcbda9922013-01-22 11:21:12 -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
63 localStateChangedDelayed ();
64
Alexander Afanasyev49a30d02013-01-21 21:38:48 -080065 void
66 updateLocalState (sqlite3_int64);
67
68// ------------------ only used in test -------------------------
69public:
70 HashPtr
71 root() const { return m_rootHash; }
72
73 sqlite3_int64
74 seq (const Ccnx::Name &name);
75
76private:
77 void
78 handleInterest(const Ccnx::Name &name);
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -080079
80 void
Alexander Afanasyev49a30d02013-01-21 21:38:48 -080081 handleSyncData(const Ccnx::Name &name, Ccnx::PcoPtr content);
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -080082
83 void
Alexander Afanasyev49a30d02013-01-21 21:38:48 -080084 handleRecoverData(const Ccnx::Name &name, Ccnx::PcoPtr content);
85
Zhenkai Zhuff4fa8a2013-01-28 22:02:40 -080086 void
87 handleSyncInterestTimeout(const Ccnx::Name &name, const Ccnx::Closure &closure, Ccnx::Selectors selectors);
Alexander Afanasyev49a30d02013-01-21 21:38:48 -080088
Zhenkai Zhuff4fa8a2013-01-28 22:02:40 -080089 void
90 handleRecoverInterestTimeout(const Ccnx::Name &name, const Ccnx::Closure &closure, Ccnx::Selectors selectors);
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -080091
92 void
Alexander Afanasyev49a30d02013-01-21 21:38:48 -080093 deregister(const Ccnx::Name &name);
Zhenkai Zhue851b952013-01-13 22:29:57 -080094
95 void
Alexander Afanasyev50526282013-01-26 13:43:57 -080096 recover(HashPtr hash);
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -080097
Alexander Afanasyevc507ac22013-01-21 16:01:58 -080098private:
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -080099 void
100 sendSyncInterest();
101
Zhenkai Zhue851b952013-01-13 22:29:57 -0800102 void
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800103 handleSyncInterest(const Ccnx::Name &name);
Zhenkai Zhue851b952013-01-13 22:29:57 -0800104
105 void
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800106 handleRecoverInterest(const Ccnx::Name &name);
Zhenkai Zhue851b952013-01-13 22:29:57 -0800107
108 void
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800109 handleStateData(const Ccnx::Bytes &content);
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800110
Alexander Afanasyevc507ac22013-01-21 16:01:58 -0800111private:
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800112 Ccnx::CcnxWrapperPtr m_ccnx;
113
Zhenkai Zhub330aed2013-01-17 13:29:37 -0800114 SyncLogPtr m_log;
Zhenkai Zhu66dc5a92013-01-08 21:41:15 -0800115 SchedulerPtr m_scheduler;
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800116 StateMsgCallback m_stateMsgCallback;
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800117
118 Ccnx::Name m_syncPrefix;
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800119 HashPtr m_rootHash;
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800120
Zhenkai Zhue573ae82013-01-15 13:15:52 -0800121 IntervalGeneratorPtr m_recoverWaitGenerator;
Zhenkai Zhu3b406592013-01-25 21:07:49 -0800122
123 TaskPtr m_sendSyncInterestTask;
Zhenkai Zhu95160102013-01-25 21:54:57 -0800124
125 double m_syncInterestInterval;
Zhenkai Zhu8224bb62013-01-06 15:58:02 -0800126};
127
128#endif // SYNC_CORE_H