blob: 721a56fd1b1c10c058710dbff8993660ea4cb15f [file] [log] [blame]
Junxiao Shi38f4ce92016-08-04 10:01:52 +00001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Junxiao Shi1f481fa2017-01-26 15:14:43 +00003 * Copyright (c) 2014-2017, Regents of the University of California,
Junxiao Shi38f4ce92016-08-04 10:01:52 +00004 * 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
Junxiao Shi38f4ce92016-08-04 10:01:52 +000026#include "face-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
Junxiao Shi1f481fa2017-01-26 15:14:43 +000034FaceModule::registerCommands(CommandParser& parser)
35{
36 CommandDefinition defFaceShow("face", "show");
37 defFaceShow
38 .setTitle("show face information")
39 .addArg("id", ArgValueType::UNSIGNED, Required::YES, Positional::YES);
40 parser.addCommand(defFaceShow, &FaceModule::show);
41}
42
43void
44FaceModule::show(ExecuteContext& ctx)
45{
46 uint64_t faceId = ctx.args.get<uint64_t>("id");
47
48 ndn::nfd::FaceQueryFilter filter;
49 filter.setFaceId(faceId);
50 ctx.controller.fetch<ndn::nfd::FaceQueryDataset>(
51 filter,
52 [faceId, &ctx] (const std::vector<FaceStatus>& result) {
53 if (result.size() != 1) {
54 ctx.exitCode = 3;
55 ctx.err << "Face " << faceId << " not found.\n";
56 return;
57 }
58 formatItemText(ctx.out, result.front(), true);
59 },
60 ctx.makeDatasetFailureHandler("face information"));
61
62 ctx.face.processEvents();
63}
64
65void
Junxiao Shi38f4ce92016-08-04 10:01:52 +000066FaceModule::fetchStatus(Controller& controller,
67 const function<void()>& onSuccess,
Junxiao Shi29b41282016-08-22 03:47:02 +000068 const Controller::DatasetFailCallback& onFailure,
Junxiao Shi38f4ce92016-08-04 10:01:52 +000069 const CommandOptions& options)
70{
71 controller.fetch<ndn::nfd::FaceDataset>(
72 [this, onSuccess] (const std::vector<FaceStatus>& result) {
73 m_status = result;
74 onSuccess();
75 },
76 onFailure, options);
77}
78
79void
80FaceModule::formatStatusXml(std::ostream& os) const
81{
82 os << "<faces>";
83 for (const FaceStatus& item : m_status) {
84 this->formatItemXml(os, item);
85 }
86 os << "</faces>";
87}
88
89void
90FaceModule::formatItemXml(std::ostream& os, const FaceStatus& item) const
91{
92 os << "<face>";
93
94 os << "<faceId>" << item.getFaceId() << "</faceId>";
95 os << "<remoteUri>" << xml::Text{item.getRemoteUri()} << "</remoteUri>";
96 os << "<localUri>" << xml::Text{item.getLocalUri()} << "</localUri>";
97
98 if (item.hasExpirationPeriod()) {
99 os << "<expirationPeriod>" << xml::formatDuration(item.getExpirationPeriod())
100 << "</expirationPeriod>";
101 }
102 os << "<faceScope>" << item.getFaceScope() << "</faceScope>";
103 os << "<facePersistency>" << item.getFacePersistency() << "</facePersistency>";
104 os << "<linkType>" << item.getLinkType() << "</linkType>";
105
Eric Newberry6d932e82016-11-24 05:05:43 +0000106 if (item.getFlags() == 0) {
107 os << "<flags/>";
108 }
109 else {
110 os << "<flags>";
111 if (item.getFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED)) {
112 os << "<localFieldsEnabled/>";
113 }
114 os << "</flags>";
115 }
116
Junxiao Shi38f4ce92016-08-04 10:01:52 +0000117 os << "<packetCounters>";
118 os << "<incomingPackets>"
119 << "<nInterests>" << item.getNInInterests() << "</nInterests>"
120 << "<nDatas>" << item.getNInDatas() << "</nDatas>"
121 << "<nNacks>" << item.getNInNacks() << "</nNacks>"
122 << "</incomingPackets>";
123 os << "<outgoingPackets>"
124 << "<nInterests>" << item.getNOutInterests() << "</nInterests>"
125 << "<nDatas>" << item.getNOutDatas() << "</nDatas>"
126 << "<nNacks>" << item.getNOutNacks() << "</nNacks>"
127 << "</outgoingPackets>";
128 os << "</packetCounters>";
129
130 os << "<byteCounters>";
131 os << "<incomingBytes>" << item.getNInBytes() << "</incomingBytes>";
132 os << "<outgoingBytes>" << item.getNOutBytes() << "</outgoingBytes>";
133 os << "</byteCounters>";
134
135 os << "</face>";
136}
137
138void
139FaceModule::formatStatusText(std::ostream& os) const
140{
141 os << "Faces:\n";
142 for (const FaceStatus& item : m_status) {
Junxiao Shi1f481fa2017-01-26 15:14:43 +0000143 os << " ";
144 formatItemText(os, item, false);
145 os << '\n';
Junxiao Shi38f4ce92016-08-04 10:01:52 +0000146 }
147}
148
149void
Junxiao Shi1f481fa2017-01-26 15:14:43 +0000150FaceModule::formatItemText(std::ostream& os, const FaceStatus& item, bool wantMultiLine)
Junxiao Shi38f4ce92016-08-04 10:01:52 +0000151{
Junxiao Shi1f481fa2017-01-26 15:14:43 +0000152 text::ItemAttributes ia(wantMultiLine, 8);
153
154 os << ia("faceid") << item.getFaceId();
155 os << ia("remote") << item.getRemoteUri();
156 os << ia("local") << item.getLocalUri();
Junxiao Shi38f4ce92016-08-04 10:01:52 +0000157
158 if (item.hasExpirationPeriod()) {
Junxiao Shi1f481fa2017-01-26 15:14:43 +0000159 os << ia("expires") << text::formatDuration(item.getExpirationPeriod());
Junxiao Shi38f4ce92016-08-04 10:01:52 +0000160 }
161
Junxiao Shi1f481fa2017-01-26 15:14:43 +0000162 os << ia("counters")
163 << "{in={"
Junxiao Shi38f4ce92016-08-04 10:01:52 +0000164 << item.getNInInterests() << "i "
165 << item.getNInDatas() << "d "
166 << item.getNInNacks() << "n "
Junxiao Shi1f481fa2017-01-26 15:14:43 +0000167 << item.getNInBytes() << "B} "
168 << "out={"
Junxiao Shi38f4ce92016-08-04 10:01:52 +0000169 << item.getNOutInterests() << "i "
170 << item.getNOutDatas() << "d "
171 << item.getNOutNacks() << "n "
172 << item.getNOutBytes() << "B}}";
173
Junxiao Shi1f481fa2017-01-26 15:14:43 +0000174 os << ia("flags") << '{';
175 text::Separator flagSep("", " ");
176 os << flagSep << item.getFaceScope();
177 os << flagSep << item.getFacePersistency();
178 os << flagSep << item.getLinkType();
Eric Newberry6d932e82016-11-24 05:05:43 +0000179 if (item.getFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED)) {
Junxiao Shi1f481fa2017-01-26 15:14:43 +0000180 os << flagSep << "local-fields";
Eric Newberry6d932e82016-11-24 05:05:43 +0000181 }
Junxiao Shi1f481fa2017-01-26 15:14:43 +0000182 os << '}';
Eric Newberry6d932e82016-11-24 05:05:43 +0000183
Junxiao Shi1f481fa2017-01-26 15:14:43 +0000184 os << ia.end();
Junxiao Shi38f4ce92016-08-04 10:01:52 +0000185}
186
Junxiao Shi331ade72016-08-19 14:07:19 +0000187} // namespace nfdc
Junxiao Shi38f4ce92016-08-04 10:01:52 +0000188} // namespace tools
189} // namespace nfd