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; // WinSock ESAETIMEDOUT
29 const uint32_t Controller::ERROR_NACK = 10800; // 10000 + TLV-TYPE of Nack header
30 const uint32_t Controller::ERROR_SERVER = 500;
31 const uint32_t Controller::ERROR_LBOUND = 400;
32 
34  : m_face(face)
35  , m_keyChain(keyChain)
36 {
37 }
38 
39 void
40 Controller::startCommand(const shared_ptr<ControlCommand>& command,
41  const ControlParameters& parameters,
42  const CommandSucceedCallback& onSuccess,
43  const CommandFailCallback& onFailure,
44  const CommandOptions& options)
45 {
46  Name requestName = command->getRequestName(options.getPrefix(), parameters);
47  Interest interest(requestName);
48  interest.setInterestLifetime(options.getTimeout());
49  m_keyChain.sign(interest, options.getSigningInfo());
50 
51  m_face.expressInterest(interest,
52  bind(&Controller::processCommandResponse, this, _2,
53  command, onSuccess, onFailure),
54  bind(onFailure, ERROR_NACK, "network Nack received"),
55  bind(onFailure, ERROR_TIMEOUT, "request timed out"));
56 }
57 
58 void
59 Controller::processCommandResponse(const Data& data,
60  const shared_ptr<ControlCommand>& command,
61  const CommandSucceedCallback& onSuccess,
62  const CommandFailCallback& onFailure)
63 {
65 
66  ControlResponse response;
67  try {
68  response.wireDecode(data.getContent().blockFromValue());
69  }
70  catch (tlv::Error& e) {
71  if (static_cast<bool>(onFailure))
72  onFailure(ERROR_SERVER, e.what());
73  return;
74  }
75 
76  uint32_t code = response.getCode();
77  if (code >= ERROR_LBOUND) {
78  if (static_cast<bool>(onFailure))
79  onFailure(code, response.getText());
80  return;
81  }
82 
83  ControlParameters parameters;
84  try {
85  parameters.wireDecode(response.getBody());
86  }
87  catch (tlv::Error& e) {
88  if (static_cast<bool>(onFailure))
89  onFailure(ERROR_SERVER, e.what());
90  return;
91  }
92 
93  try {
94  command->validateResponse(parameters);
95  }
97  if (static_cast<bool>(onFailure))
98  onFailure(ERROR_SERVER, e.what());
99  return;
100  }
101 
102  if (static_cast<bool>(onSuccess))
103  onSuccess(parameters);
104 }
105 
106 } // namespace nfd
107 } // 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 & 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:48
const time::milliseconds & getTimeout() const
represents an Interest packet
Definition: interest.hpp:45
void sign(Data &data, const SigningInfo &params=DEFAULT_SIGNING_INFO)
Sign data according to the supplied signing information.
Definition: key-chain.cpp:501
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
contains options for ControlCommand execution
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-common.hpp:40
Abstraction to communicate with local or remote NDN forwarder.
Definition: face.hpp:119
Name abstraction to represent an absolute name.
Definition: name.hpp:46
const Name & getPrefix() const
void wireDecode(const Block &block)
Block blockFromValue() const
Definition: block.cpp:437
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 ...
const std::string & getText() const
ControlCommand response.
const PendingInterestId * expressInterest(const Interest &interest, const DataCallback &afterSatisfied, const NackCallback &afterNacked, const TimeoutCallback &afterTimeout)
Express Interest.
Definition: face.cpp:117
represents an error in ControlParameters
represents a Data packet
Definition: data.hpp:39
Interest & setInterestLifetime(const time::milliseconds &interestLifetime)
Definition: interest.hpp:238
represents an error in TLV encoding or decoding
Definition: tlv.hpp:50
static const uint32_t ERROR_SERVER
error code for server error
static const uint32_t ERROR_NACK
error code for network Nack