diff --git a/daemon/core/map-value-iterator.hpp b/daemon/core/map-value-iterator.hpp new file mode 100644 index 00000000..c8d3df7d --- /dev/null +++ b/daemon/core/map-value-iterator.hpp @@ -0,0 +1,41 @@ +/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ +/** + * Copyright (C) 2014 Named Data Networking Project + * See COPYING for copyright and distribution information. + */ + +#ifndef NFD_CORE_MAP_VALUE_ITERATOR_H +#define NFD_CORE_MAP_VALUE_ITERATOR_H + +#include "common.hpp" +#include + +namespace nfd { + +/** \class MapValueIterator + * \brief ForwardIterator to iterator over map values + */ +template +class MapValueIterator + : public boost::transform_iterator, + typename Map::const_iterator> +{ +public: + explicit + MapValueIterator(typename Map::const_iterator it) + : boost::transform_iterator, + typename Map::const_iterator>(it, &takeSecond) + { + } + +private: + static const typename Map::mapped_type& + takeSecond(const typename Map::value_type& pair) + { + return pair.second; + } +}; + +} // namespace nfd + +#endif // NFD_CORE_MAP_VALUE_ITERATOR_H diff --git a/daemon/face/face.hpp b/daemon/face/face.hpp index 325ad670..4c488601 100644 --- a/daemon/face/face.hpp +++ b/daemon/face/face.hpp @@ -122,7 +122,7 @@ private: FaceCounters m_counters; // allow setting FaceId - friend class Forwarder; + friend class FaceTable; }; diff --git a/daemon/fw/face-table.cpp b/daemon/fw/face-table.cpp new file mode 100644 index 00000000..6e06438c --- /dev/null +++ b/daemon/fw/face-table.cpp @@ -0,0 +1,50 @@ +/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ +/** + * Copyright (C) 2014 Named Data Networking Project + * See COPYING for copyright and distribution information. + */ + +#include "face-table.hpp" +#include "forwarder.hpp" + +namespace nfd { + +NFD_LOG_INIT("FaceTable"); + +FaceTable::FaceTable(Forwarder& forwarder) + : m_forwarder(forwarder) + , m_lastFaceId(0) +{ +} + +void +FaceTable::add(shared_ptr face) +{ + FaceId faceId = ++m_lastFaceId; + face->setId(faceId); + m_faces[faceId] = face; + NFD_LOG_INFO("addFace id=" << faceId); + + face->onReceiveInterest += bind(&Forwarder::onInterest, + &m_forwarder, boost::ref(*face), _1); + face->onReceiveData += bind(&Forwarder::onData, + &m_forwarder, boost::ref(*face), _1); +} + +void +FaceTable::remove(shared_ptr face) +{ + FaceId faceId = face->getId(); + m_faces.erase(faceId); + face->setId(INVALID_FACEID); + NFD_LOG_INFO("removeFace id=" << faceId); + + // XXX This clears all subscriptions, because EventEmitter + // does not support only removing Forwarder's subscription + face->onReceiveInterest.clear(); + face->onReceiveData .clear(); + + m_forwarder.getFib().removeNextHopFromAllEntries(face); +} + +} // namespace nfd diff --git a/daemon/fw/face-table.hpp b/daemon/fw/face-table.hpp new file mode 100644 index 00000000..28a4faa1 --- /dev/null +++ b/daemon/fw/face-table.hpp @@ -0,0 +1,84 @@ +/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ +/** + * Copyright (C) 2014 Named Data Networking Project + * See COPYING for copyright and distribution information. + */ + +#ifndef NFD_FW_FACE_TABLE_HPP +#define NFD_FW_FACE_TABLE_HPP + +#include "face/face.hpp" +#include "core/map-value-iterator.hpp" + +namespace nfd +{ + +class Forwarder; + +/** \brief container of all Faces + */ +class FaceTable +{ +public: + explicit + FaceTable(Forwarder& forwarder); + + void + add(shared_ptr face); + + void + remove(shared_ptr face); + + shared_ptr + get(FaceId id) const; + + size_t + size() const; + +public: // enumeration + typedef std::map > FaceMap; + + /** \brief ForwarderIterator for shared_ptr + */ + typedef MapValueIterator const_iterator; + + const_iterator + begin() const; + + const_iterator + end() const; + +private: + Forwarder& m_forwarder; + FaceId m_lastFaceId; + FaceMap m_faces; +}; + +inline shared_ptr +FaceTable::get(FaceId id) const +{ + std::map >::const_iterator i = m_faces.find(id); + return (i == m_faces.end()) ? (shared_ptr()) : (i->second); +} + +inline size_t +FaceTable::size() const +{ + return m_faces.size(); +} + +inline FaceTable::const_iterator +FaceTable::begin() const +{ + return const_iterator(m_faces.begin()); +} + +inline FaceTable::const_iterator +FaceTable::end() const +{ + return const_iterator(m_faces.end()); +} + +} // namespace nfd + +#endif // NFD_FW_FACE_TABLE_HPP diff --git a/daemon/fw/forwarder.cpp b/daemon/fw/forwarder.cpp index a8121980..b32041e8 100644 --- a/daemon/fw/forwarder.cpp +++ b/daemon/fw/forwarder.cpp @@ -15,7 +15,7 @@ NFD_LOG_INIT("Forwarder"); const Name Forwarder::s_localhostName("ndn:/localhost"); Forwarder::Forwarder() - : m_lastFaceId(0) + : m_faceTable(*this) , m_nameTree(1024) // "1024" could be made as one configurable parameter of the forwarder. , m_fib(m_nameTree) , m_pit(m_nameTree) @@ -24,55 +24,6 @@ Forwarder::Forwarder() m_strategy = make_shared(boost::ref(*this)); } -void -Forwarder::addFace(shared_ptr face) -{ - FaceId faceId = ++m_lastFaceId; - face->setId(faceId); - m_faces[faceId] = face; - NFD_LOG_INFO("addFace id=" << faceId); - - face->onReceiveInterest += bind(&Forwarder::onInterest, - this, boost::ref(*face), _1); - face->onReceiveData += bind(&Forwarder::onData, - this, boost::ref(*face), _1); -} - -void -Forwarder::removeFace(shared_ptr face) -{ - FaceId faceId = face->getId(); - m_faces.erase(faceId); - face->setId(INVALID_FACEID); - NFD_LOG_INFO("removeFace id=" << faceId); - - // XXX This clears all subscriptions, because EventEmitter - // does not support only removing Forwarder's subscription - face->onReceiveInterest.clear(); - face->onReceiveData .clear(); - - m_fib.removeNextHopFromAllEntries(face); -} - -shared_ptr -Forwarder::getFace(FaceId id) -{ - std::map >::iterator i = m_faces.find(id); - return (i == m_faces.end()) ? (shared_ptr()) : (i->second); -} - -void -Forwarder::onInterest(Face& face, const Interest& interest) -{ - this->onIncomingInterest(face, interest); -} - -void -Forwarder::onData(Face& face, const Data& data) -{ - this->onIncomingData(face, data); -} - void Forwarder::onIncomingInterest(Face& inFace, const Interest& interest) { diff --git a/daemon/fw/forwarder.hpp b/daemon/fw/forwarder.hpp index 5130c76b..726d8a06 100644 --- a/daemon/fw/forwarder.hpp +++ b/daemon/fw/forwarder.hpp @@ -9,7 +9,7 @@ #include "common.hpp" #include "core/scheduler.hpp" -#include "face/face.hpp" +#include "face-table.hpp" #include "table/fib.hpp" #include "table/pit.hpp" #include "table/cs.hpp" @@ -28,19 +28,38 @@ class Forwarder public: Forwarder(); +public: // faces + FaceTable& + getFaceTable(); + + /** \brief get existing Face + * + * shortcut to .getFaceTable().get(face) + */ + shared_ptr + getFace(FaceId id) const; + + /** \brief add new Face + * + * shortcut to .getFaceTable().add(face) + */ void addFace(shared_ptr face); + /** \brief remove existing Face + * + * shortcut to .getFaceTable().remove(face) + */ void removeFace(shared_ptr face); +public: // forwarding entrypoints and tables void onInterest(Face& face, const Interest& interest); void onData(Face& face, const Data& data); -public: Fib& getFib(); @@ -53,9 +72,6 @@ public: Measurements& getMeasurements(); - shared_ptr - getFace(FaceId id); - PUBLIC_WITH_TESTS_ELSE_PRIVATE: // pipelines /** \brief incoming Interest pipeline */ @@ -115,8 +131,7 @@ PROTECTED_WITH_TESTS_ELSE_PRIVATE: shared_ptr pitEntry); private: - FaceId m_lastFaceId; - std::map > m_faces; + FaceTable m_faceTable; NameTree m_nameTree; Fib m_fib; @@ -132,6 +147,42 @@ private: friend class fw::Strategy; }; +inline FaceTable& +Forwarder::getFaceTable() +{ + return m_faceTable; +} + +inline shared_ptr +Forwarder::getFace(FaceId id) const +{ + return m_faceTable.get(id); +} + +inline void +Forwarder::addFace(shared_ptr face) +{ + m_faceTable.add(face); +} + +inline void +Forwarder::removeFace(shared_ptr face) +{ + m_faceTable.remove(face); +} + +inline void +Forwarder::onInterest(Face& face, const Interest& interest) +{ + this->onIncomingInterest(face, interest); +} + +inline void +Forwarder::onData(Face& face, const Data& data) +{ + this->onIncomingData(face, data); +} + inline Fib& Forwarder::getFib() { @@ -156,6 +207,7 @@ Forwarder::getMeasurements() return m_measurements; } + } // namespace nfd #endif // NFD_FW_FORWARDER_HPP diff --git a/tests/core/map-value-iterator.cpp b/tests/core/map-value-iterator.cpp new file mode 100644 index 00000000..c874b8fb --- /dev/null +++ b/tests/core/map-value-iterator.cpp @@ -0,0 +1,39 @@ +/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ +/** + * Copyright (C) 2014 Named Data Networking Project + * See COPYING for copyright and distribution information. + */ + +#include "core/map-value-iterator.hpp" +#include + +#include "tests/test-common.hpp" + +namespace nfd { +namespace tests { + +BOOST_FIXTURE_TEST_SUITE(CoreMapValueIterator, BaseFixture) + +BOOST_AUTO_TEST_CASE(Basic) +{ + typedef std::map CharIntMap; + typedef MapValueIterator CharIntMapValueIterator; + BOOST_CONCEPT_ASSERT((boost::ForwardIterator)); + + CharIntMap map; + map['a'] = 1918; + map['b'] = 2675; + map['c'] = 7783; + map['d'] = 2053; + + CharIntMapValueIterator begin(map.begin()); + CharIntMapValueIterator end (map.end ()); + + int expected[] = { 1918, 2675, 7783, 2053 }; + BOOST_CHECK_EQUAL_COLLECTIONS(begin, end, expected, expected + 4); +} + +BOOST_AUTO_TEST_SUITE_END() + +} // namespace tests +} // namespace nfd diff --git a/tests/fw/face-table.cpp b/tests/fw/face-table.cpp new file mode 100644 index 00000000..474a7564 --- /dev/null +++ b/tests/fw/face-table.cpp @@ -0,0 +1,99 @@ +/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ +/** + * Copyright (C) 2014 Named Data Networking Project + * See COPYING for copyright and distribution information. + */ + +#include "fw/face-table.hpp" +#include "fw/forwarder.hpp" + +#include "tests/test-common.hpp" +#include "tests/face/dummy-face.hpp" + +namespace nfd { +namespace tests { + +BOOST_FIXTURE_TEST_SUITE(FwFaceTable, BaseFixture) + +BOOST_AUTO_TEST_CASE(AddRemove) +{ + Forwarder forwarder; + + shared_ptr face1 = make_shared(); + shared_ptr face2 = make_shared(); + + BOOST_CHECK_EQUAL(face1->getId(), INVALID_FACEID); + BOOST_CHECK_EQUAL(face2->getId(), INVALID_FACEID); + + forwarder.addFace(face1); + forwarder.addFace(face2); + + BOOST_CHECK_NE(face1->getId(), INVALID_FACEID); + BOOST_CHECK_NE(face2->getId(), INVALID_FACEID); + BOOST_CHECK_NE(face1->getId(), face2->getId()); + + forwarder.removeFace(face1); + forwarder.removeFace(face2); + + BOOST_CHECK_EQUAL(face1->getId(), INVALID_FACEID); + BOOST_CHECK_EQUAL(face2->getId(), INVALID_FACEID); +} + +BOOST_AUTO_TEST_CASE(Enumerate) +{ + Forwarder forwarder; + FaceTable& faceTable = forwarder.getFaceTable(); + + shared_ptr face1 = make_shared(); + shared_ptr face2 = make_shared(); + bool hasFace1 = false, hasFace2 = false; + + BOOST_CHECK_EQUAL(faceTable.size(), 0); + BOOST_CHECK_EQUAL(std::distance(faceTable.begin(), faceTable.end()), faceTable.size()); + + faceTable.add(face1); + BOOST_CHECK_EQUAL(faceTable.size(), 1); + BOOST_CHECK_EQUAL(std::distance(faceTable.begin(), faceTable.end()), faceTable.size()); + hasFace1 = hasFace2 = false; + for (FaceTable::const_iterator it = faceTable.begin(); it != faceTable.end(); ++it) { + if (*it == face1) { + hasFace1 = true; + } + } + BOOST_CHECK(hasFace1); + + faceTable.add(face2); + BOOST_CHECK_EQUAL(faceTable.size(), 2); + BOOST_CHECK_EQUAL(std::distance(faceTable.begin(), faceTable.end()), faceTable.size()); + hasFace1 = hasFace2 = false; + for (FaceTable::const_iterator it = faceTable.begin(); it != faceTable.end(); ++it) { + if (*it == face1) { + hasFace1 = true; + } + if (*it == face2) { + hasFace2 = true; + } + } + BOOST_CHECK(hasFace1); + BOOST_CHECK(hasFace2); + + faceTable.remove(face1); + BOOST_CHECK_EQUAL(faceTable.size(), 1); + BOOST_CHECK_EQUAL(std::distance(faceTable.begin(), faceTable.end()), faceTable.size()); + hasFace1 = hasFace2 = false; + for (FaceTable::const_iterator it = faceTable.begin(); it != faceTable.end(); ++it) { + if (*it == face1) { + hasFace1 = true; + } + if (*it == face2) { + hasFace2 = true; + } + } + BOOST_CHECK(!hasFace1); + BOOST_CHECK(hasFace2); +} + +BOOST_AUTO_TEST_SUITE_END() + +} // namespace tests +} // namespace nfd diff --git a/tests/fw/forwarder.cpp b/tests/fw/forwarder.cpp index 0a308523..b9e460cb 100644 --- a/tests/fw/forwarder.cpp +++ b/tests/fw/forwarder.cpp @@ -14,30 +14,6 @@ namespace tests { BOOST_FIXTURE_TEST_SUITE(FwForwarder, BaseFixture) -BOOST_AUTO_TEST_CASE(AddRemoveFace) -{ - Forwarder forwarder; - - shared_ptr face1 = make_shared(); - shared_ptr face2 = make_shared(); - - BOOST_CHECK_EQUAL(face1->getId(), INVALID_FACEID); - BOOST_CHECK_EQUAL(face2->getId(), INVALID_FACEID); - - forwarder.addFace(face1); - forwarder.addFace(face2); - - BOOST_CHECK_NE(face1->getId(), INVALID_FACEID); - BOOST_CHECK_NE(face2->getId(), INVALID_FACEID); - BOOST_CHECK_NE(face1->getId(), face2->getId()); - - forwarder.removeFace(face1); - forwarder.removeFace(face2); - - BOOST_CHECK_EQUAL(face1->getId(), INVALID_FACEID); - BOOST_CHECK_EQUAL(face2->getId(), INVALID_FACEID); -} - BOOST_AUTO_TEST_CASE(SimpleExchange) { Forwarder forwarder;