manager-base.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2014-2025, 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 "logger.hpp"
24 
25 namespace nlsr {
26 namespace update {
27 
28 INIT_LOGGER(update.PrefixCommandProcessor);
29 
30 ManagerBase::ManagerBase(ndn::mgmt::Dispatcher& dispatcher,
31  const std::string& module)
32  : m_dispatcher(dispatcher)
33  , m_module(module)
34 {
35 }
36 
37 ndn::PartialName
38 ManagerBase::makeRelPrefix(const std::string& verb) const
39 {
40  return ndn::PartialName(m_module).append(verb);
41 }
42 
43 CommandManagerBase::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 
53 void
55  const ndn::Interest& interest,
56  const ndn::mgmt::ControlParametersBase& parameters,
57  const ndn::mgmt::CommandContinuation& done)
58 {
59  const auto& castParams = static_cast<const ndn::nfd::ControlParameters&>(parameters);
60 
61  // Only build a Name LSA if the added name is new
62  if (m_namePrefixList.insert(castParams.getName())) {
63  NLSR_LOG_INFO("Advertising name: " << castParams.getName() << "\n");
65  if (castParams.hasFlags() && castParams.getFlags() == PREFIX_FLAG) {
66  NLSR_LOG_INFO("Saving name to the configuration file ");
67  if (afterAdvertise(castParams.getName()) == true) {
68  return done(ndn::nfd::ControlResponse(205, "OK").setBody(parameters.wireEncode()));
69  }
70  else {
71  return done(ndn::nfd::ControlResponse(406, "Failed to open configuration file.")
72  .setBody(parameters.wireEncode()));
73  }
74  }
75  return done(ndn::nfd::ControlResponse(200, "OK").setBody(parameters.wireEncode()));
76  }
77  else {
78  if (castParams.hasFlags() && castParams.getFlags() == PREFIX_FLAG) {
79  // Save an already advertised prefix
80  NLSR_LOG_INFO("Saving an already advertised name: " << castParams.getName() << "\n");
81  if (afterAdvertise(castParams.getName()) == true) {
82  return done(ndn::nfd::ControlResponse(205, "OK").setBody(parameters.wireEncode()));
83  }
84  else {
85  return done(ndn::nfd::ControlResponse(406, "Prefix is already Saved/Failed to open configuration file.")
86  .setBody(parameters.wireEncode()));
87  }
88  }
89  return done(ndn::nfd::ControlResponse(204, "Prefix is already advertised/inserted.")
90  .setBody(parameters.wireEncode()));
91  }
92 }
93 
94 void
96  const ndn::Interest& interest,
97  const ndn::mgmt::ControlParametersBase& parameters,
98  const ndn::mgmt::CommandContinuation& done)
99 {
100  const auto& castParams = static_cast<const ndn::nfd::ControlParameters&>(parameters);
101 
102  // Only build a Name LSA if the added name is new
103  if (m_namePrefixList.erase(castParams.getName())) {
104  NLSR_LOG_INFO("Withdrawing/Removing name: " << castParams.getName() << "\n");
106  if (castParams.hasFlags() && castParams.getFlags() == PREFIX_FLAG) {
107  if (afterWithdraw(castParams.getName()) == true) {
108  return done(ndn::nfd::ControlResponse(205, "OK").setBody(parameters.wireEncode()));
109  }
110  else {
111  return done(ndn::nfd::ControlResponse(406, "Failed to open configuration file.")
112  .setBody(parameters.wireEncode()));
113  }
114  }
115  return done(ndn::nfd::ControlResponse(200, "OK").setBody(parameters.wireEncode()));
116  }
117  else {
118  if (castParams.hasFlags() && castParams.getFlags() == PREFIX_FLAG) {
119  // Delete an already withdrawn prefix
120  NLSR_LOG_INFO("Deleting an already withdrawn name: " << castParams.getName() << "\n");
121  if (afterWithdraw(castParams.getName()) == true) {
122  return done(ndn::nfd::ControlResponse(205, "OK").setBody(parameters.wireEncode()));
123  }
124  else {
125  return done(ndn::nfd::ControlResponse(406, "Prefix is already deleted/Failed to open configuration file.")
126  .setBody(parameters.wireEncode()));
127  }
128  }
129  return done(ndn::nfd::ControlResponse(204, "Prefix is already withdrawn/removed.")
130  .setBody(parameters.wireEncode()));
131  }
132 }
133 
134 } // namespace update
135 } // namespace nlsr
void buildAndInstallOwnNameLsa()
Builds a name LSA for this router and then installs it into the LSDB.
Definition: lsdb.cpp:93
bool erase(const ndn::Name &name, const std::string &source="")
Deletes name and source combination.
bool insert(const ndn::Name &name, const std::string &source="")
Inserts name and source combination.
virtual std::optional< bool > afterWithdraw(const ndn::Name &prefix)
save an advertised prefix to the nlsr configuration file returns bool from the overridden function wh...
void withdrawAndRemovePrefix(const ndn::Name &prefix, const ndn::Interest &interest, const ndn::mgmt::ControlParametersBase &parameters, const ndn::mgmt::CommandContinuation &done)
remove desired name prefix from the advertised name prefix list or remove a prefix from the FIB if pa...
void advertiseAndInsertPrefix(const ndn::Name &prefix, const ndn::Interest &interest, const ndn::mgmt::ControlParametersBase &parameters, const ndn::mgmt::CommandContinuation &done)
add desired name prefix to the advertised name prefix list or insert a prefix into the FIB if paramet...
virtual std::optional< bool > afterAdvertise(const ndn::Name &prefix)
save an advertised prefix to the nlsr configuration file returns bool from the overridden function wh...
CommandManagerBase(ndn::mgmt::Dispatcher &m_dispatcher, NamePrefixList &m_namePrefixList, Lsdb &lsdb, const std::string &module)
ManagerBase(ndn::mgmt::Dispatcher &m_dispatcher, const std::string &module)
ndn::PartialName makeRelPrefix(const std::string &verb) const
Generate the relative prefix for a handler by appending the verb name to the module name.
Copyright (c) 2014-2018, The University of Memphis, Regents of the University of California.
#define INIT_LOGGER(name)
Definition: logger.hpp:35
#define NLSR_LOG_INFO(x)
Definition: logger.hpp:39
Copyright (c) 2014-2020, The University of Memphis, Regents of the University of California.