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, |
Jiewen Tan | a0497d8 | 2015-02-02 21:59:18 -0800 | [diff] [blame] | 40 | ndn::KeyChain& keyChain) |
Vince Lehman | d6bb3fa | 2015-04-24 14:21:39 -0500 | [diff] [blame] | 41 | : LOCALHOST_COMMAND_PREFIX(ndn::Name(Nlsr::LOCALHOST_PREFIX).append(Lsdb::NAME_COMPONENT)) |
Jiewen Tan | a0497d8 | 2015-02-02 21:59:18 -0800 | [diff] [blame] | 42 | , m_face(face) |
| 43 | , m_keyChain(keyChain) |
Vince Lehman | d6bb3fa | 2015-04-24 14:21:39 -0500 | [diff] [blame] | 44 | , m_adjacencyLsaPublisher(lsdb, face, keyChain) |
| 45 | , m_coordinateLsaPublisher(lsdb, face, keyChain) |
| 46 | , m_nameLsaPublisher(lsdb, face, keyChain) |
| 47 | , m_lsdbStatusPublisher(lsdb, face, keyChain, |
Jiewen Tan | a0497d8 | 2015-02-02 21:59:18 -0800 | [diff] [blame] | 48 | m_adjacencyLsaPublisher, |
| 49 | m_coordinateLsaPublisher, |
| 50 | m_nameLsaPublisher) |
| 51 | |
| 52 | { |
Jiewen Tan | a0497d8 | 2015-02-02 21:59:18 -0800 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | void |
Vince Lehman | d6bb3fa | 2015-04-24 14:21:39 -0500 | [diff] [blame] | 56 | LsdbDatasetInterestHandler::startListeningOnLocalhost() |
Jiewen Tan | a0497d8 | 2015-02-02 21:59:18 -0800 | [diff] [blame] | 57 | { |
Vince Lehman | d6bb3fa | 2015-04-24 14:21:39 -0500 | [diff] [blame] | 58 | _LOG_DEBUG("Setting interest filter for: " << LOCALHOST_COMMAND_PREFIX); |
| 59 | m_face.setInterestFilter(LOCALHOST_COMMAND_PREFIX, |
| 60 | std::bind(&LsdbDatasetInterestHandler::onInterest, this, _2, |
| 61 | std::cref(LOCALHOST_COMMAND_PREFIX))); |
| 62 | } |
| 63 | |
| 64 | void |
| 65 | LsdbDatasetInterestHandler::startListeningOnRouterPrefix() |
| 66 | { |
Muktadir R Chowdhury | 3ac0728 | 2016-06-17 16:30:29 -0500 | [diff] [blame] | 67 | _LOG_DEBUG("Setting interest filter for: " << m_routerNameCommandPrefix); |
| 68 | m_face.setInterestFilter(m_routerNameCommandPrefix, |
Vince Lehman | d6bb3fa | 2015-04-24 14:21:39 -0500 | [diff] [blame] | 69 | std::bind(&LsdbDatasetInterestHandler::onInterest, this, _2, |
Muktadir R Chowdhury | 3ac0728 | 2016-06-17 16:30:29 -0500 | [diff] [blame] | 70 | std::cref(m_routerNameCommandPrefix))); |
Vince Lehman | d6bb3fa | 2015-04-24 14:21:39 -0500 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | void |
| 74 | LsdbDatasetInterestHandler::onInterest(const ndn::Interest& interest, |
| 75 | const ndn::Name& commandPrefix) |
| 76 | { |
| 77 | if (!isValidCommandPrefix(interest, commandPrefix)) |
Jiewen Tan | a0497d8 | 2015-02-02 21:59:18 -0800 | [diff] [blame] | 78 | { |
| 79 | _LOG_DEBUG("Received malformed interest: " << interest.getName()); |
| 80 | |
Vince Lehman | d6bb3fa | 2015-04-24 14:21:39 -0500 | [diff] [blame] | 81 | sendErrorResponse(interest.getName(), ERROR_CODE_MALFORMED_COMMAND, "Malformed command"); |
Jiewen Tan | a0497d8 | 2015-02-02 21:59:18 -0800 | [diff] [blame] | 82 | return; |
| 83 | } |
| 84 | |
Vince Lehman | d6bb3fa | 2015-04-24 14:21:39 -0500 | [diff] [blame] | 85 | ndn::Name::Component command = interest.getName().get(commandPrefix.size()); |
| 86 | processCommand(interest, command); |
| 87 | } |
| 88 | |
| 89 | bool |
| 90 | LsdbDatasetInterestHandler::isValidCommandPrefix(const ndn::Interest& interest, |
| 91 | const ndn::Name& commandPrefix) |
| 92 | { |
| 93 | size_t commandSize = interest.getName().size(); |
| 94 | |
| 95 | // Does the Interest match the command prefix with one additional component? |
| 96 | return (commandSize == commandPrefix.size() + 1 && commandPrefix.isPrefixOf(interest.getName())); |
| 97 | } |
| 98 | |
| 99 | void |
| 100 | LsdbDatasetInterestHandler::processCommand(const ndn::Interest& interest, |
| 101 | const ndn::Name::Component& command) |
| 102 | { |
Jiewen Tan | a0497d8 | 2015-02-02 21:59:18 -0800 | [diff] [blame] | 103 | _LOG_TRACE("Received interest with command: " << command); |
| 104 | |
| 105 | if (command.equals(AdjacencyLsaPublisher::DATASET_COMPONENT)) { |
Vince Lehman | d6bb3fa | 2015-04-24 14:21:39 -0500 | [diff] [blame] | 106 | m_adjacencyLsaPublisher.publish(interest.getName()); |
Jiewen Tan | a0497d8 | 2015-02-02 21:59:18 -0800 | [diff] [blame] | 107 | } |
| 108 | else if (command.equals(CoordinateLsaPublisher::DATASET_COMPONENT)) { |
Vince Lehman | d6bb3fa | 2015-04-24 14:21:39 -0500 | [diff] [blame] | 109 | m_coordinateLsaPublisher.publish(interest.getName()); |
Jiewen Tan | a0497d8 | 2015-02-02 21:59:18 -0800 | [diff] [blame] | 110 | } |
| 111 | else if (command.equals(NameLsaPublisher::DATASET_COMPONENT)) { |
Vince Lehman | d6bb3fa | 2015-04-24 14:21:39 -0500 | [diff] [blame] | 112 | m_nameLsaPublisher.publish(interest.getName()); |
Jiewen Tan | a0497d8 | 2015-02-02 21:59:18 -0800 | [diff] [blame] | 113 | } |
| 114 | else if (command.equals(LsdbStatusPublisher::DATASET_COMPONENT)) { |
Vince Lehman | d6bb3fa | 2015-04-24 14:21:39 -0500 | [diff] [blame] | 115 | m_lsdbStatusPublisher.publish(interest.getName()); |
Jiewen Tan | a0497d8 | 2015-02-02 21:59:18 -0800 | [diff] [blame] | 116 | } |
| 117 | else { |
| 118 | _LOG_DEBUG("Unsupported command: " << command); |
Vince Lehman | d6bb3fa | 2015-04-24 14:21:39 -0500 | [diff] [blame] | 119 | sendErrorResponse(interest.getName(), ERROR_CODE_UNSUPPORTED_COMMAND, "Unsupported command"); |
Jiewen Tan | a0497d8 | 2015-02-02 21:59:18 -0800 | [diff] [blame] | 120 | } |
| 121 | } |
| 122 | |
| 123 | void |
| 124 | LsdbDatasetInterestHandler::sendErrorResponse(const ndn::Name& name, |
| 125 | uint32_t code, |
| 126 | const std::string& error) |
| 127 | { |
| 128 | ndn::nfd::ControlResponse response(code, error); |
| 129 | |
| 130 | std::shared_ptr<ndn::Data> data = std::make_shared<ndn::Data>(name); |
| 131 | data->setContent(response.wireEncode()); |
| 132 | |
| 133 | m_keyChain.sign(*data); |
| 134 | m_face.put(*data); |
| 135 | } |
| 136 | |
| 137 | } // namespace nlsr |