Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2016, Regents of the University of California, |
| 4 | * 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 |
| 26 | */ |
| 27 | |
| 28 | #include "discover-version-iterative.hpp" |
| 29 | |
| 30 | namespace ndn { |
| 31 | namespace chunks { |
| 32 | |
| 33 | DiscoverVersionIterative::DiscoverVersionIterative(const Name& prefix, Face& face, |
| 34 | const Options& options) |
| 35 | : chunks::Options(options) |
| 36 | , DiscoverVersion(prefix, face, options) |
| 37 | , Options(options) |
| 38 | , m_latestVersion(0) |
| 39 | , m_latestVersionData(nullptr) |
| 40 | , m_foundVersion(false) |
| 41 | { |
| 42 | } |
| 43 | |
| 44 | void |
| 45 | DiscoverVersionIterative::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 | |
| 60 | void |
| 61 | DiscoverVersionIterative::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 | |
| 87 | for (const auto& i : m_strayExcludes) { |
| 88 | exclude.excludeOne(i.first); |
| 89 | } |
| 90 | |
| 91 | Interest newInterest(interest); |
| 92 | newInterest.refreshNonce(); |
| 93 | newInterest.setExclude(exclude); |
| 94 | |
| 95 | if (m_foundVersion) |
| 96 | expressInterest(newInterest, maxRetriesOnTimeoutOrNack, maxRetriesAfterVersionFound); |
| 97 | else |
| 98 | expressInterest(interest, maxRetriesOnTimeoutOrNack, maxRetriesOnTimeoutOrNack); |
| 99 | } |
| 100 | |
| 101 | void |
| 102 | DiscoverVersionIterative::handleTimeout(const Interest& interest, const std::string& reason) |
| 103 | { |
| 104 | if (m_foundVersion) { |
| 105 | // a version has been found and after a timeout error this version can be used as the latest. |
| 106 | if (isVerbose) |
| 107 | std::cerr << "Found data with the latest version: " << m_latestVersion << std::endl; |
| 108 | |
| 109 | // we discovered at least one version. assume what we have is the latest. |
| 110 | this->emitSignal(onDiscoverySuccess, *m_latestVersionData); |
| 111 | } |
| 112 | else { |
| 113 | DiscoverVersion::handleTimeout(interest, reason); |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | } // namespace chunks |
| 118 | } // namespace ndn |