blob: ee37bbcf28f08a3de4ebbee4d6ba04017416de4c [file] [log] [blame]
Alexander Afanasyev4a771362014-04-24 21:29:33 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
jeraldabraham20b2dc52014-03-15 22:17:57 -07002/**
Junxiao Shi5620fb22015-06-07 20:59:38 -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.
jeraldabraham20b2dc52014-03-15 22:17:57 -070010 *
Alexander Afanasyev4a771362014-04-24 21:29:33 -070011 * 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/>.
jeraldabraham20b2dc52014-03-15 22:17:57 -070024 */
Junxiao Shi2d2cde12014-03-31 00:32:42 -070025
Alexander Afanasyevb47d5382014-05-05 14:35:03 -070026#include "version.hpp"
27
Davide Pesaventoab1e8f22014-10-21 22:45:33 +020028#include <boost/noncopyable.hpp>
jeraldabraham20b2dc52014-03-15 22:17:57 -070029
Alexander Afanasyev4a771362014-04-24 21:29:33 -070030#include <ndn-cxx/face.hpp>
jeraldabraham20b2dc52014-03-15 22:17:57 -070031
32namespace ndntlvpeek {
33
Davide Pesaventoab1e8f22014-10-21 22:45:33 +020034class NdnTlvPeek : boost::noncopyable
jeraldabraham20b2dc52014-03-15 22:17:57 -070035{
36public:
Davide Pesaventoab1e8f22014-10-21 22:45:33 +020037 explicit
jeraldabraham20b2dc52014-03-15 22:17:57 -070038 NdnTlvPeek(char* programName)
39 : m_programName(programName)
40 , m_mustBeFresh(false)
41 , m_isChildSelectorRightmost(false)
42 , m_minSuffixComponents(-1)
43 , m_maxSuffixComponents(-1)
44 , m_interestLifetime(-1)
45 , m_isPayloadOnlySet(false)
46 , m_timeout(-1)
47 , m_prefixName("")
48 , m_isDataReceived(false)
jeraldabraham20b2dc52014-03-15 22:17:57 -070049 {
50 }
51
52 void
53 usage()
54 {
55 std::cout << "\n Usage:\n " << m_programName << " "
56 "[-f] [-r] [-m min] [-M max] [-l lifetime] [-p] [-w timeout] ndn:/name\n"
57 " Get one data item matching the name prefix and write it to stdout\n"
58 " [-f] - set MustBeFresh\n"
59 " [-r] - set ChildSelector to select rightmost child\n"
60 " [-m min] - set MinSuffixComponents\n"
61 " [-M max] - set MaxSuffixComponents\n"
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -070062 " [-l lifetime] - set InterestLifetime in time::milliseconds\n"
jeraldabraham20b2dc52014-03-15 22:17:57 -070063 " [-p] - print payload only, not full packet\n"
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -070064 " [-w timeout] - set Timeout in time::milliseconds\n"
Alexander Afanasyevb47d5382014-05-05 14:35:03 -070065 " [-h] - print help and exit\n"
66 " [-V] - print version and exit\n"
67 "\n";
jeraldabraham20b2dc52014-03-15 22:17:57 -070068 exit(1);
69 }
70
71 void
72 setMustBeFresh()
73 {
74 m_mustBeFresh = true;
75 }
76
77 void
78 setRightmostChildSelector()
79 {
80 m_isChildSelectorRightmost = true;
81 }
82
83 void
84 setMinSuffixComponents(int minSuffixComponents)
85 {
86 if (minSuffixComponents < 0)
87 usage();
88 m_minSuffixComponents = minSuffixComponents;
89 }
90
91 void
92 setMaxSuffixComponents(int maxSuffixComponents)
93 {
94 if (maxSuffixComponents < 0)
95 usage();
96 m_maxSuffixComponents = maxSuffixComponents;
97 }
98
99 void
100 setInterestLifetime(int interestLifetime)
101 {
102 if (interestLifetime < 0)
103 usage();
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700104 m_interestLifetime = ndn::time::milliseconds(interestLifetime);
jeraldabraham20b2dc52014-03-15 22:17:57 -0700105 }
106
107 void
108 setPayloadOnly()
109 {
110 m_isPayloadOnlySet = true;
111 }
112
113 void
114 setTimeout(int timeout)
115 {
116 if (timeout < 0)
117 usage();
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700118 m_timeout = ndn::time::milliseconds(timeout);
jeraldabraham20b2dc52014-03-15 22:17:57 -0700119 }
120
121 void
122 setPrefixName(char* prefixName)
123 {
124 m_prefixName = prefixName;
125 if (m_prefixName.length() == 0)
126 usage();
127 }
128
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700129 ndn::time::milliseconds
jeraldabraham20b2dc52014-03-15 22:17:57 -0700130 getDefaultInterestLifetime()
131 {
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700132 return ndn::time::seconds(4);
jeraldabraham20b2dc52014-03-15 22:17:57 -0700133 }
134
135 ndn::Interest
136 createInterestPacket()
137 {
138 ndn::Name interestName(m_prefixName);
139 ndn::Interest interestPacket(interestName);
140 if (m_mustBeFresh)
141 interestPacket.setMustBeFresh(true);
142 if (m_isChildSelectorRightmost)
143 interestPacket.setChildSelector(1);
144 if (m_minSuffixComponents >= 0)
145 interestPacket.setMinSuffixComponents(m_minSuffixComponents);
146 if (m_maxSuffixComponents >= 0)
147 interestPacket.setMaxSuffixComponents(m_maxSuffixComponents);
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700148 if (m_interestLifetime < ndn::time::milliseconds::zero())
jeraldabraham20b2dc52014-03-15 22:17:57 -0700149 interestPacket.setInterestLifetime(getDefaultInterestLifetime());
150 else
151 interestPacket.setInterestLifetime(m_interestLifetime);
152 return interestPacket;
153 }
154
155 void
156 onData(const ndn::Interest& interest, ndn::Data& data)
157 {
jeraldabraham20b2dc52014-03-15 22:17:57 -0700158 m_isDataReceived = true;
159 if (m_isPayloadOnlySet)
160 {
Junxiao Shi2d2cde12014-03-31 00:32:42 -0700161 const ndn::Block& block = data.getContent();
162 std::cout.write(reinterpret_cast<const char*>(block.value()), block.value_size());
jeraldabraham20b2dc52014-03-15 22:17:57 -0700163 }
164 else
165 {
Junxiao Shi2d2cde12014-03-31 00:32:42 -0700166 const ndn::Block& block = data.wireEncode();
167 std::cout.write(reinterpret_cast<const char*>(block.wire()), block.size());
jeraldabraham20b2dc52014-03-15 22:17:57 -0700168 }
169 }
170
171 void
172 onTimeout(const ndn::Interest& interest)
173 {
174 }
175
176 void
177 run()
178 {
179 try
180 {
181 m_face.expressInterest(createInterestPacket(),
Davide Pesaventoab1e8f22014-10-21 22:45:33 +0200182 bind(&NdnTlvPeek::onData, this, _1, _2),
183 bind(&NdnTlvPeek::onTimeout, this, _1));
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700184 if (m_timeout < ndn::time::milliseconds::zero())
jeraldabraham20b2dc52014-03-15 22:17:57 -0700185 {
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700186 if (m_interestLifetime < ndn::time::milliseconds::zero())
jeraldabraham20b2dc52014-03-15 22:17:57 -0700187 m_face.processEvents(getDefaultInterestLifetime());
188 else
189 m_face.processEvents(m_interestLifetime);
190 }
191 else
192 m_face.processEvents(m_timeout);
193 }
194 catch (std::exception& e)
195 {
196 std::cerr << "ERROR: " << e.what() << "\n" << std::endl;
197 exit(1);
198 }
199 }
200
201 bool
202 isDataReceived() const
203 {
204 return m_isDataReceived;
205 }
206
207private:
208
209 std::string m_programName;
210 bool m_mustBeFresh;
211 bool m_isChildSelectorRightmost;
212 int m_minSuffixComponents;
213 int m_maxSuffixComponents;
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700214 ndn::time::milliseconds m_interestLifetime;
jeraldabraham20b2dc52014-03-15 22:17:57 -0700215 bool m_isPayloadOnlySet;
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700216 ndn::time::milliseconds m_timeout;
jeraldabraham20b2dc52014-03-15 22:17:57 -0700217 std::string m_prefixName;
218 bool m_isDataReceived;
jeraldabraham20b2dc52014-03-15 22:17:57 -0700219 ndn::Face m_face;
220};
221
222}
223
224int
225main(int argc, char* argv[])
226{
Junxiao Shi5620fb22015-06-07 20:59:38 -0700227 std::cerr << "ndn-tlv-peek is deprecated. Use ndnpeek program from ndn-tools repository.\n"
228 "See `man ndn-tlv-peek` for details." << std::endl;
229
Alexander Afanasyevb3893c92014-05-15 01:49:54 -0700230 ndntlvpeek::NdnTlvPeek ndnTlvPeek(argv[0]);
jeraldabraham20b2dc52014-03-15 22:17:57 -0700231 int option;
Alexander Afanasyevb47d5382014-05-05 14:35:03 -0700232 while ((option = getopt(argc, argv, "hfrm:M:l:pw:V")) != -1) {
233 switch (option) {
234 case 'h':
235 ndnTlvPeek.usage();
236 break;
237 case 'f':
238 ndnTlvPeek.setMustBeFresh();
239 break;
240 case 'r':
241 ndnTlvPeek.setRightmostChildSelector();
242 break;
243 case 'm':
244 ndnTlvPeek.setMinSuffixComponents(atoi(optarg));
245 break;
246 case 'M':
247 ndnTlvPeek.setMaxSuffixComponents(atoi(optarg));
248 break;
249 case 'l':
250 ndnTlvPeek.setInterestLifetime(atoi(optarg));
251 break;
252 case 'p':
253 ndnTlvPeek.setPayloadOnly();
254 break;
255 case 'w':
256 ndnTlvPeek.setTimeout(atoi(optarg));
257 break;
258 case 'V':
259 std::cout << NFD_VERSION_BUILD_STRING << std::endl;
260 return 0;
261 default:
262 ndnTlvPeek.usage();
263 break;
jeraldabraham20b2dc52014-03-15 22:17:57 -0700264 }
Alexander Afanasyevb47d5382014-05-05 14:35:03 -0700265 }
jeraldabraham20b2dc52014-03-15 22:17:57 -0700266
267 argc -= optind;
268 argv += optind;
269
270 if (argv[0] == 0)
271 ndnTlvPeek.usage();
272
273 ndnTlvPeek.setPrefixName(argv[0]);
274 ndnTlvPeek.run();
275
276 if (ndnTlvPeek.isDataReceived())
277 return 0;
278 else
279 return 1;
280}