Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 1 | /* -*- 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 Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 7 | #include "nfd-controller.hpp" |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 8 | #include "nfd-control-response.hpp" |
| 9 | |
| 10 | namespace ndn { |
| 11 | namespace nfd { |
| 12 | |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 13 | Controller::Controller(Face& face) |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 14 | : m_face(face) |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 15 | { |
| 16 | } |
| 17 | |
| 18 | void |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 19 | Controller::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 | |
| 69 | void |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 70 | Controller::selfRegisterPrefix(const Name& prefixToRegister, |
| 71 | const SuccessCallback& onSuccess, |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame^] | 72 | const FailCallback& onFail) |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 73 | { |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 74 | 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 Afanasyev | 21abc10 | 2014-02-18 18:59:02 -0800 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | void |
Alexander Afanasyev | 884280c | 2014-03-07 09:40:39 +0000 | [diff] [blame] | 86 | Controller::selfDeregisterPrefix(const Name& prefixToDeRegister, |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 87 | const SuccessCallback& onSuccess, |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame^] | 88 | const FailCallback& onFail) |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 89 | { |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 90 | 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 Afanasyev | 884280c | 2014-03-07 09:40:39 +0000 | [diff] [blame] | 99 | } |
hilata | a99e37e | 2014-02-15 23:52:46 -0600 | [diff] [blame] | 100 | |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 101 | } // namespace nfd |
| 102 | } // namespace ndn |