blob: 00e0bdb0ba0a7cf25435e0437d80a742fdfedddf [file] [log] [blame]
jeraldabraham20b2dc52014-03-15 22:17:57 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2/**
3 * Copyright (C) 2014 University of Arizona.
4 *
5 * Author: Jerald Paul Abraham <jeraldabraham@email.arizona.edu>
6 */
Junxiao Shi2d2cde12014-03-31 00:32:42 -07007
jeraldabraham20b2dc52014-03-15 22:17:57 -07008#include <boost/asio.hpp>
9
10#include <ndn-cpp-dev/face.hpp>
11
12namespace ndntlvpeek {
13
14class NdnTlvPeek
15{
16public:
17 NdnTlvPeek(char* programName)
18 : m_programName(programName)
19 , m_mustBeFresh(false)
20 , m_isChildSelectorRightmost(false)
21 , m_minSuffixComponents(-1)
22 , m_maxSuffixComponents(-1)
23 , m_interestLifetime(-1)
24 , m_isPayloadOnlySet(false)
25 , m_timeout(-1)
26 , m_prefixName("")
27 , m_isDataReceived(false)
28 , m_ioService(new boost::asio::io_service)
29 , m_face(m_ioService)
30 {
31 }
32
33 void
34 usage()
35 {
36 std::cout << "\n Usage:\n " << m_programName << " "
37 "[-f] [-r] [-m min] [-M max] [-l lifetime] [-p] [-w timeout] ndn:/name\n"
38 " Get one data item matching the name prefix and write it to stdout\n"
39 " [-f] - set MustBeFresh\n"
40 " [-r] - set ChildSelector to select rightmost child\n"
41 " [-m min] - set MinSuffixComponents\n"
42 " [-M max] - set MaxSuffixComponents\n"
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -070043 " [-l lifetime] - set InterestLifetime in time::milliseconds\n"
jeraldabraham20b2dc52014-03-15 22:17:57 -070044 " [-p] - print payload only, not full packet\n"
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -070045 " [-w timeout] - set Timeout in time::milliseconds\n"
jeraldabraham20b2dc52014-03-15 22:17:57 -070046 " [-h] - print help and exit\n\n";
47 exit(1);
48 }
49
50 void
51 setMustBeFresh()
52 {
53 m_mustBeFresh = true;
54 }
55
56 void
57 setRightmostChildSelector()
58 {
59 m_isChildSelectorRightmost = true;
60 }
61
62 void
63 setMinSuffixComponents(int minSuffixComponents)
64 {
65 if (minSuffixComponents < 0)
66 usage();
67 m_minSuffixComponents = minSuffixComponents;
68 }
69
70 void
71 setMaxSuffixComponents(int maxSuffixComponents)
72 {
73 if (maxSuffixComponents < 0)
74 usage();
75 m_maxSuffixComponents = maxSuffixComponents;
76 }
77
78 void
79 setInterestLifetime(int interestLifetime)
80 {
81 if (interestLifetime < 0)
82 usage();
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -070083 m_interestLifetime = ndn::time::milliseconds(interestLifetime);
jeraldabraham20b2dc52014-03-15 22:17:57 -070084 }
85
86 void
87 setPayloadOnly()
88 {
89 m_isPayloadOnlySet = true;
90 }
91
92 void
93 setTimeout(int timeout)
94 {
95 if (timeout < 0)
96 usage();
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -070097 m_timeout = ndn::time::milliseconds(timeout);
jeraldabraham20b2dc52014-03-15 22:17:57 -070098 }
99
100 void
101 setPrefixName(char* prefixName)
102 {
103 m_prefixName = prefixName;
104 if (m_prefixName.length() == 0)
105 usage();
106 }
107
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700108 ndn::time::milliseconds
jeraldabraham20b2dc52014-03-15 22:17:57 -0700109 getDefaultInterestLifetime()
110 {
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700111 return ndn::time::seconds(4);
jeraldabraham20b2dc52014-03-15 22:17:57 -0700112 }
113
114 ndn::Interest
115 createInterestPacket()
116 {
117 ndn::Name interestName(m_prefixName);
118 ndn::Interest interestPacket(interestName);
119 if (m_mustBeFresh)
120 interestPacket.setMustBeFresh(true);
121 if (m_isChildSelectorRightmost)
122 interestPacket.setChildSelector(1);
123 if (m_minSuffixComponents >= 0)
124 interestPacket.setMinSuffixComponents(m_minSuffixComponents);
125 if (m_maxSuffixComponents >= 0)
126 interestPacket.setMaxSuffixComponents(m_maxSuffixComponents);
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700127 if (m_interestLifetime < ndn::time::milliseconds::zero())
jeraldabraham20b2dc52014-03-15 22:17:57 -0700128 interestPacket.setInterestLifetime(getDefaultInterestLifetime());
129 else
130 interestPacket.setInterestLifetime(m_interestLifetime);
131 return interestPacket;
132 }
133
134 void
135 onData(const ndn::Interest& interest, ndn::Data& data)
136 {
jeraldabraham20b2dc52014-03-15 22:17:57 -0700137 m_isDataReceived = true;
138 if (m_isPayloadOnlySet)
139 {
Junxiao Shi2d2cde12014-03-31 00:32:42 -0700140 const ndn::Block& block = data.getContent();
141 std::cout.write(reinterpret_cast<const char*>(block.value()), block.value_size());
jeraldabraham20b2dc52014-03-15 22:17:57 -0700142 }
143 else
144 {
Junxiao Shi2d2cde12014-03-31 00:32:42 -0700145 const ndn::Block& block = data.wireEncode();
146 std::cout.write(reinterpret_cast<const char*>(block.wire()), block.size());
jeraldabraham20b2dc52014-03-15 22:17:57 -0700147 }
148 }
149
150 void
151 onTimeout(const ndn::Interest& interest)
152 {
153 }
154
155 void
156 run()
157 {
158 try
159 {
160 m_face.expressInterest(createInterestPacket(),
161 ndn::func_lib::bind(&NdnTlvPeek::onData,
162 this, _1, _2),
163 ndn::func_lib::bind(&NdnTlvPeek::onTimeout,
164 this, _1));
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700165 if (m_timeout < ndn::time::milliseconds::zero())
jeraldabraham20b2dc52014-03-15 22:17:57 -0700166 {
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700167 if (m_interestLifetime < ndn::time::milliseconds::zero())
jeraldabraham20b2dc52014-03-15 22:17:57 -0700168 m_face.processEvents(getDefaultInterestLifetime());
169 else
170 m_face.processEvents(m_interestLifetime);
171 }
172 else
173 m_face.processEvents(m_timeout);
174 }
175 catch (std::exception& e)
176 {
177 std::cerr << "ERROR: " << e.what() << "\n" << std::endl;
178 exit(1);
179 }
180 }
181
182 bool
183 isDataReceived() const
184 {
185 return m_isDataReceived;
186 }
187
188private:
189
190 std::string m_programName;
191 bool m_mustBeFresh;
192 bool m_isChildSelectorRightmost;
193 int m_minSuffixComponents;
194 int m_maxSuffixComponents;
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700195 ndn::time::milliseconds m_interestLifetime;
jeraldabraham20b2dc52014-03-15 22:17:57 -0700196 bool m_isPayloadOnlySet;
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700197 ndn::time::milliseconds m_timeout;
jeraldabraham20b2dc52014-03-15 22:17:57 -0700198 std::string m_prefixName;
199 bool m_isDataReceived;
200 ndn::ptr_lib::shared_ptr<boost::asio::io_service> m_ioService;
201 ndn::Face m_face;
202};
203
204}
205
206int
207main(int argc, char* argv[])
208{
209 int option;
210 ndntlvpeek::NdnTlvPeek ndnTlvPeek (argv[0]);
211 while ((option = getopt(argc, argv, "hfrm:M:l:pw:")) != -1)
212 {
213 switch (option) {
214 case 'h':
215 ndnTlvPeek.usage();
216 break;
217 case 'f':
218 ndnTlvPeek.setMustBeFresh();
219 break;
220 case 'r':
221 ndnTlvPeek.setRightmostChildSelector();
222 break;
223 case 'm':
224 ndnTlvPeek.setMinSuffixComponents(atoi(optarg));
225 break;
226 case 'M':
227 ndnTlvPeek.setMaxSuffixComponents(atoi(optarg));
228 break;
229 case 'l':
230 ndnTlvPeek.setInterestLifetime(atoi(optarg));
231 break;
232 case 'p':
233 ndnTlvPeek.setPayloadOnly();
234 break;
235 case 'w':
236 ndnTlvPeek.setTimeout(atoi(optarg));
237 break;
238 default:
239 ndnTlvPeek.usage();
240 }
241 }
242
243 argc -= optind;
244 argv += optind;
245
246 if (argv[0] == 0)
247 ndnTlvPeek.usage();
248
249 ndnTlvPeek.setPrefixName(argv[0]);
250 ndnTlvPeek.run();
251
252 if (ndnTlvPeek.isDataReceived())
253 return 0;
254 else
255 return 1;
256}