blob: b9a41d2eacf3ca6814f4d1759578e8591be1269e [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07002/**
Weiwei Liu1e787c42016-04-22 10:13:41 -07003 * Copyright (c) 2013-2016 Regents of the University of California.
Wentao Shang38d79682014-01-15 15:55:52 -08004 *
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07005 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
Wentao Shang38d79682014-01-15 15:55:52 -08006 *
Alexander Afanasyevc169a812014-05-20 20:37:29 -04007 * ndn-cxx library is free software: you can redistribute it and/or modify it under the
8 * terms of the GNU Lesser General Public License as published by the Free Software
9 * Foundation, either version 3 of the License, or (at your option) any later version.
10 *
11 * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
14 *
15 * You should have received copies of the GNU General Public License and GNU Lesser
16 * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
17 * <http://www.gnu.org/licenses/>.
18 *
19 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
Wentao Shang38d79682014-01-15 15:55:52 -080020 *
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -070021 * @author Wentao Shang <http://irl.cs.ucla.edu/~wentao/>
Wentao Shang38d79682014-01-15 15:55:52 -080022 */
23
Alexander Afanasyev09c613f2014-01-29 00:23:58 -080024#include "face.hpp"
25
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070026namespace ndn {
27
Wentao Shang38d79682014-01-15 15:55:52 -080028class Consumer
29{
30public:
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070031 Consumer(const std::string& dataName,
32 size_t pipeSize, size_t nTotalSegments,
Alexander Afanasyev117f5ef2015-06-03 15:07:24 -070033 bool mustBeFresh = true)
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070034 : m_dataName(dataName)
35 , m_pipeSize(pipeSize)
36 , m_nTotalSegments(nTotalSegments)
37 , m_nextSegment(0)
38 , m_totalSize(0)
39 , m_isOutputEnabled(false)
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070040 , m_mustBeFresh(mustBeFresh)
Wentao Shang38d79682014-01-15 15:55:52 -080041 {
42 }
43
44 inline void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070045 enableOutput()
Wentao Shang38d79682014-01-15 15:55:52 -080046 {
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070047 m_isOutputEnabled = true;
Wentao Shang38d79682014-01-15 15:55:52 -080048 }
49
50 void
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070051 run();
52
Wentao Shang38d79682014-01-15 15:55:52 -080053private:
54 void
Alexander Afanasyev24b75c82014-05-31 15:59:31 +030055 onData(Data& data);
Wentao Shang38d79682014-01-15 15:55:52 -080056
57 void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070058 onTimeout(const Interest& interest);
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070059
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070060 Face m_face;
61 Name m_dataName;
62 size_t m_pipeSize;
63 size_t m_nTotalSegments;
64 size_t m_nextSegment;
65 size_t m_totalSize;
66 bool m_isOutputEnabled; // set to false by default
Alexander Afanasyevc1ebbe92014-01-16 22:24:34 -080067
Alexander Afanasyevc1ebbe92014-01-16 22:24:34 -080068 bool m_mustBeFresh;
Wentao Shang38d79682014-01-15 15:55:52 -080069};
70
71void
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070072Consumer::run()
Wentao Shang38d79682014-01-15 15:55:52 -080073{
74 try
75 {
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070076 for (size_t i = 0; i < m_pipeSize; i++)
77 {
78 Interest interest(Name(m_dataName).appendSegment(m_nextSegment++));
79 interest.setInterestLifetime(time::milliseconds(4000));
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070080 interest.setMustBeFresh(m_mustBeFresh);
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070081
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070082 m_face.expressInterest(interest,
Alexander Afanasyev24b75c82014-05-31 15:59:31 +030083 bind(&Consumer::onData, this, _2),
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070084 bind(&Consumer::onTimeout, this, _1));
85 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070086
Wentao Shang38d79682014-01-15 15:55:52 -080087 // processEvents will block until the requested data received or timeout occurs
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070088 m_face.processEvents();
Wentao Shang38d79682014-01-15 15:55:52 -080089 }
90 catch (std::exception& e)
91 {
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -070092 std::cerr << "ERROR: " << e.what() << std::endl;
Wentao Shang38d79682014-01-15 15:55:52 -080093 }
94}
95
96void
Alexander Afanasyev24b75c82014-05-31 15:59:31 +030097Consumer::onData(Data& data)
Wentao Shang38d79682014-01-15 15:55:52 -080098{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070099 const Block& content = data.getContent();
100 const Name& name = data.getName();
Wentao Shang38d79682014-01-15 15:55:52 -0800101
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700102 if (m_isOutputEnabled)
Wentao Shang38d79682014-01-15 15:55:52 -0800103 {
104 std::cout.write(reinterpret_cast<const char*>(content.value()), content.value_size());
105 }
106
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700107 m_totalSize += content.value_size();
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700108
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700109 if (name[-1].toSegment() + 1 == m_nTotalSegments)
Wentao Shang38d79682014-01-15 15:55:52 -0800110 {
111 std::cerr << "Last segment received." << std::endl;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700112 std::cerr << "Total # bytes of content received: " << m_totalSize << std::endl;
Wentao Shang38d79682014-01-15 15:55:52 -0800113 }
114 else
115 {
116 // Send interest for next segment
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700117 Interest interest(Name(m_dataName).appendSegment(m_nextSegment++));
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700118 interest.setInterestLifetime(time::milliseconds(4000));
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700119 interest.setMustBeFresh(m_mustBeFresh);
120
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -0700121 m_face.expressInterest(interest,
Alexander Afanasyev24b75c82014-05-31 15:59:31 +0300122 bind(&Consumer::onData, this, _2),
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700123 bind(&Consumer::onTimeout, this, _1));
Wentao Shang38d79682014-01-15 15:55:52 -0800124 }
125}
126
127
128void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700129Consumer::onTimeout(const Interest& interest)
Wentao Shang38d79682014-01-15 15:55:52 -0800130{
131 //XXX: currently no retrans
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700132 std::cerr << "TIMEOUT: last interest sent for segment #" << (m_nextSegment - 1) << std::endl;
Wentao Shang38d79682014-01-15 15:55:52 -0800133}
134
135
136int
137usage(const std::string &filename)
138{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700139 std::cerr << "Usage: \n "
140 << filename << " [-p pipeSize] [-c nTotalSegmentsment] [-o] /ndn/name\n";
Wentao Shang38d79682014-01-15 15:55:52 -0800141 return 1;
142}
143
144
145int
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700146main(int argc, char** argv)
Wentao Shang38d79682014-01-15 15:55:52 -0800147{
148 std::string name;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700149 int pipeSize = 1;
150 int nTotalSegments = std::numeric_limits<int>::max();
Wentao Shang38d79682014-01-15 15:55:52 -0800151 bool output = false;
152
153 int opt;
154 while ((opt = getopt(argc, argv, "op:c:")) != -1)
155 {
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700156 switch (opt)
157 {
Wentao Shang38d79682014-01-15 15:55:52 -0800158 case 'p':
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700159 pipeSize = atoi(optarg);
160 if (pipeSize <= 0)
161 pipeSize = 1;
162 std::cerr << "main(): set pipe size = " << pipeSize << std::endl;
163 break;
164 case 'c':
165 nTotalSegments = atoi(optarg);
166 if (nTotalSegments <= 0)
167 nTotalSegments = 1;
168 std::cerr << "main(): set total seg = " << nTotalSegments << std::endl;
169 break;
170 case 'o':
171 output = true;
172 break;
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700173 default:
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700174 return usage(argv[0]);
Wentao Shang38d79682014-01-15 15:55:52 -0800175 }
176 }
177
178 if (optind < argc)
179 {
180 name = argv[optind];
181 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700182
Wentao Shang38d79682014-01-15 15:55:52 -0800183 if (name.empty())
184 {
185 return usage(argv[0]);
186 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700187
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700188 Consumer consumer(name, pipeSize, nTotalSegments);
Wentao Shang38d79682014-01-15 15:55:52 -0800189
190 if (output)
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700191 consumer.enableOutput();
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700192
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -0700193 consumer.run();
Wentao Shang38d79682014-01-15 15:55:52 -0800194
195 return 0;
196}
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700197
198} // namespace ndn
199
200int
201main(int argc, char** argv)
202{
Weiwei Liu1e787c42016-04-22 10:13:41 -0700203 std::cerr << "ndncatchunks3 is deprecated. Use ndncatchunks program from ndn-tools repository.\n" << std::endl;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700204 return ndn::main(argc, argv);
205}