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