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