blob: 181979b989a6a0a238d8b2595d4e863d2d33c206 [file] [log] [blame]
Zhenkai Zhu8d935c82012-03-06 10:44:12 -08001/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
2/*
Alexander Afanasyev8722d872014-07-02 13:00:29 -07003 * Copyright (c) 2012-2014 University of California, Los Angeles
Zhenkai Zhu8d935c82012-03-06 10:44:12 -08004 *
Alexander Afanasyev8722d872014-07-02 13:00:29 -07005 * This file is part of ChronoSync, synchronization library for distributed realtime
6 * applications for NDN.
Zhenkai Zhu8d935c82012-03-06 10:44:12 -08007 *
Alexander Afanasyev8722d872014-07-02 13:00:29 -07008 * ChronoSync is free software: you can redistribute it and/or modify it under the terms
9 * of the GNU General Public License as published by the Free Software Foundation, either
10 * version 3 of the License, or (at your option) any later version.
Zhenkai Zhu8d935c82012-03-06 10:44:12 -080011 *
Alexander Afanasyev8722d872014-07-02 13:00:29 -070012 * ChronoSync is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
13 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
Zhenkai Zhu8d935c82012-03-06 10:44:12 -080015 *
Alexander Afanasyev8722d872014-07-02 13:00:29 -070016 * You should have received a copy of the GNU General Public License along with
17 * ChronoSync, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
18 *
19 * @author Zhenkai Zhu <http://irl.cs.ucla.edu/~zhenkai/>
20 * @author Chaoyi Bian <bcy@pku.edu.cn>
21 * @author Alexander Afanasyev <http://lasr.cs.ucla.edu/afanasyev/index.html>
22 * @author Yingdi Yu <http://irl.cs.ucla.edu/~yingdi/web/index.html>
Zhenkai Zhu8d935c82012-03-06 10:44:12 -080023 */
24
Chaoyi Bian11f294f2012-03-08 14:28:06 -080025#ifndef SYNC_LOGIC_H
26#define SYNC_LOGIC_H
Alexander Afanasyev45fba082012-03-12 18:05:24 -070027
Yingdi Yu06a678a2014-08-01 17:07:08 -070028#include "boost-header.h"
Zhenkai Zhuce66e212012-03-12 22:27:19 -070029#include <memory>
Zhenkai Zhud5aec4b2012-10-09 12:01:19 -070030#include <map>
Alexander Afanasyev387ac952012-03-11 23:49:27 -070031
Yingdi Yu7c64e5c2014-04-30 14:06:37 -070032#include <ndn-cxx/face.hpp>
33#include <ndn-cxx/security/validator.hpp>
34#include <ndn-cxx/security/key-chain.hpp>
35#include <ndn-cxx/util/scheduler.hpp>
Yingdi Yu46c9f1a2013-12-18 15:15:46 +080036
Zhenkai Zhu8d935c82012-03-06 10:44:12 -080037#include "sync-interest-table.h"
Zhenkai Zhu8d935c82012-03-06 10:44:12 -080038#include "sync-diff-state.h"
39#include "sync-full-state.h"
Chaoyi Bian4194b742012-03-08 17:21:35 -080040#include "sync-std-name-info.h"
Zhenkai Zhu8d935c82012-03-06 10:44:12 -080041
Alexander Afanasyevc1030192012-03-08 22:21:28 -080042#include "sync-diff-state-container.h"
43
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -070044#ifdef _DEBUG
45#ifdef HAVE_LOG4CXX
46#include <log4cxx/logger.h>
47#endif
48#endif
49
Zhenkai Zhu8d935c82012-03-06 10:44:12 -080050namespace Sync {
51
Zhenkai Zhu1cb29292012-05-31 22:54:34 -070052struct MissingDataInfo {
53 std::string prefix;
54 SeqNo low;
55 SeqNo high;
56};
57
Zhenkai Zhu8d935c82012-03-06 10:44:12 -080058/**
59 * \ingroup sync
Zhenkai Zhuaae81522012-03-06 11:05:44 -080060 * @brief A wrapper for SyncApp, which handles ccnx related things (process
61 * interests and data)
Zhenkai Zhu8d935c82012-03-06 10:44:12 -080062 */
Zhenkai Zhub9f19592012-10-16 14:27:38 -070063
Chaoyi Bian11f294f2012-03-08 14:28:06 -080064class SyncLogic
Zhenkai Zhu8d935c82012-03-06 10:44:12 -080065{
66public:
Zhenkai Zhu1cb29292012-05-31 22:54:34 -070067 //typedef boost::function< void ( const std::string &/*prefix*/, const SeqNo &/*newSeq*/, const SeqNo &/*oldSeq*/ ) > LogicUpdateCallback;
68 typedef boost::function< void (const std::vector<MissingDataInfo> & ) > LogicUpdateCallback;
Alexander Afanasyevd95c2312013-11-07 13:45:34 -080069 typedef boost::function< void (const std::string &/*prefix*/ ) > LogicRemoveCallback;
Zhenkai Zhub9f19592012-10-16 14:27:38 -070070 typedef boost::function< void (const std::string &)> LogicPerBranchCallback;
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -080071
Yingdi Yu43e71612013-10-30 22:19:31 -070072 SyncLogic (const ndn::Name& syncPrefix,
Yingdi Yu3da10fe2014-02-27 16:37:34 -080073 const ndn::IdentityCertificate& myCertificate,
Yingdi Yu280bb962014-01-30 09:52:43 -080074 ndn::shared_ptr<ndn::Validator> validator,
75 ndn::shared_ptr<ndn::Face> face,
Alexander Afanasyev750d1872012-03-12 15:33:56 -070076 LogicUpdateCallback onUpdate,
Zhenkai Zhuce66e212012-03-12 22:27:19 -070077 LogicRemoveCallback onRemove);
Zhenkai Zhu46b26a12012-03-06 13:51:16 -080078
Yingdi Yu43e71612013-10-30 22:19:31 -070079 SyncLogic (const ndn::Name& syncPrefix,
Yingdi Yu3da10fe2014-02-27 16:37:34 -080080 const ndn::IdentityCertificate& myCertificate,
Yingdi Yu280bb962014-01-30 09:52:43 -080081 ndn::shared_ptr<ndn::Validator> validator,
82 ndn::shared_ptr<ndn::Face> face,
Zhenkai Zhub9f19592012-10-16 14:27:38 -070083 LogicPerBranchCallback onUpdateBranch);
84
Alexander Afanasyevc1030192012-03-08 22:21:28 -080085 ~SyncLogic ();
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -080086
Alexander Afanasyevc1030192012-03-08 22:21:28 -080087 /**
88 * a wrapper for the same func in SyncApp
89 */
Yingdi Yu280bb962014-01-30 09:52:43 -080090 void addLocalNames (const ndn::Name &prefix, uint64_t session, uint64_t seq);
Zhenkai Zhu46b26a12012-03-06 13:51:16 -080091
Alexander Afanasyevc1030192012-03-08 22:21:28 -080092 /**
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -070093 * @brief remove a participant's subtree from the sync tree
Alexander Afanasyev03a58b72012-03-12 18:11:56 -070094 * @param prefix the name prefix for the participant
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -070095 */
Yingdi Yu6d638f02014-01-24 11:01:21 -080096 void remove (const ndn::Name &prefix);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -070097
Zhenkai Zhue5660932012-06-04 15:25:20 -070098 std::string
99 getRootDigest();
100
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700101#ifdef _DEBUG
Yingdi Yu280bb962014-01-30 09:52:43 -0800102 ndn::Scheduler &
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -0700103 getScheduler () { return m_scheduler; }
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700104#endif
Zhenkai Zhueb1d8652012-12-23 15:25:39 -0800105
Alexander Afanasyev5548c042012-10-04 19:10:09 -0700106 void
107 printState () const;
108
Zhenkai Zhud5aec4b2012-10-09 12:01:19 -0700109 std::map<std::string, bool>
110 getBranchPrefixes() const;
111
Yingdi Yu7c64e5c2014-04-30 14:06:37 -0700112private:
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800113 void
Alexander Afanasyev45fba082012-03-12 18:05:24 -0700114 delayedChecksLoop ();
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700115
116 void
Yingdi Yu51c80252014-02-10 19:32:05 -0800117 onSyncInterest (const ndn::Name& prefix, const ndn::Interest& interest);
Yingdi Yu43e71612013-10-30 22:19:31 -0700118
119 void
Yingdi Yu51c80252014-02-10 19:32:05 -0800120 onSyncRegisterFailed(const ndn::Name& prefix, const std::string& msg);
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800121
122 void
Yingdi Yu51c80252014-02-10 19:32:05 -0800123 onSyncData(const ndn::Interest& interest, ndn::Data& data);
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800124
125 void
Yingdi Yu51c80252014-02-10 19:32:05 -0800126 onSyncTimeout(const ndn::Interest& interest);
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800127
128 void
Yingdi Yu280bb962014-01-30 09:52:43 -0800129 onSyncDataValidationFailed(const ndn::shared_ptr<const ndn::Data>& data);
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800130
131 void
Yingdi Yu280bb962014-01-30 09:52:43 -0800132 onSyncDataValidated(const ndn::shared_ptr<const ndn::Data>& data);
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800133
134 void
Yingdi Yu6d638f02014-01-24 11:01:21 -0800135 processSyncInterest (const ndn::Name &name,
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700136 DigestConstPtr digest, bool timedProcessing=false);
137
138 void
Yingdi Yu6d638f02014-01-24 11:01:21 -0800139 processSyncData (const ndn::Name &name,
Zhenkai Zhu3cfdcb92012-06-06 15:20:10 -0700140 DigestConstPtr digest, const char *wireData, size_t len);
Yingdi Yu7c64e5c2014-04-30 14:06:37 -0700141
Alexander Afanasyev45fba082012-03-12 18:05:24 -0700142 void
Yingdi Yu6d638f02014-01-24 11:01:21 -0800143 processSyncRecoveryInterest (const ndn::Name &name,
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700144 DigestConstPtr digest);
Yingdi Yu7c64e5c2014-04-30 14:06:37 -0700145
146 void
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700147 insertToDiffLog (DiffStatePtr diff);
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700148
149 void
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700150 satisfyPendingSyncInterests (DiffStateConstPtr diff);
Zhenkai Zhu8d935c82012-03-06 10:44:12 -0800151
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700152 boost::tuple<DigestConstPtr, std::string>
Yingdi Yu6d638f02014-01-24 11:01:21 -0800153 convertNameToDigestAndType (const ndn::Name &name);
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700154
155 void
156 sendSyncInterest ();
157
158 void
Alexander Afanasyev46eb5262012-05-10 16:30:35 -0700159 sendSyncRecoveryInterests (DigestConstPtr digest);
160
161 void
Yingdi Yu6d638f02014-01-24 11:01:21 -0800162 sendSyncData (const ndn::Name &name,
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700163 DigestConstPtr digest, StateConstPtr state);
Alexander Afanasyev80237382012-10-04 10:20:47 -0700164
Zhenkai Zhu2d3e2702012-10-15 14:18:05 -0700165 void
Yingdi Yu6d638f02014-01-24 11:01:21 -0800166 sendSyncData (const ndn::Name &name,
Zhenkai Zhu2d3e2702012-10-15 14:18:05 -0700167 DigestConstPtr digest, SyncStateMsg &msg);
168
Alexander Afanasyev80237382012-10-04 10:20:47 -0700169 size_t
170 getNumberOfBranches () const;
Yingdi Yu7c64e5c2014-04-30 14:06:37 -0700171
Zhenkai Zhu8d935c82012-03-06 10:44:12 -0800172private:
Alexander Afanasyevf3c03a92012-05-09 12:00:37 -0700173 FullStatePtr m_state;
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800174 DiffStateContainer m_log;
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700175
Yingdi Yu6d638f02014-01-24 11:01:21 -0800176 ndn::Name m_outstandingInterestName;
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800177 SyncInterestTable m_syncInterestTable;
178
Yingdi Yu43e71612013-10-30 22:19:31 -0700179 ndn::Name m_syncPrefix;
Yingdi Yu3da10fe2014-02-27 16:37:34 -0800180 ndn::IdentityCertificate m_myCertificate;
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700181 LogicUpdateCallback m_onUpdate;
182 LogicRemoveCallback m_onRemove;
Zhenkai Zhub9f19592012-10-16 14:27:38 -0700183 LogicPerBranchCallback m_onUpdateBranch;
184 bool m_perBranch;
Yingdi Yu3da10fe2014-02-27 16:37:34 -0800185 ndn::shared_ptr<ndn::Validator> m_validator;
186 ndn::KeyChain m_keyChain;
187 ndn::shared_ptr<ndn::Face> m_face;
Yingdi Yu280bb962014-01-30 09:52:43 -0800188 const ndn::RegisteredPrefixId* m_syncRegisteredPrefixId;
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800189
Yingdi Yu280bb962014-01-30 09:52:43 -0800190 ndn::Scheduler m_scheduler;
Chaoyi Bian633255f2012-03-09 21:25:38 -0800191
Alexander Afanasyev45fba082012-03-12 18:05:24 -0700192 boost::mt19937 m_randomGenerator;
193 boost::variate_generator<boost::mt19937&, boost::uniform_int<> > m_rangeUniformRandom;
Alexander Afanasyev085742a2012-04-26 12:24:57 -0700194 boost::variate_generator<boost::mt19937&, boost::uniform_int<> > m_reexpressionJitter;
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -0700195
Alexander Afanasyev3a229132012-04-25 15:07:26 -0700196 static const int m_unknownDigestStoreTime = 10; // seconds
Yingdi Yu6e1c9cd2014-03-25 10:26:54 -0700197 static const int m_syncResponseFreshness; // MUST BE dividable by 1000!!!
198 static const int m_syncInterestReexpress; // seconds
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -0700199
Alexander Afanasyeva76010b2012-05-24 21:31:49 -0700200 static const int m_defaultRecoveryRetransmitInterval = 200; // milliseconds
201 uint32_t m_recoveryRetransmissionInterval; // milliseconds
Yingdi Yu7c64e5c2014-04-30 14:06:37 -0700202
Yingdi Yu280bb962014-01-30 09:52:43 -0800203 ndn::EventId m_delayedInterestProcessingId;
204 ndn::EventId m_reexpressingInterestId;
205 ndn::EventId m_reexpressingRecoveryInterestId;
Yingdi Yu7c64e5c2014-04-30 14:06:37 -0700206
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800207 std::string m_instanceId;
208 static int m_instanceCounter;
Zhenkai Zhu8d935c82012-03-06 10:44:12 -0800209};
210
211
212} // Sync
213
Zhenkai Zhu1ac6f802012-03-06 17:40:27 -0800214#endif // SYNC_APP_WRAPPER_H