fw: Nack in pipelines and best-route strategy

* in PIT out-record, add last incoming Nack field
* create incoming Nack pipeline
* create outgoing Nack pipeline
* modify Interest loop pipeline to send Nack upon duplicate Nonce
* in strategy API, add after receive Nack trigger and send Nack action
* in best-route strategy, send Nack-NoRoute before rejecting pending Interest
* in best-route strategy, process incoming Nack

Other changes include:

* Pit::find
* StrategyTester saved arguments structs
* TopologyTester transmit at Transport level

refs #3156

Change-Id: I7868561c0838231083d471261200aeb280cc6e9d
diff --git a/daemon/table/pit-entry.cpp b/daemon/table/pit-entry.cpp
index da7c6ed..85ea19c 100644
--- a/daemon/table/pit-entry.cpp
+++ b/daemon/table/pit-entry.cpp
@@ -149,6 +149,16 @@
 }
 
 void
+Entry::deleteInRecord(const Face& face)
+{
+  auto it = std::find_if(m_inRecords.begin(), m_inRecords.end(),
+    [&face] (const InRecord& inRecord) { return inRecord.getFace().get() == &face; });
+  if (it != m_inRecords.end()) {
+    m_inRecords.erase(it);
+  }
+}
+
+void
 Entry::deleteInRecords()
 {
   m_inRecords.clear();
@@ -168,8 +178,8 @@
   return it;
 }
 
-OutRecordCollection::const_iterator
-Entry::getOutRecord(const Face& face) const
+OutRecordCollection::iterator
+Entry::getOutRecord(const Face& face)
 {
   return std::find_if(m_outRecords.begin(), m_outRecords.end(),
     [&face] (const OutRecord& outRecord) { return outRecord.getFace().get() == &face; });
diff --git a/daemon/table/pit-entry.hpp b/daemon/table/pit-entry.hpp
index 6d59ce8..3f15598 100644
--- a/daemon/table/pit-entry.hpp
+++ b/daemon/table/pit-entry.hpp
@@ -132,6 +132,10 @@
   InRecordCollection::const_iterator
   getInRecord(const Face& face) const;
 
+  /// deletes one InRecord for face if exists
+  void
+  deleteInRecord(const Face& face);
+
   /// deletes all InRecords
   void
   deleteInRecords();
@@ -151,8 +155,8 @@
   /** \brief get the OutRecord for face
    *  \return an iterator to the OutRecord, or .end if it does not exist
    */
-  OutRecordCollection::const_iterator
-  getOutRecord(const Face& face) const;
+  OutRecordCollection::iterator
+  getOutRecord(const Face& face);
 
   /// deletes one OutRecord for face if exists
   void
diff --git a/daemon/table/pit-out-record.cpp b/daemon/table/pit-out-record.cpp
index 268b67b..1cf303d 100644
--- a/daemon/table/pit-out-record.cpp
+++ b/daemon/table/pit-out-record.cpp
@@ -1,11 +1,12 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /**
- * Copyright (c) 2014  Regents of the University of California,
- *                     Arizona Board of Regents,
- *                     Colorado State University,
- *                     University Pierre & Marie Curie, Sorbonne University,
- *                     Washington University in St. Louis,
- *                     Beijing Institute of Technology
+ * Copyright (c) 2014-2015,  Regents of the University of California,
+ *                           Arizona Board of Regents,
+ *                           Colorado State University,
+ *                           University Pierre & Marie Curie, Sorbonne University,
+ *                           Washington University in St. Louis,
+ *                           Beijing Institute of Technology,
+ *                           The University of Memphis.
  *
  * This file is part of NFD (Named Data Networking Forwarding Daemon).
  * See AUTHORS.md for complete list of NFD authors and contributors.
@@ -20,7 +21,7 @@
  *
  * You should have received a copy of the GNU General Public License along with
  * NFD, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
- **/
+ */
 
 #include "pit-out-record.hpp"
 
@@ -32,5 +33,16 @@
 {
 }
 
+bool
+OutRecord::setIncomingNack(const lp::Nack& nack)
+{
+  if (nack.getInterest().getNonce() != this->getLastNonce()) {
+    return false;
+  }
+
+  m_incomingNack.reset(new lp::NackHeader(nack.getHeader()));
+  return true;
+}
+
 } // namespace pit
 } // namespace nfd
diff --git a/daemon/table/pit-out-record.hpp b/daemon/table/pit-out-record.hpp
index c75e005..4717779 100644
--- a/daemon/table/pit-out-record.hpp
+++ b/daemon/table/pit-out-record.hpp
@@ -1,11 +1,12 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /**
- * Copyright (c) 2014  Regents of the University of California,
- *                     Arizona Board of Regents,
- *                     Colorado State University,
- *                     University Pierre & Marie Curie, Sorbonne University,
- *                     Washington University in St. Louis,
- *                     Beijing Institute of Technology
+ * Copyright (c) 2014-2015,  Regents of the University of California,
+ *                           Arizona Board of Regents,
+ *                           Colorado State University,
+ *                           University Pierre & Marie Curie, Sorbonne University,
+ *                           Washington University in St. Louis,
+ *                           Beijing Institute of Technology,
+ *                           The University of Memphis.
  *
  * This file is part of NFD (Named Data Networking Forwarding Daemon).
  * See AUTHORS.md for complete list of NFD authors and contributors.
@@ -20,7 +21,7 @@
  *
  * You should have received a copy of the GNU General Public License along with
  * NFD, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
- **/
+ */
 
 #ifndef NFD_DAEMON_TABLE_PIT_OUT_RECORD_HPP
 #define NFD_DAEMON_TABLE_PIT_OUT_RECORD_HPP
@@ -30,14 +31,49 @@
 namespace nfd {
 namespace pit {
 
-/** \class OutRecord
- *  \brief contains information about an Interest toward an outgoing face
+/** \brief contains information about an Interest toward an outgoing face
  */
 class OutRecord : public FaceRecord
 {
 public:
   explicit
   OutRecord(shared_ptr<Face> face);
+
+  /** \return last NACK returned by \p getFace()
+   *
+   *  A nullptr return value means the Interest is still pending or has timed out.
+   *  A non-null return value means the last outgoing Interest has been NACKed.
+   */
+  const lp::NackHeader*
+  getIncomingNack() const
+  {
+    return m_incomingNack.get();
+  }
+
+  /** \brief sets a NACK received from \p getFace()
+   *  \return whether incoming NACK is accepted
+   *
+   *  This is invoked in incoming NACK pipeline.
+   *  An incoming NACK is accepted if its Nonce matches \p getLastNonce().
+   *  If accepted, \p nack.getHeader() will be copied,
+   *  and any pointer previously returned by \p .getIncomingNack() .
+   */
+  bool
+  setIncomingNack(const lp::Nack& nack);
+
+  /** \brief clears last NACK
+   *
+   *  This is invoked in outgoing Interest pipeline.
+   *  This invalidates any pointer previously returned by \p .getIncomingNack() .
+   */
+  void
+  clearIncomingNack()
+  {
+    m_incomingNack.reset();
+  }
+
+private:
+  unique_ptr<lp::NackHeader> m_incomingNack;
 };
 
 } // namespace pit
diff --git a/daemon/table/pit.cpp b/daemon/table/pit.cpp
index c21ccdf..959593f 100644
--- a/daemon/table/pit.cpp
+++ b/daemon/table/pit.cpp
@@ -62,7 +62,7 @@
 }
 
 std::pair<shared_ptr<pit::Entry>, bool>
-Pit::insert(const Interest& interest)
+Pit::findOrInsert(const Interest& interest, bool allowInsert)
 {
   // first lookup() the Interest Name in the NameTree, which will creates all
   // the intermedia nodes, starting from the shortest prefix.
@@ -78,13 +78,17 @@
                                   entry->getInterest().getSelectors() == interest.getSelectors();
                          });
   if (it != pitEntries.end()) {
-    return { *it, false };
+    return {*it, false};
+  }
+
+  if (!allowInsert) {
+    return {nullptr, true};
   }
 
   shared_ptr<pit::Entry> entry = make_shared<pit::Entry>(interest);
   nameTreeEntry->insertPitEntry(entry);
   m_nItems++;
-  return { entry, true };
+  return {entry, true};
 }
 
 pit::DataMatchResult
diff --git a/daemon/table/pit.hpp b/daemon/table/pit.hpp
index e9616d5..adab085 100644
--- a/daemon/table/pit.hpp
+++ b/daemon/table/pit.hpp
@@ -58,10 +58,17 @@
   size_t
   size() const;
 
+  /** \brief finds a PIT entry for Interest
+   *  \param interest the Interest
+   *  \return an existing entry with same Name and Selectors; otherwise nullptr
+   */
+  shared_ptr<pit::Entry>
+  find(const Interest& interest) const;
+
   /** \brief inserts a PIT entry for Interest
-   *
-   *  If an entry for exact same name and selectors exists, that entry is returned.
-   *  \return the entry, and true for new entry, false for existing entry
+   *  \param interest the Interest; must be created with make_shared
+   *  \return a new or existing entry with same Name and Selectors,
+   *          and true for new entry, false for existing entry
    */
   std::pair<shared_ptr<pit::Entry>, bool>
   insert(const Interest& interest);
@@ -135,6 +142,18 @@
   };
 
 private:
+  /** \brief finds or inserts a PIT entry for Interest
+   *  \param interest the Interest; must be created with make_shared if allowInsert
+   *  \param allowInsert whether inserting new entry is allowed.
+   *  \return if allowInsert, a new or existing entry with same Name+Selectors,
+   *          and true for new entry, false for existing entry;
+   *          if not allowInsert, an existing entry with same Name+Selectors and false,
+   *          or {nullptr, true} if there's no existing entry
+   */
+  std::pair<shared_ptr<pit::Entry>, bool>
+  findOrInsert(const Interest& interest, bool allowInsert);
+
+private:
   NameTree& m_nameTree;
   size_t m_nItems;
 };
@@ -145,6 +164,18 @@
   return m_nItems;
 }
 
+inline shared_ptr<pit::Entry>
+Pit::find(const Interest& interest) const
+{
+  return const_cast<Pit*>(this)->findOrInsert(interest, false).first;
+}
+
+inline std::pair<shared_ptr<pit::Entry>, bool>
+Pit::insert(const Interest& interest)
+{
+  return this->findOrInsert(interest, true);
+}
+
 inline Pit::const_iterator
 Pit::end() const
 {