blob: 266a550a8c9ca07073a32c0073de817e47016367 [file] [log] [blame]
Junxiao Shi38f4ce92016-08-04 10:01:52 +00001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2014-2016, 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.
10 *
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/>.
24 */
25
26#include "rib-module.hpp"
27#include "format-helpers.hpp"
28
29namespace nfd {
30namespace tools {
Junxiao Shi331ade72016-08-19 14:07:19 +000031namespace nfdc {
Junxiao Shi38f4ce92016-08-04 10:01:52 +000032
33void
34RibModule::fetchStatus(Controller& controller,
35 const function<void()>& onSuccess,
Junxiao Shi29b41282016-08-22 03:47:02 +000036 const Controller::DatasetFailCallback& onFailure,
Junxiao Shi38f4ce92016-08-04 10:01:52 +000037 const CommandOptions& options)
38{
39 controller.fetch<ndn::nfd::RibDataset>(
40 [this, onSuccess] (const std::vector<RibEntry>& result) {
41 m_status = result;
42 onSuccess();
43 },
44 onFailure, options);
45}
46
47void
48RibModule::formatStatusXml(std::ostream& os) const
49{
50 os << "<rib>";
51 for (const RibEntry& item : m_status) {
52 this->formatItemXml(os, item);
53 }
54 os << "</rib>";
55}
56
57void
58RibModule::formatItemXml(std::ostream& os, const RibEntry& item) const
59{
60 os << "<ribEntry>";
61
62 os << "<prefix>" << xml::Text{item.getName().toUri()} << "</prefix>";
63
64 os << "<routes>";
65 for (const Route& route : item.getRoutes()) {
66 os << "<route>"
67 << "<faceId>" << route.getFaceId() << "</faceId>"
68 << "<origin>" << route.getOrigin() << "</origin>"
69 << "<cost>" << route.getCost() << "</cost>";
70 if (route.getFlags() == ndn::nfd::ROUTE_FLAGS_NONE) {
71 os << "<flags/>";
72 }
73 else {
74 os << "<flags>";
75 if (route.isChildInherit()) {
76 os << "<childInherit/>";
77 }
78 if (route.isRibCapture()) {
79 os << "<ribCapture/>";
80 }
81 os << "</flags>";
82 }
83 if (!route.hasInfiniteExpirationPeriod()) {
84 os << "<expirationPeriod>"
85 << xml::formatDuration(route.getExpirationPeriod())
86 << "</expirationPeriod>";
87 }
88 os << "</route>";
89 }
90 os << "</routes>";
91
92 os << "</ribEntry>";
93}
94
95void
96RibModule::formatStatusText(std::ostream& os) const
97{
98 os << "RIB:\n";
99 for (const RibEntry& item : m_status) {
100 this->formatItemText(os, item);
101 }
102}
103
104void
105RibModule::formatItemText(std::ostream& os, const RibEntry& item) const
106{
107 os << " " << item.getName() << " route={";
108
109 text::Separator sep(", ");
110 for (const Route& route : item.getRoutes()) {
111 os << sep
112 << "faceid=" << route.getFaceId()
113 << " (origin=" << route.getOrigin()
114 << " cost=" << route.getCost();
115 if (!route.hasInfiniteExpirationPeriod()) {
116 os << " expires=" << text::formatDuration(route.getExpirationPeriod());
117 }
118 if (route.isChildInherit()) {
119 os << " ChildInherit";
120 }
121 if (route.isRibCapture()) {
122 os << " RibCapture";
123 }
124 os << ")";
125 }
126
127 os << "}";
128 os << "\n";
129}
130
Junxiao Shi331ade72016-08-19 14:07:19 +0000131} // namespace nfdc
Junxiao Shi38f4ce92016-08-04 10:01:52 +0000132} // namespace tools
133} // namespace nfd