Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (C) 2014 Named Data Networking Project |
| 4 | * See COPYING for copyright and distribution information. |
| 5 | */ |
| 6 | |
| 7 | #include "manager-base.hpp" |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 8 | #include "core/logger.hpp" |
Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 9 | |
Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 10 | namespace nfd { |
| 11 | |
Steve DiBenedetto | 3970c89 | 2014-01-31 23:31:13 -0700 | [diff] [blame] | 12 | NFD_LOG_INIT("ManagerBase"); |
| 13 | |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 14 | ManagerBase::ManagerBase(shared_ptr<InternalFace> face, const std::string& privilege) |
Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 15 | : m_face(face) |
| 16 | { |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 17 | face->getValidator().addSupportedPrivilege(privilege); |
Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 18 | } |
| 19 | |
| 20 | ManagerBase::~ManagerBase() |
| 21 | { |
| 22 | |
| 23 | } |
| 24 | |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 25 | bool |
| 26 | ManagerBase::extractParameters(const Name::Component& parameterComponent, |
| 27 | ControlParameters& extractedParameters) |
| 28 | { |
| 29 | try |
| 30 | { |
| 31 | Block rawParameters = parameterComponent.blockFromValue(); |
| 32 | extractedParameters.wireDecode(rawParameters); |
| 33 | } |
| 34 | catch (const ndn::Tlv::Error& e) |
| 35 | { |
| 36 | return false; |
| 37 | } |
| 38 | |
| 39 | NFD_LOG_DEBUG("Parameters parsed OK"); |
| 40 | return true; |
| 41 | } |
| 42 | |
Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 43 | void |
| 44 | ManagerBase::sendResponse(const Name& name, |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 45 | uint32_t code, |
| 46 | const std::string& text) |
Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 47 | { |
Alexander Afanasyev | d482fd3 | 2014-02-09 23:40:20 -0800 | [diff] [blame] | 48 | ControlResponse response(code, text); |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 49 | sendResponse(name, response); |
| 50 | } |
Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 51 | |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 52 | void |
| 53 | ManagerBase::sendResponse(const Name& name, |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 54 | uint32_t code, |
| 55 | const std::string& text, |
| 56 | const Block& body) |
| 57 | { |
| 58 | ControlResponse response(code, text); |
| 59 | response.setBody(body); |
| 60 | sendResponse(name, response); |
| 61 | } |
| 62 | |
| 63 | void |
| 64 | ManagerBase::sendResponse(const Name& name, |
Alexander Afanasyev | d482fd3 | 2014-02-09 23:40:20 -0800 | [diff] [blame] | 65 | const ControlResponse& response) |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 66 | { |
| 67 | NFD_LOG_DEBUG("responding" |
| 68 | << " name: " << name |
| 69 | << " code: " << response.getCode() |
| 70 | << " text: " << response.getText()); |
Steve DiBenedetto | 3970c89 | 2014-01-31 23:31:13 -0700 | [diff] [blame] | 71 | |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 72 | const Block& encodedControl = response.wireEncode(); |
Steve DiBenedetto | 3970c89 | 2014-01-31 23:31:13 -0700 | [diff] [blame] | 73 | |
Steve DiBenedetto | 86fce8e | 2014-03-06 12:16:20 -0700 | [diff] [blame] | 74 | shared_ptr<Data> responseData(make_shared<Data>(name)); |
| 75 | responseData->setContent(encodedControl); |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 76 | |
Steve DiBenedetto | 86fce8e | 2014-03-06 12:16:20 -0700 | [diff] [blame] | 77 | m_face->sign(*responseData); |
| 78 | m_face->put(*responseData); |
Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 79 | } |
| 80 | |
Steve DiBenedetto | 51d242a | 2014-03-31 13:46:43 -0600 | [diff] [blame] | 81 | bool |
| 82 | ManagerBase::validateParameters(const ControlCommand& command, |
| 83 | ControlParameters& parameters) |
| 84 | { |
| 85 | try |
| 86 | { |
| 87 | command.validateRequest(parameters); |
| 88 | } |
| 89 | catch (const ControlCommand::ArgumentError& error) |
| 90 | { |
| 91 | return false; |
| 92 | } |
| 93 | |
| 94 | command.applyDefaultsToRequest(parameters); |
| 95 | |
| 96 | return true; |
| 97 | } |
| 98 | |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 99 | void |
| 100 | ManagerBase::onCommandValidationFailed(const shared_ptr<const Interest>& command, |
| 101 | const std::string& error) |
| 102 | { |
Alexander Afanasyev | bf9edee | 2014-03-31 23:05:27 -0700 | [diff] [blame] | 103 | NFD_LOG_DEBUG("command result: unauthorized command: " << *command); |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 104 | sendResponse(command->getName(), 403, "Unauthorized command"); |
| 105 | } |
| 106 | |
Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 107 | |
| 108 | } // namespace nfd |