blob: f1d1d6f25847720d366c2d971db76552889e3a85 [file] [log] [blame]
Junxiao Shi2ac79d92015-03-23 11:16:18 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Teng Liangd4ab87f2016-03-08 18:50:47 -07003 * Copyright (c) 2014-2016, Regents of the University of California,
Junxiao Shi1688ded2015-03-29 10:09:26 -07004 * Arizona Board of Regents,
5 * Colorado State University,
6 * University Pierre & Marie Curie, Sorbonne University,
7 * Washington University in St. Louis,
8 * Beijing Institute of Technology,
9 * The University of Memphis.
10 *
11 * This file is part of ndn-tools (Named Data Networking Essential Tools).
12 * See AUTHORS.md for complete list of ndn-tools authors and contributors.
13 *
14 * ndn-tools is free software: you can redistribute it and/or modify it under the terms
15 * of the GNU General Public License as published by the Free Software Foundation,
16 * either version 3 of the License, or (at your option) any later version.
17 *
18 * ndn-tools is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
19 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
20 * PURPOSE. See the GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License along with
23 * ndn-tools, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
24 */
25/**
Junxiao Shi2ac79d92015-03-23 11:16:18 -070026 * Copyright (c) 2014, Regents of the University of California,
27 * Arizona Board of Regents,
28 * Colorado State University,
29 * University Pierre & Marie Curie, Sorbonne University,
30 * Washington University in St. Louis,
31 * Beijing Institute of Technology,
32 * The University of Memphis
33 *
34 * This file is part of NFD (Named Data Networking Forwarding Daemon).
35 * See AUTHORS.md for complete list of NFD authors and contributors.
36 *
37 * NFD is free software: you can redistribute it and/or modify it under the terms
38 * of the GNU General Public License as published by the Free Software Foundation,
39 * either version 3 of the License, or (at your option) any later version.
40 *
41 * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
42 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
43 * PURPOSE. See the GNU General Public License for more details.
44 *
45 * You should have received a copy of the GNU General Public License along with
46 * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
47 *
48 * @author Jerald Paul Abraham <jeraldabraham@email.arizona.edu>
49 */
50
Junxiao Shi1753afb2015-04-17 20:59:50 -070051#include "core/version.hpp"
Teng Liangd4ab87f2016-03-08 18:50:47 -070052#include "core/common.hpp"
53
54#include <ndn-cxx/util/io.hpp>
Junxiao Shi2ac79d92015-03-23 11:16:18 -070055
Junxiao Shi0c75f992015-03-24 21:39:47 -070056namespace ndn {
57namespace peek {
Junxiao Shi2ac79d92015-03-23 11:16:18 -070058
Junxiao Shi0c75f992015-03-24 21:39:47 -070059class NdnPeek : boost::noncopyable
Junxiao Shi2ac79d92015-03-23 11:16:18 -070060{
61public:
62 explicit
Junxiao Shi0c75f992015-03-24 21:39:47 -070063 NdnPeek(char* programName)
Teng Liangd4ab87f2016-03-08 18:50:47 -070064 : isVerbose(false)
65 , mustBeFresh(false)
66 , wantRightmostChild(false)
67 , wantPayloadOnly(false)
68 , m_programName(programName)
Junxiao Shi2ac79d92015-03-23 11:16:18 -070069 , m_minSuffixComponents(-1)
70 , m_maxSuffixComponents(-1)
71 , m_interestLifetime(-1)
Junxiao Shi2ac79d92015-03-23 11:16:18 -070072 , m_timeout(-1)
73 , m_prefixName("")
Teng Liangd4ab87f2016-03-08 18:50:47 -070074 , m_didReceiveData(false)
Teng Liang2864b9d2016-03-17 11:17:38 -070075 , m_didReceiveNack(false)
Junxiao Shi2ac79d92015-03-23 11:16:18 -070076 {
77 }
78
79 void
Teng Liangd4ab87f2016-03-08 18:50:47 -070080 usage(std::ostream& os ,const boost::program_options::options_description& options) const
Junxiao Shi2ac79d92015-03-23 11:16:18 -070081 {
Teng Liangd4ab87f2016-03-08 18:50:47 -070082 os << "Usage: " << m_programName << " [options] ndn:/name\n"
83 " Fetch one data item matching the name prefix and write it to standard output\n"
84 "\n"
85 << options;
Junxiao Shi2ac79d92015-03-23 11:16:18 -070086 }
87
88 void
89 setMinSuffixComponents(int minSuffixComponents)
90 {
91 if (minSuffixComponents < 0)
Teng Liangd4ab87f2016-03-08 18:50:47 -070092 throw std::out_of_range("'minSuffixComponents' must be a non-negative integer");
Junxiao Shi0c75f992015-03-24 21:39:47 -070093
Junxiao Shi2ac79d92015-03-23 11:16:18 -070094 m_minSuffixComponents = minSuffixComponents;
95 }
96
97 void
98 setMaxSuffixComponents(int maxSuffixComponents)
99 {
100 if (maxSuffixComponents < 0)
Teng Liangd4ab87f2016-03-08 18:50:47 -0700101 throw std::out_of_range("'maxSuffixComponents' must be a non-negative integer");
Junxiao Shi0c75f992015-03-24 21:39:47 -0700102
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700103 m_maxSuffixComponents = maxSuffixComponents;
104 }
105
106 void
107 setInterestLifetime(int interestLifetime)
108 {
109 if (interestLifetime < 0)
Teng Liangd4ab87f2016-03-08 18:50:47 -0700110 throw std::out_of_range("'lifetime' must be a non-negative integer");
Junxiao Shi0c75f992015-03-24 21:39:47 -0700111
112 m_interestLifetime = time::milliseconds(interestLifetime);
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700113 }
114
115 void
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700116 setTimeout(int timeout)
117 {
118 if (timeout < 0)
Teng Liangd4ab87f2016-03-08 18:50:47 -0700119 throw std::out_of_range("'timeout' must be a non-negative integer");
Junxiao Shi0c75f992015-03-24 21:39:47 -0700120
121 m_timeout = time::milliseconds(timeout);
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700122 }
123
124 void
Teng Liang799390a2016-03-12 23:14:29 -0700125 setLink(const std::string& file)
126 {
127 m_link = io::load<Link>(file);
128 if (m_link == nullptr)
129 throw std::runtime_error(file + " is either nonreadable or nonparseable");
130 }
131
132 void
Teng Liangd4ab87f2016-03-08 18:50:47 -0700133 setPrefixName(const std::string& prefixName)
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700134 {
135 m_prefixName = prefixName;
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700136 }
137
Junxiao Shi0c75f992015-03-24 21:39:47 -0700138 time::milliseconds
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700139 getDefaultInterestLifetime()
140 {
Junxiao Shi0c75f992015-03-24 21:39:47 -0700141 return time::seconds(4);
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700142 }
143
Junxiao Shi0c75f992015-03-24 21:39:47 -0700144 Interest
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700145 createInterestPacket()
146 {
Junxiao Shi0c75f992015-03-24 21:39:47 -0700147 Name interestName(m_prefixName);
148 Interest interestPacket(interestName);
149
Teng Liangd4ab87f2016-03-08 18:50:47 -0700150 if (mustBeFresh)
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700151 interestPacket.setMustBeFresh(true);
Junxiao Shi0c75f992015-03-24 21:39:47 -0700152
Teng Liangd4ab87f2016-03-08 18:50:47 -0700153 if (wantRightmostChild)
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700154 interestPacket.setChildSelector(1);
Junxiao Shi0c75f992015-03-24 21:39:47 -0700155
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700156 if (m_minSuffixComponents >= 0)
157 interestPacket.setMinSuffixComponents(m_minSuffixComponents);
Junxiao Shi0c75f992015-03-24 21:39:47 -0700158
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700159 if (m_maxSuffixComponents >= 0)
160 interestPacket.setMaxSuffixComponents(m_maxSuffixComponents);
Junxiao Shi0c75f992015-03-24 21:39:47 -0700161
162 if (m_interestLifetime < time::milliseconds::zero())
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700163 interestPacket.setInterestLifetime(getDefaultInterestLifetime());
164 else
165 interestPacket.setInterestLifetime(m_interestLifetime);
Junxiao Shi0c75f992015-03-24 21:39:47 -0700166
Teng Liang799390a2016-03-12 23:14:29 -0700167 if (m_link != nullptr)
168 interestPacket.setLink(m_link->wireEncode());
169
Teng Liangd4ab87f2016-03-08 18:50:47 -0700170 if (isVerbose) {
Junxiao Shie02fb522015-10-18 08:45:09 -0700171 std::cerr << "INTEREST: " << interestPacket << std::endl;
172 }
173
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700174 return interestPacket;
175 }
176
177 void
Teng Liang2864b9d2016-03-17 11:17:38 -0700178 onData(const Interest& interest, const Data& data)
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700179 {
Teng Liangd4ab87f2016-03-08 18:50:47 -0700180 m_didReceiveData = true;
Junxiao Shie02fb522015-10-18 08:45:09 -0700181
Teng Liangd4ab87f2016-03-08 18:50:47 -0700182 if (isVerbose) {
Junxiao Shie02fb522015-10-18 08:45:09 -0700183 std::cerr << "DATA, RTT: "
184 << time::duration_cast<time::milliseconds>(time::steady_clock::now() - m_expressInterestTime).count()
185 << "ms" << std::endl;
186 }
187
Teng Liangd4ab87f2016-03-08 18:50:47 -0700188 if (wantPayloadOnly) {
Junxiao Shi0c75f992015-03-24 21:39:47 -0700189 const Block& block = data.getContent();
190 std::cout.write(reinterpret_cast<const char*>(block.value()), block.value_size());
191 }
192 else {
193 const Block& block = data.wireEncode();
194 std::cout.write(reinterpret_cast<const char*>(block.wire()), block.size());
195 }
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700196 }
197
198 void
Teng Liang2864b9d2016-03-17 11:17:38 -0700199 onNack(const Interest& interest, const lp::Nack& nack)
200 {
201 m_didReceiveNack = true;
202 lp::NackHeader header = nack.getHeader();
203
204 if (isVerbose) {
205 std::cerr << "NACK, RTT: "
206 << time::duration_cast<time::milliseconds>(time::steady_clock::now() - m_expressInterestTime).count()
207 << "ms" << std::endl;
208 }
209
210 if (wantPayloadOnly) {
211 std::cout << header.getReason() << std::endl;
212 }
213 else {
214 const Block& block = header.wireEncode();
215 std::cout.write(reinterpret_cast<const char*>(block.wire()), block.size());
216 }
217 }
218
219 void
Junxiao Shi0c75f992015-03-24 21:39:47 -0700220 onTimeout(const Interest& interest)
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700221 {
222 }
223
Teng Liangd4ab87f2016-03-08 18:50:47 -0700224 int
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700225 run()
226 {
Junxiao Shi0c75f992015-03-24 21:39:47 -0700227 try {
228 m_face.expressInterest(createInterestPacket(),
229 bind(&NdnPeek::onData, this, _1, _2),
Teng Liang2864b9d2016-03-17 11:17:38 -0700230 bind(&NdnPeek::onNack, this, _1, _2),
Junxiao Shi0c75f992015-03-24 21:39:47 -0700231 bind(&NdnPeek::onTimeout, this, _1));
Junxiao Shie02fb522015-10-18 08:45:09 -0700232 m_expressInterestTime = time::steady_clock::now();
Junxiao Shi0c75f992015-03-24 21:39:47 -0700233 if (m_timeout < time::milliseconds::zero()) {
Junxiao Shie02fb522015-10-18 08:45:09 -0700234 m_timeout = m_interestLifetime < time::milliseconds::zero() ?
235 getDefaultInterestLifetime() : m_interestLifetime;
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700236 }
Junxiao Shie02fb522015-10-18 08:45:09 -0700237 m_face.processEvents(m_timeout);
Junxiao Shi0c75f992015-03-24 21:39:47 -0700238 }
Teng Liangd4ab87f2016-03-08 18:50:47 -0700239 catch (const std::exception& e) {
Junxiao Shie02fb522015-10-18 08:45:09 -0700240 std::cerr << "ERROR: " << e.what() << std::endl;
Teng Liangd4ab87f2016-03-08 18:50:47 -0700241 return 1;
Junxiao Shi0c75f992015-03-24 21:39:47 -0700242 }
Teng Liangd4ab87f2016-03-08 18:50:47 -0700243
Teng Liang2864b9d2016-03-17 11:17:38 -0700244 if (m_didReceiveNack)
245 return 4;
246
Teng Liangd4ab87f2016-03-08 18:50:47 -0700247 if (isVerbose && !m_didReceiveData) {
Junxiao Shie02fb522015-10-18 08:45:09 -0700248 std::cerr << "TIMEOUT" << std::endl;
Teng Liangd4ab87f2016-03-08 18:50:47 -0700249 return 3;
Junxiao Shie02fb522015-10-18 08:45:09 -0700250 }
Teng Liangd4ab87f2016-03-08 18:50:47 -0700251
Teng Liang2864b9d2016-03-17 11:17:38 -0700252 if (!m_didReceiveData)
253 return 3;
254
Teng Liangd4ab87f2016-03-08 18:50:47 -0700255 return 0;
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700256 }
257
Teng Liangd4ab87f2016-03-08 18:50:47 -0700258public:
259 bool isVerbose;
260 bool mustBeFresh;
261 bool wantRightmostChild;
262 bool wantPayloadOnly;
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700263
264private:
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700265 std::string m_programName;
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700266 int m_minSuffixComponents;
267 int m_maxSuffixComponents;
Junxiao Shi0c75f992015-03-24 21:39:47 -0700268 time::milliseconds m_interestLifetime;
Junxiao Shi0c75f992015-03-24 21:39:47 -0700269 time::milliseconds m_timeout;
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700270 std::string m_prefixName;
Junxiao Shie02fb522015-10-18 08:45:09 -0700271 time::steady_clock::TimePoint m_expressInterestTime;
Teng Liang799390a2016-03-12 23:14:29 -0700272 shared_ptr<Link> m_link;
Teng Liangd4ab87f2016-03-08 18:50:47 -0700273 bool m_didReceiveData;
Teng Liang2864b9d2016-03-17 11:17:38 -0700274 bool m_didReceiveNack;
Junxiao Shi0c75f992015-03-24 21:39:47 -0700275 Face m_face;
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700276};
277
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700278int
279main(int argc, char* argv[])
280{
Junxiao Shi0c75f992015-03-24 21:39:47 -0700281 NdnPeek program(argv[0]);
Teng Liangd4ab87f2016-03-08 18:50:47 -0700282
283 namespace po = boost::program_options;
284
285 po::options_description visibleOptDesc("Allowed options");
286 visibleOptDesc.add_options()
287 ("help,h", "print help and exit")
288 ("version,V", "print version and exit")
289 ("fresh,f", po::bool_switch(&program.mustBeFresh),
290 "set MustBeFresh")
291 ("rightmost,r", po::bool_switch(&program.wantRightmostChild),
292 "set ChildSelector to rightmost")
293 ("minsuffix,m", po::value<int>()->notifier(bind(&NdnPeek::setMinSuffixComponents, &program, _1)),
294 "set MinSuffixComponents")
295 ("maxsuffix,M", po::value<int>()->notifier(bind(&NdnPeek::setMaxSuffixComponents, &program, _1)),
296 "set MaxSuffixComponents")
297 ("lifetime,l", po::value<int>()->notifier(bind(&NdnPeek::setInterestLifetime, &program, _1)),
298 "set InterestLifetime (in milliseconds)")
299 ("payload,p", po::bool_switch(&program.wantPayloadOnly),
300 "print payload only, instead of full packet")
301 ("timeout,w", po::value<int>()->notifier(bind(&NdnPeek::setTimeout, &program, _1)),
302 "set timeout (in milliseconds)")
303 ("verbose,v", po::bool_switch(&program.isVerbose),
304 "turn on verbose output")
Teng Liang799390a2016-03-12 23:14:29 -0700305 ("link-file", po::value<std::string>()->notifier(bind(&NdnPeek::setLink, &program, _1)),
306 "set Link from a file")
Teng Liangd4ab87f2016-03-08 18:50:47 -0700307 ;
308
309 po::options_description hiddenOptDesc("Hidden options");
310 hiddenOptDesc.add_options()
311 ("prefix", po::value<std::string>(), "Interest name");
312
313 po::options_description optDesc("Allowed options");
314 optDesc.add(visibleOptDesc).add(hiddenOptDesc);
315
316 po::positional_options_description optPos;
317 optPos.add("prefix", -1);
318
319 try {
320 po::variables_map vm;
321 po::store(po::command_line_parser(argc, argv).options(optDesc).positional(optPos).run(), vm);
322 po::notify(vm);
323
324 if (vm.count("help") > 0) {
325 program.usage(std::cout, visibleOptDesc);
326 return 0;
327 }
328
329 if (vm.count("version") > 0) {
Junxiao Shi1753afb2015-04-17 20:59:50 -0700330 std::cout << "ndnpeek " << tools::VERSION << std::endl;
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700331 return 0;
Teng Liangd4ab87f2016-03-08 18:50:47 -0700332 }
333
334 if (vm.count("prefix") > 0) {
335 std::string prefixName = vm["prefix"].as<std::string>();
336 program.setPrefixName(prefixName);
337 }
338 else {
339 throw std::runtime_error("Required argument 'prefix' is missing");
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700340 }
341 }
Teng Liangd4ab87f2016-03-08 18:50:47 -0700342 catch (const std::exception& e) {
343 std::cerr << "ERROR: " << e.what() << std::endl;
344 program.usage(std::cerr, visibleOptDesc);
345 return 2;
346 }
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700347
Teng Liangd4ab87f2016-03-08 18:50:47 -0700348 return program.run();
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700349}
Junxiao Shi0c75f992015-03-24 21:39:47 -0700350
351} // namespace peek
352} // namespace ndn
353
354int
355main(int argc, char** argv)
356{
357 return ndn::peek::main(argc, argv);
358}