Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Yanbiao Li | df846e5 | 2016-01-30 21:53:47 -0800 | [diff] [blame] | 3 | * Copyright (c) 2014-2016, Regents of the University of California, |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 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 "manager-base.hpp" |
| 27 | |
| 28 | namespace nfd { |
| 29 | |
| 30 | using ndn::mgmt::ValidateParameters; |
| 31 | using ndn::mgmt::Authorization; |
| 32 | |
| 33 | ManagerBase::ManagerBase(Dispatcher& dispatcher, |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 34 | const std::string& module) |
| 35 | : m_dispatcher(dispatcher) |
Junxiao Shi | 9ddf1b5 | 2016-08-22 03:58:55 +0000 | [diff] [blame^] | 36 | , m_module(module) |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 37 | { |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | void |
| 41 | ManagerBase::registerStatusDatasetHandler(const std::string& verb, |
| 42 | const ndn::mgmt::StatusDatasetHandler& handler) |
| 43 | { |
| 44 | m_dispatcher.addStatusDataset(makeRelPrefix(verb), |
| 45 | ndn::mgmt::makeAcceptAllAuthorization(), |
| 46 | handler); |
| 47 | } |
| 48 | |
| 49 | ndn::mgmt::PostNotification |
| 50 | ManagerBase::registerNotificationStream(const std::string& verb) |
| 51 | { |
| 52 | return m_dispatcher.addNotificationStream(makeRelPrefix(verb)); |
| 53 | } |
| 54 | |
| 55 | void |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 56 | ManagerBase::extractRequester(const Interest& interest, |
| 57 | ndn::mgmt::AcceptContinuation accept) |
| 58 | { |
| 59 | const Name& interestName = interest.getName(); |
| 60 | |
| 61 | try { |
| 62 | ndn::SignatureInfo sigInfo(interestName.at(ndn::signed_interest::POS_SIG_INFO).blockFromValue()); |
| 63 | if (!sigInfo.hasKeyLocator() || |
| 64 | sigInfo.getKeyLocator().getType() != ndn::KeyLocator::KeyLocator_Name) { |
| 65 | return accept(""); |
| 66 | } |
| 67 | |
| 68 | accept(sigInfo.getKeyLocator().getName().toUri()); |
| 69 | } |
| 70 | catch (const tlv::Error&) { |
| 71 | accept(""); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | bool |
| 76 | ManagerBase::validateParameters(const nfd::ControlCommand& command, const ndn::mgmt::ControlParameters& parameters) |
| 77 | { |
| 78 | BOOST_ASSERT(dynamic_cast<const ControlParameters*>(¶meters) != nullptr); |
| 79 | |
| 80 | try { |
| 81 | command.validateRequest(static_cast<const ControlParameters&>(parameters)); |
| 82 | } |
| 83 | catch (const ControlCommand::ArgumentError&) { |
| 84 | return false; |
| 85 | } |
| 86 | return true; |
| 87 | } |
| 88 | |
| 89 | void |
| 90 | ManagerBase::handleCommand(shared_ptr<nfd::ControlCommand> command, |
| 91 | const ControlCommandHandler& handler, |
| 92 | const Name& prefix, const Interest& interest, |
| 93 | const ndn::mgmt::ControlParameters& params, |
| 94 | ndn::mgmt::CommandContinuation done) |
| 95 | { |
| 96 | BOOST_ASSERT(dynamic_cast<const ControlParameters*>(¶ms) != nullptr); |
| 97 | ControlParameters parameters = static_cast<const ControlParameters&>(params); |
| 98 | command->applyDefaultsToRequest(parameters); |
| 99 | handler(*command, prefix, interest, parameters, done); |
| 100 | } |
| 101 | |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 102 | } // namespace nfd |