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" |
| 15 | |
| 16 | namespace ndn { |
| 17 | namespace nfd { |
| 18 | |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 19 | Controller::Controller(Face& face) |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 20 | : m_face(face) |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 21 | { |
| 22 | } |
| 23 | |
| 24 | void |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 25 | Controller::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 | |
| 75 | void |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 76 | Controller::selfRegisterPrefix(const Name& prefixToRegister, |
| 77 | const SuccessCallback& onSuccess, |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 78 | const FailCallback& onFail) |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 79 | { |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 80 | 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 Afanasyev | 21abc10 | 2014-02-18 18:59:02 -0800 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | void |
Alexander Afanasyev | 884280c | 2014-03-07 09:40:39 +0000 | [diff] [blame] | 92 | Controller::selfDeregisterPrefix(const Name& prefixToDeRegister, |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 93 | const SuccessCallback& onSuccess, |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 94 | const FailCallback& onFail) |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 95 | { |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 96 | 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 Afanasyev | 884280c | 2014-03-07 09:40:39 +0000 | [diff] [blame] | 105 | } |
hilata | a99e37e | 2014-02-15 23:52:46 -0600 | [diff] [blame] | 106 | |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 107 | } // namespace nfd |
| 108 | } // namespace ndn |