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