manager-base.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
26 #include "manager-base.hpp"
27 
28 namespace nfd {
29 
30 using ndn::mgmt::ValidateParameters;
31 using ndn::mgmt::Authorization;
32 
33 ManagerBase::ManagerBase(Dispatcher& dispatcher, const std::string& module)
34  : m_dispatcher(dispatcher)
35  , m_module(module)
36 {
37 }
38 
39 ManagerBase::~ManagerBase() = default;
40 
41 void
43  const ndn::mgmt::StatusDatasetHandler& handler)
44 {
45  m_dispatcher.addStatusDataset(makeRelPrefix(verb),
46  ndn::mgmt::makeAcceptAllAuthorization(),
47  handler);
48 }
49 
50 ndn::mgmt::PostNotification
52 {
53  return m_dispatcher.addNotificationStream(makeRelPrefix(verb));
54 }
55 
56 void
57 ManagerBase::extractRequester(const Interest& interest,
58  ndn::mgmt::AcceptContinuation accept)
59 {
60  const Name& interestName = interest.getName();
61 
62  try {
63  ndn::SignatureInfo sigInfo(interestName.at(ndn::signed_interest::POS_SIG_INFO).blockFromValue());
64  if (!sigInfo.hasKeyLocator() ||
65  sigInfo.getKeyLocator().getType() != ndn::KeyLocator::KeyLocator_Name) {
66  return accept("");
67  }
68 
69  accept(sigInfo.getKeyLocator().getName().toUri());
70  }
71  catch (const tlv::Error&) {
72  accept("");
73  }
74 }
75 
76 bool
77 ManagerBase::validateParameters(const nfd::ControlCommand& command, const ndn::mgmt::ControlParameters& parameters)
78 {
79  BOOST_ASSERT(dynamic_cast<const ControlParameters*>(&parameters) != nullptr);
80 
81  try {
82  command.validateRequest(static_cast<const ControlParameters&>(parameters));
83  }
84  catch (const ControlCommand::ArgumentError&) {
85  return false;
86  }
87  return true;
88 }
89 
90 void
91 ManagerBase::handleCommand(shared_ptr<nfd::ControlCommand> command,
92  const ControlCommandHandler& handler,
93  const Name& prefix, const Interest& interest,
94  const ndn::mgmt::ControlParameters& params,
95  ndn::mgmt::CommandContinuation done)
96 {
97  BOOST_ASSERT(dynamic_cast<const ControlParameters*>(&params) != nullptr);
98  ControlParameters parameters = static_cast<const ControlParameters&>(params);
99  command->applyDefaultsToRequest(parameters);
100  handler(*command, prefix, interest, parameters, done);
101 }
102 
103 } // namespace nfd
void registerStatusDatasetHandler(const std::string &verb, const ndn::mgmt::StatusDatasetHandler &handler)
ManagerBase(Dispatcher &dispatcher, const std::string &module)
function< void(const ControlCommand &command, const Name &prefix, const Interest &interest, const ControlParameters &parameters, const ndn::mgmt::CommandContinuation done)> ControlCommandHandler
Copyright (c) 2014-2015, Regents of the University of California, Arizona Board of Regents...
Definition: algorithm.hpp:32
ndn::mgmt::PostNotification registerNotificationStream(const std::string &verb)
virtual ~ManagerBase()
void extractRequester(const Interest &interest, ndn::mgmt::AcceptContinuation accept)
extract a requester from a ControlCommand request