Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /** |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 3 | * 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 Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 11 | */ |
| 12 | |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 13 | #include "nfd-controller.hpp" |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 14 | #include "nfd-control-response.hpp" |
Yingdi Yu | e66bf2a | 2014-04-28 17:07:36 -0700 | [diff] [blame] | 15 | #include "../security/identity-certificate.hpp" |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 16 | |
| 17 | namespace ndn { |
| 18 | namespace nfd { |
| 19 | |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 20 | Controller::Controller(Face& face) |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 21 | : m_face(face) |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 22 | { |
| 23 | } |
| 24 | |
| 25 | void |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 26 | Controller::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 | |
| 76 | void |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 77 | Controller::selfRegisterPrefix(const Name& prefixToRegister, |
| 78 | const SuccessCallback& onSuccess, |
Yingdi Yu | e66bf2a | 2014-04-28 17:07:36 -0700 | [diff] [blame] | 79 | const FailCallback& onFail, |
| 80 | const Sign& sign) |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 81 | { |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 82 | 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 Yu | e66bf2a | 2014-04-28 17:07:36 -0700 | [diff] [blame] | 90 | bind(onFail, _2), |
| 91 | sign); |
Alexander Afanasyev | 21abc10 | 2014-02-18 18:59:02 -0800 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | void |
Alexander Afanasyev | 884280c | 2014-03-07 09:40:39 +0000 | [diff] [blame] | 95 | Controller::selfDeregisterPrefix(const Name& prefixToDeRegister, |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 96 | const SuccessCallback& onSuccess, |
Yingdi Yu | e66bf2a | 2014-04-28 17:07:36 -0700 | [diff] [blame] | 97 | const FailCallback& onFail, |
| 98 | const Sign& sign) |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 99 | { |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 100 | 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 Yu | e66bf2a | 2014-04-28 17:07:36 -0700 | [diff] [blame] | 108 | bind(onFail, _2), |
| 109 | sign); |
Alexander Afanasyev | 884280c | 2014-03-07 09:40:39 +0000 | [diff] [blame] | 110 | } |
hilata | a99e37e | 2014-02-15 23:52:46 -0600 | [diff] [blame] | 111 | |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 112 | } // namespace nfd |
| 113 | } // namespace ndn |