Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 3 | * Copyright (c) 2014 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 | * |
| 10 | * This file is part of NFD (Named Data Networking Forwarding Daemon). |
| 11 | * See AUTHORS.md for complete list of NFD authors and contributors. |
| 12 | * |
| 13 | * NFD is free software: you can redistribute it and/or modify it under the terms |
| 14 | * of the GNU General Public License as published by the Free Software Foundation, |
| 15 | * either version 3 of the License, or (at your option) any later version. |
| 16 | * |
| 17 | * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 18 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 19 | * PURPOSE. See the GNU General Public License for more details. |
| 20 | * |
| 21 | * You should have received a copy of the GNU General Public License along with |
| 22 | * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 23 | **/ |
Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 24 | |
| 25 | #include "manager-base.hpp" |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 26 | #include "core/logger.hpp" |
Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 27 | |
Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 28 | namespace nfd { |
| 29 | |
Steve DiBenedetto | 3970c89 | 2014-01-31 23:31:13 -0700 | [diff] [blame] | 30 | NFD_LOG_INIT("ManagerBase"); |
| 31 | |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 32 | ManagerBase::ManagerBase(shared_ptr<InternalFace> face, const std::string& privilege) |
Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 33 | : m_face(face) |
| 34 | { |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 35 | face->getValidator().addSupportedPrivilege(privilege); |
Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | ManagerBase::~ManagerBase() |
| 39 | { |
| 40 | |
| 41 | } |
| 42 | |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 43 | bool |
| 44 | ManagerBase::extractParameters(const Name::Component& parameterComponent, |
| 45 | ControlParameters& extractedParameters) |
| 46 | { |
| 47 | try |
| 48 | { |
| 49 | Block rawParameters = parameterComponent.blockFromValue(); |
| 50 | extractedParameters.wireDecode(rawParameters); |
| 51 | } |
| 52 | catch (const ndn::Tlv::Error& e) |
| 53 | { |
| 54 | return false; |
| 55 | } |
| 56 | |
| 57 | NFD_LOG_DEBUG("Parameters parsed OK"); |
| 58 | return true; |
| 59 | } |
| 60 | |
Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 61 | void |
| 62 | ManagerBase::sendResponse(const Name& name, |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 63 | uint32_t code, |
| 64 | const std::string& text) |
Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 65 | { |
Alexander Afanasyev | d482fd3 | 2014-02-09 23:40:20 -0800 | [diff] [blame] | 66 | ControlResponse response(code, text); |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 67 | sendResponse(name, response); |
| 68 | } |
Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 69 | |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 70 | void |
| 71 | ManagerBase::sendResponse(const Name& name, |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 72 | uint32_t code, |
| 73 | const std::string& text, |
| 74 | const Block& body) |
| 75 | { |
| 76 | ControlResponse response(code, text); |
| 77 | response.setBody(body); |
| 78 | sendResponse(name, response); |
| 79 | } |
| 80 | |
| 81 | void |
| 82 | ManagerBase::sendResponse(const Name& name, |
Alexander Afanasyev | d482fd3 | 2014-02-09 23:40:20 -0800 | [diff] [blame] | 83 | const ControlResponse& response) |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 84 | { |
| 85 | NFD_LOG_DEBUG("responding" |
| 86 | << " name: " << name |
| 87 | << " code: " << response.getCode() |
| 88 | << " text: " << response.getText()); |
Steve DiBenedetto | 3970c89 | 2014-01-31 23:31:13 -0700 | [diff] [blame] | 89 | |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 90 | const Block& encodedControl = response.wireEncode(); |
Steve DiBenedetto | 3970c89 | 2014-01-31 23:31:13 -0700 | [diff] [blame] | 91 | |
Steve DiBenedetto | 86fce8e | 2014-03-06 12:16:20 -0700 | [diff] [blame] | 92 | shared_ptr<Data> responseData(make_shared<Data>(name)); |
| 93 | responseData->setContent(encodedControl); |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 94 | |
Steve DiBenedetto | 86fce8e | 2014-03-06 12:16:20 -0700 | [diff] [blame] | 95 | m_face->sign(*responseData); |
| 96 | m_face->put(*responseData); |
Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 97 | } |
| 98 | |
Steve DiBenedetto | 51d242a | 2014-03-31 13:46:43 -0600 | [diff] [blame] | 99 | bool |
| 100 | ManagerBase::validateParameters(const ControlCommand& command, |
| 101 | ControlParameters& parameters) |
| 102 | { |
| 103 | try |
| 104 | { |
| 105 | command.validateRequest(parameters); |
| 106 | } |
| 107 | catch (const ControlCommand::ArgumentError& error) |
| 108 | { |
| 109 | return false; |
| 110 | } |
| 111 | |
| 112 | command.applyDefaultsToRequest(parameters); |
| 113 | |
| 114 | return true; |
| 115 | } |
| 116 | |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 117 | void |
| 118 | ManagerBase::onCommandValidationFailed(const shared_ptr<const Interest>& command, |
| 119 | const std::string& error) |
| 120 | { |
Alexander Afanasyev | f4e89b4 | 2014-05-31 15:54:18 +0300 | [diff] [blame^] | 121 | NFD_LOG_DEBUG("command result: unauthorized command: " << *command << " (" << error << ")"); |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 122 | sendResponse(command->getName(), 403, "Unauthorized command"); |
| 123 | } |
| 124 | |
Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 125 | |
| 126 | } // namespace nfd |