Jiewen Tan | a0497d8 | 2015-02-02 21:59:18 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2014-2015, The University of Memphis, |
| 4 | * Regents of the University of California, |
| 5 | * Arizona Board of Regents. |
| 6 | * |
| 7 | * This file is part of NLSR (Named-data Link State Routing). |
| 8 | * See AUTHORS.md for complete list of NLSR authors and contributors. |
| 9 | * |
| 10 | * NLSR is free software: you can redistribute it and/or modify it under the terms |
| 11 | * of the GNU General Public License as published by the Free Software Foundation, |
| 12 | * either version 3 of the License, or (at your option) any later version. |
| 13 | * |
| 14 | * NLSR is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 15 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 16 | * PURPOSE. See the GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License along with |
| 19 | * NLSR, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 20 | **/ |
| 21 | |
| 22 | #include "lsdb-dataset-interest-handler.hpp" |
| 23 | |
| 24 | #include "logger.hpp" |
Vince Lehman | d6bb3fa | 2015-04-24 14:21:39 -0500 | [diff] [blame^] | 25 | #include "nlsr.hpp" |
Jiewen Tan | a0497d8 | 2015-02-02 21:59:18 -0800 | [diff] [blame] | 26 | |
| 27 | #include <ndn-cxx/face.hpp> |
| 28 | #include <ndn-cxx/management/nfd-control-response.hpp> |
| 29 | #include <ndn-cxx/util/regex.hpp> |
| 30 | |
| 31 | namespace nlsr { |
| 32 | |
| 33 | INIT_LOGGER("LsdbDatasetInterestHandler"); |
| 34 | |
Vince Lehman | d6bb3fa | 2015-04-24 14:21:39 -0500 | [diff] [blame^] | 35 | const uint32_t LsdbDatasetInterestHandler::ERROR_CODE_MALFORMED_COMMAND = 400; |
| 36 | const uint32_t LsdbDatasetInterestHandler::ERROR_CODE_UNSUPPORTED_COMMAND = 501; |
| 37 | |
Jiewen Tan | a0497d8 | 2015-02-02 21:59:18 -0800 | [diff] [blame] | 38 | LsdbDatasetInterestHandler::LsdbDatasetInterestHandler(Lsdb& lsdb, |
| 39 | ndn::Face& face, |
| 40 | const ndn::Name& routerName, |
| 41 | ndn::KeyChain& keyChain) |
Vince Lehman | d6bb3fa | 2015-04-24 14:21:39 -0500 | [diff] [blame^] | 42 | : LOCALHOST_COMMAND_PREFIX(ndn::Name(Nlsr::LOCALHOST_PREFIX).append(Lsdb::NAME_COMPONENT)) |
| 43 | , ROUTER_NAME_COMMAND_PREFIX(ndn::Name(routerName).append(Lsdb::NAME_COMPONENT)) |
Jiewen Tan | a0497d8 | 2015-02-02 21:59:18 -0800 | [diff] [blame] | 44 | , m_face(face) |
| 45 | , m_keyChain(keyChain) |
Vince Lehman | d6bb3fa | 2015-04-24 14:21:39 -0500 | [diff] [blame^] | 46 | , m_adjacencyLsaPublisher(lsdb, face, keyChain) |
| 47 | , m_coordinateLsaPublisher(lsdb, face, keyChain) |
| 48 | , m_nameLsaPublisher(lsdb, face, keyChain) |
| 49 | , m_lsdbStatusPublisher(lsdb, face, keyChain, |
Jiewen Tan | a0497d8 | 2015-02-02 21:59:18 -0800 | [diff] [blame] | 50 | m_adjacencyLsaPublisher, |
| 51 | m_coordinateLsaPublisher, |
| 52 | m_nameLsaPublisher) |
| 53 | |
| 54 | { |
Jiewen Tan | a0497d8 | 2015-02-02 21:59:18 -0800 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | void |
Vince Lehman | d6bb3fa | 2015-04-24 14:21:39 -0500 | [diff] [blame^] | 58 | LsdbDatasetInterestHandler::startListeningOnLocalhost() |
Jiewen Tan | a0497d8 | 2015-02-02 21:59:18 -0800 | [diff] [blame] | 59 | { |
Vince Lehman | d6bb3fa | 2015-04-24 14:21:39 -0500 | [diff] [blame^] | 60 | _LOG_DEBUG("Setting interest filter for: " << LOCALHOST_COMMAND_PREFIX); |
| 61 | m_face.setInterestFilter(LOCALHOST_COMMAND_PREFIX, |
| 62 | std::bind(&LsdbDatasetInterestHandler::onInterest, this, _2, |
| 63 | std::cref(LOCALHOST_COMMAND_PREFIX))); |
| 64 | } |
| 65 | |
| 66 | void |
| 67 | LsdbDatasetInterestHandler::startListeningOnRouterPrefix() |
| 68 | { |
| 69 | _LOG_DEBUG("Setting interest filter for: " << ROUTER_NAME_COMMAND_PREFIX); |
| 70 | m_face.setInterestFilter(ROUTER_NAME_COMMAND_PREFIX, |
| 71 | std::bind(&LsdbDatasetInterestHandler::onInterest, this, _2, |
| 72 | std::cref(ROUTER_NAME_COMMAND_PREFIX))); |
| 73 | } |
| 74 | |
| 75 | void |
| 76 | LsdbDatasetInterestHandler::onInterest(const ndn::Interest& interest, |
| 77 | const ndn::Name& commandPrefix) |
| 78 | { |
| 79 | if (!isValidCommandPrefix(interest, commandPrefix)) |
Jiewen Tan | a0497d8 | 2015-02-02 21:59:18 -0800 | [diff] [blame] | 80 | { |
| 81 | _LOG_DEBUG("Received malformed interest: " << interest.getName()); |
| 82 | |
Vince Lehman | d6bb3fa | 2015-04-24 14:21:39 -0500 | [diff] [blame^] | 83 | sendErrorResponse(interest.getName(), ERROR_CODE_MALFORMED_COMMAND, "Malformed command"); |
Jiewen Tan | a0497d8 | 2015-02-02 21:59:18 -0800 | [diff] [blame] | 84 | return; |
| 85 | } |
| 86 | |
Vince Lehman | d6bb3fa | 2015-04-24 14:21:39 -0500 | [diff] [blame^] | 87 | ndn::Name::Component command = interest.getName().get(commandPrefix.size()); |
| 88 | processCommand(interest, command); |
| 89 | } |
| 90 | |
| 91 | bool |
| 92 | LsdbDatasetInterestHandler::isValidCommandPrefix(const ndn::Interest& interest, |
| 93 | const ndn::Name& commandPrefix) |
| 94 | { |
| 95 | size_t commandSize = interest.getName().size(); |
| 96 | |
| 97 | // Does the Interest match the command prefix with one additional component? |
| 98 | return (commandSize == commandPrefix.size() + 1 && commandPrefix.isPrefixOf(interest.getName())); |
| 99 | } |
| 100 | |
| 101 | void |
| 102 | LsdbDatasetInterestHandler::processCommand(const ndn::Interest& interest, |
| 103 | const ndn::Name::Component& command) |
| 104 | { |
Jiewen Tan | a0497d8 | 2015-02-02 21:59:18 -0800 | [diff] [blame] | 105 | _LOG_TRACE("Received interest with command: " << command); |
| 106 | |
| 107 | if (command.equals(AdjacencyLsaPublisher::DATASET_COMPONENT)) { |
Vince Lehman | d6bb3fa | 2015-04-24 14:21:39 -0500 | [diff] [blame^] | 108 | m_adjacencyLsaPublisher.publish(interest.getName()); |
Jiewen Tan | a0497d8 | 2015-02-02 21:59:18 -0800 | [diff] [blame] | 109 | } |
| 110 | else if (command.equals(CoordinateLsaPublisher::DATASET_COMPONENT)) { |
Vince Lehman | d6bb3fa | 2015-04-24 14:21:39 -0500 | [diff] [blame^] | 111 | m_coordinateLsaPublisher.publish(interest.getName()); |
Jiewen Tan | a0497d8 | 2015-02-02 21:59:18 -0800 | [diff] [blame] | 112 | } |
| 113 | else if (command.equals(NameLsaPublisher::DATASET_COMPONENT)) { |
Vince Lehman | d6bb3fa | 2015-04-24 14:21:39 -0500 | [diff] [blame^] | 114 | m_nameLsaPublisher.publish(interest.getName()); |
Jiewen Tan | a0497d8 | 2015-02-02 21:59:18 -0800 | [diff] [blame] | 115 | } |
| 116 | else if (command.equals(LsdbStatusPublisher::DATASET_COMPONENT)) { |
Vince Lehman | d6bb3fa | 2015-04-24 14:21:39 -0500 | [diff] [blame^] | 117 | m_lsdbStatusPublisher.publish(interest.getName()); |
Jiewen Tan | a0497d8 | 2015-02-02 21:59:18 -0800 | [diff] [blame] | 118 | } |
| 119 | else { |
| 120 | _LOG_DEBUG("Unsupported command: " << command); |
Vince Lehman | d6bb3fa | 2015-04-24 14:21:39 -0500 | [diff] [blame^] | 121 | sendErrorResponse(interest.getName(), ERROR_CODE_UNSUPPORTED_COMMAND, "Unsupported command"); |
Jiewen Tan | a0497d8 | 2015-02-02 21:59:18 -0800 | [diff] [blame] | 122 | } |
| 123 | } |
| 124 | |
| 125 | void |
| 126 | LsdbDatasetInterestHandler::sendErrorResponse(const ndn::Name& name, |
| 127 | uint32_t code, |
| 128 | const std::string& error) |
| 129 | { |
| 130 | ndn::nfd::ControlResponse response(code, error); |
| 131 | |
| 132 | std::shared_ptr<ndn::Data> data = std::make_shared<ndn::Data>(name); |
| 133 | data->setContent(response.wireEncode()); |
| 134 | |
| 135 | m_keyChain.sign(*data); |
| 136 | m_face.put(*data); |
| 137 | } |
| 138 | |
| 139 | } // namespace nlsr |