blob: 08d845c0eec87e90f6f19f28551d8276fe6fccee [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 DiBenedetto042bfe92014-01-30 15:05:08 -07008
Steve DiBenedetto042bfe92014-01-30 15:05:08 -07009namespace nfd {
10
Steve DiBenedetto3970c892014-01-31 23:31:13 -070011NFD_LOG_INIT("ManagerBase");
12
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -070013ManagerBase::ManagerBase(shared_ptr<InternalFace> face, const std::string& privilege)
Steve DiBenedetto042bfe92014-01-30 15:05:08 -070014 : m_face(face)
15{
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -070016 face->getValidator().addSupportedPrivilege(privilege);
Steve DiBenedetto042bfe92014-01-30 15:05:08 -070017}
18
19ManagerBase::~ManagerBase()
20{
21
22}
23
Steve DiBenedetto7564d972014-03-24 14:28:46 -060024bool
25ManagerBase::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 DiBenedetto042bfe92014-01-30 15:05:08 -070042void
43ManagerBase::sendResponse(const Name& name,
Steve DiBenedetto0b73f442014-02-05 22:02:03 -070044 uint32_t code,
45 const std::string& text)
Steve DiBenedetto042bfe92014-01-30 15:05:08 -070046{
Alexander Afanasyevd482fd32014-02-09 23:40:20 -080047 ControlResponse response(code, text);
Steve DiBenedetto0b73f442014-02-05 22:02:03 -070048 sendResponse(name, response);
49}
Steve DiBenedetto042bfe92014-01-30 15:05:08 -070050
Steve DiBenedetto0b73f442014-02-05 22:02:03 -070051void
52ManagerBase::sendResponse(const Name& name,
Steve DiBenedetto7564d972014-03-24 14:28:46 -060053 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
62void
63ManagerBase::sendResponse(const Name& name,
Alexander Afanasyevd482fd32014-02-09 23:40:20 -080064 const ControlResponse& response)
Steve DiBenedetto0b73f442014-02-05 22:02:03 -070065{
66 NFD_LOG_DEBUG("responding"
67 << " name: " << name
68 << " code: " << response.getCode()
69 << " text: " << response.getText());
Steve DiBenedetto3970c892014-01-31 23:31:13 -070070
Steve DiBenedetto0b73f442014-02-05 22:02:03 -070071 const Block& encodedControl = response.wireEncode();
Steve DiBenedetto3970c892014-01-31 23:31:13 -070072
Steve DiBenedetto86fce8e2014-03-06 12:16:20 -070073 shared_ptr<Data> responseData(make_shared<Data>(name));
74 responseData->setContent(encodedControl);
Steve DiBenedetto0b73f442014-02-05 22:02:03 -070075
Steve DiBenedetto86fce8e2014-03-06 12:16:20 -070076 m_face->sign(*responseData);
77 m_face->put(*responseData);
Steve DiBenedetto042bfe92014-01-30 15:05:08 -070078}
79
Steve DiBenedetto51d242a2014-03-31 13:46:43 -060080bool
81ManagerBase::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 DiBenedetto2c2b8892014-02-27 11:46:48 -070098void
99ManagerBase::onCommandValidationFailed(const shared_ptr<const Interest>& command,
100 const std::string& error)
101{
Steve DiBenedetto9dcc6332014-03-12 16:50:59 -0600102 NFD_LOG_INFO("command result: unauthorized command: " << *command);
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700103 sendResponse(command->getName(), 403, "Unauthorized command");
104}
105
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700106
107} // namespace nfd