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 | |
| 117 | private: |
| 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); |
| 146 | |
| 147 | void |
| 148 | processSyncRecoveryInterest (const ndn::Name &name, |
| 149 | DigestConstPtr digest); |
| 150 | |
| 151 | void |
| 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; |
| 176 | |
| 177 | private: |
| 178 | FullStatePtr m_state; |
akmhoque | 05d5fcf | 2014-04-15 14:58:45 -0500 | [diff] [blame^] | 179 | |
| 180 | int m_unknownDigestStoreTime; |
| 181 | int m_syncResponseFreshness; |
| 182 | int m_syncInterestReexpress; |
| 183 | int m_defaultRecoveryRetransmitInterval; |
| 184 | |
akmhoque | 66e6618 | 2014-02-21 17:56:03 -0600 | [diff] [blame] | 185 | DiffStateContainer m_log; |
| 186 | |
| 187 | ndn::Name m_outstandingInterestName; |
| 188 | SyncInterestTable m_syncInterestTable; |
| 189 | |
| 190 | ndn::Name m_syncPrefix; |
| 191 | LogicUpdateCallback m_onUpdate; |
| 192 | LogicRemoveCallback m_onRemove; |
| 193 | LogicPerBranchCallback m_onUpdateBranch; |
| 194 | bool m_perBranch; |
| 195 | ndn::ptr_lib::shared_ptr<ndn::Validator> m_validator; |
| 196 | ndn::ptr_lib::shared_ptr<ndn::KeyChain> m_keyChain; |
| 197 | ndn::ptr_lib::shared_ptr<ndn::Face> m_face; |
| 198 | const ndn::RegisteredPrefixId* m_syncRegisteredPrefixId; |
| 199 | |
| 200 | ndn::Scheduler m_scheduler; |
| 201 | |
| 202 | boost::mt19937 m_randomGenerator; |
| 203 | boost::variate_generator<boost::mt19937&, boost::uniform_int<> > m_rangeUniformRandom; |
| 204 | boost::variate_generator<boost::mt19937&, boost::uniform_int<> > m_reexpressionJitter; |
| 205 | |
akmhoque | 05d5fcf | 2014-04-15 14:58:45 -0500 | [diff] [blame^] | 206 | /* |
akmhoque | 66e6618 | 2014-02-21 17:56:03 -0600 | [diff] [blame] | 207 | static const int m_unknownDigestStoreTime = 10; // seconds |
| 208 | static const int m_syncResponseFreshness = 1000; // MUST BE dividable by 1000!!! |
| 209 | static const int m_syncInterestReexpress = 4; // seconds |
| 210 | |
| 211 | static const int m_defaultRecoveryRetransmitInterval = 200; // milliseconds |
akmhoque | 05d5fcf | 2014-04-15 14:58:45 -0500 | [diff] [blame^] | 212 | */ |
| 213 | |
akmhoque | 66e6618 | 2014-02-21 17:56:03 -0600 | [diff] [blame] | 214 | uint32_t m_recoveryRetransmissionInterval; // milliseconds |
| 215 | |
| 216 | ndn::EventId m_delayedInterestProcessingId; |
| 217 | ndn::EventId m_reexpressingInterestId; |
| 218 | ndn::EventId m_reexpressingRecoveryInterestId; |
| 219 | |
| 220 | std::string m_instanceId; |
| 221 | static int m_instanceCounter; |
| 222 | }; |
| 223 | |
| 224 | |
| 225 | } // Sync |
| 226 | |
| 227 | #endif // SYNC_APP_WRAPPER_H |