blob: 3c087f48f669623ebc254e0d8a9673861963d864 [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);
Junxiao Shi1d7fef52017-02-02 05:33:14 +000041
42 CommandDefinition defFaceCreate("face", "create");
43 defFaceCreate
44 .setTitle("create a face")
45 .addArg("remote", ArgValueType::FACE_URI, Required::YES, Positional::YES)
46 .addArg("persistency", ArgValueType::FACE_PERSISTENCY, Required::NO, Positional::YES);
47 parser.addCommand(defFaceCreate, &FaceModule::create);
Junxiao Shi1f481fa2017-01-26 15:14:43 +000048}
49
50void
51FaceModule::show(ExecuteContext& ctx)
52{
53 uint64_t faceId = ctx.args.get<uint64_t>("id");
54
55 ndn::nfd::FaceQueryFilter filter;
56 filter.setFaceId(faceId);
57 ctx.controller.fetch<ndn::nfd::FaceQueryDataset>(
58 filter,
59 [faceId, &ctx] (const std::vector<FaceStatus>& result) {
60 if (result.size() != 1) {
61 ctx.exitCode = 3;
62 ctx.err << "Face " << faceId << " not found.\n";
63 return;
64 }
65 formatItemText(ctx.out, result.front(), true);
66 },
Junxiao Shi1d7fef52017-02-02 05:33:14 +000067 ctx.makeDatasetFailureHandler("face information"),
68 ctx.makeCommandOptions());
69
70 ctx.face.processEvents();
71}
72
73void
74FaceModule::create(ExecuteContext& ctx)
75{
76 auto faceUri = ctx.args.get<FaceUri>("remote");
77 auto persistency = ctx.args.get<FacePersistency>("persistency", FacePersistency::FACE_PERSISTENCY_PERSISTENT);
78
79 faceUri.canonize(
80 [&] (const FaceUri& canonicalUri) {
81 ctx.controller.start<ndn::nfd::FaceCreateCommand>(
82 ControlParameters().setUri(canonicalUri.toString()).setFacePersistency(persistency),
83 [&] (const ControlParameters& resp) {
84 ctx.out << "face-created ";
85 text::ItemAttributes ia;
86 ctx.out << ia("id") << resp.getFaceId()
87 << ia("remote") << resp.getUri()
88 << ia("persistency") << resp.getFacePersistency() << '\n';
89 ///\todo #3864 display localUri
90 },
91 ctx.makeCommandFailureHandler("creating face"), ///\todo #3232 update persistency upon 409
92 ctx.makeCommandOptions());
93 },
94 [&] (const std::string& canonizeError) {
95 ctx.exitCode = 4;
96 ctx.err << "Error when canonizing FaceUri: " << canonizeError << '\n';
97 },
98 ctx.face.getIoService(), ctx.getTimeout());
Junxiao Shi1f481fa2017-01-26 15:14:43 +000099
100 ctx.face.processEvents();
101}
102
103void
Junxiao Shi38f4ce92016-08-04 10:01:52 +0000104FaceModule::fetchStatus(Controller& controller,
105 const function<void()>& onSuccess,
Junxiao Shi29b41282016-08-22 03:47:02 +0000106 const Controller::DatasetFailCallback& onFailure,
Junxiao Shi38f4ce92016-08-04 10:01:52 +0000107 const CommandOptions& options)
108{
109 controller.fetch<ndn::nfd::FaceDataset>(
110 [this, onSuccess] (const std::vector<FaceStatus>& result) {
111 m_status = result;
112 onSuccess();
113 },
114 onFailure, options);
115}
116
117void
118FaceModule::formatStatusXml(std::ostream& os) const
119{
120 os << "<faces>";
121 for (const FaceStatus& item : m_status) {
122 this->formatItemXml(os, item);
123 }
124 os << "</faces>";
125}
126
127void
128FaceModule::formatItemXml(std::ostream& os, const FaceStatus& item) const
129{
130 os << "<face>";
131
132 os << "<faceId>" << item.getFaceId() << "</faceId>";
133 os << "<remoteUri>" << xml::Text{item.getRemoteUri()} << "</remoteUri>";
134 os << "<localUri>" << xml::Text{item.getLocalUri()} << "</localUri>";
135
136 if (item.hasExpirationPeriod()) {
137 os << "<expirationPeriod>" << xml::formatDuration(item.getExpirationPeriod())
138 << "</expirationPeriod>";
139 }
140 os << "<faceScope>" << item.getFaceScope() << "</faceScope>";
141 os << "<facePersistency>" << item.getFacePersistency() << "</facePersistency>";
142 os << "<linkType>" << item.getLinkType() << "</linkType>";
143
Eric Newberry6d932e82016-11-24 05:05:43 +0000144 if (item.getFlags() == 0) {
145 os << "<flags/>";
146 }
147 else {
148 os << "<flags>";
149 if (item.getFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED)) {
150 os << "<localFieldsEnabled/>";
151 }
152 os << "</flags>";
153 }
154
Junxiao Shi38f4ce92016-08-04 10:01:52 +0000155 os << "<packetCounters>";
156 os << "<incomingPackets>"
157 << "<nInterests>" << item.getNInInterests() << "</nInterests>"
158 << "<nDatas>" << item.getNInDatas() << "</nDatas>"
159 << "<nNacks>" << item.getNInNacks() << "</nNacks>"
160 << "</incomingPackets>";
161 os << "<outgoingPackets>"
162 << "<nInterests>" << item.getNOutInterests() << "</nInterests>"
163 << "<nDatas>" << item.getNOutDatas() << "</nDatas>"
164 << "<nNacks>" << item.getNOutNacks() << "</nNacks>"
165 << "</outgoingPackets>";
166 os << "</packetCounters>";
167
168 os << "<byteCounters>";
169 os << "<incomingBytes>" << item.getNInBytes() << "</incomingBytes>";
170 os << "<outgoingBytes>" << item.getNOutBytes() << "</outgoingBytes>";
171 os << "</byteCounters>";
172
173 os << "</face>";
174}
175
176void
177FaceModule::formatStatusText(std::ostream& os) const
178{
179 os << "Faces:\n";
180 for (const FaceStatus& item : m_status) {
Junxiao Shi1f481fa2017-01-26 15:14:43 +0000181 os << " ";
182 formatItemText(os, item, false);
183 os << '\n';
Junxiao Shi38f4ce92016-08-04 10:01:52 +0000184 }
185}
186
187void
Junxiao Shi1f481fa2017-01-26 15:14:43 +0000188FaceModule::formatItemText(std::ostream& os, const FaceStatus& item, bool wantMultiLine)
Junxiao Shi38f4ce92016-08-04 10:01:52 +0000189{
Junxiao Shi1f481fa2017-01-26 15:14:43 +0000190 text::ItemAttributes ia(wantMultiLine, 8);
191
192 os << ia("faceid") << item.getFaceId();
193 os << ia("remote") << item.getRemoteUri();
194 os << ia("local") << item.getLocalUri();
Junxiao Shi38f4ce92016-08-04 10:01:52 +0000195
196 if (item.hasExpirationPeriod()) {
Junxiao Shi1f481fa2017-01-26 15:14:43 +0000197 os << ia("expires") << text::formatDuration(item.getExpirationPeriod());
Junxiao Shi38f4ce92016-08-04 10:01:52 +0000198 }
199
Junxiao Shi1f481fa2017-01-26 15:14:43 +0000200 os << ia("counters")
201 << "{in={"
Junxiao Shi38f4ce92016-08-04 10:01:52 +0000202 << item.getNInInterests() << "i "
203 << item.getNInDatas() << "d "
204 << item.getNInNacks() << "n "
Junxiao Shi1f481fa2017-01-26 15:14:43 +0000205 << item.getNInBytes() << "B} "
206 << "out={"
Junxiao Shi38f4ce92016-08-04 10:01:52 +0000207 << item.getNOutInterests() << "i "
208 << item.getNOutDatas() << "d "
209 << item.getNOutNacks() << "n "
210 << item.getNOutBytes() << "B}}";
211
Junxiao Shi1f481fa2017-01-26 15:14:43 +0000212 os << ia("flags") << '{';
213 text::Separator flagSep("", " ");
214 os << flagSep << item.getFaceScope();
215 os << flagSep << item.getFacePersistency();
216 os << flagSep << item.getLinkType();
Eric Newberry6d932e82016-11-24 05:05:43 +0000217 if (item.getFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED)) {
Junxiao Shi1f481fa2017-01-26 15:14:43 +0000218 os << flagSep << "local-fields";
Eric Newberry6d932e82016-11-24 05:05:43 +0000219 }
Junxiao Shi1f481fa2017-01-26 15:14:43 +0000220 os << '}';
Eric Newberry6d932e82016-11-24 05:05:43 +0000221
Junxiao Shi1f481fa2017-01-26 15:14:43 +0000222 os << ia.end();
Junxiao Shi38f4ce92016-08-04 10:01:52 +0000223}
224
Junxiao Shi331ade72016-08-19 14:07:19 +0000225} // namespace nfdc
Junxiao Shi38f4ce92016-08-04 10:01:52 +0000226} // namespace tools
227} // namespace nfd