nlsr: Added a finally() to canonizeNeighborUris
Change-Id: I0046ae7a07be69f20a8f9d2b4acb33d8533a28e7
refs: #4128
diff --git a/src/nlsr-runner.cpp b/src/nlsr-runner.cpp
index 827593a..84ec812 100644
--- a/src/nlsr-runner.cpp
+++ b/src/nlsr-runner.cpp
@@ -57,15 +57,18 @@
m_nlsr.daemonize();
}
- /**
- * This really should be part of Nlsr::initialize, but because URI
- * canonization happens asynchronously and we need to ensure that it
- * happens before we proceed, the canonization function has to be
- * the one to call initialize.
- */
+ /** Because URI canonization needs to occur before initialization,
+ we have to pass initialize as the finally() in neighbor
+ canonization.
+ */
m_nlsr.canonizeNeighborUris(m_nlsr.getAdjacencyList().getAdjList().begin(),
[this] (std::list<Adjacent>::iterator iterator) {
- m_nlsr.canonizeContinuation(iterator);
+ m_nlsr.canonizeContinuation(iterator, [this] {
+ m_nlsr.initialize();
+ });
+ },
+ [this] {
+ m_nlsr.initialize();
});
try {
m_nlsr.startEventLoop();
diff --git a/src/nlsr.cpp b/src/nlsr.cpp
index 02915e4..a54f737 100644
--- a/src/nlsr.cpp
+++ b/src/nlsr.cpp
@@ -192,16 +192,19 @@
}
void
-Nlsr::canonizeContinuation(std::list<Adjacent>::iterator iterator)
+Nlsr::canonizeContinuation(std::list<Adjacent>::iterator iterator,
+ std::function<void(void)> finally)
{
- canonizeNeighborUris(iterator, [this] (std::list<Adjacent>::iterator iterator) {
- canonizeContinuation(iterator);
- });
+ canonizeNeighborUris(iterator, [this, finally] (std::list<Adjacent>::iterator iterator) {
+ canonizeContinuation(iterator, finally);
+ },
+ finally);
}
void
Nlsr::canonizeNeighborUris(std::list<Adjacent>::iterator currentNeighbor,
- std::function<void(std::list<Adjacent>::iterator)> then)
+ std::function<void(std::list<Adjacent>::iterator)> then,
+ std::function<void(void)> finally)
{
if (currentNeighbor != m_adjacencyList.getAdjList().end()) {
ndn::util::FaceUri uri(currentNeighbor->getFaceUri());
@@ -219,9 +222,9 @@
m_nlsrFace.getIoService(),
TIME_ALLOWED_FOR_CANONIZATION);
}
- // We have finished canonizing all neighbors, so initialize NLSR.
+ // We have finished canonizing all neighbors, so call finally()
else {
- initialize();
+ finally();
}
}
diff --git a/src/nlsr.hpp b/src/nlsr.hpp
index 89410e9..c3ed420 100644
--- a/src/nlsr.hpp
+++ b/src/nlsr.hpp
@@ -378,7 +378,8 @@
* invocation would be to pass the begin() iterator of NLSR's
* adjacency list, and to provide Nlsr::canonizeContinuation as the
* callback. Because every URI must be canonical before we begin
- * operations, the canonize function must call initialize itself.
+ * operations, the canonize function provides a finally() function
+ * to resume whatever needs to occur.
*
* \sa Nlsr::canonizeContinuation
* \sa Nlsr::initialize
@@ -386,7 +387,8 @@
*/
void
canonizeNeighborUris(std::list<Adjacent>::iterator currentNeighbor,
- std::function<void(std::list<Adjacent>::iterator)> then);
+ std::function<void(std::list<Adjacent>::iterator)> then,
+ std::function<void(void)> finally);
StatsCollector&
getStatsCollector()
@@ -449,7 +451,8 @@
* continuation function.
*/
void
- canonizeContinuation(std::list<Adjacent>::iterator iterator);
+ canonizeContinuation(std::list<Adjacent>::iterator iterator,
+ std::function<void(void)> finally);
void
scheduleDatasetFetch();
diff --git a/tests/test-nlsr.cpp b/tests/test-nlsr.cpp
index 502d5f6..0159cf4 100644
--- a/tests/test-nlsr.cpp
+++ b/tests/test-nlsr.cpp
@@ -436,15 +436,19 @@
neighbors.insert(neighborB);
int nCanonizationsLeft = nlsr.getAdjacencyList().getAdjList().size();
+ std::function<void(void)> finallyCallback = [this] {
+ nlsr.initialize();
+ };
std::function<void(std::list<Adjacent>::iterator)> thenCallback =
- [this, &thenCallback, &nCanonizationsLeft] (std::list<Adjacent>::iterator iterator) {
+ [this, &thenCallback, &finallyCallback, &nCanonizationsLeft] (std::list<Adjacent>::iterator iterator) {
nCanonizationsLeft--;
- nlsr.canonizeNeighborUris(iterator, thenCallback);
+ nlsr.canonizeNeighborUris(iterator, thenCallback, finallyCallback);
};
nlsr.canonizeNeighborUris(nlsr.getAdjacencyList().getAdjList().begin(),
- [thenCallback] (std::list<Adjacent>::iterator iterator) {
+ [&thenCallback, &finallyCallback] (std::list<Adjacent>::iterator iterator) {
thenCallback(iterator);
- });
+ },
+ finallyCallback);
while (nCanonizationsLeft != 0) {
this->advanceClocks(ndn::time::milliseconds(1));
}