blob: c0c7729e8d646b17f3fd8c9b20f4cc9afea82d6c [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"
15
16namespace ndn {
17namespace nfd {
18
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080019Controller::Controller(Face& face)
Alexander Afanasyeve289b532014-02-09 22:14:44 -080020 : m_face(face)
Alexander Afanasyeve289b532014-02-09 22:14:44 -080021{
22}
23
24void
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070025Controller::processCommandResponse(const Data& data,
26 const shared_ptr<ControlCommand>& command,
27 const CommandSucceedCallback& onSuccess,
28 const CommandFailCallback& onFailure)
29{
30 /// \todo verify Data signature
31
32 const uint32_t serverErrorCode = 500;
33
34 ControlResponse response;
35 try {
36 response.wireDecode(data.getContent().blockFromValue());
37 }
38 catch (ndn::Tlv::Error& e) {
39 if (static_cast<bool>(onFailure))
40 onFailure(serverErrorCode, e.what());
41 return;
42 }
43
44 uint32_t code = response.getCode();
45 const uint32_t errorCodeLowerBound = 400;
46 if (code >= errorCodeLowerBound) {
47 if (static_cast<bool>(onFailure))
48 onFailure(code, response.getText());
49 return;
50 }
51
52 ControlParameters parameters;
53 try {
54 parameters.wireDecode(response.getBody());
55 }
56 catch (ndn::Tlv::Error& e) {
57 if (static_cast<bool>(onFailure))
58 onFailure(serverErrorCode, e.what());
59 return;
60 }
61
62 try {
63 command->validateResponse(parameters);
64 }
65 catch (ControlCommand::ArgumentError& e) {
66 if (static_cast<bool>(onFailure))
67 onFailure(serverErrorCode, e.what());
68 return;
69 }
70
71 onSuccess(parameters);
72}
73
74
75void
Alexander Afanasyeve289b532014-02-09 22:14:44 -080076Controller::selfRegisterPrefix(const Name& prefixToRegister,
77 const SuccessCallback& onSuccess,
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -070078 const FailCallback& onFail)
Alexander Afanasyeve289b532014-02-09 22:14:44 -080079{
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070080 const uint32_t selfFaceId = 0;
81
82 ControlParameters parameters;
83 parameters.setName(prefixToRegister)
84 .setFaceId(selfFaceId);
85
86 this->start<FibAddNextHopCommand>(parameters,
87 bind(onSuccess),
88 bind(onFail, _2));
Alexander Afanasyev21abc102014-02-18 18:59:02 -080089}
90
91void
Alexander Afanasyev884280c2014-03-07 09:40:39 +000092Controller::selfDeregisterPrefix(const Name& prefixToDeRegister,
Alexander Afanasyeve289b532014-02-09 22:14:44 -080093 const SuccessCallback& onSuccess,
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -070094 const FailCallback& onFail)
Alexander Afanasyeve289b532014-02-09 22:14:44 -080095{
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070096 const uint32_t selfFaceId = 0;
97
98 ControlParameters parameters;
99 parameters.setName(prefixToDeRegister)
100 .setFaceId(selfFaceId);
101
102 this->start<FibRemoveNextHopCommand>(parameters,
103 bind(onSuccess),
104 bind(onFail, _2));
Alexander Afanasyev884280c2014-03-07 09:40:39 +0000105}
hilataa99e37e2014-02-15 23:52:46 -0600106
Alexander Afanasyeve289b532014-02-09 22:14:44 -0800107} // namespace nfd
108} // namespace ndn