blob: af263c8ae425cfaf88bbe724b170d6af85696c64 [file] [log] [blame]
Ashlesh Gawande32ec3fd2018-07-18 13:42:32 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2014-2018, The University of Memphis,
4 * Regents of the University of California,
5 * Arizona Board of Regents.
6 *
7 * This file is part of NLSR (Named-data Link State Routing).
8 * See AUTHORS.md for complete list of NLSR authors and contributors.
9 *
10 * NLSR is free software: you can redistribute it and/or modify it under the terms
11 * of the GNU General Public License as published by the Free Software Foundation,
12 * either version 3 of the License, or (at your option) any later version.
13 *
14 * NLSR is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
15 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
16 * PURPOSE. See the GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along with
19 * NLSR, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
20 **/
21
22#include "sync-protocol-adapter.hpp"
23#include "logger.hpp"
24
25INIT_LOGGER(SyncProtocolAdapter);
26
27namespace nlsr {
28
29const auto FIXED_SESSION = ndn::name::Component::fromNumber(0);
30
31SyncProtocolAdapter::SyncProtocolAdapter(ndn::Face& face,
32 int32_t syncProtocol,
33 const ndn::Name& syncPrefix,
34 const ndn::Name& userPrefix,
35 ndn::time::milliseconds syncInterestLifetime,
36 const SyncUpdateCallback& syncUpdateCallback)
37 : m_syncProtocol(syncProtocol)
38 , m_syncUpdateCallback(syncUpdateCallback)
39{
40 if (m_syncProtocol == SYNC_PROTOCOL_CHRONOSYNC) {
41 NDN_LOG_DEBUG("Using ChronoSync");
42 m_chronoSyncLogic = std::make_shared<chronosync::Logic>(face,
43 syncPrefix,
44 userPrefix,
45 std::bind(&SyncProtocolAdapter::onChronoSyncUpdate, this, _1),
46 chronosync::Logic::DEFAULT_NAME,
47 chronosync::Logic::DEFAULT_VALIDATOR,
48 chronosync::Logic::DEFAULT_RESET_TIMER,
49 chronosync::Logic::DEFAULT_CANCEL_RESET_TIMER,
50 chronosync::Logic::DEFAULT_RESET_INTEREST_LIFETIME,
51 syncInterestLifetime,
52 chronosync::Logic::DEFAULT_SYNC_REPLY_FRESHNESS,
53 chronosync::Logic::DEFAULT_RECOVERY_INTEREST_LIFETIME,
54 FIXED_SESSION);
55 }
56 else {
57 NDN_LOG_DEBUG("Using PSync");
58 m_psyncLogic = std::make_shared<psync::FullProducer>(80,
59 face,
60 syncPrefix,
61 userPrefix,
62 std::bind(&SyncProtocolAdapter::onPSyncUpdate, this, _1),
63 syncInterestLifetime);
64 }
65}
66
67void
68SyncProtocolAdapter::addUserNode(const ndn::Name& userPrefix)
69{
70 if (m_syncProtocol == SYNC_PROTOCOL_CHRONOSYNC) {
71 m_chronoSyncLogic->addUserNode(userPrefix, chronosync::Logic::DEFAULT_NAME, FIXED_SESSION);
72 }
73 else {
74 m_psyncLogic->addUserNode(userPrefix);
75 }
76}
77
78void
79SyncProtocolAdapter::publishUpdate(const ndn::Name& userPrefix, uint64_t seq)
80{
81 if (m_syncProtocol == SYNC_PROTOCOL_CHRONOSYNC) {
82 m_chronoSyncLogic->updateSeqNo(seq, userPrefix);
83 }
84 else {
85 m_psyncLogic->publishName(userPrefix, seq);
86 }
87}
88
89void
90SyncProtocolAdapter::onChronoSyncUpdate(const std::vector<chronosync::MissingDataInfo>& updates)
91{
92 NLSR_LOG_TRACE("Received ChronoSync update event");
93
94 for (const auto& update : updates) {
95 // Remove FIXED_SESSION
96 m_syncUpdateCallback(update.session.getPrefix(-1), update.high);
97 }
98}
99
100void
101SyncProtocolAdapter::onPSyncUpdate(const std::vector<psync::MissingDataInfo>& updates)
102{
103 NLSR_LOG_TRACE("Received PSync update event");
104
105 for (const auto& update : updates) {
106 m_syncUpdateCallback(update.prefix, update.highSeq);
107 }
108}
109
110} // namespace nlsr