blob: dc701756f33aa034a248318ee346431329d3f1f2 [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
jeraldabraham20b2dc52014-03-15 22:17:57 -070030#include <boost/asio.hpp>
31
Alexander Afanasyev4a771362014-04-24 21:29:33 -070032#include <ndn-cxx/face.hpp>
jeraldabraham20b2dc52014-03-15 22:17:57 -070033
34namespace ndntlvpeek {
35
36class NdnTlvPeek
37{
38public:
39 NdnTlvPeek(char* programName)
40 : m_programName(programName)
41 , m_mustBeFresh(false)
42 , m_isChildSelectorRightmost(false)
43 , m_minSuffixComponents(-1)
44 , m_maxSuffixComponents(-1)
45 , m_interestLifetime(-1)
46 , m_isPayloadOnlySet(false)
47 , m_timeout(-1)
48 , m_prefixName("")
49 , m_isDataReceived(false)
jeraldabraham20b2dc52014-03-15 22:17:57 -070050 {
51 }
52
53 void
54 usage()
55 {
56 std::cout << "\n Usage:\n " << m_programName << " "
57 "[-f] [-r] [-m min] [-M max] [-l lifetime] [-p] [-w timeout] ndn:/name\n"
58 " Get one data item matching the name prefix and write it to stdout\n"
59 " [-f] - set MustBeFresh\n"
60 " [-r] - set ChildSelector to select rightmost child\n"
61 " [-m min] - set MinSuffixComponents\n"
62 " [-M max] - set MaxSuffixComponents\n"
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -070063 " [-l lifetime] - set InterestLifetime in time::milliseconds\n"
jeraldabraham20b2dc52014-03-15 22:17:57 -070064 " [-p] - print payload only, not full packet\n"
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -070065 " [-w timeout] - set Timeout in time::milliseconds\n"
Alexander Afanasyevb47d5382014-05-05 14:35:03 -070066 " [-h] - print help and exit\n"
67 " [-V] - print version and exit\n"
68 "\n";
jeraldabraham20b2dc52014-03-15 22:17:57 -070069 exit(1);
70 }
71
72 void
73 setMustBeFresh()
74 {
75 m_mustBeFresh = true;
76 }
77
78 void
79 setRightmostChildSelector()
80 {
81 m_isChildSelectorRightmost = true;
82 }
83
84 void
85 setMinSuffixComponents(int minSuffixComponents)
86 {
87 if (minSuffixComponents < 0)
88 usage();
89 m_minSuffixComponents = minSuffixComponents;
90 }
91
92 void
93 setMaxSuffixComponents(int maxSuffixComponents)
94 {
95 if (maxSuffixComponents < 0)
96 usage();
97 m_maxSuffixComponents = maxSuffixComponents;
98 }
99
100 void
101 setInterestLifetime(int interestLifetime)
102 {
103 if (interestLifetime < 0)
104 usage();
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700105 m_interestLifetime = ndn::time::milliseconds(interestLifetime);
jeraldabraham20b2dc52014-03-15 22:17:57 -0700106 }
107
108 void
109 setPayloadOnly()
110 {
111 m_isPayloadOnlySet = true;
112 }
113
114 void
115 setTimeout(int timeout)
116 {
117 if (timeout < 0)
118 usage();
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700119 m_timeout = ndn::time::milliseconds(timeout);
jeraldabraham20b2dc52014-03-15 22:17:57 -0700120 }
121
122 void
123 setPrefixName(char* prefixName)
124 {
125 m_prefixName = prefixName;
126 if (m_prefixName.length() == 0)
127 usage();
128 }
129
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700130 ndn::time::milliseconds
jeraldabraham20b2dc52014-03-15 22:17:57 -0700131 getDefaultInterestLifetime()
132 {
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700133 return ndn::time::seconds(4);
jeraldabraham20b2dc52014-03-15 22:17:57 -0700134 }
135
136 ndn::Interest
137 createInterestPacket()
138 {
139 ndn::Name interestName(m_prefixName);
140 ndn::Interest interestPacket(interestName);
141 if (m_mustBeFresh)
142 interestPacket.setMustBeFresh(true);
143 if (m_isChildSelectorRightmost)
144 interestPacket.setChildSelector(1);
145 if (m_minSuffixComponents >= 0)
146 interestPacket.setMinSuffixComponents(m_minSuffixComponents);
147 if (m_maxSuffixComponents >= 0)
148 interestPacket.setMaxSuffixComponents(m_maxSuffixComponents);
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700149 if (m_interestLifetime < ndn::time::milliseconds::zero())
jeraldabraham20b2dc52014-03-15 22:17:57 -0700150 interestPacket.setInterestLifetime(getDefaultInterestLifetime());
151 else
152 interestPacket.setInterestLifetime(m_interestLifetime);
153 return interestPacket;
154 }
155
156 void
157 onData(const ndn::Interest& interest, ndn::Data& data)
158 {
jeraldabraham20b2dc52014-03-15 22:17:57 -0700159 m_isDataReceived = true;
160 if (m_isPayloadOnlySet)
161 {
Junxiao Shi2d2cde12014-03-31 00:32:42 -0700162 const ndn::Block& block = data.getContent();
163 std::cout.write(reinterpret_cast<const char*>(block.value()), block.value_size());
jeraldabraham20b2dc52014-03-15 22:17:57 -0700164 }
165 else
166 {
Junxiao Shi2d2cde12014-03-31 00:32:42 -0700167 const ndn::Block& block = data.wireEncode();
168 std::cout.write(reinterpret_cast<const char*>(block.wire()), block.size());
jeraldabraham20b2dc52014-03-15 22:17:57 -0700169 }
170 }
171
172 void
173 onTimeout(const ndn::Interest& interest)
174 {
175 }
176
177 void
178 run()
179 {
180 try
181 {
182 m_face.expressInterest(createInterestPacket(),
183 ndn::func_lib::bind(&NdnTlvPeek::onData,
184 this, _1, _2),
185 ndn::func_lib::bind(&NdnTlvPeek::onTimeout,
186 this, _1));
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700187 if (m_timeout < ndn::time::milliseconds::zero())
jeraldabraham20b2dc52014-03-15 22:17:57 -0700188 {
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700189 if (m_interestLifetime < ndn::time::milliseconds::zero())
jeraldabraham20b2dc52014-03-15 22:17:57 -0700190 m_face.processEvents(getDefaultInterestLifetime());
191 else
192 m_face.processEvents(m_interestLifetime);
193 }
194 else
195 m_face.processEvents(m_timeout);
196 }
197 catch (std::exception& e)
198 {
199 std::cerr << "ERROR: " << e.what() << "\n" << std::endl;
200 exit(1);
201 }
202 }
203
204 bool
205 isDataReceived() const
206 {
207 return m_isDataReceived;
208 }
209
210private:
211
212 std::string m_programName;
213 bool m_mustBeFresh;
214 bool m_isChildSelectorRightmost;
215 int m_minSuffixComponents;
216 int m_maxSuffixComponents;
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700217 ndn::time::milliseconds m_interestLifetime;
jeraldabraham20b2dc52014-03-15 22:17:57 -0700218 bool m_isPayloadOnlySet;
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700219 ndn::time::milliseconds m_timeout;
jeraldabraham20b2dc52014-03-15 22:17:57 -0700220 std::string m_prefixName;
221 bool m_isDataReceived;
jeraldabraham20b2dc52014-03-15 22:17:57 -0700222 ndn::Face m_face;
223};
224
225}
226
227int
228main(int argc, char* argv[])
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}