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, |
| 72 | const FailCallback& onFail) |
| 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, |
| 88 | const FailCallback& onFail) |
| 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 | 884280c | 2014-03-07 09:40:39 +0000 | [diff] [blame] | 101 | void |
| 102 | Controller::fibAddNextHop(const Name& prefix, uint64_t faceId, int cost, |
Obaid | 6e7f5f1 | 2014-03-11 14:46:10 -0500 | [diff] [blame] | 103 | const FibCommandSucceedCallback& onSuccess, |
| 104 | const FailCallback& onFail) |
Alexander Afanasyev | 884280c | 2014-03-07 09:40:39 +0000 | [diff] [blame] | 105 | { |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame^] | 106 | BOOST_ASSERT(cost >= 0); |
| 107 | |
| 108 | ControlParameters parameters; |
| 109 | parameters.setName(prefix) |
| 110 | .setFaceId(faceId) |
| 111 | .setCost(static_cast<uint64_t>(cost)); |
| 112 | |
| 113 | this->start<FibAddNextHopCommand>(parameters, |
| 114 | onSuccess, |
| 115 | bind(onFail, _2)); |
Alexander Afanasyev | 884280c | 2014-03-07 09:40:39 +0000 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | void |
| 119 | Controller::fibRemoveNextHop(const Name& prefix, uint64_t faceId, |
Obaid | 6e7f5f1 | 2014-03-11 14:46:10 -0500 | [diff] [blame] | 120 | const FibCommandSucceedCallback& onSuccess, |
| 121 | const FailCallback& onFail) |
Alexander Afanasyev | 884280c | 2014-03-07 09:40:39 +0000 | [diff] [blame] | 122 | { |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame^] | 123 | ControlParameters parameters; |
| 124 | parameters.setName(prefix) |
| 125 | .setFaceId(faceId); |
| 126 | |
| 127 | this->start<FibRemoveNextHopCommand>(parameters, |
| 128 | onSuccess, |
| 129 | bind(onFail, _2)); |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | void |
| 133 | Controller::startFibCommand(const std::string& command, |
| 134 | const FibManagementOptions& options, |
| 135 | const FibCommandSucceedCallback& onSuccess, |
| 136 | const FailCallback& onFail) |
| 137 | { |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame^] | 138 | if (command == "add-nexthop") { |
| 139 | this->start<FibAddNextHopCommand>(options, |
| 140 | onSuccess, |
| 141 | bind(onFail, _2)); |
| 142 | } |
| 143 | else if (command == "remove-nexthop") { |
| 144 | this->start<FibRemoveNextHopCommand>(options, |
| 145 | onSuccess, |
| 146 | bind(onFail, _2)); |
| 147 | } |
| 148 | else { |
| 149 | onFail("unknown command"); |
| 150 | } |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 151 | } |
| 152 | |
hilata | a99e37e | 2014-02-15 23:52:46 -0600 | [diff] [blame] | 153 | void |
| 154 | Controller::startFaceCommand(const std::string& command, |
| 155 | const FaceManagementOptions& options, |
| 156 | const FaceCommandSucceedCallback& onSuccess, |
| 157 | const FailCallback& onFail) |
| 158 | { |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame^] | 159 | if (command == "create") { |
| 160 | this->start<FaceCreateCommand>(options, |
| 161 | onSuccess, |
| 162 | bind(onFail, _2)); |
hilata | 7d160f2 | 2014-03-13 19:51:42 -0500 | [diff] [blame] | 163 | } |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame^] | 164 | else if (command == "destroy") { |
| 165 | this->start<FaceDestroyCommand>(options, |
| 166 | onSuccess, |
| 167 | bind(onFail, _2)); |
| 168 | } |
| 169 | // enable-local-control and disable-local-control are not in legacy API. |
| 170 | else { |
| 171 | onFail("unknown command"); |
hilata | 7d160f2 | 2014-03-13 19:51:42 -0500 | [diff] [blame] | 172 | } |
hilata | a99e37e | 2014-02-15 23:52:46 -0600 | [diff] [blame] | 173 | } |
Alexander Afanasyev | 884280c | 2014-03-07 09:40:39 +0000 | [diff] [blame] | 174 | |
hilata | 7d160f2 | 2014-03-13 19:51:42 -0500 | [diff] [blame] | 175 | void |
| 176 | Controller::startStrategyChoiceCommand(const std::string& command, |
| 177 | const StrategyChoiceOptions& options, |
| 178 | const StrategyChoiceCommandSucceedCallback& onSuccess, |
| 179 | const FailCallback& onFail) |
| 180 | { |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame^] | 181 | if (command == "set") { |
| 182 | this->start<StrategyChoiceSetCommand>(options, |
| 183 | onSuccess, |
| 184 | bind(onFail, _2)); |
hilata | 7d160f2 | 2014-03-13 19:51:42 -0500 | [diff] [blame] | 185 | } |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame^] | 186 | else if (command == "unset") { |
| 187 | this->start<StrategyChoiceUnsetCommand>(options, |
| 188 | onSuccess, |
| 189 | bind(onFail, _2)); |
| 190 | } |
| 191 | else { |
| 192 | onFail("unknown command"); |
hilata | 7d160f2 | 2014-03-13 19:51:42 -0500 | [diff] [blame] | 193 | } |
| 194 | } |
| 195 | |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame^] | 196 | |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 197 | } // namespace nfd |
| 198 | } // namespace ndn |