blob: 08285c1261d71255ee082c2d518b8ca20be391e6 [file] [log] [blame]
Alexander Afanasyeve289b532014-02-09 22:14:44 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2/**
3 * Copyright (C) 2013 Regents of the University of California.
4 * See COPYING for copyright and distribution information.
5 */
6
Alexander Afanasyeve289b532014-02-09 22:14:44 -08007#include "nfd-controller.hpp"
Alexander Afanasyeve289b532014-02-09 22:14:44 -08008#include "nfd-control-response.hpp"
9
10namespace ndn {
11namespace nfd {
12
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080013Controller::Controller(Face& face)
Alexander Afanasyeve289b532014-02-09 22:14:44 -080014 : m_face(face)
Alexander Afanasyeve289b532014-02-09 22:14:44 -080015{
16}
17
18void
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070019Controller::processCommandResponse(const Data& data,
20 const shared_ptr<ControlCommand>& command,
21 const CommandSucceedCallback& onSuccess,
22 const CommandFailCallback& onFailure)
23{
24 /// \todo verify Data signature
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
69void
Alexander Afanasyeve289b532014-02-09 22:14:44 -080070Controller::selfRegisterPrefix(const Name& prefixToRegister,
71 const SuccessCallback& onSuccess,
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -070072 const FailCallback& onFail)
Alexander Afanasyeve289b532014-02-09 22:14:44 -080073{
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070074 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));
Alexander Afanasyev21abc102014-02-18 18:59:02 -080083}
84
85void
Alexander Afanasyev884280c2014-03-07 09:40:39 +000086Controller::selfDeregisterPrefix(const Name& prefixToDeRegister,
Alexander Afanasyeve289b532014-02-09 22:14:44 -080087 const SuccessCallback& onSuccess,
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -070088 const FailCallback& onFail)
Alexander Afanasyeve289b532014-02-09 22:14:44 -080089{
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070090 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));
Alexander Afanasyev884280c2014-03-07 09:40:39 +000099}
hilataa99e37e2014-02-15 23:52:46 -0600100
Alexander Afanasyeve289b532014-02-09 22:14:44 -0800101} // namespace nfd
102} // namespace ndn