blob: ec0a9b0cbc51d07552cef2f8f4b971472caf762a [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)
50 , m_ioService(new boost::asio::io_service)
51 , m_face(m_ioService)
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"
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -070065 " [-l lifetime] - set InterestLifetime in time::milliseconds\n"
jeraldabraham20b2dc52014-03-15 22:17:57 -070066 " [-p] - print payload only, not full packet\n"
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -070067 " [-w timeout] - set Timeout in time::milliseconds\n"
Alexander Afanasyevb47d5382014-05-05 14:35:03 -070068 " [-h] - print help and exit\n"
69 " [-V] - print version and exit\n"
70 "\n";
jeraldabraham20b2dc52014-03-15 22:17:57 -070071 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();
91 m_minSuffixComponents = minSuffixComponents;
92 }
93
94 void
95 setMaxSuffixComponents(int maxSuffixComponents)
96 {
97 if (maxSuffixComponents < 0)
98 usage();
99 m_maxSuffixComponents = maxSuffixComponents;
100 }
101
102 void
103 setInterestLifetime(int interestLifetime)
104 {
105 if (interestLifetime < 0)
106 usage();
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700107 m_interestLifetime = ndn::time::milliseconds(interestLifetime);
jeraldabraham20b2dc52014-03-15 22:17:57 -0700108 }
109
110 void
111 setPayloadOnly()
112 {
113 m_isPayloadOnlySet = true;
114 }
115
116 void
117 setTimeout(int timeout)
118 {
119 if (timeout < 0)
120 usage();
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700121 m_timeout = ndn::time::milliseconds(timeout);
jeraldabraham20b2dc52014-03-15 22:17:57 -0700122 }
123
124 void
125 setPrefixName(char* prefixName)
126 {
127 m_prefixName = prefixName;
128 if (m_prefixName.length() == 0)
129 usage();
130 }
131
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700132 ndn::time::milliseconds
jeraldabraham20b2dc52014-03-15 22:17:57 -0700133 getDefaultInterestLifetime()
134 {
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700135 return ndn::time::seconds(4);
jeraldabraham20b2dc52014-03-15 22:17:57 -0700136 }
137
138 ndn::Interest
139 createInterestPacket()
140 {
141 ndn::Name interestName(m_prefixName);
142 ndn::Interest interestPacket(interestName);
143 if (m_mustBeFresh)
144 interestPacket.setMustBeFresh(true);
145 if (m_isChildSelectorRightmost)
146 interestPacket.setChildSelector(1);
147 if (m_minSuffixComponents >= 0)
148 interestPacket.setMinSuffixComponents(m_minSuffixComponents);
149 if (m_maxSuffixComponents >= 0)
150 interestPacket.setMaxSuffixComponents(m_maxSuffixComponents);
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700151 if (m_interestLifetime < ndn::time::milliseconds::zero())
jeraldabraham20b2dc52014-03-15 22:17:57 -0700152 interestPacket.setInterestLifetime(getDefaultInterestLifetime());
153 else
154 interestPacket.setInterestLifetime(m_interestLifetime);
155 return interestPacket;
156 }
157
158 void
159 onData(const ndn::Interest& interest, ndn::Data& data)
160 {
jeraldabraham20b2dc52014-03-15 22:17:57 -0700161 m_isDataReceived = true;
162 if (m_isPayloadOnlySet)
163 {
Junxiao Shi2d2cde12014-03-31 00:32:42 -0700164 const ndn::Block& block = data.getContent();
165 std::cout.write(reinterpret_cast<const char*>(block.value()), block.value_size());
jeraldabraham20b2dc52014-03-15 22:17:57 -0700166 }
167 else
168 {
Junxiao Shi2d2cde12014-03-31 00:32:42 -0700169 const ndn::Block& block = data.wireEncode();
170 std::cout.write(reinterpret_cast<const char*>(block.wire()), block.size());
jeraldabraham20b2dc52014-03-15 22:17:57 -0700171 }
172 }
173
174 void
175 onTimeout(const ndn::Interest& interest)
176 {
177 }
178
179 void
180 run()
181 {
182 try
183 {
184 m_face.expressInterest(createInterestPacket(),
185 ndn::func_lib::bind(&NdnTlvPeek::onData,
186 this, _1, _2),
187 ndn::func_lib::bind(&NdnTlvPeek::onTimeout,
188 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;
224 ndn::ptr_lib::shared_ptr<boost::asio::io_service> m_ioService;
225 ndn::Face m_face;
226};
227
228}
229
230int
231main(int argc, char* argv[])
232{
233 int option;
234 ndntlvpeek::NdnTlvPeek ndnTlvPeek (argv[0]);
Alexander Afanasyevb47d5382014-05-05 14:35:03 -0700235 while ((option = getopt(argc, argv, "hfrm:M:l:pw:V")) != -1) {
236 switch (option) {
237 case 'h':
238 ndnTlvPeek.usage();
239 break;
240 case 'f':
241 ndnTlvPeek.setMustBeFresh();
242 break;
243 case 'r':
244 ndnTlvPeek.setRightmostChildSelector();
245 break;
246 case 'm':
247 ndnTlvPeek.setMinSuffixComponents(atoi(optarg));
248 break;
249 case 'M':
250 ndnTlvPeek.setMaxSuffixComponents(atoi(optarg));
251 break;
252 case 'l':
253 ndnTlvPeek.setInterestLifetime(atoi(optarg));
254 break;
255 case 'p':
256 ndnTlvPeek.setPayloadOnly();
257 break;
258 case 'w':
259 ndnTlvPeek.setTimeout(atoi(optarg));
260 break;
261 case 'V':
262 std::cout << NFD_VERSION_BUILD_STRING << std::endl;
263 return 0;
264 default:
265 ndnTlvPeek.usage();
266 break;
jeraldabraham20b2dc52014-03-15 22:17:57 -0700267 }
Alexander Afanasyevb47d5382014-05-05 14:35:03 -0700268 }
jeraldabraham20b2dc52014-03-15 22:17:57 -0700269
270 argc -= optind;
271 argv += optind;
272
273 if (argv[0] == 0)
274 ndnTlvPeek.usage();
275
276 ndnTlvPeek.setPrefixName(argv[0]);
277 ndnTlvPeek.run();
278
279 if (ndnTlvPeek.isDataReceived())
280 return 0;
281 else
282 return 1;
283}