blob: b06b8b8f9346f74512a26e652443ac067749a4a0 [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;
38 typedef sqlite3_int64 seqno_t;
39 typedef Map<Name, Name> YellowPage;
40 typedef boost::shared_mutex Mutex;
41 typedef boost::shared_lock<Mutex> ReadLock;
42 typedef boost::unique_lock<Mutex> WriteLock;
43
44 static const int FRESHNESS = 2;
45
46public:
47 SyncCore(const string &path // path where SyncLog is stored
48 , const Name &userName // unique permanent name for local user
49 , const Name &localPrefix // routable name used by the local user
50 , const Name &syncPrefix // the prefix for the sync collection
51 , const StateMsgCallback &callback // callback when state change is detected
52 , CcnxWrapperPtr handle
53 , SchedulerPtr scheduler);
Zhenkai Zhu8224bb62013-01-06 15:58:02 -080054 ~SyncCore();
55
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -080056 // some other code should call this fuction when local prefix
57 // changes; e.g. when wake up in another network
58 void
59 updateLocalPrefix(const Name &localPrefix);
60
61 void
62 updateLocalState(seqno_t);
63
64 Name
65 yp(const Name &name);
66
67 void
68 handleSyncInterest(const Name &name);
69
70 Closure::TimeoutCallbackReturnValue
71 handleSyncInterestTimeout(const Name &name);
72
73 void
74 handleSyncData(const Name &name, const Bytes &content);
75
76 void
77 deregister();
78
79protected:
80 void
81 sendSyncInterest();
82
83 Name
84 constructSyncName(const HashPtr &hash);
85
86 static void
87 msgToBytes(const SyncStateMsgPtr &msg, Bytes &bytes);
88
Zhenkai Zhu8224bb62013-01-06 15:58:02 -080089protected:
90 SyncLog m_log;
Zhenkai Zhu66dc5a92013-01-08 21:41:15 -080091 SchedulerPtr m_scheduler;
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -080092 StateMsgCallback m_stateMsgCallback;
93 Name m_userName;
94 Name m_localPrefix;
95 Name m_syncPrefix;
96 HashPtr m_rootHash;
97 YellowPage m_yp;
98 Mutex m_ypMutex;
99 CcnxWrapperPtr m_handle;
100 Closure *m_interestClosure;
101
Zhenkai Zhu8224bb62013-01-06 15:58:02 -0800102};
103
104#endif // SYNC_CORE_H