blob: 460ed52c0261cf92ca3d5dc3c969cf97a230543b [file] [log] [blame]
Wentao Shang38d79682014-01-15 15:55:52 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2/*
3 * Copyright (c) 2013 University of California, Los Angeles
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation;
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 *
18 * Author: Wentao Shang <wentao@cs.ucla.edu>
19 */
20
Alexander Afanasyev09c613f2014-01-29 00:23:58 -080021#include "face.hpp"
22
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070023namespace ndn {
24
Wentao Shang38d79682014-01-15 15:55:52 -080025class Consumer
26{
27public:
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070028 Consumer(const std::string& dataName,
29 size_t pipeSize, size_t nTotalSegments,
Alexander Afanasyev4b98e8c2014-03-22 19:10:19 -070030 int scope = -1, bool mustBeFresh = true)
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070031 : m_dataName(dataName)
32 , m_pipeSize(pipeSize)
33 , m_nTotalSegments(nTotalSegments)
34 , m_nextSegment(0)
35 , m_totalSize(0)
36 , m_isOutputEnabled(false)
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070037 , m_scope(scope)
38 , m_mustBeFresh(mustBeFresh)
Wentao Shang38d79682014-01-15 15:55:52 -080039 {
40 }
41
42 inline void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070043 enableOutput()
Wentao Shang38d79682014-01-15 15:55:52 -080044 {
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070045 m_isOutputEnabled = true;
Wentao Shang38d79682014-01-15 15:55:52 -080046 }
47
48 void
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070049 run();
50
Wentao Shang38d79682014-01-15 15:55:52 -080051private:
52 void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070053 onData(const Interest& interest, Data& data);
Wentao Shang38d79682014-01-15 15:55:52 -080054
55 void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070056 onTimeout(const Interest& interest);
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070057
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070058 Face m_face;
59 Name m_dataName;
60 size_t m_pipeSize;
61 size_t m_nTotalSegments;
62 size_t m_nextSegment;
63 size_t m_totalSize;
64 bool m_isOutputEnabled; // set to false by default
Alexander Afanasyevc1ebbe92014-01-16 22:24:34 -080065
66 int m_scope;
67 bool m_mustBeFresh;
Wentao Shang38d79682014-01-15 15:55:52 -080068};
69
70void
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070071Consumer::run()
Wentao Shang38d79682014-01-15 15:55:52 -080072{
73 try
74 {
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070075 for (size_t i = 0; i < m_pipeSize; i++)
76 {
77 Interest interest(Name(m_dataName).appendSegment(m_nextSegment++));
78 interest.setInterestLifetime(time::milliseconds(4000));
79 if (m_scope >= 0)
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070080 interest.setScope(m_scope);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070081 interest.setMustBeFresh(m_mustBeFresh);
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070082
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070083 m_face.expressInterest(interest,
84 bind(&Consumer::onData, this, _1, _2),
85 bind(&Consumer::onTimeout, this, _1));
86 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070087
Wentao Shang38d79682014-01-15 15:55:52 -080088 // processEvents will block until the requested data received or timeout occurs
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070089 m_face.processEvents();
Wentao Shang38d79682014-01-15 15:55:52 -080090 }
91 catch (std::exception& e)
92 {
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -070093 std::cerr << "ERROR: " << e.what() << std::endl;
Wentao Shang38d79682014-01-15 15:55:52 -080094 }
95}
96
97void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070098Consumer::onData(const Interest& interest, Data& data)
Wentao Shang38d79682014-01-15 15:55:52 -080099{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700100 const Block& content = data.getContent();
101 const Name& name = data.getName();
Wentao Shang38d79682014-01-15 15:55:52 -0800102
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700103 if (m_isOutputEnabled)
Wentao Shang38d79682014-01-15 15:55:52 -0800104 {
105 std::cout.write(reinterpret_cast<const char*>(content.value()), content.value_size());
106 }
107
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700108 m_totalSize += content.value_size();
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700109
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700110 if (name[-1].toSegment() + 1 == m_nTotalSegments)
Wentao Shang38d79682014-01-15 15:55:52 -0800111 {
112 std::cerr << "Last segment received." << std::endl;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700113 std::cerr << "Total # bytes of content received: " << m_totalSize << std::endl;
Wentao Shang38d79682014-01-15 15:55:52 -0800114 }
115 else
116 {
117 // Send interest for next segment
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700118 Interest interest(Name(m_dataName).appendSegment(m_nextSegment++));
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700119 if (m_scope >= 0)
120 interest.setScope(m_scope);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700121 interest.setInterestLifetime(time::milliseconds(4000));
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700122 interest.setMustBeFresh(m_mustBeFresh);
123
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -0700124 m_face.expressInterest(interest,
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700125 bind(&Consumer::onData, this, _1, _2),
126 bind(&Consumer::onTimeout, this, _1));
Wentao Shang38d79682014-01-15 15:55:52 -0800127 }
128}
129
130
131void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700132Consumer::onTimeout(const Interest& interest)
Wentao Shang38d79682014-01-15 15:55:52 -0800133{
134 //XXX: currently no retrans
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700135 std::cerr << "TIMEOUT: last interest sent for segment #" << (m_nextSegment - 1) << std::endl;
Wentao Shang38d79682014-01-15 15:55:52 -0800136}
137
138
139int
140usage(const std::string &filename)
141{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700142 std::cerr << "Usage: \n "
143 << filename << " [-p pipeSize] [-c nTotalSegmentsment] [-o] /ndn/name\n";
Wentao Shang38d79682014-01-15 15:55:52 -0800144 return 1;
145}
146
147
148int
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700149main(int argc, char** argv)
Wentao Shang38d79682014-01-15 15:55:52 -0800150{
151 std::string name;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700152 int pipeSize = 1;
153 int nTotalSegments = std::numeric_limits<int>::max();
Wentao Shang38d79682014-01-15 15:55:52 -0800154 bool output = false;
155
156 int opt;
157 while ((opt = getopt(argc, argv, "op:c:")) != -1)
158 {
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700159 switch (opt)
160 {
Wentao Shang38d79682014-01-15 15:55:52 -0800161 case 'p':
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700162 pipeSize = atoi(optarg);
163 if (pipeSize <= 0)
164 pipeSize = 1;
165 std::cerr << "main(): set pipe size = " << pipeSize << std::endl;
166 break;
167 case 'c':
168 nTotalSegments = atoi(optarg);
169 if (nTotalSegments <= 0)
170 nTotalSegments = 1;
171 std::cerr << "main(): set total seg = " << nTotalSegments << std::endl;
172 break;
173 case 'o':
174 output = true;
175 break;
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700176 default:
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700177 return usage(argv[0]);
Wentao Shang38d79682014-01-15 15:55:52 -0800178 }
179 }
180
181 if (optind < argc)
182 {
183 name = argv[optind];
184 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700185
Wentao Shang38d79682014-01-15 15:55:52 -0800186 if (name.empty())
187 {
188 return usage(argv[0]);
189 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700190
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700191 Consumer consumer(name, pipeSize, nTotalSegments);
Wentao Shang38d79682014-01-15 15:55:52 -0800192
193 if (output)
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700194 consumer.enableOutput();
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700195
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -0700196 consumer.run();
Wentao Shang38d79682014-01-15 15:55:52 -0800197
198 return 0;
199}
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700200
201} // namespace ndn
202
203int
204main(int argc, char** argv)
205{
206 return ndn::main(argc, argv);
207}