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 | |
| 7 | #include "common.hpp" |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 8 | #include "../face.hpp" |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 9 | |
| 10 | #include "nfd-controller.hpp" |
| 11 | #include "nfd-fib-management-options.hpp" |
hilata | a99e37e | 2014-02-15 23:52:46 -0600 | [diff] [blame] | 12 | #include "nfd-face-management-options.hpp" |
hilata | 7d160f2 | 2014-03-13 19:51:42 -0500 | [diff] [blame^] | 13 | #include "nfd-strategy-choice-options.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 |
| 25 | Controller::selfRegisterPrefix(const Name& prefixToRegister, |
| 26 | const SuccessCallback& onSuccess, |
| 27 | const FailCallback& onFail) |
| 28 | { |
Obaid | 6e7f5f1 | 2014-03-11 14:46:10 -0500 | [diff] [blame] | 29 | fibAddNextHop(prefixToRegister, 0, 0, bind(onSuccess), onFail); |
Alexander Afanasyev | 21abc10 | 2014-02-18 18:59:02 -0800 | [diff] [blame] | 30 | } |
| 31 | |
| 32 | void |
Alexander Afanasyev | 884280c | 2014-03-07 09:40:39 +0000 | [diff] [blame] | 33 | Controller::selfDeregisterPrefix(const Name& prefixToDeRegister, |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 34 | const SuccessCallback& onSuccess, |
| 35 | const FailCallback& onFail) |
| 36 | { |
Obaid | 6e7f5f1 | 2014-03-11 14:46:10 -0500 | [diff] [blame] | 37 | fibRemoveNextHop(prefixToDeRegister, 0, bind(onSuccess), onFail); |
Alexander Afanasyev | 884280c | 2014-03-07 09:40:39 +0000 | [diff] [blame] | 38 | } |
hilata | a99e37e | 2014-02-15 23:52:46 -0600 | [diff] [blame] | 39 | |
Alexander Afanasyev | 884280c | 2014-03-07 09:40:39 +0000 | [diff] [blame] | 40 | void |
| 41 | Controller::fibAddNextHop(const Name& prefix, uint64_t faceId, int cost, |
Obaid | 6e7f5f1 | 2014-03-11 14:46:10 -0500 | [diff] [blame] | 42 | const FibCommandSucceedCallback& onSuccess, |
| 43 | const FailCallback& onFail) |
Alexander Afanasyev | 884280c | 2014-03-07 09:40:39 +0000 | [diff] [blame] | 44 | { |
| 45 | startFibCommand("add-nexthop", |
| 46 | FibManagementOptions() |
| 47 | .setName(prefix) |
| 48 | .setFaceId(faceId) |
| 49 | .setCost(cost), |
Obaid | 6e7f5f1 | 2014-03-11 14:46:10 -0500 | [diff] [blame] | 50 | onSuccess, onFail); |
Alexander Afanasyev | 884280c | 2014-03-07 09:40:39 +0000 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | void |
| 54 | Controller::fibRemoveNextHop(const Name& prefix, uint64_t faceId, |
Obaid | 6e7f5f1 | 2014-03-11 14:46:10 -0500 | [diff] [blame] | 55 | const FibCommandSucceedCallback& onSuccess, |
| 56 | const FailCallback& onFail) |
Alexander Afanasyev | 884280c | 2014-03-07 09:40:39 +0000 | [diff] [blame] | 57 | { |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 58 | startFibCommand("remove-nexthop", |
| 59 | FibManagementOptions() |
Alexander Afanasyev | 884280c | 2014-03-07 09:40:39 +0000 | [diff] [blame] | 60 | .setName(prefix) |
| 61 | .setFaceId(faceId), |
Obaid | 6e7f5f1 | 2014-03-11 14:46:10 -0500 | [diff] [blame] | 62 | onSuccess, onFail); |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | void |
| 66 | Controller::startFibCommand(const std::string& command, |
| 67 | const FibManagementOptions& options, |
| 68 | const FibCommandSucceedCallback& onSuccess, |
| 69 | const FailCallback& onFail) |
| 70 | { |
| 71 | Name fibCommandInterestName("/localhost/nfd/fib"); |
| 72 | fibCommandInterestName |
| 73 | .append(command) |
| 74 | .append(options.wireEncode()); |
| 75 | |
| 76 | Interest fibCommandInterest(fibCommandInterestName); |
Alexander Afanasyev | 884280c | 2014-03-07 09:40:39 +0000 | [diff] [blame] | 77 | m_commandInterestGenerator.generate(fibCommandInterest); |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 78 | |
| 79 | m_face.expressInterest(fibCommandInterest, |
| 80 | bind(&Controller::processFibCommandResponse, this, _2, |
| 81 | onSuccess, onFail), |
| 82 | bind(onFail, "Command Interest timed out")); |
| 83 | } |
| 84 | |
| 85 | void |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 86 | Controller::processFibCommandResponse(Data& data, |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 87 | const FibCommandSucceedCallback& onSuccess, |
| 88 | const FailCallback& onFail) |
| 89 | { |
Obaid | 6e7f5f1 | 2014-03-11 14:46:10 -0500 | [diff] [blame] | 90 | /// \todo Add validation of incoming Data |
| 91 | |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 92 | try |
| 93 | { |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 94 | ControlResponse response(data.getContent().blockFromValue()); |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 95 | if (response.getCode() != 200) |
| 96 | return onFail(response.getText()); |
| 97 | |
| 98 | FibManagementOptions options(response.getBody()); |
| 99 | return onSuccess(options); |
| 100 | } |
| 101 | catch(ndn::Tlv::Error& e) |
| 102 | { |
| 103 | if (static_cast<bool>(onFail)) |
| 104 | return onFail(e.what()); |
| 105 | } |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 106 | } |
| 107 | |
hilata | a99e37e | 2014-02-15 23:52:46 -0600 | [diff] [blame] | 108 | void |
| 109 | Controller::startFaceCommand(const std::string& command, |
| 110 | const FaceManagementOptions& options, |
| 111 | const FaceCommandSucceedCallback& onSuccess, |
| 112 | const FailCallback& onFail) |
| 113 | { |
| 114 | Name faceCommandInterestName("/localhost/nfd/faces"); |
| 115 | faceCommandInterestName |
| 116 | .append(command) |
| 117 | .append(options.wireEncode()); |
| 118 | |
| 119 | Interest faceCommandInterest(faceCommandInterestName); |
Alexander Afanasyev | 884280c | 2014-03-07 09:40:39 +0000 | [diff] [blame] | 120 | m_commandInterestGenerator.generate(faceCommandInterest); |
hilata | a99e37e | 2014-02-15 23:52:46 -0600 | [diff] [blame] | 121 | |
| 122 | m_face.expressInterest(faceCommandInterest, |
| 123 | bind(&Controller::processFaceCommandResponse, this, _2, |
| 124 | onSuccess, onFail), |
| 125 | bind(onFail, "Command Interest timed out")); |
| 126 | } |
hilata | 7d160f2 | 2014-03-13 19:51:42 -0500 | [diff] [blame^] | 127 | |
hilata | a99e37e | 2014-02-15 23:52:46 -0600 | [diff] [blame] | 128 | void |
| 129 | Controller::processFaceCommandResponse(Data& data, |
| 130 | const FaceCommandSucceedCallback& onSuccess, |
| 131 | const FailCallback& onFail) |
| 132 | { |
Obaid | 6e7f5f1 | 2014-03-11 14:46:10 -0500 | [diff] [blame] | 133 | /// \todo Add validation of incoming Data |
hilata | 7d160f2 | 2014-03-13 19:51:42 -0500 | [diff] [blame^] | 134 | |
hilata | a99e37e | 2014-02-15 23:52:46 -0600 | [diff] [blame] | 135 | try |
hilata | 7d160f2 | 2014-03-13 19:51:42 -0500 | [diff] [blame^] | 136 | { |
| 137 | ControlResponse response(data.getContent().blockFromValue()); |
| 138 | if (response.getCode() != 200) |
| 139 | return onFail(response.getText()); |
| 140 | |
| 141 | FaceManagementOptions options(response.getBody()); |
| 142 | return onSuccess(options); |
| 143 | } |
hilata | a99e37e | 2014-02-15 23:52:46 -0600 | [diff] [blame] | 144 | catch(ndn::Tlv::Error& e) |
hilata | 7d160f2 | 2014-03-13 19:51:42 -0500 | [diff] [blame^] | 145 | { |
| 146 | if (static_cast<bool>(onFail)) |
| 147 | return onFail(e.what()); |
| 148 | } |
hilata | a99e37e | 2014-02-15 23:52:46 -0600 | [diff] [blame] | 149 | } |
Alexander Afanasyev | 884280c | 2014-03-07 09:40:39 +0000 | [diff] [blame] | 150 | |
hilata | 7d160f2 | 2014-03-13 19:51:42 -0500 | [diff] [blame^] | 151 | void |
| 152 | Controller::startStrategyChoiceCommand(const std::string& command, |
| 153 | const StrategyChoiceOptions& options, |
| 154 | const StrategyChoiceCommandSucceedCallback& onSuccess, |
| 155 | const FailCallback& onFail) |
| 156 | { |
| 157 | Name strategyChoiceCommandInterestName("/localhost/nfd/strategy-choice"); |
| 158 | strategyChoiceCommandInterestName |
| 159 | .append(command) |
| 160 | .append(options.wireEncode()); |
| 161 | |
| 162 | Interest strategyChoiceCommandInterest(strategyChoiceCommandInterestName); |
| 163 | m_commandInterestGenerator.generate(strategyChoiceCommandInterest); |
| 164 | |
| 165 | m_face.expressInterest(strategyChoiceCommandInterest, |
| 166 | bind(&Controller::processStrategyChoiceCommandResponse, this, _2, |
| 167 | onSuccess, onFail), |
| 168 | bind(onFail, "Command Interest timed out")); |
| 169 | } |
| 170 | void |
| 171 | Controller::processStrategyChoiceCommandResponse( |
| 172 | Data& data, |
| 173 | const StrategyChoiceCommandSucceedCallback& onSuccess, |
| 174 | const FailCallback& onFail) |
| 175 | { |
| 176 | /// \todo Add validation of incoming Data |
| 177 | |
| 178 | try |
| 179 | { |
| 180 | ControlResponse response(data.getContent().blockFromValue()); |
| 181 | if (response.getCode() != 200) |
| 182 | return onFail(response.getText()); |
| 183 | |
| 184 | StrategyChoiceOptions options(response.getBody()); |
| 185 | return onSuccess(options); |
| 186 | } |
| 187 | catch (ndn::Tlv::Error& error) |
| 188 | { |
| 189 | if (static_cast<bool>(onFail)) |
| 190 | return onFail(error.what()); |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 195 | } // namespace nfd |
| 196 | } // namespace ndn |