blob: bd7c5e8663889cbc64ecdf18199a3d60c4eb8ebe [file] [log] [blame]
/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
/*
* Copyright (c) 2013 University of California, Los Angeles
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation;
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Author: Zhenkai Zhu <zhenkai@cs.ucla.edu>
* Alexander Afanasyev <alexander.afanasyev@ucla.edu>
*/
#ifndef SYNC_CORE_H
#define SYNC_CORE_H
#include "sync-log.h"
#include "ccnx-wrapper.h"
#include "scheduler.h"
#include <boost/function.hpp>
class SyncCore
{
public:
typedef boost::function<void (const SyncStateMsgPtr & stateMsg) > StateMsgCallback;
static const int FRESHNESS = 2; // seconds
static const string RECOVER;
static const double WAIT; // seconds;
static const double RANDOM_PERCENT; // seconds;
public:
SyncCore(SyncLogPtr syncLog
, const Ccnx::Name &userName
, const Ccnx::Name &localPrefix // routable name used by the local user
, const Ccnx::Name &syncPrefix // the prefix for the sync collection
, const StateMsgCallback &callback // callback when state change is detected
, Ccnx::CcnxWrapperPtr ccnx
, SchedulerPtr scheduler);
~SyncCore();
void
updateLocalPrefix (const Ccnx::Name &localPrefix);
void
localStateChanged ();
void
updateLocalState (sqlite3_int64);
// ------------------ only used in test -------------------------
public:
HashPtr
root() const { return m_rootHash; }
sqlite3_int64
seq (const Ccnx::Name &name);
private:
void
handleInterest(const Ccnx::Name &name);
void
handleSyncData(const Ccnx::Name &name, Ccnx::PcoPtr content);
void
handleRecoverData(const Ccnx::Name &name, Ccnx::PcoPtr content);
Ccnx::Closure::TimeoutCallbackReturnValue
handleSyncInterestTimeout(const Ccnx::Name &name);
Ccnx::Closure::TimeoutCallbackReturnValue
handleRecoverInterestTimeout(const Ccnx::Name &name);
void
deregister(const Ccnx::Name &name);
void
recover(const HashPtr &hash);
private:
void
sendSyncInterest();
void
handleSyncInterest(const Ccnx::Name &name);
void
handleRecoverInterest(const Ccnx::Name &name);
void
handleStateData(const Ccnx::Bytes &content);
private:
Ccnx::CcnxWrapperPtr m_ccnx;
SyncLogPtr m_log;
SchedulerPtr m_scheduler;
StateMsgCallback m_stateMsgCallback;
Ccnx::Name m_syncPrefix;
HashPtr m_rootHash;
IntervalGeneratorPtr m_recoverWaitGenerator;
};
#endif // SYNC_CORE_H