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" |
| 25 | |
| 26 | #include <ndn-cxx/face.hpp> |
| 27 | #include <ndn-cxx/management/nfd-control-response.hpp> |
| 28 | #include <ndn-cxx/util/regex.hpp> |
| 29 | |
| 30 | namespace nlsr { |
| 31 | |
| 32 | INIT_LOGGER("LsdbDatasetInterestHandler"); |
| 33 | |
| 34 | LsdbDatasetInterestHandler::LsdbDatasetInterestHandler(Lsdb& lsdb, |
| 35 | ndn::Face& face, |
| 36 | const ndn::Name& routerName, |
| 37 | ndn::KeyChain& keyChain) |
| 38 | : COMMAND_PREFIX(ndn::Name(routerName).append(Lsdb::NAME_COMPONENT)) |
| 39 | , m_face(face) |
| 40 | , m_keyChain(keyChain) |
| 41 | , m_adjacencyLsaPublisher(lsdb, face, COMMAND_PREFIX, keyChain) |
| 42 | , m_coordinateLsaPublisher(lsdb, face, COMMAND_PREFIX, keyChain) |
| 43 | , m_nameLsaPublisher(lsdb, face, COMMAND_PREFIX, keyChain) |
| 44 | , m_lsdbStatusPublisher(lsdb, face, COMMAND_PREFIX, keyChain, |
| 45 | m_adjacencyLsaPublisher, |
| 46 | m_coordinateLsaPublisher, |
| 47 | m_nameLsaPublisher) |
| 48 | |
| 49 | { |
| 50 | _LOG_DEBUG("Setting interest filter for: " << COMMAND_PREFIX); |
| 51 | m_face.setInterestFilter(COMMAND_PREFIX, |
| 52 | std::bind(&LsdbDatasetInterestHandler::onInterest, this, _2)); |
| 53 | } |
| 54 | |
| 55 | void |
| 56 | LsdbDatasetInterestHandler::onInterest(const ndn::Interest& interest) |
| 57 | { |
| 58 | // Does interest match command prefix with one additional component? |
| 59 | if (interest.getName().size() != COMMAND_PREFIX.size() + 1 || |
| 60 | !COMMAND_PREFIX.isPrefixOf(interest.getName())) |
| 61 | { |
| 62 | _LOG_DEBUG("Received malformed interest: " << interest.getName()); |
| 63 | |
| 64 | sendErrorResponse(interest.getName(), 400, "Malformed command"); |
| 65 | return; |
| 66 | } |
| 67 | |
| 68 | ndn::Name::Component command = interest.getName().get(COMMAND_PREFIX.size()); |
| 69 | _LOG_TRACE("Received interest with command: " << command); |
| 70 | |
| 71 | if (command.equals(AdjacencyLsaPublisher::DATASET_COMPONENT)) { |
| 72 | m_adjacencyLsaPublisher.publish(); |
| 73 | } |
| 74 | else if (command.equals(CoordinateLsaPublisher::DATASET_COMPONENT)) { |
| 75 | m_coordinateLsaPublisher.publish(); |
| 76 | } |
| 77 | else if (command.equals(NameLsaPublisher::DATASET_COMPONENT)) { |
| 78 | m_nameLsaPublisher.publish(); |
| 79 | } |
| 80 | else if (command.equals(LsdbStatusPublisher::DATASET_COMPONENT)) { |
| 81 | m_lsdbStatusPublisher.publish(); |
| 82 | } |
| 83 | else { |
| 84 | _LOG_DEBUG("Unsupported command: " << command); |
| 85 | sendErrorResponse(interest.getName(), 501, "Unsupported command"); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | void |
| 90 | LsdbDatasetInterestHandler::sendErrorResponse(const ndn::Name& name, |
| 91 | uint32_t code, |
| 92 | const std::string& error) |
| 93 | { |
| 94 | ndn::nfd::ControlResponse response(code, error); |
| 95 | |
| 96 | std::shared_ptr<ndn::Data> data = std::make_shared<ndn::Data>(name); |
| 97 | data->setContent(response.wireEncode()); |
| 98 | |
| 99 | m_keyChain.sign(*data); |
| 100 | m_face.put(*data); |
| 101 | } |
| 102 | |
| 103 | } // namespace nlsr |