blob: 661b08ac136c4b0e6020be8bc47c050e1f75fad5 [file] [log] [blame]
Alexander Afanasyeve289b532014-02-09 22:14:44 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2/**
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07003 * Copyright (c) 2013-2014, Regents of the University of California.
4 * All rights reserved.
5 *
6 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
7 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
8 *
9 * This file licensed under New BSD License. See COPYING for detailed information about
10 * ndn-cxx library copyright, permissions, and redistribution restrictions.
Alexander Afanasyeve289b532014-02-09 22:14:44 -080011 */
12
Alexander Afanasyeve289b532014-02-09 22:14:44 -080013#include "nfd-controller.hpp"
Alexander Afanasyeve289b532014-02-09 22:14:44 -080014#include "nfd-control-response.hpp"
Yingdi Yue66bf2a2014-04-28 17:07:36 -070015#include "../security/identity-certificate.hpp"
Alexander Afanasyeve289b532014-02-09 22:14:44 -080016
17namespace ndn {
18namespace nfd {
19
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080020Controller::Controller(Face& face)
Alexander Afanasyeve289b532014-02-09 22:14:44 -080021 : m_face(face)
Alexander Afanasyeve289b532014-02-09 22:14:44 -080022{
23}
24
25void
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070026Controller::processCommandResponse(const Data& data,
27 const shared_ptr<ControlCommand>& command,
28 const CommandSucceedCallback& onSuccess,
29 const CommandFailCallback& onFailure)
30{
31 /// \todo verify Data signature
32
33 const uint32_t serverErrorCode = 500;
34
35 ControlResponse response;
36 try {
37 response.wireDecode(data.getContent().blockFromValue());
38 }
39 catch (ndn::Tlv::Error& e) {
40 if (static_cast<bool>(onFailure))
41 onFailure(serverErrorCode, e.what());
42 return;
43 }
44
45 uint32_t code = response.getCode();
46 const uint32_t errorCodeLowerBound = 400;
47 if (code >= errorCodeLowerBound) {
48 if (static_cast<bool>(onFailure))
49 onFailure(code, response.getText());
50 return;
51 }
52
53 ControlParameters parameters;
54 try {
55 parameters.wireDecode(response.getBody());
56 }
57 catch (ndn::Tlv::Error& e) {
58 if (static_cast<bool>(onFailure))
59 onFailure(serverErrorCode, e.what());
60 return;
61 }
62
63 try {
64 command->validateResponse(parameters);
65 }
66 catch (ControlCommand::ArgumentError& e) {
67 if (static_cast<bool>(onFailure))
68 onFailure(serverErrorCode, e.what());
69 return;
70 }
71
72 onSuccess(parameters);
73}
74
75
76void
Alexander Afanasyeve289b532014-02-09 22:14:44 -080077Controller::selfRegisterPrefix(const Name& prefixToRegister,
78 const SuccessCallback& onSuccess,
Yingdi Yue66bf2a2014-04-28 17:07:36 -070079 const FailCallback& onFail,
80 const Sign& sign)
Alexander Afanasyeve289b532014-02-09 22:14:44 -080081{
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070082 const uint32_t selfFaceId = 0;
83
84 ControlParameters parameters;
85 parameters.setName(prefixToRegister)
86 .setFaceId(selfFaceId);
87
88 this->start<FibAddNextHopCommand>(parameters,
89 bind(onSuccess),
Yingdi Yue66bf2a2014-04-28 17:07:36 -070090 bind(onFail, _2),
91 sign);
Alexander Afanasyev21abc102014-02-18 18:59:02 -080092}
93
94void
Alexander Afanasyev884280c2014-03-07 09:40:39 +000095Controller::selfDeregisterPrefix(const Name& prefixToDeRegister,
Alexander Afanasyeve289b532014-02-09 22:14:44 -080096 const SuccessCallback& onSuccess,
Yingdi Yue66bf2a2014-04-28 17:07:36 -070097 const FailCallback& onFail,
98 const Sign& sign)
Alexander Afanasyeve289b532014-02-09 22:14:44 -080099{
Junxiao Shi7b6b79d2014-03-26 20:59:35 -0700100 const uint32_t selfFaceId = 0;
101
102 ControlParameters parameters;
103 parameters.setName(prefixToDeRegister)
104 .setFaceId(selfFaceId);
105
106 this->start<FibRemoveNextHopCommand>(parameters,
107 bind(onSuccess),
Yingdi Yue66bf2a2014-04-28 17:07:36 -0700108 bind(onFail, _2),
109 sign);
Alexander Afanasyev884280c2014-03-07 09:40:39 +0000110}
hilataa99e37e2014-02-15 23:52:46 -0600111
Alexander Afanasyeve289b532014-02-09 22:14:44 -0800112} // namespace nfd
113} // namespace ndn