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 | |
akmhoque | c8a10f7 | 2014-04-25 18:42:55 -0500 | [diff] [blame] | 31 | #include <ndn-cxx/face.hpp> |
| 32 | #include <ndn-cxx/security/validator.hpp> |
| 33 | #include <ndn-cxx/security/key-chain.hpp> |
| 34 | #include <ndn-cxx/util/scheduler.hpp> |
akmhoque | 66e6618 | 2014-02-21 17:56:03 -0600 | [diff] [blame] | 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: |
Vince Lehman | 0a7da61 | 2014-10-29 14:39:29 -0500 | [diff] [blame] | 66 | typedef function< void (const std::vector<MissingDataInfo> & ) > LogicUpdateCallback; |
| 67 | typedef function< void (const std::string &/*prefix*/ ) > LogicRemoveCallback; |
| 68 | typedef function< void (const std::string &)> LogicPerBranchCallback; |
akmhoque | 66e6618 | 2014-02-21 17:56:03 -0600 | [diff] [blame] | 69 | |
| 70 | /** |
| 71 | * @brief Constructor |
| 72 | * @param syncPrefix the name prefix to use for the Sync Interest |
| 73 | * @param onUpdate function that will be called when new state is detected |
| 74 | * @param onRemove function that will be called when state is removed |
| 75 | * @param ccnxHandle ccnx handle |
| 76 | * the app data when new remote names are learned |
| 77 | */ |
| 78 | SyncLogic (const ndn::Name& syncPrefix, |
| 79 | ndn::shared_ptr<ndn::Validator> validator, |
| 80 | ndn::shared_ptr<ndn::Face> face, |
| 81 | LogicUpdateCallback onUpdate, |
| 82 | LogicRemoveCallback onRemove); |
| 83 | |
| 84 | SyncLogic (const ndn::Name& syncPrefix, |
| 85 | ndn::shared_ptr<ndn::Validator> validator, |
| 86 | ndn::shared_ptr<ndn::Face> face, |
| 87 | LogicPerBranchCallback onUpdateBranch); |
| 88 | |
| 89 | ~SyncLogic (); |
| 90 | |
| 91 | /** |
| 92 | * a wrapper for the same func in SyncApp |
| 93 | */ |
| 94 | void addLocalNames (const ndn::Name &prefix, uint64_t session, uint64_t seq); |
| 95 | |
| 96 | /** |
| 97 | * @brief remove a participant's subtree from the sync tree |
| 98 | * @param prefix the name prefix for the participant |
| 99 | */ |
| 100 | void remove (const ndn::Name &prefix); |
| 101 | |
| 102 | std::string |
| 103 | getRootDigest(); |
| 104 | |
| 105 | #ifdef _DEBUG |
| 106 | ndn::Scheduler & |
| 107 | getScheduler () { return m_scheduler; } |
| 108 | #endif |
| 109 | |
| 110 | void |
| 111 | printState () const; |
| 112 | |
| 113 | std::map<std::string, bool> |
| 114 | getBranchPrefixes() const; |
| 115 | |
Yingdi Yu | 40cd1c3 | 2014-04-17 15:02:17 -0700 | [diff] [blame] | 116 | private: |
akmhoque | 66e6618 | 2014-02-21 17:56:03 -0600 | [diff] [blame] | 117 | void |
| 118 | delayedChecksLoop (); |
| 119 | |
| 120 | void |
| 121 | onSyncInterest (const ndn::Name& prefix, const ndn::Interest& interest); |
| 122 | |
| 123 | void |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 124 | onSyncRegisterSucceed(const ndn::Name& prefix); |
| 125 | |
| 126 | void |
akmhoque | 66e6618 | 2014-02-21 17:56:03 -0600 | [diff] [blame] | 127 | onSyncRegisterFailed(const ndn::Name& prefix, const std::string& msg); |
| 128 | |
| 129 | void |
Alexander Afanasyev | 1de901f | 2017-03-09 12:43:57 -0800 | [diff] [blame^] | 130 | onSyncData(const ndn::Interest& interest, const ndn::Data& data); |
akmhoque | 66e6618 | 2014-02-21 17:56:03 -0600 | [diff] [blame] | 131 | |
| 132 | void |
| 133 | onSyncTimeout(const ndn::Interest& interest); |
| 134 | |
| 135 | void |
| 136 | onSyncDataValidationFailed(const ndn::shared_ptr<const ndn::Data>& data); |
| 137 | |
| 138 | void |
| 139 | onSyncDataValidated(const ndn::shared_ptr<const ndn::Data>& data); |
| 140 | |
| 141 | void |
| 142 | processSyncInterest (const ndn::Name &name, |
| 143 | DigestConstPtr digest, bool timedProcessing=false); |
| 144 | |
| 145 | void |
| 146 | processSyncData (const ndn::Name &name, |
| 147 | DigestConstPtr digest, const char *wireData, size_t len); |
Yingdi Yu | 40cd1c3 | 2014-04-17 15:02:17 -0700 | [diff] [blame] | 148 | |
akmhoque | 66e6618 | 2014-02-21 17:56:03 -0600 | [diff] [blame] | 149 | void |
| 150 | processSyncRecoveryInterest (const ndn::Name &name, |
| 151 | DigestConstPtr digest); |
Yingdi Yu | 40cd1c3 | 2014-04-17 15:02:17 -0700 | [diff] [blame] | 152 | |
| 153 | void |
akmhoque | 66e6618 | 2014-02-21 17:56:03 -0600 | [diff] [blame] | 154 | insertToDiffLog (DiffStatePtr diff); |
| 155 | |
| 156 | void |
| 157 | satisfyPendingSyncInterests (DiffStateConstPtr diff); |
| 158 | |
Vince Lehman | 0a7da61 | 2014-10-29 14:39:29 -0500 | [diff] [blame] | 159 | tuple<DigestConstPtr, std::string> |
akmhoque | 66e6618 | 2014-02-21 17:56:03 -0600 | [diff] [blame] | 160 | convertNameToDigestAndType (const ndn::Name &name); |
| 161 | |
| 162 | void |
| 163 | sendSyncInterest (); |
| 164 | |
| 165 | void |
| 166 | sendSyncRecoveryInterests (DigestConstPtr digest); |
| 167 | |
| 168 | void |
| 169 | sendSyncData (const ndn::Name &name, |
| 170 | DigestConstPtr digest, StateConstPtr state); |
| 171 | |
| 172 | void |
| 173 | sendSyncData (const ndn::Name &name, |
| 174 | DigestConstPtr digest, SyncStateMsg &msg); |
| 175 | |
| 176 | size_t |
| 177 | getNumberOfBranches () const; |
Yingdi Yu | 40cd1c3 | 2014-04-17 15:02:17 -0700 | [diff] [blame] | 178 | |
akmhoque | 66e6618 | 2014-02-21 17:56:03 -0600 | [diff] [blame] | 179 | private: |
| 180 | FullStatePtr m_state; |
Yingdi Yu | 40cd1c3 | 2014-04-17 15:02:17 -0700 | [diff] [blame] | 181 | |
akmhoque | 66e6618 | 2014-02-21 17:56:03 -0600 | [diff] [blame] | 182 | DiffStateContainer m_log; |
| 183 | |
| 184 | ndn::Name m_outstandingInterestName; |
| 185 | SyncInterestTable m_syncInterestTable; |
| 186 | |
| 187 | ndn::Name m_syncPrefix; |
| 188 | LogicUpdateCallback m_onUpdate; |
| 189 | LogicRemoveCallback m_onRemove; |
| 190 | LogicPerBranchCallback m_onUpdateBranch; |
| 191 | bool m_perBranch; |
Spyridon Mastorakis | c00650f | 2015-02-22 23:00:08 -0800 | [diff] [blame] | 192 | std::shared_ptr<ndn::Validator> m_validator; |
| 193 | std::shared_ptr<ndn::KeyChain> m_keyChain; |
| 194 | std::shared_ptr<ndn::Face> m_face; |
akmhoque | 66e6618 | 2014-02-21 17:56:03 -0600 | [diff] [blame] | 195 | const ndn::RegisteredPrefixId* m_syncRegisteredPrefixId; |
| 196 | |
| 197 | ndn::Scheduler m_scheduler; |
| 198 | |
| 199 | boost::mt19937 m_randomGenerator; |
| 200 | boost::variate_generator<boost::mt19937&, boost::uniform_int<> > m_rangeUniformRandom; |
| 201 | boost::variate_generator<boost::mt19937&, boost::uniform_int<> > m_reexpressionJitter; |
| 202 | |
akmhoque | 66e6618 | 2014-02-21 17:56:03 -0600 | [diff] [blame] | 203 | |
Yingdi Yu | 40cd1c3 | 2014-04-17 15:02:17 -0700 | [diff] [blame] | 204 | static const int m_unknownDigestStoreTime = 10; // seconds |
| 205 | static const int m_syncResponseFreshness; // MUST BE dividable by 1000!!! |
| 206 | static const int m_syncInterestReexpress; // seconds |
akmhoque | 66e6618 | 2014-02-21 17:56:03 -0600 | [diff] [blame] | 207 | static const int m_defaultRecoveryRetransmitInterval = 200; // milliseconds |
Yingdi Yu | 40cd1c3 | 2014-04-17 15:02:17 -0700 | [diff] [blame] | 208 | |
| 209 | |
akmhoque | 66e6618 | 2014-02-21 17:56:03 -0600 | [diff] [blame] | 210 | uint32_t m_recoveryRetransmissionInterval; // milliseconds |
Yingdi Yu | 40cd1c3 | 2014-04-17 15:02:17 -0700 | [diff] [blame] | 211 | |
akmhoque | 66e6618 | 2014-02-21 17:56:03 -0600 | [diff] [blame] | 212 | ndn::EventId m_delayedInterestProcessingId; |
| 213 | ndn::EventId m_reexpressingInterestId; |
| 214 | ndn::EventId m_reexpressingRecoveryInterestId; |
Yingdi Yu | 40cd1c3 | 2014-04-17 15:02:17 -0700 | [diff] [blame] | 215 | |
akmhoque | 66e6618 | 2014-02-21 17:56:03 -0600 | [diff] [blame] | 216 | std::string m_instanceId; |
| 217 | static int m_instanceCounter; |
| 218 | }; |
| 219 | |
| 220 | |
| 221 | } // Sync |
| 222 | |
| 223 | #endif // SYNC_APP_WRAPPER_H |