fw: make strategies understand scope

refs #1253

Change-Id: I57f7a6008e6f08c9817e58f480020eb9219a4aec
diff --git a/daemon/fw/best-route-strategy.cpp b/daemon/fw/best-route-strategy.cpp
index 8f87270..ac865d0 100644
--- a/daemon/fw/best-route-strategy.cpp
+++ b/daemon/fw/best-route-strategy.cpp
@@ -20,19 +20,34 @@
 {
 }
 
+static inline bool
+predicate_PitEntry_canForwardTo_NextHop(shared_ptr<pit::Entry> pitEntry,
+                                        const fib::NextHop& nexthop)
+{
+  return pitEntry->canForwardTo(*nexthop.getFace());
+}
+
 void
 BestRouteStrategy::afterReceiveInterest(const Face& inFace,
                    const Interest& interest,
                    shared_ptr<fib::Entry> fibEntry,
                    shared_ptr<pit::Entry> pitEntry)
 {
+  if (pitEntry->hasUnexpiredOutRecords()) {
+    // not a new Interest, don't forward
+    return;
+  }
+
   const fib::NextHopList& nexthops = fibEntry->getNextHops();
-  if (nexthops.size() == 0) {
+  fib::NextHopList::const_iterator it = std::find_if(nexthops.begin(), nexthops.end(),
+    bind(&predicate_PitEntry_canForwardTo_NextHop, pitEntry, _1));
+
+  if (it == nexthops.end()) {
     this->rejectPendingInterest(pitEntry);
     return;
   }
-  
-  shared_ptr<Face> outFace = nexthops.begin()->getFace();
+
+  shared_ptr<Face> outFace = it->getFace();
   this->sendInterest(pitEntry, outFace);
 }
 
diff --git a/daemon/fw/broadcast-strategy.cpp b/daemon/fw/broadcast-strategy.cpp
index 3e8c2bf..7f5b248 100644
--- a/daemon/fw/broadcast-strategy.cpp
+++ b/daemon/fw/broadcast-strategy.cpp
@@ -28,16 +28,14 @@
 {
   const fib::NextHopList& nexthops = fibEntry->getNextHops();
 
-  bool isPropagated = false;
   for (fib::NextHopList::const_iterator it = nexthops.begin(); it != nexthops.end(); ++it) {
     shared_ptr<Face> outFace = it->getFace();
-    if (outFace->getId() != inFace.getId()) {
+    if (pitEntry->canForwardTo(*outFace)) {
       this->sendInterest(pitEntry, outFace);
-      isPropagated = true;
     }
   }
 
-  if (!isPropagated) {
+  if (!pitEntry->hasUnexpiredOutRecords()) {
     this->rejectPendingInterest(pitEntry);
   }
 }
diff --git a/daemon/fw/forwarder.cpp b/daemon/fw/forwarder.cpp
index a723a17..4ab852e 100644
--- a/daemon/fw/forwarder.cpp
+++ b/daemon/fw/forwarder.cpp
@@ -13,9 +13,7 @@
 
 using fw::Strategy;
 
-const ndn::Milliseconds Forwarder::DEFAULT_INTEREST_LIFETIME(static_cast<ndn::Milliseconds>(4000));
 const Name Forwarder::LOCALHOST_NAME("ndn:/localhost");
-const Name Forwarder::LOCALHOP_NAME("ndn:/localhop");
 
 Forwarder::Forwarder()
   : m_faceTable(*this)
@@ -35,9 +33,6 @@
   NFD_LOG_DEBUG("onIncomingInterest face=" << inFace.getId() <<
                 " interest=" << interest.getName());
   const_cast<Interest&>(interest).setIncomingFaceId(inFace.getId());
-  if (interest.getInterestLifetime() < 0) {
-    const_cast<Interest&>(interest).setInterestLifetime(DEFAULT_INTEREST_LIFETIME);
-  }
   m_counters.getInInterest() ++;
 
   // /localhost scope control
@@ -132,22 +127,10 @@
   NFD_LOG_DEBUG("onOutgoingInterest face=" << outFace.getId() <<
                 " interest=" << pitEntry->getName());
 
-  // /localhost scope control
-  bool isViolatingLocalhost = !outFace.isLocal() &&
-                              LOCALHOST_NAME.isPrefixOf(pitEntry->getName());
-  if (isViolatingLocalhost) {
+  // scope control
+  if (pitEntry->violatesScope(outFace)) {
     NFD_LOG_DEBUG("onOutgoingInterest face=" << outFace.getId() <<
-                  " interest=" << pitEntry->getName() << " violates /localhost");
-    return;
-  }
-
-  // /localhop scope control
-  bool isViolatingLocalhop = !outFace.isLocal() &&
-                             LOCALHOP_NAME.isPrefixOf(pitEntry->getName()) &&
-                             !pitEntry->hasLocalInRecord();
-  if (isViolatingLocalhop) {
-    NFD_LOG_DEBUG("onOutgoingInterest face=" << outFace.getId() <<
-                  " interest=" << pitEntry->getName() << " violates /localhop");
+                  " interest=" << pitEntry->getName() << " violates scope");
     return;
   }
 
@@ -323,6 +306,12 @@
 void
 Forwarder::setStragglerTimer(shared_ptr<pit::Entry> pitEntry)
 {
+  if (pitEntry->hasUnexpiredOutRecords()) {
+    NFD_LOG_DEBUG("setStragglerTimer " << pitEntry->getName() <<
+                  " cannot set StragglerTimer when an OutRecord is pending");
+    return;
+  }
+
   time::Duration stragglerTime = time::milliseconds(100);
 
   pitEntry->m_stragglerTimer = scheduler::schedule(stragglerTime,
diff --git a/daemon/fw/forwarder.hpp b/daemon/fw/forwarder.hpp
index e229dcf..2d4d325 100644
--- a/daemon/fw/forwarder.hpp
+++ b/daemon/fw/forwarder.hpp
@@ -157,9 +157,7 @@
   Measurements   m_measurements;
   StrategyChoice m_strategyChoice;
 
-  static const ndn::Milliseconds DEFAULT_INTEREST_LIFETIME;
   static const Name LOCALHOST_NAME;
-  static const Name LOCALHOP_NAME;
 
   // allow Strategy (base class) to enter pipelines
   friend class fw::Strategy;
diff --git a/daemon/fw/ncc-strategy.cpp b/daemon/fw/ncc-strategy.cpp
index d8928f5..79d8b1a 100644
--- a/daemon/fw/ncc-strategy.cpp
+++ b/daemon/fw/ncc-strategy.cpp
@@ -53,7 +53,7 @@
 
   shared_ptr<Face> bestFace = measurementsEntryInfo->getBestFace();
   if (static_cast<bool>(bestFace) && fibEntry->hasNextHop(bestFace) &&
-      pitEntry->canForwardTo(bestFace)) {
+      pitEntry->canForwardTo(*bestFace)) {
     // TODO Should we use `randlow = 100 + nrand48(h->seed) % 4096U;` ?
     deferFirst = measurementsEntryInfo->m_prediction;
     deferRange = static_cast<time::Duration>((deferFirst + time::microseconds(1)) / 2);
@@ -71,7 +71,7 @@
 
   shared_ptr<Face> previousFace = measurementsEntryInfo->m_previousFace.lock();
   if (static_cast<bool>(previousFace) && fibEntry->hasNextHop(previousFace) &&
-      pitEntry->canForwardTo(previousFace)) {
+      pitEntry->canForwardTo(*previousFace)) {
     --nUpstreams;
   }
 
@@ -103,7 +103,7 @@
 
   shared_ptr<Face> previousFace = measurementsEntryInfo->m_previousFace.lock();
   if (static_cast<bool>(previousFace) && fibEntry->hasNextHop(previousFace) &&
-      pitEntry->canForwardTo(previousFace)) {
+      pitEntry->canForwardTo(*previousFace)) {
     this->sendInterest(pitEntry, previousFace);
   }
 
@@ -111,7 +111,7 @@
   bool isForwarded = false;
   for (fib::NextHopList::const_iterator it = nexthops.begin(); it != nexthops.end(); ++it) {
     shared_ptr<Face> face = it->getFace();
-    if (pitEntry->canForwardTo(face)) {
+    if (pitEntry->canForwardTo(*face)) {
       isForwarded = true;
       this->sendInterest(pitEntry, face);
       break;