From 27f1add26dddb95ead2ac2eeafd60d2f93bddcdd Mon Sep 17 00:00:00 2001 From: Vince Lehman Date: Thu, 16 Oct 2014 17:14:46 -0500 Subject: [PATCH] util: Create Face with canonical FaceUri refs: #1911 Change-Id: I13cc6522b5456d91a793365e33b2e53f9bd13e46 --- src/route/fib.cpp | 7 +- src/route/fib.hpp | 3 + src/utility/face-controller.cpp | 81 ++++++++++++++ src/utility/face-controller.hpp | 77 ++++++++++++++ tests/control-commands.hpp | 7 ++ tests/utility/test-face-controller.cpp | 141 +++++++++++++++++++++++++ tests/wscript | 2 +- 7 files changed, 311 insertions(+), 7 deletions(-) create mode 100644 src/utility/face-controller.cpp create mode 100644 src/utility/face-controller.hpp create mode 100644 tests/utility/test-face-controller.cpp diff --git a/src/route/fib.cpp b/src/route/fib.cpp index 1815b27..16ed8a8 100644 --- a/src/route/fib.cpp +++ b/src/route/fib.cpp @@ -294,12 +294,7 @@ Fib::createFace(const std::string& faceUri, const CommandSucceedCallback& onSuccess, const CommandFailCallback& onFailure) { - ndn::nfd::ControlParameters faceParameters; - faceParameters - .setUri(faceUri); - m_controller.start(faceParameters, - onSuccess, - onFailure); + m_faceController.createFace(faceUri, onSuccess, onFailure); } void diff --git a/src/route/fib.hpp b/src/route/fib.hpp index e49727b..d1737d2 100644 --- a/src/route/fib.hpp +++ b/src/route/fib.hpp @@ -31,6 +31,7 @@ #include "face-map.hpp" #include "fib-entry.hpp" #include "test-access-control.hpp" +#include "utility/face-controller.hpp" namespace nlsr { @@ -48,6 +49,7 @@ public: , m_table() , m_refreshTime(0) , m_controller(face) + , m_faceController(face.getIoService(), m_controller) , m_faceMap() , m_adjacencyList(adjacencyList) , m_confParameter(conf) @@ -186,6 +188,7 @@ private: std::list m_table; int32_t m_refreshTime; ndn::nfd::Controller m_controller; + util::FaceController m_faceController; PUBLIC_WITH_TESTS_ELSE_PRIVATE: FaceMap m_faceMap; diff --git a/src/utility/face-controller.cpp b/src/utility/face-controller.cpp new file mode 100644 index 0000000..a0c615f --- /dev/null +++ b/src/utility/face-controller.cpp @@ -0,0 +1,81 @@ +/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ +/** + * Copyright (c) 2014 University of Memphis, + * Regents of the University of California + * + * This file is part of NLSR (Named-data Link State Routing). + * See AUTHORS.md for complete list of NLSR authors and contributors. + * + * NLSR is free software: you can redistribute it and/or modify it under the terms + * of the GNU General Public License as published by the Free Software Foundation, + * either version 3 of the License, or (at your option) any later version. + * + * NLSR is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; + * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR + * PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with + * NLSR, e.g., in COPYING.md file. If not, see . + * + **/ + +#include "face-controller.hpp" +#include "logger.hpp" + +namespace nlsr { +namespace util { + +INIT_LOGGER("FaceController"); + +using ndn::util::FaceUri; + +const ndn::time::seconds FaceController::TIME_ALLOWED_FOR_CANONIZATION = ndn::time::seconds(4); + +void +FaceController::createFace(const std::string& request, + const CommandSuccessCallback& onSuccess, + const CommandFailureCallback& onFailure) +{ + FaceUri uri(request); + + _LOG_TRACE("Converting " << uri << " to canonical form"); + uri.canonize(bind(&FaceController::onCanonizeSuccess, this, _1, onSuccess, onFailure, uri), + bind(&FaceController::onCanonizeFailure, this, _1, onSuccess, onFailure, uri), + m_ioService, TIME_ALLOWED_FOR_CANONIZATION); +} + +void +FaceController::createFaceInNfd(const FaceUri& uri, + const CommandSuccessCallback& onSuccess, + const CommandFailureCallback& onFailure) +{ + ndn::nfd::ControlParameters faceParameters; + faceParameters.setUri(uri.toString()); + + _LOG_DEBUG("Creating Face in NFD with face-uri: " << uri); + m_controller.start(faceParameters, onSuccess, onFailure); +} + +void +FaceController::onCanonizeSuccess(const FaceUri& uri, + const CommandSuccessCallback& onSuccess, + const CommandFailureCallback& onFailure, + const FaceUri& request) +{ + _LOG_DEBUG("Converted " << request << " to canonical form: " << uri); + + createFaceInNfd(uri, onSuccess, onFailure); +} + +void +FaceController::onCanonizeFailure(const std::string& reason, + const CommandSuccessCallback& onSuccess, + const CommandFailureCallback& onFailure, + const FaceUri& request) +{ + _LOG_WARN("Could not convert " << request << " to canonical form: " << reason); + onFailure(CANONIZE_ERROR_CODE, "Could not canonize face-uri: " + request.toString()); +} + +} // namespace util +} // namespace nlsr diff --git a/src/utility/face-controller.hpp b/src/utility/face-controller.hpp new file mode 100644 index 0000000..3c42afe --- /dev/null +++ b/src/utility/face-controller.hpp @@ -0,0 +1,77 @@ +/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ +/** + * Copyright (c) 2014 University of Memphis, + * Regents of the University of California + * + * This file is part of NLSR (Named-data Link State Routing). + * See AUTHORS.md for complete list of NLSR authors and contributors. + * + * NLSR is free software: you can redistribute it and/or modify it under the terms + * of the GNU General Public License as published by the Free Software Foundation, + * either version 3 of the License, or (at your option) any later version. + * + * NLSR is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; + * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR + * PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with + * NLSR, e.g., in COPYING.md file. If not, see . + * + **/ + +#ifndef NLSR_UTIL_FACE_CONTROLLER_HPP +#define NLSR_UTIL_FACE_CONTROLLER_HPP + +#include +#include + +namespace nlsr { +namespace util { + +class FaceController : boost::noncopyable +{ +public: + typedef ndn::function CommandSuccessCallback; + typedef ndn::function CommandFailureCallback; + + FaceController(boost::asio::io_service& io, ndn::nfd::Controller& controller) + : m_ioService(io) + , m_controller(controller) + { + } + + void + createFace(const std::string& request, + const CommandSuccessCallback& onSuccess, + const CommandFailureCallback& onFailure); + +private: + void + createFaceInNfd(const ndn::util::FaceUri& uri, + const CommandSuccessCallback& onSuccess, + const CommandFailureCallback& onFailure); + + void + onCanonizeSuccess(const ndn::util::FaceUri& uri, + const CommandSuccessCallback& onSuccess, + const CommandFailureCallback& onFailure, + const ndn::util::FaceUri& request); + + void + onCanonizeFailure(const std::string& reason, + const CommandSuccessCallback& onSuccess, + const CommandFailureCallback& onFailure, + const ndn::util::FaceUri& request); + +private: + boost::asio::io_service& m_ioService; + ndn::nfd::Controller& m_controller; + + static const uint32_t CANONIZE_ERROR_CODE = 408; + static const ndn::time::seconds TIME_ALLOWED_FOR_CANONIZATION; +}; + +} // namespace util +} // namespace nlsr + +#endif // NLSR_UTIL_FACE_CONTROLLER_HPP diff --git a/tests/control-commands.hpp b/tests/control-commands.hpp index e84deea..17100db 100644 --- a/tests/control-commands.hpp +++ b/tests/control-commands.hpp @@ -50,6 +50,13 @@ extractRibCommandParameters(const ndn::Interest& interest, ndn::Name::Component& extractParameters(interest, verb, extractedParameters, ndn::Name("/localhost/nfd/rib")); } +inline void +extractFaceCommandParameters(const ndn::Interest& interest, ndn::Name::Component& verb, + ndn::nfd::ControlParameters& extractedParameters) +{ + extractParameters(interest, verb, extractedParameters, ndn::Name("/localhost/nfd/faces")); +} + } // namespace test } // namespace nlsr diff --git a/tests/utility/test-face-controller.cpp b/tests/utility/test-face-controller.cpp new file mode 100644 index 0000000..d242ee7 --- /dev/null +++ b/tests/utility/test-face-controller.cpp @@ -0,0 +1,141 @@ +/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ +/** + * Copyright (c) 2014 University of Memphis, + * Regents of the University of California + * + * This file is part of NLSR (Named-data Link State Routing). + * See AUTHORS.md for complete list of NLSR authors and contributors. + * + * NLSR is free software: you can redistribute it and/or modify it under the terms + * of the GNU General Public License as published by the Free Software Foundation, + * either version 3 of the License, or (at your option) any later version. + * + * NLSR is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; + * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR + * PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with + * NLSR, e.g., in COPYING.md file. If not, see . + * + **/ + +#include "tests/test-common.hpp" +#include "tests/control-commands.hpp" +#include "tests/dummy-face.hpp" + +#include "utility/face-controller.hpp" + +#include + +namespace nlsr { +namespace test { + +using ndn::bind; +using ndn::shared_ptr; +using ndn::Interest; + +class FaceControllerFixture : public BaseFixture +{ +public: + FaceControllerFixture() + : face(ndn::makeDummyFace()) + , interests(face->m_sentInterests) + , controller(ndn::ref(*face)) + , faceManager(g_ioService, controller) + { + } + + void + expectCanonizeSuccess(const std::string& request, const std::string expectedUri) + { + faceManager.createFace(request, 0, bind(&FaceControllerFixture::onFailure, this, _1, _2)); + expectedUris.push_back(expectedUri); + } + + void + expectCanonizeFailure(const std::string& request) + { + faceManager.createFace(request, 0, bind(&FaceControllerFixture::onFailure, this, _1, _2)); + } + + void + onFailure(uint32_t code, const std::string& reason) + { + BOOST_CHECK_EQUAL(code, 408); + } + +public: + shared_ptr face; + std::vector& interests; + ndn::nfd::Controller controller; + util::FaceController faceManager; + + std::list expectedUris; +}; + +BOOST_FIXTURE_TEST_SUITE(TestFaceController, FaceControllerFixture) + +BOOST_AUTO_TEST_CASE(FaceCreateCanonize) +{ + expectCanonizeSuccess("udp4://192.0.2.1:6363", "udp4://192.0.2.1:6363"); + expectCanonizeSuccess("tcp4://192.0.2.1:6363", "tcp4://192.0.2.1:6363"); + + expectCanonizeSuccess("udp://192.0.2.1", "udp4://192.0.2.1:6363"); + expectCanonizeSuccess("udp://203.0.113.1:6363", "udp4://203.0.113.1:6363"); + expectCanonizeSuccess("udp4://google-public-dns-a.google.com", "udp4://8.8.8.8:6363"); + + expectCanonizeSuccess("tcp://192.0.2.1", "tcp4://192.0.2.1:6363"); + expectCanonizeSuccess("tcp://203.0.113.1:6363", "tcp4://203.0.113.1:6363"); + expectCanonizeSuccess("tcp4://google-public-dns-a.google.com", "tcp4://8.8.8.8:6363"); + + expectCanonizeSuccess("udp6://[2001:4860:4860::8888]:6363", "udp6://[2001:4860:4860::8888]:6363"); + expectCanonizeSuccess("tcp6://[2001:4860:4860::8888]:6363", "tcp6://[2001:4860:4860::8888]:6363"); + + expectCanonizeSuccess("udp://[2001:4860:4860::8888]", "udp6://[2001:4860:4860::8888]:6363"); + expectCanonizeSuccess("udp://[2001:4860:4860::8888]:6363", "udp6://[2001:4860:4860::8888]:6363"); + expectCanonizeSuccess("udp6://google-public-dns-a.google.com", + "udp6://[2001:4860:4860::8888]:6363"); + + expectCanonizeSuccess("tcp://[2001:4860:4860::8888]", "tcp6://[2001:4860:4860::8888]:6363"); + expectCanonizeSuccess("tcp://[2001:4860:4860::8888]:6363", "tcp6://[2001:4860:4860::8888]:6363"); + expectCanonizeSuccess("tcp6://google-public-dns-a.google.com", + "tcp6://[2001:4860:4860::8888]:6363"); + + g_ioService.run(); + face->processEvents(ndn::time::milliseconds(1)); + + BOOST_CHECK_EQUAL(interests.size(), expectedUris.size()); + + for (std::vector::iterator it = interests.begin(); it != interests.end(); ++it) { + + ndn::nfd::ControlParameters extractedParameters; + ndn::Name::Component verb; + + extractFaceCommandParameters(*it, verb, extractedParameters); + + BOOST_CHECK_EQUAL(verb, ndn::Name::Component("create")); + + std::list::iterator uri = std::find(expectedUris.begin(), + expectedUris.end(), + extractedParameters.getUri()); + + BOOST_REQUIRE(uri != expectedUris.end()); + expectedUris.erase(uri); + } +} + +BOOST_AUTO_TEST_CASE(FaceCreateFailure) +{ + expectCanonizeFailure("udp4://not-a-valid.uri"); + expectCanonizeFailure("udp4://256.0.0.1:6363"); + + g_ioService.run(); + face->processEvents(ndn::time::milliseconds(1)); + + BOOST_CHECK_EQUAL(interests.size(), 0); +} + +BOOST_AUTO_TEST_SUITE_END() + +} //namespace test +} //namespace nlsr diff --git a/tests/wscript b/tests/wscript index 3ac955e..63bcafa 100644 --- a/tests/wscript +++ b/tests/wscript @@ -28,7 +28,7 @@ def build(bld): target='unit-tests-main', name='unit-tests-main', features='cxx', - source=bld.path.ant_glob(['*.cpp']), + source=bld.path.ant_glob(['*.cpp', 'utility/*.cpp']), use='nlsr-objects', )