legacy-nfdc.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
26 #include "legacy-nfdc.hpp"
27 #include "face-id-fetcher.hpp"
28 
29 #include <boost/program_options.hpp>
30 #include <boost/regex.hpp>
31 
32 namespace nfd {
33 namespace tools {
34 namespace nfdc {
35 
36 using ndn::nfd::ControlParameters;
37 
38 const time::milliseconds LegacyNfdc::DEFAULT_EXPIRATION_PERIOD = time::milliseconds::max();
39 const uint64_t LegacyNfdc::DEFAULT_COST = 0;
40 
41 LegacyNfdc::LegacyNfdc(Face& face, KeyChain& keyChain)
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)
47  , m_face(face)
48  , m_controller(face, keyChain)
49 {
50 }
51 
52 bool
53 LegacyNfdc::dispatch(const std::string& command)
54 {
55  if (command == "add-nexthop") {
56  if (m_commandLineArguments.size() != 2)
57  return false;
58  fibAddNextHop();
59  }
60  else if (command == "remove-nexthop") {
61  if (m_commandLineArguments.size() != 2)
62  return false;
64  }
65  else if (command == "register") {
66  if (m_commandLineArguments.size() != 2)
67  return false;
69  }
70  else if (command == "unregister") {
71  if (m_commandLineArguments.size() != 2)
72  return false;
74  }
75  else if (command == "create") {
76  if (m_commandLineArguments.size() != 1)
77  return false;
78  faceCreate();
79  }
80  else if (command == "destroy") {
81  if (m_commandLineArguments.size() != 1)
82  return false;
83  faceDestroy();
84  }
85  else if (command == "set-strategy") {
86  if (m_commandLineArguments.size() != 2)
87  return false;
89  }
90  else if (command == "unset-strategy") {
91  if (m_commandLineArguments.size() != 1)
92  return false;
94  }
95  else
96  return false;
97 
98  return true;
99 }
100 
101 void
103 {
105  const std::string& faceName = m_commandLineArguments[1];
106 
107  FaceIdFetcher::start(m_face, m_controller, faceName, true,
108  [this] (const uint32_t faceId) {
109  ControlParameters parameters;
110  parameters
111  .setName(m_name)
112  .setCost(m_cost)
113  .setFaceId(faceId);
114 
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"));
118  },
119  bind(&LegacyNfdc::onObtainFaceIdFailure, this, _1));
120 }
121 
122 void
124 {
126  const std::string& faceName = m_commandLineArguments[1];
127 
128  FaceIdFetcher::start(m_face, m_controller, faceName, false,
129  [this] (const uint32_t faceId) {
130  ControlParameters parameters;
131  parameters
132  .setName(m_name)
133  .setFaceId(faceId);
134 
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"));
138  },
139  bind(&LegacyNfdc::onObtainFaceIdFailure, this, _1));
140 }
141 
142 void
144 {
146  const std::string& faceName = m_commandLineArguments[1];
147 
148  FaceIdFetcher::start(m_face, m_controller, faceName, true,
149  [this] (const uint32_t faceId) {
150  ControlParameters parameters;
151  parameters
152  .setName(m_name)
153  .setCost(m_cost)
154  .setFlags(m_flags)
155  .setOrigin(m_origin)
156  .setFaceId(faceId);
157 
159  parameters.setExpirationPeriod(m_expires);
160 
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"));
164  },
165  bind(&LegacyNfdc::onObtainFaceIdFailure, this, _1));
166 }
167 
168 void
170 {
172  const std::string& faceName = m_commandLineArguments[1];
173 
174  FaceIdFetcher::start(m_face, m_controller, faceName, false,
175  [this] (const uint32_t faceId) {
176  ControlParameters parameters;
177  parameters
178  .setName(m_name)
179  .setFaceId(faceId)
180  .setOrigin(m_origin);
181 
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"));
185  },
186  bind(&LegacyNfdc::onObtainFaceIdFailure, this, _1));
187 }
188 
189 void
190 LegacyNfdc::onCanonizeFailure(const std::string& reason)
191 {
192  BOOST_THROW_EXCEPTION(Error(reason));
193 }
194 
195 void
196 LegacyNfdc::onObtainFaceIdFailure(const std::string& message)
197 {
198  BOOST_THROW_EXCEPTION(Error(message));
199 }
200 
201 void
203 {
204  boost::regex e("^[a-z0-9]+\\:.*");
205  if (!boost::regex_match(m_commandLineArguments[0], e))
206  BOOST_THROW_EXCEPTION(Error("invalid uri format"));
207 
208  FaceUri faceUri;
209  faceUri.parse(m_commandLineArguments[0]);
210 
211  faceUri.canonize(bind(&LegacyNfdc::startFaceCreate, this, _1),
212  bind(&LegacyNfdc::onCanonizeFailure, this, _1),
213  m_face.getIoService(), time::seconds(4));
214 }
215 
216 void
217 LegacyNfdc::startFaceCreate(const FaceUri& canonicalUri)
218 {
219  ControlParameters parameters;
220  parameters.setUri(canonicalUri.toString());
221  parameters.setFacePersistency(m_facePersistency);
222 
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"));
226 }
227 
228 void
230 {
231  ControlParameters parameters;
232  const std::string& faceName = m_commandLineArguments[0];
233 
234  FaceIdFetcher::start(m_face, m_controller, faceName, false,
235  [this] (const uint32_t faceId) {
236  ControlParameters faceParameters;
237  faceParameters.setFaceId(faceId);
238 
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"));
242  },
243  bind(&LegacyNfdc::onObtainFaceIdFailure, this, _1));
244 }
245 
246 void
248 {
249  const std::string& name = m_commandLineArguments[0];
250  const std::string& strategy = m_commandLineArguments[1];
251 
252  ControlParameters parameters;
253  parameters
254  .setName(name)
255  .setStrategy(strategy);
256 
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"));
260 }
261 
262 void
264 {
265  const std::string& name = m_commandLineArguments[0];
266 
267  ControlParameters parameters;
268  parameters.setName(name);
269 
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"));
273 }
274 
275 void
276 LegacyNfdc::onSuccess(const ControlParameters& commandSuccessResult, const std::string& message)
277 {
278  std::cout << message << ": " << commandSuccessResult << std::endl;
279 }
280 
281 void
282 LegacyNfdc::onError(const ndn::nfd::ControlResponse& response, const std::string& message)
283 {
284  std::ostringstream os;
285  os << message << ": " << response.getText() << " (code: " << response.getCode() << ")";
286  BOOST_THROW_EXCEPTION(Error(os.str()));
287 }
288 
289 void
291 {
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"
296  "\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"
315  " Destroy a face\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"
325  << std::endl;
326 }
327 
328 int
330 {
331  LegacyNfdc p(ctx.face, ctx.keyChain);
332 
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;
339 
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;
351  try {
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);
355  po::notify(vm);
356  }
357  catch (const po::error& e) {
358  std::cerr << e.what() << std::endl;
359  legacyNfdcUsage();
360  return 2;
361  }
362 
363  if (wantUnsetChildInherit) {
364  p.m_flags &= ~(ndn::nfd::ROUTE_FLAG_CHILD_INHERIT);
365  }
366  if (wantCapture) {
367  p.m_flags |= ndn::nfd::ROUTE_FLAG_CAPTURE;
368  }
369  if (expires >= 0) {
370  // accept negative values as no expiration
371  p.m_expires = time::milliseconds(expires);
372  }
373  if (wantPermanentFace) {
374  p.m_facePersistency = ndn::nfd::FACE_PERSISTENCY_PERMANENT;
375  }
376 
377  if (std::any_of(unparsed.begin(), unparsed.end(),
378  [] (const std::string& s) { return s.empty() || s[0] == '-'; })) {
379  // unrecognized -option
380  legacyNfdcUsage();
381  return 2;
382  }
383  p.m_commandLineArguments = unparsed;
384 
385  bool isOk = p.dispatch(subcommand);
386  if (!isOk) {
387  legacyNfdcUsage();
388  return 2;
389  }
390  ctx.face.processEvents();
391  return 0;
392 }
393 
394 } // namespace nfdc
395 } // namespace tools
396 } // namespace nfd
void ribUnregisterPrefix()
Unregisters name from the given faceId/faceUri.
Copyright (c) 2014-2016, Regents of the University of California, Arizona Board of Regents...
Definition: nfd.hpp:35
void strategyChoiceSet()
Sets the strategy for a namespace.
context for command execution
bool dispatch(const std::string &cmd)
Definition: legacy-nfdc.cpp:53
static const uint64_t DEFAULT_COST
Definition: legacy-nfdc.hpp:40
ndn::nfd::FacePersistency m_facePersistency
T get(const std::string &key, const T &defaultValue=T()) const
void strategyChoiceUnset()
Unset the strategy for a namespace.
void fibAddNextHop()
Adds a nexthop to a FIB entry.
void ribRegisterPrefix()
Registers name to the given faceId or faceUri.
void fibRemoveNextHop()
Removes a nexthop from an existing FIB entry.
LegacyNfdc(Face &face, KeyChain &keyChain)
Definition: legacy-nfdc.cpp:41
Copyright (c) 2014-2015, Regents of the University of California, Arizona Board of Regents...
Definition: algorithm.hpp:32
std::vector< std::string > m_commandLineArguments
const CommandArguments & args
int legacyNfdcMain(ExecuteContext &ctx)
void faceCreate()
Creates new face.
time::milliseconds m_expires
static void start(ndn::Face &face, ndn::nfd::Controller &controller, const std::string &input, bool allowCreate, const SuccessCallback &onSucceed, const FailureCallback &onFail)
obtain FaceId from input
void faceDestroy()
Destroys face.
static const time::milliseconds DEFAULT_EXPIRATION_PERIOD
Definition: legacy-nfdc.hpp:39