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