blob: d8c96757b3de4406f3493effb76d9d214848f4b8 [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"
26#include <ccnx-wrapper.h>
Zhenkai Zhu66dc5a92013-01-08 21:41:15 -080027#include <event-scheduler.h>
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -080028#include <boost/function.hpp>
29#include <boost/thread/shared_mutex.hpp>
Zhenkai Zhu8224bb62013-01-06 15:58:02 -080030
31using namespace std;
32using namespace Ccnx;
33
34class SyncCore
35{
36public:
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -080037 typedef boost::function<void (const SyncStateMsgPtr & stateMsg) > StateMsgCallback;
Zhenkai Zhue851b952013-01-13 22:29:57 -080038 typedef map<Name, Name> YellowPage;
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -080039 typedef boost::shared_mutex Mutex;
40 typedef boost::shared_lock<Mutex> ReadLock;
41 typedef boost::unique_lock<Mutex> WriteLock;
42
Zhenkai Zhue851b952013-01-13 22:29:57 -080043 static const int FRESHNESS = 2; // seconds
44 static const string RECOVER;
45 static const double WAIT; // seconds;
46 static const double RANDOM_PERCENT; // seconds;
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -080047
48public:
49 SyncCore(const string &path // path where SyncLog is stored
50 , const Name &userName // unique permanent name for local user
51 , const Name &localPrefix // routable name used by the local user
52 , const Name &syncPrefix // the prefix for the sync collection
53 , const StateMsgCallback &callback // callback when state change is detected
Zhenkai Zhue573ae82013-01-15 13:15:52 -080054 , CcnxWrapperPtr handle);
Zhenkai Zhu8224bb62013-01-06 15:58:02 -080055 ~SyncCore();
56
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -080057 // some other code should call this fuction when local prefix
58 // changes; e.g. when wake up in another network
59 void
60 updateLocalPrefix(const Name &localPrefix);
61
62 void
Zhenkai Zhue851b952013-01-13 22:29:57 -080063 updateLocalState(sqlite3_int64);
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -080064
65 Name
66 yp(const Name &name);
67
68 void
Zhenkai Zhue851b952013-01-13 22:29:57 -080069 handleInterest(const Name &name);
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -080070
71 void
72 handleSyncData(const Name &name, const Bytes &content);
73
74 void
Zhenkai Zhue851b952013-01-13 22:29:57 -080075 handleRecoverData(const Name &name, const Bytes &content);
76
77 Closure::TimeoutCallbackReturnValue
78 handleSyncInterestTimeout(const Name &name);
79
80 Closure::TimeoutCallbackReturnValue
81 handleRecoverInterestTimeout(const Name &name);
82
83 void
84 deregister(const Name &name);
85
86 void
87 recover(const HashPtr &hash);
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -080088
Zhenkai Zhu9501b8b2013-01-17 12:37:00 -080089// ------------------ only used in test -------------------------
Zhenkai Zhue573ae82013-01-15 13:15:52 -080090 HashPtr
91 root() { return m_rootHash; }
92
Zhenkai Zhu9501b8b2013-01-17 12:37:00 -080093 sqlite3_int64
94 seq(const Name &name);
95
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -080096protected:
97 void
98 sendSyncInterest();
99
Zhenkai Zhue851b952013-01-13 22:29:57 -0800100 void
101 handleSyncInterest(const Name &name);
102
103 void
104 handleRecoverInterest(const Name &name);
105
106 void
107 handleStateData(const Bytes &content);
108
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800109 Name
110 constructSyncName(const HashPtr &hash);
111
112 static void
113 msgToBytes(const SyncStateMsgPtr &msg, Bytes &bytes);
114
Zhenkai Zhu8224bb62013-01-06 15:58:02 -0800115protected:
116 SyncLog m_log;
Zhenkai Zhu66dc5a92013-01-08 21:41:15 -0800117 SchedulerPtr m_scheduler;
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800118 StateMsgCallback m_stateMsgCallback;
119 Name m_userName;
120 Name m_localPrefix;
121 Name m_syncPrefix;
122 HashPtr m_rootHash;
123 YellowPage m_yp;
124 Mutex m_ypMutex;
125 CcnxWrapperPtr m_handle;
Zhenkai Zhue851b952013-01-13 22:29:57 -0800126 Closure *m_syncClosure;
127 Closure *m_recoverClosure;
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -0800128
Zhenkai Zhue573ae82013-01-15 13:15:52 -0800129 IntervalGeneratorPtr m_recoverWaitGenerator;
130
Zhenkai Zhu8224bb62013-01-06 15:58:02 -0800131};
132
133#endif // SYNC_CORE_H