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 | { |
Obaid | 6e7f5f1 | 2014-03-11 14:46:10 -0500 | [diff] [blame^] | 28 | fibAddNextHop(prefixToRegister, 0, 0, bind(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 | { |
Obaid | 6e7f5f1 | 2014-03-11 14:46:10 -0500 | [diff] [blame^] | 36 | fibRemoveNextHop(prefixToDeRegister, 0, bind(onSuccess), onFail); |
Alexander Afanasyev | 884280c | 2014-03-07 09:40:39 +0000 | [diff] [blame] | 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, |
Obaid | 6e7f5f1 | 2014-03-11 14:46:10 -0500 | [diff] [blame^] | 41 | const FibCommandSucceedCallback& onSuccess, |
| 42 | const FailCallback& onFail) |
Alexander Afanasyev | 884280c | 2014-03-07 09:40:39 +0000 | [diff] [blame] | 43 | { |
| 44 | startFibCommand("add-nexthop", |
| 45 | FibManagementOptions() |
| 46 | .setName(prefix) |
| 47 | .setFaceId(faceId) |
| 48 | .setCost(cost), |
Obaid | 6e7f5f1 | 2014-03-11 14:46:10 -0500 | [diff] [blame^] | 49 | onSuccess, onFail); |
Alexander Afanasyev | 884280c | 2014-03-07 09:40:39 +0000 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | void |
| 53 | Controller::fibRemoveNextHop(const Name& prefix, uint64_t faceId, |
Obaid | 6e7f5f1 | 2014-03-11 14:46:10 -0500 | [diff] [blame^] | 54 | const FibCommandSucceedCallback& onSuccess, |
| 55 | const FailCallback& onFail) |
Alexander Afanasyev | 884280c | 2014-03-07 09:40:39 +0000 | [diff] [blame] | 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), |
Obaid | 6e7f5f1 | 2014-03-11 14:46:10 -0500 | [diff] [blame^] | 61 | onSuccess, onFail); |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 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 | { |
Obaid | 6e7f5f1 | 2014-03-11 14:46:10 -0500 | [diff] [blame^] | 89 | /// \todo Add validation of incoming Data |
| 90 | |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 91 | try |
| 92 | { |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 93 | ControlResponse response(data.getContent().blockFromValue()); |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 94 | if (response.getCode() != 200) |
| 95 | return onFail(response.getText()); |
| 96 | |
| 97 | FibManagementOptions options(response.getBody()); |
| 98 | return onSuccess(options); |
| 99 | } |
| 100 | catch(ndn::Tlv::Error& e) |
| 101 | { |
| 102 | if (static_cast<bool>(onFail)) |
| 103 | return onFail(e.what()); |
| 104 | } |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 105 | } |
| 106 | |
hilata | a99e37e | 2014-02-15 23:52:46 -0600 | [diff] [blame] | 107 | void |
| 108 | Controller::startFaceCommand(const std::string& command, |
| 109 | const FaceManagementOptions& options, |
| 110 | const FaceCommandSucceedCallback& onSuccess, |
| 111 | const FailCallback& onFail) |
| 112 | { |
| 113 | Name faceCommandInterestName("/localhost/nfd/faces"); |
| 114 | faceCommandInterestName |
| 115 | .append(command) |
| 116 | .append(options.wireEncode()); |
| 117 | |
| 118 | Interest faceCommandInterest(faceCommandInterestName); |
Alexander Afanasyev | 884280c | 2014-03-07 09:40:39 +0000 | [diff] [blame] | 119 | m_commandInterestGenerator.generate(faceCommandInterest); |
hilata | a99e37e | 2014-02-15 23:52:46 -0600 | [diff] [blame] | 120 | |
| 121 | m_face.expressInterest(faceCommandInterest, |
| 122 | bind(&Controller::processFaceCommandResponse, this, _2, |
| 123 | onSuccess, onFail), |
| 124 | bind(onFail, "Command Interest timed out")); |
| 125 | } |
| 126 | |
| 127 | void |
| 128 | Controller::processFaceCommandResponse(Data& data, |
| 129 | const FaceCommandSucceedCallback& onSuccess, |
| 130 | const FailCallback& onFail) |
| 131 | { |
Obaid | 6e7f5f1 | 2014-03-11 14:46:10 -0500 | [diff] [blame^] | 132 | /// \todo Add validation of incoming Data |
| 133 | |
hilata | a99e37e | 2014-02-15 23:52:46 -0600 | [diff] [blame] | 134 | try |
| 135 | { |
| 136 | ControlResponse response(data.getContent().blockFromValue()); |
| 137 | if (response.getCode() != 200) |
| 138 | return onFail(response.getText()); |
| 139 | |
| 140 | FaceManagementOptions options(response.getBody()); |
| 141 | return onSuccess(options); |
| 142 | } |
| 143 | catch(ndn::Tlv::Error& e) |
| 144 | { |
| 145 | if (static_cast<bool>(onFail)) |
| 146 | return onFail(e.what()); |
| 147 | } |
| 148 | } |
Alexander Afanasyev | 884280c | 2014-03-07 09:40:39 +0000 | [diff] [blame] | 149 | |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 150 | } // namespace nfd |
| 151 | } // namespace ndn |