Alexander Afanasyev | 42290b2 | 2017-03-09 12:58:29 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Alexander Afanasyev | c0e2658 | 2017-08-13 21:16:49 -0400 | [diff] [blame] | 2 | /* |
Davide Pesavento | f43a03e | 2018-02-21 22:04:21 -0500 | [diff] [blame] | 3 | * Copyright (c) 2014-2018, Regents of the University of California. |
Wentao Shang | bcbc929 | 2014-04-28 21:17:06 -0700 | [diff] [blame] | 4 | * |
| 5 | * This file is part of NDN repo-ng (Next generation of NDN repository). |
| 6 | * See AUTHORS.md for complete list of repo-ng authors and contributors. |
| 7 | * |
| 8 | * repo-ng 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, |
| 10 | * either version 3 of the License, or (at your option) any later version. |
| 11 | * |
| 12 | * repo-ng 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 | * repo-ng, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 18 | */ |
| 19 | |
| 20 | #include "ndngetfile.hpp" |
Alexander Afanasyev | c0e2658 | 2017-08-13 21:16:49 -0400 | [diff] [blame] | 21 | |
Wentao Shang | 91fb4f2 | 2014-05-20 10:55:22 -0700 | [diff] [blame] | 22 | #include <fstream> |
Alexander Afanasyev | c0e2658 | 2017-08-13 21:16:49 -0400 | [diff] [blame] | 23 | #include <iostream> |
| 24 | |
| 25 | #include <boost/lexical_cast.hpp> |
Wentao Shang | bcbc929 | 2014-04-28 21:17:06 -0700 | [diff] [blame] | 26 | |
| 27 | namespace repo { |
| 28 | |
Wentao Shang | a8f3c40 | 2014-10-30 14:03:27 -0700 | [diff] [blame] | 29 | using ndn::Name; |
| 30 | using ndn::Interest; |
| 31 | using ndn::Data; |
| 32 | using ndn::Block; |
| 33 | |
| 34 | using std::bind; |
| 35 | using std::placeholders::_1; |
| 36 | using std::placeholders::_2; |
Wentao Shang | bcbc929 | 2014-04-28 21:17:06 -0700 | [diff] [blame] | 37 | |
Weiqi Shi | 5822e34 | 2014-08-21 20:05:30 -0700 | [diff] [blame] | 38 | static const int MAX_RETRY = 3; |
| 39 | |
Wentao Shang | bcbc929 | 2014-04-28 21:17:06 -0700 | [diff] [blame] | 40 | void |
| 41 | Consumer::fetchData(const Name& name) |
| 42 | { |
| 43 | Interest interest(name); |
| 44 | interest.setInterestLifetime(m_interestLifetime); |
Davide Pesavento | f43a03e | 2018-02-21 22:04:21 -0500 | [diff] [blame] | 45 | if (m_hasVersion) { |
| 46 | interest.setMustBeFresh(m_mustBeFresh); |
| 47 | } |
| 48 | else { |
| 49 | interest.setMustBeFresh(true); |
| 50 | interest.setChildSelector(1); |
| 51 | } |
Wentao Shang | bcbc929 | 2014-04-28 21:17:06 -0700 | [diff] [blame] | 52 | |
| 53 | m_face.expressInterest(interest, |
Davide Pesavento | f43a03e | 2018-02-21 22:04:21 -0500 | [diff] [blame] | 54 | m_hasVersion ? bind(&Consumer::onVersionedData, this, _1, _2) |
| 55 | : bind(&Consumer::onUnversionedData, this, _1, _2), |
Alexander Afanasyev | 42290b2 | 2017-03-09 12:58:29 -0800 | [diff] [blame] | 56 | bind(&Consumer::onTimeout, this, _1), // Nack |
Wentao Shang | bcbc929 | 2014-04-28 21:17:06 -0700 | [diff] [blame] | 57 | bind(&Consumer::onTimeout, this, _1)); |
| 58 | } |
| 59 | |
| 60 | void |
| 61 | Consumer::run() |
| 62 | { |
| 63 | // Send the first Interest |
| 64 | Name name(m_dataName); |
Wentao Shang | bcbc929 | 2014-04-28 21:17:06 -0700 | [diff] [blame] | 65 | |
Weiqi Shi | 5822e34 | 2014-08-21 20:05:30 -0700 | [diff] [blame] | 66 | m_nextSegment++; |
Wentao Shang | bcbc929 | 2014-04-28 21:17:06 -0700 | [diff] [blame] | 67 | fetchData(name); |
| 68 | |
| 69 | // processEvents will block until the requested data received or timeout occurs |
| 70 | m_face.processEvents(m_timeout); |
| 71 | } |
| 72 | |
| 73 | void |
Alexander Afanasyev | 42290b2 | 2017-03-09 12:58:29 -0800 | [diff] [blame] | 74 | Consumer::onVersionedData(const Interest& interest, const Data& data) |
Wentao Shang | bcbc929 | 2014-04-28 21:17:06 -0700 | [diff] [blame] | 75 | { |
| 76 | const Name& name = data.getName(); |
Wentao Shang | bcbc929 | 2014-04-28 21:17:06 -0700 | [diff] [blame] | 77 | |
Weiqi Shi | 5822e34 | 2014-08-21 20:05:30 -0700 | [diff] [blame] | 78 | // the received data name may have segment number or not |
| 79 | if (name.size() == m_dataName.size()) { |
| 80 | if (!m_isSingle) { |
| 81 | Name fetchName = name; |
| 82 | fetchName.appendSegment(0); |
| 83 | fetchData(fetchName); |
| 84 | } |
| 85 | } |
| 86 | else if (name.size() == m_dataName.size() + 1) { |
| 87 | if (!m_isSingle) { |
| 88 | if (m_isFirst) { |
| 89 | uint64_t segment = name[-1].toSegment(); |
| 90 | if (segment != 0) { |
| 91 | fetchData(Name(m_dataName).appendSegment(0)); |
| 92 | m_isFirst = false; |
Wentao Shang | bcbc929 | 2014-04-28 21:17:06 -0700 | [diff] [blame] | 93 | return; |
| 94 | } |
Weiqi Shi | 5822e34 | 2014-08-21 20:05:30 -0700 | [diff] [blame] | 95 | m_isFirst = false; |
| 96 | } |
| 97 | fetchNextData(name, data); |
Wentao Shang | bcbc929 | 2014-04-28 21:17:06 -0700 | [diff] [blame] | 98 | } |
Weiqi Shi | 5822e34 | 2014-08-21 20:05:30 -0700 | [diff] [blame] | 99 | else { |
| 100 | std::cerr << "ERROR: Data is not stored in a single packet" << std::endl; |
| 101 | return; |
| 102 | } |
| 103 | } |
| 104 | else { |
| 105 | std::cerr << "ERROR: Name size does not match" << std::endl; |
| 106 | return; |
| 107 | } |
| 108 | readData(data); |
| 109 | } |
Wentao Shang | bcbc929 | 2014-04-28 21:17:06 -0700 | [diff] [blame] | 110 | |
Weiqi Shi | 5822e34 | 2014-08-21 20:05:30 -0700 | [diff] [blame] | 111 | void |
Alexander Afanasyev | 42290b2 | 2017-03-09 12:58:29 -0800 | [diff] [blame] | 112 | Consumer::onUnversionedData(const Interest& interest, const Data& data) |
Weiqi Shi | 5822e34 | 2014-08-21 20:05:30 -0700 | [diff] [blame] | 113 | { |
| 114 | const Name& name = data.getName(); |
| 115 | //std::cout<<"recevied data name = "<<name<<std::endl; |
| 116 | if (name.size() == m_dataName.size() + 1) { |
| 117 | if (!m_isSingle) { |
| 118 | Name fetchName = name; |
| 119 | fetchName.append(name[-1]).appendSegment(0); |
| 120 | fetchData(fetchName); |
| 121 | } |
| 122 | } |
| 123 | else if (name.size() == m_dataName.size() + 2) { |
| 124 | if (!m_isSingle) { |
| 125 | if (m_isFirst) { |
| 126 | uint64_t segment = name[-1].toSegment(); |
| 127 | if (segment != 0) { |
| 128 | fetchData(Name(m_dataName).append(name[-2]).appendSegment(0)); |
| 129 | m_isFirst = false; |
| 130 | return; |
| 131 | } |
| 132 | m_isFirst = false; |
| 133 | } |
| 134 | fetchNextData(name, data); |
| 135 | } |
| 136 | else { |
| 137 | std::cerr << "ERROR: Data is not stored in a single packet" << std::endl; |
| 138 | return; |
| 139 | } |
| 140 | } |
| 141 | else { |
| 142 | std::cerr << "ERROR: Name size does not match" << std::endl; |
| 143 | return; |
| 144 | } |
| 145 | readData(data); |
| 146 | } |
Wentao Shang | bcbc929 | 2014-04-28 21:17:06 -0700 | [diff] [blame] | 147 | |
Weiqi Shi | 5822e34 | 2014-08-21 20:05:30 -0700 | [diff] [blame] | 148 | void |
| 149 | Consumer::readData(const Data& data) |
| 150 | { |
Wentao Shang | bcbc929 | 2014-04-28 21:17:06 -0700 | [diff] [blame] | 151 | const Block& content = data.getContent(); |
| 152 | m_os.write(reinterpret_cast<const char*>(content.value()), content.value_size()); |
| 153 | m_totalSize += content.value_size(); |
Davide Pesavento | f43a03e | 2018-02-21 22:04:21 -0500 | [diff] [blame] | 154 | if (m_verbose) { |
Weiqi Shi | 5822e34 | 2014-08-21 20:05:30 -0700 | [diff] [blame] | 155 | std::cerr << "LOG: received data = " << data.getName() << std::endl; |
| 156 | } |
| 157 | if (m_isFinished || m_isSingle) { |
| 158 | std::cerr << "INFO: End of file is reached." << std::endl; |
| 159 | std::cerr << "INFO: Total # of segments received: " << m_nextSegment << std::endl; |
| 160 | std::cerr << "INFO: Total # bytes of content received: " << m_totalSize << std::endl; |
| 161 | } |
Wentao Shang | bcbc929 | 2014-04-28 21:17:06 -0700 | [diff] [blame] | 162 | } |
| 163 | |
Weiqi Shi | 5822e34 | 2014-08-21 20:05:30 -0700 | [diff] [blame] | 164 | void |
| 165 | Consumer::fetchNextData(const Name& name, const Data& data) |
| 166 | { |
| 167 | uint64_t segment = name[-1].toSegment(); |
Davide Pesavento | f43a03e | 2018-02-21 22:04:21 -0500 | [diff] [blame] | 168 | BOOST_VERIFY(segment == (m_nextSegment - 1)); |
| 169 | |
Wentao Shang | a8f3c40 | 2014-10-30 14:03:27 -0700 | [diff] [blame] | 170 | const ndn::name::Component& finalBlockId = data.getMetaInfo().getFinalBlockId(); |
Weiqi Shi | 5822e34 | 2014-08-21 20:05:30 -0700 | [diff] [blame] | 171 | if (finalBlockId == name[-1]) { |
| 172 | m_isFinished = true; |
| 173 | } |
Davide Pesavento | f43a03e | 2018-02-21 22:04:21 -0500 | [diff] [blame] | 174 | else { |
Weiqi Shi | 5822e34 | 2014-08-21 20:05:30 -0700 | [diff] [blame] | 175 | // Reset retry counter |
| 176 | m_retryCount = 0; |
| 177 | if (m_hasVersion) |
| 178 | fetchData(Name(m_dataName).appendSegment(m_nextSegment++)); |
| 179 | else |
| 180 | fetchData(Name(m_dataName).append(name[-2]).appendSegment(m_nextSegment++)); |
| 181 | } |
| 182 | } |
Wentao Shang | bcbc929 | 2014-04-28 21:17:06 -0700 | [diff] [blame] | 183 | |
| 184 | void |
| 185 | Consumer::onTimeout(const Interest& interest) |
| 186 | { |
Davide Pesavento | f43a03e | 2018-02-21 22:04:21 -0500 | [diff] [blame] | 187 | if (m_retryCount++ < MAX_RETRY) { |
| 188 | // Retransmit the interest |
| 189 | fetchData(interest.getName()); |
| 190 | if (m_verbose) |
| 191 | std::cerr << "TIMEOUT: retransmit interest for " << interest.getName() << std::endl; |
| 192 | } |
| 193 | else { |
| 194 | std::cerr << "TIMEOUT: last interest sent for segment #" << (m_nextSegment - 1) << std::endl; |
| 195 | std::cerr << "TIMEOUT: abort fetching after " << MAX_RETRY |
| 196 | << " times of retry" << std::endl; |
| 197 | } |
Wentao Shang | bcbc929 | 2014-04-28 21:17:06 -0700 | [diff] [blame] | 198 | } |
| 199 | |
Wentao Shang | bcbc929 | 2014-04-28 21:17:06 -0700 | [diff] [blame] | 200 | int |
| 201 | usage(const std::string& filename) |
| 202 | { |
| 203 | std::cerr << "Usage: \n " |
Weiqi Shi | 5822e34 | 2014-08-21 20:05:30 -0700 | [diff] [blame] | 204 | << filename << " [-v] [-s] [-u] [-l lifetime] [-w timeout] [-o filename] ndn-name\n\n" |
Wentao Shang | bcbc929 | 2014-04-28 21:17:06 -0700 | [diff] [blame] | 205 | << "-v: be verbose\n" |
Weiqi Shi | 5822e34 | 2014-08-21 20:05:30 -0700 | [diff] [blame] | 206 | << "-s: only get single data packet\n" |
| 207 | << "-u: versioned: ndn-name contains version component\n" |
| 208 | << " if -u is not specified, this command will return the rightmost child for the prefix\n" |
Wentao Shang | bcbc929 | 2014-04-28 21:17:06 -0700 | [diff] [blame] | 209 | << "-l: InterestLifetime in milliseconds\n" |
| 210 | << "-w: timeout in milliseconds for whole process (default unlimited)\n" |
Weiqi Shi | 5822e34 | 2014-08-21 20:05:30 -0700 | [diff] [blame] | 211 | << "-o: write to local file name instead of stdout\n" |
| 212 | << "ndn-name: NDN Name prefix for Data to be read\n"; |
Wentao Shang | bcbc929 | 2014-04-28 21:17:06 -0700 | [diff] [blame] | 213 | return 1; |
| 214 | } |
| 215 | |
Wentao Shang | bcbc929 | 2014-04-28 21:17:06 -0700 | [diff] [blame] | 216 | int |
| 217 | main(int argc, char** argv) |
| 218 | { |
| 219 | std::string name; |
Davide Pesavento | f43a03e | 2018-02-21 22:04:21 -0500 | [diff] [blame] | 220 | const char* outputFile = nullptr; |
Weiqi Shi | 5822e34 | 2014-08-21 20:05:30 -0700 | [diff] [blame] | 221 | bool verbose = false, versioned = false, single = false; |
Wentao Shang | bcbc929 | 2014-04-28 21:17:06 -0700 | [diff] [blame] | 222 | int interestLifetime = 4000; // in milliseconds |
| 223 | int timeout = 0; // in milliseconds |
| 224 | |
| 225 | int opt; |
Davide Pesavento | f43a03e | 2018-02-21 22:04:21 -0500 | [diff] [blame] | 226 | while ((opt = getopt(argc, argv, "vsul:w:o:")) != -1) { |
| 227 | switch (opt) { |
| 228 | case 'v': |
| 229 | verbose = true; |
| 230 | break; |
| 231 | case 's': |
| 232 | single = true; |
| 233 | break; |
| 234 | case 'u': |
| 235 | versioned = true; |
| 236 | break; |
| 237 | case 'l': |
| 238 | try { |
| 239 | interestLifetime = boost::lexical_cast<int>(optarg); |
Wentao Shang | bcbc929 | 2014-04-28 21:17:06 -0700 | [diff] [blame] | 240 | } |
Davide Pesavento | f43a03e | 2018-02-21 22:04:21 -0500 | [diff] [blame] | 241 | catch (const boost::bad_lexical_cast&) { |
| 242 | std::cerr << "ERROR: -l option should be an integer." << std::endl; |
| 243 | return 1; |
| 244 | } |
| 245 | interestLifetime = std::max(interestLifetime, 0); |
| 246 | break; |
| 247 | case 'w': |
| 248 | try { |
| 249 | timeout = boost::lexical_cast<int>(optarg); |
| 250 | } |
| 251 | catch (const boost::bad_lexical_cast&) { |
| 252 | std::cerr << "ERROR: -w option should be an integer." << std::endl; |
| 253 | return 1; |
| 254 | } |
| 255 | timeout = std::max(timeout, 0); |
| 256 | break; |
| 257 | case 'o': |
| 258 | outputFile = optarg; |
| 259 | break; |
| 260 | default: |
Wentao Shang | bcbc929 | 2014-04-28 21:17:06 -0700 | [diff] [blame] | 261 | return usage(argv[0]); |
| 262 | } |
Davide Pesavento | f43a03e | 2018-02-21 22:04:21 -0500 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | if (optind < argc) |
| 266 | name = argv[optind]; |
| 267 | |
| 268 | if (name.empty()) |
| 269 | return usage(argv[0]); |
Wentao Shang | bcbc929 | 2014-04-28 21:17:06 -0700 | [diff] [blame] | 270 | |
| 271 | std::streambuf* buf; |
| 272 | std::ofstream of; |
| 273 | |
Davide Pesavento | f43a03e | 2018-02-21 22:04:21 -0500 | [diff] [blame] | 274 | if (outputFile != nullptr) { |
| 275 | of.open(outputFile, std::ios::out | std::ios::binary | std::ios::trunc); |
| 276 | if (!of || !of.is_open()) { |
| 277 | std::cerr << "ERROR: cannot open " << outputFile << std::endl; |
| 278 | return 1; |
Wentao Shang | bcbc929 | 2014-04-28 21:17:06 -0700 | [diff] [blame] | 279 | } |
Davide Pesavento | f43a03e | 2018-02-21 22:04:21 -0500 | [diff] [blame] | 280 | buf = of.rdbuf(); |
| 281 | } |
| 282 | else { |
| 283 | buf = std::cout.rdbuf(); |
| 284 | } |
Wentao Shang | bcbc929 | 2014-04-28 21:17:06 -0700 | [diff] [blame] | 285 | |
| 286 | std::ostream os(buf); |
Davide Pesavento | f43a03e | 2018-02-21 22:04:21 -0500 | [diff] [blame] | 287 | Consumer consumer(name, os, verbose, versioned, single, interestLifetime, timeout); |
Wentao Shang | bcbc929 | 2014-04-28 21:17:06 -0700 | [diff] [blame] | 288 | |
Davide Pesavento | f43a03e | 2018-02-21 22:04:21 -0500 | [diff] [blame] | 289 | try { |
| 290 | consumer.run(); |
| 291 | } |
| 292 | catch (const std::exception& e) { |
| 293 | std::cerr << "ERROR: " << e.what() << std::endl; |
| 294 | } |
Wentao Shang | bcbc929 | 2014-04-28 21:17:06 -0700 | [diff] [blame] | 295 | |
| 296 | return 0; |
| 297 | } |
| 298 | |
| 299 | } // namespace repo |
| 300 | |
| 301 | int |
| 302 | main(int argc, char** argv) |
| 303 | { |
| 304 | return repo::main(argc, argv); |
| 305 | } |