blob: b624ecb1402695c9eeb07faf4a07528bf175d7f2 [file] [log] [blame]
Zhenkai Zhu8d935c82012-03-06 10:44:12 -08001/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
2/*
3 * Copyright (c) 2012 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 * Author: Zhenkai Zhu <zhenkai@cs.ucla.edu>
19 * 卞超轶 Chaoyi Bian <bcy@pku.edu.cn>
Alexander Afanasyevc1030192012-03-08 22:21:28 -080020 * Alexander Afanasyev <alexander.afanasyev@ucla.edu>
Zhenkai Zhu8d935c82012-03-06 10:44:12 -080021 */
22
Chaoyi Bian11f294f2012-03-08 14:28:06 -080023#ifndef SYNC_LOGIC_H
24#define SYNC_LOGIC_H
Zhenkai Zhu8d935c82012-03-06 10:44:12 -080025#include <boost/shared_ptr.hpp>
26#include <boost/function.hpp>
Alexander Afanasyev387ac952012-03-11 23:49:27 -070027#include "boost/date_time/posix_time/posix_time_types.hpp"
28
Zhenkai Zhu8d935c82012-03-06 10:44:12 -080029#include "sync-ccnx-wrapper.h"
30#include "sync-interest-table.h"
Zhenkai Zhu8d935c82012-03-06 10:44:12 -080031#include "sync-diff-state.h"
32#include "sync-full-state.h"
Chaoyi Bian4194b742012-03-08 17:21:35 -080033#include "sync-std-name-info.h"
Zhenkai Zhu8d935c82012-03-06 10:44:12 -080034
Alexander Afanasyevc1030192012-03-08 22:21:28 -080035#include "sync-diff-state-container.h"
36
Zhenkai Zhu8d935c82012-03-06 10:44:12 -080037namespace Sync {
38
39/**
40 * \ingroup sync
Zhenkai Zhuaae81522012-03-06 11:05:44 -080041 * @brief A wrapper for SyncApp, which handles ccnx related things (process
42 * interests and data)
Zhenkai Zhu8d935c82012-03-06 10:44:12 -080043 */
Chaoyi Bian11f294f2012-03-08 14:28:06 -080044class SyncLogic
Zhenkai Zhu8d935c82012-03-06 10:44:12 -080045{
46public:
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -080047 typedef boost::function<void (const std::string &, uint32_t, uint32_t)> LogicCallback;
48
Alexander Afanasyevc1030192012-03-08 22:21:28 -080049 /**
Alexander Afanasyev172d2b72012-03-08 23:43:39 -080050 * @brief Constructor
Alexander Afanasyevc1030192012-03-08 22:21:28 -080051 * @param syncPrefix the name prefix to use for the Sync Interest
52 * @param fetch the fetch function, which will be called to actually fetch
Alexander Afanasyev172d2b72012-03-08 23:43:39 -080053 * @param ccnxHandle ccnx handle
Alexander Afanasyevc1030192012-03-08 22:21:28 -080054 * the app data when new remote names are learned
55 */
Alexander Afanasyev172d2b72012-03-08 23:43:39 -080056 SyncLogic (const std::string &syncPrefix, LogicCallback fetch, CcnxWrapperPtr ccnxHandle);
Zhenkai Zhu46b26a12012-03-06 13:51:16 -080057
Alexander Afanasyevc1030192012-03-08 22:21:28 -080058 ~SyncLogic ();
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -080059
Alexander Afanasyevc1030192012-03-08 22:21:28 -080060 /**
61 * a wrapper for the same func in SyncApp
62 */
63 void addLocalNames (const std::string &prefix, uint32_t session, uint32_t seq);
Zhenkai Zhu46b26a12012-03-06 13:51:16 -080064
Alexander Afanasyevc1030192012-03-08 22:21:28 -080065 /**
66 * @brief respond to the Sync Interest; a lot of logic needs to go in here
67 * @param interest the Sync Interest in string format
68 */
69 void respondSyncInterest (const std::string &interest);
Zhenkai Zhu46b26a12012-03-06 13:51:16 -080070
Alexander Afanasyevc1030192012-03-08 22:21:28 -080071 /**
72 * @brief process the fetched sync data
Chaoyi Bian633255f2012-03-09 21:25:38 -080073 * @param name the data name
Alexander Afanasyevc1030192012-03-08 22:21:28 -080074 * @param dataBuffer the sync data
75 */
76 void processSyncData (const std::string &name, const std::string &dataBuffer);
Zhenkai Zhu8d935c82012-03-06 10:44:12 -080077
Alexander Afanasyev387ac952012-03-11 23:49:27 -070078#ifdef _DEBUG
79 size_t
80 getListChecksSize ()
81 {
82 boost::lock_guard<boost::mutex> lock (m_listChecksMutex);
83 return m_listChecks.size ();
84 }
85#endif
86
Zhenkai Zhu8d935c82012-03-06 10:44:12 -080087private:
Alexander Afanasyev387ac952012-03-11 23:49:27 -070088 void delayedChecksLoop ();
89
90 void
91 processSyncInterest (DigestConstPtr digest, const std::string &interestname, bool timedProcessing=false);
92
Alexander Afanasyevc1030192012-03-08 22:21:28 -080093 void sendSyncInterest ();
Alexander Afanasyev387ac952012-03-11 23:49:27 -070094 // void checkAgain (const std::string &interest, DigestPtr digest);
Zhenkai Zhu8d935c82012-03-06 10:44:12 -080095
96private:
Alexander Afanasyev387ac952012-03-11 23:49:27 -070097 typedef std::list< boost::tuple< boost::system_time, boost::function< void ( ) > > > DelayedChecksList;
98
Alexander Afanasyevc1030192012-03-08 22:21:28 -080099 FullState m_state;
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800100 DiffStateContainer m_log;
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800101 SyncInterestTable m_syncInterestTable;
102
103 std::string m_syncPrefix;
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800104 LogicCallback m_fetch;
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800105 CcnxWrapperPtr m_ccnxHandle;
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800106
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700107 boost::thread m_delayedCheckThread;
108 bool m_delayedCheckThreadRunning;
109 DelayedChecksList m_listChecks;
110 boost::condition_variable m_listChecksCondition;
111 boost::mutex m_listChecksMutex;
Chaoyi Bian633255f2012-03-09 21:25:38 -0800112
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700113 static const boost::posix_time::time_duration m_delayedCheckTime;
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800114 static const int m_syncResponseFreshness = 2;
Zhenkai Zhu8d935c82012-03-06 10:44:12 -0800115};
116
117
118} // Sync
119
Zhenkai Zhu1ac6f802012-03-06 17:40:27 -0800120#endif // SYNC_APP_WRAPPER_H