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