blob: a0e1c560b688424e51a96cb9d17525ada28e8836 [file] [log] [blame]
hilata198cadb2014-02-15 23:46:19 -06001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Syed Obaid4ae0ce32014-06-17 13:59:20 -05003 * 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
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070010 *
11 * 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/>.
Syed Obaid4ae0ce32014-06-17 13:59:20 -050024 */
Alexander Afanasyevb47d5382014-05-05 14:35:03 -070025
hilata198cadb2014-02-15 23:46:19 -060026#include "nfdc.hpp"
Alexander Afanasyevb47d5382014-05-05 14:35:03 -070027#include "version.hpp"
28
hilata198cadb2014-02-15 23:46:19 -060029#include <boost/lexical_cast.hpp>
30#include <boost/algorithm/string.hpp>
31#include <boost/algorithm/string/regex_find_format.hpp>
32#include <boost/regex.hpp>
33
34void
35usage(const char* programName)
36{
Alexander Afanasyevb47d5382014-05-05 14:35:03 -070037 std::cout << "Usage:\n" << programName << " [-h] [-V] COMMAND [<Command Options>]\n"
Alexander Afanasyev352e14e2014-03-27 16:02:12 -070038 " -h print usage and exit\n"
Alexander Afanasyevb47d5382014-05-05 14:35:03 -070039 " -V print version and exit\n"
Alexander Afanasyev352e14e2014-03-27 16:02:12 -070040 "\n"
Obaiddca50792014-04-24 18:38:40 -050041 " COMMAND can be one of the following:\n"
Syed Obaid4ae0ce32014-06-17 13:59:20 -050042 " register [-I] [-C] [-c cost] [-e expiration time] [-o origin] name <faceId | faceUri>\n"
Obaiddca50792014-04-24 18:38:40 -050043 " register name to the given faceId or faceUri\n"
44 " -I: unset CHILD_INHERIT flag\n"
45 " -C: set CAPTURE flag\n"
Syed Obaid818e8a62014-07-02 18:24:11 -050046 " -c: specify cost (default 0)\n"
47 " -e: specify expiration time in ms\n"
48 " (by default the entry remains in FIB for the lifetime of the associated face)\n"
Syed Obaid4ae0ce32014-06-17 13:59:20 -050049 " -o: specify origin\n"
Syed Obaid818e8a62014-07-02 18:24:11 -050050 " 0 for Local producer applications, 128 for NLSR, 255(default) for static routes\n"
Syed Obaidf93f9d92014-06-22 13:50:48 -050051 " unregister [-o origin] name <faceId>\n"
Obaiddca50792014-04-24 18:38:40 -050052 " unregister name from the given faceId\n"
53 " create <faceUri> \n"
Alexander Afanasyev352e14e2014-03-27 16:02:12 -070054 " Create a face in one of the following formats:\n"
55 " UDP unicast: udp[4|6]://<remote-IP-or-host>[:<remote-port>]\n"
56 " TCP: tcp[4|6]://<remote-IP-or-host>[:<remote-port>] \n"
Obaiddca50792014-04-24 18:38:40 -050057 " destroy <faceId | faceUri> \n"
Alexander Afanasyev352e14e2014-03-27 16:02:12 -070058 " Destroy a face\n"
59 " set-strategy <name> <strategy> \n"
60 " Set the strategy for a namespace \n"
61 " unset-strategy <name> \n"
62 " Unset the strategy for a namespace \n"
hilata6ee6e072014-04-20 17:10:18 -050063 " add-nexthop [-c <cost>] <name> <faceId | faceUri>\n"
64 " Add a nexthop to a FIB entry\n"
Syed Obaid818e8a62014-07-02 18:24:11 -050065 " -c: specify cost (default 0)\n"
hilata6ee6e072014-04-20 17:10:18 -050066 " remove-nexthop <name> <faceId> \n"
67 " Remove a nexthop from a FIB entry\n"
Alexander Afanasyev352e14e2014-03-27 16:02:12 -070068 << std::endl;
hilata198cadb2014-02-15 23:46:19 -060069}
70
71namespace nfdc {
Alexander Afanasyev352e14e2014-03-27 16:02:12 -070072
Syed Obaid818e8a62014-07-02 18:24:11 -050073const ndn::time::milliseconds Nfdc::DEFAULT_EXPIRATION_PERIOD = ndn::time::milliseconds::max();
Syed Obaid4ae0ce32014-06-17 13:59:20 -050074const uint64_t Nfdc::DEFAULT_COST = 0;
75
Alexander Afanasyev352e14e2014-03-27 16:02:12 -070076Nfdc::Nfdc(ndn::Face& face)
Obaiddca50792014-04-24 18:38:40 -050077 : m_flags(ROUTE_FLAG_CHILD_INHERIT)
Syed Obaid4ae0ce32014-06-17 13:59:20 -050078 , m_cost(DEFAULT_COST)
79 , m_origin(ROUTE_ORIGIN_STATIC)
80 , m_expires(DEFAULT_EXPIRATION_PERIOD)
Obaiddca50792014-04-24 18:38:40 -050081 , m_controller(face)
hilata198cadb2014-02-15 23:46:19 -060082{
83}
Alexander Afanasyev352e14e2014-03-27 16:02:12 -070084
85Nfdc::~Nfdc()
hilata198cadb2014-02-15 23:46:19 -060086{
87}
Alexander Afanasyev352e14e2014-03-27 16:02:12 -070088
hilata198cadb2014-02-15 23:46:19 -060089bool
hilata54e4eaf2014-04-10 23:38:33 -050090Nfdc::dispatch(const std::string& command)
hilata198cadb2014-02-15 23:46:19 -060091{
Obaiddca50792014-04-24 18:38:40 -050092 if (command == "add-nexthop") {
93 if (m_nOptions != 2)
hilata54e4eaf2014-04-10 23:38:33 -050094 return false;
Obaiddca50792014-04-24 18:38:40 -050095 fibAddNextHop();
hilata198cadb2014-02-15 23:46:19 -060096 }
97 else if (command == "remove-nexthop") {
hilata54e4eaf2014-04-10 23:38:33 -050098 if (m_nOptions != 2)
hilata198cadb2014-02-15 23:46:19 -060099 return false;
hilata54e4eaf2014-04-10 23:38:33 -0500100 fibRemoveNextHop();
hilata198cadb2014-02-15 23:46:19 -0600101 }
Obaiddca50792014-04-24 18:38:40 -0500102 else if (command == "register") {
103 if (m_nOptions != 2)
104 return false;
105 ribRegisterPrefix();
106 }
107 else if (command == "unregister") {
108 if (m_nOptions != 2)
109 return false;
110 ribUnregisterPrefix();
111 }
hilata198cadb2014-02-15 23:46:19 -0600112 else if (command == "create") {
hilata54e4eaf2014-04-10 23:38:33 -0500113 if (m_nOptions != 1)
hilata198cadb2014-02-15 23:46:19 -0600114 return false;
hilata54e4eaf2014-04-10 23:38:33 -0500115 faceCreate();
hilata198cadb2014-02-15 23:46:19 -0600116 }
117 else if (command == "destroy") {
hilata54e4eaf2014-04-10 23:38:33 -0500118 if (m_nOptions != 1)
hilata198cadb2014-02-15 23:46:19 -0600119 return false;
hilata54e4eaf2014-04-10 23:38:33 -0500120 faceDestroy();
hilata198cadb2014-02-15 23:46:19 -0600121 }
hilata141eaae2014-03-13 19:54:47 -0500122 else if (command == "set-strategy") {
hilata54e4eaf2014-04-10 23:38:33 -0500123 if (m_nOptions != 2)
hilata141eaae2014-03-13 19:54:47 -0500124 return false;
hilata54e4eaf2014-04-10 23:38:33 -0500125 strategyChoiceSet();
hilata141eaae2014-03-13 19:54:47 -0500126 }
127 else if (command == "unset-strategy") {
hilata54e4eaf2014-04-10 23:38:33 -0500128 if (m_nOptions != 1)
hilata141eaae2014-03-13 19:54:47 -0500129 return false;
hilata54e4eaf2014-04-10 23:38:33 -0500130 strategyChoiceUnset();
hilata141eaae2014-03-13 19:54:47 -0500131 }
hilata198cadb2014-02-15 23:46:19 -0600132 else
133 usage(m_programName);
134
135 return true;
136}
hilata198cadb2014-02-15 23:46:19 -0600137
hilata198cadb2014-02-15 23:46:19 -0600138namespace {
Alexander Afanasyev352e14e2014-03-27 16:02:12 -0700139
140inline bool
hilata198cadb2014-02-15 23:46:19 -0600141isValidUri(const std::string& input)
142{
143 // an extended regex to support the validation of uri structure
144 // boost::regex e("^[a-z0-9]+-?+[a-z0-9]+\\:\\/\\/.*");
145 boost::regex e("^[a-z0-9]+\\:.*");
146 return boost::regex_match(input, e);
147}
Alexander Afanasyev352e14e2014-03-27 16:02:12 -0700148
hilata198cadb2014-02-15 23:46:19 -0600149} // anonymous namespace
150
151void
Obaiddca50792014-04-24 18:38:40 -0500152Nfdc::fibAddNextHop()
hilata198cadb2014-02-15 23:46:19 -0600153{
Obaiddca50792014-04-24 18:38:40 -0500154 m_name = m_commandLineArguments[0];
Alexander Afanasyev352e14e2014-03-27 16:02:12 -0700155 ControlParameters parameters;
156 parameters
Obaiddca50792014-04-24 18:38:40 -0500157 .setName(m_name)
158 .setCost(m_cost);
hilata54e4eaf2014-04-10 23:38:33 -0500159
Obaiddca50792014-04-24 18:38:40 -0500160 if (!isValidUri(m_commandLineArguments[1])) {
161 try { //So the uri is not valid, may be a faceId is provided.
162 m_faceId = boost::lexical_cast<int>(m_commandLineArguments[1]);
163 }
164 catch (const std::exception& e) {
165 std::cerr << "No valid faceUri or faceId is provided"<< std::endl;
166 return;
167 }
168 parameters.setFaceId(m_faceId);
169 fibAddNextHop(parameters);
hilata54e4eaf2014-04-10 23:38:33 -0500170 }
Obaiddca50792014-04-24 18:38:40 -0500171 else {
172 ControlParameters faceParameters;
173 faceParameters.setUri(m_commandLineArguments[1]);
hilata54e4eaf2014-04-10 23:38:33 -0500174
Obaiddca50792014-04-24 18:38:40 -0500175 m_controller.start<FaceCreateCommand>(faceParameters,
176 bind(&Nfdc::fibAddNextHop, this, _1),
177 bind(&Nfdc::onError, this, _1, _2,
178 "Face creation failed"));
179 }
180}
181
182void
183Nfdc::fibAddNextHop(const ControlParameters& faceCreateResult)
184{
185 ControlParameters ribParameters;
186 ribParameters
187 .setName(m_name)
188 .setCost(m_cost)
189 .setFaceId(faceCreateResult.getFaceId());
190
191 m_controller.start<FibAddNextHopCommand>(ribParameters,
192 bind(&Nfdc::onSuccess, this, _1,
193 "Nexthop insertion succeeded"),
194 bind(&Nfdc::onError, this, _1, _2,
195 "Nexthop insertion failed"));
hilata54e4eaf2014-04-10 23:38:33 -0500196}
197
198void
199Nfdc::fibRemoveNextHop()
200{
Obaiddca50792014-04-24 18:38:40 -0500201 m_name = m_commandLineArguments[0];
202 try {
203 m_faceId = boost::lexical_cast<int>(m_commandLineArguments[1]);
204 }
205 catch (const std::exception& e) {
206 std::cerr << "No valid faceUri or faceId is provided"<< std::endl;
207 return;
208 }
hilata54e4eaf2014-04-10 23:38:33 -0500209
210 ControlParameters parameters;
211 parameters
Obaiddca50792014-04-24 18:38:40 -0500212 .setName(m_name)
213 .setFaceId(m_faceId);
hilata54e4eaf2014-04-10 23:38:33 -0500214
Obaiddca50792014-04-24 18:38:40 -0500215 m_controller.start<FibRemoveNextHopCommand>(parameters,
216 bind(&Nfdc::onSuccess, this, _1,
217 "Nexthop removal succeeded"),
218 bind(&Nfdc::onError, this, _1, _2,
219 "Nexthop removal failed"));
220}
221
222void
223Nfdc::ribRegisterPrefix()
224{
225 m_name = m_commandLineArguments[0];
226 ControlParameters parameters;
227 parameters
228 .setName(m_name)
229 .setCost(m_cost)
Syed Obaid4ae0ce32014-06-17 13:59:20 -0500230 .setFlags(m_flags)
231 .setOrigin(m_origin)
232 .setExpirationPeriod(m_expires);
Obaiddca50792014-04-24 18:38:40 -0500233
234 if (!isValidUri(m_commandLineArguments[1])) {
235 try { //So the uri is not valid, may be a faceId is provided.
236 m_faceId = boost::lexical_cast<int>(m_commandLineArguments[1]);
237 }
238 catch (const std::exception& e) {
239 std::cerr << "No valid faceUri or faceId is provided"<< std::endl;
240 return;
241 }
242 parameters.setFaceId(m_faceId);
243 ribRegisterPrefix(parameters);
244 }
245 else {
246 ControlParameters faceParameters;
247 faceParameters.setUri(m_commandLineArguments[1]);
248
249 m_controller.start<FaceCreateCommand>(faceParameters,
250 bind(&Nfdc::ribRegisterPrefix, this, _1),
251 bind(&Nfdc::onError, this, _1, _2,
252 "Face creation failed"));
253 }
254}
255
256void
257Nfdc::ribRegisterPrefix(const ControlParameters& faceCreateResult)
258{
259 ControlParameters ribParameters;
260 ribParameters
261 .setName(m_name)
262 .setCost(m_cost)
263 .setFlags(m_flags)
Syed Obaid4ae0ce32014-06-17 13:59:20 -0500264 .setExpirationPeriod(m_expires)
265 .setOrigin(m_origin)
Obaiddca50792014-04-24 18:38:40 -0500266 .setFaceId(faceCreateResult.getFaceId());
267
268 m_controller.start<RibRegisterCommand>(ribParameters,
269 bind(&Nfdc::onSuccess, this, _1,
270 "Successful in name registration"),
271 bind(&Nfdc::onError, this, _1, _2,
272 "Failed in name registration"));
273}
274
275void
276Nfdc::ribUnregisterPrefix()
277{
278 m_name = m_commandLineArguments[0];
279 try {
280 m_faceId = boost::lexical_cast<int>(m_commandLineArguments[1]);
281 }
282 catch (const std::exception& e) {
Syed Obaid4ae0ce32014-06-17 13:59:20 -0500283 std::cerr << "No valid faceId is provided" << std::endl;
Obaiddca50792014-04-24 18:38:40 -0500284 return;
285 }
286
287 ControlParameters parameters;
288 parameters
289 .setName(m_name)
Syed Obaidf93f9d92014-06-22 13:50:48 -0500290 .setFaceId(m_faceId)
291 .setOrigin(m_origin);
Obaiddca50792014-04-24 18:38:40 -0500292
293 m_controller.start<RibUnregisterCommand>(parameters,
294 bind(&Nfdc::onSuccess, this, _1,
295 "Successful in unregistering name"),
296 bind(&Nfdc::onError, this, _1, _2,
297 "Failed in unregistering name"));
hilata54e4eaf2014-04-10 23:38:33 -0500298}
299
300void
301Nfdc::faceCreate()
302{
303 if (!isValidUri(m_commandLineArguments[0]))
304 throw Error("invalid uri format");
305
306 ControlParameters parameters;
Obaiddca50792014-04-24 18:38:40 -0500307 parameters.setUri(m_commandLineArguments[0]);
Alexander Afanasyev352e14e2014-03-27 16:02:12 -0700308
Obaiddca50792014-04-24 18:38:40 -0500309 m_controller.start<FaceCreateCommand>(parameters,
310 bind(&Nfdc::onSuccess, this, _1,
311 "Face creation succeeded"),
312 bind(&Nfdc::onError, this, _1, _2,
313 "Face creation failed"));
hilata198cadb2014-02-15 23:46:19 -0600314}
Alexander Afanasyev352e14e2014-03-27 16:02:12 -0700315
hilata198cadb2014-02-15 23:46:19 -0600316void
hilata54e4eaf2014-04-10 23:38:33 -0500317Nfdc::faceDestroy()
hilata198cadb2014-02-15 23:46:19 -0600318{
Alexander Afanasyev352e14e2014-03-27 16:02:12 -0700319 ControlParameters parameters;
Obaiddca50792014-04-24 18:38:40 -0500320 if (!isValidUri(m_commandLineArguments[0])) {
321 try { //So the uri is not valid, may be a faceId is provided.
322 m_faceId = boost::lexical_cast<int>(m_commandLineArguments[0]);
323 }
324 catch (const std::exception& e) {
325 std::cerr << "No valid faceUri or faceId is provided" << std::endl;
326 return;
327 }
328 parameters.setFaceId(m_faceId);
329 faceDestroy(parameters);
330 }
331 else{
332 ControlParameters faceParameters;
333 faceParameters.setUri(m_commandLineArguments[0]);
334 m_controller.start<FaceCreateCommand>(faceParameters,
335 bind(&Nfdc::faceDestroy, this, _1),
336 bind(&Nfdc::onError, this, _1, _2,
337 "Face destroy failed"));
338 }
339}
Alexander Afanasyev352e14e2014-03-27 16:02:12 -0700340
Obaiddca50792014-04-24 18:38:40 -0500341void
342Nfdc::faceDestroy(const ControlParameters& faceCreateResult)
343{
344 ControlParameters faceParameters;
345 faceParameters.setFaceId(faceCreateResult.getFaceId());
346
347 m_controller.start<FaceDestroyCommand>(faceParameters,
348 bind(&Nfdc::onSuccess, this, _1,
349 "Face destroy succeeded"),
350 bind(&Nfdc::onError, this, _1, _2,
351 "Face destroy failed"));
hilata198cadb2014-02-15 23:46:19 -0600352}
Alexander Afanasyev352e14e2014-03-27 16:02:12 -0700353
hilata141eaae2014-03-13 19:54:47 -0500354void
hilata54e4eaf2014-04-10 23:38:33 -0500355Nfdc::strategyChoiceSet()
hilata141eaae2014-03-13 19:54:47 -0500356{
hilata54e4eaf2014-04-10 23:38:33 -0500357 const std::string& name = m_commandLineArguments[0];
358 const std::string& strategy = m_commandLineArguments[1];
Alexander Afanasyev352e14e2014-03-27 16:02:12 -0700359
360 ControlParameters parameters;
361 parameters
362 .setName(name)
363 .setStrategy(strategy);
364
Obaiddca50792014-04-24 18:38:40 -0500365 m_controller.start<StrategyChoiceSetCommand>(parameters,
366 bind(&Nfdc::onSuccess, this, _1,
367 "Successfully set strategy choice"),
368 bind(&Nfdc::onError, this, _1, _2,
369 "Failed to set strategy choice"));
hilata141eaae2014-03-13 19:54:47 -0500370}
Alexander Afanasyev352e14e2014-03-27 16:02:12 -0700371
hilata141eaae2014-03-13 19:54:47 -0500372void
hilata54e4eaf2014-04-10 23:38:33 -0500373Nfdc::strategyChoiceUnset()
hilata141eaae2014-03-13 19:54:47 -0500374{
hilata54e4eaf2014-04-10 23:38:33 -0500375 const std::string& name = m_commandLineArguments[0];
Alexander Afanasyev352e14e2014-03-27 16:02:12 -0700376
377 ControlParameters parameters;
Obaiddca50792014-04-24 18:38:40 -0500378 parameters.setName(name);
Alexander Afanasyev352e14e2014-03-27 16:02:12 -0700379
Obaiddca50792014-04-24 18:38:40 -0500380 m_controller.start<StrategyChoiceUnsetCommand>(parameters,
381 bind(&Nfdc::onSuccess, this, _1,
382 "Successfully unset strategy choice"),
383 bind(&Nfdc::onError, this, _1, _2,
384 "Failed to unset strategy choice"));
hilata141eaae2014-03-13 19:54:47 -0500385}
Alexander Afanasyev352e14e2014-03-27 16:02:12 -0700386
hilata198cadb2014-02-15 23:46:19 -0600387void
Obaiddca50792014-04-24 18:38:40 -0500388Nfdc::onSuccess(const ControlParameters& commandSuccessResult, const std::string& message)
hilata198cadb2014-02-15 23:46:19 -0600389{
Obaiddca50792014-04-24 18:38:40 -0500390 std::cout << message << ": " << commandSuccessResult << std::endl;
hilata198cadb2014-02-15 23:46:19 -0600391}
Alexander Afanasyev352e14e2014-03-27 16:02:12 -0700392
hilata198cadb2014-02-15 23:46:19 -0600393void
Alexander Afanasyev352e14e2014-03-27 16:02:12 -0700394Nfdc::onError(uint32_t code, const std::string& error, const std::string& message)
hilata198cadb2014-02-15 23:46:19 -0600395{
Alexander Afanasyev352e14e2014-03-27 16:02:12 -0700396 std::ostringstream os;
397 os << message << ": " << error << " (code: " << code << ")";
398 throw Error(os.str());
hilata141eaae2014-03-13 19:54:47 -0500399}
Alexander Afanasyev352e14e2014-03-27 16:02:12 -0700400
Obaiddca50792014-04-24 18:38:40 -0500401
402
hilata141eaae2014-03-13 19:54:47 -0500403} // namespace nfdc
hilata198cadb2014-02-15 23:46:19 -0600404
405int
406main(int argc, char** argv)
407{
408 ndn::Face face;
Alexander Afanasyev352e14e2014-03-27 16:02:12 -0700409 nfdc::Nfdc p(face);
Alexander Afanasyev9f935b82014-02-24 15:14:22 -0800410
hilata198cadb2014-02-15 23:46:19 -0600411 p.m_programName = argv[0];
hilata54e4eaf2014-04-10 23:38:33 -0500412
Obaiddca50792014-04-24 18:38:40 -0500413 if (argc < 2) {
414 usage(p.m_programName);
415 return 0;
416 }
Alexander Afanasyev352e14e2014-03-27 16:02:12 -0700417
Obaiddca50792014-04-24 18:38:40 -0500418 if (!strcmp(argv[1], "-h")) {
419 usage(p.m_programName);
420 return 0;
421 }
422
Alexander Afanasyevb47d5382014-05-05 14:35:03 -0700423 if (!strcmp(argv[1], "-V")) {
424 std::cout << NFD_VERSION_BUILD_STRING << std::endl;
425 return 0;
426 }
427
Junxiao Shi22295032014-04-29 22:57:40 -0700428 ::optind = 2; //start reading options from 2nd argument i.e. Command
Obaiddca50792014-04-24 18:38:40 -0500429 int opt;
Syed Obaid4ae0ce32014-06-17 13:59:20 -0500430 while ((opt = ::getopt(argc, argv, "ICc:e:o:")) != -1) {
Obaiddca50792014-04-24 18:38:40 -0500431 switch (opt) {
432 case 'I':
433 p.m_flags = p.m_flags & ~(nfdc::ROUTE_FLAG_CHILD_INHERIT);
434 break;
435
436 case 'C':
437 p.m_flags = p.m_flags | nfdc::ROUTE_FLAG_CAPTURE;
438 break;
439
440 case 'c':
441 try {
Syed Obaid4ae0ce32014-06-17 13:59:20 -0500442 p.m_cost = boost::lexical_cast<uint64_t>(::optarg);
Obaiddca50792014-04-24 18:38:40 -0500443 }
Junxiao Shi22295032014-04-29 22:57:40 -0700444 catch (boost::bad_lexical_cast&) {
Syed Obaid4ae0ce32014-06-17 13:59:20 -0500445 std::cerr << "Error: cost must be in unsigned integer format" << std::endl;
446 return 1;
447 }
448 break;
449
450 case 'e':
451 uint64_t expires;
452 try {
453 expires = boost::lexical_cast<uint64_t>(::optarg);
454 }
455 catch (boost::bad_lexical_cast&) {
456 std::cerr << "Error: expiration time must be in unsigned integer format" << std::endl;
457 return 1;
458 }
459 p.m_expires = ndn::time::milliseconds(expires);
460 break;
461
462 case 'o':
463 try {
464 p.m_origin = boost::lexical_cast<uint64_t>(::optarg);
465 }
466 catch (boost::bad_lexical_cast&) {
467 std::cerr << "Error: origin must be in unsigned integer format" << std::endl;
hilata198cadb2014-02-15 23:46:19 -0600468 return 1;
Obaiddca50792014-04-24 18:38:40 -0500469 }
470 break;
Junxiao Shi22295032014-04-29 22:57:40 -0700471
Obaiddca50792014-04-24 18:38:40 -0500472 default:
473 usage(p.m_programName);
474 return 1;
hilata198cadb2014-02-15 23:46:19 -0600475 }
476 }
Alexander Afanasyev9f935b82014-02-24 15:14:22 -0800477
Junxiao Shi22295032014-04-29 22:57:40 -0700478 if (argc == ::optind) {
Alexander Afanasyev9f935b82014-02-24 15:14:22 -0800479 usage(p.m_programName);
480 return 1;
481 }
482
hilata198cadb2014-02-15 23:46:19 -0600483 try {
Junxiao Shi22295032014-04-29 22:57:40 -0700484 p.m_commandLineArguments = argv + ::optind;
485 p.m_nOptions = argc - ::optind;
Obaiddca50792014-04-24 18:38:40 -0500486
487 //argv[1] points to the command, so pass it to the dispatch
488 bool isOk = p.dispatch(argv[1]);
Alexander Afanasyev352e14e2014-03-27 16:02:12 -0700489 if (!isOk) {
hilata198cadb2014-02-15 23:46:19 -0600490 usage(p.m_programName);
491 return 1;
492 }
hilata198cadb2014-02-15 23:46:19 -0600493 face.processEvents();
494 }
495 catch (const std::exception& e) {
496 std::cerr << "ERROR: " << e.what() << std::endl;
497 return 2;
498 }
499 return 0;
500}