blob: edee04a3a413e25c87b4e38d8bfaa921a5e49001 [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 Zhuc3fd51e2013-01-22 10:45:54 -080066 localStateChanged();
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 Zhuc3fd51e2013-01-22 10:45:54 -080099 void
100 updateLocalState(sqlite3_int64);
101
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800102protected:
103 void
104 sendSyncInterest();
105
Zhenkai Zhue851b952013-01-13 22:29:57 -0800106 void
107 handleSyncInterest(const Name &name);
108
109 void
110 handleRecoverInterest(const Name &name);
111
112 void
113 handleStateData(const Bytes &content);
114
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800115 Name
116 constructSyncName(const HashPtr &hash);
117
118 static void
119 msgToBytes(const SyncStateMsgPtr &msg, Bytes &bytes);
120
Zhenkai Zhu8224bb62013-01-06 15:58:02 -0800121protected:
Zhenkai Zhub330aed2013-01-17 13:29:37 -0800122 SyncLogPtr m_log;
Zhenkai Zhu66dc5a92013-01-08 21:41:15 -0800123 SchedulerPtr m_scheduler;
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800124 StateMsgCallback m_stateMsgCallback;
125 Name m_userName;
126 Name m_localPrefix;
127 Name m_syncPrefix;
128 HashPtr m_rootHash;
129 YellowPage m_yp;
130 Mutex m_ypMutex;
131 CcnxWrapperPtr m_handle;
Zhenkai Zhue851b952013-01-13 22:29:57 -0800132 Closure *m_syncClosure;
133 Closure *m_recoverClosure;
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800134
Zhenkai Zhue573ae82013-01-15 13:15:52 -0800135 IntervalGeneratorPtr m_recoverWaitGenerator;
136
Zhenkai Zhu8224bb62013-01-06 15:58:02 -0800137};
138
139#endif // SYNC_CORE_H