Alexander Afanasyev | efe3ab2 | 2014-02-19 14:57:50 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /** |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame^] | 3 | * Copyright (c) 2013-2014, Regents of the University of California. |
| 4 | * All rights reserved. |
| 5 | * |
| 6 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
| 7 | * See AUTHORS.md for complete list of ndn-cxx authors and contributors. |
| 8 | * |
| 9 | * This file licensed under New BSD License. See COPYING for detailed information about |
| 10 | * ndn-cxx library copyright, permissions, and redistribution restrictions. |
Alexander Afanasyev | efe3ab2 | 2014-02-19 14:57:50 -0800 | [diff] [blame] | 11 | */ |
| 12 | |
Alexander Afanasyev | efe3ab2 | 2014-02-19 14:57:50 -0800 | [diff] [blame] | 13 | #include "nrd-controller.hpp" |
| 14 | #include "nrd-prefix-reg-options.hpp" |
Junxiao Shi | 5f6c74f | 2014-04-18 16:29:44 -0700 | [diff] [blame] | 15 | #include "nfd-control-response.hpp" // used in deprecated function only |
Alexander Afanasyev | efe3ab2 | 2014-02-19 14:57:50 -0800 | [diff] [blame] | 16 | |
| 17 | namespace ndn { |
| 18 | namespace nrd { |
| 19 | |
Junxiao Shi | 5f6c74f | 2014-04-18 16:29:44 -0700 | [diff] [blame] | 20 | using nfd::ControlParameters; |
| 21 | using nfd::RibRegisterCommand; |
| 22 | using nfd::RibUnregisterCommand; |
| 23 | |
Alexander Afanasyev | efe3ab2 | 2014-02-19 14:57:50 -0800 | [diff] [blame] | 24 | Controller::Controller(Face& face) |
Junxiao Shi | 5f6c74f | 2014-04-18 16:29:44 -0700 | [diff] [blame] | 25 | : nfd::Controller(face) |
Alexander Afanasyev | efe3ab2 | 2014-02-19 14:57:50 -0800 | [diff] [blame] | 26 | { |
| 27 | } |
| 28 | |
| 29 | void |
| 30 | Controller::selfRegisterPrefix(const Name& prefixToRegister, |
| 31 | const SuccessCallback& onSuccess, |
| 32 | const FailCallback& onFail) |
| 33 | { |
Junxiao Shi | 5f6c74f | 2014-04-18 16:29:44 -0700 | [diff] [blame] | 34 | ControlParameters parameters; |
| 35 | parameters.setName(prefixToRegister); |
| 36 | |
| 37 | this->start<RibRegisterCommand>(parameters, |
| 38 | bind(onSuccess), |
| 39 | bind(onFail, _2)); |
Alexander Afanasyev | efe3ab2 | 2014-02-19 14:57:50 -0800 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | void |
| 43 | Controller::selfDeregisterPrefix(const Name& prefixToRegister, |
| 44 | const SuccessCallback& onSuccess, |
| 45 | const FailCallback& onFail) |
| 46 | { |
Junxiao Shi | 5f6c74f | 2014-04-18 16:29:44 -0700 | [diff] [blame] | 47 | ControlParameters parameters; |
| 48 | parameters.setName(prefixToRegister); |
| 49 | |
| 50 | this->start<RibUnregisterCommand>(parameters, |
| 51 | bind(onSuccess), |
| 52 | bind(onFail, _2)); |
Alexander Afanasyev | efe3ab2 | 2014-02-19 14:57:50 -0800 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | void |
Obaid | 6e7f5f1 | 2014-03-11 14:46:10 -0500 | [diff] [blame] | 56 | Controller::registerPrefix(const PrefixRegOptions& options, |
| 57 | const CommandSucceedCallback& onSuccess, |
| 58 | const FailCallback& onFail) |
| 59 | { |
| 60 | startCommand("register", options, onSuccess, onFail); |
| 61 | } |
| 62 | |
| 63 | void |
| 64 | Controller::unregisterPrefix(const PrefixRegOptions& options, |
| 65 | const CommandSucceedCallback& onSuccess, |
| 66 | const FailCallback& onFail) |
| 67 | { |
| 68 | startCommand("unregister", options, onSuccess, onFail); |
| 69 | } |
| 70 | |
| 71 | void |
| 72 | Controller::advertisePrefix(const PrefixRegOptions& options, |
| 73 | const CommandSucceedCallback& onSuccess, |
| 74 | const FailCallback& onFail) |
| 75 | { |
| 76 | startCommand("advertise", options, onSuccess, onFail); |
| 77 | } |
| 78 | |
| 79 | void |
| 80 | Controller::withdrawPrefix(const PrefixRegOptions& options, |
| 81 | const CommandSucceedCallback& onSuccess, |
| 82 | const FailCallback& onFail) |
| 83 | { |
| 84 | startCommand("withdraw", options, onSuccess, onFail); |
| 85 | } |
| 86 | |
| 87 | void |
Alexander Afanasyev | efe3ab2 | 2014-02-19 14:57:50 -0800 | [diff] [blame] | 88 | Controller::startCommand(const std::string& command, |
| 89 | const PrefixRegOptions& options, |
| 90 | const CommandSucceedCallback& onSuccess, |
| 91 | const FailCallback& onFail) |
| 92 | { |
| 93 | Name commandInterestName("/localhost/nrd"); |
| 94 | commandInterestName |
| 95 | .append(command) |
| 96 | .append(options.wireEncode()); |
| 97 | |
| 98 | Interest commandInterest(commandInterestName); |
Obaid | 6e7f5f1 | 2014-03-11 14:46:10 -0500 | [diff] [blame] | 99 | m_commandInterestGenerator.generate(commandInterest); |
Alexander Afanasyev | efe3ab2 | 2014-02-19 14:57:50 -0800 | [diff] [blame] | 100 | |
| 101 | m_face.expressInterest(commandInterest, |
| 102 | bind(&Controller::processCommandResponse, this, _2, |
| 103 | onSuccess, onFail), |
| 104 | bind(onFail, "Command Interest timed out")); |
| 105 | } |
| 106 | |
| 107 | void |
Alexander Afanasyev | efe3ab2 | 2014-02-19 14:57:50 -0800 | [diff] [blame] | 108 | Controller::processCommandResponse(Data& data, |
| 109 | const CommandSucceedCallback& onSuccess, |
| 110 | const FailCallback& onFail) |
| 111 | { |
Obaid | 6e7f5f1 | 2014-03-11 14:46:10 -0500 | [diff] [blame] | 112 | /// \todo Add validation of incoming Data |
| 113 | |
Alexander Afanasyev | efe3ab2 | 2014-02-19 14:57:50 -0800 | [diff] [blame] | 114 | try |
| 115 | { |
| 116 | nfd::ControlResponse response(data.getContent().blockFromValue()); |
| 117 | if (response.getCode() != 200) |
| 118 | return onFail(response.getText()); |
| 119 | |
| 120 | PrefixRegOptions options(response.getBody()); |
| 121 | return onSuccess(options); |
| 122 | } |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 123 | catch (ndn::Tlv::Error& e) |
Alexander Afanasyev | efe3ab2 | 2014-02-19 14:57:50 -0800 | [diff] [blame] | 124 | { |
| 125 | if (static_cast<bool>(onFail)) |
| 126 | return onFail(e.what()); |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | } // namespace nrd |
| 131 | } // namespace ndn |