blob: 11ddc6914f2e3ba8b766af0c1deef698acaf6bdc [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,
40 const ndn::Name& routerName,
41 ndn::KeyChain& keyChain)
Vince Lehmand6bb3fa2015-04-24 14:21:39 -050042 : 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 Tana0497d82015-02-02 21:59:18 -080044 , m_face(face)
45 , m_keyChain(keyChain)
Vince Lehmand6bb3fa2015-04-24 14:21:39 -050046 , m_adjacencyLsaPublisher(lsdb, face, keyChain)
47 , m_coordinateLsaPublisher(lsdb, face, keyChain)
48 , m_nameLsaPublisher(lsdb, face, keyChain)
49 , m_lsdbStatusPublisher(lsdb, face, keyChain,
Jiewen Tana0497d82015-02-02 21:59:18 -080050 m_adjacencyLsaPublisher,
51 m_coordinateLsaPublisher,
52 m_nameLsaPublisher)
53
54{
Jiewen Tana0497d82015-02-02 21:59:18 -080055}
56
57void
Vince Lehmand6bb3fa2015-04-24 14:21:39 -050058LsdbDatasetInterestHandler::startListeningOnLocalhost()
Jiewen Tana0497d82015-02-02 21:59:18 -080059{
Vince Lehmand6bb3fa2015-04-24 14:21:39 -050060 _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
66void
67LsdbDatasetInterestHandler::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
75void
76LsdbDatasetInterestHandler::onInterest(const ndn::Interest& interest,
77 const ndn::Name& commandPrefix)
78{
79 if (!isValidCommandPrefix(interest, commandPrefix))
Jiewen Tana0497d82015-02-02 21:59:18 -080080 {
81 _LOG_DEBUG("Received malformed interest: " << interest.getName());
82
Vince Lehmand6bb3fa2015-04-24 14:21:39 -050083 sendErrorResponse(interest.getName(), ERROR_CODE_MALFORMED_COMMAND, "Malformed command");
Jiewen Tana0497d82015-02-02 21:59:18 -080084 return;
85 }
86
Vince Lehmand6bb3fa2015-04-24 14:21:39 -050087 ndn::Name::Component command = interest.getName().get(commandPrefix.size());
88 processCommand(interest, command);
89}
90
91bool
92LsdbDatasetInterestHandler::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
101void
102LsdbDatasetInterestHandler::processCommand(const ndn::Interest& interest,
103 const ndn::Name::Component& command)
104{
Jiewen Tana0497d82015-02-02 21:59:18 -0800105 _LOG_TRACE("Received interest with command: " << command);
106
107 if (command.equals(AdjacencyLsaPublisher::DATASET_COMPONENT)) {
Vince Lehmand6bb3fa2015-04-24 14:21:39 -0500108 m_adjacencyLsaPublisher.publish(interest.getName());
Jiewen Tana0497d82015-02-02 21:59:18 -0800109 }
110 else if (command.equals(CoordinateLsaPublisher::DATASET_COMPONENT)) {
Vince Lehmand6bb3fa2015-04-24 14:21:39 -0500111 m_coordinateLsaPublisher.publish(interest.getName());
Jiewen Tana0497d82015-02-02 21:59:18 -0800112 }
113 else if (command.equals(NameLsaPublisher::DATASET_COMPONENT)) {
Vince Lehmand6bb3fa2015-04-24 14:21:39 -0500114 m_nameLsaPublisher.publish(interest.getName());
Jiewen Tana0497d82015-02-02 21:59:18 -0800115 }
116 else if (command.equals(LsdbStatusPublisher::DATASET_COMPONENT)) {
Vince Lehmand6bb3fa2015-04-24 14:21:39 -0500117 m_lsdbStatusPublisher.publish(interest.getName());
Jiewen Tana0497d82015-02-02 21:59:18 -0800118 }
119 else {
120 _LOG_DEBUG("Unsupported command: " << command);
Vince Lehmand6bb3fa2015-04-24 14:21:39 -0500121 sendErrorResponse(interest.getName(), ERROR_CODE_UNSUPPORTED_COMMAND, "Unsupported command");
Jiewen Tana0497d82015-02-02 21:59:18 -0800122 }
123}
124
125void
126LsdbDatasetInterestHandler::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