blob: 5c9df4d180ed9d7ec7f725c0a2858a42a97b2621 [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)
Junxiao Shi2ac79d92015-03-23 11:16:18 -070075 {
76 }
77
78 void
Teng Liangd4ab87f2016-03-08 18:50:47 -070079 usage(std::ostream& os ,const boost::program_options::options_description& options) const
Junxiao Shi2ac79d92015-03-23 11:16:18 -070080 {
Teng Liangd4ab87f2016-03-08 18:50:47 -070081 os << "Usage: " << m_programName << " [options] ndn:/name\n"
82 " Fetch one data item matching the name prefix and write it to standard output\n"
83 "\n"
84 << options;
Junxiao Shi2ac79d92015-03-23 11:16:18 -070085 }
86
87 void
88 setMinSuffixComponents(int minSuffixComponents)
89 {
90 if (minSuffixComponents < 0)
Teng Liangd4ab87f2016-03-08 18:50:47 -070091 throw std::out_of_range("'minSuffixComponents' must be a non-negative integer");
Junxiao Shi0c75f992015-03-24 21:39:47 -070092
Junxiao Shi2ac79d92015-03-23 11:16:18 -070093 m_minSuffixComponents = minSuffixComponents;
94 }
95
96 void
97 setMaxSuffixComponents(int maxSuffixComponents)
98 {
99 if (maxSuffixComponents < 0)
Teng Liangd4ab87f2016-03-08 18:50:47 -0700100 throw std::out_of_range("'maxSuffixComponents' must be a non-negative integer");
Junxiao Shi0c75f992015-03-24 21:39:47 -0700101
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700102 m_maxSuffixComponents = maxSuffixComponents;
103 }
104
105 void
106 setInterestLifetime(int interestLifetime)
107 {
108 if (interestLifetime < 0)
Teng Liangd4ab87f2016-03-08 18:50:47 -0700109 throw std::out_of_range("'lifetime' must be a non-negative integer");
Junxiao Shi0c75f992015-03-24 21:39:47 -0700110
111 m_interestLifetime = time::milliseconds(interestLifetime);
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700112 }
113
114 void
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700115 setTimeout(int timeout)
116 {
117 if (timeout < 0)
Teng Liangd4ab87f2016-03-08 18:50:47 -0700118 throw std::out_of_range("'timeout' must be a non-negative integer");
Junxiao Shi0c75f992015-03-24 21:39:47 -0700119
120 m_timeout = time::milliseconds(timeout);
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700121 }
122
123 void
Teng Liang799390a2016-03-12 23:14:29 -0700124 setLink(const std::string& file)
125 {
126 m_link = io::load<Link>(file);
127 if (m_link == nullptr)
128 throw std::runtime_error(file + " is either nonreadable or nonparseable");
129 }
130
131 void
Teng Liangd4ab87f2016-03-08 18:50:47 -0700132 setPrefixName(const std::string& prefixName)
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700133 {
134 m_prefixName = prefixName;
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700135 }
136
Junxiao Shi0c75f992015-03-24 21:39:47 -0700137 time::milliseconds
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700138 getDefaultInterestLifetime()
139 {
Junxiao Shi0c75f992015-03-24 21:39:47 -0700140 return time::seconds(4);
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700141 }
142
Junxiao Shi0c75f992015-03-24 21:39:47 -0700143 Interest
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700144 createInterestPacket()
145 {
Junxiao Shi0c75f992015-03-24 21:39:47 -0700146 Name interestName(m_prefixName);
147 Interest interestPacket(interestName);
148
Teng Liangd4ab87f2016-03-08 18:50:47 -0700149 if (mustBeFresh)
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700150 interestPacket.setMustBeFresh(true);
Junxiao Shi0c75f992015-03-24 21:39:47 -0700151
Teng Liangd4ab87f2016-03-08 18:50:47 -0700152 if (wantRightmostChild)
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700153 interestPacket.setChildSelector(1);
Junxiao Shi0c75f992015-03-24 21:39:47 -0700154
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700155 if (m_minSuffixComponents >= 0)
156 interestPacket.setMinSuffixComponents(m_minSuffixComponents);
Junxiao Shi0c75f992015-03-24 21:39:47 -0700157
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700158 if (m_maxSuffixComponents >= 0)
159 interestPacket.setMaxSuffixComponents(m_maxSuffixComponents);
Junxiao Shi0c75f992015-03-24 21:39:47 -0700160
161 if (m_interestLifetime < time::milliseconds::zero())
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700162 interestPacket.setInterestLifetime(getDefaultInterestLifetime());
163 else
164 interestPacket.setInterestLifetime(m_interestLifetime);
Junxiao Shi0c75f992015-03-24 21:39:47 -0700165
Teng Liang799390a2016-03-12 23:14:29 -0700166 if (m_link != nullptr)
167 interestPacket.setLink(m_link->wireEncode());
168
Teng Liangd4ab87f2016-03-08 18:50:47 -0700169 if (isVerbose) {
Junxiao Shie02fb522015-10-18 08:45:09 -0700170 std::cerr << "INTEREST: " << interestPacket << std::endl;
171 }
172
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700173 return interestPacket;
174 }
175
176 void
Junxiao Shi0c75f992015-03-24 21:39:47 -0700177 onData(const Interest& interest, Data& data)
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700178 {
Teng Liangd4ab87f2016-03-08 18:50:47 -0700179 m_didReceiveData = true;
Junxiao Shie02fb522015-10-18 08:45:09 -0700180
Teng Liangd4ab87f2016-03-08 18:50:47 -0700181 if (isVerbose) {
Junxiao Shie02fb522015-10-18 08:45:09 -0700182 std::cerr << "DATA, RTT: "
183 << time::duration_cast<time::milliseconds>(time::steady_clock::now() - m_expressInterestTime).count()
184 << "ms" << std::endl;
185 }
186
Teng Liangd4ab87f2016-03-08 18:50:47 -0700187 if (wantPayloadOnly) {
Junxiao Shi0c75f992015-03-24 21:39:47 -0700188 const Block& block = data.getContent();
189 std::cout.write(reinterpret_cast<const char*>(block.value()), block.value_size());
190 }
191 else {
192 const Block& block = data.wireEncode();
193 std::cout.write(reinterpret_cast<const char*>(block.wire()), block.size());
194 }
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700195 }
196
197 void
Junxiao Shi0c75f992015-03-24 21:39:47 -0700198 onTimeout(const Interest& interest)
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700199 {
200 }
201
Teng Liangd4ab87f2016-03-08 18:50:47 -0700202 int
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700203 run()
204 {
Junxiao Shi0c75f992015-03-24 21:39:47 -0700205 try {
206 m_face.expressInterest(createInterestPacket(),
207 bind(&NdnPeek::onData, this, _1, _2),
208 bind(&NdnPeek::onTimeout, this, _1));
Junxiao Shie02fb522015-10-18 08:45:09 -0700209 m_expressInterestTime = time::steady_clock::now();
Junxiao Shi0c75f992015-03-24 21:39:47 -0700210 if (m_timeout < time::milliseconds::zero()) {
Junxiao Shie02fb522015-10-18 08:45:09 -0700211 m_timeout = m_interestLifetime < time::milliseconds::zero() ?
212 getDefaultInterestLifetime() : m_interestLifetime;
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700213 }
Junxiao Shie02fb522015-10-18 08:45:09 -0700214 m_face.processEvents(m_timeout);
Junxiao Shi0c75f992015-03-24 21:39:47 -0700215 }
Teng Liangd4ab87f2016-03-08 18:50:47 -0700216 catch (const std::exception& e) {
Junxiao Shie02fb522015-10-18 08:45:09 -0700217 std::cerr << "ERROR: " << e.what() << std::endl;
Teng Liangd4ab87f2016-03-08 18:50:47 -0700218 return 1;
Junxiao Shi0c75f992015-03-24 21:39:47 -0700219 }
Teng Liangd4ab87f2016-03-08 18:50:47 -0700220
221 if (isVerbose && !m_didReceiveData) {
Junxiao Shie02fb522015-10-18 08:45:09 -0700222 std::cerr << "TIMEOUT" << std::endl;
Teng Liangd4ab87f2016-03-08 18:50:47 -0700223 return 3;
Junxiao Shie02fb522015-10-18 08:45:09 -0700224 }
Teng Liangd4ab87f2016-03-08 18:50:47 -0700225
226 return 0;
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700227 }
228
Teng Liangd4ab87f2016-03-08 18:50:47 -0700229public:
230 bool isVerbose;
231 bool mustBeFresh;
232 bool wantRightmostChild;
233 bool wantPayloadOnly;
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700234
235private:
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700236 std::string m_programName;
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700237 int m_minSuffixComponents;
238 int m_maxSuffixComponents;
Junxiao Shi0c75f992015-03-24 21:39:47 -0700239 time::milliseconds m_interestLifetime;
Junxiao Shi0c75f992015-03-24 21:39:47 -0700240 time::milliseconds m_timeout;
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700241 std::string m_prefixName;
Junxiao Shie02fb522015-10-18 08:45:09 -0700242 time::steady_clock::TimePoint m_expressInterestTime;
Teng Liang799390a2016-03-12 23:14:29 -0700243 shared_ptr<Link> m_link;
Teng Liangd4ab87f2016-03-08 18:50:47 -0700244 bool m_didReceiveData;
Junxiao Shi0c75f992015-03-24 21:39:47 -0700245 Face m_face;
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700246};
247
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700248int
249main(int argc, char* argv[])
250{
Junxiao Shi0c75f992015-03-24 21:39:47 -0700251 NdnPeek program(argv[0]);
Teng Liangd4ab87f2016-03-08 18:50:47 -0700252
253 namespace po = boost::program_options;
254
255 po::options_description visibleOptDesc("Allowed options");
256 visibleOptDesc.add_options()
257 ("help,h", "print help and exit")
258 ("version,V", "print version and exit")
259 ("fresh,f", po::bool_switch(&program.mustBeFresh),
260 "set MustBeFresh")
261 ("rightmost,r", po::bool_switch(&program.wantRightmostChild),
262 "set ChildSelector to rightmost")
263 ("minsuffix,m", po::value<int>()->notifier(bind(&NdnPeek::setMinSuffixComponents, &program, _1)),
264 "set MinSuffixComponents")
265 ("maxsuffix,M", po::value<int>()->notifier(bind(&NdnPeek::setMaxSuffixComponents, &program, _1)),
266 "set MaxSuffixComponents")
267 ("lifetime,l", po::value<int>()->notifier(bind(&NdnPeek::setInterestLifetime, &program, _1)),
268 "set InterestLifetime (in milliseconds)")
269 ("payload,p", po::bool_switch(&program.wantPayloadOnly),
270 "print payload only, instead of full packet")
271 ("timeout,w", po::value<int>()->notifier(bind(&NdnPeek::setTimeout, &program, _1)),
272 "set timeout (in milliseconds)")
273 ("verbose,v", po::bool_switch(&program.isVerbose),
274 "turn on verbose output")
Teng Liang799390a2016-03-12 23:14:29 -0700275 ("link-file", po::value<std::string>()->notifier(bind(&NdnPeek::setLink, &program, _1)),
276 "set Link from a file")
Teng Liangd4ab87f2016-03-08 18:50:47 -0700277 ;
278
279 po::options_description hiddenOptDesc("Hidden options");
280 hiddenOptDesc.add_options()
281 ("prefix", po::value<std::string>(), "Interest name");
282
283 po::options_description optDesc("Allowed options");
284 optDesc.add(visibleOptDesc).add(hiddenOptDesc);
285
286 po::positional_options_description optPos;
287 optPos.add("prefix", -1);
288
289 try {
290 po::variables_map vm;
291 po::store(po::command_line_parser(argc, argv).options(optDesc).positional(optPos).run(), vm);
292 po::notify(vm);
293
294 if (vm.count("help") > 0) {
295 program.usage(std::cout, visibleOptDesc);
296 return 0;
297 }
298
299 if (vm.count("version") > 0) {
Junxiao Shi1753afb2015-04-17 20:59:50 -0700300 std::cout << "ndnpeek " << tools::VERSION << std::endl;
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700301 return 0;
Teng Liangd4ab87f2016-03-08 18:50:47 -0700302 }
303
304 if (vm.count("prefix") > 0) {
305 std::string prefixName = vm["prefix"].as<std::string>();
306 program.setPrefixName(prefixName);
307 }
308 else {
309 throw std::runtime_error("Required argument 'prefix' is missing");
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700310 }
311 }
Teng Liangd4ab87f2016-03-08 18:50:47 -0700312 catch (const std::exception& e) {
313 std::cerr << "ERROR: " << e.what() << std::endl;
314 program.usage(std::cerr, visibleOptDesc);
315 return 2;
316 }
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700317
Teng Liangd4ab87f2016-03-08 18:50:47 -0700318 return program.run();
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700319}
Junxiao Shi0c75f992015-03-24 21:39:47 -0700320
321} // namespace peek
322} // namespace ndn
323
324int
325main(int argc, char** argv)
326{
327 return ndn::peek::main(argc, argv);
328}