Zhenkai Zhu | 8d935c8 | 2012-03-06 10:44:12 -0800 | [diff] [blame] | 1 | /* -*- 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 Bian | 3e1eb16 | 2012-04-03 16:59:32 -0700 | [diff] [blame] | 19 | * Chaoyi Bian <bcy@pku.edu.cn> |
Alexander Afanasyev | c103019 | 2012-03-08 22:21:28 -0800 | [diff] [blame] | 20 | * Alexander Afanasyev <alexander.afanasyev@ucla.edu> |
Zhenkai Zhu | 8d935c8 | 2012-03-06 10:44:12 -0800 | [diff] [blame] | 21 | */ |
| 22 | |
Chaoyi Bian | 11f294f | 2012-03-08 14:28:06 -0800 | [diff] [blame] | 23 | #ifndef SYNC_LOGIC_H |
| 24 | #define SYNC_LOGIC_H |
Alexander Afanasyev | 45fba08 | 2012-03-12 18:05:24 -0700 | [diff] [blame] | 25 | |
Zhenkai Zhu | 8d935c8 | 2012-03-06 10:44:12 -0800 | [diff] [blame] | 26 | #include <boost/shared_ptr.hpp> |
Alexander Afanasyev | 750d187 | 2012-03-12 15:33:56 -0700 | [diff] [blame] | 27 | #include <boost/thread/recursive_mutex.hpp> |
Alexander Afanasyev | 45fba08 | 2012-03-12 18:05:24 -0700 | [diff] [blame] | 28 | #include <boost/random.hpp> |
Zhenkai Zhu | ce66e21 | 2012-03-12 22:27:19 -0700 | [diff] [blame] | 29 | #include <memory> |
Alexander Afanasyev | 387ac95 | 2012-03-11 23:49:27 -0700 | [diff] [blame] | 30 | |
Zhenkai Zhu | 8d935c8 | 2012-03-06 10:44:12 -0800 | [diff] [blame] | 31 | #include "sync-ccnx-wrapper.h" |
| 32 | #include "sync-interest-table.h" |
Zhenkai Zhu | 8d935c8 | 2012-03-06 10:44:12 -0800 | [diff] [blame] | 33 | #include "sync-diff-state.h" |
| 34 | #include "sync-full-state.h" |
Chaoyi Bian | 4194b74 | 2012-03-08 17:21:35 -0800 | [diff] [blame] | 35 | #include "sync-std-name-info.h" |
Alexander Afanasyev | 45fba08 | 2012-03-12 18:05:24 -0700 | [diff] [blame] | 36 | #include "sync-scheduler.h" |
Zhenkai Zhu | 8d935c8 | 2012-03-06 10:44:12 -0800 | [diff] [blame] | 37 | |
Alexander Afanasyev | c103019 | 2012-03-08 22:21:28 -0800 | [diff] [blame] | 38 | #include "sync-diff-state-container.h" |
| 39 | |
Alexander Afanasyev | 860e6fe | 2012-03-15 17:30:31 -0700 | [diff] [blame] | 40 | #ifdef _DEBUG |
| 41 | #ifdef HAVE_LOG4CXX |
| 42 | #include <log4cxx/logger.h> |
| 43 | #endif |
| 44 | #endif |
| 45 | |
Alexander Afanasyev | 181d7e5 | 2012-04-09 13:54:11 -0700 | [diff] [blame] | 46 | #ifdef NS3_MODULE |
| 47 | #include <ns3/application.h> |
Alexander Afanasyev | 73f7f9a | 2012-04-09 15:45:47 -0700 | [diff] [blame] | 48 | #include <ns3/random-variable.h> |
Alexander Afanasyev | 181d7e5 | 2012-04-09 13:54:11 -0700 | [diff] [blame] | 49 | #endif |
| 50 | |
Zhenkai Zhu | 8d935c8 | 2012-03-06 10:44:12 -0800 | [diff] [blame] | 51 | namespace Sync { |
| 52 | |
Zhenkai Zhu | 1cb2929 | 2012-05-31 22:54:34 -0700 | [diff] [blame] | 53 | struct MissingDataInfo { |
| 54 | std::string prefix; |
| 55 | SeqNo low; |
| 56 | SeqNo high; |
| 57 | }; |
| 58 | |
Zhenkai Zhu | 8d935c8 | 2012-03-06 10:44:12 -0800 | [diff] [blame] | 59 | /** |
| 60 | * \ingroup sync |
Zhenkai Zhu | aae8152 | 2012-03-06 11:05:44 -0800 | [diff] [blame] | 61 | * @brief A wrapper for SyncApp, which handles ccnx related things (process |
| 62 | * interests and data) |
Zhenkai Zhu | 8d935c8 | 2012-03-06 10:44:12 -0800 | [diff] [blame] | 63 | */ |
Chaoyi Bian | 11f294f | 2012-03-08 14:28:06 -0800 | [diff] [blame] | 64 | class SyncLogic |
Alexander Afanasyev | 181d7e5 | 2012-04-09 13:54:11 -0700 | [diff] [blame] | 65 | #ifdef NS3_MODULE |
| 66 | : public ns3::Application |
| 67 | #endif |
Zhenkai Zhu | 8d935c8 | 2012-03-06 10:44:12 -0800 | [diff] [blame] | 68 | { |
| 69 | public: |
Zhenkai Zhu | 1cb2929 | 2012-05-31 22:54:34 -0700 | [diff] [blame] | 70 | //typedef boost::function< void ( const std::string &/*prefix*/, const SeqNo &/*newSeq*/, const SeqNo &/*oldSeq*/ ) > LogicUpdateCallback; |
| 71 | typedef boost::function< void (const std::vector<MissingDataInfo> & ) > LogicUpdateCallback; |
Alexander Afanasyev | 750d187 | 2012-03-12 15:33:56 -0700 | [diff] [blame] | 72 | typedef boost::function< void ( const std::string &/*prefix*/ ) > LogicRemoveCallback; |
Chaoyi Bian | 89ee2dc | 2012-03-09 14:06:01 -0800 | [diff] [blame] | 73 | |
Alexander Afanasyev | c103019 | 2012-03-08 22:21:28 -0800 | [diff] [blame] | 74 | /** |
Alexander Afanasyev | 172d2b7 | 2012-03-08 23:43:39 -0800 | [diff] [blame] | 75 | * @brief Constructor |
Alexander Afanasyev | c103019 | 2012-03-08 22:21:28 -0800 | [diff] [blame] | 76 | * @param syncPrefix the name prefix to use for the Sync Interest |
Alexander Afanasyev | 03a58b7 | 2012-03-12 18:11:56 -0700 | [diff] [blame] | 77 | * @param onUpdate function that will be called when new state is detected |
| 78 | * @param onRemove function that will be called when state is removed |
Alexander Afanasyev | 172d2b7 | 2012-03-08 23:43:39 -0800 | [diff] [blame] | 79 | * @param ccnxHandle ccnx handle |
Alexander Afanasyev | c103019 | 2012-03-08 22:21:28 -0800 | [diff] [blame] | 80 | * the app data when new remote names are learned |
| 81 | */ |
Alexander Afanasyev | 750d187 | 2012-03-12 15:33:56 -0700 | [diff] [blame] | 82 | SyncLogic (const std::string &syncPrefix, |
| 83 | LogicUpdateCallback onUpdate, |
Zhenkai Zhu | ce66e21 | 2012-03-12 22:27:19 -0700 | [diff] [blame] | 84 | LogicRemoveCallback onRemove); |
Zhenkai Zhu | 46b26a1 | 2012-03-06 13:51:16 -0800 | [diff] [blame] | 85 | |
Alexander Afanasyev | c103019 | 2012-03-08 22:21:28 -0800 | [diff] [blame] | 86 | ~SyncLogic (); |
Chaoyi Bian | 89ee2dc | 2012-03-09 14:06:01 -0800 | [diff] [blame] | 87 | |
Alexander Afanasyev | c103019 | 2012-03-08 22:21:28 -0800 | [diff] [blame] | 88 | /** |
| 89 | * a wrapper for the same func in SyncApp |
| 90 | */ |
| 91 | void addLocalNames (const std::string &prefix, uint32_t session, uint32_t seq); |
Zhenkai Zhu | 46b26a1 | 2012-03-06 13:51:16 -0800 | [diff] [blame] | 92 | |
Alexander Afanasyev | c103019 | 2012-03-08 22:21:28 -0800 | [diff] [blame] | 93 | /** |
| 94 | * @brief respond to the Sync Interest; a lot of logic needs to go in here |
| 95 | * @param interest the Sync Interest in string format |
| 96 | */ |
| 97 | void respondSyncInterest (const std::string &interest); |
Zhenkai Zhu | 46b26a1 | 2012-03-06 13:51:16 -0800 | [diff] [blame] | 98 | |
Alexander Afanasyev | c103019 | 2012-03-08 22:21:28 -0800 | [diff] [blame] | 99 | /** |
| 100 | * @brief process the fetched sync data |
Chaoyi Bian | 633255f | 2012-03-09 21:25:38 -0800 | [diff] [blame] | 101 | * @param name the data name |
Alexander Afanasyev | c103019 | 2012-03-08 22:21:28 -0800 | [diff] [blame] | 102 | * @param dataBuffer the sync data |
| 103 | */ |
Zhenkai Zhu | 3cfdcb9 | 2012-06-06 15:20:10 -0700 | [diff] [blame] | 104 | void respondSyncData (const std::string &name, const char *wireData, size_t len); |
Zhenkai Zhu | 8d935c8 | 2012-03-06 10:44:12 -0800 | [diff] [blame] | 105 | |
Zhenkai Zhu | 0efa37b | 2012-03-12 13:54:12 -0700 | [diff] [blame] | 106 | /** |
| 107 | * @brief remove a participant's subtree from the sync tree |
Alexander Afanasyev | 03a58b7 | 2012-03-12 18:11:56 -0700 | [diff] [blame] | 108 | * @param prefix the name prefix for the participant |
Zhenkai Zhu | 0efa37b | 2012-03-12 13:54:12 -0700 | [diff] [blame] | 109 | */ |
Alexander Afanasyev | 03a58b7 | 2012-03-12 18:11:56 -0700 | [diff] [blame] | 110 | void remove (const std::string &prefix); |
Zhenkai Zhu | 0efa37b | 2012-03-12 13:54:12 -0700 | [diff] [blame] | 111 | |
Zhenkai Zhu | e566093 | 2012-06-04 15:25:20 -0700 | [diff] [blame] | 112 | std::string |
| 113 | getRootDigest(); |
| 114 | |
Alexander Afanasyev | 387ac95 | 2012-03-11 23:49:27 -0700 | [diff] [blame] | 115 | #ifdef _DEBUG |
Alexander Afanasyev | 45fba08 | 2012-03-12 18:05:24 -0700 | [diff] [blame] | 116 | Scheduler & |
Alexander Afanasyev | bf2b436 | 2012-03-12 23:55:09 -0700 | [diff] [blame] | 117 | getScheduler () { return m_scheduler; } |
Alexander Afanasyev | 387ac95 | 2012-03-11 23:49:27 -0700 | [diff] [blame] | 118 | #endif |
Alexander Afanasyev | 181d7e5 | 2012-04-09 13:54:11 -0700 | [diff] [blame] | 119 | |
Alexander Afanasyev | 181d7e5 | 2012-04-09 13:54:11 -0700 | [diff] [blame] | 120 | #ifdef NS3_MODULE |
Alexander Afanasyev | 40942f4 | 2012-04-21 20:53:16 -0700 | [diff] [blame] | 121 | public: |
Alexander Afanasyev | 181d7e5 | 2012-04-09 13:54:11 -0700 | [diff] [blame] | 122 | virtual void StartApplication (); |
| 123 | virtual void StopApplication (); |
| 124 | #endif |
Alexander Afanasyev | 387ac95 | 2012-03-11 23:49:27 -0700 | [diff] [blame] | 125 | |
Zhenkai Zhu | 8d935c8 | 2012-03-06 10:44:12 -0800 | [diff] [blame] | 126 | private: |
Alexander Afanasyev | 45fba08 | 2012-03-12 18:05:24 -0700 | [diff] [blame] | 127 | void |
| 128 | delayedChecksLoop (); |
Alexander Afanasyev | 387ac95 | 2012-03-11 23:49:27 -0700 | [diff] [blame] | 129 | |
| 130 | void |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 131 | processSyncInterest (const std::string &name, |
| 132 | DigestConstPtr digest, bool timedProcessing=false); |
| 133 | |
| 134 | void |
| 135 | processSyncData (const std::string &name, |
Zhenkai Zhu | 3cfdcb9 | 2012-06-06 15:20:10 -0700 | [diff] [blame] | 136 | DigestConstPtr digest, const char *wireData, size_t len); |
Alexander Afanasyev | 387ac95 | 2012-03-11 23:49:27 -0700 | [diff] [blame] | 137 | |
Alexander Afanasyev | 45fba08 | 2012-03-12 18:05:24 -0700 | [diff] [blame] | 138 | void |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 139 | processSyncRecoveryInterest (const std::string &name, |
| 140 | DigestConstPtr digest); |
| 141 | |
Alexander Afanasyev | 45fba08 | 2012-03-12 18:05:24 -0700 | [diff] [blame] | 142 | void |
Alexander Afanasyev | 235c6d7 | 2012-03-15 22:28:43 -0700 | [diff] [blame] | 143 | insertToDiffLog (DiffStatePtr diff); |
Alexander Afanasyev | 1b449c4 | 2012-03-13 20:24:07 -0700 | [diff] [blame] | 144 | |
| 145 | void |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 146 | satisfyPendingSyncInterests (DiffStateConstPtr diff); |
Zhenkai Zhu | 8d935c8 | 2012-03-06 10:44:12 -0800 | [diff] [blame] | 147 | |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 148 | boost::tuple<DigestConstPtr, std::string> |
| 149 | convertNameToDigestAndType (const std::string &name); |
| 150 | |
| 151 | void |
| 152 | sendSyncInterest (); |
| 153 | |
| 154 | void |
Alexander Afanasyev | 46eb526 | 2012-05-10 16:30:35 -0700 | [diff] [blame] | 155 | sendSyncRecoveryInterests (DigestConstPtr digest); |
| 156 | |
| 157 | void |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 158 | sendSyncData (const std::string &name, |
| 159 | DigestConstPtr digest, StateConstPtr state); |
Alexander Afanasyev | 8023738 | 2012-10-04 10:20:47 -0700 | [diff] [blame^] | 160 | |
| 161 | size_t |
| 162 | getNumberOfBranches () const; |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 163 | |
Zhenkai Zhu | 8d935c8 | 2012-03-06 10:44:12 -0800 | [diff] [blame] | 164 | private: |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 165 | FullStatePtr m_state; |
Chaoyi Bian | 89ee2dc | 2012-03-09 14:06:01 -0800 | [diff] [blame] | 166 | DiffStateContainer m_log; |
Alexander Afanasyev | 8023738 | 2012-10-04 10:20:47 -0700 | [diff] [blame^] | 167 | mutable boost::recursive_mutex m_stateMutex; |
Alexander Afanasyev | 750d187 | 2012-03-12 15:33:56 -0700 | [diff] [blame] | 168 | |
Alexander Afanasyev | f3c03a9 | 2012-05-09 12:00:37 -0700 | [diff] [blame] | 169 | std::string m_outstandingInterestName; |
Alexander Afanasyev | c103019 | 2012-03-08 22:21:28 -0800 | [diff] [blame] | 170 | SyncInterestTable m_syncInterestTable; |
| 171 | |
| 172 | std::string m_syncPrefix; |
Alexander Afanasyev | 750d187 | 2012-03-12 15:33:56 -0700 | [diff] [blame] | 173 | LogicUpdateCallback m_onUpdate; |
| 174 | LogicRemoveCallback m_onRemove; |
Zhenkai Zhu | ce66e21 | 2012-03-12 22:27:19 -0700 | [diff] [blame] | 175 | std::auto_ptr<CcnxWrapper> m_ccnxHandle; |
Chaoyi Bian | 89ee2dc | 2012-03-09 14:06:01 -0800 | [diff] [blame] | 176 | |
Alexander Afanasyev | bf2b436 | 2012-03-12 23:55:09 -0700 | [diff] [blame] | 177 | Scheduler m_scheduler; |
Chaoyi Bian | 633255f | 2012-03-09 21:25:38 -0800 | [diff] [blame] | 178 | |
Alexander Afanasyev | 73f7f9a | 2012-04-09 15:45:47 -0700 | [diff] [blame] | 179 | #ifndef NS3_MODULE |
Alexander Afanasyev | 45fba08 | 2012-03-12 18:05:24 -0700 | [diff] [blame] | 180 | boost::mt19937 m_randomGenerator; |
| 181 | boost::variate_generator<boost::mt19937&, boost::uniform_int<> > m_rangeUniformRandom; |
Alexander Afanasyev | 085742a | 2012-04-26 12:24:57 -0700 | [diff] [blame] | 182 | boost::variate_generator<boost::mt19937&, boost::uniform_int<> > m_reexpressionJitter; |
Alexander Afanasyev | 73f7f9a | 2012-04-09 15:45:47 -0700 | [diff] [blame] | 183 | #else |
| 184 | ns3::UniformVariable m_rangeUniformRandom; |
Alexander Afanasyev | 085742a | 2012-04-26 12:24:57 -0700 | [diff] [blame] | 185 | ns3::UniformVariable m_reexpressionJitter; |
Alexander Afanasyev | 73f7f9a | 2012-04-09 15:45:47 -0700 | [diff] [blame] | 186 | #endif |
Alexander Afanasyev | bf2b436 | 2012-03-12 23:55:09 -0700 | [diff] [blame] | 187 | |
Alexander Afanasyev | 3a22913 | 2012-04-25 15:07:26 -0700 | [diff] [blame] | 188 | static const int m_unknownDigestStoreTime = 10; // seconds |
Alexander Afanasyev | 89571f1 | 2012-04-23 13:44:57 -0700 | [diff] [blame] | 189 | #ifdef NS3_MODULE |
Alexander Afanasyev | 3a22913 | 2012-04-25 15:07:26 -0700 | [diff] [blame] | 190 | static const int m_syncResponseFreshness = 100; // milliseconds |
Alexander Afanasyev | 5560861 | 2012-04-30 13:24:50 -0700 | [diff] [blame] | 191 | static const int m_syncInterestReexpress = 10; // seconds |
Alexander Afanasyev | 3a22913 | 2012-04-25 15:07:26 -0700 | [diff] [blame] | 192 | // don't forget to adjust value in SyncCcnxWrapper |
Alexander Afanasyev | 40942f4 | 2012-04-21 20:53:16 -0700 | [diff] [blame] | 193 | #else |
Zhenkai Zhu | 60e8cb3 | 2012-06-04 17:29:30 -0700 | [diff] [blame] | 194 | static const int m_syncResponseFreshness = 4; |
Alexander Afanasyev | 89571f1 | 2012-04-23 13:44:57 -0700 | [diff] [blame] | 195 | static const int m_syncInterestReexpress = 4; |
Alexander Afanasyev | 40942f4 | 2012-04-21 20:53:16 -0700 | [diff] [blame] | 196 | #endif |
Alexander Afanasyev | bf2b436 | 2012-03-12 23:55:09 -0700 | [diff] [blame] | 197 | |
Alexander Afanasyev | a76010b | 2012-05-24 21:31:49 -0700 | [diff] [blame] | 198 | static const int m_defaultRecoveryRetransmitInterval = 200; // milliseconds |
| 199 | uint32_t m_recoveryRetransmissionInterval; // milliseconds |
| 200 | |
Alexander Afanasyev | bf2b436 | 2012-03-12 23:55:09 -0700 | [diff] [blame] | 201 | enum EventLabels |
| 202 | { |
| 203 | DELAYED_INTEREST_PROCESSING = 1, |
Alexander Afanasyev | 46eb526 | 2012-05-10 16:30:35 -0700 | [diff] [blame] | 204 | REEXPRESSING_INTEREST = 2, |
| 205 | REEXPRESSING_RECOVERY_INTEREST = 3 |
Alexander Afanasyev | bf2b436 | 2012-03-12 23:55:09 -0700 | [diff] [blame] | 206 | }; |
Zhenkai Zhu | 8d935c8 | 2012-03-06 10:44:12 -0800 | [diff] [blame] | 207 | }; |
| 208 | |
| 209 | |
| 210 | } // Sync |
| 211 | |
Zhenkai Zhu | 1ac6f80 | 2012-03-06 17:40:27 -0800 | [diff] [blame] | 212 | #endif // SYNC_APP_WRAPPER_H |