blob: dca57e0188bf439dfbef5cbb345e9342d2d81912 [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/**
Alexander Afanasyev4a771362014-04-24 21:29:33 -07003 * 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
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/>.
24 *
25 * @author Jerald Paul Abraham <jeraldabraham@email.arizona.edu>
jeraldabraham20b2dc52014-03-15 22:17:57 -070026 */
Junxiao Shi2d2cde12014-03-31 00:32:42 -070027
Alexander Afanasyevb47d5382014-05-05 14:35:03 -070028#include "version.hpp"
29
Davide Pesaventoab1e8f22014-10-21 22:45:33 +020030#include <boost/noncopyable.hpp>
jeraldabraham20b2dc52014-03-15 22:17:57 -070031
Alexander Afanasyev4a771362014-04-24 21:29:33 -070032#include <ndn-cxx/face.hpp>
jeraldabraham20b2dc52014-03-15 22:17:57 -070033
34namespace ndntlvpeek {
35
Davide Pesaventoab1e8f22014-10-21 22:45:33 +020036using ndn::_1;
37using ndn::_2;
38
39class NdnTlvPeek : boost::noncopyable
jeraldabraham20b2dc52014-03-15 22:17:57 -070040{
41public:
Davide Pesaventoab1e8f22014-10-21 22:45:33 +020042 explicit
jeraldabraham20b2dc52014-03-15 22:17:57 -070043 NdnTlvPeek(char* programName)
44 : m_programName(programName)
45 , m_mustBeFresh(false)
46 , m_isChildSelectorRightmost(false)
47 , m_minSuffixComponents(-1)
48 , m_maxSuffixComponents(-1)
49 , m_interestLifetime(-1)
50 , m_isPayloadOnlySet(false)
51 , m_timeout(-1)
52 , m_prefixName("")
53 , m_isDataReceived(false)
jeraldabraham20b2dc52014-03-15 22:17:57 -070054 {
55 }
56
57 void
58 usage()
59 {
60 std::cout << "\n Usage:\n " << m_programName << " "
61 "[-f] [-r] [-m min] [-M max] [-l lifetime] [-p] [-w timeout] ndn:/name\n"
62 " Get one data item matching the name prefix and write it to stdout\n"
63 " [-f] - set MustBeFresh\n"
64 " [-r] - set ChildSelector to select rightmost child\n"
65 " [-m min] - set MinSuffixComponents\n"
66 " [-M max] - set MaxSuffixComponents\n"
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -070067 " [-l lifetime] - set InterestLifetime in time::milliseconds\n"
jeraldabraham20b2dc52014-03-15 22:17:57 -070068 " [-p] - print payload only, not full packet\n"
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -070069 " [-w timeout] - set Timeout in time::milliseconds\n"
Alexander Afanasyevb47d5382014-05-05 14:35:03 -070070 " [-h] - print help and exit\n"
71 " [-V] - print version and exit\n"
72 "\n";
jeraldabraham20b2dc52014-03-15 22:17:57 -070073 exit(1);
74 }
75
76 void
77 setMustBeFresh()
78 {
79 m_mustBeFresh = true;
80 }
81
82 void
83 setRightmostChildSelector()
84 {
85 m_isChildSelectorRightmost = true;
86 }
87
88 void
89 setMinSuffixComponents(int minSuffixComponents)
90 {
91 if (minSuffixComponents < 0)
92 usage();
93 m_minSuffixComponents = minSuffixComponents;
94 }
95
96 void
97 setMaxSuffixComponents(int maxSuffixComponents)
98 {
99 if (maxSuffixComponents < 0)
100 usage();
101 m_maxSuffixComponents = maxSuffixComponents;
102 }
103
104 void
105 setInterestLifetime(int interestLifetime)
106 {
107 if (interestLifetime < 0)
108 usage();
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700109 m_interestLifetime = ndn::time::milliseconds(interestLifetime);
jeraldabraham20b2dc52014-03-15 22:17:57 -0700110 }
111
112 void
113 setPayloadOnly()
114 {
115 m_isPayloadOnlySet = true;
116 }
117
118 void
119 setTimeout(int timeout)
120 {
121 if (timeout < 0)
122 usage();
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700123 m_timeout = ndn::time::milliseconds(timeout);
jeraldabraham20b2dc52014-03-15 22:17:57 -0700124 }
125
126 void
127 setPrefixName(char* prefixName)
128 {
129 m_prefixName = prefixName;
130 if (m_prefixName.length() == 0)
131 usage();
132 }
133
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700134 ndn::time::milliseconds
jeraldabraham20b2dc52014-03-15 22:17:57 -0700135 getDefaultInterestLifetime()
136 {
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700137 return ndn::time::seconds(4);
jeraldabraham20b2dc52014-03-15 22:17:57 -0700138 }
139
140 ndn::Interest
141 createInterestPacket()
142 {
143 ndn::Name interestName(m_prefixName);
144 ndn::Interest interestPacket(interestName);
145 if (m_mustBeFresh)
146 interestPacket.setMustBeFresh(true);
147 if (m_isChildSelectorRightmost)
148 interestPacket.setChildSelector(1);
149 if (m_minSuffixComponents >= 0)
150 interestPacket.setMinSuffixComponents(m_minSuffixComponents);
151 if (m_maxSuffixComponents >= 0)
152 interestPacket.setMaxSuffixComponents(m_maxSuffixComponents);
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700153 if (m_interestLifetime < ndn::time::milliseconds::zero())
jeraldabraham20b2dc52014-03-15 22:17:57 -0700154 interestPacket.setInterestLifetime(getDefaultInterestLifetime());
155 else
156 interestPacket.setInterestLifetime(m_interestLifetime);
157 return interestPacket;
158 }
159
160 void
161 onData(const ndn::Interest& interest, ndn::Data& data)
162 {
jeraldabraham20b2dc52014-03-15 22:17:57 -0700163 m_isDataReceived = true;
164 if (m_isPayloadOnlySet)
165 {
Junxiao Shi2d2cde12014-03-31 00:32:42 -0700166 const ndn::Block& block = data.getContent();
167 std::cout.write(reinterpret_cast<const char*>(block.value()), block.value_size());
jeraldabraham20b2dc52014-03-15 22:17:57 -0700168 }
169 else
170 {
Junxiao Shi2d2cde12014-03-31 00:32:42 -0700171 const ndn::Block& block = data.wireEncode();
172 std::cout.write(reinterpret_cast<const char*>(block.wire()), block.size());
jeraldabraham20b2dc52014-03-15 22:17:57 -0700173 }
174 }
175
176 void
177 onTimeout(const ndn::Interest& interest)
178 {
179 }
180
181 void
182 run()
183 {
184 try
185 {
186 m_face.expressInterest(createInterestPacket(),
Davide Pesaventoab1e8f22014-10-21 22:45:33 +0200187 bind(&NdnTlvPeek::onData, this, _1, _2),
188 bind(&NdnTlvPeek::onTimeout, this, _1));
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700189 if (m_timeout < ndn::time::milliseconds::zero())
jeraldabraham20b2dc52014-03-15 22:17:57 -0700190 {
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700191 if (m_interestLifetime < ndn::time::milliseconds::zero())
jeraldabraham20b2dc52014-03-15 22:17:57 -0700192 m_face.processEvents(getDefaultInterestLifetime());
193 else
194 m_face.processEvents(m_interestLifetime);
195 }
196 else
197 m_face.processEvents(m_timeout);
198 }
199 catch (std::exception& e)
200 {
201 std::cerr << "ERROR: " << e.what() << "\n" << std::endl;
202 exit(1);
203 }
204 }
205
206 bool
207 isDataReceived() const
208 {
209 return m_isDataReceived;
210 }
211
212private:
213
214 std::string m_programName;
215 bool m_mustBeFresh;
216 bool m_isChildSelectorRightmost;
217 int m_minSuffixComponents;
218 int m_maxSuffixComponents;
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700219 ndn::time::milliseconds m_interestLifetime;
jeraldabraham20b2dc52014-03-15 22:17:57 -0700220 bool m_isPayloadOnlySet;
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700221 ndn::time::milliseconds m_timeout;
jeraldabraham20b2dc52014-03-15 22:17:57 -0700222 std::string m_prefixName;
223 bool m_isDataReceived;
jeraldabraham20b2dc52014-03-15 22:17:57 -0700224 ndn::Face m_face;
225};
226
227}
228
229int
230main(int argc, char* argv[])
231{
Alexander Afanasyevb3893c92014-05-15 01:49:54 -0700232 ndntlvpeek::NdnTlvPeek ndnTlvPeek(argv[0]);
jeraldabraham20b2dc52014-03-15 22:17:57 -0700233 int option;
Alexander Afanasyevb47d5382014-05-05 14:35:03 -0700234 while ((option = getopt(argc, argv, "hfrm:M:l:pw:V")) != -1) {
235 switch (option) {
236 case 'h':
237 ndnTlvPeek.usage();
238 break;
239 case 'f':
240 ndnTlvPeek.setMustBeFresh();
241 break;
242 case 'r':
243 ndnTlvPeek.setRightmostChildSelector();
244 break;
245 case 'm':
246 ndnTlvPeek.setMinSuffixComponents(atoi(optarg));
247 break;
248 case 'M':
249 ndnTlvPeek.setMaxSuffixComponents(atoi(optarg));
250 break;
251 case 'l':
252 ndnTlvPeek.setInterestLifetime(atoi(optarg));
253 break;
254 case 'p':
255 ndnTlvPeek.setPayloadOnly();
256 break;
257 case 'w':
258 ndnTlvPeek.setTimeout(atoi(optarg));
259 break;
260 case 'V':
261 std::cout << NFD_VERSION_BUILD_STRING << std::endl;
262 return 0;
263 default:
264 ndnTlvPeek.usage();
265 break;
jeraldabraham20b2dc52014-03-15 22:17:57 -0700266 }
Alexander Afanasyevb47d5382014-05-05 14:35:03 -0700267 }
jeraldabraham20b2dc52014-03-15 22:17:57 -0700268
269 argc -= optind;
270 argv += optind;
271
272 if (argv[0] == 0)
273 ndnTlvPeek.usage();
274
275 ndnTlvPeek.setPrefixName(argv[0]);
276 ndnTlvPeek.run();
277
278 if (ndnTlvPeek.isDataReceived())
279 return 0;
280 else
281 return 1;
282}