29 #include <boost/program_options.hpp>
30 #include <boost/regex.hpp>
36 using ndn::nfd::ControlParameters;
42 : m_flags(
ndn::
nfd::ROUTE_FLAG_CHILD_INHERIT)
43 , m_cost(DEFAULT_COST)
44 , m_origin(
ndn::
nfd::ROUTE_ORIGIN_STATIC)
45 , m_expires(DEFAULT_EXPIRATION_PERIOD)
46 , m_facePersistency(
ndn::
nfd::FACE_PERSISTENCY_PERSISTENT)
48 , m_controller(face, keyChain)
55 if (command ==
"add-nexthop") {
60 else if (command ==
"remove-nexthop") {
65 else if (command ==
"register") {
70 else if (command ==
"unregister") {
75 else if (command ==
"create") {
80 else if (command ==
"destroy") {
85 else if (command ==
"set-strategy") {
90 else if (command ==
"unset-strategy") {
108 [
this] (
const uint32_t faceId) {
109 ControlParameters parameters;
115 m_controller.start<ndn::nfd::FibAddNextHopCommand>(parameters,
116 bind(&LegacyNfdc::onSuccess,
this, _1,
"Nexthop insertion succeeded"),
117 bind(&LegacyNfdc::onError,
this, _1,
"Nexthop insertion failed"));
119 bind(&LegacyNfdc::onObtainFaceIdFailure,
this, _1));
129 [
this] (
const uint32_t faceId) {
130 ControlParameters parameters;
135 m_controller.start<ndn::nfd::FibRemoveNextHopCommand>(parameters,
136 bind(&LegacyNfdc::onSuccess,
this, _1,
"Nexthop removal succeeded"),
137 bind(&LegacyNfdc::onError,
this, _1,
"Nexthop removal failed"));
139 bind(&LegacyNfdc::onObtainFaceIdFailure,
this, _1));
149 [
this] (
const uint32_t faceId) {
150 ControlParameters parameters;
159 parameters.setExpirationPeriod(
m_expires);
161 m_controller.start<ndn::nfd::RibRegisterCommand>(parameters,
162 bind(&LegacyNfdc::onSuccess,
this, _1,
"Successful in name registration"),
163 bind(&LegacyNfdc::onError,
this, _1,
"Failed in name registration"));
165 bind(&LegacyNfdc::onObtainFaceIdFailure,
this, _1));
175 [
this] (
const uint32_t faceId) {
176 ControlParameters parameters;
182 m_controller.start<ndn::nfd::RibUnregisterCommand>(parameters,
183 bind(&LegacyNfdc::onSuccess,
this, _1,
"Successful in unregistering name"),
184 bind(&LegacyNfdc::onError,
this, _1,
"Failed in unregistering name"));
186 bind(&LegacyNfdc::onObtainFaceIdFailure,
this, _1));
190 LegacyNfdc::onCanonizeFailure(
const std::string& reason)
192 BOOST_THROW_EXCEPTION(Error(reason));
196 LegacyNfdc::onObtainFaceIdFailure(
const std::string& message)
198 BOOST_THROW_EXCEPTION(Error(message));
204 boost::regex e(
"^[a-z0-9]+\\:.*");
206 BOOST_THROW_EXCEPTION(
Error(
"invalid uri format"));
211 faceUri.canonize(bind(&LegacyNfdc::startFaceCreate,
this, _1),
212 bind(&LegacyNfdc::onCanonizeFailure,
this, _1),
213 m_face.getIoService(), time::seconds(4));
217 LegacyNfdc::startFaceCreate(
const FaceUri& canonicalUri)
219 ControlParameters parameters;
220 parameters.setUri(canonicalUri.toString());
223 m_controller.start<ndn::nfd::FaceCreateCommand>(parameters,
224 bind(&LegacyNfdc::onSuccess,
this, _1,
"Face creation succeeded"),
225 bind(&LegacyNfdc::onError,
this, _1,
"Face creation failed"));
231 ControlParameters parameters;
235 [
this] (
const uint32_t faceId) {
236 ControlParameters faceParameters;
237 faceParameters.setFaceId(faceId);
239 m_controller.start<ndn::nfd::FaceDestroyCommand>(faceParameters,
240 bind(&LegacyNfdc::onSuccess,
this, _1,
"Face destroy succeeded"),
241 bind(&LegacyNfdc::onError,
this, _1,
"Face destroy failed"));
243 bind(&LegacyNfdc::onObtainFaceIdFailure,
this, _1));
252 ControlParameters parameters;
255 .setStrategy(strategy);
257 m_controller.start<ndn::nfd::StrategyChoiceSetCommand>(parameters,
258 bind(&LegacyNfdc::onSuccess,
this, _1,
"Successfully set strategy choice"),
259 bind(&LegacyNfdc::onError,
this, _1,
"Failed to set strategy choice"));
267 ControlParameters parameters;
268 parameters.setName(name);
270 m_controller.start<ndn::nfd::StrategyChoiceUnsetCommand>(parameters,
271 bind(&LegacyNfdc::onSuccess,
this, _1,
"Successfully unset strategy choice"),
272 bind(&LegacyNfdc::onError,
this, _1,
"Failed to unset strategy choice"));
276 LegacyNfdc::onSuccess(
const ControlParameters& commandSuccessResult,
const std::string& message)
278 std::cout << message <<
": " << commandSuccessResult << std::endl;
282 LegacyNfdc::onError(
const ndn::nfd::ControlResponse& response,
const std::string& message)
284 std::ostringstream os;
285 os << message <<
": " << response.getText() <<
" (code: " << response.getCode() <<
")";
286 BOOST_THROW_EXCEPTION(Error(os.str()));
292 std::cout <<
"Usage:\n"
293 "nfdc [-h] [-V] COMMAND [<Command Options>]\n"
294 " -h print usage and exit\n"
295 " -V print version and exit\n"
297 " COMMAND can be one of the following:\n"
298 " register [-I] [-C] [-c cost] [-e expiration time] [-o origin] name <faceId | faceUri>\n"
299 " register name to the given faceId or faceUri\n"
300 " -I: unset CHILD_INHERIT flag\n"
301 " -C: set CAPTURE flag\n"
302 " -c: specify cost (default 0)\n"
303 " -e: specify expiration time in ms\n"
304 " (by default the entry remains in FIB for the lifetime of the associated face)\n"
305 " -o: specify origin\n"
306 " 0 for Local producer applications, 128 for NLSR, 255(default) for static routes\n"
307 " unregister [-o origin] name <faceId | faceUri>\n"
308 " unregister name from the given faceId\n"
309 " create [-P] <faceUri> \n"
310 " Create a face in one of the following formats:\n"
311 " UDP unicast: udp[4|6]://<remote-IP-or-host>[:<remote-port>]\n"
312 " TCP: tcp[4|6]://<remote-IP-or-host>[:<remote-port>] \n"
313 " -P: create permanent (instead of persistent) face\n"
314 " destroy <faceId | faceUri> \n"
316 " set-strategy <name> <strategy> \n"
317 " Set the strategy for a namespace \n"
318 " unset-strategy <name> \n"
319 " Unset the strategy for a namespace \n"
320 " add-nexthop [-c <cost>] <name> <faceId | faceUri>\n"
321 " Add a nexthop to a FIB entry\n"
322 " -c: specify cost (default 0)\n"
323 " remove-nexthop <name> <faceId | faceUri> \n"
324 " Remove a nexthop from a FIB entry\n"
333 const std::string& subcommand = ctx.
noun;
334 auto args = ctx.
args.
get<std::vector<std::string>>(
"args");
335 bool wantUnsetChildInherit =
false;
336 bool wantCapture =
false;
337 bool wantPermanentFace =
false;
338 int64_t expires = -1;
340 namespace po = boost::program_options;
341 po::options_description options;
342 options.add_options()
343 (
",I", po::bool_switch(&wantUnsetChildInherit))
344 (
",C", po::bool_switch(&wantCapture))
345 (
",c", po::value<uint64_t>(&p.
m_cost))
346 (
",e", po::value<int64_t>(&expires))
347 (
",o", po::value<uint64_t>(&p.
m_origin))
348 (
",P", po::bool_switch(&wantPermanentFace));
349 po::variables_map vm;
350 std::vector<std::string> unparsed;
352 po::parsed_options parsed = po::command_line_parser(args).options(options).allow_unregistered().run();
353 unparsed = po::collect_unrecognized(parsed.options, po::include_positional);
354 po::store(parsed, vm);
357 catch (
const po::error& e) {
358 std::cerr << e.what() << std::endl;
363 if (wantUnsetChildInherit) {
364 p.
m_flags &= ~(ndn::nfd::ROUTE_FLAG_CHILD_INHERIT);
367 p.
m_flags |= ndn::nfd::ROUTE_FLAG_CAPTURE;
371 p.
m_expires = time::milliseconds(expires);
373 if (wantPermanentFace) {
377 if (std::any_of(unparsed.begin(), unparsed.end(),
378 [] (
const std::string& s) {
return s.empty() || s[0] ==
'-'; })) {
390 ctx.
face.processEvents();
Copyright (c) 2014-2016, Regents of the University of California, Arizona Board of Regents...
context for command execution
Copyright (c) 2014-2015, Regents of the University of California, Arizona Board of Regents...
const CommandArguments & args