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