Yanbiao Li | 6704a4a | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Yanbiao Li | df846e5 | 2016-01-30 21:53:47 -0800 | [diff] [blame] | 3 | * Copyright (c) 2014-2016, Regents of the University of California, |
Yanbiao Li | 6704a4a | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 4 | * Arizona Board of Regents, |
| 5 | * Colorado State University, |
| 6 | * University Pierre & Marie Curie, Sorbonne University, |
| 7 | * Washington University in St. Louis, |
| 8 | * Beijing Institute of Technology, |
| 9 | * The University of Memphis. |
| 10 | * |
| 11 | * This file is part of NFD (Named Data Networking Forwarding Daemon). |
| 12 | * See AUTHORS.md for complete list of NFD authors and contributors. |
| 13 | * |
| 14 | * NFD is free software: you can redistribute it and/or modify it under the terms |
| 15 | * of the GNU General Public License as published by the Free Software Foundation, |
| 16 | * either version 3 of the License, or (at your option) any later version. |
| 17 | * |
| 18 | * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 19 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 20 | * PURPOSE. See the GNU General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU General Public License along with |
| 23 | * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 24 | */ |
| 25 | |
| 26 | #include "strategy-choice-manager.hpp" |
| 27 | #include "table/strategy-choice.hpp" |
| 28 | #include <ndn-cxx/management/nfd-strategy-choice.hpp> |
| 29 | |
| 30 | namespace nfd { |
| 31 | |
| 32 | NFD_LOG_INIT("StrategyChoiceManager"); |
| 33 | |
| 34 | StrategyChoiceManager::StrategyChoiceManager(StrategyChoice& strategyChoice, |
| 35 | Dispatcher& dispatcher, |
Junxiao Shi | 9ddf1b5 | 2016-08-22 03:58:55 +0000 | [diff] [blame] | 36 | CommandAuthenticator& authenticator) |
| 37 | : NfdManagerBase(dispatcher, authenticator, "strategy-choice") |
Junxiao Shi | ff10da6 | 2016-07-13 17:57:43 +0000 | [diff] [blame] | 38 | , m_table(strategyChoice) |
Yanbiao Li | 6704a4a | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 39 | { |
| 40 | registerCommandHandler<ndn::nfd::StrategyChoiceSetCommand>("set", |
| 41 | bind(&StrategyChoiceManager::setStrategy, this, _2, _3, _4, _5)); |
| 42 | registerCommandHandler<ndn::nfd::StrategyChoiceUnsetCommand>("unset", |
| 43 | bind(&StrategyChoiceManager::unsetStrategy, this, _2, _3, _4, _5)); |
| 44 | |
| 45 | registerStatusDatasetHandler("list", |
| 46 | bind(&StrategyChoiceManager::listChoices, this, _1, _2, _3)); |
| 47 | } |
| 48 | |
| 49 | void |
| 50 | StrategyChoiceManager::setStrategy(const Name& topPrefix, const Interest& interest, |
| 51 | ControlParameters parameters, |
| 52 | const ndn::mgmt::CommandContinuation& done) |
| 53 | { |
| 54 | const Name& prefix = parameters.getName(); |
| 55 | const Name& selectedStrategy = parameters.getStrategy(); |
| 56 | |
Junxiao Shi | ff10da6 | 2016-07-13 17:57:43 +0000 | [diff] [blame] | 57 | if (!m_table.hasStrategy(selectedStrategy)) { |
Yanbiao Li | 6704a4a | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 58 | NFD_LOG_DEBUG("strategy-choice result: FAIL reason: unknown-strategy: " |
| 59 | << parameters.getStrategy()); |
| 60 | return done(ControlResponse(504, "Unsupported strategy")); |
| 61 | } |
| 62 | |
Junxiao Shi | ff10da6 | 2016-07-13 17:57:43 +0000 | [diff] [blame] | 63 | if (m_table.insert(prefix, selectedStrategy)) { |
Yanbiao Li | 6704a4a | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 64 | NFD_LOG_DEBUG("strategy-choice result: SUCCESS"); |
Junxiao Shi | ff10da6 | 2016-07-13 17:57:43 +0000 | [diff] [blame] | 65 | auto currentStrategyChoice = m_table.get(prefix); |
Yanbiao Li | 6704a4a | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 66 | BOOST_ASSERT(currentStrategyChoice.first); |
| 67 | parameters.setStrategy(currentStrategyChoice.second); |
| 68 | return done(ControlResponse(200, "OK").setBody(parameters.wireEncode())); |
| 69 | } |
| 70 | else { |
| 71 | NFD_LOG_DEBUG("strategy-choice result: FAIL reason: not-installed"); |
| 72 | return done(ControlResponse(405, "Strategy not installed")); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | void |
| 77 | StrategyChoiceManager::unsetStrategy(const Name& topPrefix, const Interest& interest, |
| 78 | ControlParameters parameters, |
| 79 | const ndn::mgmt::CommandContinuation& done) |
| 80 | { |
Junxiao Shi | ff10da6 | 2016-07-13 17:57:43 +0000 | [diff] [blame] | 81 | m_table.erase(parameters.getName()); |
Yanbiao Li | 6704a4a | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 82 | |
| 83 | NFD_LOG_DEBUG("strategy-choice result: SUCCESS"); |
| 84 | done(ControlResponse(200, "OK").setBody(parameters.wireEncode())); |
| 85 | } |
| 86 | |
| 87 | void |
| 88 | StrategyChoiceManager::listChoices(const Name& topPrefix, const Interest& interest, |
| 89 | ndn::mgmt::StatusDatasetContext& context) |
| 90 | { |
Junxiao Shi | ff10da6 | 2016-07-13 17:57:43 +0000 | [diff] [blame] | 91 | for (auto&& i : m_table) { |
Yanbiao Li | 6704a4a | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 92 | ndn::nfd::StrategyChoice entry; |
| 93 | entry.setName(i.getPrefix()).setStrategy(i.getStrategyName()); |
| 94 | context.append(entry.wireEncode()); |
| 95 | } |
| 96 | context.end(); |
| 97 | } |
| 98 | |
Yanbiao Li | df846e5 | 2016-01-30 21:53:47 -0800 | [diff] [blame] | 99 | } // namespace nfd |