blob: 5a1034c78f2f7d083fbb558f0d1efb8961b3a325 [file] [log] [blame]
Laqin Fan54a43f02017-03-08 12:31:30 -06001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2014-2017, 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 "manager-base.hpp"
23#include <iostream>
24
25namespace nlsr {
26namespace update {
27
28INIT_LOGGER("UpdatePrefixCommandProcessor");
29
30ManagerBase::ManagerBase(ndn::mgmt::Dispatcher& dispatcher,
31 const std::string& module)
32 : m_dispatcher(dispatcher)
33 , m_module(module)
34{
35}
36
37ndn::PartialName
38ManagerBase::makeRelPrefix(const std::string& verb) const
39{
40 return ndn::PartialName(m_module).append(verb);
41}
42
43CommandManagerBase::CommandManagerBase(ndn::mgmt::Dispatcher& dispatcher,
44 NamePrefixList& namePrefixList,
45 Lsdb& lsdb,
46 const std::string& module)
47 : ManagerBase(dispatcher, module)
48 , m_namePrefixList(namePrefixList)
49 , m_lsdb(lsdb)
50{
51}
52
53void
54CommandManagerBase::advertiseAndInsertPrefix(const ndn::Name& prefix,
55 const ndn::Interest& interest,
56 const ndn::mgmt::ControlParameters& parameters,
57 const ndn::mgmt::CommandContinuation& done)
58 {
59 const ndn::nfd::ControlParameters& castParams =
60 static_cast<const ndn::nfd::ControlParameters&>(parameters);
61
62 // Only build a Name LSA if the added name is new
63 if (m_namePrefixList.insert(castParams.getName())) {
dmcoomes5bcb39e2017-10-31 15:07:55 -050064 NLSR_LOG_INFO("Advertising/Inserting name: " << castParams.getName() << "\n");
Laqin Fan54a43f02017-03-08 12:31:30 -060065 m_lsdb.buildAndInstallOwnNameLsa();
66 return done(ndn::nfd::ControlResponse(200, "OK").setBody(parameters.wireEncode()));
67 }
68
69 return done(ndn::nfd::ControlResponse(204, "Prefix is already advetised/inserted.").setBody(parameters.wireEncode()));
70}
71
72void
73CommandManagerBase::withdrawAndRemovePrefix(const ndn::Name& prefix,
74 const ndn::Interest& interest,
75 const ndn::mgmt::ControlParameters& parameters,
76 const ndn::mgmt::CommandContinuation& done)
77{
78 const ndn::nfd::ControlParameters& castParams =
79 static_cast<const ndn::nfd::ControlParameters&>(parameters);
80
81 // Only build a Name LSA if the added name is new
82 if (m_namePrefixList.remove(castParams.getName())) {
dmcoomes5bcb39e2017-10-31 15:07:55 -050083 NLSR_LOG_INFO("Withdrawing/Removing name: " << castParams.getName() << "\n");
Laqin Fan54a43f02017-03-08 12:31:30 -060084 m_lsdb.buildAndInstallOwnNameLsa();
85 return done(ndn::nfd::ControlResponse(200, "OK").setBody(parameters.wireEncode()));
86 }
87
88 return done(ndn::nfd::ControlResponse(204, "Prefix is already withdrawn/removed.").setBody(parameters.wireEncode()));
89}
90
91} // namespace update
92} // namespace nlsr