NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.0: NDN, CCN, CCNx, content centric networks
API Documentation
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
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;
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
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
virtual void wireDecode(const Block &wire) final
#define NFD_LOG_DEBUG(expression)
Definition: logger.hpp:36
indicates a producer generated NACK
Definition: tlv.hpp:135
represents parameters in a ControlCommand request or response
ndn::KeyChain & m_keyChain
void sendResponse(const Name &name, const ControlResponse &response)
base class of NFD ControlCommand
virtual void validateRequest(const ControlParameters &parameters) const
validate request parameters
Class representing a wire element of NDN-TLV packet format.
Definition: block.hpp:43
static bool extractParameters(const Name::Component &parameterComponent, ControlParameters &extractedParameters)
ndn::mgmt::ControlResponse ControlResponse
MetaInfo & setType(uint32_t type)
set ContentType
Definition: meta-info.cpp:47
Block blockFromValue() const
Definition: block.cpp:437
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-common.hpp:38
An MetaInfo holds the meta info which is signed inside the data packet.
Definition: meta-info.hpp:56
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()
Name abstraction to represent an absolute name.
Definition: name.hpp:46
void sendNack(const Name &name)
Component holds a read-only name component value.
#define NFD_LOG_INIT(name)
Definition: logger.hpp:33
virtual bool validateParameters(const ControlCommand &command, ControlParameters &parameters)
virtual void applyDefaultsToRequest(ControlParameters &parameters) const
apply default values to missing fields in request
represents an error in ControlParameters
represents an error in TLV encoding or decoding
Definition: tlv.hpp:50