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