blob: 9d38cfed7bc1997413ee261bf3e75776b95bb420 [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/*
3 * Copyright (c) 2016-2017, Regents of the University of California,
Andrea Tosatto672b9a72016-01-05 16:18:20 +01004 * Colorado State University,
5 * University Pierre & Marie Curie, Sorbonne University.
6 *
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)
37 , DiscoverVersion(prefix, face, options)
38 , Options(options)
39 , m_latestVersion(0)
40 , m_latestVersionData(nullptr)
41 , m_foundVersion(false)
42{
43}
44
45void
46DiscoverVersionIterative::run()
47{
48 m_latestVersion = 0;
49 m_foundVersion = false;
50
51 Interest interest(m_prefix);
52 interest.setInterestLifetime(interestLifetime);
53 interest.setMustBeFresh(mustBeFresh);
54 interest.setMinSuffixComponents(3);
55 interest.setMaxSuffixComponents(3);
56 interest.setChildSelector(1);
57
58 expressInterest(interest, maxRetriesOnTimeoutOrNack, maxRetriesOnTimeoutOrNack);
59}
60
61void
62DiscoverVersionIterative::handleData(const Interest& interest, const Data& data)
63{
64 size_t versionindex = m_prefix.size();
65
66 const Name& name = data.getName();
67 Exclude exclude;
68
69 if (isVerbose)
70 std::cerr << "Data: " << data << std::endl;
71
72 BOOST_ASSERT(name.size() > m_prefix.size());
73 if (name[versionindex].isVersion()) {
74 m_latestVersion = name[versionindex].toVersion();
75 m_latestVersionData = make_shared<Data>(data);
76 m_foundVersion = true;
77
78 exclude.excludeBefore(name[versionindex]);
79
80 if (isVerbose)
81 std::cerr << "Discovered version = " << m_latestVersion << std::endl;
82 }
83 else {
84 // didn't find a version number at expected index.
85 m_strayExcludes.excludeOne(name[versionindex]);
86 }
87
Junxiao Shib098e9f2016-07-20 14:00:40 +000088 for (const Exclude::Range& range : m_strayExcludes) {
89 BOOST_ASSERT(range.isSingular());
90 exclude.excludeOne(range.from);
Andrea Tosatto672b9a72016-01-05 16:18:20 +010091 }
92
93 Interest newInterest(interest);
94 newInterest.refreshNonce();
95 newInterest.setExclude(exclude);
Klaus Schneider7072e162017-09-16 13:43:00 -070096 newInterest.setInterestLifetime(discoveryTimeout);
Andrea Tosatto672b9a72016-01-05 16:18:20 +010097
98 if (m_foundVersion)
99 expressInterest(newInterest, maxRetriesOnTimeoutOrNack, maxRetriesAfterVersionFound);
100 else
101 expressInterest(interest, maxRetriesOnTimeoutOrNack, maxRetriesOnTimeoutOrNack);
102}
103
104void
105DiscoverVersionIterative::handleTimeout(const Interest& interest, const std::string& reason)
106{
107 if (m_foundVersion) {
108 // a version has been found and after a timeout error this version can be used as the latest.
109 if (isVerbose)
110 std::cerr << "Found data with the latest version: " << m_latestVersion << std::endl;
111
112 // we discovered at least one version. assume what we have is the latest.
113 this->emitSignal(onDiscoverySuccess, *m_latestVersionData);
114 }
115 else {
116 DiscoverVersion::handleTimeout(interest, reason);
117 }
118}
119
120} // namespace chunks
121} // namespace ndn