blob: 5c3abe43525fbf3565f622f174b6f013466de6e3 [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>
Chaoyi Bian3e1eb162012-04-03 16:59:32 -070019 * Chaoyi Bian <bcy@pku.edu.cn>
Alexander Afanasyevc1030192012-03-08 22:21:28 -080020 * Alexander Afanasyev <alexander.afanasyev@ucla.edu>
Yingdi Yu6d638f02014-01-24 11:01:21 -080021 * Yingdi Yu <yingdi@cs.ucla.edu>
Zhenkai Zhu8d935c82012-03-06 10:44:12 -080022 */
23
Chaoyi Bian11f294f2012-03-08 14:28:06 -080024#ifndef SYNC_LOGIC_H
25#define SYNC_LOGIC_H
Alexander Afanasyev45fba082012-03-12 18:05:24 -070026
Alexander Afanasyev45fba082012-03-12 18:05:24 -070027#include <boost/random.hpp>
Zhenkai Zhuce66e212012-03-12 22:27:19 -070028#include <memory>
Zhenkai Zhud5aec4b2012-10-09 12:01:19 -070029#include <map>
Alexander Afanasyev387ac952012-03-11 23:49:27 -070030
Yingdi Yue8154712014-01-21 10:20:14 -080031#include <ndn-cpp-dev/face.hpp>
Yingdi Yu280bb962014-01-30 09:52:43 -080032#include <ndn-cpp-dev/security/validator.hpp>
Yingdi Yue8154712014-01-21 10:20:14 -080033#include <ndn-cpp-dev/security/key-chain.hpp>
Yingdi Yu280bb962014-01-30 09:52:43 -080034#include <ndn-cpp-dev/util/scheduler.hpp>
Yingdi Yu46c9f1a2013-12-18 15:15:46 +080035
Zhenkai Zhu8d935c82012-03-06 10:44:12 -080036#include "sync-interest-table.h"
Zhenkai Zhu8d935c82012-03-06 10:44:12 -080037#include "sync-diff-state.h"
38#include "sync-full-state.h"
Chaoyi Bian4194b742012-03-08 17:21:35 -080039#include "sync-std-name-info.h"
Zhenkai Zhu8d935c82012-03-06 10:44:12 -080040
Alexander Afanasyevc1030192012-03-08 22:21:28 -080041#include "sync-diff-state-container.h"
42
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -070043#ifdef _DEBUG
44#ifdef HAVE_LOG4CXX
45#include <log4cxx/logger.h>
46#endif
47#endif
48
Zhenkai Zhu8d935c82012-03-06 10:44:12 -080049namespace Sync {
50
Zhenkai Zhu1cb29292012-05-31 22:54:34 -070051struct MissingDataInfo {
52 std::string prefix;
53 SeqNo low;
54 SeqNo high;
55};
56
Zhenkai Zhu8d935c82012-03-06 10:44:12 -080057/**
58 * \ingroup sync
Zhenkai Zhuaae81522012-03-06 11:05:44 -080059 * @brief A wrapper for SyncApp, which handles ccnx related things (process
60 * interests and data)
Zhenkai Zhu8d935c82012-03-06 10:44:12 -080061 */
Zhenkai Zhub9f19592012-10-16 14:27:38 -070062
Chaoyi Bian11f294f2012-03-08 14:28:06 -080063class SyncLogic
Zhenkai Zhu8d935c82012-03-06 10:44:12 -080064{
65public:
Zhenkai Zhu1cb29292012-05-31 22:54:34 -070066 //typedef boost::function< void ( const std::string &/*prefix*/, const SeqNo &/*newSeq*/, const SeqNo &/*oldSeq*/ ) > LogicUpdateCallback;
67 typedef boost::function< void (const std::vector<MissingDataInfo> & ) > LogicUpdateCallback;
Alexander Afanasyevd95c2312013-11-07 13:45:34 -080068 typedef boost::function< void (const std::string &/*prefix*/ ) > LogicRemoveCallback;
Zhenkai Zhub9f19592012-10-16 14:27:38 -070069 typedef boost::function< void (const std::string &)> LogicPerBranchCallback;
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -080070
Alexander Afanasyevc1030192012-03-08 22:21:28 -080071 /**
Alexander Afanasyev172d2b72012-03-08 23:43:39 -080072 * @brief Constructor
Alexander Afanasyevc1030192012-03-08 22:21:28 -080073 * @param syncPrefix the name prefix to use for the Sync Interest
Alexander Afanasyev03a58b72012-03-12 18:11:56 -070074 * @param onUpdate function that will be called when new state is detected
75 * @param onRemove function that will be called when state is removed
Alexander Afanasyev172d2b72012-03-08 23:43:39 -080076 * @param ccnxHandle ccnx handle
Alexander Afanasyevc1030192012-03-08 22:21:28 -080077 * the app data when new remote names are learned
78 */
Yingdi Yu43e71612013-10-30 22:19:31 -070079 SyncLogic (const ndn::Name& syncPrefix,
Yingdi Yu280bb962014-01-30 09:52:43 -080080 ndn::shared_ptr<ndn::Validator> validator,
81 ndn::shared_ptr<ndn::Face> face,
Alexander Afanasyev750d1872012-03-12 15:33:56 -070082 LogicUpdateCallback onUpdate,
Zhenkai Zhuce66e212012-03-12 22:27:19 -070083 LogicRemoveCallback onRemove);
Zhenkai Zhu46b26a12012-03-06 13:51:16 -080084
Yingdi Yu43e71612013-10-30 22:19:31 -070085 SyncLogic (const ndn::Name& syncPrefix,
Yingdi Yu280bb962014-01-30 09:52:43 -080086 ndn::shared_ptr<ndn::Validator> validator,
87 ndn::shared_ptr<ndn::Face> face,
Zhenkai Zhub9f19592012-10-16 14:27:38 -070088 LogicPerBranchCallback onUpdateBranch);
89
Alexander Afanasyevc1030192012-03-08 22:21:28 -080090 ~SyncLogic ();
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -080091
Alexander Afanasyevc1030192012-03-08 22:21:28 -080092 /**
93 * a wrapper for the same func in SyncApp
94 */
Yingdi Yu280bb962014-01-30 09:52:43 -080095 void addLocalNames (const ndn::Name &prefix, uint64_t session, uint64_t seq);
Zhenkai Zhu46b26a12012-03-06 13:51:16 -080096
Alexander Afanasyevc1030192012-03-08 22:21:28 -080097 /**
98 * @brief respond to the Sync Interest; a lot of logic needs to go in here
99 * @param interest the Sync Interest in string format
100 */
Yingdi Yu280bb962014-01-30 09:52:43 -0800101 void respondSyncInterest (ndn::shared_ptr<ndn::Interest> interest);
Zhenkai Zhu46b26a12012-03-06 13:51:16 -0800102
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800103 /**
104 * @brief process the fetched sync data
Chaoyi Bian633255f2012-03-09 21:25:38 -0800105 * @param name the data name
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800106 * @param dataBuffer the sync data
107 */
Yingdi Yu280bb962014-01-30 09:52:43 -0800108 void respondSyncData (ndn::shared_ptr<ndn::Data> data);
Zhenkai Zhu8d935c82012-03-06 10:44:12 -0800109
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700110 /**
111 * @brief remove a participant's subtree from the sync tree
Alexander Afanasyev03a58b72012-03-12 18:11:56 -0700112 * @param prefix the name prefix for the participant
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700113 */
Yingdi Yu6d638f02014-01-24 11:01:21 -0800114 void remove (const ndn::Name &prefix);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700115
Zhenkai Zhue5660932012-06-04 15:25:20 -0700116 std::string
117 getRootDigest();
118
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700119#ifdef _DEBUG
Yingdi Yu280bb962014-01-30 09:52:43 -0800120 ndn::Scheduler &
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -0700121 getScheduler () { return m_scheduler; }
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700122#endif
123
Zhenkai Zhueb1d8652012-12-23 15:25:39 -0800124 void stop();
125
Alexander Afanasyev5548c042012-10-04 19:10:09 -0700126 void
127 printState () const;
128
Zhenkai Zhud5aec4b2012-10-09 12:01:19 -0700129 std::map<std::string, bool>
130 getBranchPrefixes() const;
131
Yingdi Yu280bb962014-01-30 09:52:43 -0800132private:
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800133 void
Alexander Afanasyev45fba082012-03-12 18:05:24 -0700134 delayedChecksLoop ();
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700135
136 void
Yingdi Yu280bb962014-01-30 09:52:43 -0800137 onSyncInterest (const ndn::shared_ptr<const ndn::Name>& prefix,
138 const ndn::shared_ptr<const ndn::Interest>& interest);
Yingdi Yu43e71612013-10-30 22:19:31 -0700139
140 void
Yingdi Yu280bb962014-01-30 09:52:43 -0800141 onSyncRegisterFailed(const ndn::shared_ptr<const ndn::Name>& prefix);
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800142
143 void
Yingdi Yu280bb962014-01-30 09:52:43 -0800144 onSyncData(const ndn::shared_ptr<const ndn::Interest>& interest,
145 const ndn::shared_ptr<ndn::Data>& data,
146 const ndn::OnDataValidated& onValidated,
147 const ndn::OnDataValidationFailed& onValidationFailed);
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800148
149 void
Yingdi Yu280bb962014-01-30 09:52:43 -0800150 onSyncTimeout(const ndn::shared_ptr<const ndn::Interest>& interest);
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800151
152 void
Yingdi Yu280bb962014-01-30 09:52:43 -0800153 onSyncDataValidationFailed(const ndn::shared_ptr<const ndn::Data>& data);
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800154
155 void
Yingdi Yu280bb962014-01-30 09:52:43 -0800156 onSyncDataValidated(const ndn::shared_ptr<const ndn::Data>& data);
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800157
158 void
Yingdi Yu6d638f02014-01-24 11:01:21 -0800159 processSyncInterest (const ndn::Name &name,
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700160 DigestConstPtr digest, bool timedProcessing=false);
161
162 void
Yingdi Yu6d638f02014-01-24 11:01:21 -0800163 processSyncData (const ndn::Name &name,
Zhenkai Zhu3cfdcb92012-06-06 15:20:10 -0700164 DigestConstPtr digest, const char *wireData, size_t len);
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700165
Alexander Afanasyev45fba082012-03-12 18:05:24 -0700166 void
Yingdi Yu6d638f02014-01-24 11:01:21 -0800167 processSyncRecoveryInterest (const ndn::Name &name,
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700168 DigestConstPtr digest);
169
Alexander Afanasyev45fba082012-03-12 18:05:24 -0700170 void
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700171 insertToDiffLog (DiffStatePtr diff);
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700172
173 void
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700174 satisfyPendingSyncInterests (DiffStateConstPtr diff);
Zhenkai Zhu8d935c82012-03-06 10:44:12 -0800175
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700176 boost::tuple<DigestConstPtr, std::string>
Yingdi Yu6d638f02014-01-24 11:01:21 -0800177 convertNameToDigestAndType (const ndn::Name &name);
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700178
179 void
180 sendSyncInterest ();
181
182 void
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700183 sendSyncRecoveryInterests (DigestConstPtr digest);
184
185 void
Yingdi Yu6d638f02014-01-24 11:01:21 -0800186 sendSyncData (const ndn::Name &name,
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700187 DigestConstPtr digest, StateConstPtr state);
Alexander Afanasyev80237382012-10-04 10:20:47 -0700188
Zhenkai Zhu2d3e2702012-10-15 14:18:05 -0700189 void
Yingdi Yu6d638f02014-01-24 11:01:21 -0800190 sendSyncData (const ndn::Name &name,
Zhenkai Zhu2d3e2702012-10-15 14:18:05 -0700191 DigestConstPtr digest, SyncStateMsg &msg);
192
Alexander Afanasyev80237382012-10-04 10:20:47 -0700193 size_t
194 getNumberOfBranches () const;
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700195
Zhenkai Zhu8d935c82012-03-06 10:44:12 -0800196private:
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700197 FullStatePtr m_state;
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800198 DiffStateContainer m_log;
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700199
Yingdi Yu6d638f02014-01-24 11:01:21 -0800200 ndn::Name m_outstandingInterestName;
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800201 SyncInterestTable m_syncInterestTable;
202
Yingdi Yu43e71612013-10-30 22:19:31 -0700203 ndn::Name m_syncPrefix;
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700204 LogicUpdateCallback m_onUpdate;
205 LogicRemoveCallback m_onRemove;
Zhenkai Zhub9f19592012-10-16 14:27:38 -0700206 LogicPerBranchCallback m_onUpdateBranch;
207 bool m_perBranch;
Yingdi Yu280bb962014-01-30 09:52:43 -0800208 ndn::ptr_lib::shared_ptr<ndn::Validator> m_validator;
Yingdi Yu0cb0f2b2014-01-09 13:51:16 -0800209 ndn::ptr_lib::shared_ptr<ndn::KeyChain> m_keyChain;
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800210 ndn::ptr_lib::shared_ptr<ndn::Face> m_face;
Yingdi Yu280bb962014-01-30 09:52:43 -0800211 const ndn::RegisteredPrefixId* m_syncRegisteredPrefixId;
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800212
Yingdi Yu280bb962014-01-30 09:52:43 -0800213 ndn::Scheduler m_scheduler;
Chaoyi Bian633255f2012-03-09 21:25:38 -0800214
Alexander Afanasyev45fba082012-03-12 18:05:24 -0700215 boost::mt19937 m_randomGenerator;
216 boost::variate_generator<boost::mt19937&, boost::uniform_int<> > m_rangeUniformRandom;
Alexander Afanasyev085742a2012-04-26 12:24:57 -0700217 boost::variate_generator<boost::mt19937&, boost::uniform_int<> > m_reexpressionJitter;
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -0700218
Alexander Afanasyev3a229132012-04-25 15:07:26 -0700219 static const int m_unknownDigestStoreTime = 10; // seconds
Alexander Afanasyev531803b2014-02-05 15:57:35 -0800220 static const int m_syncResponseFreshness = 1000; // MUST BE dividable by 1000!!!
221 static const int m_syncInterestReexpress = 4; // seconds
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -0700222
Alexander Afanasyeva76010b2012-05-24 21:31:49 -0700223 static const int m_defaultRecoveryRetransmitInterval = 200; // milliseconds
224 uint32_t m_recoveryRetransmissionInterval; // milliseconds
225
Yingdi Yu280bb962014-01-30 09:52:43 -0800226 ndn::EventId m_delayedInterestProcessingId;
227 ndn::EventId m_reexpressingInterestId;
228 ndn::EventId m_reexpressingRecoveryInterestId;
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800229
230 std::string m_instanceId;
231 static int m_instanceCounter;
Zhenkai Zhu8d935c82012-03-06 10:44:12 -0800232};
233
234
235} // Sync
236
Zhenkai Zhu1ac6f802012-03-06 17:40:27 -0800237#endif // SYNC_APP_WRAPPER_H