NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.0: NDN, CCN, CCNx, content centric networks
API Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
manager-base.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
26 #include "manager-base.hpp"
27 #include "core/logger.hpp"
28 
29 namespace nfd {
30 
31 NFD_LOG_INIT("ManagerBase");
32 
33 ManagerBase::ManagerBase(shared_ptr<InternalFace> face, const std::string& privilege,
34  ndn::KeyChain& keyChain)
35  : m_face(face)
36  , m_keyChain(keyChain)
37 {
38  face->getValidator().addSupportedPrivilege(privilege);
39 }
40 
42 {
43 
44 }
45 
46 bool
47 ManagerBase::extractParameters(const Name::Component& parameterComponent,
48  ControlParameters& extractedParameters)
49 {
50  try
51  {
52  Block rawParameters = parameterComponent.blockFromValue();
53  extractedParameters.wireDecode(rawParameters);
54  }
55  catch (const tlv::Error&)
56  {
57  return false;
58  }
59 
60  NFD_LOG_DEBUG("Parameters parsed OK");
61  return true;
62 }
63 
64 void
66  uint32_t code,
67  const std::string& text)
68 {
69  ControlResponse response(code, text);
70  sendResponse(name, response);
71 }
72 
73 void
75  uint32_t code,
76  const std::string& text,
77  const Block& body)
78 {
79  ControlResponse response(code, text);
80  response.setBody(body);
81  sendResponse(name, response);
82 }
83 
84 void
86  const ControlResponse& response)
87 {
88  NFD_LOG_DEBUG("responding"
89  << " name: " << name
90  << " code: " << response.getCode()
91  << " text: " << response.getText());
92 
93  const Block& encodedControl = response.wireEncode();
94 
95  shared_ptr<Data> responseData(make_shared<Data>(name));
96  responseData->setContent(encodedControl);
97 
98  m_keyChain.sign(*responseData);
99  m_face->put(*responseData);
100 }
101 
102 void
104 {
105  NFD_LOG_DEBUG("responding NACK to " << name);
106 
107  ndn::MetaInfo meta;
108  meta.setType(tlv::ContentType_Nack);
109 
110  shared_ptr<Data> responseData(make_shared<Data>(name));
111  responseData->setMetaInfo(meta);
112 
113  m_keyChain.sign(*responseData);
114  m_face->put(*responseData);
115 }
116 
117 bool
118 ManagerBase::validateParameters(const ControlCommand& command,
119  ControlParameters& parameters)
120 {
121  try
122  {
123  command.validateRequest(parameters);
124  }
125  catch (const ControlCommand::ArgumentError& error)
126  {
127  return false;
128  }
129 
130  command.applyDefaultsToRequest(parameters);
131 
132  return true;
133 }
134 
135 void
136 ManagerBase::onCommandValidationFailed(const shared_ptr<const Interest>& command,
137  const std::string& error)
138 {
139  NFD_LOG_DEBUG("command result: unauthorized command: " << *command << " (" << error << ")");
140  sendResponse(command->getName(), 403, "Unauthorized command");
141 }
142 
143 
144 } // namespace nfd
shared_ptr< InternalFace > m_face
#define NFD_LOG_DEBUG(expression)
Definition: logger.hpp:36
ndn::KeyChain & m_keyChain
void sendResponse(const Name &name, const ControlResponse &response)
static bool extractParameters(const Name::Component &parameterComponent, ControlParameters &extractedParameters)
void onCommandValidationFailed(const shared_ptr< const Interest > &command, const std::string &error)
ManagerBase(shared_ptr< InternalFace > face, const std::string &privilege, ndn::KeyChain &keyChain)
virtual ~ManagerBase()
void sendNack(const Name &name)
#define NFD_LOG_INIT(name)
Definition: logger.hpp:33
virtual bool validateParameters(const ControlCommand &command, ControlParameters &parameters)