blob: 44f7b6317f9f883e439bd96efb5b807603fab749 [file] [log] [blame]
Wentao Shang38d79682014-01-15 15:55:52 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07002/**
3 * Copyright (c) 2013-2014, Regents of the University of California.
4 * All rights reserved.
Wentao Shang38d79682014-01-15 15:55:52 -08005 *
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07006 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
7 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
Wentao Shang38d79682014-01-15 15:55:52 -08008 *
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07009 * This file licensed under New BSD License. See COPYING for detailed information about
10 * ndn-cxx library copyright, permissions, and redistribution restrictions.
Wentao Shang38d79682014-01-15 15:55:52 -080011 *
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -070012 * @author Wentao Shang <http://irl.cs.ucla.edu/~wentao/>
Wentao Shang38d79682014-01-15 15:55:52 -080013 */
14
Alexander Afanasyev09c613f2014-01-29 00:23:58 -080015#include "face.hpp"
16
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070017namespace ndn {
18
Wentao Shang38d79682014-01-15 15:55:52 -080019class Consumer
20{
21public:
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070022 Consumer(const std::string& dataName,
23 size_t pipeSize, size_t nTotalSegments,
Alexander Afanasyev4b98e8c2014-03-22 19:10:19 -070024 int scope = -1, bool mustBeFresh = true)
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070025 : m_dataName(dataName)
26 , m_pipeSize(pipeSize)
27 , m_nTotalSegments(nTotalSegments)
28 , m_nextSegment(0)
29 , m_totalSize(0)
30 , m_isOutputEnabled(false)
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070031 , m_scope(scope)
32 , m_mustBeFresh(mustBeFresh)
Wentao Shang38d79682014-01-15 15:55:52 -080033 {
34 }
35
36 inline void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070037 enableOutput()
Wentao Shang38d79682014-01-15 15:55:52 -080038 {
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070039 m_isOutputEnabled = true;
Wentao Shang38d79682014-01-15 15:55:52 -080040 }
41
42 void
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070043 run();
44
Wentao Shang38d79682014-01-15 15:55:52 -080045private:
46 void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070047 onData(const Interest& interest, Data& data);
Wentao Shang38d79682014-01-15 15:55:52 -080048
49 void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070050 onTimeout(const Interest& interest);
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070051
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070052 Face m_face;
53 Name m_dataName;
54 size_t m_pipeSize;
55 size_t m_nTotalSegments;
56 size_t m_nextSegment;
57 size_t m_totalSize;
58 bool m_isOutputEnabled; // set to false by default
Alexander Afanasyevc1ebbe92014-01-16 22:24:34 -080059
60 int m_scope;
61 bool m_mustBeFresh;
Wentao Shang38d79682014-01-15 15:55:52 -080062};
63
64void
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070065Consumer::run()
Wentao Shang38d79682014-01-15 15:55:52 -080066{
67 try
68 {
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070069 for (size_t i = 0; i < m_pipeSize; i++)
70 {
71 Interest interest(Name(m_dataName).appendSegment(m_nextSegment++));
72 interest.setInterestLifetime(time::milliseconds(4000));
73 if (m_scope >= 0)
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070074 interest.setScope(m_scope);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070075 interest.setMustBeFresh(m_mustBeFresh);
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070076
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070077 m_face.expressInterest(interest,
78 bind(&Consumer::onData, this, _1, _2),
79 bind(&Consumer::onTimeout, this, _1));
80 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070081
Wentao Shang38d79682014-01-15 15:55:52 -080082 // processEvents will block until the requested data received or timeout occurs
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070083 m_face.processEvents();
Wentao Shang38d79682014-01-15 15:55:52 -080084 }
85 catch (std::exception& e)
86 {
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -070087 std::cerr << "ERROR: " << e.what() << std::endl;
Wentao Shang38d79682014-01-15 15:55:52 -080088 }
89}
90
91void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070092Consumer::onData(const Interest& interest, Data& data)
Wentao Shang38d79682014-01-15 15:55:52 -080093{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070094 const Block& content = data.getContent();
95 const Name& name = data.getName();
Wentao Shang38d79682014-01-15 15:55:52 -080096
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070097 if (m_isOutputEnabled)
Wentao Shang38d79682014-01-15 15:55:52 -080098 {
99 std::cout.write(reinterpret_cast<const char*>(content.value()), content.value_size());
100 }
101
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700102 m_totalSize += content.value_size();
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700103
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700104 if (name[-1].toSegment() + 1 == m_nTotalSegments)
Wentao Shang38d79682014-01-15 15:55:52 -0800105 {
106 std::cerr << "Last segment received." << std::endl;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700107 std::cerr << "Total # bytes of content received: " << m_totalSize << std::endl;
Wentao Shang38d79682014-01-15 15:55:52 -0800108 }
109 else
110 {
111 // Send interest for next segment
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700112 Interest interest(Name(m_dataName).appendSegment(m_nextSegment++));
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700113 if (m_scope >= 0)
114 interest.setScope(m_scope);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700115 interest.setInterestLifetime(time::milliseconds(4000));
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700116 interest.setMustBeFresh(m_mustBeFresh);
117
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -0700118 m_face.expressInterest(interest,
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700119 bind(&Consumer::onData, this, _1, _2),
120 bind(&Consumer::onTimeout, this, _1));
Wentao Shang38d79682014-01-15 15:55:52 -0800121 }
122}
123
124
125void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700126Consumer::onTimeout(const Interest& interest)
Wentao Shang38d79682014-01-15 15:55:52 -0800127{
128 //XXX: currently no retrans
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700129 std::cerr << "TIMEOUT: last interest sent for segment #" << (m_nextSegment - 1) << std::endl;
Wentao Shang38d79682014-01-15 15:55:52 -0800130}
131
132
133int
134usage(const std::string &filename)
135{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700136 std::cerr << "Usage: \n "
137 << filename << " [-p pipeSize] [-c nTotalSegmentsment] [-o] /ndn/name\n";
Wentao Shang38d79682014-01-15 15:55:52 -0800138 return 1;
139}
140
141
142int
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700143main(int argc, char** argv)
Wentao Shang38d79682014-01-15 15:55:52 -0800144{
145 std::string name;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700146 int pipeSize = 1;
147 int nTotalSegments = std::numeric_limits<int>::max();
Wentao Shang38d79682014-01-15 15:55:52 -0800148 bool output = false;
149
150 int opt;
151 while ((opt = getopt(argc, argv, "op:c:")) != -1)
152 {
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700153 switch (opt)
154 {
Wentao Shang38d79682014-01-15 15:55:52 -0800155 case 'p':
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700156 pipeSize = atoi(optarg);
157 if (pipeSize <= 0)
158 pipeSize = 1;
159 std::cerr << "main(): set pipe size = " << pipeSize << std::endl;
160 break;
161 case 'c':
162 nTotalSegments = atoi(optarg);
163 if (nTotalSegments <= 0)
164 nTotalSegments = 1;
165 std::cerr << "main(): set total seg = " << nTotalSegments << std::endl;
166 break;
167 case 'o':
168 output = true;
169 break;
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700170 default:
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700171 return usage(argv[0]);
Wentao Shang38d79682014-01-15 15:55:52 -0800172 }
173 }
174
175 if (optind < argc)
176 {
177 name = argv[optind];
178 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700179
Wentao Shang38d79682014-01-15 15:55:52 -0800180 if (name.empty())
181 {
182 return usage(argv[0]);
183 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700184
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700185 Consumer consumer(name, pipeSize, nTotalSegments);
Wentao Shang38d79682014-01-15 15:55:52 -0800186
187 if (output)
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700188 consumer.enableOutput();
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700189
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -0700190 consumer.run();
Wentao Shang38d79682014-01-15 15:55:52 -0800191
192 return 0;
193}
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700194
195} // namespace ndn
196
197int
198main(int argc, char** argv)
199{
200 return ndn::main(argc, argv);
201}