manager-base.hpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2014-2022, 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_DAEMON_MGMT_MANAGER_BASE_HPP
27 #define NFD_DAEMON_MGMT_MANAGER_BASE_HPP
28 
30 
31 #include <ndn-cxx/mgmt/dispatcher.hpp>
32 #include <ndn-cxx/mgmt/nfd/control-command.hpp>
33 #include <ndn-cxx/mgmt/nfd/control-parameters.hpp>
34 #include <ndn-cxx/mgmt/nfd/control-response.hpp>
35 
36 namespace nfd {
37 
38 using ndn::mgmt::Dispatcher;
39 using ndn::nfd::ControlCommand;
40 using ndn::nfd::ControlParameters;
41 using ndn::nfd::ControlResponse;
42 
47 class ManagerBase : noncopyable
48 {
49 public:
50  class Error : public std::runtime_error
51  {
52  public:
53  using std::runtime_error::runtime_error;
54  };
55 
56  virtual
58 
59  const std::string&
60  getModule() const
61  {
62  return m_module;
63  }
64 
65 protected:
69  ManagerBase(const std::string& module, Dispatcher& dispatcher);
70 
71  ManagerBase(const std::string& module, Dispatcher& dispatcher,
72  CommandAuthenticator& authenticator);
73 
74 NFD_PUBLIC_WITH_TESTS_ELSE_PROTECTED: // registrations to the dispatcher
75  // difference from mgmt::ControlCommand: accepts nfd::ControlParameters
76  using ControlCommandHandler = std::function<void(const ControlCommand& command,
77  const Name& prefix, const Interest& interest,
78  const ControlParameters& parameters,
79  const ndn::mgmt::CommandContinuation done)>;
80 
81  template<typename Command>
82  void
83  registerCommandHandler(const std::string& verb,
84  const ControlCommandHandler& handler);
85 
86  void
87  registerStatusDatasetHandler(const std::string& verb,
88  const ndn::mgmt::StatusDatasetHandler& handler);
89 
90  ndn::mgmt::PostNotification
91  registerNotificationStream(const std::string& verb);
92 
100  static std::string
101  extractSigner(const Interest& interest);
102 
107  virtual ndn::mgmt::Authorization
108  makeAuthorization(const std::string& verb);
109 
116  [[nodiscard]] static bool
117  validateParameters(const ControlCommand& command,
118  const ndn::mgmt::ControlParameters& parameters);
119 
123  static void
124  handleCommand(shared_ptr<ControlCommand> command,
125  const ControlCommandHandler& handler,
126  const Name& prefix, const Interest& interest,
127  const ndn::mgmt::ControlParameters& params,
128  const ndn::mgmt::CommandContinuation& done);
129 
136  PartialName
137  makeRelPrefix(const std::string& verb)
138  {
139  return PartialName(m_module).append(verb);
140  }
141 
142 private:
143  std::string m_module;
144  Dispatcher& m_dispatcher;
145  CommandAuthenticator* m_authenticator = nullptr;
146 };
147 
148 template<typename Command>
149 void
150 ManagerBase::registerCommandHandler(const std::string& verb,
151  const ControlCommandHandler& handler)
152 {
153  auto command = make_shared<Command>();
154 
155  m_dispatcher.addControlCommand<ControlParameters>(
156  makeRelPrefix(verb),
157  makeAuthorization(verb),
158  [=] (const auto& params) { return validateParameters(*command, params); },
159  [=] (auto&&... args) { handleCommand(command, handler, std::forward<decltype(args)>(args)...); });
160 }
161 
162 } // namespace nfd
163 
164 #endif // NFD_DAEMON_MGMT_MANAGER_BASE_HPP
Provides ControlCommand authorization according to NFD's configuration file.
A collection of common functions shared by all NFD managers, such as communicating with the dispatche...
const std::string & getModule() const
ndn::mgmt::PostNotification registerNotificationStream(const std::string &verb)
std::function< void(const ControlCommand &command, const Name &prefix, const Interest &interest, const ControlParameters &parameters, const ndn::mgmt::CommandContinuation done)> ControlCommandHandler
void registerStatusDatasetHandler(const std::string &verb, const ndn::mgmt::StatusDatasetHandler &handler)
void registerCommandHandler(const std::string &verb, const ControlCommandHandler &handler)
ManagerBase(const std::string &module, Dispatcher &dispatcher)
static std::string extractSigner(const Interest &interest)
Extracts the name from the KeyLocator of a ControlCommand request.
virtual ~ManagerBase()
#define NFD_PUBLIC_WITH_TESTS_ELSE_PROTECTED
Definition: common.hpp:40
#define NFD_PUBLIC_WITH_TESTS_ELSE_PRIVATE
Definition: common.hpp:41
Definition: common.hpp:77