akmhoque | 66e6618 | 2014-02-21 17:56:03 -0600 | [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> |
| 19 | * Chaoyi Bian <bcy@pku.edu.cn> |
| 20 | * Alexander Afanasyev <alexander.afanasyev@ucla.edu> |
| 21 | * Yingdi Yu <yingdi@cs.ucla.edu> |
| 22 | */ |
| 23 | |
| 24 | #ifndef SYNC_LOGIC_H |
| 25 | #define SYNC_LOGIC_H |
| 26 | |
| 27 | #include <boost/random.hpp> |
| 28 | #include <memory> |
| 29 | #include <map> |
| 30 | |
| 31 | #include <ndn-cpp-dev/face.hpp> |
| 32 | #include <ndn-cpp-dev/security/validator.hpp> |
| 33 | #include <ndn-cpp-dev/security/key-chain.hpp> |
| 34 | #include <ndn-cpp-dev/util/scheduler.hpp> |
| 35 | |
| 36 | #include "sync-interest-table.h" |
| 37 | #include "sync-diff-state.h" |
| 38 | #include "sync-full-state.h" |
| 39 | #include "sync-std-name-info.h" |
| 40 | |
| 41 | #include "sync-diff-state-container.h" |
| 42 | |
| 43 | #ifdef _DEBUG |
| 44 | #ifdef HAVE_LOG4CXX |
| 45 | #include <log4cxx/logger.h> |
| 46 | #endif |
| 47 | #endif |
| 48 | |
| 49 | namespace Sync { |
| 50 | |
| 51 | struct MissingDataInfo { |
| 52 | std::string prefix; |
| 53 | SeqNo low; |
| 54 | SeqNo high; |
| 55 | }; |
| 56 | |
| 57 | /** |
| 58 | * \ingroup sync |
| 59 | * @brief A wrapper for SyncApp, which handles ccnx related things (process |
| 60 | * interests and data) |
| 61 | */ |
| 62 | |
| 63 | class SyncLogic |
| 64 | { |
| 65 | public: |
| 66 | //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; |
| 68 | typedef boost::function< void (const std::string &/*prefix*/ ) > LogicRemoveCallback; |
| 69 | typedef boost::function< void (const std::string &)> LogicPerBranchCallback; |
| 70 | |
| 71 | /** |
| 72 | * @brief Constructor |
| 73 | * @param syncPrefix the name prefix to use for the Sync Interest |
| 74 | * @param onUpdate function that will be called when new state is detected |
| 75 | * @param onRemove function that will be called when state is removed |
| 76 | * @param ccnxHandle ccnx handle |
| 77 | * the app data when new remote names are learned |
| 78 | */ |
| 79 | SyncLogic (const ndn::Name& syncPrefix, |
| 80 | ndn::shared_ptr<ndn::Validator> validator, |
| 81 | ndn::shared_ptr<ndn::Face> face, |
| 82 | LogicUpdateCallback onUpdate, |
| 83 | LogicRemoveCallback onRemove); |
| 84 | |
| 85 | SyncLogic (const ndn::Name& syncPrefix, |
| 86 | ndn::shared_ptr<ndn::Validator> validator, |
| 87 | ndn::shared_ptr<ndn::Face> face, |
| 88 | LogicPerBranchCallback onUpdateBranch); |
| 89 | |
| 90 | ~SyncLogic (); |
| 91 | |
| 92 | /** |
| 93 | * a wrapper for the same func in SyncApp |
| 94 | */ |
| 95 | void addLocalNames (const ndn::Name &prefix, uint64_t session, uint64_t seq); |
| 96 | |
| 97 | /** |
| 98 | * @brief remove a participant's subtree from the sync tree |
| 99 | * @param prefix the name prefix for the participant |
| 100 | */ |
| 101 | void remove (const ndn::Name &prefix); |
| 102 | |
| 103 | std::string |
| 104 | getRootDigest(); |
| 105 | |
| 106 | #ifdef _DEBUG |
| 107 | ndn::Scheduler & |
| 108 | getScheduler () { return m_scheduler; } |
| 109 | #endif |
| 110 | |
| 111 | void |
| 112 | printState () const; |
| 113 | |
| 114 | std::map<std::string, bool> |
| 115 | getBranchPrefixes() const; |
| 116 | |
Yingdi Yu | 40cd1c3 | 2014-04-17 15:02:17 -0700 | [diff] [blame^] | 117 | private: |
akmhoque | 66e6618 | 2014-02-21 17:56:03 -0600 | [diff] [blame] | 118 | void |
| 119 | delayedChecksLoop (); |
| 120 | |
| 121 | void |
| 122 | onSyncInterest (const ndn::Name& prefix, const ndn::Interest& interest); |
| 123 | |
| 124 | void |
| 125 | onSyncRegisterFailed(const ndn::Name& prefix, const std::string& msg); |
| 126 | |
| 127 | void |
| 128 | onSyncData(const ndn::Interest& interest, ndn::Data& data); |
| 129 | |
| 130 | void |
| 131 | onSyncTimeout(const ndn::Interest& interest); |
| 132 | |
| 133 | void |
| 134 | onSyncDataValidationFailed(const ndn::shared_ptr<const ndn::Data>& data); |
| 135 | |
| 136 | void |
| 137 | onSyncDataValidated(const ndn::shared_ptr<const ndn::Data>& data); |
| 138 | |
| 139 | void |
| 140 | processSyncInterest (const ndn::Name &name, |
| 141 | DigestConstPtr digest, bool timedProcessing=false); |
| 142 | |
| 143 | void |
| 144 | processSyncData (const ndn::Name &name, |
| 145 | DigestConstPtr digest, const char *wireData, size_t len); |
Yingdi Yu | 40cd1c3 | 2014-04-17 15:02:17 -0700 | [diff] [blame^] | 146 | |
akmhoque | 66e6618 | 2014-02-21 17:56:03 -0600 | [diff] [blame] | 147 | void |
| 148 | processSyncRecoveryInterest (const ndn::Name &name, |
| 149 | DigestConstPtr digest); |
Yingdi Yu | 40cd1c3 | 2014-04-17 15:02:17 -0700 | [diff] [blame^] | 150 | |
| 151 | void |
akmhoque | 66e6618 | 2014-02-21 17:56:03 -0600 | [diff] [blame] | 152 | insertToDiffLog (DiffStatePtr diff); |
| 153 | |
| 154 | void |
| 155 | satisfyPendingSyncInterests (DiffStateConstPtr diff); |
| 156 | |
| 157 | boost::tuple<DigestConstPtr, std::string> |
| 158 | convertNameToDigestAndType (const ndn::Name &name); |
| 159 | |
| 160 | void |
| 161 | sendSyncInterest (); |
| 162 | |
| 163 | void |
| 164 | sendSyncRecoveryInterests (DigestConstPtr digest); |
| 165 | |
| 166 | void |
| 167 | sendSyncData (const ndn::Name &name, |
| 168 | DigestConstPtr digest, StateConstPtr state); |
| 169 | |
| 170 | void |
| 171 | sendSyncData (const ndn::Name &name, |
| 172 | DigestConstPtr digest, SyncStateMsg &msg); |
| 173 | |
| 174 | size_t |
| 175 | getNumberOfBranches () const; |
Yingdi Yu | 40cd1c3 | 2014-04-17 15:02:17 -0700 | [diff] [blame^] | 176 | |
akmhoque | 66e6618 | 2014-02-21 17:56:03 -0600 | [diff] [blame] | 177 | private: |
| 178 | FullStatePtr m_state; |
Yingdi Yu | 40cd1c3 | 2014-04-17 15:02:17 -0700 | [diff] [blame^] | 179 | |
akmhoque | 66e6618 | 2014-02-21 17:56:03 -0600 | [diff] [blame] | 180 | DiffStateContainer m_log; |
| 181 | |
| 182 | ndn::Name m_outstandingInterestName; |
| 183 | SyncInterestTable m_syncInterestTable; |
| 184 | |
| 185 | ndn::Name m_syncPrefix; |
| 186 | LogicUpdateCallback m_onUpdate; |
| 187 | LogicRemoveCallback m_onRemove; |
| 188 | LogicPerBranchCallback m_onUpdateBranch; |
| 189 | bool m_perBranch; |
| 190 | ndn::ptr_lib::shared_ptr<ndn::Validator> m_validator; |
| 191 | ndn::ptr_lib::shared_ptr<ndn::KeyChain> m_keyChain; |
| 192 | ndn::ptr_lib::shared_ptr<ndn::Face> m_face; |
| 193 | const ndn::RegisteredPrefixId* m_syncRegisteredPrefixId; |
| 194 | |
| 195 | ndn::Scheduler m_scheduler; |
| 196 | |
| 197 | boost::mt19937 m_randomGenerator; |
| 198 | boost::variate_generator<boost::mt19937&, boost::uniform_int<> > m_rangeUniformRandom; |
| 199 | boost::variate_generator<boost::mt19937&, boost::uniform_int<> > m_reexpressionJitter; |
| 200 | |
akmhoque | 66e6618 | 2014-02-21 17:56:03 -0600 | [diff] [blame] | 201 | |
Yingdi Yu | 40cd1c3 | 2014-04-17 15:02:17 -0700 | [diff] [blame^] | 202 | static const int m_unknownDigestStoreTime = 10; // seconds |
| 203 | static const int m_syncResponseFreshness; // MUST BE dividable by 1000!!! |
| 204 | static const int m_syncInterestReexpress; // seconds |
akmhoque | 66e6618 | 2014-02-21 17:56:03 -0600 | [diff] [blame] | 205 | static const int m_defaultRecoveryRetransmitInterval = 200; // milliseconds |
Yingdi Yu | 40cd1c3 | 2014-04-17 15:02:17 -0700 | [diff] [blame^] | 206 | |
| 207 | |
akmhoque | 66e6618 | 2014-02-21 17:56:03 -0600 | [diff] [blame] | 208 | uint32_t m_recoveryRetransmissionInterval; // milliseconds |
Yingdi Yu | 40cd1c3 | 2014-04-17 15:02:17 -0700 | [diff] [blame^] | 209 | |
akmhoque | 66e6618 | 2014-02-21 17:56:03 -0600 | [diff] [blame] | 210 | ndn::EventId m_delayedInterestProcessingId; |
| 211 | ndn::EventId m_reexpressingInterestId; |
| 212 | ndn::EventId m_reexpressingRecoveryInterestId; |
Yingdi Yu | 40cd1c3 | 2014-04-17 15:02:17 -0700 | [diff] [blame^] | 213 | |
akmhoque | 66e6618 | 2014-02-21 17:56:03 -0600 | [diff] [blame] | 214 | std::string m_instanceId; |
| 215 | static int m_instanceCounter; |
| 216 | }; |
| 217 | |
| 218 | |
| 219 | } // Sync |
| 220 | |
| 221 | #endif // SYNC_APP_WRAPPER_H |