NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.3: NDN, CCN, CCNx, content centric networks
API Documentation
controller.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
22 #include "controller.hpp"
23 #include "../../face.hpp"
24 #include "../../security/key-chain.hpp"
25 #include "../../util/segment-fetcher.hpp"
26 
27 namespace ndn {
28 namespace nfd {
29 
31 
32 const uint32_t Controller::ERROR_TIMEOUT = 10060; // WinSock ESAETIMEDOUT
33 const uint32_t Controller::ERROR_NACK = 10800; // 10000 + TLV-TYPE of Nack header
34 const uint32_t Controller::ERROR_VALIDATION = 10021; // 10000 + TLS1_ALERT_DECRYPTION_FAILED
35 const uint32_t Controller::ERROR_SERVER = 500;
36 const uint32_t Controller::ERROR_LBOUND = 400;
37 ValidatorNull Controller::s_validatorNull;
38 
40  : m_face(face)
41  , m_keyChain(keyChain)
42  , m_validator(validator)
43 {
44 }
45 
46 void
47 Controller::startCommand(const shared_ptr<ControlCommand>& command,
48  const ControlParameters& parameters,
49  const CommandSucceedCallback& onSuccess1,
50  const CommandFailCallback& onFailure1,
51  const CommandOptions& options)
52 {
53  const CommandSucceedCallback& onSuccess = onSuccess1 ?
54  onSuccess1 : [] (const ControlParameters&) {};
55  const CommandFailCallback& onFailure = onFailure1 ?
56  onFailure1 : [] (const ControlResponse&) {};
57 
58  Name requestName = command->getRequestName(options.getPrefix(), parameters);
59  Interest interest(requestName);
60  interest.setInterestLifetime(options.getTimeout());
61  m_keyChain.sign(interest, options.getSigningInfo());
62 
63  m_face.expressInterest(interest,
64  [=] (const Interest&, const Data& data) {
65  this->processCommandResponse(data, command, onSuccess, onFailure);
66  },
67  [=] (const Interest&, const lp::Nack&) {
68  onFailure(ControlResponse(Controller::ERROR_NACK, "network Nack received"));
69  },
70  [=] (const Interest&) {
71  onFailure(ControlResponse(Controller::ERROR_TIMEOUT, "request timed out"));
72  });
73 }
74 
75 void
76 Controller::processCommandResponse(const Data& data,
77  const shared_ptr<ControlCommand>& command,
78  const CommandSucceedCallback& onSuccess,
79  const CommandFailCallback& onFailure)
80 {
81  m_validator.validate(data,
82  [=] (const shared_ptr<const Data>& data) {
83  this->processValidatedCommandResponse(*data, command, onSuccess, onFailure);
84  },
85  [=] (const shared_ptr<const Data>&, const std::string& msg) {
86  onFailure(ControlResponse(ERROR_VALIDATION, msg));
87  }
88  );
89 }
90 
91 void
92 Controller::processValidatedCommandResponse(const Data& data,
93  const shared_ptr<ControlCommand>& command,
94  const CommandSucceedCallback& onSuccess,
95  const CommandFailCallback& onFailure)
96 {
97  ControlResponse response;
98  try {
99  response.wireDecode(data.getContent().blockFromValue());
100  }
101  catch (const tlv::Error& e) {
102  onFailure(ControlResponse(ERROR_SERVER, e.what()));
103  return;
104  }
105 
106  uint32_t code = response.getCode();
107  if (code >= ERROR_LBOUND) {
108  onFailure(response);
109  return;
110  }
111 
112  ControlParameters parameters;
113  try {
114  parameters.wireDecode(response.getBody());
115  }
116  catch (const tlv::Error& e) {
117  onFailure(ControlResponse(ERROR_SERVER, e.what()));
118  return;
119  }
120 
121  try {
122  command->validateResponse(parameters);
123  }
124  catch (const ControlCommand::ArgumentError& e) {
125  onFailure(ControlResponse(ERROR_SERVER, e.what()));
126  return;
127  }
128 
129  onSuccess(parameters);
130 }
131 
132 void
133 Controller::fetchDataset(const Name& prefix,
134  const std::function<void(const ConstBufferPtr&)>& processResponse,
135  const DatasetFailCallback& onFailure,
136  const CommandOptions& options)
137 {
138  Interest baseInterest(prefix);
139  baseInterest.setInterestLifetime(options.getTimeout());
140 
141  SegmentFetcher::fetch(m_face, baseInterest, m_validator, processResponse,
142  bind(&Controller::processDatasetFetchError, this, onFailure, _1, _2));
143 }
144 
145 void
146 Controller::processDatasetFetchError(const DatasetFailCallback& onFailure,
147  uint32_t code, std::string msg)
148 {
149  switch (static_cast<SegmentFetcher::ErrorCode>(code)) {
150  // It's intentional to cast as SegmentFetcher::ErrorCode, and to not have a 'default' clause.
151  // This forces the switch statement to handle every defined SegmentFetcher::ErrorCode,
152  // and breaks compilation if it does not.
153  case SegmentFetcher::ErrorCode::INTEREST_TIMEOUT:
154  onFailure(ERROR_TIMEOUT, msg);
155  break;
156  case SegmentFetcher::ErrorCode::DATA_HAS_NO_SEGMENT:
157  onFailure(ERROR_SERVER, msg);
158  break;
159  case SegmentFetcher::ErrorCode::SEGMENT_VALIDATION_FAIL:
162  onFailure(ERROR_VALIDATION, msg);
163  break;
164  case SegmentFetcher::ErrorCode::NACK_ERROR:
165  onFailure(ERROR_NACK, msg);
166  break;
167  }
168 }
169 
170 } // namespace nfd
171 } // namespace ndn
virtual void wireDecode(const Block &wire) final
void validate(const Data &data, const OnDataValidated &onValidated, const OnDataValidationFailed &onValidationFailed)
Validate Data and call either onValidated or onValidationFailed.
Definition: validator.hpp:81
Copyright (c) 2011-2015 Regents of the University of California.
security::KeyChain & m_keyChain
Definition: controller.hpp:173
represents parameters in a ControlCommand request or response
const Block & getContent() const
Get content Block.
Definition: data.cpp:230
const Block & getBody() const
const security::SigningInfo & getSigningInfo() const
The packet signing interface.
Definition: key-chain.hpp:47
Utility class to fetch latest version of the segmented data.
const time::milliseconds & getTimeout() const
represents an Interest packet
Definition: interest.hpp:42
function< void(const ControlResponse &)> CommandFailCallback
a callback on command failure
Definition: controller.hpp:60
void sign(Data &data, const SigningInfo &params=DEFAULT_SIGNING_INFO)
Sign data according to the supplied signing information.
Definition: key-chain.cpp:517
static const uint32_t ERROR_TIMEOUT
error code for timeout
Definition: controller.hpp:153
represents a Network Nack
Definition: nack.hpp:40
function< void(const ControlParameters &)> CommandSucceedCallback
a callback on command success
Definition: controller.hpp:56
static const uint32_t ERROR_LBOUND
inclusive lower bound of error codes
Definition: controller.hpp:169
ndn::mgmt::ControlResponse ControlResponse
contains options for ControlCommand execution
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-common.hpp:40
provides the interfaces for packet validation.
Definition: validator.hpp:42
Provide a communication channel with local or remote NDN forwarder.
Definition: face.hpp:125
static const uint32_t ERROR_VALIDATION
error code for response validation failure
Definition: controller.hpp:161
Name abstraction to represent an absolute name.
Definition: name.hpp:46
static void fetch(Face &face, const Interest &baseInterest, Validator &validator, const CompleteCallback &completeCallback, const ErrorCallback &errorCallback)
Initiate segment fetching.
Controller(Face &face, security::KeyChain &keyChain, security::Validator &validator=s_validatorNull)
construct a Controller that uses face for transport, and uses the passed KeyChain to sign commands ...
Definition: controller.cpp:39
const Name & getPrefix() const
void wireDecode(const Block &block)
Block blockFromValue() const
Definition: block.cpp:437
security::Validator & m_validator
Definition: controller.hpp:174
ControlCommand response.
const PendingInterestId * expressInterest(const Interest &interest, const DataCallback &afterSatisfied, const NackCallback &afterNacked, const TimeoutCallback &afterTimeout)
Express Interest.
Definition: face.cpp:132
represents an error in ControlParameters
shared_ptr< const Buffer > ConstBufferPtr
Definition: buffer.hpp:33
represents a Data packet
Definition: data.hpp:37
Interest & setInterestLifetime(const time::milliseconds &interestLifetime)
Definition: interest.hpp:235
represents an error in TLV encoding or decoding
Definition: tlv.hpp:50
static const uint32_t ERROR_SERVER
error code for server error
Definition: controller.hpp:165
static const uint32_t ERROR_NACK
error code for network Nack
Definition: controller.hpp:157
function< void(uint32_t code, const std::string &reason)> DatasetFailCallback
a callback on dataset retrieval failure
Definition: controller.hpp:64