blob: b9b30572d200207a70b1dce2b0c7749c832e49c3 [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>
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>
Zhenkai Zhuce66e212012-03-12 22:27:19 -070029#include <memory>
Zhenkai Zhud5aec4b2012-10-09 12:01:19 -070030#include <map>
Alexander Afanasyev387ac952012-03-11 23:49:27 -070031
Yingdi Yu46c9f1a2013-12-18 15:15:46 +080032#include <ndn-cpp/face.hpp>
33#include <ndn-cpp/security/identity/identity-manager.hpp>
34
Zhenkai Zhu8d935c82012-03-06 10:44:12 -080035#include "sync-interest-table.h"
Zhenkai Zhu8d935c82012-03-06 10:44:12 -080036#include "sync-diff-state.h"
37#include "sync-full-state.h"
Chaoyi Bian4194b742012-03-08 17:21:35 -080038#include "sync-std-name-info.h"
Alexander Afanasyev45fba082012-03-12 18:05:24 -070039#include "sync-scheduler.h"
Yingdi Yu43e71612013-10-30 22:19:31 -070040#include "sync-policy-manager.h"
Zhenkai Zhu8d935c82012-03-06 10:44:12 -080041
Alexander Afanasyevc1030192012-03-08 22:21:28 -080042#include "sync-diff-state-container.h"
43
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -070044#ifdef _DEBUG
45#ifdef HAVE_LOG4CXX
46#include <log4cxx/logger.h>
47#endif
48#endif
49
Alexander Afanasyev181d7e52012-04-09 13:54:11 -070050#ifdef NS3_MODULE
51#include <ns3/application.h>
Alexander Afanasyev73f7f9a2012-04-09 15:45:47 -070052#include <ns3/random-variable.h>
Alexander Afanasyev181d7e52012-04-09 13:54:11 -070053#endif
54
Zhenkai Zhu8d935c82012-03-06 10:44:12 -080055namespace Sync {
56
Zhenkai Zhu1cb29292012-05-31 22:54:34 -070057struct MissingDataInfo {
58 std::string prefix;
59 SeqNo low;
60 SeqNo high;
61};
62
Zhenkai Zhu8d935c82012-03-06 10:44:12 -080063/**
64 * \ingroup sync
Zhenkai Zhuaae81522012-03-06 11:05:44 -080065 * @brief A wrapper for SyncApp, which handles ccnx related things (process
66 * interests and data)
Zhenkai Zhu8d935c82012-03-06 10:44:12 -080067 */
Zhenkai Zhub9f19592012-10-16 14:27:38 -070068
Chaoyi Bian11f294f2012-03-08 14:28:06 -080069class SyncLogic
Alexander Afanasyev181d7e52012-04-09 13:54:11 -070070#ifdef NS3_MODULE
71 : public ns3::Application
72#endif
Zhenkai Zhu8d935c82012-03-06 10:44:12 -080073{
74public:
Zhenkai Zhu1cb29292012-05-31 22:54:34 -070075 //typedef boost::function< void ( const std::string &/*prefix*/, const SeqNo &/*newSeq*/, const SeqNo &/*oldSeq*/ ) > LogicUpdateCallback;
76 typedef boost::function< void (const std::vector<MissingDataInfo> & ) > LogicUpdateCallback;
Alexander Afanasyevd95c2312013-11-07 13:45:34 -080077 typedef boost::function< void (const std::string &/*prefix*/ ) > LogicRemoveCallback;
Zhenkai Zhub9f19592012-10-16 14:27:38 -070078 typedef boost::function< void (const std::string &)> LogicPerBranchCallback;
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -080079
Alexander Afanasyevc1030192012-03-08 22:21:28 -080080 /**
Alexander Afanasyev172d2b72012-03-08 23:43:39 -080081 * @brief Constructor
Alexander Afanasyevc1030192012-03-08 22:21:28 -080082 * @param syncPrefix the name prefix to use for the Sync Interest
Alexander Afanasyev03a58b72012-03-12 18:11:56 -070083 * @param onUpdate function that will be called when new state is detected
84 * @param onRemove function that will be called when state is removed
Alexander Afanasyev172d2b72012-03-08 23:43:39 -080085 * @param ccnxHandle ccnx handle
Alexander Afanasyevc1030192012-03-08 22:21:28 -080086 * the app data when new remote names are learned
87 */
Yingdi Yu43e71612013-10-30 22:19:31 -070088 SyncLogic (const ndn::Name& syncPrefix,
Yingdi Yu46c9f1a2013-12-18 15:15:46 +080089 ndn::ptr_lib::shared_ptr<SyncPolicyManager> syncPolicyManager,
Alexander Afanasyev750d1872012-03-12 15:33:56 -070090 LogicUpdateCallback onUpdate,
Zhenkai Zhuce66e212012-03-12 22:27:19 -070091 LogicRemoveCallback onRemove);
Zhenkai Zhu46b26a12012-03-06 13:51:16 -080092
Yingdi Yu43e71612013-10-30 22:19:31 -070093 SyncLogic (const ndn::Name& syncPrefix,
Yingdi Yu46c9f1a2013-12-18 15:15:46 +080094 ndn::ptr_lib::shared_ptr<SyncPolicyManager> syncPolicyManager,
Zhenkai Zhub9f19592012-10-16 14:27:38 -070095 LogicPerBranchCallback onUpdateBranch);
96
Alexander Afanasyevc1030192012-03-08 22:21:28 -080097 ~SyncLogic ();
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -080098
Alexander Afanasyevc1030192012-03-08 22:21:28 -080099 /**
100 * a wrapper for the same func in SyncApp
101 */
102 void addLocalNames (const std::string &prefix, uint32_t session, uint32_t seq);
Zhenkai Zhu46b26a12012-03-06 13:51:16 -0800103
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800104 /**
105 * @brief respond to the Sync Interest; a lot of logic needs to go in here
106 * @param interest the Sync Interest in string format
107 */
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800108 void respondSyncInterest (ndn::ptr_lib::shared_ptr<ndn::Interest> interest);
Zhenkai Zhu46b26a12012-03-06 13:51:16 -0800109
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800110 /**
111 * @brief process the fetched sync data
Chaoyi Bian633255f2012-03-09 21:25:38 -0800112 * @param name the data name
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800113 * @param dataBuffer the sync data
114 */
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800115 void respondSyncData (ndn::ptr_lib::shared_ptr<ndn::Data> data);
Zhenkai Zhu8d935c82012-03-06 10:44:12 -0800116
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700117 /**
118 * @brief remove a participant's subtree from the sync tree
Alexander Afanasyev03a58b72012-03-12 18:11:56 -0700119 * @param prefix the name prefix for the participant
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700120 */
Alexander Afanasyev03a58b72012-03-12 18:11:56 -0700121 void remove (const std::string &prefix);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700122
Zhenkai Zhue5660932012-06-04 15:25:20 -0700123 std::string
124 getRootDigest();
125
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700126#ifdef _DEBUG
Alexander Afanasyev45fba082012-03-12 18:05:24 -0700127 Scheduler &
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -0700128 getScheduler () { return m_scheduler; }
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700129#endif
Alexander Afanasyev181d7e52012-04-09 13:54:11 -0700130
Alexander Afanasyev181d7e52012-04-09 13:54:11 -0700131#ifdef NS3_MODULE
Alexander Afanasyev40942f42012-04-21 20:53:16 -0700132public:
Alexander Afanasyev181d7e52012-04-09 13:54:11 -0700133 virtual void StartApplication ();
134 virtual void StopApplication ();
135#endif
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700136
Zhenkai Zhueb1d8652012-12-23 15:25:39 -0800137 void stop();
138
Alexander Afanasyev5548c042012-10-04 19:10:09 -0700139 void
140 printState () const;
141
Zhenkai Zhud5aec4b2012-10-09 12:01:19 -0700142 std::map<std::string, bool>
143 getBranchPrefixes() const;
144
Zhenkai Zhu8d935c82012-03-06 10:44:12 -0800145private:
Alexander Afanasyev45fba082012-03-12 18:05:24 -0700146 void
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800147 connectToDaemon();
148
149 void
150 onConnectionData(const ndn::ptr_lib::shared_ptr<const ndn::Interest>& interest,
151 const ndn::ptr_lib::shared_ptr<ndn::Data>& data);
152
153 void
154 onConnectionDataTimeout(const ndn::ptr_lib::shared_ptr<const ndn::Interest>& interest);
155
156 void
Alexander Afanasyev45fba082012-03-12 18:05:24 -0700157 delayedChecksLoop ();
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700158
159 void
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800160 onSyncInterest (const ndn::ptr_lib::shared_ptr<const ndn::Name>& prefix,
161 const ndn::ptr_lib::shared_ptr<const ndn::Interest>& interest,
162 ndn::Transport& transport,
163 uint64_t registeredPrefixId);
Yingdi Yu43e71612013-10-30 22:19:31 -0700164
165 void
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800166 onSyncRegisterFailed(const ndn::ptr_lib::shared_ptr<const ndn::Name>& prefix);
167
168 void
169 onSyncData(const ndn::ptr_lib::shared_ptr<const ndn::Interest>& interest,
170 const ndn::ptr_lib::shared_ptr<ndn::Data>& data,
171 int stepCount,
172 const ndn::OnVerified& onVerified,
173 const ndn::OnVerifyFailed& onVerifyFailed);
174
175 void
176 onSyncDataTimeout(const ndn::ptr_lib::shared_ptr<const ndn::Interest>& interest,
177 int retry,
178 int stepCount,
179 const ndn::OnVerified& onVerified,
180 const ndn::OnVerifyFailed& onVerifyFailed);
181
182 void
183 onSyncDataVerifyFailed(const ndn::ptr_lib::shared_ptr<ndn::Data>& data);
184
185 void
186 onSyncDataVerified(const ndn::ptr_lib::shared_ptr<ndn::Data>& data);
187
188 void
189 onSyncCert(const ndn::ptr_lib::shared_ptr<const ndn::Interest>& interest,
190 const ndn::ptr_lib::shared_ptr<ndn::Data>& cert,
191 ndn::ptr_lib::shared_ptr<ndn::ValidationRequest> previousStep);
192
193 void
194 onSyncCertTimeout(const ndn::ptr_lib::shared_ptr<const ndn::Interest>& interest,
195 const ndn::OnVerifyFailed& onVerifyFailed,
196 const ndn::ptr_lib::shared_ptr<ndn::Data>& data,
197 ndn::ptr_lib::shared_ptr<ndn::ValidationRequest> nextStep);
Yingdi Yu43e71612013-10-30 22:19:31 -0700198
199 void
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700200 processSyncInterest (const std::string &name,
201 DigestConstPtr digest, bool timedProcessing=false);
202
203 void
204 processSyncData (const std::string &name,
Zhenkai Zhu3cfdcb92012-06-06 15:20:10 -0700205 DigestConstPtr digest, const char *wireData, size_t len);
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700206
Alexander Afanasyev45fba082012-03-12 18:05:24 -0700207 void
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700208 processSyncRecoveryInterest (const std::string &name,
209 DigestConstPtr digest);
210
Alexander Afanasyev45fba082012-03-12 18:05:24 -0700211 void
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700212 insertToDiffLog (DiffStatePtr diff);
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700213
214 void
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700215 satisfyPendingSyncInterests (DiffStateConstPtr diff);
Zhenkai Zhu8d935c82012-03-06 10:44:12 -0800216
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700217 boost::tuple<DigestConstPtr, std::string>
218 convertNameToDigestAndType (const std::string &name);
219
220 void
221 sendSyncInterest ();
222
223 void
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700224 sendSyncRecoveryInterests (DigestConstPtr digest);
225
226 void
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700227 sendSyncData (const std::string &name,
228 DigestConstPtr digest, StateConstPtr state);
Alexander Afanasyev80237382012-10-04 10:20:47 -0700229
Zhenkai Zhu2d3e2702012-10-15 14:18:05 -0700230 void
231 sendSyncData (const std::string &name,
232 DigestConstPtr digest, SyncStateMsg &msg);
233
Alexander Afanasyev80237382012-10-04 10:20:47 -0700234 size_t
235 getNumberOfBranches () const;
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700236
Zhenkai Zhu8d935c82012-03-06 10:44:12 -0800237private:
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700238 FullStatePtr m_state;
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800239 DiffStateContainer m_log;
Alexander Afanasyev80237382012-10-04 10:20:47 -0700240 mutable boost::recursive_mutex m_stateMutex;
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700241
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700242 std::string m_outstandingInterestName;
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800243 SyncInterestTable m_syncInterestTable;
244
Yingdi Yu43e71612013-10-30 22:19:31 -0700245 ndn::Name m_syncPrefix;
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700246 LogicUpdateCallback m_onUpdate;
247 LogicRemoveCallback m_onRemove;
Zhenkai Zhub9f19592012-10-16 14:27:38 -0700248 LogicPerBranchCallback m_onUpdateBranch;
249 bool m_perBranch;
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800250 ndn::ptr_lib::shared_ptr<SyncPolicyManager> m_policyManager;
251 ndn::ptr_lib::shared_ptr<ndn::IdentityManager> m_identityManager;
252 ndn::ptr_lib::shared_ptr<ndn::Face> m_face;
253 ndn::ptr_lib::shared_ptr<ndn::Transport> m_transport;
254 uint64_t m_syncRegisteredPrefixId;
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800255
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -0700256 Scheduler m_scheduler;
Chaoyi Bian633255f2012-03-09 21:25:38 -0800257
Alexander Afanasyev73f7f9a2012-04-09 15:45:47 -0700258#ifndef NS3_MODULE
Alexander Afanasyev45fba082012-03-12 18:05:24 -0700259 boost::mt19937 m_randomGenerator;
260 boost::variate_generator<boost::mt19937&, boost::uniform_int<> > m_rangeUniformRandom;
Alexander Afanasyev085742a2012-04-26 12:24:57 -0700261 boost::variate_generator<boost::mt19937&, boost::uniform_int<> > m_reexpressionJitter;
Alexander Afanasyev73f7f9a2012-04-09 15:45:47 -0700262#else
263 ns3::UniformVariable m_rangeUniformRandom;
Alexander Afanasyev085742a2012-04-26 12:24:57 -0700264 ns3::UniformVariable m_reexpressionJitter;
Alexander Afanasyev73f7f9a2012-04-09 15:45:47 -0700265#endif
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -0700266
Alexander Afanasyev3a229132012-04-25 15:07:26 -0700267 static const int m_unknownDigestStoreTime = 10; // seconds
Alexander Afanasyev89571f12012-04-23 13:44:57 -0700268#ifdef NS3_MODULE
Alexander Afanasyev3a229132012-04-25 15:07:26 -0700269 static const int m_syncResponseFreshness = 100; // milliseconds
Alexander Afanasyev55608612012-04-30 13:24:50 -0700270 static const int m_syncInterestReexpress = 10; // seconds
Alexander Afanasyev3a229132012-04-25 15:07:26 -0700271 // don't forget to adjust value in SyncCcnxWrapper
Alexander Afanasyev40942f42012-04-21 20:53:16 -0700272#else
Zhenkai Zhu60e8cb32012-06-04 17:29:30 -0700273 static const int m_syncResponseFreshness = 4;
Alexander Afanasyev89571f12012-04-23 13:44:57 -0700274 static const int m_syncInterestReexpress = 4;
Alexander Afanasyev40942f42012-04-21 20:53:16 -0700275#endif
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -0700276
Alexander Afanasyeva76010b2012-05-24 21:31:49 -0700277 static const int m_defaultRecoveryRetransmitInterval = 200; // milliseconds
278 uint32_t m_recoveryRetransmissionInterval; // milliseconds
279
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -0700280 enum EventLabels
281 {
282 DELAYED_INTEREST_PROCESSING = 1,
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700283 REEXPRESSING_INTEREST = 2,
284 REEXPRESSING_RECOVERY_INTEREST = 3
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -0700285 };
Zhenkai Zhu8d935c82012-03-06 10:44:12 -0800286};
287
288
289} // Sync
290
Zhenkai Zhu1ac6f802012-03-06 17:40:27 -0800291#endif // SYNC_APP_WRAPPER_H