blob: ab0b79475892a2225cdbc3a34f04941ec80dfac4 [file] [log] [blame]
Yingdi Yuf7ede412014-08-30 20:37:52 -07001/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
2/*
Ashlesh Gawande04e8d492018-02-04 13:08:15 -06003 * Copyright (c) 2012-2018 University of California, Los Angeles
Yingdi Yuf7ede412014-08-30 20:37:52 -07004 *
5 * This file is part of ChronoSync, synchronization library for distributed realtime
6 * applications for NDN.
7 *
8 * 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.
11 *
12 * 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.
15 *
16 * 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 <yingdi@cs.ucla.edu>
Sonu Mishra0dadc572016-12-12 23:59:41 -080023 * @author Sonu Mishra <https://www.linkedin.com/in/mishrasonu>
Yingdi Yuf7ede412014-08-30 20:37:52 -070024 */
25
26#include "logic.hpp"
27#include "logger.hpp"
Alexander Afanasyev6ee98ff2018-02-13 19:12:28 -050028#include "bzip2-helper.hpp"
Yingdi Yuf7ede412014-08-30 20:37:52 -070029
Alexander Afanasyev89036292018-02-13 17:19:50 -050030#include <ndn-cxx/util/backports.hpp>
Ashlesh Gawande687cf922017-05-30 15:04:16 -050031#include <ndn-cxx/util/string-helper.hpp>
32
Alexander Afanasyev36eb3ed2017-01-11 12:35:58 -080033INIT_LOGGER(Logic);
Yingdi Yuf7ede412014-08-30 20:37:52 -070034
Yingdi Yuf7ede412014-08-30 20:37:52 -070035#define _LOG_DEBUG_ID(v) _LOG_DEBUG("Instance" << m_instanceId << ": " << v)
Yingdi Yuf7ede412014-08-30 20:37:52 -070036
37namespace chronosync {
38
Yingdi Yuf7ede412014-08-30 20:37:52 -070039using ndn::EventId;
40
41const uint8_t EMPTY_DIGEST_VALUE[] = {
42 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14,
43 0x9a, 0xfb, 0xf4, 0xc8, 0x99, 0x6f, 0xb9, 0x24,
44 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
45 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55
46};
47
Ashlesh Gawande08784d42017-09-06 23:40:21 -050048int Logic::s_instanceCounter = 0;
Yingdi Yuf7ede412014-08-30 20:37:52 -070049
Yingdi Yucd339022014-11-05 17:51:19 -080050const ndn::Name Logic::DEFAULT_NAME;
Qiuhan Ding8c095fd2014-11-19 17:38:32 -080051const ndn::Name Logic::EMPTY_NAME;
Ashlesh Gawande08784d42017-09-06 23:40:21 -050052const std::shared_ptr<Validator> Logic::DEFAULT_VALIDATOR;
Yingdi Yuf7ede412014-08-30 20:37:52 -070053const time::steady_clock::Duration Logic::DEFAULT_RESET_TIMER = time::seconds(0);
54const time::steady_clock::Duration Logic::DEFAULT_CANCEL_RESET_TIMER = time::milliseconds(500);
55const time::milliseconds Logic::DEFAULT_RESET_INTEREST_LIFETIME(1000);
56const time::milliseconds Logic::DEFAULT_SYNC_INTEREST_LIFETIME(1000);
57const time::milliseconds Logic::DEFAULT_SYNC_REPLY_FRESHNESS(1000);
Sonu Mishra4d3a2e02017-01-18 20:27:51 -080058const time::milliseconds Logic::DEFAULT_RECOVERY_INTEREST_LIFETIME(1000);
Yingdi Yuf7ede412014-08-30 20:37:52 -070059
Ashlesh Gawande08784d42017-09-06 23:40:21 -050060const ConstBufferPtr Logic::EMPTY_DIGEST(new ndn::Buffer(EMPTY_DIGEST_VALUE, 32));
Yingdi Yuf7ede412014-08-30 20:37:52 -070061const ndn::name::Component Logic::RESET_COMPONENT("reset");
Sonu Mishra4d3a2e02017-01-18 20:27:51 -080062const ndn::name::Component Logic::RECOVERY_COMPONENT("recovery");
Yingdi Yuf7ede412014-08-30 20:37:52 -070063
Alexander Afanasyev89036292018-02-13 17:19:50 -050064const size_t NDNLP_EXPECTED_OVERHEAD = 20;
65
66/**
67 * Get maximum packet limit
68 *
69 * By default, it returns `ndn::MAX_NDN_PACKET_SIZE`.
70 * The returned value can be customized using the environment variable `CHRONOSYNC_MAX_PACKET_SIZE`,
71 * but the returned value will be at least 500 and no more than `ndn::MAX_NDN_PACKET_SIZE`.
72 */
73#ifndef CHRONOSYNC_HAVE_TESTS
74static
75#endif // CHRONOSYNC_HAVE_TESTS
76size_t
77getMaxPacketLimit()
78{
79 static size_t limit = 0;
80#ifndef CHRONOSYNC_HAVE_TESTS
81 if (limit != 0) {
82 return limit;
83 }
84#endif // CHRONOSYNC_HAVE_TESTS
85
86 if (getenv("CHRONOSYNC_MAX_PACKET_SIZE") != nullptr) {
87 try {
88 limit = ndn::clamp<size_t>(boost::lexical_cast<size_t>(getenv("CHRONOSYNC_MAX_PACKET_SIZE")),
89 500, ndn::MAX_NDN_PACKET_SIZE);
90 }
91 catch (const boost::bad_lexical_cast&) {
92 limit = ndn::MAX_NDN_PACKET_SIZE;
93 }
94 }
95 else {
96 limit = ndn::MAX_NDN_PACKET_SIZE;
97 }
98
99 return limit;
100}
101
Yingdi Yuf7ede412014-08-30 20:37:52 -0700102Logic::Logic(ndn::Face& face,
103 const Name& syncPrefix,
Qiuhan Ding8c095fd2014-11-19 17:38:32 -0800104 const Name& defaultUserPrefix,
Yingdi Yuf7ede412014-08-30 20:37:52 -0700105 const UpdateCallback& onUpdate,
Qiuhan Ding8c095fd2014-11-19 17:38:32 -0800106 const Name& defaultSigningId,
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500107 std::shared_ptr<Validator> validator,
Yingdi Yuf7ede412014-08-30 20:37:52 -0700108 const time::steady_clock::Duration& resetTimer,
109 const time::steady_clock::Duration& cancelResetTimer,
110 const time::milliseconds& resetInterestLifetime,
111 const time::milliseconds& syncInterestLifetime,
Sonu Mishra4d3a2e02017-01-18 20:27:51 -0800112 const time::milliseconds& syncReplyFreshness,
Alexander Afanasyevbf5bc6c2018-02-19 11:26:09 -0500113 const time::milliseconds& recoveryInterestLifetime,
114 const name::Component& session)
Yingdi Yuf7ede412014-08-30 20:37:52 -0700115 : m_face(face)
116 , m_syncPrefix(syncPrefix)
Qiuhan Ding8c095fd2014-11-19 17:38:32 -0800117 , m_defaultUserPrefix(defaultUserPrefix)
Yingdi Yuf7ede412014-08-30 20:37:52 -0700118 , m_interestTable(m_face.getIoService())
119 , m_outstandingInterestId(0)
120 , m_isInReset(false)
121 , m_needPeriodReset(resetTimer > time::steady_clock::Duration::zero())
122 , m_onUpdate(onUpdate)
123 , m_scheduler(m_face.getIoService())
Ashlesh Gawande4a9ecd52018-02-06 14:36:19 -0600124 , m_rng(std::random_device{}())
125 , m_rangeUniformRandom(100, 500)
126 , m_reexpressionJitter(100, 500)
Yingdi Yuf7ede412014-08-30 20:37:52 -0700127 , m_resetTimer(resetTimer)
128 , m_cancelResetTimer(cancelResetTimer)
129 , m_resetInterestLifetime(resetInterestLifetime)
130 , m_syncInterestLifetime(syncInterestLifetime)
131 , m_syncReplyFreshness(syncReplyFreshness)
Sonu Mishra4d3a2e02017-01-18 20:27:51 -0800132 , m_recoveryInterestLifetime(recoveryInterestLifetime)
Yingdi Yucd339022014-11-05 17:51:19 -0800133 , m_validator(validator)
Alexander Afanasyev90587b82018-02-11 20:36:53 -0500134 , m_instanceId(s_instanceCounter++)
Yingdi Yuf7ede412014-08-30 20:37:52 -0700135{
Yingdi Yuf7ede412014-08-30 20:37:52 -0700136 _LOG_DEBUG_ID(">> Logic::Logic");
137
Alexander Afanasyevbf5bc6c2018-02-19 11:26:09 -0500138 addUserNode(m_defaultUserPrefix, defaultSigningId, session);
Qiuhan Ding8c095fd2014-11-19 17:38:32 -0800139
Yingdi Yuf7ede412014-08-30 20:37:52 -0700140 m_syncReset = m_syncPrefix;
141 m_syncReset.append("reset");
142
143 _LOG_DEBUG_ID("Listen to: " << m_syncPrefix);
144 m_syncRegisteredPrefixId =
Junxiao Shi4e010bc2017-11-16 14:11:00 +0000145 m_face.setInterestFilter(ndn::InterestFilter(m_syncPrefix).allowLoopback(false),
Yingdi Yuf7ede412014-08-30 20:37:52 -0700146 bind(&Logic::onSyncInterest, this, _1, _2),
147 bind(&Logic::onSyncRegisterFailed, this, _1, _2));
148
Qiuhan Dinge246b622014-12-03 21:57:48 -0800149 sendSyncInterest();
Yingdi Yuf7ede412014-08-30 20:37:52 -0700150 _LOG_DEBUG_ID("<< Logic::Logic");
151}
152
153Logic::~Logic()
154{
Nick Gordon0b3beab2018-03-02 13:03:28 -0600155 _LOG_DEBUG_ID(">> Logic::~Logic");
156 for (const auto& pendingInterestId : m_pendingInterests) {
157 m_face.removePendingInterest(pendingInterestId);
158 }
159 if (m_outstandingInterestId != nullptr) {
160 m_face.removePendingInterest(m_outstandingInterestId);
161 m_outstandingInterestId = nullptr;
162 }
163 m_face.unsetInterestFilter(m_syncRegisteredPrefixId);
164
Yingdi Yu9d5679a2015-02-01 00:17:58 -0800165 m_interestTable.clear();
Nick Gordon0b3beab2018-03-02 13:03:28 -0600166 m_scheduler.cancelAllEvents();
167 _LOG_DEBUG_ID("<< Logic::~Logic");
Yingdi Yuf7ede412014-08-30 20:37:52 -0700168}
169
170void
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800171Logic::reset(bool isOnInterest)
Yingdi Yuf7ede412014-08-30 20:37:52 -0700172{
173 m_isInReset = true;
174
175 m_state.reset();
176 m_log.clear();
177
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800178 if (!isOnInterest)
179 sendResetInterest();
Yingdi Yuf7ede412014-08-30 20:37:52 -0700180
181 // reset outstanding interest name, so that data for previous interest will be dropped.
182 if (m_outstandingInterestId != 0) {
183 m_face.removePendingInterest(m_outstandingInterestId);
184 m_outstandingInterestId = 0;
185 }
186
187 sendSyncInterest();
188
189 if (static_cast<bool>(m_delayedInterestProcessingId))
190 m_scheduler.cancelEvent(m_delayedInterestProcessingId);
191
192 m_delayedInterestProcessingId =
193 m_scheduler.scheduleEvent(m_cancelResetTimer,
194 bind(&Logic::cancelReset, this));
195}
196
197void
Qiuhan Ding8c095fd2014-11-19 17:38:32 -0800198Logic::setDefaultUserPrefix(const Name& defaultUserPrefix)
Yingdi Yuf7ede412014-08-30 20:37:52 -0700199{
Qiuhan Ding8c095fd2014-11-19 17:38:32 -0800200 if (defaultUserPrefix != EMPTY_NAME) {
201 if (m_nodeList.find(defaultUserPrefix) != m_nodeList.end()) {
202 m_defaultUserPrefix = defaultUserPrefix;
Qiuhan Ding8c095fd2014-11-19 17:38:32 -0800203 }
204 }
Yingdi Yuf7ede412014-08-30 20:37:52 -0700205}
206
207void
Alexander Afanasyevbf5bc6c2018-02-19 11:26:09 -0500208Logic::addUserNode(const Name& userPrefix, const Name& signingId, const name::Component& session)
Yingdi Yuf7ede412014-08-30 20:37:52 -0700209{
Qiuhan Ding8c095fd2014-11-19 17:38:32 -0800210 if (userPrefix == EMPTY_NAME)
Yingdi Yuf7ede412014-08-30 20:37:52 -0700211 return;
Qiuhan Ding8c095fd2014-11-19 17:38:32 -0800212 if (m_defaultUserPrefix == EMPTY_NAME) {
213 m_defaultUserPrefix = userPrefix;
Qiuhan Ding8c095fd2014-11-19 17:38:32 -0800214 }
215 if (m_nodeList.find(userPrefix) == m_nodeList.end()) {
216 m_nodeList[userPrefix].userPrefix = userPrefix;
217 m_nodeList[userPrefix].signingId = signingId;
218 Name sessionName = userPrefix;
Alexander Afanasyevbf5bc6c2018-02-19 11:26:09 -0500219 if (!session.empty()) {
220 sessionName.append(session);
221 }
222 else {
223 sessionName.appendNumber(ndn::time::toUnixTimestamp(ndn::time::system_clock::now()).count());
224 }
Qiuhan Ding8c095fd2014-11-19 17:38:32 -0800225 m_nodeList[userPrefix].sessionName = sessionName;
226 m_nodeList[userPrefix].seqNo = 0;
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800227 reset(false);
Qiuhan Ding8c095fd2014-11-19 17:38:32 -0800228 }
229}
Yingdi Yuf7ede412014-08-30 20:37:52 -0700230
Qiuhan Ding8c095fd2014-11-19 17:38:32 -0800231void
232Logic::removeUserNode(const Name& userPrefix)
233{
234 auto userNode = m_nodeList.find(userPrefix);
235 if (userNode != m_nodeList.end()) {
236 m_nodeList.erase(userNode);
237 if (m_defaultUserPrefix == userPrefix) {
238 if (!m_nodeList.empty()) {
239 m_defaultUserPrefix = m_nodeList.begin()->second.userPrefix;
Qiuhan Ding8c095fd2014-11-19 17:38:32 -0800240 }
241 else {
242 m_defaultUserPrefix = EMPTY_NAME;
Qiuhan Ding8c095fd2014-11-19 17:38:32 -0800243 }
Yingdi Yuf7ede412014-08-30 20:37:52 -0700244 }
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800245 reset(false);
Qiuhan Ding8c095fd2014-11-19 17:38:32 -0800246 }
247}
Yingdi Yuf7ede412014-08-30 20:37:52 -0700248
Qiuhan Ding8c095fd2014-11-19 17:38:32 -0800249const Name&
250Logic::getSessionName(Name prefix)
251{
252 if (prefix == EMPTY_NAME)
253 prefix = m_defaultUserPrefix;
254 auto node = m_nodeList.find(prefix);
255 if (node != m_nodeList.end())
256 return node->second.sessionName;
257 else
Alexander Afanasyeve9eda8a2017-03-09 14:40:03 -0800258 BOOST_THROW_EXCEPTION(Error("Refer to non-existent node:" + prefix.toUri()));
Qiuhan Ding8c095fd2014-11-19 17:38:32 -0800259}
Yingdi Yuf7ede412014-08-30 20:37:52 -0700260
Qiuhan Ding8c095fd2014-11-19 17:38:32 -0800261const SeqNo&
262Logic::getSeqNo(Name prefix)
263{
264 if (prefix == EMPTY_NAME)
265 prefix = m_defaultUserPrefix;
266 auto node = m_nodeList.find(prefix);
267 if (node != m_nodeList.end())
268 return node->second.seqNo;
269 else
Alexander Afanasyeve9eda8a2017-03-09 14:40:03 -0800270 BOOST_THROW_EXCEPTION(Logic::Error("Refer to non-existent node:" + prefix.toUri()));
Yingdi Yuf7ede412014-08-30 20:37:52 -0700271
Qiuhan Ding8c095fd2014-11-19 17:38:32 -0800272}
273
274void
Alexander Afanasyeve9eda8a2017-03-09 14:40:03 -0800275Logic::updateSeqNo(const SeqNo& seqNo, const Name& updatePrefix)
Qiuhan Ding8c095fd2014-11-19 17:38:32 -0800276{
277 Name prefix;
278 if (updatePrefix == EMPTY_NAME) {
279 if (m_defaultUserPrefix == EMPTY_NAME)
280 return;
281 prefix = m_defaultUserPrefix;
282 }
283 else
284 prefix = updatePrefix;
285
286 auto it = m_nodeList.find(prefix);
287 if (it != m_nodeList.end()) {
288 NodeInfo& node = it->second;
289 _LOG_DEBUG_ID(">> Logic::updateSeqNo");
290 _LOG_DEBUG_ID("seqNo: " << seqNo << " m_seqNo: " << node.seqNo);
291 if (seqNo < node.seqNo || seqNo == 0)
292 return;
293
294 node.seqNo = seqNo;
295 _LOG_DEBUG_ID("updateSeqNo: m_seqNo " << node.seqNo);
296
297 if (!m_isInReset) {
298 _LOG_DEBUG_ID("updateSeqNo: not in Reset ");
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500299 ConstBufferPtr previousRoot = m_state.getRootDigest();
Qiuhan Ding8c095fd2014-11-19 17:38:32 -0800300 {
Davide Pesavento5473abe2017-10-09 01:35:33 -0400301 std::string hash = ndn::toHex(previousRoot->data(), previousRoot->size(), false);
Qiuhan Ding8c095fd2014-11-19 17:38:32 -0800302 _LOG_DEBUG_ID("Hash: " << hash);
303 }
304
305 bool isInserted = false;
306 bool isUpdated = false;
307 SeqNo oldSeq;
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500308 std::tie(isInserted, isUpdated, oldSeq) = m_state.update(node.sessionName, node.seqNo);
Qiuhan Ding8c095fd2014-11-19 17:38:32 -0800309
310 _LOG_DEBUG_ID("Insert: " << std::boolalpha << isInserted);
311 _LOG_DEBUG_ID("Updated: " << std::boolalpha << isUpdated);
312 if (isInserted || isUpdated) {
313 DiffStatePtr commit = make_shared<DiffState>();
314 commit->update(node.sessionName, node.seqNo);
315 commit->setRootDigest(m_state.getRootDigest());
316 insertToDiffLog(commit, previousRoot);
317
318 satisfyPendingSyncInterests(prefix, commit);
Alexander Afanasyevfcbf81d2018-02-19 10:25:46 -0500319 // formAndSendExcludeInterest(prefix, *commit, previousRoot);
Qiuhan Ding8c095fd2014-11-19 17:38:32 -0800320 }
Yingdi Yuf7ede412014-08-30 20:37:52 -0700321 }
322 }
323}
324
325ConstBufferPtr
326Logic::getRootDigest() const
327{
328 return m_state.getRootDigest();
329}
330
331void
332Logic::printState(std::ostream& os) const
333{
Nick Gordon0b3beab2018-03-02 13:03:28 -0600334 for (const auto& leaf : m_state.getLeaves()) {
335 os << *leaf << "\n";
336 }
Yingdi Yuf7ede412014-08-30 20:37:52 -0700337}
338
339std::set<Name>
340Logic::getSessionNames() const
341{
342 std::set<Name> sessionNames;
Nick Gordon0b3beab2018-03-02 13:03:28 -0600343 for (const auto& leaf : m_state.getLeaves()) {
344 sessionNames.insert(leaf->getSessionName());
345 }
Yingdi Yuf7ede412014-08-30 20:37:52 -0700346 return sessionNames;
347}
348
349void
350Logic::onSyncInterest(const Name& prefix, const Interest& interest)
351{
352 _LOG_DEBUG_ID(">> Logic::onSyncInterest");
353 Name name = interest.getName();
354
355 _LOG_DEBUG_ID("InterestName: " << name);
356
Sonu Mishra4d3a2e02017-01-18 20:27:51 -0800357 if (name.size() >= 1 && RESET_COMPONENT == name.get(-1)) {
358 processResetInterest(interest);
359 }
360 else if (name.size() >= 2 && RECOVERY_COMPONENT == name.get(-2)) {
361 processRecoveryInterest(interest);
362 }
Ashlesh Gawande8ba7d5a2017-07-24 14:43:12 -0500363 // Do not process exclude interests, they should be answered by CS
364 else if (interest.getExclude().empty()) {
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500365 processSyncInterest(interest);
Yingdi Yuf7ede412014-08-30 20:37:52 -0700366 }
Yingdi Yuf7ede412014-08-30 20:37:52 -0700367
368 _LOG_DEBUG_ID("<< Logic::onSyncInterest");
369}
370
371void
372Logic::onSyncRegisterFailed(const Name& prefix, const std::string& msg)
373{
374 //Sync prefix registration failed
375 _LOG_DEBUG_ID(">> Logic::onSyncRegisterFailed");
376}
377
378void
Alexander Afanasyeve9eda8a2017-03-09 14:40:03 -0800379Logic::onSyncData(const Interest& interest, const Data& data)
Yingdi Yuf7ede412014-08-30 20:37:52 -0700380{
381 _LOG_DEBUG_ID(">> Logic::onSyncData");
Sonu Mishraf42aa2c2017-01-22 18:47:33 -0800382 // if (static_cast<bool>(m_validator))
383 // m_validator->validate(data,
384 // bind(&Logic::onSyncDataValidated, this, _1),
385 // bind(&Logic::onSyncDataValidationFailed, this, _1));
386 // else
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500387 // onSyncDataValidated(data);
Sonu Mishraf42aa2c2017-01-22 18:47:33 -0800388
389 if (interest.getExclude().empty()) {
390 _LOG_DEBUG_ID("First data");
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500391 onSyncDataValidated(data);
Sonu Mishraf42aa2c2017-01-22 18:47:33 -0800392 }
393 else {
394 _LOG_DEBUG_ID("Data obtained using exclude filter");
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500395 onSyncDataValidated(data, false);
Sonu Mishraf42aa2c2017-01-22 18:47:33 -0800396 }
Alexander Afanasyevfcbf81d2018-02-19 10:25:46 -0500397 // sendExcludeInterest(interest, data);
Sonu Mishraf42aa2c2017-01-22 18:47:33 -0800398
Yingdi Yuf7ede412014-08-30 20:37:52 -0700399 _LOG_DEBUG_ID("<< Logic::onSyncData");
400}
401
402void
Alexander Afanasyeve9eda8a2017-03-09 14:40:03 -0800403Logic::onResetData(const Interest& interest, const Data& data)
Yingdi Yuf7ede412014-08-30 20:37:52 -0700404{
405 // This should not happened, drop the received data.
406}
407
408void
409Logic::onSyncTimeout(const Interest& interest)
410{
411 // It is OK. Others will handle the time out situation.
412 _LOG_DEBUG_ID(">> Logic::onSyncTimeout");
413 _LOG_DEBUG_ID("Interest: " << interest.getName());
414 _LOG_DEBUG_ID("<< Logic::onSyncTimeout");
415}
416
417void
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500418Logic::onSyncDataValidationFailed(const Data& data)
Yingdi Yuf7ede412014-08-30 20:37:52 -0700419{
420 // SyncReply cannot be validated.
421}
422
423void
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500424Logic::onSyncDataValidated(const Data& data, bool firstData)
Yingdi Yuf7ede412014-08-30 20:37:52 -0700425{
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500426 Name name = data.getName();
Yingdi Yuf7ede412014-08-30 20:37:52 -0700427 ConstBufferPtr digest = make_shared<ndn::Buffer>(name.get(-1).value(), name.get(-1).value_size());
428
Alexander Afanasyevfcbf81d2018-02-19 10:25:46 -0500429 try {
430 auto contentBuffer = bzip2::decompress(reinterpret_cast<const char*>(data.getContent().value()),
431 data.getContent().value_size());
432 processSyncData(name, digest, Block(contentBuffer), firstData);
433 }
434 catch (const std::ios_base::failure& error) {
435 _LOG_WARN("Error decompressing content of " << data.getName() << " (" << error.what() << ")");
436 }
Yingdi Yuf7ede412014-08-30 20:37:52 -0700437}
438
439void
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500440Logic::processSyncInterest(const Interest& interest, bool isTimedProcessing/*=false*/)
Yingdi Yuf7ede412014-08-30 20:37:52 -0700441{
442 _LOG_DEBUG_ID(">> Logic::processSyncInterest");
443
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500444 Name name = interest.getName();
445 ConstBufferPtr digest = make_shared<ndn::Buffer>(name.get(-1).value(), name.get(-1).value_size());
Yingdi Yuf7ede412014-08-30 20:37:52 -0700446
447 ConstBufferPtr rootDigest = m_state.getRootDigest();
448
449 // If the digest of the incoming interest is the same as root digest
450 // Put the interest into InterestTable
451 if (*rootDigest == *digest) {
452 _LOG_DEBUG_ID("Oh, we are in the same state");
453 m_interestTable.insert(interest, digest, false);
454
455 if (!m_isInReset)
456 return;
457
458 if (!isTimedProcessing) {
459 _LOG_DEBUG_ID("Non timed processing in reset");
460 // Still in reset, our own seq has not been put into state yet
461 // Do not hurry, some others may be also resetting and may send their reply
462 if (static_cast<bool>(m_delayedInterestProcessingId))
463 m_scheduler.cancelEvent(m_delayedInterestProcessingId);
464
Ashlesh Gawande4a9ecd52018-02-06 14:36:19 -0600465 time::milliseconds after(m_rangeUniformRandom(m_rng));
Yingdi Yuf7ede412014-08-30 20:37:52 -0700466 _LOG_DEBUG_ID("After: " << after);
467 m_delayedInterestProcessingId =
468 m_scheduler.scheduleEvent(after,
469 bind(&Logic::processSyncInterest, this, interest, true));
470 }
471 else {
472 _LOG_DEBUG_ID("Timed processing in reset");
473 // Now we can get out of reset state by putting our own stuff into m_state.
474 cancelReset();
475 }
476
477 return;
478 }
479
480 // If the digest of incoming interest is an "empty" digest
Sonu Mishrae10acbc2017-01-18 14:14:05 -0800481 if (*digest == *EMPTY_DIGEST) {
Yingdi Yuf7ede412014-08-30 20:37:52 -0700482 _LOG_DEBUG_ID("Poor guy, he knows nothing");
Qiuhan Ding8c095fd2014-11-19 17:38:32 -0800483 sendSyncData(m_defaultUserPrefix, name, m_state);
Yingdi Yuf7ede412014-08-30 20:37:52 -0700484 return;
485 }
486
487 DiffStateContainer::iterator stateIter = m_log.find(digest);
488 // If the digest of incoming interest can be found from the log
489 if (stateIter != m_log.end()) {
490 _LOG_DEBUG_ID("It is ok, you are so close");
Qiuhan Ding8c095fd2014-11-19 17:38:32 -0800491 sendSyncData(m_defaultUserPrefix, name, *(*stateIter)->diff());
Yingdi Yuf7ede412014-08-30 20:37:52 -0700492 return;
493 }
494
495 if (!isTimedProcessing) {
496 _LOG_DEBUG_ID("Let's wait, just wait for a while");
497 // Do not hurry, some incoming SyncReplies may help us to recognize the digest
Yingdi Yu53f5f042015-01-31 16:33:25 -0800498 bool doesExist = m_interestTable.has(digest);
499 m_interestTable.insert(interest, digest, true);
Yingdi Yuf7ede412014-08-30 20:37:52 -0700500 if (doesExist)
501 // Original comment (not sure): somebody else replied, so restart random-game timer
502 // YY: Get the same SyncInterest again, refresh the timer
503 m_scheduler.cancelEvent(m_delayedInterestProcessingId);
504
505 m_delayedInterestProcessingId =
Ashlesh Gawande4a9ecd52018-02-06 14:36:19 -0600506 m_scheduler.scheduleEvent(time::milliseconds(m_rangeUniformRandom(m_rng)),
Yingdi Yuf7ede412014-08-30 20:37:52 -0700507 bind(&Logic::processSyncInterest, this, interest, true));
508 }
509 else {
510 // OK, nobody is helping us, just tell the truth.
Sonu Mishra4d3a2e02017-01-18 20:27:51 -0800511 _LOG_DEBUG_ID("OK, nobody is helping us, let us try to recover");
Yingdi Yuf7ede412014-08-30 20:37:52 -0700512 m_interestTable.erase(digest);
Sonu Mishra4d3a2e02017-01-18 20:27:51 -0800513 sendRecoveryInterest(digest);
Yingdi Yuf7ede412014-08-30 20:37:52 -0700514 }
515
516 _LOG_DEBUG_ID("<< Logic::processSyncInterest");
517}
518
519void
520Logic::processResetInterest(const Interest& interest)
521{
522 _LOG_DEBUG_ID(">> Logic::processResetInterest");
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800523 reset(true);
Yingdi Yuf7ede412014-08-30 20:37:52 -0700524}
525
526void
527Logic::processSyncData(const Name& name,
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500528 ConstBufferPtr digest,
Sonu Mishraf42aa2c2017-01-22 18:47:33 -0800529 const Block& syncReplyBlock,
530 bool firstData)
Yingdi Yuf7ede412014-08-30 20:37:52 -0700531{
532 _LOG_DEBUG_ID(">> Logic::processSyncData");
Yingdi Yuf7ede412014-08-30 20:37:52 -0700533 DiffStatePtr commit = make_shared<DiffState>();
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500534 ConstBufferPtr previousRoot = m_state.getRootDigest();
Yingdi Yuf7ede412014-08-30 20:37:52 -0700535
536 try {
537 m_interestTable.erase(digest); // Remove satisfied interest from PIT
538
539 State reply;
540 reply.wireDecode(syncReplyBlock);
541
542 std::vector<MissingDataInfo> v;
543 BOOST_FOREACH(ConstLeafPtr leaf, reply.getLeaves().get<ordered>())
544 {
545 BOOST_ASSERT(leaf != 0);
546
547 const Name& info = leaf->getSessionName();
548 SeqNo seq = leaf->getSeq();
549
550 bool isInserted = false;
551 bool isUpdated = false;
552 SeqNo oldSeq;
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500553 std::tie(isInserted, isUpdated, oldSeq) = m_state.update(info, seq);
Yingdi Yuf7ede412014-08-30 20:37:52 -0700554 if (isInserted || isUpdated) {
555 commit->update(info, seq);
556
557 oldSeq++;
558 MissingDataInfo mdi = {info, oldSeq, seq};
559 v.push_back(mdi);
560 }
561 }
562
563 if (!v.empty()) {
564 m_onUpdate(v);
565
566 commit->setRootDigest(m_state.getRootDigest());
567 insertToDiffLog(commit, previousRoot);
568 }
569 else {
570 _LOG_DEBUG_ID("What? nothing new");
571 }
572 }
Alexander Afanasyeve9eda8a2017-03-09 14:40:03 -0800573 catch (const State::Error&) {
Yingdi Yuf7ede412014-08-30 20:37:52 -0700574 _LOG_DEBUG_ID("Something really fishy happened during state decoding");
575 // Something really fishy happened during state decoding;
576 commit.reset();
577 return;
578 }
579
Sonu Mishraf42aa2c2017-01-22 18:47:33 -0800580 if (static_cast<bool>(commit) && !commit->getLeaves().empty() && firstData) {
Yingdi Yuf7ede412014-08-30 20:37:52 -0700581 // state changed and it is safe to express a new interest
Ashlesh Gawande4a9ecd52018-02-06 14:36:19 -0600582 time::steady_clock::Duration after = time::milliseconds(m_reexpressionJitter(m_rng));
Yingdi Yuf7ede412014-08-30 20:37:52 -0700583 _LOG_DEBUG_ID("Reschedule sync interest after: " << after);
584 EventId eventId = m_scheduler.scheduleEvent(after,
585 bind(&Logic::sendSyncInterest, this));
586
587 m_scheduler.cancelEvent(m_reexpressingInterestId);
588 m_reexpressingInterestId = eventId;
589 }
590}
591
592void
Qiuhan Ding8c095fd2014-11-19 17:38:32 -0800593Logic::satisfyPendingSyncInterests(const Name& updatedPrefix, ConstDiffStatePtr commit)
Yingdi Yuf7ede412014-08-30 20:37:52 -0700594{
595 _LOG_DEBUG_ID(">> Logic::satisfyPendingSyncInterests");
596 try {
597 _LOG_DEBUG_ID("InterestTable size: " << m_interestTable.size());
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500598 auto it = m_interestTable.begin();
599 while (it != m_interestTable.end()) {
Yingdi Yuf7ede412014-08-30 20:37:52 -0700600 ConstUnsatisfiedInterestPtr request = *it;
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500601 ++it;
Yingdi Yuf7ede412014-08-30 20:37:52 -0700602 if (request->isUnknown)
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500603 sendSyncData(updatedPrefix, request->interest.getName(), m_state);
Yingdi Yuf7ede412014-08-30 20:37:52 -0700604 else
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500605 sendSyncData(updatedPrefix, request->interest.getName(), *commit);
Yingdi Yuf7ede412014-08-30 20:37:52 -0700606 }
607 m_interestTable.clear();
608 }
Alexander Afanasyeve9eda8a2017-03-09 14:40:03 -0800609 catch (const InterestTable::Error&) {
Yingdi Yuf7ede412014-08-30 20:37:52 -0700610 // ok. not really an error
611 }
612 _LOG_DEBUG_ID("<< Logic::satisfyPendingSyncInterests");
613}
614
615void
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500616Logic::insertToDiffLog(DiffStatePtr commit, ConstBufferPtr previousRoot)
Yingdi Yuf7ede412014-08-30 20:37:52 -0700617{
618 _LOG_DEBUG_ID(">> Logic::insertToDiffLog");
619 // Connect to the history
620 if (!m_log.empty())
621 (*m_log.find(previousRoot))->setNext(commit);
622
623 // Insert the commit
624 m_log.erase(commit->getRootDigest());
625 m_log.insert(commit);
626 _LOG_DEBUG_ID("<< Logic::insertToDiffLog");
627}
628
629void
630Logic::sendResetInterest()
631{
632 _LOG_DEBUG_ID(">> Logic::sendResetInterest");
633
634 if (m_needPeriodReset) {
635 _LOG_DEBUG_ID("Need Period Reset");
636 _LOG_DEBUG_ID("ResetTimer: " << m_resetTimer);
637
638 EventId eventId =
Ashlesh Gawande4a9ecd52018-02-06 14:36:19 -0600639 m_scheduler.scheduleEvent(m_resetTimer + ndn::time::milliseconds(m_reexpressionJitter(m_rng)),
Yingdi Yuf7ede412014-08-30 20:37:52 -0700640 bind(&Logic::sendResetInterest, this));
641 m_scheduler.cancelEvent(m_resetInterestId);
642 m_resetInterestId = eventId;
643 }
644
645 Interest interest(m_syncReset);
646 interest.setMustBeFresh(true);
647 interest.setInterestLifetime(m_resetInterestLifetime);
Nick Gordon0b3beab2018-03-02 13:03:28 -0600648 const ndn::PendingInterestId* pendingInterestId = m_face.expressInterest(interest,
649 bind(&Logic::onResetData, this, _1, _2),
650 bind(&Logic::onSyncTimeout, this, _1), // Nack
651 bind(&Logic::onSyncTimeout, this, _1));
652 m_scheduler.scheduleEvent(m_resetInterestLifetime + ndn::time::milliseconds(5),
653 [pendingInterestId, this] {
654 cleanupPendingInterest(pendingInterestId);
655 });
656 m_pendingInterests.push_back(pendingInterestId);
Yingdi Yuf7ede412014-08-30 20:37:52 -0700657 _LOG_DEBUG_ID("<< Logic::sendResetInterest");
658}
659
660void
661Logic::sendSyncInterest()
662{
663 _LOG_DEBUG_ID(">> Logic::sendSyncInterest");
664
665 Name interestName;
666 interestName.append(m_syncPrefix)
667 .append(ndn::name::Component(*m_state.getRootDigest()));
668
669 m_outstandingInterestName = interestName;
670
671#ifdef _DEBUG
672 printDigest(m_state.getRootDigest());
673#endif
674
675 EventId eventId =
Sonu Mishra0dadc572016-12-12 23:59:41 -0800676 m_scheduler.scheduleEvent(m_syncInterestLifetime / 2 +
Ashlesh Gawande4a9ecd52018-02-06 14:36:19 -0600677 ndn::time::milliseconds(m_reexpressionJitter(m_rng)),
Yingdi Yuf7ede412014-08-30 20:37:52 -0700678 bind(&Logic::sendSyncInterest, this));
679 m_scheduler.cancelEvent(m_reexpressingInterestId);
680 m_reexpressingInterestId = eventId;
681
682 Interest interest(interestName);
683 interest.setMustBeFresh(true);
684 interest.setInterestLifetime(m_syncInterestLifetime);
685
Nick Gordon0b3beab2018-03-02 13:03:28 -0600686 if (m_outstandingInterestId != nullptr) {
687 m_face.removePendingInterest(m_outstandingInterestId);
688 m_outstandingInterestId = nullptr;
689 }
Yingdi Yuf7ede412014-08-30 20:37:52 -0700690 m_outstandingInterestId = m_face.expressInterest(interest,
691 bind(&Logic::onSyncData, this, _1, _2),
Alexander Afanasyeve9eda8a2017-03-09 14:40:03 -0800692 bind(&Logic::onSyncTimeout, this, _1), // Nack
Yingdi Yuf7ede412014-08-30 20:37:52 -0700693 bind(&Logic::onSyncTimeout, this, _1));
694
695 _LOG_DEBUG_ID("Send interest: " << interest.getName());
696 _LOG_DEBUG_ID("<< Logic::sendSyncInterest");
697}
698
699void
Alexander Afanasyev6ee98ff2018-02-13 19:12:28 -0500700Logic::trimState(State& partialState, const State& state, size_t nExcludedStates)
Ashlesh Gawande4a9ecd52018-02-06 14:36:19 -0600701{
702 partialState.reset();
Alexander Afanasyev6ee98ff2018-02-13 19:12:28 -0500703
Ashlesh Gawande4a9ecd52018-02-06 14:36:19 -0600704 std::vector<ConstLeafPtr> leaves;
705 for (const ConstLeafPtr& leaf : state.getLeaves()) {
706 leaves.push_back(leaf);
707 }
708
709 std::shuffle(leaves.begin(), leaves.end(), m_rng);
710
Alexander Afanasyev6ee98ff2018-02-13 19:12:28 -0500711 size_t statesToEncode = leaves.size() - std::min(leaves.size() - 1, nExcludedStates);
Ashlesh Gawande4a9ecd52018-02-06 14:36:19 -0600712 for (const auto& constLeafPtr : leaves) {
Alexander Afanasyev6ee98ff2018-02-13 19:12:28 -0500713 if (statesToEncode == 0) {
Ashlesh Gawande4a9ecd52018-02-06 14:36:19 -0600714 break;
715 }
716 partialState.update(constLeafPtr->getSessionName(), constLeafPtr->getSeq());
Alexander Afanasyev6ee98ff2018-02-13 19:12:28 -0500717 --statesToEncode;
Ashlesh Gawande4a9ecd52018-02-06 14:36:19 -0600718 }
719}
720
Alexander Afanasyev6ee98ff2018-02-13 19:12:28 -0500721Data
722Logic::encodeSyncReply(const Name& nodePrefix, const Name& name, const State& state)
723{
724 Data syncReply(name);
725 syncReply.setFreshnessPeriod(m_syncReplyFreshness);
726
727 auto finalizeReply = [this, &nodePrefix, &syncReply] (const State& state) {
728 auto contentBuffer = bzip2::compress(reinterpret_cast<const char*>(state.wireEncode().wire()),
729 state.wireEncode().size());
730 syncReply.setContent(contentBuffer);
731
732 if (m_nodeList[nodePrefix].signingId.empty())
733 m_keyChain.sign(syncReply);
734 else
735 m_keyChain.sign(syncReply, security::signingByIdentity(m_nodeList[nodePrefix].signingId));
736 };
737
738 finalizeReply(state);
739
740 size_t nExcludedStates = 1;
741 while (syncReply.wireEncode().size() > getMaxPacketLimit() - NDNLP_EXPECTED_OVERHEAD) {
742 if (nExcludedStates == 1) {
743 // To show this debug message only once
744 _LOG_DEBUG("Sync reply size exceeded maximum packet limit (" << (getMaxPacketLimit() - NDNLP_EXPECTED_OVERHEAD) << ")");
745 }
746 State partialState;
747 trimState(partialState, state, nExcludedStates);
748 finalizeReply(partialState);
749
750 BOOST_ASSERT(state.getLeaves().size() != 0);
751 nExcludedStates *= 2;
752 }
753
754 return syncReply;
755}
756
Ashlesh Gawande4a9ecd52018-02-06 14:36:19 -0600757void
Qiuhan Ding8c095fd2014-11-19 17:38:32 -0800758Logic::sendSyncData(const Name& nodePrefix, const Name& name, const State& state)
Yingdi Yuf7ede412014-08-30 20:37:52 -0700759{
760 _LOG_DEBUG_ID(">> Logic::sendSyncData");
Ashlesh Gawande4a9ecd52018-02-06 14:36:19 -0600761 if (m_nodeList.find(nodePrefix) == m_nodeList.end())
762 return;
763
Alexander Afanasyev6ee98ff2018-02-13 19:12:28 -0500764 m_face.put(encodeSyncReply(nodePrefix, name, state));
Yingdi Yuf7ede412014-08-30 20:37:52 -0700765
766 // checking if our own interest got satisfied
767 if (m_outstandingInterestName == name) {
768 // remove outstanding interest
769 if (m_outstandingInterestId != 0) {
770 m_face.removePendingInterest(m_outstandingInterestId);
771 m_outstandingInterestId = 0;
772 }
773
774 // re-schedule sending Sync interest
Ashlesh Gawande4a9ecd52018-02-06 14:36:19 -0600775 time::milliseconds after(m_reexpressionJitter(m_rng));
Yingdi Yuf7ede412014-08-30 20:37:52 -0700776 _LOG_DEBUG_ID("Satisfy our own interest");
777 _LOG_DEBUG_ID("Reschedule sync interest after " << after);
778 EventId eventId = m_scheduler.scheduleEvent(after, bind(&Logic::sendSyncInterest, this));
779 m_scheduler.cancelEvent(m_reexpressingInterestId);
780 m_reexpressingInterestId = eventId;
781 }
782 _LOG_DEBUG_ID("<< Logic::sendSyncData");
783}
784
785void
786Logic::cancelReset()
787{
788 _LOG_DEBUG_ID(">> Logic::cancelReset");
789 if (!m_isInReset)
790 return;
791
792 m_isInReset = false;
Qiuhan Ding8c095fd2014-11-19 17:38:32 -0800793 for (const auto& node : m_nodeList) {
794 updateSeqNo(node.second.seqNo, node.first);
795 }
Yingdi Yuf7ede412014-08-30 20:37:52 -0700796 _LOG_DEBUG_ID("<< Logic::cancelReset");
797}
798
799void
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500800Logic::printDigest(ConstBufferPtr digest)
Yingdi Yuf7ede412014-08-30 20:37:52 -0700801{
Davide Pesavento5473abe2017-10-09 01:35:33 -0400802 std::string hash = ndn::toHex(digest->data(), digest->size(), false);
Yingdi Yuf7ede412014-08-30 20:37:52 -0700803 _LOG_DEBUG_ID("Hash: " << hash);
804}
805
Sonu Mishra4d3a2e02017-01-18 20:27:51 -0800806void
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500807Logic::sendRecoveryInterest(ConstBufferPtr digest)
Sonu Mishra4d3a2e02017-01-18 20:27:51 -0800808{
809 _LOG_DEBUG_ID(">> Logic::sendRecoveryInterest");
810
811 Name interestName;
812 interestName.append(m_syncPrefix)
813 .append(RECOVERY_COMPONENT)
814 .append(ndn::name::Component(*digest));
815
816 Interest interest(interestName);
817 interest.setMustBeFresh(true);
818 interest.setInterestLifetime(m_recoveryInterestLifetime);
819
Nick Gordon0b3beab2018-03-02 13:03:28 -0600820 const ndn::PendingInterestId* pendingInterestId = m_face.expressInterest(interest,
821 bind(&Logic::onRecoveryData, this, _1, _2),
822 bind(&Logic::onRecoveryTimeout, this, _1), // Nack
823 bind(&Logic::onRecoveryTimeout, this, _1));
824 m_scheduler.scheduleEvent(m_recoveryInterestLifetime + ndn::time::milliseconds(5),
825 [pendingInterestId, this] {
826 cleanupPendingInterest(pendingInterestId);
827 });
828 m_pendingInterests.push_back(pendingInterestId);
Sonu Mishra4d3a2e02017-01-18 20:27:51 -0800829 _LOG_DEBUG_ID("interest: " << interest.getName());
830 _LOG_DEBUG_ID("<< Logic::sendRecoveryInterest");
831}
832
833void
834Logic::processRecoveryInterest(const Interest& interest)
835{
836 _LOG_DEBUG_ID(">> Logic::processRecoveryInterest");
837
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500838 Name name = interest.getName();
Sonu Mishra4d3a2e02017-01-18 20:27:51 -0800839 ConstBufferPtr digest = make_shared<ndn::Buffer>(name.get(-1).value(), name.get(-1).value_size());
840
841 ConstBufferPtr rootDigest = m_state.getRootDigest();
842
843 DiffStateContainer::iterator stateIter = m_log.find(digest);
844
845 if (stateIter != m_log.end() || *digest == *EMPTY_DIGEST || *rootDigest == *digest) {
846 _LOG_DEBUG_ID("I can help you recover");
847 sendSyncData(m_defaultUserPrefix, name, m_state);
848 return;
849 }
850 _LOG_DEBUG_ID("<< Logic::processRecoveryInterest");
851}
852
853void
Alexander Afanasyeve9eda8a2017-03-09 14:40:03 -0800854Logic::onRecoveryData(const Interest& interest, const Data& data)
Sonu Mishra4d3a2e02017-01-18 20:27:51 -0800855{
856 _LOG_DEBUG_ID(">> Logic::onRecoveryData");
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500857 onSyncDataValidated(data);
Sonu Mishra4d3a2e02017-01-18 20:27:51 -0800858 _LOG_DEBUG_ID("<< Logic::onRecoveryData");
859}
860
861void
862Logic::onRecoveryTimeout(const Interest& interest)
863{
864 _LOG_DEBUG_ID(">> Logic::onRecoveryTimeout");
865 _LOG_DEBUG_ID("Interest: " << interest.getName());
866 _LOG_DEBUG_ID("<< Logic::onRecoveryTimeout");
867}
868
Nick Gordon0b3beab2018-03-02 13:03:28 -0600869void
870Logic::cleanupPendingInterest(const ndn::PendingInterestId* pendingInterestId)
871{
872 auto itr = std::find(m_pendingInterests.begin(), m_pendingInterests.end(), pendingInterestId);
873 if (itr != m_pendingInterests.end()) {
874 m_pendingInterests.erase(itr);
875 }
876}
877
Alexander Afanasyevfcbf81d2018-02-19 10:25:46 -0500878// void
879// Logic::sendExcludeInterest(const Interest& interest, const Data& data)
880// {
881// _LOG_DEBUG_ID(">> Logic::sendExcludeInterest");
Sonu Mishraf42aa2c2017-01-22 18:47:33 -0800882
Alexander Afanasyevfcbf81d2018-02-19 10:25:46 -0500883// Name interestName = interest.getName();
884// Interest excludeInterest(interestName);
Sonu Mishraf42aa2c2017-01-22 18:47:33 -0800885
Alexander Afanasyevfcbf81d2018-02-19 10:25:46 -0500886// Exclude exclude = interest.getExclude();
887// exclude.excludeOne(data.getFullName().get(-1));
888// excludeInterest.setExclude(exclude);
889// excludeInterest.setMustBeFresh(true);
Sonu Mishraf42aa2c2017-01-22 18:47:33 -0800890
Alexander Afanasyevfcbf81d2018-02-19 10:25:46 -0500891// excludeInterest.setInterestLifetime(m_syncInterestLifetime);
Sonu Mishraf42aa2c2017-01-22 18:47:33 -0800892
Alexander Afanasyevfcbf81d2018-02-19 10:25:46 -0500893// if (excludeInterest.wireEncode().size() > ndn::MAX_NDN_PACKET_SIZE) {
894// return;
895// }
Ashlesh Gawanded31d6b12017-03-31 11:43:22 -0500896
Alexander Afanasyevfcbf81d2018-02-19 10:25:46 -0500897// m_face.expressInterest(excludeInterest,
898// bind(&Logic::onSyncData, this, _1, _2),
899// bind(&Logic::onSyncTimeout, this, _1), // Nack
900// bind(&Logic::onSyncTimeout, this, _1));
Sonu Mishraf42aa2c2017-01-22 18:47:33 -0800901
Alexander Afanasyevfcbf81d2018-02-19 10:25:46 -0500902// _LOG_DEBUG_ID("Send interest: " << excludeInterest.getName());
903// _LOG_DEBUG_ID("<< Logic::sendExcludeInterest");
904// }
Sonu Mishraf42aa2c2017-01-22 18:47:33 -0800905
Alexander Afanasyevfcbf81d2018-02-19 10:25:46 -0500906// void
907// Logic::formAndSendExcludeInterest(const Name& nodePrefix, const State& commit, ConstBufferPtr previousRoot)
908// {
909// _LOG_DEBUG_ID(">> Logic::formAndSendExcludeInterest");
910// Name interestName;
911// interestName.append(m_syncPrefix)
912// .append(ndn::name::Component(*previousRoot));
913// Interest interest(interestName);
Sonu Mishraf42aa2c2017-01-22 18:47:33 -0800914
Alexander Afanasyevfcbf81d2018-02-19 10:25:46 -0500915// Data data(interestName);
916// data.setContent(commit.wireEncode());
917// data.setFreshnessPeriod(m_syncReplyFreshness);
918// if (m_nodeList.find(nodePrefix) == m_nodeList.end())
919// return;
920// if (m_nodeList[nodePrefix].signingId.empty())
921// m_keyChain.sign(data);
922// else
923// m_keyChain.sign(data, security::signingByIdentity(m_nodeList[nodePrefix].signingId));
Sonu Mishraf42aa2c2017-01-22 18:47:33 -0800924
Alexander Afanasyevfcbf81d2018-02-19 10:25:46 -0500925// sendExcludeInterest(interest, data);
Sonu Mishraf42aa2c2017-01-22 18:47:33 -0800926
Alexander Afanasyevfcbf81d2018-02-19 10:25:46 -0500927// _LOG_DEBUG_ID("<< Logic::formAndSendExcludeInterest");
928// }
Sonu Mishraf42aa2c2017-01-22 18:47:33 -0800929
Alexander Afanasyeve9eda8a2017-03-09 14:40:03 -0800930} // namespace chronosync