NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.0: NDN, CCN, CCNx, content centric networks
API Documentation
nfd-controller.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
22 #include "nfd-controller.hpp"
23 #include "nfd-control-response.hpp"
24 
25 namespace ndn {
26 namespace nfd {
27 
28 const uint32_t Controller::ERROR_TIMEOUT = 10060;
29 const uint32_t Controller::ERROR_SERVER = 500;
30 const uint32_t Controller::ERROR_LBOUND = 400;
31 
32 Controller::Controller(Face& face, KeyChain& keyChain)
33  : m_face(face)
34  , m_keyChain(keyChain)
35 {
36 }
37 
38 void
39 Controller::startCommand(const shared_ptr<ControlCommand>& command,
40  const ControlParameters& parameters,
41  const CommandSucceedCallback& onSuccess,
42  const CommandFailCallback& onFailure,
43  const CommandOptions& options)
44 {
45  Name requestName = command->getRequestName(options.getPrefix(), parameters);
46  Interest interest(requestName);
47  interest.setInterestLifetime(options.getTimeout());
48  m_keyChain.sign(interest, options.getSigningInfo());
49 
50  m_face.expressInterest(interest,
51  bind(&Controller::processCommandResponse, this, _2,
52  command, onSuccess, onFailure),
53  bind(onFailure, ERROR_TIMEOUT, "request timed out"));
54 }
55 
56 void
57 Controller::processCommandResponse(const Data& data,
58  const shared_ptr<ControlCommand>& command,
59  const CommandSucceedCallback& onSuccess,
60  const CommandFailCallback& onFailure)
61 {
63 
64  ControlResponse response;
65  try {
66  response.wireDecode(data.getContent().blockFromValue());
67  }
68  catch (tlv::Error& e) {
69  if (static_cast<bool>(onFailure))
70  onFailure(ERROR_SERVER, e.what());
71  return;
72  }
73 
74  uint32_t code = response.getCode();
75  if (code >= ERROR_LBOUND) {
76  if (static_cast<bool>(onFailure))
77  onFailure(code, response.getText());
78  return;
79  }
80 
81  ControlParameters parameters;
82  try {
83  parameters.wireDecode(response.getBody());
84  }
85  catch (tlv::Error& e) {
86  if (static_cast<bool>(onFailure))
87  onFailure(ERROR_SERVER, e.what());
88  return;
89  }
90 
91  try {
92  command->validateResponse(parameters);
93  }
95  if (static_cast<bool>(onFailure))
96  onFailure(ERROR_SERVER, e.what());
97  return;
98  }
99 
100  if (static_cast<bool>(onSuccess))
101  onSuccess(parameters);
102 }
103 
104 } // namespace nfd
105 } // namespace ndn
virtual void wireDecode(const Block &wire) final
Copyright (c) 2011-2015 Regents of the University of California.
represents parameters in a ControlCommand request or response
const Block & getBody() const
represents an Interest packet
Definition: interest.hpp:45
const Name & getPrefix() const
const std::string & getText() const
static const uint32_t ERROR_TIMEOUT
error code for timeout
function< void(const ControlParameters &)> CommandSucceedCallback
a callback on command success
static const uint32_t ERROR_LBOUND
inclusive lower bound of error codes
Block blockFromValue() const
Definition: block.cpp:437
contains options for ControlCommand execution
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-common.hpp:38
Abstraction to communicate with local or remote NDN forwarder.
Definition: face.hpp:100
Name abstraction to represent an absolute name.
Definition: name.hpp:46
const PendingInterestId * expressInterest(const Interest &interest, const OnData &onData, const OnTimeout &onTimeout=OnTimeout())
Express Interest.
Definition: face.cpp:63
void wireDecode(const Block &block)
const Block & getContent() const
Get content Block.
Definition: data.cpp:230
function< void(uint32_t, const std::string &)> CommandFailCallback
a callback on command failure
Controller(Face &face, KeyChain &keyChain)
construct a Controller that uses face for transport, and uses the passed KeyChain to sign commands ...
ControlCommand response.
represents an error in ControlParameters
represents a Data packet
Definition: data.hpp:39
Interest & setInterestLifetime(const time::milliseconds &interestLifetime)
Definition: interest.hpp:236
const time::milliseconds & getTimeout() const
represents an error in TLV encoding or decoding
Definition: tlv.hpp:50
static const uint32_t ERROR_SERVER
error code for server error
const security::SigningInfo & getSigningInfo() const