blob: fddf08e0f394260513e02e8350211a2b945c05e9 [file] [log] [blame]
Andrea Tosatto672b9a72016-01-05 16:18:20 +01001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Klaus Schneider7072e162017-09-16 13:43:00 -07002/*
Davide Pesavento06807dc2017-09-19 15:52:43 -04003 * Copyright (c) 2016-2017, Regents of the University of California,
4 * Colorado State University,
5 * University Pierre & Marie Curie, Sorbonne University.
Andrea Tosatto672b9a72016-01-05 16:18:20 +01006 *
7 * This file is part of ndn-tools (Named Data Networking Essential Tools).
8 * See AUTHORS.md for complete list of ndn-tools authors and contributors.
9 *
10 * ndn-tools 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 * ndn-tools 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 * ndn-tools, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
20 *
21 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
22 *
23 * @author Wentao Shang
24 * @author Steve DiBenedetto
25 * @author Andrea Tosatto
Klaus Schneider7072e162017-09-16 13:43:00 -070026 * @author Klaus Schneider
Andrea Tosatto672b9a72016-01-05 16:18:20 +010027 */
28
29#include "discover-version-iterative.hpp"
30
31namespace ndn {
32namespace chunks {
33
34DiscoverVersionIterative::DiscoverVersionIterative(const Name& prefix, Face& face,
35 const Options& options)
36 : chunks::Options(options)
Davide Pesavento06807dc2017-09-19 15:52:43 -040037 , DiscoverVersion(prefix, face)
Andrea Tosatto672b9a72016-01-05 16:18:20 +010038 , Options(options)
39 , m_latestVersion(0)
Andrea Tosatto672b9a72016-01-05 16:18:20 +010040 , m_foundVersion(false)
41{
42}
43
44void
45DiscoverVersionIterative::run()
46{
47 m_latestVersion = 0;
48 m_foundVersion = false;
49
50 Interest interest(m_prefix);
51 interest.setInterestLifetime(interestLifetime);
52 interest.setMustBeFresh(mustBeFresh);
53 interest.setMinSuffixComponents(3);
54 interest.setMaxSuffixComponents(3);
55 interest.setChildSelector(1);
56
57 expressInterest(interest, maxRetriesOnTimeoutOrNack, maxRetriesOnTimeoutOrNack);
58}
59
60void
61DiscoverVersionIterative::handleData(const Interest& interest, const Data& data)
62{
63 size_t versionindex = m_prefix.size();
64
65 const Name& name = data.getName();
66 Exclude exclude;
67
68 if (isVerbose)
69 std::cerr << "Data: " << data << std::endl;
70
71 BOOST_ASSERT(name.size() > m_prefix.size());
72 if (name[versionindex].isVersion()) {
73 m_latestVersion = name[versionindex].toVersion();
74 m_latestVersionData = make_shared<Data>(data);
75 m_foundVersion = true;
76
77 exclude.excludeBefore(name[versionindex]);
78
79 if (isVerbose)
80 std::cerr << "Discovered version = " << m_latestVersion << std::endl;
81 }
82 else {
83 // didn't find a version number at expected index.
84 m_strayExcludes.excludeOne(name[versionindex]);
85 }
86
Junxiao Shib098e9f2016-07-20 14:00:40 +000087 for (const Exclude::Range& range : m_strayExcludes) {
88 BOOST_ASSERT(range.isSingular());
89 exclude.excludeOne(range.from);
Andrea Tosatto672b9a72016-01-05 16:18:20 +010090 }
91
92 Interest newInterest(interest);
93 newInterest.refreshNonce();
94 newInterest.setExclude(exclude);
Klaus Schneider7072e162017-09-16 13:43:00 -070095 newInterest.setInterestLifetime(discoveryTimeout);
Andrea Tosatto672b9a72016-01-05 16:18:20 +010096
97 if (m_foundVersion)
98 expressInterest(newInterest, maxRetriesOnTimeoutOrNack, maxRetriesAfterVersionFound);
99 else
100 expressInterest(interest, maxRetriesOnTimeoutOrNack, maxRetriesOnTimeoutOrNack);
101}
102
103void
104DiscoverVersionIterative::handleTimeout(const Interest& interest, const std::string& reason)
105{
106 if (m_foundVersion) {
107 // a version has been found and after a timeout error this version can be used as the latest.
108 if (isVerbose)
109 std::cerr << "Found data with the latest version: " << m_latestVersion << std::endl;
110
111 // we discovered at least one version. assume what we have is the latest.
112 this->emitSignal(onDiscoverySuccess, *m_latestVersionData);
113 }
114 else {
115 DiscoverVersion::handleTimeout(interest, reason);
116 }
117}
118
119} // namespace chunks
120} // namespace ndn