blob: cb44398532f09a3f3bc712b47099bd2048e2eba8 [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 *
18 * Zhenkai Zhu <zhenkai@cs.ucla.edu>
19 * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
20 */
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"
28#include "interval-generator.h"
29
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -080030#include <boost/function.hpp>
31#include <boost/thread/shared_mutex.hpp>
Zhenkai Zhu8224bb62013-01-06 15:58:02 -080032
33using namespace std;
34using namespace Ccnx;
35
36class SyncCore
37{
38public:
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -080039 typedef boost::function<void (const SyncStateMsgPtr & stateMsg) > StateMsgCallback;
Zhenkai Zhue851b952013-01-13 22:29:57 -080040 typedef map<Name, Name> YellowPage;
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -080041 typedef boost::shared_mutex Mutex;
42 typedef boost::shared_lock<Mutex> ReadLock;
43 typedef boost::unique_lock<Mutex> WriteLock;
44
Zhenkai Zhue851b952013-01-13 22:29:57 -080045 static const int FRESHNESS = 2; // seconds
46 static const string RECOVER;
47 static const double WAIT; // seconds;
48 static const double RANDOM_PERCENT; // seconds;
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -080049
50public:
Zhenkai Zhub330aed2013-01-17 13:29:37 -080051 SyncCore(SyncLogPtr syncLog
52 , const Name &userName
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -080053 , const Name &localPrefix // routable name used by the local user
54 , const Name &syncPrefix // the prefix for the sync collection
55 , const StateMsgCallback &callback // callback when state change is detected
Zhenkai Zhub330aed2013-01-17 13:29:37 -080056 , const CcnxWrapperPtr &handle
57 , const SchedulerPtr &scheduler);
Zhenkai Zhu8224bb62013-01-06 15:58:02 -080058 ~SyncCore();
59
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -080060 // some other code should call this fuction when local prefix
61 // changes; e.g. when wake up in another network
62 void
63 updateLocalPrefix(const Name &localPrefix);
64
65 void
Zhenkai Zhue851b952013-01-13 22:29:57 -080066 updateLocalState(sqlite3_int64);
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -080067
68 Name
69 yp(const Name &name);
70
71 void
Zhenkai Zhue851b952013-01-13 22:29:57 -080072 handleInterest(const Name &name);
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -080073
74 void
75 handleSyncData(const Name &name, const Bytes &content);
76
77 void
Zhenkai Zhue851b952013-01-13 22:29:57 -080078 handleRecoverData(const Name &name, const Bytes &content);
79
80 Closure::TimeoutCallbackReturnValue
81 handleSyncInterestTimeout(const Name &name);
82
83 Closure::TimeoutCallbackReturnValue
84 handleRecoverInterestTimeout(const Name &name);
85
86 void
87 deregister(const Name &name);
88
89 void
90 recover(const HashPtr &hash);
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -080091
Zhenkai Zhu9501b8b2013-01-17 12:37:00 -080092// ------------------ only used in test -------------------------
Zhenkai Zhue573ae82013-01-15 13:15:52 -080093 HashPtr
94 root() { return m_rootHash; }
95
Zhenkai Zhu9501b8b2013-01-17 12:37:00 -080096 sqlite3_int64
97 seq(const Name &name);
98
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -080099protected:
100 void
101 sendSyncInterest();
102
Zhenkai Zhue851b952013-01-13 22:29:57 -0800103 void
104 handleSyncInterest(const Name &name);
105
106 void
107 handleRecoverInterest(const Name &name);
108
109 void
110 handleStateData(const Bytes &content);
111
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800112 Name
113 constructSyncName(const HashPtr &hash);
114
115 static void
116 msgToBytes(const SyncStateMsgPtr &msg, Bytes &bytes);
117
Zhenkai Zhu8224bb62013-01-06 15:58:02 -0800118protected:
Zhenkai Zhub330aed2013-01-17 13:29:37 -0800119 SyncLogPtr m_log;
Zhenkai Zhu66dc5a92013-01-08 21:41:15 -0800120 SchedulerPtr m_scheduler;
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800121 StateMsgCallback m_stateMsgCallback;
122 Name m_userName;
123 Name m_localPrefix;
124 Name m_syncPrefix;
125 HashPtr m_rootHash;
126 YellowPage m_yp;
127 Mutex m_ypMutex;
128 CcnxWrapperPtr m_handle;
Alexander Afanasyevd6c2a902013-01-19 21:24:30 -0800129 Closure m_syncClosure;
130 Closure m_recoverClosure;
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800131
Zhenkai Zhue573ae82013-01-15 13:15:52 -0800132 IntervalGeneratorPtr m_recoverWaitGenerator;
133
Zhenkai Zhu8224bb62013-01-06 15:58:02 -0800134};
135
136#endif // SYNC_CORE_H