blob: 4caba0a03776371400560b13f42f17077f552f1b [file] [log] [blame]
Junxiao Shi2ac79d92015-03-23 11:16:18 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2014, 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 NFD (Named Data Networking Forwarding Daemon).
12 * See AUTHORS.md for complete list of NFD authors and contributors.
13 *
14 * NFD 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 * NFD 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 * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
24 *
25 * @author Jerald Paul Abraham <jeraldabraham@email.arizona.edu>
26 */
27
28#include "version.hpp"
29
30#include <boost/noncopyable.hpp>
31
32#include <ndn-cxx/face.hpp>
33
Junxiao Shi0c75f992015-03-24 21:39:47 -070034namespace ndn {
35namespace peek {
Junxiao Shi2ac79d92015-03-23 11:16:18 -070036
Junxiao Shi0c75f992015-03-24 21:39:47 -070037class NdnPeek : boost::noncopyable
Junxiao Shi2ac79d92015-03-23 11:16:18 -070038{
39public:
40 explicit
Junxiao Shi0c75f992015-03-24 21:39:47 -070041 NdnPeek(char* programName)
Junxiao Shi2ac79d92015-03-23 11:16:18 -070042 : m_programName(programName)
43 , m_mustBeFresh(false)
44 , m_isChildSelectorRightmost(false)
45 , m_minSuffixComponents(-1)
46 , m_maxSuffixComponents(-1)
47 , m_interestLifetime(-1)
48 , m_isPayloadOnlySet(false)
49 , m_timeout(-1)
50 , m_prefixName("")
51 , m_isDataReceived(false)
52 {
53 }
54
55 void
56 usage()
57 {
58 std::cout << "\n Usage:\n " << m_programName << " "
59 "[-f] [-r] [-m min] [-M max] [-l lifetime] [-p] [-w timeout] ndn:/name\n"
60 " Get one data item matching the name prefix and write it to stdout\n"
61 " [-f] - set MustBeFresh\n"
62 " [-r] - set ChildSelector to select rightmost child\n"
63 " [-m min] - set MinSuffixComponents\n"
64 " [-M max] - set MaxSuffixComponents\n"
65 " [-l lifetime] - set InterestLifetime in time::milliseconds\n"
66 " [-p] - print payload only, not full packet\n"
67 " [-w timeout] - set Timeout in time::milliseconds\n"
68 " [-h] - print help and exit\n"
69 " [-V] - print version and exit\n"
70 "\n";
71 exit(1);
72 }
73
74 void
75 setMustBeFresh()
76 {
77 m_mustBeFresh = true;
78 }
79
80 void
81 setRightmostChildSelector()
82 {
83 m_isChildSelectorRightmost = true;
84 }
85
86 void
87 setMinSuffixComponents(int minSuffixComponents)
88 {
89 if (minSuffixComponents < 0)
90 usage();
Junxiao Shi0c75f992015-03-24 21:39:47 -070091
Junxiao Shi2ac79d92015-03-23 11:16:18 -070092 m_minSuffixComponents = minSuffixComponents;
93 }
94
95 void
96 setMaxSuffixComponents(int maxSuffixComponents)
97 {
98 if (maxSuffixComponents < 0)
99 usage();
Junxiao Shi0c75f992015-03-24 21:39:47 -0700100
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700101 m_maxSuffixComponents = maxSuffixComponents;
102 }
103
104 void
105 setInterestLifetime(int interestLifetime)
106 {
107 if (interestLifetime < 0)
108 usage();
Junxiao Shi0c75f992015-03-24 21:39:47 -0700109
110 m_interestLifetime = time::milliseconds(interestLifetime);
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700111 }
112
113 void
114 setPayloadOnly()
115 {
116 m_isPayloadOnlySet = true;
117 }
118
119 void
120 setTimeout(int timeout)
121 {
122 if (timeout < 0)
123 usage();
Junxiao Shi0c75f992015-03-24 21:39:47 -0700124
125 m_timeout = time::milliseconds(timeout);
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700126 }
127
128 void
129 setPrefixName(char* prefixName)
130 {
131 m_prefixName = prefixName;
Junxiao Shi0c75f992015-03-24 21:39:47 -0700132
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700133 if (m_prefixName.length() == 0)
134 usage();
135 }
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
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700149 if (m_mustBeFresh)
150 interestPacket.setMustBeFresh(true);
Junxiao Shi0c75f992015-03-24 21:39:47 -0700151
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700152 if (m_isChildSelectorRightmost)
153 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
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700166 return interestPacket;
167 }
168
169 void
Junxiao Shi0c75f992015-03-24 21:39:47 -0700170 onData(const Interest& interest, Data& data)
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700171 {
172 m_isDataReceived = true;
Junxiao Shi0c75f992015-03-24 21:39:47 -0700173 if (m_isPayloadOnlySet) {
174 const Block& block = data.getContent();
175 std::cout.write(reinterpret_cast<const char*>(block.value()), block.value_size());
176 }
177 else {
178 const Block& block = data.wireEncode();
179 std::cout.write(reinterpret_cast<const char*>(block.wire()), block.size());
180 }
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700181 }
182
183 void
Junxiao Shi0c75f992015-03-24 21:39:47 -0700184 onTimeout(const Interest& interest)
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700185 {
186 }
187
188 void
189 run()
190 {
Junxiao Shi0c75f992015-03-24 21:39:47 -0700191 try {
192 m_face.expressInterest(createInterestPacket(),
193 bind(&NdnPeek::onData, this, _1, _2),
194 bind(&NdnPeek::onTimeout, this, _1));
195 if (m_timeout < time::milliseconds::zero()) {
196 if (m_interestLifetime < time::milliseconds::zero())
197 m_face.processEvents(getDefaultInterestLifetime());
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700198 else
Junxiao Shi0c75f992015-03-24 21:39:47 -0700199 m_face.processEvents(m_interestLifetime);
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700200 }
Junxiao Shi0c75f992015-03-24 21:39:47 -0700201 else
202 m_face.processEvents(m_timeout);
203 }
204 catch (std::exception& e) {
205 std::cerr << "ERROR: " << e.what() << "\n" << std::endl;
206 exit(1);
207 }
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700208 }
209
210 bool
211 isDataReceived() const
212 {
213 return m_isDataReceived;
214 }
215
216private:
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700217 std::string m_programName;
218 bool m_mustBeFresh;
219 bool m_isChildSelectorRightmost;
220 int m_minSuffixComponents;
221 int m_maxSuffixComponents;
Junxiao Shi0c75f992015-03-24 21:39:47 -0700222 time::milliseconds m_interestLifetime;
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700223 bool m_isPayloadOnlySet;
Junxiao Shi0c75f992015-03-24 21:39:47 -0700224 time::milliseconds m_timeout;
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700225 std::string m_prefixName;
226 bool m_isDataReceived;
Junxiao Shi0c75f992015-03-24 21:39:47 -0700227 Face m_face;
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700228};
229
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700230int
231main(int argc, char* argv[])
232{
Junxiao Shi0c75f992015-03-24 21:39:47 -0700233 NdnPeek program(argv[0]);
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700234 int option;
235 while ((option = getopt(argc, argv, "hfrm:M:l:pw:V")) != -1) {
236 switch (option) {
237 case 'h':
Junxiao Shi0c75f992015-03-24 21:39:47 -0700238 program.usage();
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700239 break;
240 case 'f':
Junxiao Shi0c75f992015-03-24 21:39:47 -0700241 program.setMustBeFresh();
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700242 break;
243 case 'r':
Junxiao Shi0c75f992015-03-24 21:39:47 -0700244 program.setRightmostChildSelector();
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700245 break;
246 case 'm':
Junxiao Shi0c75f992015-03-24 21:39:47 -0700247 program.setMinSuffixComponents(atoi(optarg));
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700248 break;
249 case 'M':
Junxiao Shi0c75f992015-03-24 21:39:47 -0700250 program.setMaxSuffixComponents(atoi(optarg));
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700251 break;
252 case 'l':
Junxiao Shi0c75f992015-03-24 21:39:47 -0700253 program.setInterestLifetime(atoi(optarg));
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700254 break;
255 case 'p':
Junxiao Shi0c75f992015-03-24 21:39:47 -0700256 program.setPayloadOnly();
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700257 break;
258 case 'w':
Junxiao Shi0c75f992015-03-24 21:39:47 -0700259 program.setTimeout(atoi(optarg));
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700260 break;
261 case 'V':
262 std::cout << NFD_VERSION_BUILD_STRING << std::endl;
263 return 0;
264 default:
Junxiao Shi0c75f992015-03-24 21:39:47 -0700265 program.usage();
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700266 break;
267 }
268 }
269
270 argc -= optind;
271 argv += optind;
272
273 if (argv[0] == 0)
Junxiao Shi0c75f992015-03-24 21:39:47 -0700274 program.usage();
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700275
Junxiao Shi0c75f992015-03-24 21:39:47 -0700276 program.setPrefixName(argv[0]);
277 program.run();
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700278
Junxiao Shi0c75f992015-03-24 21:39:47 -0700279 if (program.isDataReceived())
Junxiao Shi2ac79d92015-03-23 11:16:18 -0700280 return 0;
281 else
282 return 1;
283}
Junxiao Shi0c75f992015-03-24 21:39:47 -0700284
285} // namespace peek
286} // namespace ndn
287
288int
289main(int argc, char** argv)
290{
291 return ndn::peek::main(argc, argv);
292}