blob: 5efb81e6145ccf0823999b108ed3b0ce93e957df [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>
Yingdi Yu280bb962014-01-30 09:52:43 -080034#include <ndn-cpp-dev/security/validator.hpp>
Yingdi Yue8154712014-01-21 10:20:14 -080035#include <ndn-cpp-dev/security/key-chain.hpp>
Yingdi Yu280bb962014-01-30 09:52:43 -080036#include <ndn-cpp-dev/util/scheduler.hpp>
Yingdi Yu46c9f1a2013-12-18 15:15:46 +080037
Zhenkai Zhu8d935c82012-03-06 10:44:12 -080038#include "sync-interest-table.h"
Zhenkai Zhu8d935c82012-03-06 10:44:12 -080039#include "sync-diff-state.h"
40#include "sync-full-state.h"
Chaoyi Bian4194b742012-03-08 17:21:35 -080041#include "sync-std-name-info.h"
Zhenkai Zhu8d935c82012-03-06 10:44:12 -080042
Alexander Afanasyevc1030192012-03-08 22:21:28 -080043#include "sync-diff-state-container.h"
44
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -070045#ifdef _DEBUG
46#ifdef HAVE_LOG4CXX
47#include <log4cxx/logger.h>
48#endif
49#endif
50
Alexander Afanasyev181d7e52012-04-09 13:54:11 -070051#ifdef NS3_MODULE
52#include <ns3/application.h>
Alexander Afanasyev73f7f9a2012-04-09 15:45:47 -070053#include <ns3/random-variable.h>
Alexander Afanasyev181d7e52012-04-09 13:54:11 -070054#endif
55
Zhenkai Zhu8d935c82012-03-06 10:44:12 -080056namespace Sync {
57
Zhenkai Zhu1cb29292012-05-31 22:54:34 -070058struct MissingDataInfo {
59 std::string prefix;
60 SeqNo low;
61 SeqNo high;
62};
63
Zhenkai Zhu8d935c82012-03-06 10:44:12 -080064/**
65 * \ingroup sync
Zhenkai Zhuaae81522012-03-06 11:05:44 -080066 * @brief A wrapper for SyncApp, which handles ccnx related things (process
67 * interests and data)
Zhenkai Zhu8d935c82012-03-06 10:44:12 -080068 */
Zhenkai Zhub9f19592012-10-16 14:27:38 -070069
Chaoyi Bian11f294f2012-03-08 14:28:06 -080070class SyncLogic
Alexander Afanasyev181d7e52012-04-09 13:54:11 -070071#ifdef NS3_MODULE
72 : public ns3::Application
73#endif
Zhenkai Zhu8d935c82012-03-06 10:44:12 -080074{
75public:
Zhenkai Zhu1cb29292012-05-31 22:54:34 -070076 //typedef boost::function< void ( const std::string &/*prefix*/, const SeqNo &/*newSeq*/, const SeqNo &/*oldSeq*/ ) > LogicUpdateCallback;
77 typedef boost::function< void (const std::vector<MissingDataInfo> & ) > LogicUpdateCallback;
Alexander Afanasyevd95c2312013-11-07 13:45:34 -080078 typedef boost::function< void (const std::string &/*prefix*/ ) > LogicRemoveCallback;
Zhenkai Zhub9f19592012-10-16 14:27:38 -070079 typedef boost::function< void (const std::string &)> LogicPerBranchCallback;
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -080080
Alexander Afanasyevc1030192012-03-08 22:21:28 -080081 /**
Alexander Afanasyev172d2b72012-03-08 23:43:39 -080082 * @brief Constructor
Alexander Afanasyevc1030192012-03-08 22:21:28 -080083 * @param syncPrefix the name prefix to use for the Sync Interest
Alexander Afanasyev03a58b72012-03-12 18:11:56 -070084 * @param onUpdate function that will be called when new state is detected
85 * @param onRemove function that will be called when state is removed
Alexander Afanasyev172d2b72012-03-08 23:43:39 -080086 * @param ccnxHandle ccnx handle
Alexander Afanasyevc1030192012-03-08 22:21:28 -080087 * the app data when new remote names are learned
88 */
Yingdi Yu43e71612013-10-30 22:19:31 -070089 SyncLogic (const ndn::Name& syncPrefix,
Yingdi Yu280bb962014-01-30 09:52:43 -080090 ndn::shared_ptr<ndn::Validator> validator,
91 ndn::shared_ptr<ndn::Face> face,
Alexander Afanasyev750d1872012-03-12 15:33:56 -070092 LogicUpdateCallback onUpdate,
Zhenkai Zhuce66e212012-03-12 22:27:19 -070093 LogicRemoveCallback onRemove);
Zhenkai Zhu46b26a12012-03-06 13:51:16 -080094
Yingdi Yu43e71612013-10-30 22:19:31 -070095 SyncLogic (const ndn::Name& syncPrefix,
Yingdi Yu280bb962014-01-30 09:52:43 -080096 ndn::shared_ptr<ndn::Validator> validator,
97 ndn::shared_ptr<ndn::Face> face,
Zhenkai Zhub9f19592012-10-16 14:27:38 -070098 LogicPerBranchCallback onUpdateBranch);
99
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800100 ~SyncLogic ();
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800101
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800102 /**
103 * a wrapper for the same func in SyncApp
104 */
Yingdi Yu280bb962014-01-30 09:52:43 -0800105 void addLocalNames (const ndn::Name &prefix, uint64_t session, uint64_t seq);
Zhenkai Zhu46b26a12012-03-06 13:51:16 -0800106
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800107 /**
108 * @brief respond to the Sync Interest; a lot of logic needs to go in here
109 * @param interest the Sync Interest in string format
110 */
Yingdi Yu280bb962014-01-30 09:52:43 -0800111 void respondSyncInterest (ndn::shared_ptr<ndn::Interest> interest);
Zhenkai Zhu46b26a12012-03-06 13:51:16 -0800112
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800113 /**
114 * @brief process the fetched sync data
Chaoyi Bian633255f2012-03-09 21:25:38 -0800115 * @param name the data name
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800116 * @param dataBuffer the sync data
117 */
Yingdi Yu280bb962014-01-30 09:52:43 -0800118 void respondSyncData (ndn::shared_ptr<ndn::Data> data);
Zhenkai Zhu8d935c82012-03-06 10:44:12 -0800119
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700120 /**
121 * @brief remove a participant's subtree from the sync tree
Alexander Afanasyev03a58b72012-03-12 18:11:56 -0700122 * @param prefix the name prefix for the participant
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700123 */
Yingdi Yu6d638f02014-01-24 11:01:21 -0800124 void remove (const ndn::Name &prefix);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700125
Zhenkai Zhue5660932012-06-04 15:25:20 -0700126 std::string
127 getRootDigest();
128
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700129#ifdef _DEBUG
Yingdi Yu280bb962014-01-30 09:52:43 -0800130 ndn::Scheduler &
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -0700131 getScheduler () { return m_scheduler; }
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700132#endif
Alexander Afanasyev181d7e52012-04-09 13:54:11 -0700133
Alexander Afanasyev181d7e52012-04-09 13:54:11 -0700134#ifdef NS3_MODULE
Alexander Afanasyev40942f42012-04-21 20:53:16 -0700135public:
Alexander Afanasyev181d7e52012-04-09 13:54:11 -0700136 virtual void StartApplication ();
137 virtual void StopApplication ();
138#endif
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700139
Zhenkai Zhueb1d8652012-12-23 15:25:39 -0800140 void stop();
141
Alexander Afanasyev5548c042012-10-04 19:10:09 -0700142 void
143 printState () const;
144
Zhenkai Zhud5aec4b2012-10-09 12:01:19 -0700145 std::map<std::string, bool>
146 getBranchPrefixes() const;
147
Yingdi Yu280bb962014-01-30 09:52:43 -0800148private:
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800149 void
Alexander Afanasyev45fba082012-03-12 18:05:24 -0700150 delayedChecksLoop ();
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700151
152 void
Yingdi Yu280bb962014-01-30 09:52:43 -0800153 onSyncInterest (const ndn::shared_ptr<const ndn::Name>& prefix,
154 const ndn::shared_ptr<const ndn::Interest>& interest);
Yingdi Yu43e71612013-10-30 22:19:31 -0700155
156 void
Yingdi Yu280bb962014-01-30 09:52:43 -0800157 onSyncRegisterFailed(const ndn::shared_ptr<const ndn::Name>& prefix);
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800158
159 void
Yingdi Yu280bb962014-01-30 09:52:43 -0800160 onSyncData(const ndn::shared_ptr<const ndn::Interest>& interest,
161 const ndn::shared_ptr<ndn::Data>& data,
162 const ndn::OnDataValidated& onValidated,
163 const ndn::OnDataValidationFailed& onValidationFailed);
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800164
165 void
Yingdi Yu280bb962014-01-30 09:52:43 -0800166 onSyncTimeout(const ndn::shared_ptr<const ndn::Interest>& interest);
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800167
168 void
Yingdi Yu280bb962014-01-30 09:52:43 -0800169 onSyncDataValidationFailed(const ndn::shared_ptr<const ndn::Data>& data);
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800170
171 void
Yingdi Yu280bb962014-01-30 09:52:43 -0800172 onSyncDataValidated(const ndn::shared_ptr<const ndn::Data>& data);
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800173
174 void
Yingdi Yu6d638f02014-01-24 11:01:21 -0800175 processSyncInterest (const ndn::Name &name,
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700176 DigestConstPtr digest, bool timedProcessing=false);
177
178 void
Yingdi Yu6d638f02014-01-24 11:01:21 -0800179 processSyncData (const ndn::Name &name,
Zhenkai Zhu3cfdcb92012-06-06 15:20:10 -0700180 DigestConstPtr digest, const char *wireData, size_t len);
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700181
Alexander Afanasyev45fba082012-03-12 18:05:24 -0700182 void
Yingdi Yu6d638f02014-01-24 11:01:21 -0800183 processSyncRecoveryInterest (const ndn::Name &name,
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700184 DigestConstPtr digest);
185
Alexander Afanasyev45fba082012-03-12 18:05:24 -0700186 void
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700187 insertToDiffLog (DiffStatePtr diff);
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700188
189 void
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700190 satisfyPendingSyncInterests (DiffStateConstPtr diff);
Zhenkai Zhu8d935c82012-03-06 10:44:12 -0800191
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700192 boost::tuple<DigestConstPtr, std::string>
Yingdi Yu6d638f02014-01-24 11:01:21 -0800193 convertNameToDigestAndType (const ndn::Name &name);
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700194
195 void
196 sendSyncInterest ();
197
198 void
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700199 sendSyncRecoveryInterests (DigestConstPtr digest);
200
201 void
Yingdi Yu6d638f02014-01-24 11:01:21 -0800202 sendSyncData (const ndn::Name &name,
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700203 DigestConstPtr digest, StateConstPtr state);
Alexander Afanasyev80237382012-10-04 10:20:47 -0700204
Zhenkai Zhu2d3e2702012-10-15 14:18:05 -0700205 void
Yingdi Yu6d638f02014-01-24 11:01:21 -0800206 sendSyncData (const ndn::Name &name,
Zhenkai Zhu2d3e2702012-10-15 14:18:05 -0700207 DigestConstPtr digest, SyncStateMsg &msg);
208
Alexander Afanasyev80237382012-10-04 10:20:47 -0700209 size_t
210 getNumberOfBranches () const;
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700211
Zhenkai Zhu8d935c82012-03-06 10:44:12 -0800212private:
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700213 FullStatePtr m_state;
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800214 DiffStateContainer m_log;
Alexander Afanasyev80237382012-10-04 10:20:47 -0700215 mutable boost::recursive_mutex m_stateMutex;
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700216
Yingdi Yu6d638f02014-01-24 11:01:21 -0800217 ndn::Name m_outstandingInterestName;
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800218 SyncInterestTable m_syncInterestTable;
219
Yingdi Yu43e71612013-10-30 22:19:31 -0700220 ndn::Name m_syncPrefix;
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700221 LogicUpdateCallback m_onUpdate;
222 LogicRemoveCallback m_onRemove;
Zhenkai Zhub9f19592012-10-16 14:27:38 -0700223 LogicPerBranchCallback m_onUpdateBranch;
224 bool m_perBranch;
Yingdi Yu280bb962014-01-30 09:52:43 -0800225 ndn::ptr_lib::shared_ptr<ndn::Validator> m_validator;
Yingdi Yu0cb0f2b2014-01-09 13:51:16 -0800226 ndn::ptr_lib::shared_ptr<ndn::KeyChain> m_keyChain;
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800227 ndn::ptr_lib::shared_ptr<ndn::Face> m_face;
Yingdi Yu280bb962014-01-30 09:52:43 -0800228 const ndn::RegisteredPrefixId* m_syncRegisteredPrefixId;
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800229
Yingdi Yu280bb962014-01-30 09:52:43 -0800230 ndn::Scheduler m_scheduler;
Chaoyi Bian633255f2012-03-09 21:25:38 -0800231
Alexander Afanasyev73f7f9a2012-04-09 15:45:47 -0700232#ifndef NS3_MODULE
Alexander Afanasyev45fba082012-03-12 18:05:24 -0700233 boost::mt19937 m_randomGenerator;
234 boost::variate_generator<boost::mt19937&, boost::uniform_int<> > m_rangeUniformRandom;
Alexander Afanasyev085742a2012-04-26 12:24:57 -0700235 boost::variate_generator<boost::mt19937&, boost::uniform_int<> > m_reexpressionJitter;
Alexander Afanasyev73f7f9a2012-04-09 15:45:47 -0700236#else
237 ns3::UniformVariable m_rangeUniformRandom;
Alexander Afanasyev085742a2012-04-26 12:24:57 -0700238 ns3::UniformVariable m_reexpressionJitter;
Alexander Afanasyev73f7f9a2012-04-09 15:45:47 -0700239#endif
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -0700240
Alexander Afanasyev3a229132012-04-25 15:07:26 -0700241 static const int m_unknownDigestStoreTime = 10; // seconds
Alexander Afanasyev89571f12012-04-23 13:44:57 -0700242#ifdef NS3_MODULE
Alexander Afanasyev3a229132012-04-25 15:07:26 -0700243 static const int m_syncResponseFreshness = 100; // milliseconds
Alexander Afanasyev55608612012-04-30 13:24:50 -0700244 static const int m_syncInterestReexpress = 10; // seconds
Alexander Afanasyev3a229132012-04-25 15:07:26 -0700245 // don't forget to adjust value in SyncCcnxWrapper
Alexander Afanasyev40942f42012-04-21 20:53:16 -0700246#else
Zhenkai Zhu60e8cb32012-06-04 17:29:30 -0700247 static const int m_syncResponseFreshness = 4;
Alexander Afanasyev89571f12012-04-23 13:44:57 -0700248 static const int m_syncInterestReexpress = 4;
Alexander Afanasyev40942f42012-04-21 20:53:16 -0700249#endif
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -0700250
Alexander Afanasyeva76010b2012-05-24 21:31:49 -0700251 static const int m_defaultRecoveryRetransmitInterval = 200; // milliseconds
252 uint32_t m_recoveryRetransmissionInterval; // milliseconds
253
Yingdi Yu280bb962014-01-30 09:52:43 -0800254 ndn::EventId m_delayedInterestProcessingId;
255 ndn::EventId m_reexpressingInterestId;
256 ndn::EventId m_reexpressingRecoveryInterestId;
Zhenkai Zhu8d935c82012-03-06 10:44:12 -0800257};
258
259
260} // Sync
261
Zhenkai Zhu1ac6f802012-03-06 17:40:27 -0800262#endif // SYNC_APP_WRAPPER_H