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 | |
Alexander Afanasyev | efe3ab2 | 2014-02-19 14:57:50 -0800 | [diff] [blame] | 7 | #include "nrd-controller.hpp" |
| 8 | #include "nrd-prefix-reg-options.hpp" |
Junxiao Shi | 5f6c74f | 2014-04-18 16:29:44 -0700 | [diff] [blame] | 9 | #include "nfd-control-response.hpp" // used in deprecated function only |
Alexander Afanasyev | efe3ab2 | 2014-02-19 14:57:50 -0800 | [diff] [blame] | 10 | |
| 11 | namespace ndn { |
| 12 | namespace nrd { |
| 13 | |
Junxiao Shi | 5f6c74f | 2014-04-18 16:29:44 -0700 | [diff] [blame] | 14 | using nfd::ControlParameters; |
| 15 | using nfd::RibRegisterCommand; |
| 16 | using nfd::RibUnregisterCommand; |
| 17 | |
Alexander Afanasyev | efe3ab2 | 2014-02-19 14:57:50 -0800 | [diff] [blame] | 18 | Controller::Controller(Face& face) |
Junxiao Shi | 5f6c74f | 2014-04-18 16:29:44 -0700 | [diff] [blame] | 19 | : nfd::Controller(face) |
Alexander Afanasyev | efe3ab2 | 2014-02-19 14:57:50 -0800 | [diff] [blame] | 20 | { |
| 21 | } |
| 22 | |
| 23 | void |
| 24 | Controller::selfRegisterPrefix(const Name& prefixToRegister, |
| 25 | const SuccessCallback& onSuccess, |
| 26 | const FailCallback& onFail) |
| 27 | { |
Junxiao Shi | 5f6c74f | 2014-04-18 16:29:44 -0700 | [diff] [blame] | 28 | ControlParameters parameters; |
| 29 | parameters.setName(prefixToRegister); |
| 30 | |
| 31 | this->start<RibRegisterCommand>(parameters, |
| 32 | bind(onSuccess), |
| 33 | bind(onFail, _2)); |
Alexander Afanasyev | efe3ab2 | 2014-02-19 14:57:50 -0800 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | void |
| 37 | Controller::selfDeregisterPrefix(const Name& prefixToRegister, |
| 38 | const SuccessCallback& onSuccess, |
| 39 | const FailCallback& onFail) |
| 40 | { |
Junxiao Shi | 5f6c74f | 2014-04-18 16:29:44 -0700 | [diff] [blame] | 41 | ControlParameters parameters; |
| 42 | parameters.setName(prefixToRegister); |
| 43 | |
| 44 | this->start<RibUnregisterCommand>(parameters, |
| 45 | bind(onSuccess), |
| 46 | bind(onFail, _2)); |
Alexander Afanasyev | efe3ab2 | 2014-02-19 14:57:50 -0800 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | void |
Obaid | 6e7f5f1 | 2014-03-11 14:46:10 -0500 | [diff] [blame] | 50 | Controller::registerPrefix(const PrefixRegOptions& options, |
| 51 | const CommandSucceedCallback& onSuccess, |
| 52 | const FailCallback& onFail) |
| 53 | { |
| 54 | startCommand("register", options, onSuccess, onFail); |
| 55 | } |
| 56 | |
| 57 | void |
| 58 | Controller::unregisterPrefix(const PrefixRegOptions& options, |
| 59 | const CommandSucceedCallback& onSuccess, |
| 60 | const FailCallback& onFail) |
| 61 | { |
| 62 | startCommand("unregister", options, onSuccess, onFail); |
| 63 | } |
| 64 | |
| 65 | void |
| 66 | Controller::advertisePrefix(const PrefixRegOptions& options, |
| 67 | const CommandSucceedCallback& onSuccess, |
| 68 | const FailCallback& onFail) |
| 69 | { |
| 70 | startCommand("advertise", options, onSuccess, onFail); |
| 71 | } |
| 72 | |
| 73 | void |
| 74 | Controller::withdrawPrefix(const PrefixRegOptions& options, |
| 75 | const CommandSucceedCallback& onSuccess, |
| 76 | const FailCallback& onFail) |
| 77 | { |
| 78 | startCommand("withdraw", options, onSuccess, onFail); |
| 79 | } |
| 80 | |
| 81 | void |
Alexander Afanasyev | efe3ab2 | 2014-02-19 14:57:50 -0800 | [diff] [blame] | 82 | Controller::startCommand(const std::string& command, |
| 83 | const PrefixRegOptions& options, |
| 84 | const CommandSucceedCallback& onSuccess, |
| 85 | const FailCallback& onFail) |
| 86 | { |
| 87 | Name commandInterestName("/localhost/nrd"); |
| 88 | commandInterestName |
| 89 | .append(command) |
| 90 | .append(options.wireEncode()); |
| 91 | |
| 92 | Interest commandInterest(commandInterestName); |
Obaid | 6e7f5f1 | 2014-03-11 14:46:10 -0500 | [diff] [blame] | 93 | m_commandInterestGenerator.generate(commandInterest); |
Alexander Afanasyev | efe3ab2 | 2014-02-19 14:57:50 -0800 | [diff] [blame] | 94 | |
| 95 | m_face.expressInterest(commandInterest, |
| 96 | bind(&Controller::processCommandResponse, this, _2, |
| 97 | onSuccess, onFail), |
| 98 | bind(onFail, "Command Interest timed out")); |
| 99 | } |
| 100 | |
| 101 | void |
Alexander Afanasyev | efe3ab2 | 2014-02-19 14:57:50 -0800 | [diff] [blame] | 102 | Controller::processCommandResponse(Data& data, |
| 103 | const CommandSucceedCallback& onSuccess, |
| 104 | const FailCallback& onFail) |
| 105 | { |
Obaid | 6e7f5f1 | 2014-03-11 14:46:10 -0500 | [diff] [blame] | 106 | /// \todo Add validation of incoming Data |
| 107 | |
Alexander Afanasyev | efe3ab2 | 2014-02-19 14:57:50 -0800 | [diff] [blame] | 108 | try |
| 109 | { |
| 110 | nfd::ControlResponse response(data.getContent().blockFromValue()); |
| 111 | if (response.getCode() != 200) |
| 112 | return onFail(response.getText()); |
| 113 | |
| 114 | PrefixRegOptions options(response.getBody()); |
| 115 | return onSuccess(options); |
| 116 | } |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 117 | catch (ndn::Tlv::Error& e) |
Alexander Afanasyev | efe3ab2 | 2014-02-19 14:57:50 -0800 | [diff] [blame] | 118 | { |
| 119 | if (static_cast<bool>(onFail)) |
| 120 | return onFail(e.what()); |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | } // namespace nrd |
| 125 | } // namespace ndn |