All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
nfd-controller.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
7 #include "nfd-controller.hpp"
9 
10 namespace ndn {
11 namespace nfd {
12 
14  : m_face(face)
15 {
16 }
17 
18 void
19 Controller::processCommandResponse(const Data& data,
20  const shared_ptr<ControlCommand>& command,
21  const CommandSucceedCallback& onSuccess,
22  const CommandFailCallback& onFailure)
23 {
25 
26  const uint32_t serverErrorCode = 500;
27 
28  ControlResponse response;
29  try {
30  response.wireDecode(data.getContent().blockFromValue());
31  }
32  catch (ndn::Tlv::Error& e) {
33  if (static_cast<bool>(onFailure))
34  onFailure(serverErrorCode, e.what());
35  return;
36  }
37 
38  uint32_t code = response.getCode();
39  const uint32_t errorCodeLowerBound = 400;
40  if (code >= errorCodeLowerBound) {
41  if (static_cast<bool>(onFailure))
42  onFailure(code, response.getText());
43  return;
44  }
45 
46  ControlParameters parameters;
47  try {
48  parameters.wireDecode(response.getBody());
49  }
50  catch (ndn::Tlv::Error& e) {
51  if (static_cast<bool>(onFailure))
52  onFailure(serverErrorCode, e.what());
53  return;
54  }
55 
56  try {
57  command->validateResponse(parameters);
58  }
59  catch (ControlCommand::ArgumentError& e) {
60  if (static_cast<bool>(onFailure))
61  onFailure(serverErrorCode, e.what());
62  return;
63  }
64 
65  onSuccess(parameters);
66 }
67 
68 
69 void
70 Controller::selfRegisterPrefix(const Name& prefixToRegister,
71  const SuccessCallback& onSuccess,
72  const FailCallback& onFail)
73 {
74  const uint32_t selfFaceId = 0;
75 
76  ControlParameters parameters;
77  parameters.setName(prefixToRegister)
78  .setFaceId(selfFaceId);
79 
80  this->start<FibAddNextHopCommand>(parameters,
81  bind(onSuccess),
82  bind(onFail, _2));
83 }
84 
85 void
86 Controller::selfDeregisterPrefix(const Name& prefixToDeRegister,
87  const SuccessCallback& onSuccess,
88  const FailCallback& onFail)
89 {
90  const uint32_t selfFaceId = 0;
91 
92  ControlParameters parameters;
93  parameters.setName(prefixToDeRegister)
94  .setFaceId(selfFaceId);
95 
96  this->start<FibRemoveNextHopCommand>(parameters,
97  bind(onSuccess),
98  bind(onFail, _2));
99 }
100 
101 } // namespace nfd
102 } // namespace ndn
ControlParameters & setFaceId(uint64_t faceId)
represents parameters in a ControlCommand request or response
const Block & getBody() const
function< void()> SuccessCallback
Definition: controller.hpp:23
Block blockFromValue() const
Definition: block.cpp:352
function< void(const std::string &)> FailCallback
Definition: controller.hpp:24
Class defining abstraction of ControlResponse for NFD Control Protocol.
Abstraction to communicate with local or remote NDN forwarder.
Definition: face.hpp:54
const std::string & getText() const
void wireDecode(const Block &block)
A Name holds an array of Name::Component and represents an NDN name.
Definition: name.hpp:26
const Block & getContent() const
Get content Block.
Definition: data.hpp:411
ControlParameters & setName(const Name &name)
virtual void selfRegisterPrefix(const Name &prefixToRegister, const SuccessCallback &onSuccess, const FailCallback &onFail)
virtual void selfDeregisterPrefix(const Name &prefixToDeRegister, const SuccessCallback &onSuccess, const FailCallback &onFail)