45 parser.
addAlias(
"route",
"list",
"");
49 .
setTitle(
"show routes toward a prefix")
77 auto nexthopIt = ctx.
args.find(
"nexthop");
78 std::set<uint64_t> nexthops;
81 if (nexthopIt != ctx.
args.end()) {
85 ctx.
exitCode =
static_cast<int>(res);
95 BOOST_ASSERT_MSG(
false,
"unexpected FindFace result");
102 listRoutesImpl(ctx, [&] (
const RibEntry& entry,
const Route& route) {
103 return (nexthops.empty() || nexthops.count(route.getFaceId()) > 0) &&
104 (!origin || route.getOrigin() == *origin);
111 auto prefix = ctx.
args.
get<Name>(
"prefix");
113 listRoutesImpl(ctx, [&] (
const RibEntry& entry,
const Route& route) {
114 return entry.getName() == prefix;
119 RibModule::listRoutesImpl(
ExecuteContext& ctx,
const RoutePredicate& filter)
122 [&] (
const std::vector<RibEntry>& dataset) {
123 bool hasRoute =
false;
124 for (
const RibEntry& entry : dataset) {
125 for (
const Route& route : entry.getRoutes()) {
126 if (filter(entry, route)) {
128 formatRouteText(ctx.
out, entry, route,
true);
136 ctx.
err <<
"Route not found\n";
142 ctx.
face.processEvents();
148 auto prefix = ctx.
args.
get<Name>(
"prefix");
149 auto nexthop = ctx.
args.at(
"nexthop");
150 auto origin = ctx.
args.
get<RouteOrigin>(
"origin", ndn::nfd::ROUTE_ORIGIN_STATIC);
151 auto cost = ctx.
args.
get<uint64_t>(
"cost", 0);
152 bool wantChildInherit = !ctx.
args.
get<
bool>(
"no-inherit",
false);
153 bool wantCapture = ctx.
args.
get<
bool>(
"capture",
false);
156 auto registerRoute = [&] (uint64_t faceId) {
157 ControlParameters registerParams;
163 .setFlags((wantChildInherit ? ndn::nfd::ROUTE_FLAG_CHILD_INHERIT : ndn::nfd::ROUTE_FLAGS_NONE) |
164 (wantCapture ? ndn::nfd::ROUTE_FLAG_CAPTURE : ndn::nfd::ROUTE_FLAGS_NONE));
166 registerParams.setExpirationPeriod(time::milliseconds(*expiresMillis));
169 ctx.
controller.start<ndn::nfd::RibRegisterCommand>(
171 [&] (
const ControlParameters& resp) {
173 ctx.
out <<
"route-add-accepted ";
175 ctx.
out << ia(
"prefix") << resp.getName()
176 << ia(
"nexthop") << resp.getFaceId()
177 << ia(
"origin") << resp.getOrigin()
178 << ia(
"cost") << resp.getCost()
179 << ia(
"flags") <<
static_cast<ndn::nfd::RouteFlags
>(resp.getFlags());
180 if (resp.hasExpirationPeriod()) {
181 ctx.
out << ia(
"expires") << text::formatDuration<time::milliseconds>(resp.getExpirationPeriod()) <<
"\n";
184 ctx.
out<< ia(
"expires") <<
"never\n";
191 auto handleFaceNotFound = [&] {
192 const FaceUri* faceUri = ndn::any_cast<FaceUri>(&nexthop);
193 if (faceUri ==
nullptr) {
194 ctx.
err <<
"Face not found\n";
198 if (faceUri->getScheme() ==
"ether") {
201 ctx.
err <<
"Unable to implicitly create Ethernet faces\n";
202 ctx.
err <<
"Please create the face with 'nfdc face create' before adding the route\n";
206 optional<FaceUri> canonized;
208 std::tie(canonized, error) =
canonize(ctx, *faceUri);
212 ctx.
exitCode =
static_cast<int>(canonizationError.first);
213 ctx.
err << canonizationError.second <<
'\n';
217 ControlParameters faceCreateParams;
218 faceCreateParams.setUri(canonized->toString());
220 ctx.
controller.start<ndn::nfd::FaceCreateCommand>(
222 [&] (
const ControlParameters& resp) {
224 registerRoute(resp.getFaceId());
233 ctx.
exitCode =
static_cast<int>(res);
244 handleFaceNotFound();
247 ctx.
err <<
"Multiple faces match specified remote FaceUri. Re-run the command with a FaceId:";
252 BOOST_ASSERT_MSG(
false,
"unexpected FindFace result");
256 ctx.
face.processEvents();
262 auto prefix = ctx.
args.
get<Name>(
"prefix");
263 auto nexthop = ctx.
args.at(
"nexthop");
264 auto origin = ctx.
args.
get<RouteOrigin>(
"origin", ndn::nfd::ROUTE_ORIGIN_STATIC);
269 ctx.
exitCode =
static_cast<int>(res);
276 ctx.
err << findFace.getErrorReason() <<
'\n';
279 BOOST_ASSERT_MSG(
false,
"unexpected FindFace result");
283 for (uint64_t faceId : findFace.getFaceIds()) {
284 ControlParameters unregisterParams;
290 ctx.
controller.start<ndn::nfd::RibUnregisterCommand>(
292 [&] (
const ControlParameters& resp) {
293 ctx.
out <<
"route-removed ";
295 ctx.
out << ia(
"prefix") << resp.getName()
296 << ia(
"nexthop") << resp.getFaceId()
297 << ia(
"origin") << resp.getOrigin()
304 ctx.
face.processEvents();
309 const std::function<
void()>& onSuccess,
310 const Controller::DatasetFailCallback& onFailure,
311 const CommandOptions& options)
313 controller.fetch<ndn::nfd::RibDataset>(
314 [
this, onSuccess] (
const std::vector<RibEntry>& result) {
325 for (
const RibEntry& item : m_status) {
326 this->formatItemXml(os, item);
332 RibModule::formatItemXml(std::ostream& os,
const RibEntry& item)
const 336 os <<
"<prefix>" <<
xml::Text{item.getName().toUri()} <<
"</prefix>";
339 for (
const Route& route : item.getRoutes()) {
341 <<
"<faceId>" << route.getFaceId() <<
"</faceId>" 342 <<
"<origin>" << route.getOrigin() <<
"</origin>" 343 <<
"<cost>" << route.getCost() <<
"</cost>";
344 if (route.getFlags() == ndn::nfd::ROUTE_FLAGS_NONE) {
349 if (route.isChildInherit()) {
350 os <<
"<childInherit/>";
352 if (route.isRibCapture()) {
353 os <<
"<ribCapture/>";
357 if (route.hasExpirationPeriod()) {
358 os <<
"<expirationPeriod>" 360 <<
"</expirationPeriod>";
373 for (
const RibEntry& item : m_status) {
375 formatEntryText(os, item);
381 RibModule::formatEntryText(std::ostream& os,
const RibEntry& entry)
383 os << entry.getName() <<
" routes={";
386 for (
const Route& route : entry.getRoutes()) {
388 formatRouteText(os, entry, route,
false);
395 RibModule::formatRouteText(std::ostream& os,
const RibEntry& entry,
const Route& route,
401 os << ia(
"prefix") << entry.getName();
403 os << ia(
"nexthop") << route.getFaceId();
404 os << ia(
"origin") << route.getOrigin();
405 os << ia(
"cost") << route.getCost();
406 os << ia(
"flags") <<
static_cast<ndn::nfd::RouteFlags
>(route.getFlags());
407 if (route.hasExpirationPeriod()) {
408 os << ia(
"expires") << text::formatDuration<time::seconds>(route.getExpirationPeriod());
411 os << ia(
"expires") <<
"never";
const CommandArguments & args
Controller::CommandFailCallback makeCommandFailureHandler(const std::string &commandName)
std::ostream & out
output stream
context for command execution
int exitCode
program exit code
ndn::nfd::CommandOptions makeCommandOptions() const
Controller::DatasetFailCallback makeDatasetFailureHandler(const std::string &datasetName)
Copyright (c) 2014-2015, Regents of the University of California, Arizona Board of Regents...
print attributes of an item
std::ostream & err
error stream
print different string on first and subsequent usage