blob: 99f54ca290e5d7637980b8fb8ca49bc82a0c87a3 [file] [log] [blame]
Wentao Shangbcbc9292014-04-28 21:17:06 -07001/* -*- 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 Shang91fb4f22014-05-20 10:55:22 -070022#include <fstream>
Wentao Shangbcbc9292014-04-28 21:17:06 -070023
24namespace repo {
25
Wentao Shanga8f3c402014-10-30 14:03:27 -070026using ndn::Name;
27using ndn::Interest;
28using ndn::Data;
29using ndn::Block;
30
31using std::bind;
32using std::placeholders::_1;
33using std::placeholders::_2;
Wentao Shangbcbc9292014-04-28 21:17:06 -070034
Weiqi Shi5822e342014-08-21 20:05:30 -070035static const int MAX_RETRY = 3;
36
Wentao Shangbcbc9292014-04-28 21:17:06 -070037void
38Consumer::fetchData(const Name& name)
39{
40 Interest interest(name);
41 interest.setInterestLifetime(m_interestLifetime);
Weiqi Shi5822e342014-08-21 20:05:30 -070042 //std::cout<<"interest name = "<<interest.getName()<<std::endl;
Wentao Shangbcbc9292014-04-28 21:17:06 -070043 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 Shi5822e342014-08-21 20:05:30 -070054 m_hasVersion ?
Wentao Shanga8f3c402014-10-30 14:03:27 -070055 bind(&Consumer::onVersionedData, this, _1, _2)
56 :
57 bind(&Consumer::onUnversionedData, this, _1, _2),
Wentao Shangbcbc9292014-04-28 21:17:06 -070058 bind(&Consumer::onTimeout, this, _1));
59}
60
61void
62Consumer::run()
63{
64 // Send the first Interest
65 Name name(m_dataName);
Wentao Shangbcbc9292014-04-28 21:17:06 -070066
Weiqi Shi5822e342014-08-21 20:05:30 -070067 m_nextSegment++;
Wentao Shangbcbc9292014-04-28 21:17:06 -070068 fetchData(name);
69
70 // processEvents will block until the requested data received or timeout occurs
71 m_face.processEvents(m_timeout);
72}
73
74void
Weiqi Shi5822e342014-08-21 20:05:30 -070075Consumer::onVersionedData(const Interest& interest, Data& data)
Wentao Shangbcbc9292014-04-28 21:17:06 -070076{
77 const Name& name = data.getName();
Wentao Shangbcbc9292014-04-28 21:17:06 -070078
Weiqi Shi5822e342014-08-21 20:05:30 -070079 // 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 Shangbcbc9292014-04-28 21:17:06 -070094 return;
95 }
Weiqi Shi5822e342014-08-21 20:05:30 -070096 m_isFirst = false;
97 }
98 fetchNextData(name, data);
Wentao Shangbcbc9292014-04-28 21:17:06 -070099 }
Weiqi Shi5822e342014-08-21 20:05:30 -0700100 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 Shangbcbc9292014-04-28 21:17:06 -0700111
Weiqi Shi5822e342014-08-21 20:05:30 -0700112void
113Consumer::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 Shangbcbc9292014-04-28 21:17:06 -0700148
Weiqi Shi5822e342014-08-21 20:05:30 -0700149void
150Consumer::readData(const Data& data)
151{
Wentao Shangbcbc9292014-04-28 21:17:06 -0700152 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 Shi5822e342014-08-21 20:05:30 -0700156 {
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 Shangbcbc9292014-04-28 21:17:06 -0700164}
165
Weiqi Shi5822e342014-08-21 20:05:30 -0700166void
167Consumer::fetchNextData(const Name& name, const Data& data)
168{
169 uint64_t segment = name[-1].toSegment();
170 BOOST_ASSERT(segment == (m_nextSegment - 1));
Wentao Shanga8f3c402014-10-30 14:03:27 -0700171 const ndn::name::Component& finalBlockId = data.getMetaInfo().getFinalBlockId();
Weiqi Shi5822e342014-08-21 20:05:30 -0700172 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 Shangbcbc9292014-04-28 21:17:06 -0700185
186void
187Consumer::onTimeout(const Interest& interest)
188{
Weiqi Shi5822e342014-08-21 20:05:30 -0700189 if (m_retryCount++ < MAX_RETRY)
Wentao Shangbcbc9292014-04-28 21:17:06 -0700190 {
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 Shi5822e342014-08-21 20:05:30 -0700201 std::cerr << "TIMEOUT: abort fetching after " << MAX_RETRY
Wentao Shangbcbc9292014-04-28 21:17:06 -0700202 << " times of retry" << std::endl;
203 }
204}
205
206
207int
208usage(const std::string& filename)
209{
210 std::cerr << "Usage: \n "
Weiqi Shi5822e342014-08-21 20:05:30 -0700211 << filename << " [-v] [-s] [-u] [-l lifetime] [-w timeout] [-o filename] ndn-name\n\n"
Wentao Shangbcbc9292014-04-28 21:17:06 -0700212 << "-v: be verbose\n"
Weiqi Shi5822e342014-08-21 20:05:30 -0700213 << "-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 Shangbcbc9292014-04-28 21:17:06 -0700216 << "-l: InterestLifetime in milliseconds\n"
217 << "-w: timeout in milliseconds for whole process (default unlimited)\n"
Weiqi Shi5822e342014-08-21 20:05:30 -0700218 << "-o: write to local file name instead of stdout\n"
219 << "ndn-name: NDN Name prefix for Data to be read\n";
Wentao Shangbcbc9292014-04-28 21:17:06 -0700220 return 1;
221}
222
223
224int
225main(int argc, char** argv)
226{
227 std::string name;
228 const char* outputFile = 0;
Weiqi Shi5822e342014-08-21 20:05:30 -0700229 bool verbose = false, versioned = false, single = false;
Wentao Shangbcbc9292014-04-28 21:17:06 -0700230 int interestLifetime = 4000; // in milliseconds
231 int timeout = 0; // in milliseconds
232
233 int opt;
Weiqi Shi5822e342014-08-21 20:05:30 -0700234 while ((opt = getopt(argc, argv, "vsul:w:o:")) != -1)
Wentao Shangbcbc9292014-04-28 21:17:06 -0700235 {
236 switch (opt) {
237 case 'v':
238 verbose = true;
239 break;
Weiqi Shi5822e342014-08-21 20:05:30 -0700240 case 's':
241 single = true;
242 break;
Wentao Shangbcbc9292014-04-28 21:17:06 -0700243 case 'u':
Weiqi Shi5822e342014-08-21 20:05:30 -0700244 versioned = true;
Wentao Shangbcbc9292014-04-28 21:17:06 -0700245 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 Collinsdd6348a2014-12-14 12:32:07 -0800293 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 Shangbcbc9292014-04-28 21:17:06 -0700298 buf = of.rdbuf();
299 }
300 else
301 {
302 buf = std::cout.rdbuf();
303 }
304
305 std::ostream os(buf);
306
Weiqi Shi5822e342014-08-21 20:05:30 -0700307 Consumer consumer(name, os, verbose, versioned, single,
Wentao Shangbcbc9292014-04-28 21:17:06 -0700308 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
324int
325main(int argc, char** argv)
326{
327 return repo::main(argc, argv);
328}