src: Assign 0 cost to hyperbolic direct neighbors

refs: #1975

Change-Id: I4349a7935d1defca4772c0b4bf806a4dd12a8e50
diff --git a/src/nlsr.cpp b/src/nlsr.cpp
index e4e37fb..930dc33 100644
--- a/src/nlsr.cpp
+++ b/src/nlsr.cpp
@@ -143,6 +143,16 @@
 
   registerKeyPrefix();
   m_helloProtocol.scheduleInterest(10);
+
+  // Need to set direct neighbors' costs to 0 for hyperbolic routing
+  if (m_confParam.getHyperbolicState() == HYPERBOLIC_STATE_ON) {
+
+    std::list<Adjacent>& neighbors = m_adjacencyList.getAdjList();
+
+    for (std::list<Adjacent>::iterator it = neighbors.begin(); it != neighbors.end(); ++it) {
+      it->setLinkCost(0);
+    }
+  }
 }
 
 void
diff --git a/tests/test-nlsr.cpp b/tests/test-nlsr.cpp
new file mode 100644
index 0000000..e76508c
--- /dev/null
+++ b/tests/test-nlsr.cpp
@@ -0,0 +1,91 @@
+/* -*- 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 <http://www.gnu.org/licenses/>.
+ *
+ *
+ **/
+
+ #include "test-common.hpp"
+ #include "dummy-face.hpp"
+
+ #include "nlsr.hpp"
+
+namespace nlsr {
+namespace test {
+
+using ndn::DummyFace;
+using ndn::shared_ptr;
+
+BOOST_FIXTURE_TEST_SUITE(TestNlsr, BaseFixture)
+
+BOOST_AUTO_TEST_CASE(HyperbolicOn_ZeroCostNeighbors)
+{
+  shared_ptr<DummyFace> face = ndn::makeDummyFace();
+  Nlsr nlsr(g_ioService, g_scheduler, ndn::ref(*face));
+
+  // Simulate loading configuration file
+  AdjacencyList& neighbors = nlsr.getAdjacencyList();
+
+  Adjacent neighborA("/ndn/neighborA", "uri://faceA", 25, Adjacent::STATUS_INACTIVE, 0, 0);
+  neighbors.insert(neighborA);
+
+  Adjacent neighborB("/ndn/neighborB", "uri://faceB", 10, Adjacent::STATUS_INACTIVE, 0, 0);
+  neighbors.insert(neighborB);
+
+  Adjacent neighborC("/ndn/neighborC", "uri://faceC", 17, Adjacent::STATUS_INACTIVE, 0, 0);
+  neighbors.insert(neighborC);
+
+  nlsr.getConfParameter().setHyperbolicState(HYPERBOLIC_STATE_ON);
+
+  nlsr.initialize();
+
+  std::list<Adjacent> neighborList = neighbors.getAdjList();
+  for (std::list<Adjacent>::iterator it = neighborList.begin(); it != neighborList.end(); ++it) {
+    BOOST_CHECK_EQUAL(it->getLinkCost(), 0);
+  }
+}
+
+BOOST_AUTO_TEST_CASE(HyperbolicOff_LinkStateCost)
+{
+  shared_ptr<DummyFace> face = ndn::makeDummyFace();
+  Nlsr nlsr(g_ioService, g_scheduler, ndn::ref(*face));
+
+  // Simulate loading configuration file
+  AdjacencyList& neighbors = nlsr.getAdjacencyList();
+
+  Adjacent neighborA("/ndn/neighborA", "uri://faceA", 25, Adjacent::STATUS_INACTIVE, 0, 0);
+  neighbors.insert(neighborA);
+
+  Adjacent neighborB("/ndn/neighborB", "uri://faceB", 10, Adjacent::STATUS_INACTIVE, 0, 0);
+  neighbors.insert(neighborB);
+
+  Adjacent neighborC("/ndn/neighborC", "uri://faceC", 17, Adjacent::STATUS_INACTIVE, 0, 0);
+  neighbors.insert(neighborC);
+
+  nlsr.initialize();
+
+  std::list<Adjacent> neighborList = neighbors.getAdjList();
+  for (std::list<Adjacent>::iterator it = neighborList.begin(); it != neighborList.end(); ++it) {
+    BOOST_CHECK(it->getLinkCost() != 0);
+  }
+}
+
+BOOST_AUTO_TEST_SUITE_END()
+
+} //namespace test
+} //namespace nlsr