blob: ee0436ffe062d17725c73c30117a3cdf05102cce [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
jeraldabraham20b2dc52014-03-15 22:17:57 -070028#include <boost/asio.hpp>
29
Alexander Afanasyev4a771362014-04-24 21:29:33 -070030#include <ndn-cxx/face.hpp>
jeraldabraham20b2dc52014-03-15 22:17:57 -070031
32namespace ndntlvpeek {
33
34class NdnTlvPeek
35{
36public:
37 NdnTlvPeek(char* programName)
38 : m_programName(programName)
39 , m_mustBeFresh(false)
40 , m_isChildSelectorRightmost(false)
41 , m_minSuffixComponents(-1)
42 , m_maxSuffixComponents(-1)
43 , m_interestLifetime(-1)
44 , m_isPayloadOnlySet(false)
45 , m_timeout(-1)
46 , m_prefixName("")
47 , m_isDataReceived(false)
48 , m_ioService(new boost::asio::io_service)
49 , m_face(m_ioService)
50 {
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"
jeraldabraham20b2dc52014-03-15 22:17:57 -070066 " [-h] - print help and exit\n\n";
67 exit(1);
68 }
69
70 void
71 setMustBeFresh()
72 {
73 m_mustBeFresh = true;
74 }
75
76 void
77 setRightmostChildSelector()
78 {
79 m_isChildSelectorRightmost = true;
80 }
81
82 void
83 setMinSuffixComponents(int minSuffixComponents)
84 {
85 if (minSuffixComponents < 0)
86 usage();
87 m_minSuffixComponents = minSuffixComponents;
88 }
89
90 void
91 setMaxSuffixComponents(int maxSuffixComponents)
92 {
93 if (maxSuffixComponents < 0)
94 usage();
95 m_maxSuffixComponents = maxSuffixComponents;
96 }
97
98 void
99 setInterestLifetime(int interestLifetime)
100 {
101 if (interestLifetime < 0)
102 usage();
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700103 m_interestLifetime = ndn::time::milliseconds(interestLifetime);
jeraldabraham20b2dc52014-03-15 22:17:57 -0700104 }
105
106 void
107 setPayloadOnly()
108 {
109 m_isPayloadOnlySet = true;
110 }
111
112 void
113 setTimeout(int timeout)
114 {
115 if (timeout < 0)
116 usage();
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700117 m_timeout = ndn::time::milliseconds(timeout);
jeraldabraham20b2dc52014-03-15 22:17:57 -0700118 }
119
120 void
121 setPrefixName(char* prefixName)
122 {
123 m_prefixName = prefixName;
124 if (m_prefixName.length() == 0)
125 usage();
126 }
127
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700128 ndn::time::milliseconds
jeraldabraham20b2dc52014-03-15 22:17:57 -0700129 getDefaultInterestLifetime()
130 {
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700131 return ndn::time::seconds(4);
jeraldabraham20b2dc52014-03-15 22:17:57 -0700132 }
133
134 ndn::Interest
135 createInterestPacket()
136 {
137 ndn::Name interestName(m_prefixName);
138 ndn::Interest interestPacket(interestName);
139 if (m_mustBeFresh)
140 interestPacket.setMustBeFresh(true);
141 if (m_isChildSelectorRightmost)
142 interestPacket.setChildSelector(1);
143 if (m_minSuffixComponents >= 0)
144 interestPacket.setMinSuffixComponents(m_minSuffixComponents);
145 if (m_maxSuffixComponents >= 0)
146 interestPacket.setMaxSuffixComponents(m_maxSuffixComponents);
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700147 if (m_interestLifetime < ndn::time::milliseconds::zero())
jeraldabraham20b2dc52014-03-15 22:17:57 -0700148 interestPacket.setInterestLifetime(getDefaultInterestLifetime());
149 else
150 interestPacket.setInterestLifetime(m_interestLifetime);
151 return interestPacket;
152 }
153
154 void
155 onData(const ndn::Interest& interest, ndn::Data& data)
156 {
jeraldabraham20b2dc52014-03-15 22:17:57 -0700157 m_isDataReceived = true;
158 if (m_isPayloadOnlySet)
159 {
Junxiao Shi2d2cde12014-03-31 00:32:42 -0700160 const ndn::Block& block = data.getContent();
161 std::cout.write(reinterpret_cast<const char*>(block.value()), block.value_size());
jeraldabraham20b2dc52014-03-15 22:17:57 -0700162 }
163 else
164 {
Junxiao Shi2d2cde12014-03-31 00:32:42 -0700165 const ndn::Block& block = data.wireEncode();
166 std::cout.write(reinterpret_cast<const char*>(block.wire()), block.size());
jeraldabraham20b2dc52014-03-15 22:17:57 -0700167 }
168 }
169
170 void
171 onTimeout(const ndn::Interest& interest)
172 {
173 }
174
175 void
176 run()
177 {
178 try
179 {
180 m_face.expressInterest(createInterestPacket(),
181 ndn::func_lib::bind(&NdnTlvPeek::onData,
182 this, _1, _2),
183 ndn::func_lib::bind(&NdnTlvPeek::onTimeout,
184 this, _1));
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700185 if (m_timeout < ndn::time::milliseconds::zero())
jeraldabraham20b2dc52014-03-15 22:17:57 -0700186 {
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700187 if (m_interestLifetime < ndn::time::milliseconds::zero())
jeraldabraham20b2dc52014-03-15 22:17:57 -0700188 m_face.processEvents(getDefaultInterestLifetime());
189 else
190 m_face.processEvents(m_interestLifetime);
191 }
192 else
193 m_face.processEvents(m_timeout);
194 }
195 catch (std::exception& e)
196 {
197 std::cerr << "ERROR: " << e.what() << "\n" << std::endl;
198 exit(1);
199 }
200 }
201
202 bool
203 isDataReceived() const
204 {
205 return m_isDataReceived;
206 }
207
208private:
209
210 std::string m_programName;
211 bool m_mustBeFresh;
212 bool m_isChildSelectorRightmost;
213 int m_minSuffixComponents;
214 int m_maxSuffixComponents;
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700215 ndn::time::milliseconds m_interestLifetime;
jeraldabraham20b2dc52014-03-15 22:17:57 -0700216 bool m_isPayloadOnlySet;
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700217 ndn::time::milliseconds m_timeout;
jeraldabraham20b2dc52014-03-15 22:17:57 -0700218 std::string m_prefixName;
219 bool m_isDataReceived;
220 ndn::ptr_lib::shared_ptr<boost::asio::io_service> m_ioService;
221 ndn::Face m_face;
222};
223
224}
225
226int
227main(int argc, char* argv[])
228{
229 int option;
230 ndntlvpeek::NdnTlvPeek ndnTlvPeek (argv[0]);
231 while ((option = getopt(argc, argv, "hfrm:M:l:pw:")) != -1)
232 {
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 default:
259 ndnTlvPeek.usage();
260 }
261 }
262
263 argc -= optind;
264 argv += optind;
265
266 if (argv[0] == 0)
267 ndnTlvPeek.usage();
268
269 ndnTlvPeek.setPrefixName(argv[0]);
270 ndnTlvPeek.run();
271
272 if (ndnTlvPeek.isDataReceived())
273 return 0;
274 else
275 return 1;
276}