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" |
| 8 | #include "../node.hpp" |
| 9 | |
| 10 | #include "nfd-controller.hpp" |
| 11 | #include "nfd-fib-management-options.hpp" |
| 12 | #include "nfd-control-response.hpp" |
| 13 | |
| 14 | namespace ndn { |
| 15 | namespace nfd { |
| 16 | |
| 17 | Controller::Controller(Node& face) |
| 18 | : m_face(face) |
| 19 | , m_faceId(0) |
| 20 | { |
| 21 | } |
| 22 | |
| 23 | void |
| 24 | Controller::selfRegisterPrefix(const Name& prefixToRegister, |
| 25 | const SuccessCallback& onSuccess, |
| 26 | const FailCallback& onFail) |
| 27 | { |
| 28 | startFibCommand("add-nexthop", |
| 29 | FibManagementOptions() |
| 30 | .setName(prefixToRegister) |
| 31 | .setFaceId(0) // self-registration |
| 32 | .setCost(0), |
| 33 | bind(&Controller::recordSelfRegisteredFaceId, this, _1, onSuccess), |
| 34 | onFail); |
| 35 | } |
| 36 | |
| 37 | void |
| 38 | Controller::selfDeregisterPrefix(const Name& prefixToRegister, |
| 39 | const SuccessCallback& onSuccess, |
| 40 | const FailCallback& onFail) |
| 41 | { |
| 42 | if (m_faceId == 0) |
| 43 | { |
| 44 | if (static_cast<bool>(onFail)) |
| 45 | onFail("Face ID is not set, should have been set after a successful prefix registration command"); |
| 46 | return; |
| 47 | } |
| 48 | |
| 49 | startFibCommand("remove-nexthop", |
| 50 | FibManagementOptions() |
| 51 | .setName(prefixToRegister) |
| 52 | .setFaceId(m_faceId), |
| 53 | bind(onSuccess), onFail); |
| 54 | } |
| 55 | |
| 56 | void |
| 57 | Controller::startFibCommand(const std::string& command, |
| 58 | const FibManagementOptions& options, |
| 59 | const FibCommandSucceedCallback& onSuccess, |
| 60 | const FailCallback& onFail) |
| 61 | { |
| 62 | Name fibCommandInterestName("/localhost/nfd/fib"); |
| 63 | fibCommandInterestName |
| 64 | .append(command) |
| 65 | .append(options.wireEncode()); |
| 66 | |
| 67 | Interest fibCommandInterest(fibCommandInterestName); |
| 68 | m_keyChain.sign(fibCommandInterest); |
| 69 | |
| 70 | m_face.expressInterest(fibCommandInterest, |
| 71 | bind(&Controller::processFibCommandResponse, this, _2, |
| 72 | onSuccess, onFail), |
| 73 | bind(onFail, "Command Interest timed out")); |
| 74 | } |
| 75 | |
| 76 | void |
| 77 | Controller::recordSelfRegisteredFaceId(const FibManagementOptions& entry, |
| 78 | const SuccessCallback& onSuccess) |
| 79 | { |
| 80 | m_faceId = entry.getFaceId(); |
| 81 | onSuccess(); |
| 82 | } |
| 83 | |
| 84 | // void |
| 85 | // processFaceActionResponse(const shared_ptr<Data>& data, |
| 86 | // const FaceOperationSucceedCallback& onSuccess, |
| 87 | // const FailCallback& onFail); |
| 88 | |
| 89 | void |
| 90 | Controller::processFibCommandResponse(const shared_ptr<Data>& data, |
| 91 | const FibCommandSucceedCallback& onSuccess, |
| 92 | const FailCallback& onFail) |
| 93 | { |
| 94 | try |
| 95 | { |
| 96 | ControlResponse response(data->getContent().blockFromValue()); |
| 97 | |
| 98 | if (response.getCode() != 200) |
| 99 | return onFail(response.getText()); |
| 100 | |
| 101 | FibManagementOptions options(response.getBody()); |
| 102 | return onSuccess(options); |
| 103 | } |
| 104 | catch(ndn::Tlv::Error& e) |
| 105 | { |
| 106 | if (static_cast<bool>(onFail)) |
| 107 | return onFail(e.what()); |
| 108 | } |
| 109 | catch(ControlResponse::Error& e) |
| 110 | { |
| 111 | if (static_cast<bool>(onFail)) |
| 112 | return onFail(e.what()); |
| 113 | } |
| 114 | catch(FibManagementOptions::Error& e) |
| 115 | { |
| 116 | if (static_cast<bool>(onFail)) |
| 117 | return onFail(e.what()); |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | } // namespace nfd |
| 122 | } // namespace ndn |