blob: 1cc42b6484aa11324617bf7ee54df4fd73fa99c0 [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
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -070078 /**
79 * @brief remove a participant's subtree from the sync tree
80 * @param the name prefix for the participant
81 */
82 void remove(const std::string &prefix);
83
Alexander Afanasyev387ac952012-03-11 23:49:27 -070084#ifdef _DEBUG
85 size_t
86 getListChecksSize ()
87 {
88 boost::lock_guard<boost::mutex> lock (m_listChecksMutex);
89 return m_listChecks.size ();
90 }
91#endif
92
Zhenkai Zhu8d935c82012-03-06 10:44:12 -080093private:
Alexander Afanasyev387ac952012-03-11 23:49:27 -070094 void delayedChecksLoop ();
95
96 void
97 processSyncInterest (DigestConstPtr digest, const std::string &interestname, bool timedProcessing=false);
98
Alexander Afanasyevc1030192012-03-08 22:21:28 -080099 void sendSyncInterest ();
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700100
101 void
102 processPendingSyncInterests();
Zhenkai Zhu8d935c82012-03-06 10:44:12 -0800103
104private:
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700105 typedef std::list< boost::tuple< boost::system_time, boost::function< void ( ) > > > DelayedChecksList;
106
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800107 FullState m_state;
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800108 DiffStateContainer m_log;
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800109 SyncInterestTable m_syncInterestTable;
110
111 std::string m_syncPrefix;
Alexander Afanasyeve4e2bf72012-03-12 12:44:54 -0700112 LogicCallback m_fetchCallback;
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800113 CcnxWrapperPtr m_ccnxHandle;
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800114
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700115 boost::thread m_delayedCheckThread;
116 bool m_delayedCheckThreadRunning;
117 DelayedChecksList m_listChecks;
118 boost::condition_variable m_listChecksCondition;
119 boost::mutex m_listChecksMutex;
Chaoyi Bian633255f2012-03-09 21:25:38 -0800120
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700121 static const boost::posix_time::time_duration m_delayedCheckTime;
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800122 static const int m_syncResponseFreshness = 2;
Zhenkai Zhu8d935c82012-03-06 10:44:12 -0800123};
124
125
126} // Sync
127
Zhenkai Zhu1ac6f802012-03-06 17:40:27 -0800128#endif // SYNC_APP_WRAPPER_H