blob: 50c995fa93290c1455969ba33ef48366021584e9 [file] [log] [blame]
/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
/*
* Copyright (c) 2012 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>
* 卞超轶 Chaoyi Bian <bcy@pku.edu.cn>
* Alexander Afanasyev <alexander.afanasyev@ucla.edu>
*/
#ifndef SYNC_LOGIC_H
#define SYNC_LOGIC_H
#include <boost/shared_ptr.hpp>
#include <boost/function.hpp>
#include "boost/date_time/posix_time/posix_time_types.hpp"
#include "sync-ccnx-wrapper.h"
#include "sync-interest-table.h"
#include "sync-diff-state.h"
#include "sync-full-state.h"
#include "sync-std-name-info.h"
#include "sync-diff-state-container.h"
namespace Sync {
/**
* \ingroup sync
* @brief A wrapper for SyncApp, which handles ccnx related things (process
* interests and data)
*/
class SyncLogic
{
public:
typedef boost::function<void (const std::string &, uint32_t, uint32_t)> LogicCallback;
/**
* @brief Constructor
* @param syncPrefix the name prefix to use for the Sync Interest
* @param fetch the fetch function, which will be called to actually fetch
* @param ccnxHandle ccnx handle
* the app data when new remote names are learned
*/
SyncLogic (const std::string &syncPrefix, LogicCallback fetch, CcnxWrapperPtr ccnxHandle);
~SyncLogic ();
/**
* a wrapper for the same func in SyncApp
*/
void addLocalNames (const std::string &prefix, uint32_t session, uint32_t seq);
/**
* @brief respond to the Sync Interest; a lot of logic needs to go in here
* @param interest the Sync Interest in string format
*/
void respondSyncInterest (const std::string &interest);
/**
* @brief process the fetched sync data
* @param name the data name
* @param dataBuffer the sync data
*/
void processSyncData (const std::string &name, const std::string &dataBuffer);
/**
* @brief remove a participant's subtree from the sync tree
* @param the name prefix for the participant
*/
void remove(const std::string &prefix);
#ifdef _DEBUG
size_t
getListChecksSize ()
{
boost::lock_guard<boost::mutex> lock (m_listChecksMutex);
return m_listChecks.size ();
}
#endif
private:
void delayedChecksLoop ();
void
processSyncInterest (DigestConstPtr digest, const std::string &interestname, bool timedProcessing=false);
void sendSyncInterest ();
void
processPendingSyncInterests(DiffStatePtr &diff);
private:
typedef std::list< boost::tuple< boost::system_time, boost::function< void ( ) > > > DelayedChecksList;
FullState m_state;
DiffStateContainer m_log;
SyncInterestTable m_syncInterestTable;
std::string m_syncPrefix;
LogicCallback m_fetchCallback;
CcnxWrapperPtr m_ccnxHandle;
boost::thread m_delayedCheckThread;
bool m_delayedCheckThreadRunning;
DelayedChecksList m_listChecks;
boost::condition_variable m_listChecksCondition;
boost::mutex m_listChecksMutex;
static const boost::posix_time::time_duration m_delayedCheckTime;
static const int m_syncResponseFreshness = 2;
};
} // Sync
#endif // SYNC_APP_WRAPPER_H