face-helpers.hpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2014-2024, 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 #ifndef NFD_TOOLS_NFDC_FACE_HELPERS_HPP
27 #define NFD_TOOLS_NFDC_FACE_HELPERS_HPP
28 
29 #include "core/common.hpp"
30 #include "execute-command.hpp"
31 
32 #include <ndn-cxx/mgmt/nfd/face-query-filter.hpp>
33 #include <ndn-cxx/mgmt/nfd/face-status.hpp>
34 #include <ndn-cxx/net/face-uri.hpp>
35 
36 #include <set>
37 
38 namespace nfd::tools::nfdc {
39 
40 using ndn::FaceUri;
41 using ndn::nfd::FaceQueryFilter;
42 using ndn::nfd::FaceStatus;
43 
47 class FindFace : noncopyable
48 {
49 public:
50  enum class Code {
51  OK = 0,
52  ERROR = 1,
53  NOT_FOUND = 3,
54  CANONIZE_ERROR = 4,
55  AMBIGUOUS = 5,
56  NOT_STARTED = -1,
57  IN_PROGRESS = -2,
58  };
59 
61  {
62  LOCAL_URI = 1
63  };
64 
65  explicit
67 
71  Code
72  execute(const FaceUri& faceUri, bool allowMulti = false);
73 
77  Code
78  execute(uint64_t faceId);
79 
85  Code
86  execute(const std::any& faceIdOrUri, bool allowMulti = false);
87 
91  Code
92  execute(const FaceQueryFilter& filter, bool allowMulti = false);
93 
96  const std::vector<FaceStatus>&
97  getResults() const
98  {
99  return m_results;
100  }
101 
104  std::set<uint64_t>
105  getFaceIds() const;
106 
110  const FaceStatus&
111  getFaceStatus() const;
112 
113  uint64_t
114  getFaceId() const
115  {
116  return this->getFaceStatus().getFaceId();
117  }
118 
119  const std::string&
121  {
122  return m_errorReason;
123  }
124 
127  void
128  printDisambiguation(std::ostream& os, DisambiguationStyle style) const;
129 
130 private:
131  std::optional<FaceUri>
132  canonize(const std::string& fieldName, const FaceUri& uri);
133 
138  void
139  query();
140 
141 private:
142  ExecuteContext& m_ctx;
143  FaceQueryFilter m_filter;
144  Code m_res = Code::NOT_STARTED;
145  std::vector<FaceStatus> m_results;
146  std::string m_errorReason;
147 };
148 
153 std::pair<std::optional<FaceUri>, std::string>
154 canonize(ExecuteContext& ctx, const FaceUri& uri);
155 
163 std::pair<FindFace::Code, std::string>
164 canonizeErrorHelper(const FaceUri& uri,
165  const std::string& error,
166  const std::string& field = "");
167 
168 } // namespace nfd::tools::nfdc
169 
170 #endif // NFD_TOOLS_NFDC_FACE_HELPERS_HPP
Context for command execution.
Procedure to find a face.
std::set< uint64_t > getFaceIds() const
const FaceStatus & getFaceStatus() const
FindFace(ExecuteContext &ctx)
uint64_t getFaceId() const
@ AMBIGUOUS
found multiple faces and allowMulti is false
@ CANONIZE_ERROR
error during FaceUri canonization
@ OK
found exactly one face, or found multiple faces when allowMulti is true
Code execute(const FaceUri &faceUri, bool allowMulti=false)
Find face by FaceUri.
const std::vector< FaceStatus > & getResults() const
const std::string & getErrorReason() const
void printDisambiguation(std::ostream &os, DisambiguationStyle style) const
Print results for disambiguation.
std::pair< FindFace::Code, std::string > canonizeErrorHelper(const FaceUri &uri, const std::string &error, const std::string &field)
Helper to generate exit code and error message for face canonization failures.
std::pair< std::optional< FaceUri >, std::string > canonize(ExecuteContext &ctx, const FaceUri &uri)
Canonize a FaceUri.