blob: 5296e28f77d4b3241c99d60cc242992215ce5b51 [file] [log] [blame]
Steve DiBenedetto042bfe92014-01-30 15:05:08 -07001/* -*- 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 DiBenedettobf6a93d2014-03-21 14:03:02 -06008#include "core/logger.hpp"
Steve DiBenedetto042bfe92014-01-30 15:05:08 -07009
Steve DiBenedetto042bfe92014-01-30 15:05:08 -070010namespace nfd {
11
Steve DiBenedetto3970c892014-01-31 23:31:13 -070012NFD_LOG_INIT("ManagerBase");
13
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -070014ManagerBase::ManagerBase(shared_ptr<InternalFace> face, const std::string& privilege)
Steve DiBenedetto042bfe92014-01-30 15:05:08 -070015 : m_face(face)
16{
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -070017 face->getValidator().addSupportedPrivilege(privilege);
Steve DiBenedetto042bfe92014-01-30 15:05:08 -070018}
19
20ManagerBase::~ManagerBase()
21{
22
23}
24
Steve DiBenedetto7564d972014-03-24 14:28:46 -060025bool
26ManagerBase::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 DiBenedetto042bfe92014-01-30 15:05:08 -070043void
44ManagerBase::sendResponse(const Name& name,
Steve DiBenedetto0b73f442014-02-05 22:02:03 -070045 uint32_t code,
46 const std::string& text)
Steve DiBenedetto042bfe92014-01-30 15:05:08 -070047{
Alexander Afanasyevd482fd32014-02-09 23:40:20 -080048 ControlResponse response(code, text);
Steve DiBenedetto0b73f442014-02-05 22:02:03 -070049 sendResponse(name, response);
50}
Steve DiBenedetto042bfe92014-01-30 15:05:08 -070051
Steve DiBenedetto0b73f442014-02-05 22:02:03 -070052void
53ManagerBase::sendResponse(const Name& name,
Steve DiBenedetto7564d972014-03-24 14:28:46 -060054 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
63void
64ManagerBase::sendResponse(const Name& name,
Alexander Afanasyevd482fd32014-02-09 23:40:20 -080065 const ControlResponse& response)
Steve DiBenedetto0b73f442014-02-05 22:02:03 -070066{
67 NFD_LOG_DEBUG("responding"
68 << " name: " << name
69 << " code: " << response.getCode()
70 << " text: " << response.getText());
Steve DiBenedetto3970c892014-01-31 23:31:13 -070071
Steve DiBenedetto0b73f442014-02-05 22:02:03 -070072 const Block& encodedControl = response.wireEncode();
Steve DiBenedetto3970c892014-01-31 23:31:13 -070073
Steve DiBenedetto86fce8e2014-03-06 12:16:20 -070074 shared_ptr<Data> responseData(make_shared<Data>(name));
75 responseData->setContent(encodedControl);
Steve DiBenedetto0b73f442014-02-05 22:02:03 -070076
Steve DiBenedetto86fce8e2014-03-06 12:16:20 -070077 m_face->sign(*responseData);
78 m_face->put(*responseData);
Steve DiBenedetto042bfe92014-01-30 15:05:08 -070079}
80
Steve DiBenedetto51d242a2014-03-31 13:46:43 -060081bool
82ManagerBase::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 DiBenedetto2c2b8892014-02-27 11:46:48 -070099void
100ManagerBase::onCommandValidationFailed(const shared_ptr<const Interest>& command,
101 const std::string& error)
102{
Alexander Afanasyevbf9edee2014-03-31 23:05:27 -0700103 NFD_LOG_DEBUG("command result: unauthorized command: " << *command);
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700104 sendResponse(command->getName(), 403, "Unauthorized command");
105}
106
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700107
108} // namespace nfd