blob: 7bc8daed49cd52dbfc0d9df648ce8bf37da547a7 [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
Junxiao Shi1753afb2015-04-17 20:59:50 -070051#include "core/version.hpp"
Junxiao Shi2ac79d92015-03-23 11:16:18 -070052
Junxiao Shi0c75f992015-03-24 21:39:47 -070053namespace ndn {
54namespace peek {
Junxiao Shi2ac79d92015-03-23 11:16:18 -070055
Junxiao Shi0c75f992015-03-24 21:39:47 -070056class NdnPeek : boost::noncopyable
Junxiao Shi2ac79d92015-03-23 11:16:18 -070057{
58public:
59 explicit
Junxiao Shi0c75f992015-03-24 21:39:47 -070060 NdnPeek(char* programName)
Junxiao Shi2ac79d92015-03-23 11:16:18 -070061 : m_programName(programName)
62 , m_mustBeFresh(false)
63 , m_isChildSelectorRightmost(false)
64 , m_minSuffixComponents(-1)
65 , m_maxSuffixComponents(-1)
66 , m_interestLifetime(-1)
67 , m_isPayloadOnlySet(false)
68 , m_timeout(-1)
69 , m_prefixName("")
70 , m_isDataReceived(false)
71 {
72 }
73
74 void
75 usage()
76 {
77 std::cout << "\n Usage:\n " << m_programName << " "
78 "[-f] [-r] [-m min] [-M max] [-l lifetime] [-p] [-w timeout] ndn:/name\n"
79 " Get one data item matching the name prefix and write it to stdout\n"
80 " [-f] - set MustBeFresh\n"
81 " [-r] - set ChildSelector to select rightmost child\n"
82 " [-m min] - set MinSuffixComponents\n"
83 " [-M max] - set MaxSuffixComponents\n"
84 " [-l lifetime] - set InterestLifetime in time::milliseconds\n"
85 " [-p] - print payload only, not full packet\n"
86 " [-w timeout] - set Timeout in time::milliseconds\n"
87 " [-h] - print help and exit\n"
88 " [-V] - print version and exit\n"
89 "\n";
90 exit(1);
91 }
92
93 void
94 setMustBeFresh()
95 {
96 m_mustBeFresh = true;
97 }
98
99 void
100 setRightmostChildSelector()
101 {
102 m_isChildSelectorRightmost = true;
103 }
104
105 void
106 setMinSuffixComponents(int minSuffixComponents)
107 {
108 if (minSuffixComponents < 0)
109 usage();
Junxiao Shi0c75f992015-03-24 21:39:47 -0700110
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700111 m_minSuffixComponents = minSuffixComponents;
112 }
113
114 void
115 setMaxSuffixComponents(int maxSuffixComponents)
116 {
117 if (maxSuffixComponents < 0)
118 usage();
Junxiao Shi0c75f992015-03-24 21:39:47 -0700119
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700120 m_maxSuffixComponents = maxSuffixComponents;
121 }
122
123 void
124 setInterestLifetime(int interestLifetime)
125 {
126 if (interestLifetime < 0)
127 usage();
Junxiao Shi0c75f992015-03-24 21:39:47 -0700128
129 m_interestLifetime = time::milliseconds(interestLifetime);
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700130 }
131
132 void
133 setPayloadOnly()
134 {
135 m_isPayloadOnlySet = true;
136 }
137
138 void
139 setTimeout(int timeout)
140 {
141 if (timeout < 0)
142 usage();
Junxiao Shi0c75f992015-03-24 21:39:47 -0700143
144 m_timeout = time::milliseconds(timeout);
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700145 }
146
147 void
148 setPrefixName(char* prefixName)
149 {
150 m_prefixName = prefixName;
Junxiao Shi0c75f992015-03-24 21:39:47 -0700151
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700152 if (m_prefixName.length() == 0)
153 usage();
154 }
155
Junxiao Shi0c75f992015-03-24 21:39:47 -0700156 time::milliseconds
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700157 getDefaultInterestLifetime()
158 {
Junxiao Shi0c75f992015-03-24 21:39:47 -0700159 return time::seconds(4);
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700160 }
161
Junxiao Shi0c75f992015-03-24 21:39:47 -0700162 Interest
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700163 createInterestPacket()
164 {
Junxiao Shi0c75f992015-03-24 21:39:47 -0700165 Name interestName(m_prefixName);
166 Interest interestPacket(interestName);
167
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700168 if (m_mustBeFresh)
169 interestPacket.setMustBeFresh(true);
Junxiao Shi0c75f992015-03-24 21:39:47 -0700170
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700171 if (m_isChildSelectorRightmost)
172 interestPacket.setChildSelector(1);
Junxiao Shi0c75f992015-03-24 21:39:47 -0700173
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700174 if (m_minSuffixComponents >= 0)
175 interestPacket.setMinSuffixComponents(m_minSuffixComponents);
Junxiao Shi0c75f992015-03-24 21:39:47 -0700176
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700177 if (m_maxSuffixComponents >= 0)
178 interestPacket.setMaxSuffixComponents(m_maxSuffixComponents);
Junxiao Shi0c75f992015-03-24 21:39:47 -0700179
180 if (m_interestLifetime < time::milliseconds::zero())
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700181 interestPacket.setInterestLifetime(getDefaultInterestLifetime());
182 else
183 interestPacket.setInterestLifetime(m_interestLifetime);
Junxiao Shi0c75f992015-03-24 21:39:47 -0700184
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700185 return interestPacket;
186 }
187
188 void
Junxiao Shi0c75f992015-03-24 21:39:47 -0700189 onData(const Interest& interest, Data& data)
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700190 {
191 m_isDataReceived = true;
Junxiao Shi0c75f992015-03-24 21:39:47 -0700192 if (m_isPayloadOnlySet) {
193 const Block& block = data.getContent();
194 std::cout.write(reinterpret_cast<const char*>(block.value()), block.value_size());
195 }
196 else {
197 const Block& block = data.wireEncode();
198 std::cout.write(reinterpret_cast<const char*>(block.wire()), block.size());
199 }
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700200 }
201
202 void
Junxiao Shi0c75f992015-03-24 21:39:47 -0700203 onTimeout(const Interest& interest)
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700204 {
205 }
206
207 void
208 run()
209 {
Junxiao Shi0c75f992015-03-24 21:39:47 -0700210 try {
211 m_face.expressInterest(createInterestPacket(),
212 bind(&NdnPeek::onData, this, _1, _2),
213 bind(&NdnPeek::onTimeout, this, _1));
214 if (m_timeout < time::milliseconds::zero()) {
215 if (m_interestLifetime < time::milliseconds::zero())
216 m_face.processEvents(getDefaultInterestLifetime());
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700217 else
Junxiao Shi0c75f992015-03-24 21:39:47 -0700218 m_face.processEvents(m_interestLifetime);
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700219 }
Junxiao Shi0c75f992015-03-24 21:39:47 -0700220 else
221 m_face.processEvents(m_timeout);
222 }
223 catch (std::exception& e) {
224 std::cerr << "ERROR: " << e.what() << "\n" << std::endl;
225 exit(1);
226 }
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700227 }
228
229 bool
230 isDataReceived() const
231 {
232 return m_isDataReceived;
233 }
234
235private:
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700236 std::string m_programName;
237 bool m_mustBeFresh;
238 bool m_isChildSelectorRightmost;
239 int m_minSuffixComponents;
240 int m_maxSuffixComponents;
Junxiao Shi0c75f992015-03-24 21:39:47 -0700241 time::milliseconds m_interestLifetime;
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700242 bool m_isPayloadOnlySet;
Junxiao Shi0c75f992015-03-24 21:39:47 -0700243 time::milliseconds m_timeout;
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700244 std::string m_prefixName;
245 bool m_isDataReceived;
Junxiao Shi0c75f992015-03-24 21:39:47 -0700246 Face m_face;
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700247};
248
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700249int
250main(int argc, char* argv[])
251{
Junxiao Shi0c75f992015-03-24 21:39:47 -0700252 NdnPeek program(argv[0]);
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700253 int option;
254 while ((option = getopt(argc, argv, "hfrm:M:l:pw:V")) != -1) {
255 switch (option) {
256 case 'h':
Junxiao Shi0c75f992015-03-24 21:39:47 -0700257 program.usage();
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700258 break;
259 case 'f':
Junxiao Shi0c75f992015-03-24 21:39:47 -0700260 program.setMustBeFresh();
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700261 break;
262 case 'r':
Junxiao Shi0c75f992015-03-24 21:39:47 -0700263 program.setRightmostChildSelector();
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700264 break;
265 case 'm':
Junxiao Shi0c75f992015-03-24 21:39:47 -0700266 program.setMinSuffixComponents(atoi(optarg));
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700267 break;
268 case 'M':
Junxiao Shi0c75f992015-03-24 21:39:47 -0700269 program.setMaxSuffixComponents(atoi(optarg));
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700270 break;
271 case 'l':
Junxiao Shi0c75f992015-03-24 21:39:47 -0700272 program.setInterestLifetime(atoi(optarg));
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700273 break;
274 case 'p':
Junxiao Shi0c75f992015-03-24 21:39:47 -0700275 program.setPayloadOnly();
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700276 break;
277 case 'w':
Junxiao Shi0c75f992015-03-24 21:39:47 -0700278 program.setTimeout(atoi(optarg));
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700279 break;
280 case 'V':
Junxiao Shi1753afb2015-04-17 20:59:50 -0700281 std::cout << "ndnpeek " << tools::VERSION << std::endl;
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700282 return 0;
283 default:
Junxiao Shi0c75f992015-03-24 21:39:47 -0700284 program.usage();
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700285 break;
286 }
287 }
288
289 argc -= optind;
290 argv += optind;
291
292 if (argv[0] == 0)
Junxiao Shi0c75f992015-03-24 21:39:47 -0700293 program.usage();
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700294
Junxiao Shi0c75f992015-03-24 21:39:47 -0700295 program.setPrefixName(argv[0]);
296 program.run();
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700297
Junxiao Shi0c75f992015-03-24 21:39:47 -0700298 if (program.isDataReceived())
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700299 return 0;
300 else
301 return 1;
302}
Junxiao Shi0c75f992015-03-24 21:39:47 -0700303
304} // namespace peek
305} // namespace ndn
306
307int
308main(int argc, char** argv)
309{
310 return ndn::peek::main(argc, argv);
311}