blob: 4270181e5db2e15d5876cdabbe0788130abac0d5 [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
Alexander Afanasyev45fba082012-03-12 18:05:24 -070025
Zhenkai Zhu8d935c82012-03-06 10:44:12 -080026#include <boost/shared_ptr.hpp>
Alexander Afanasyev750d1872012-03-12 15:33:56 -070027#include <boost/thread/recursive_mutex.hpp>
Alexander Afanasyev45fba082012-03-12 18:05:24 -070028#include <boost/random.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"
Alexander Afanasyev45fba082012-03-12 18:05:24 -070035#include "sync-scheduler.h"
Zhenkai Zhu8d935c82012-03-06 10:44:12 -080036
Alexander Afanasyevc1030192012-03-08 22:21:28 -080037#include "sync-diff-state-container.h"
38
Zhenkai Zhu8d935c82012-03-06 10:44:12 -080039namespace Sync {
40
41/**
42 * \ingroup sync
Zhenkai Zhuaae81522012-03-06 11:05:44 -080043 * @brief A wrapper for SyncApp, which handles ccnx related things (process
44 * interests and data)
Zhenkai Zhu8d935c82012-03-06 10:44:12 -080045 */
Chaoyi Bian11f294f2012-03-08 14:28:06 -080046class SyncLogic
Zhenkai Zhu8d935c82012-03-06 10:44:12 -080047{
48public:
Alexander Afanasyev750d1872012-03-12 15:33:56 -070049 typedef boost::function< void ( const std::string &/*prefix*/, const SeqNo &/*newSeq*/, const SeqNo &/*oldSeq*/ ) > LogicUpdateCallback;
50 typedef boost::function< void ( const std::string &/*prefix*/ ) > LogicRemoveCallback;
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -080051
Alexander Afanasyevc1030192012-03-08 22:21:28 -080052 /**
Alexander Afanasyev172d2b72012-03-08 23:43:39 -080053 * @brief Constructor
Alexander Afanasyevc1030192012-03-08 22:21:28 -080054 * @param syncPrefix the name prefix to use for the Sync Interest
Alexander Afanasyev03a58b72012-03-12 18:11:56 -070055 * @param onUpdate function that will be called when new state is detected
56 * @param onRemove function that will be called when state is removed
Alexander Afanasyev172d2b72012-03-08 23:43:39 -080057 * @param ccnxHandle ccnx handle
Alexander Afanasyevc1030192012-03-08 22:21:28 -080058 * the app data when new remote names are learned
59 */
Alexander Afanasyev750d1872012-03-12 15:33:56 -070060 SyncLogic (const std::string &syncPrefix,
61 LogicUpdateCallback onUpdate,
62 LogicRemoveCallback onRemove,
63 CcnxWrapperPtr ccnxHandle);
Zhenkai Zhu46b26a12012-03-06 13:51:16 -080064
Alexander Afanasyevc1030192012-03-08 22:21:28 -080065 ~SyncLogic ();
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -080066
Alexander Afanasyevc1030192012-03-08 22:21:28 -080067 /**
68 * a wrapper for the same func in SyncApp
69 */
70 void addLocalNames (const std::string &prefix, uint32_t session, uint32_t seq);
Zhenkai Zhu46b26a12012-03-06 13:51:16 -080071
Alexander Afanasyevc1030192012-03-08 22:21:28 -080072 /**
73 * @brief respond to the Sync Interest; a lot of logic needs to go in here
74 * @param interest the Sync Interest in string format
75 */
76 void respondSyncInterest (const std::string &interest);
Zhenkai Zhu46b26a12012-03-06 13:51:16 -080077
Alexander Afanasyevc1030192012-03-08 22:21:28 -080078 /**
79 * @brief process the fetched sync data
Chaoyi Bian633255f2012-03-09 21:25:38 -080080 * @param name the data name
Alexander Afanasyevc1030192012-03-08 22:21:28 -080081 * @param dataBuffer the sync data
82 */
83 void processSyncData (const std::string &name, const std::string &dataBuffer);
Zhenkai Zhu8d935c82012-03-06 10:44:12 -080084
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -070085 /**
86 * @brief remove a participant's subtree from the sync tree
Alexander Afanasyev03a58b72012-03-12 18:11:56 -070087 * @param prefix the name prefix for the participant
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -070088 */
Alexander Afanasyev03a58b72012-03-12 18:11:56 -070089 void remove (const std::string &prefix);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -070090
Alexander Afanasyev387ac952012-03-11 23:49:27 -070091#ifdef _DEBUG
Alexander Afanasyev45fba082012-03-12 18:05:24 -070092 Scheduler &
93 getScheduler () { return m_delayedChecksScheduler; }
Alexander Afanasyev387ac952012-03-11 23:49:27 -070094#endif
95
Zhenkai Zhu8d935c82012-03-06 10:44:12 -080096private:
Alexander Afanasyev45fba082012-03-12 18:05:24 -070097 void
98 delayedChecksLoop ();
Alexander Afanasyev387ac952012-03-11 23:49:27 -070099
100 void
101 processSyncInterest (DigestConstPtr digest, const std::string &interestname, bool timedProcessing=false);
102
Alexander Afanasyev45fba082012-03-12 18:05:24 -0700103 void
104 sendSyncInterest ();
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700105
Alexander Afanasyev45fba082012-03-12 18:05:24 -0700106 void
107 processPendingSyncInterests(DiffStatePtr &diff);
Zhenkai Zhu8d935c82012-03-06 10:44:12 -0800108
109private:
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800110 FullState m_state;
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800111 DiffStateContainer m_log;
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700112 boost::recursive_mutex m_stateMutex;
113
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800114 SyncInterestTable m_syncInterestTable;
115
116 std::string m_syncPrefix;
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700117 LogicUpdateCallback m_onUpdate;
118 LogicRemoveCallback m_onRemove;
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800119 CcnxWrapperPtr m_ccnxHandle;
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800120
Alexander Afanasyev45fba082012-03-12 18:05:24 -0700121 Scheduler m_delayedChecksScheduler;
Chaoyi Bian633255f2012-03-09 21:25:38 -0800122
Alexander Afanasyev45fba082012-03-12 18:05:24 -0700123 boost::mt19937 m_randomGenerator;
124 boost::variate_generator<boost::mt19937&, boost::uniform_int<> > m_rangeUniformRandom;
125
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800126 static const int m_syncResponseFreshness = 2;
Zhenkai Zhu8d935c82012-03-06 10:44:12 -0800127};
128
129
130} // Sync
131
Zhenkai Zhu1ac6f802012-03-06 17:40:27 -0800132#endif // SYNC_APP_WRAPPER_H