Refactor and modernize namespace declarations
Move all unit tests to namespace nfd::tests
Delete unused header core/algorithm.hpp
Change-Id: I5591f0c5f3bb5db67f8b45fae95471f8a555ca68
diff --git a/tools/nfd-autoreg.cpp b/tools/nfd-autoreg.cpp
index 3745cb0..7a72b82 100644
--- a/tools/nfd-autoreg.cpp
+++ b/tools/nfd-autoreg.cpp
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
- * Copyright (c) 2014-2021, Regents of the University of California,
+ * Copyright (c) 2014-2022, Regents of the University of California,
* Arizona Board of Regents,
* Colorado State University,
* University Pierre & Marie Curie, Sorbonne University,
@@ -27,8 +27,6 @@
#include "core/version.hpp"
#include <ndn-cxx/face.hpp>
-#include <ndn-cxx/name.hpp>
-#include <ndn-cxx/encoding/buffer-stream.hpp>
#include <ndn-cxx/mgmt/nfd/controller.hpp>
#include <ndn-cxx/mgmt/nfd/face-monitor.hpp>
#include <ndn-cxx/mgmt/nfd/face-status.hpp>
@@ -37,35 +35,25 @@
#include <boost/exception/diagnostic_information.hpp>
#include <boost/program_options/options_description.hpp>
-#include <boost/program_options/variables_map.hpp>
#include <boost/program_options/parsers.hpp>
+#include <boost/program_options/variables_map.hpp>
#include <iostream>
-namespace ndn {
-namespace nfd_autoreg {
-
-using ::nfd::Network;
+namespace nfd::tools::autoreg {
class AutoregServer : boost::noncopyable
{
public:
- AutoregServer()
- : m_controller(m_face, m_keyChain)
- , m_faceMonitor(m_face)
- , m_cost(255)
- {
- }
-
- void
+ static void
onRegisterCommandSuccess(uint64_t faceId, const Name& prefix)
{
- std::cerr << "SUCCEED: register " << prefix << " on face " << faceId << std::endl;
+ std::cerr << "SUCCESS: register " << prefix << " on face " << faceId << std::endl;
}
- void
+ static void
onRegisterCommandFailure(uint64_t faceId, const Name& prefix,
- const nfd::ControlResponse& response)
+ const ndn::nfd::ControlResponse& response)
{
std::cerr << "FAILED: register " << prefix << " on face " << faceId
<< " (code: " << response.getCode() << ", reason: " << response.getText() << ")"
@@ -90,7 +78,7 @@
isBlacklisted(const boost::asio::ip::address& address) const
{
return std::any_of(m_blackList.begin(), m_blackList.end(),
- std::bind(&Network::doesContain, _1, address));
+ [&] (const auto& net) { return net.doesContain(address); });
}
/**
@@ -100,27 +88,27 @@
isWhitelisted(const boost::asio::ip::address& address) const
{
return std::any_of(m_whiteList.begin(), m_whiteList.end(),
- std::bind(&Network::doesContain, _1, address));
+ [&] (const auto& net) { return net.doesContain(address); });
}
void
registerPrefixesForFace(uint64_t faceId, const std::vector<Name>& prefixes)
{
for (const Name& prefix : prefixes) {
- m_controller.start<nfd::RibRegisterCommand>(
- nfd::ControlParameters()
+ m_controller.start<ndn::nfd::RibRegisterCommand>(
+ ndn::nfd::ControlParameters()
.setName(prefix)
.setFaceId(faceId)
- .setOrigin(nfd::ROUTE_ORIGIN_AUTOREG)
+ .setOrigin(ndn::nfd::ROUTE_ORIGIN_AUTOREG)
.setCost(m_cost)
.setExpirationPeriod(time::milliseconds::max()),
- std::bind(&AutoregServer::onRegisterCommandSuccess, this, faceId, prefix),
- std::bind(&AutoregServer::onRegisterCommandFailure, this, faceId, prefix, _1));
+ [=] (auto&&...) { onRegisterCommandSuccess(faceId, prefix); },
+ [=] (const auto& response) { onRegisterCommandFailure(faceId, prefix, response); });
}
}
void
- registerPrefixesIfNeeded(uint64_t faceId, const FaceUri& uri, nfd::FacePersistency facePersistency)
+ registerPrefixesIfNeeded(uint64_t faceId, const FaceUri& uri, ndn::nfd::FacePersistency facePersistency)
{
if (hasAllowedSchema(uri)) {
boost::system::error_code ec;
@@ -131,7 +119,7 @@
registerPrefixesForFace(faceId, m_allFacesPrefixes);
// register autoreg prefixes if new face is on-demand and not blacklisted and whitelisted
- if (facePersistency == nfd::FACE_PERSISTENCY_ON_DEMAND &&
+ if (facePersistency == ndn::nfd::FACE_PERSISTENCY_ON_DEMAND &&
!isBlacklisted(address) && isWhitelisted(address)) {
registerPrefixesForFace(faceId, m_autoregPrefixes);
}
@@ -140,10 +128,10 @@
}
void
- onNotification(const nfd::FaceEventNotification& notification)
+ onNotification(const ndn::nfd::FaceEventNotification& notification)
{
- if (notification.getKind() == nfd::FACE_EVENT_CREATED &&
- notification.getFaceScope() != nfd::FACE_SCOPE_LOCAL) {
+ if (notification.getKind() == ndn::nfd::FACE_EVENT_CREATED &&
+ notification.getFaceScope() != ndn::nfd::FACE_SCOPE_LOCAL) {
std::cerr << "PROCESSING: " << notification << std::endl;
registerPrefixesIfNeeded(notification.getFaceId(), FaceUri(notification.getRemoteUri()),
@@ -154,16 +142,6 @@
}
}
- static void
- usage(std::ostream& os,
- const boost::program_options::options_description& desc,
- const char* programName)
- {
- os << "Usage: " << programName << " [--prefix=</autoreg/prefix>]... [options]\n"
- << "\n"
- << desc;
- }
-
void
startProcessing()
{
@@ -188,7 +166,7 @@
std::cout << " " << network << std::endl;
}
- m_faceMonitor.onNotification.connect(std::bind(&AutoregServer::onNotification, this, _1));
+ m_faceMonitor.onNotification.connect([this] (const auto& notif) { onNotification(notif); });
m_faceMonitor.start();
boost::asio::signal_set signalSet(m_face.getIoService(), SIGINT, SIGTERM);
@@ -200,7 +178,7 @@
void
startFetchingFaceStatusDataset()
{
- m_controller.fetch<nfd::FaceDataset>(
+ m_controller.fetch<ndn::nfd::FaceDataset>(
[this] (const auto& faces) {
for (const auto& faceStatus : faces) {
registerPrefixesIfNeeded(faceStatus.getFaceId(), FaceUri(faceStatus.getRemoteUri()),
@@ -224,7 +202,7 @@
("all-faces-prefix,a", po::value<std::vector<Name>>(&m_allFacesPrefixes)->composing(),
"prefix that should be automatically registered for all TCP and UDP non-local faces "
"(blacklists and whitelists do not apply to this prefix)")
- ("cost,c", po::value<uint64_t>(&m_cost)->default_value(255),
+ ("cost,c", po::value<uint64_t>(&m_cost)->default_value(m_cost),
"FIB cost that should be assigned to autoreg nexthops")
("whitelist,w", po::value<std::vector<Network>>(&m_whiteList)->composing(),
"Whitelisted network, e.g., 192.168.2.0/24 or ::1/128")
@@ -232,6 +210,12 @@
"Blacklisted network, e.g., 192.168.2.32/30 or ::1/128")
;
+ auto usage = [&] (std::ostream& os) {
+ os << "Usage: " << argv[0] << " [--prefix=</autoreg/prefix>]... [options]\n"
+ << "\n"
+ << optionsDesc;
+ };
+
po::variables_map options;
try {
po::store(po::parse_command_line(argc, argv, optionsDesc), options);
@@ -239,12 +223,12 @@
}
catch (const std::exception& e) {
std::cerr << "ERROR: " << e.what() << std::endl << std::endl;
- usage(std::cerr, optionsDesc, argv[0]);
+ usage(std::cerr);
return 2;
}
if (options.count("help") > 0) {
- usage(std::cout, optionsDesc, argv[0]);
+ usage(std::cout);
return 0;
}
@@ -256,7 +240,7 @@
if (m_autoregPrefixes.empty() && m_allFacesPrefixes.empty()) {
std::cerr << "ERROR: at least one --prefix or --all-faces-prefix must be specified"
<< std::endl << std::endl;
- usage(std::cerr, optionsDesc, argv[0]);
+ usage(std::cerr);
return 2;
}
@@ -279,23 +263,22 @@
}
private:
- Face m_face;
- KeyChain m_keyChain;
- nfd::Controller m_controller;
- nfd::FaceMonitor m_faceMonitor;
+ ndn::Face m_face;
+ ndn::KeyChain m_keyChain;
+ ndn::nfd::Controller m_controller{m_face, m_keyChain};
+ ndn::nfd::FaceMonitor m_faceMonitor{m_face};
std::vector<Name> m_autoregPrefixes;
std::vector<Name> m_allFacesPrefixes;
- uint64_t m_cost;
+ uint64_t m_cost = 255;
std::vector<Network> m_whiteList;
std::vector<Network> m_blackList;
};
-} // namespace nfd_autoreg
-} // namespace ndn
+} // namespace nfd::tools::autoreg
int
main(int argc, char* argv[])
{
- ndn::nfd_autoreg::AutoregServer server;
+ nfd::tools::autoreg::AutoregServer server;
return server.main(argc, argv);
}