blob: 8f79a616121a69d73ecfeb000890e63861e050ac [file] [log] [blame]
Junxiao Shi2ac79d92015-03-23 11:16:18 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Junxiao Shi1688ded2015-03-29 10:09:26 -07003 * Copyright (c) 2014-2015, Regents of the University of California,
4 * 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
51#include "version.hpp"
52
53#include <boost/noncopyable.hpp>
54
55#include <ndn-cxx/face.hpp>
56
Junxiao Shi0c75f992015-03-24 21:39:47 -070057namespace ndn {
58namespace peek {
Junxiao Shi2ac79d92015-03-23 11:16:18 -070059
Junxiao Shi0c75f992015-03-24 21:39:47 -070060class NdnPeek : boost::noncopyable
Junxiao Shi2ac79d92015-03-23 11:16:18 -070061{
62public:
63 explicit
Junxiao Shi0c75f992015-03-24 21:39:47 -070064 NdnPeek(char* programName)
Junxiao Shi2ac79d92015-03-23 11:16:18 -070065 : m_programName(programName)
66 , m_mustBeFresh(false)
67 , m_isChildSelectorRightmost(false)
68 , m_minSuffixComponents(-1)
69 , m_maxSuffixComponents(-1)
70 , m_interestLifetime(-1)
71 , m_isPayloadOnlySet(false)
72 , m_timeout(-1)
73 , m_prefixName("")
74 , m_isDataReceived(false)
75 {
76 }
77
78 void
79 usage()
80 {
81 std::cout << "\n Usage:\n " << m_programName << " "
82 "[-f] [-r] [-m min] [-M max] [-l lifetime] [-p] [-w timeout] ndn:/name\n"
83 " Get one data item matching the name prefix and write it to stdout\n"
84 " [-f] - set MustBeFresh\n"
85 " [-r] - set ChildSelector to select rightmost child\n"
86 " [-m min] - set MinSuffixComponents\n"
87 " [-M max] - set MaxSuffixComponents\n"
88 " [-l lifetime] - set InterestLifetime in time::milliseconds\n"
89 " [-p] - print payload only, not full packet\n"
90 " [-w timeout] - set Timeout in time::milliseconds\n"
91 " [-h] - print help and exit\n"
92 " [-V] - print version and exit\n"
93 "\n";
94 exit(1);
95 }
96
97 void
98 setMustBeFresh()
99 {
100 m_mustBeFresh = true;
101 }
102
103 void
104 setRightmostChildSelector()
105 {
106 m_isChildSelectorRightmost = true;
107 }
108
109 void
110 setMinSuffixComponents(int minSuffixComponents)
111 {
112 if (minSuffixComponents < 0)
113 usage();
Junxiao Shi0c75f992015-03-24 21:39:47 -0700114
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700115 m_minSuffixComponents = minSuffixComponents;
116 }
117
118 void
119 setMaxSuffixComponents(int maxSuffixComponents)
120 {
121 if (maxSuffixComponents < 0)
122 usage();
Junxiao Shi0c75f992015-03-24 21:39:47 -0700123
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700124 m_maxSuffixComponents = maxSuffixComponents;
125 }
126
127 void
128 setInterestLifetime(int interestLifetime)
129 {
130 if (interestLifetime < 0)
131 usage();
Junxiao Shi0c75f992015-03-24 21:39:47 -0700132
133 m_interestLifetime = time::milliseconds(interestLifetime);
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700134 }
135
136 void
137 setPayloadOnly()
138 {
139 m_isPayloadOnlySet = true;
140 }
141
142 void
143 setTimeout(int timeout)
144 {
145 if (timeout < 0)
146 usage();
Junxiao Shi0c75f992015-03-24 21:39:47 -0700147
148 m_timeout = time::milliseconds(timeout);
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700149 }
150
151 void
152 setPrefixName(char* prefixName)
153 {
154 m_prefixName = prefixName;
Junxiao Shi0c75f992015-03-24 21:39:47 -0700155
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700156 if (m_prefixName.length() == 0)
157 usage();
158 }
159
Junxiao Shi0c75f992015-03-24 21:39:47 -0700160 time::milliseconds
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700161 getDefaultInterestLifetime()
162 {
Junxiao Shi0c75f992015-03-24 21:39:47 -0700163 return time::seconds(4);
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700164 }
165
Junxiao Shi0c75f992015-03-24 21:39:47 -0700166 Interest
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700167 createInterestPacket()
168 {
Junxiao Shi0c75f992015-03-24 21:39:47 -0700169 Name interestName(m_prefixName);
170 Interest interestPacket(interestName);
171
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700172 if (m_mustBeFresh)
173 interestPacket.setMustBeFresh(true);
Junxiao Shi0c75f992015-03-24 21:39:47 -0700174
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700175 if (m_isChildSelectorRightmost)
176 interestPacket.setChildSelector(1);
Junxiao Shi0c75f992015-03-24 21:39:47 -0700177
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700178 if (m_minSuffixComponents >= 0)
179 interestPacket.setMinSuffixComponents(m_minSuffixComponents);
Junxiao Shi0c75f992015-03-24 21:39:47 -0700180
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700181 if (m_maxSuffixComponents >= 0)
182 interestPacket.setMaxSuffixComponents(m_maxSuffixComponents);
Junxiao Shi0c75f992015-03-24 21:39:47 -0700183
184 if (m_interestLifetime < time::milliseconds::zero())
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700185 interestPacket.setInterestLifetime(getDefaultInterestLifetime());
186 else
187 interestPacket.setInterestLifetime(m_interestLifetime);
Junxiao Shi0c75f992015-03-24 21:39:47 -0700188
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700189 return interestPacket;
190 }
191
192 void
Junxiao Shi0c75f992015-03-24 21:39:47 -0700193 onData(const Interest& interest, Data& data)
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700194 {
195 m_isDataReceived = true;
Junxiao Shi0c75f992015-03-24 21:39:47 -0700196 if (m_isPayloadOnlySet) {
197 const Block& block = data.getContent();
198 std::cout.write(reinterpret_cast<const char*>(block.value()), block.value_size());
199 }
200 else {
201 const Block& block = data.wireEncode();
202 std::cout.write(reinterpret_cast<const char*>(block.wire()), block.size());
203 }
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700204 }
205
206 void
Junxiao Shi0c75f992015-03-24 21:39:47 -0700207 onTimeout(const Interest& interest)
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700208 {
209 }
210
211 void
212 run()
213 {
Junxiao Shi0c75f992015-03-24 21:39:47 -0700214 try {
215 m_face.expressInterest(createInterestPacket(),
216 bind(&NdnPeek::onData, this, _1, _2),
217 bind(&NdnPeek::onTimeout, this, _1));
218 if (m_timeout < time::milliseconds::zero()) {
219 if (m_interestLifetime < time::milliseconds::zero())
220 m_face.processEvents(getDefaultInterestLifetime());
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700221 else
Junxiao Shi0c75f992015-03-24 21:39:47 -0700222 m_face.processEvents(m_interestLifetime);
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700223 }
Junxiao Shi0c75f992015-03-24 21:39:47 -0700224 else
225 m_face.processEvents(m_timeout);
226 }
227 catch (std::exception& e) {
228 std::cerr << "ERROR: " << e.what() << "\n" << std::endl;
229 exit(1);
230 }
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700231 }
232
233 bool
234 isDataReceived() const
235 {
236 return m_isDataReceived;
237 }
238
239private:
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700240 std::string m_programName;
241 bool m_mustBeFresh;
242 bool m_isChildSelectorRightmost;
243 int m_minSuffixComponents;
244 int m_maxSuffixComponents;
Junxiao Shi0c75f992015-03-24 21:39:47 -0700245 time::milliseconds m_interestLifetime;
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700246 bool m_isPayloadOnlySet;
Junxiao Shi0c75f992015-03-24 21:39:47 -0700247 time::milliseconds m_timeout;
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700248 std::string m_prefixName;
249 bool m_isDataReceived;
Junxiao Shi0c75f992015-03-24 21:39:47 -0700250 Face m_face;
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700251};
252
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700253int
254main(int argc, char* argv[])
255{
Junxiao Shi0c75f992015-03-24 21:39:47 -0700256 NdnPeek program(argv[0]);
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700257 int option;
258 while ((option = getopt(argc, argv, "hfrm:M:l:pw:V")) != -1) {
259 switch (option) {
260 case 'h':
Junxiao Shi0c75f992015-03-24 21:39:47 -0700261 program.usage();
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700262 break;
263 case 'f':
Junxiao Shi0c75f992015-03-24 21:39:47 -0700264 program.setMustBeFresh();
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700265 break;
266 case 'r':
Junxiao Shi0c75f992015-03-24 21:39:47 -0700267 program.setRightmostChildSelector();
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700268 break;
269 case 'm':
Junxiao Shi0c75f992015-03-24 21:39:47 -0700270 program.setMinSuffixComponents(atoi(optarg));
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700271 break;
272 case 'M':
Junxiao Shi0c75f992015-03-24 21:39:47 -0700273 program.setMaxSuffixComponents(atoi(optarg));
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700274 break;
275 case 'l':
Junxiao Shi0c75f992015-03-24 21:39:47 -0700276 program.setInterestLifetime(atoi(optarg));
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700277 break;
278 case 'p':
Junxiao Shi0c75f992015-03-24 21:39:47 -0700279 program.setPayloadOnly();
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700280 break;
281 case 'w':
Junxiao Shi0c75f992015-03-24 21:39:47 -0700282 program.setTimeout(atoi(optarg));
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700283 break;
284 case 'V':
285 std::cout << NFD_VERSION_BUILD_STRING << std::endl;
286 return 0;
287 default:
Junxiao Shi0c75f992015-03-24 21:39:47 -0700288 program.usage();
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700289 break;
290 }
291 }
292
293 argc -= optind;
294 argv += optind;
295
296 if (argv[0] == 0)
Junxiao Shi0c75f992015-03-24 21:39:47 -0700297 program.usage();
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700298
Junxiao Shi0c75f992015-03-24 21:39:47 -0700299 program.setPrefixName(argv[0]);
300 program.run();
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700301
Junxiao Shi0c75f992015-03-24 21:39:47 -0700302 if (program.isDataReceived())
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700303 return 0;
304 else
305 return 1;
306}
Junxiao Shi0c75f992015-03-24 21:39:47 -0700307
308} // namespace peek
309} // namespace ndn
310
311int
312main(int argc, char** argv)
313{
314 return ndn::peek::main(argc, argv);
315}