blob: 2f3bef538fa8bdbbea079e4ac1a7b4a4985f7fa6 [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 Liangd4ab87f2016-03-08 18:50:47 -0700124 setPrefixName(const std::string& prefixName)
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700125 {
126 m_prefixName = prefixName;
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700127 }
128
Junxiao Shi0c75f992015-03-24 21:39:47 -0700129 time::milliseconds
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700130 getDefaultInterestLifetime()
131 {
Junxiao Shi0c75f992015-03-24 21:39:47 -0700132 return time::seconds(4);
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700133 }
134
Junxiao Shi0c75f992015-03-24 21:39:47 -0700135 Interest
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700136 createInterestPacket()
137 {
Junxiao Shi0c75f992015-03-24 21:39:47 -0700138 Name interestName(m_prefixName);
139 Interest interestPacket(interestName);
140
Teng Liangd4ab87f2016-03-08 18:50:47 -0700141 if (mustBeFresh)
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700142 interestPacket.setMustBeFresh(true);
Junxiao Shi0c75f992015-03-24 21:39:47 -0700143
Teng Liangd4ab87f2016-03-08 18:50:47 -0700144 if (wantRightmostChild)
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700145 interestPacket.setChildSelector(1);
Junxiao Shi0c75f992015-03-24 21:39:47 -0700146
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700147 if (m_minSuffixComponents >= 0)
148 interestPacket.setMinSuffixComponents(m_minSuffixComponents);
Junxiao Shi0c75f992015-03-24 21:39:47 -0700149
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700150 if (m_maxSuffixComponents >= 0)
151 interestPacket.setMaxSuffixComponents(m_maxSuffixComponents);
Junxiao Shi0c75f992015-03-24 21:39:47 -0700152
153 if (m_interestLifetime < time::milliseconds::zero())
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700154 interestPacket.setInterestLifetime(getDefaultInterestLifetime());
155 else
156 interestPacket.setInterestLifetime(m_interestLifetime);
Junxiao Shi0c75f992015-03-24 21:39:47 -0700157
Teng Liangd4ab87f2016-03-08 18:50:47 -0700158 if (isVerbose) {
Junxiao Shie02fb522015-10-18 08:45:09 -0700159 std::cerr << "INTEREST: " << interestPacket << std::endl;
160 }
161
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700162 return interestPacket;
163 }
164
165 void
Junxiao Shi0c75f992015-03-24 21:39:47 -0700166 onData(const Interest& interest, Data& data)
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700167 {
Teng Liangd4ab87f2016-03-08 18:50:47 -0700168 m_didReceiveData = true;
Junxiao Shie02fb522015-10-18 08:45:09 -0700169
Teng Liangd4ab87f2016-03-08 18:50:47 -0700170 if (isVerbose) {
Junxiao Shie02fb522015-10-18 08:45:09 -0700171 std::cerr << "DATA, RTT: "
172 << time::duration_cast<time::milliseconds>(time::steady_clock::now() - m_expressInterestTime).count()
173 << "ms" << std::endl;
174 }
175
Teng Liangd4ab87f2016-03-08 18:50:47 -0700176 if (wantPayloadOnly) {
Junxiao Shi0c75f992015-03-24 21:39:47 -0700177 const Block& block = data.getContent();
178 std::cout.write(reinterpret_cast<const char*>(block.value()), block.value_size());
179 }
180 else {
181 const Block& block = data.wireEncode();
182 std::cout.write(reinterpret_cast<const char*>(block.wire()), block.size());
183 }
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700184 }
185
186 void
Junxiao Shi0c75f992015-03-24 21:39:47 -0700187 onTimeout(const Interest& interest)
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700188 {
189 }
190
Teng Liangd4ab87f2016-03-08 18:50:47 -0700191 int
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700192 run()
193 {
Junxiao Shi0c75f992015-03-24 21:39:47 -0700194 try {
195 m_face.expressInterest(createInterestPacket(),
196 bind(&NdnPeek::onData, this, _1, _2),
197 bind(&NdnPeek::onTimeout, this, _1));
Junxiao Shie02fb522015-10-18 08:45:09 -0700198 m_expressInterestTime = time::steady_clock::now();
Junxiao Shi0c75f992015-03-24 21:39:47 -0700199 if (m_timeout < time::milliseconds::zero()) {
Junxiao Shie02fb522015-10-18 08:45:09 -0700200 m_timeout = m_interestLifetime < time::milliseconds::zero() ?
201 getDefaultInterestLifetime() : m_interestLifetime;
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700202 }
Junxiao Shie02fb522015-10-18 08:45:09 -0700203 m_face.processEvents(m_timeout);
Junxiao Shi0c75f992015-03-24 21:39:47 -0700204 }
Teng Liangd4ab87f2016-03-08 18:50:47 -0700205 catch (const std::exception& e) {
Junxiao Shie02fb522015-10-18 08:45:09 -0700206 std::cerr << "ERROR: " << e.what() << std::endl;
Teng Liangd4ab87f2016-03-08 18:50:47 -0700207 return 1;
Junxiao Shi0c75f992015-03-24 21:39:47 -0700208 }
Teng Liangd4ab87f2016-03-08 18:50:47 -0700209
210 if (isVerbose && !m_didReceiveData) {
Junxiao Shie02fb522015-10-18 08:45:09 -0700211 std::cerr << "TIMEOUT" << std::endl;
Teng Liangd4ab87f2016-03-08 18:50:47 -0700212 return 3;
Junxiao Shie02fb522015-10-18 08:45:09 -0700213 }
Teng Liangd4ab87f2016-03-08 18:50:47 -0700214
215 return 0;
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700216 }
217
Teng Liangd4ab87f2016-03-08 18:50:47 -0700218public:
219 bool isVerbose;
220 bool mustBeFresh;
221 bool wantRightmostChild;
222 bool wantPayloadOnly;
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700223
224private:
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700225 std::string m_programName;
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700226 int m_minSuffixComponents;
227 int m_maxSuffixComponents;
Junxiao Shi0c75f992015-03-24 21:39:47 -0700228 time::milliseconds m_interestLifetime;
Junxiao Shi0c75f992015-03-24 21:39:47 -0700229 time::milliseconds m_timeout;
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700230 std::string m_prefixName;
Junxiao Shie02fb522015-10-18 08:45:09 -0700231 time::steady_clock::TimePoint m_expressInterestTime;
Teng Liangd4ab87f2016-03-08 18:50:47 -0700232 bool m_didReceiveData;
Junxiao Shi0c75f992015-03-24 21:39:47 -0700233 Face m_face;
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700234};
235
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700236int
237main(int argc, char* argv[])
238{
Junxiao Shi0c75f992015-03-24 21:39:47 -0700239 NdnPeek program(argv[0]);
Teng Liangd4ab87f2016-03-08 18:50:47 -0700240
241 namespace po = boost::program_options;
242
243 po::options_description visibleOptDesc("Allowed options");
244 visibleOptDesc.add_options()
245 ("help,h", "print help and exit")
246 ("version,V", "print version and exit")
247 ("fresh,f", po::bool_switch(&program.mustBeFresh),
248 "set MustBeFresh")
249 ("rightmost,r", po::bool_switch(&program.wantRightmostChild),
250 "set ChildSelector to rightmost")
251 ("minsuffix,m", po::value<int>()->notifier(bind(&NdnPeek::setMinSuffixComponents, &program, _1)),
252 "set MinSuffixComponents")
253 ("maxsuffix,M", po::value<int>()->notifier(bind(&NdnPeek::setMaxSuffixComponents, &program, _1)),
254 "set MaxSuffixComponents")
255 ("lifetime,l", po::value<int>()->notifier(bind(&NdnPeek::setInterestLifetime, &program, _1)),
256 "set InterestLifetime (in milliseconds)")
257 ("payload,p", po::bool_switch(&program.wantPayloadOnly),
258 "print payload only, instead of full packet")
259 ("timeout,w", po::value<int>()->notifier(bind(&NdnPeek::setTimeout, &program, _1)),
260 "set timeout (in milliseconds)")
261 ("verbose,v", po::bool_switch(&program.isVerbose),
262 "turn on verbose output")
263 ;
264
265 po::options_description hiddenOptDesc("Hidden options");
266 hiddenOptDesc.add_options()
267 ("prefix", po::value<std::string>(), "Interest name");
268
269 po::options_description optDesc("Allowed options");
270 optDesc.add(visibleOptDesc).add(hiddenOptDesc);
271
272 po::positional_options_description optPos;
273 optPos.add("prefix", -1);
274
275 try {
276 po::variables_map vm;
277 po::store(po::command_line_parser(argc, argv).options(optDesc).positional(optPos).run(), vm);
278 po::notify(vm);
279
280 if (vm.count("help") > 0) {
281 program.usage(std::cout, visibleOptDesc);
282 return 0;
283 }
284
285 if (vm.count("version") > 0) {
Junxiao Shi1753afb2015-04-17 20:59:50 -0700286 std::cout << "ndnpeek " << tools::VERSION << std::endl;
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700287 return 0;
Teng Liangd4ab87f2016-03-08 18:50:47 -0700288 }
289
290 if (vm.count("prefix") > 0) {
291 std::string prefixName = vm["prefix"].as<std::string>();
292 program.setPrefixName(prefixName);
293 }
294 else {
295 throw std::runtime_error("Required argument 'prefix' is missing");
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700296 }
297 }
Teng Liangd4ab87f2016-03-08 18:50:47 -0700298 catch (const std::exception& e) {
299 std::cerr << "ERROR: " << e.what() << std::endl;
300 program.usage(std::cerr, visibleOptDesc);
301 return 2;
302 }
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700303
Teng Liangd4ab87f2016-03-08 18:50:47 -0700304 return program.run();
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700305}
Junxiao Shi0c75f992015-03-24 21:39:47 -0700306
307} // namespace peek
308} // namespace ndn
309
310int
311main(int argc, char** argv)
312{
313 return ndn::peek::main(argc, argv);
314}