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" |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 13 | #include "nfd-control-response.hpp" |
| 14 | |
| 15 | namespace ndn { |
| 16 | namespace nfd { |
| 17 | |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 18 | Controller::Controller(Face& face) |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 19 | : m_face(face) |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 20 | { |
| 21 | } |
| 22 | |
| 23 | void |
| 24 | Controller::selfRegisterPrefix(const Name& prefixToRegister, |
| 25 | const SuccessCallback& onSuccess, |
| 26 | const FailCallback& onFail) |
| 27 | { |
Alexander Afanasyev | 884280c | 2014-03-07 09:40:39 +0000 | [diff] [blame^] | 28 | fibAddNextHop(prefixToRegister, 0, 0, onSuccess, onFail); |
Alexander Afanasyev | 21abc10 | 2014-02-18 18:59:02 -0800 | [diff] [blame] | 29 | } |
| 30 | |
| 31 | void |
Alexander Afanasyev | 884280c | 2014-03-07 09:40:39 +0000 | [diff] [blame^] | 32 | Controller::selfDeregisterPrefix(const Name& prefixToDeRegister, |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 33 | const SuccessCallback& onSuccess, |
| 34 | const FailCallback& onFail) |
| 35 | { |
Alexander Afanasyev | 884280c | 2014-03-07 09:40:39 +0000 | [diff] [blame^] | 36 | fibRemoveNextHop(prefixToDeRegister, 0, onSuccess, onFail); |
| 37 | } |
hilata | a99e37e | 2014-02-15 23:52:46 -0600 | [diff] [blame] | 38 | |
Alexander Afanasyev | 884280c | 2014-03-07 09:40:39 +0000 | [diff] [blame^] | 39 | void |
| 40 | Controller::fibAddNextHop(const Name& prefix, uint64_t faceId, int cost, |
| 41 | const SuccessCallback& onSuccess, |
| 42 | const FailCallback& onFail) |
| 43 | { |
| 44 | startFibCommand("add-nexthop", |
| 45 | FibManagementOptions() |
| 46 | .setName(prefix) |
| 47 | .setFaceId(faceId) |
| 48 | .setCost(cost), |
| 49 | bind(onSuccess), onFail); |
| 50 | } |
| 51 | |
| 52 | void |
| 53 | Controller::fibRemoveNextHop(const Name& prefix, uint64_t faceId, |
| 54 | const SuccessCallback& onSuccess, |
| 55 | const FailCallback& onFail) |
| 56 | { |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 57 | startFibCommand("remove-nexthop", |
| 58 | FibManagementOptions() |
Alexander Afanasyev | 884280c | 2014-03-07 09:40:39 +0000 | [diff] [blame^] | 59 | .setName(prefix) |
| 60 | .setFaceId(faceId), |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 61 | bind(onSuccess), onFail); |
| 62 | } |
| 63 | |
| 64 | void |
| 65 | Controller::startFibCommand(const std::string& command, |
| 66 | const FibManagementOptions& options, |
| 67 | const FibCommandSucceedCallback& onSuccess, |
| 68 | const FailCallback& onFail) |
| 69 | { |
| 70 | Name fibCommandInterestName("/localhost/nfd/fib"); |
| 71 | fibCommandInterestName |
| 72 | .append(command) |
| 73 | .append(options.wireEncode()); |
| 74 | |
| 75 | Interest fibCommandInterest(fibCommandInterestName); |
Alexander Afanasyev | 884280c | 2014-03-07 09:40:39 +0000 | [diff] [blame^] | 76 | m_commandInterestGenerator.generate(fibCommandInterest); |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 77 | |
| 78 | m_face.expressInterest(fibCommandInterest, |
| 79 | bind(&Controller::processFibCommandResponse, this, _2, |
| 80 | onSuccess, onFail), |
| 81 | bind(onFail, "Command Interest timed out")); |
| 82 | } |
| 83 | |
| 84 | void |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 85 | Controller::processFibCommandResponse(Data& data, |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 86 | const FibCommandSucceedCallback& onSuccess, |
| 87 | const FailCallback& onFail) |
| 88 | { |
| 89 | try |
| 90 | { |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 91 | ControlResponse response(data.getContent().blockFromValue()); |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 92 | if (response.getCode() != 200) |
| 93 | return onFail(response.getText()); |
| 94 | |
| 95 | FibManagementOptions options(response.getBody()); |
| 96 | return onSuccess(options); |
| 97 | } |
| 98 | catch(ndn::Tlv::Error& e) |
| 99 | { |
| 100 | if (static_cast<bool>(onFail)) |
| 101 | return onFail(e.what()); |
| 102 | } |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 103 | } |
| 104 | |
hilata | a99e37e | 2014-02-15 23:52:46 -0600 | [diff] [blame] | 105 | void |
| 106 | Controller::startFaceCommand(const std::string& command, |
| 107 | const FaceManagementOptions& options, |
| 108 | const FaceCommandSucceedCallback& onSuccess, |
| 109 | const FailCallback& onFail) |
| 110 | { |
| 111 | Name faceCommandInterestName("/localhost/nfd/faces"); |
| 112 | faceCommandInterestName |
| 113 | .append(command) |
| 114 | .append(options.wireEncode()); |
| 115 | |
| 116 | Interest faceCommandInterest(faceCommandInterestName); |
Alexander Afanasyev | 884280c | 2014-03-07 09:40:39 +0000 | [diff] [blame^] | 117 | m_commandInterestGenerator.generate(faceCommandInterest); |
hilata | a99e37e | 2014-02-15 23:52:46 -0600 | [diff] [blame] | 118 | |
| 119 | m_face.expressInterest(faceCommandInterest, |
| 120 | bind(&Controller::processFaceCommandResponse, this, _2, |
| 121 | onSuccess, onFail), |
| 122 | bind(onFail, "Command Interest timed out")); |
| 123 | } |
| 124 | |
| 125 | void |
| 126 | Controller::processFaceCommandResponse(Data& data, |
| 127 | const FaceCommandSucceedCallback& onSuccess, |
| 128 | const FailCallback& onFail) |
| 129 | { |
| 130 | try |
| 131 | { |
| 132 | ControlResponse response(data.getContent().blockFromValue()); |
| 133 | if (response.getCode() != 200) |
| 134 | return onFail(response.getText()); |
| 135 | |
| 136 | FaceManagementOptions options(response.getBody()); |
| 137 | return onSuccess(options); |
| 138 | } |
| 139 | catch(ndn::Tlv::Error& e) |
| 140 | { |
| 141 | if (static_cast<bool>(onFail)) |
| 142 | return onFail(e.what()); |
| 143 | } |
| 144 | } |
Alexander Afanasyev | 884280c | 2014-03-07 09:40:39 +0000 | [diff] [blame^] | 145 | |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 146 | } // namespace nfd |
| 147 | } // namespace ndn |