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