blob: d3e70c126cb139eb53212a4d874d0bec27350581 [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
Zhenkai Zhu8d935c82012-03-06 10:44:12 -080027#include <boost/shared_ptr.hpp>
Alexander Afanasyev750d1872012-03-12 15:33:56 -070028#include <boost/thread/recursive_mutex.hpp>
Alexander Afanasyev45fba082012-03-12 18:05:24 -070029#include <boost/random.hpp>
Zhenkai Zhuce66e212012-03-12 22:27:19 -070030#include <memory>
Zhenkai Zhud5aec4b2012-10-09 12:01:19 -070031#include <map>
Alexander Afanasyev387ac952012-03-11 23:49:27 -070032
Yingdi Yue8154712014-01-21 10:20:14 -080033#include <ndn-cpp-dev/face.hpp>
34#include <ndn-cpp-dev/security/verifier.hpp>
35#include <ndn-cpp-dev/security/key-chain.hpp>
Yingdi Yu46c9f1a2013-12-18 15:15:46 +080036
Zhenkai Zhu8d935c82012-03-06 10:44:12 -080037#include "sync-interest-table.h"
Zhenkai Zhu8d935c82012-03-06 10:44:12 -080038#include "sync-diff-state.h"
39#include "sync-full-state.h"
Chaoyi Bian4194b742012-03-08 17:21:35 -080040#include "sync-std-name-info.h"
Alexander Afanasyev45fba082012-03-12 18:05:24 -070041#include "sync-scheduler.h"
Yingdi Yu5e0af3e2014-01-15 19:33:25 -080042#include "sec-policy-sync.h"
Zhenkai Zhu8d935c82012-03-06 10:44:12 -080043
Alexander Afanasyevc1030192012-03-08 22:21:28 -080044#include "sync-diff-state-container.h"
45
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -070046#ifdef _DEBUG
47#ifdef HAVE_LOG4CXX
48#include <log4cxx/logger.h>
49#endif
50#endif
51
Alexander Afanasyev181d7e52012-04-09 13:54:11 -070052#ifdef NS3_MODULE
53#include <ns3/application.h>
Alexander Afanasyev73f7f9a2012-04-09 15:45:47 -070054#include <ns3/random-variable.h>
Alexander Afanasyev181d7e52012-04-09 13:54:11 -070055#endif
56
Zhenkai Zhu8d935c82012-03-06 10:44:12 -080057namespace Sync {
58
Zhenkai Zhu1cb29292012-05-31 22:54:34 -070059struct MissingDataInfo {
60 std::string prefix;
61 SeqNo low;
62 SeqNo high;
63};
64
Zhenkai Zhu8d935c82012-03-06 10:44:12 -080065/**
66 * \ingroup sync
Zhenkai Zhuaae81522012-03-06 11:05:44 -080067 * @brief A wrapper for SyncApp, which handles ccnx related things (process
68 * interests and data)
Zhenkai Zhu8d935c82012-03-06 10:44:12 -080069 */
Zhenkai Zhub9f19592012-10-16 14:27:38 -070070
Chaoyi Bian11f294f2012-03-08 14:28:06 -080071class SyncLogic
Alexander Afanasyev181d7e52012-04-09 13:54:11 -070072#ifdef NS3_MODULE
73 : public ns3::Application
74#endif
Zhenkai Zhu8d935c82012-03-06 10:44:12 -080075{
76public:
Zhenkai Zhu1cb29292012-05-31 22:54:34 -070077 //typedef boost::function< void ( const std::string &/*prefix*/, const SeqNo &/*newSeq*/, const SeqNo &/*oldSeq*/ ) > LogicUpdateCallback;
78 typedef boost::function< void (const std::vector<MissingDataInfo> & ) > LogicUpdateCallback;
Alexander Afanasyevd95c2312013-11-07 13:45:34 -080079 typedef boost::function< void (const std::string &/*prefix*/ ) > LogicRemoveCallback;
Zhenkai Zhub9f19592012-10-16 14:27:38 -070080 typedef boost::function< void (const std::string &)> LogicPerBranchCallback;
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -080081
Alexander Afanasyevc1030192012-03-08 22:21:28 -080082 /**
Alexander Afanasyev172d2b72012-03-08 23:43:39 -080083 * @brief Constructor
Alexander Afanasyevc1030192012-03-08 22:21:28 -080084 * @param syncPrefix the name prefix to use for the Sync Interest
Alexander Afanasyev03a58b72012-03-12 18:11:56 -070085 * @param onUpdate function that will be called when new state is detected
86 * @param onRemove function that will be called when state is removed
Alexander Afanasyev172d2b72012-03-08 23:43:39 -080087 * @param ccnxHandle ccnx handle
Alexander Afanasyevc1030192012-03-08 22:21:28 -080088 * the app data when new remote names are learned
89 */
Yingdi Yu43e71612013-10-30 22:19:31 -070090 SyncLogic (const ndn::Name& syncPrefix,
Yingdi Yu5e0af3e2014-01-15 19:33:25 -080091 ndn::ptr_lib::shared_ptr<SecPolicySync> syncPolicyManager,
Yingdi Yu6e235db2013-12-27 08:40:53 +080092 ndn::ptr_lib::shared_ptr<ndn::Face> face,
Alexander Afanasyev750d1872012-03-12 15:33:56 -070093 LogicUpdateCallback onUpdate,
Zhenkai Zhuce66e212012-03-12 22:27:19 -070094 LogicRemoveCallback onRemove);
Zhenkai Zhu46b26a12012-03-06 13:51:16 -080095
Yingdi Yu43e71612013-10-30 22:19:31 -070096 SyncLogic (const ndn::Name& syncPrefix,
Yingdi Yu5e0af3e2014-01-15 19:33:25 -080097 ndn::ptr_lib::shared_ptr<SecPolicySync> syncPolicyManager,
Yingdi Yu6e235db2013-12-27 08:40:53 +080098 ndn::ptr_lib::shared_ptr<ndn::Face> face,
Zhenkai Zhub9f19592012-10-16 14:27:38 -070099 LogicPerBranchCallback onUpdateBranch);
100
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800101 ~SyncLogic ();
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800102
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800103 /**
104 * a wrapper for the same func in SyncApp
105 */
Yingdi Yu6d638f02014-01-24 11:01:21 -0800106 void addLocalNames (const ndn::Name &prefix, uint32_t session, uint32_t seq);
Zhenkai Zhu46b26a12012-03-06 13:51:16 -0800107
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800108 /**
109 * @brief respond to the Sync Interest; a lot of logic needs to go in here
110 * @param interest the Sync Interest in string format
111 */
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800112 void respondSyncInterest (ndn::ptr_lib::shared_ptr<ndn::Interest> interest);
Zhenkai Zhu46b26a12012-03-06 13:51:16 -0800113
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800114 /**
115 * @brief process the fetched sync data
Chaoyi Bian633255f2012-03-09 21:25:38 -0800116 * @param name the data name
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800117 * @param dataBuffer the sync data
118 */
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800119 void respondSyncData (ndn::ptr_lib::shared_ptr<ndn::Data> data);
Zhenkai Zhu8d935c82012-03-06 10:44:12 -0800120
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700121 /**
122 * @brief remove a participant's subtree from the sync tree
Alexander Afanasyev03a58b72012-03-12 18:11:56 -0700123 * @param prefix the name prefix for the participant
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700124 */
Yingdi Yu6d638f02014-01-24 11:01:21 -0800125 void remove (const ndn::Name &prefix);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700126
Zhenkai Zhue5660932012-06-04 15:25:20 -0700127 std::string
128 getRootDigest();
129
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700130#ifdef _DEBUG
Alexander Afanasyev45fba082012-03-12 18:05:24 -0700131 Scheduler &
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -0700132 getScheduler () { return m_scheduler; }
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700133#endif
Alexander Afanasyev181d7e52012-04-09 13:54:11 -0700134
Alexander Afanasyev181d7e52012-04-09 13:54:11 -0700135#ifdef NS3_MODULE
Alexander Afanasyev40942f42012-04-21 20:53:16 -0700136public:
Alexander Afanasyev181d7e52012-04-09 13:54:11 -0700137 virtual void StartApplication ();
138 virtual void StopApplication ();
139#endif
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700140
Zhenkai Zhueb1d8652012-12-23 15:25:39 -0800141 void stop();
142
Alexander Afanasyev5548c042012-10-04 19:10:09 -0700143 void
144 printState () const;
145
Zhenkai Zhud5aec4b2012-10-09 12:01:19 -0700146 std::map<std::string, bool>
147 getBranchPrefixes() const;
148
Zhenkai Zhu8d935c82012-03-06 10:44:12 -0800149private:
Yingdi Yu6e235db2013-12-27 08:40:53 +0800150 // void
151 // connectToDaemon();
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800152
Yingdi Yu6e235db2013-12-27 08:40:53 +0800153 // void
154 // onConnectionData(const ndn::ptr_lib::shared_ptr<const ndn::Interest>& interest,
155 // const ndn::ptr_lib::shared_ptr<ndn::Data>& data);
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800156
Yingdi Yu6e235db2013-12-27 08:40:53 +0800157 // void
158 // onConnectionDataTimeout(const ndn::ptr_lib::shared_ptr<const ndn::Interest>& interest);
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800159
160 void
Alexander Afanasyev45fba082012-03-12 18:05:24 -0700161 delayedChecksLoop ();
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700162
163 void
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800164 onSyncInterest (const ndn::ptr_lib::shared_ptr<const ndn::Name>& prefix,
165 const ndn::ptr_lib::shared_ptr<const ndn::Interest>& interest,
166 ndn::Transport& transport,
167 uint64_t registeredPrefixId);
Yingdi Yu43e71612013-10-30 22:19:31 -0700168
169 void
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800170 onSyncRegisterFailed(const ndn::ptr_lib::shared_ptr<const ndn::Name>& prefix);
171
172 void
173 onSyncData(const ndn::ptr_lib::shared_ptr<const ndn::Interest>& interest,
174 const ndn::ptr_lib::shared_ptr<ndn::Data>& data,
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800175 const ndn::OnVerified& onVerified,
176 const ndn::OnVerifyFailed& onVerifyFailed);
177
178 void
179 onSyncDataTimeout(const ndn::ptr_lib::shared_ptr<const ndn::Interest>& interest,
180 int retry,
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800181 const ndn::OnVerified& onVerified,
182 const ndn::OnVerifyFailed& onVerifyFailed);
183
184 void
185 onSyncDataVerifyFailed(const ndn::ptr_lib::shared_ptr<ndn::Data>& data);
186
187 void
188 onSyncDataVerified(const ndn::ptr_lib::shared_ptr<ndn::Data>& data);
189
190 void
Yingdi Yu6d638f02014-01-24 11:01:21 -0800191 processSyncInterest (const ndn::Name &name,
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700192 DigestConstPtr digest, bool timedProcessing=false);
193
194 void
Yingdi Yu6d638f02014-01-24 11:01:21 -0800195 processSyncData (const ndn::Name &name,
Zhenkai Zhu3cfdcb92012-06-06 15:20:10 -0700196 DigestConstPtr digest, const char *wireData, size_t len);
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700197
Alexander Afanasyev45fba082012-03-12 18:05:24 -0700198 void
Yingdi Yu6d638f02014-01-24 11:01:21 -0800199 processSyncRecoveryInterest (const ndn::Name &name,
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700200 DigestConstPtr digest);
201
Alexander Afanasyev45fba082012-03-12 18:05:24 -0700202 void
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700203 insertToDiffLog (DiffStatePtr diff);
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700204
205 void
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700206 satisfyPendingSyncInterests (DiffStateConstPtr diff);
Zhenkai Zhu8d935c82012-03-06 10:44:12 -0800207
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700208 boost::tuple<DigestConstPtr, std::string>
Yingdi Yu6d638f02014-01-24 11:01:21 -0800209 convertNameToDigestAndType (const ndn::Name &name);
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700210
211 void
212 sendSyncInterest ();
213
214 void
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700215 sendSyncRecoveryInterests (DigestConstPtr digest);
216
217 void
Yingdi Yu6d638f02014-01-24 11:01:21 -0800218 sendSyncData (const ndn::Name &name,
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700219 DigestConstPtr digest, StateConstPtr state);
Alexander Afanasyev80237382012-10-04 10:20:47 -0700220
Zhenkai Zhu2d3e2702012-10-15 14:18:05 -0700221 void
Yingdi Yu6d638f02014-01-24 11:01:21 -0800222 sendSyncData (const ndn::Name &name,
Zhenkai Zhu2d3e2702012-10-15 14:18:05 -0700223 DigestConstPtr digest, SyncStateMsg &msg);
224
Alexander Afanasyev80237382012-10-04 10:20:47 -0700225 size_t
226 getNumberOfBranches () const;
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700227
Zhenkai Zhu8d935c82012-03-06 10:44:12 -0800228private:
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700229 FullStatePtr m_state;
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800230 DiffStateContainer m_log;
Alexander Afanasyev80237382012-10-04 10:20:47 -0700231 mutable boost::recursive_mutex m_stateMutex;
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700232
Yingdi Yu6d638f02014-01-24 11:01:21 -0800233 ndn::Name m_outstandingInterestName;
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800234 SyncInterestTable m_syncInterestTable;
235
Yingdi Yu43e71612013-10-30 22:19:31 -0700236 ndn::Name m_syncPrefix;
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700237 LogicUpdateCallback m_onUpdate;
238 LogicRemoveCallback m_onRemove;
Zhenkai Zhub9f19592012-10-16 14:27:38 -0700239 LogicPerBranchCallback m_onUpdateBranch;
240 bool m_perBranch;
Yingdi Yu5e0af3e2014-01-15 19:33:25 -0800241 ndn::ptr_lib::shared_ptr<SecPolicySync> m_policy;
Yingdi Yu0cb0f2b2014-01-09 13:51:16 -0800242 ndn::ptr_lib::shared_ptr<ndn::Verifier> m_verifier;
243 ndn::ptr_lib::shared_ptr<ndn::KeyChain> m_keyChain;
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800244 ndn::ptr_lib::shared_ptr<ndn::Face> m_face;
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800245 uint64_t m_syncRegisteredPrefixId;
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800246
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -0700247 Scheduler m_scheduler;
Chaoyi Bian633255f2012-03-09 21:25:38 -0800248
Alexander Afanasyev73f7f9a2012-04-09 15:45:47 -0700249#ifndef NS3_MODULE
Alexander Afanasyev45fba082012-03-12 18:05:24 -0700250 boost::mt19937 m_randomGenerator;
251 boost::variate_generator<boost::mt19937&, boost::uniform_int<> > m_rangeUniformRandom;
Alexander Afanasyev085742a2012-04-26 12:24:57 -0700252 boost::variate_generator<boost::mt19937&, boost::uniform_int<> > m_reexpressionJitter;
Alexander Afanasyev73f7f9a2012-04-09 15:45:47 -0700253#else
254 ns3::UniformVariable m_rangeUniformRandom;
Alexander Afanasyev085742a2012-04-26 12:24:57 -0700255 ns3::UniformVariable m_reexpressionJitter;
Alexander Afanasyev73f7f9a2012-04-09 15:45:47 -0700256#endif
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -0700257
Alexander Afanasyev3a229132012-04-25 15:07:26 -0700258 static const int m_unknownDigestStoreTime = 10; // seconds
Alexander Afanasyev89571f12012-04-23 13:44:57 -0700259#ifdef NS3_MODULE
Alexander Afanasyev3a229132012-04-25 15:07:26 -0700260 static const int m_syncResponseFreshness = 100; // milliseconds
Alexander Afanasyev55608612012-04-30 13:24:50 -0700261 static const int m_syncInterestReexpress = 10; // seconds
Alexander Afanasyev3a229132012-04-25 15:07:26 -0700262 // don't forget to adjust value in SyncCcnxWrapper
Alexander Afanasyev40942f42012-04-21 20:53:16 -0700263#else
Zhenkai Zhu60e8cb32012-06-04 17:29:30 -0700264 static const int m_syncResponseFreshness = 4;
Alexander Afanasyev89571f12012-04-23 13:44:57 -0700265 static const int m_syncInterestReexpress = 4;
Alexander Afanasyev40942f42012-04-21 20:53:16 -0700266#endif
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -0700267
Alexander Afanasyeva76010b2012-05-24 21:31:49 -0700268 static const int m_defaultRecoveryRetransmitInterval = 200; // milliseconds
269 uint32_t m_recoveryRetransmissionInterval; // milliseconds
270
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -0700271 enum EventLabels
272 {
273 DELAYED_INTEREST_PROCESSING = 1,
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700274 REEXPRESSING_INTEREST = 2,
275 REEXPRESSING_RECOVERY_INTEREST = 3
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -0700276 };
Zhenkai Zhu8d935c82012-03-06 10:44:12 -0800277};
278
279
280} // Sync
281
Zhenkai Zhu1ac6f802012-03-06 17:40:27 -0800282#endif // SYNC_APP_WRAPPER_H