status-report.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2014-2017, 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 "status-report.hpp"
27 #include "format-helpers.hpp"
28 
29 namespace nfd {
30 namespace tools {
31 namespace nfdc {
32 
34 parseReportFormat(const std::string& s)
35 {
36  if (s == "xml") {
37  return ReportFormat::XML;
38  }
39  if (s == "text") {
40  return ReportFormat::TEXT;
41  }
42  BOOST_THROW_EXCEPTION(std::invalid_argument("unrecognized ReportFormat '" + s + "'"));
43 }
44 
45 std::ostream&
46 operator<<(std::ostream& os, ReportFormat fmt)
47 {
48  switch (fmt) {
49  case ReportFormat::XML:
50  return os << "xml";
51  case ReportFormat::TEXT:
52  return os << "text";
53  }
54  return os << static_cast<int>(fmt);
55 }
56 
57 uint32_t
58 StatusReport::collect(Face& face, KeyChain& keyChain, Validator& validator, const CommandOptions& options)
59 {
60  Controller controller(face, keyChain, validator);
61  uint32_t errorCode = 0;
62 
63  for (size_t i = 0; i < sections.size(); ++i) {
64  Module& module = *sections[i];
65  module.fetchStatus(
66  controller,
67  []{},
68  [i, &errorCode] (uint32_t code, const std::string& reason) {
69  errorCode = i * 1000000 + code;
70  },
71  options);
72  }
73 
74  this->processEvents(face);
75  return errorCode;
76 }
77 
78 void
79 StatusReport::processEvents(Face& face)
80 {
81  face.processEvents();
82 }
83 
84 void
85 StatusReport::formatXml(std::ostream& os) const
86 {
87  xml::printHeader(os);
88  for (const unique_ptr<Module>& module : sections) {
89  module->formatStatusXml(os);
90  }
91  xml::printFooter(os);
92 }
93 
94 void
95 StatusReport::formatText(std::ostream& os) const
96 {
97  for (const unique_ptr<Module>& module : sections) {
98  module->formatStatusText(os);
99  }
100 }
101 
102 } // namespace nfdc
103 } // namespace tools
104 } // namespace nfd
ReportFormat parseReportFormat(const std::string &s)
std::vector< unique_ptr< Module > > sections
modules through which status is collected
virtual void fetchStatus(Controller &controller, const function< void()> &onSuccess, const Controller::DatasetFailCallback &onFailure, const CommandOptions &options)=0
collect status from NFD
void formatXml(std::ostream &os) const
print an XML report
uint32_t collect(Face &face, KeyChain &keyChain, Validator &validator, const CommandOptions &options)
collect status via chosen sections
std::ostream & operator<<(std::ostream &os, ArgValueType vt)
void printHeader(std::ostream &os)
Copyright (c) 2014-2015, Regents of the University of California, Arizona Board of Regents...
Definition: algorithm.hpp:32
void formatText(std::ostream &os) const
print a text report
provides access to an NFD management module
Definition: module.hpp:43
void printFooter(std::ostream &os)