blob: 4ea6454c1f156b96e377b486572b3c22ee7e4725 [file] [log] [blame]
Jiewen Tana0497d82015-02-02 21:59:18 -08001/* -*- 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 Lehmand6bb3fa2015-04-24 14:21:39 -050025#include "nlsr.hpp"
Jiewen Tana0497d82015-02-02 21:59:18 -080026
27#include <ndn-cxx/face.hpp>
28#include <ndn-cxx/management/nfd-control-response.hpp>
29#include <ndn-cxx/util/regex.hpp>
30
31namespace nlsr {
32
33INIT_LOGGER("LsdbDatasetInterestHandler");
34
Vince Lehmand6bb3fa2015-04-24 14:21:39 -050035const uint32_t LsdbDatasetInterestHandler::ERROR_CODE_MALFORMED_COMMAND = 400;
36const uint32_t LsdbDatasetInterestHandler::ERROR_CODE_UNSUPPORTED_COMMAND = 501;
37
Jiewen Tana0497d82015-02-02 21:59:18 -080038LsdbDatasetInterestHandler::LsdbDatasetInterestHandler(Lsdb& lsdb,
39 ndn::Face& face,
Jiewen Tana0497d82015-02-02 21:59:18 -080040 ndn::KeyChain& keyChain)
Vince Lehmand6bb3fa2015-04-24 14:21:39 -050041 : LOCALHOST_COMMAND_PREFIX(ndn::Name(Nlsr::LOCALHOST_PREFIX).append(Lsdb::NAME_COMPONENT))
Jiewen Tana0497d82015-02-02 21:59:18 -080042 , m_face(face)
43 , m_keyChain(keyChain)
Vince Lehmand6bb3fa2015-04-24 14:21:39 -050044 , m_adjacencyLsaPublisher(lsdb, face, keyChain)
45 , m_coordinateLsaPublisher(lsdb, face, keyChain)
46 , m_nameLsaPublisher(lsdb, face, keyChain)
47 , m_lsdbStatusPublisher(lsdb, face, keyChain,
Jiewen Tana0497d82015-02-02 21:59:18 -080048 m_adjacencyLsaPublisher,
49 m_coordinateLsaPublisher,
50 m_nameLsaPublisher)
51
52{
Jiewen Tana0497d82015-02-02 21:59:18 -080053}
54
55void
Vince Lehmand6bb3fa2015-04-24 14:21:39 -050056LsdbDatasetInterestHandler::startListeningOnLocalhost()
Jiewen Tana0497d82015-02-02 21:59:18 -080057{
Vince Lehmand6bb3fa2015-04-24 14:21:39 -050058 _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
64void
65LsdbDatasetInterestHandler::startListeningOnRouterPrefix()
66{
Muktadir R Chowdhury3ac07282016-06-17 16:30:29 -050067 _LOG_DEBUG("Setting interest filter for: " << m_routerNameCommandPrefix);
68 m_face.setInterestFilter(m_routerNameCommandPrefix,
Vince Lehmand6bb3fa2015-04-24 14:21:39 -050069 std::bind(&LsdbDatasetInterestHandler::onInterest, this, _2,
Muktadir R Chowdhury3ac07282016-06-17 16:30:29 -050070 std::cref(m_routerNameCommandPrefix)));
Vince Lehmand6bb3fa2015-04-24 14:21:39 -050071}
72
73void
74LsdbDatasetInterestHandler::onInterest(const ndn::Interest& interest,
75 const ndn::Name& commandPrefix)
76{
77 if (!isValidCommandPrefix(interest, commandPrefix))
Jiewen Tana0497d82015-02-02 21:59:18 -080078 {
79 _LOG_DEBUG("Received malformed interest: " << interest.getName());
80
Vince Lehmand6bb3fa2015-04-24 14:21:39 -050081 sendErrorResponse(interest.getName(), ERROR_CODE_MALFORMED_COMMAND, "Malformed command");
Jiewen Tana0497d82015-02-02 21:59:18 -080082 return;
83 }
84
Vince Lehmand6bb3fa2015-04-24 14:21:39 -050085 ndn::Name::Component command = interest.getName().get(commandPrefix.size());
86 processCommand(interest, command);
87}
88
89bool
90LsdbDatasetInterestHandler::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
99void
100LsdbDatasetInterestHandler::processCommand(const ndn::Interest& interest,
101 const ndn::Name::Component& command)
102{
Jiewen Tana0497d82015-02-02 21:59:18 -0800103 _LOG_TRACE("Received interest with command: " << command);
104
105 if (command.equals(AdjacencyLsaPublisher::DATASET_COMPONENT)) {
Vince Lehmand6bb3fa2015-04-24 14:21:39 -0500106 m_adjacencyLsaPublisher.publish(interest.getName());
Jiewen Tana0497d82015-02-02 21:59:18 -0800107 }
108 else if (command.equals(CoordinateLsaPublisher::DATASET_COMPONENT)) {
Vince Lehmand6bb3fa2015-04-24 14:21:39 -0500109 m_coordinateLsaPublisher.publish(interest.getName());
Jiewen Tana0497d82015-02-02 21:59:18 -0800110 }
111 else if (command.equals(NameLsaPublisher::DATASET_COMPONENT)) {
Vince Lehmand6bb3fa2015-04-24 14:21:39 -0500112 m_nameLsaPublisher.publish(interest.getName());
Jiewen Tana0497d82015-02-02 21:59:18 -0800113 }
114 else if (command.equals(LsdbStatusPublisher::DATASET_COMPONENT)) {
Vince Lehmand6bb3fa2015-04-24 14:21:39 -0500115 m_lsdbStatusPublisher.publish(interest.getName());
Jiewen Tana0497d82015-02-02 21:59:18 -0800116 }
117 else {
118 _LOG_DEBUG("Unsupported command: " << command);
Vince Lehmand6bb3fa2015-04-24 14:21:39 -0500119 sendErrorResponse(interest.getName(), ERROR_CODE_UNSUPPORTED_COMMAND, "Unsupported command");
Jiewen Tana0497d82015-02-02 21:59:18 -0800120 }
121}
122
123void
124LsdbDatasetInterestHandler::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