blob: d2d9f0f5a64842b02f467d5b74361b5b3bcf0e1f [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"
Alexander Afanasyev750d1872012-03-12 15:33:56 -070028#include <boost/thread/recursive_mutex.hpp>
Alexander Afanasyev387ac952012-03-11 23:49:27 -070029
Zhenkai Zhu8d935c82012-03-06 10:44:12 -080030#include "sync-ccnx-wrapper.h"
31#include "sync-interest-table.h"
Zhenkai Zhu8d935c82012-03-06 10:44:12 -080032#include "sync-diff-state.h"
33#include "sync-full-state.h"
Chaoyi Bian4194b742012-03-08 17:21:35 -080034#include "sync-std-name-info.h"
Zhenkai Zhu8d935c82012-03-06 10:44:12 -080035
Alexander Afanasyevc1030192012-03-08 22:21:28 -080036#include "sync-diff-state-container.h"
37
Zhenkai Zhu8d935c82012-03-06 10:44:12 -080038namespace Sync {
39
40/**
41 * \ingroup sync
Zhenkai Zhuaae81522012-03-06 11:05:44 -080042 * @brief A wrapper for SyncApp, which handles ccnx related things (process
43 * interests and data)
Zhenkai Zhu8d935c82012-03-06 10:44:12 -080044 */
Chaoyi Bian11f294f2012-03-08 14:28:06 -080045class SyncLogic
Zhenkai Zhu8d935c82012-03-06 10:44:12 -080046{
47public:
Alexander Afanasyev750d1872012-03-12 15:33:56 -070048 typedef boost::function< void ( const std::string &/*prefix*/, const SeqNo &/*newSeq*/, const SeqNo &/*oldSeq*/ ) > LogicUpdateCallback;
49 typedef boost::function< void ( const std::string &/*prefix*/ ) > LogicRemoveCallback;
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -080050
Alexander Afanasyevc1030192012-03-08 22:21:28 -080051 /**
Alexander Afanasyev172d2b72012-03-08 23:43:39 -080052 * @brief Constructor
Alexander Afanasyevc1030192012-03-08 22:21:28 -080053 * @param syncPrefix the name prefix to use for the Sync Interest
54 * @param fetch the fetch function, which will be called to actually fetch
Alexander Afanasyev172d2b72012-03-08 23:43:39 -080055 * @param ccnxHandle ccnx handle
Alexander Afanasyevc1030192012-03-08 22:21:28 -080056 * the app data when new remote names are learned
57 */
Alexander Afanasyev750d1872012-03-12 15:33:56 -070058 SyncLogic (const std::string &syncPrefix,
59 LogicUpdateCallback onUpdate,
60 LogicRemoveCallback onRemove,
61 CcnxWrapperPtr ccnxHandle);
Zhenkai Zhu46b26a12012-03-06 13:51:16 -080062
Alexander Afanasyevc1030192012-03-08 22:21:28 -080063 ~SyncLogic ();
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -080064
Alexander Afanasyevc1030192012-03-08 22:21:28 -080065 /**
66 * a wrapper for the same func in SyncApp
67 */
68 void addLocalNames (const std::string &prefix, uint32_t session, uint32_t seq);
Zhenkai Zhu46b26a12012-03-06 13:51:16 -080069
Alexander Afanasyevc1030192012-03-08 22:21:28 -080070 /**
71 * @brief respond to the Sync Interest; a lot of logic needs to go in here
72 * @param interest the Sync Interest in string format
73 */
74 void respondSyncInterest (const std::string &interest);
Zhenkai Zhu46b26a12012-03-06 13:51:16 -080075
Alexander Afanasyevc1030192012-03-08 22:21:28 -080076 /**
77 * @brief process the fetched sync data
Chaoyi Bian633255f2012-03-09 21:25:38 -080078 * @param name the data name
Alexander Afanasyevc1030192012-03-08 22:21:28 -080079 * @param dataBuffer the sync data
80 */
81 void processSyncData (const std::string &name, const std::string &dataBuffer);
Zhenkai Zhu8d935c82012-03-06 10:44:12 -080082
Alexander Afanasyev387ac952012-03-11 23:49:27 -070083#ifdef _DEBUG
84 size_t
85 getListChecksSize ()
86 {
87 boost::lock_guard<boost::mutex> lock (m_listChecksMutex);
88 return m_listChecks.size ();
89 }
90#endif
91
Zhenkai Zhu8d935c82012-03-06 10:44:12 -080092private:
Alexander Afanasyev387ac952012-03-11 23:49:27 -070093 void delayedChecksLoop ();
94
95 void
96 processSyncInterest (DigestConstPtr digest, const std::string &interestname, bool timedProcessing=false);
97
Alexander Afanasyevc1030192012-03-08 22:21:28 -080098 void sendSyncInterest ();
Alexander Afanasyev387ac952012-03-11 23:49:27 -070099 // void checkAgain (const std::string &interest, DigestPtr digest);
Zhenkai Zhu8d935c82012-03-06 10:44:12 -0800100
101private:
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700102 typedef std::list< boost::tuple< boost::system_time, boost::function< void ( ) > > > DelayedChecksList;
103
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800104 FullState m_state;
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800105 DiffStateContainer m_log;
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700106 boost::recursive_mutex m_stateMutex;
107
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800108 SyncInterestTable m_syncInterestTable;
109
110 std::string m_syncPrefix;
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700111 LogicUpdateCallback m_onUpdate;
112 LogicRemoveCallback m_onRemove;
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