fw: broadcast strategy

refs #1243

Change-Id: I9ca2164812ea3e1815fa27b73a68166c93f97b27
diff --git a/daemon/fw/broadcast-strategy.cpp b/daemon/fw/broadcast-strategy.cpp
new file mode 100644
index 0000000..7781377
--- /dev/null
+++ b/daemon/fw/broadcast-strategy.cpp
@@ -0,0 +1,44 @@
+/* -*- 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 "broadcast-strategy.hpp"
+
+namespace nfd {
+namespace fw {
+
+BroadcastStrategy::BroadcastStrategy(Forwarder& forwarder)
+  : Strategy(forwarder)
+{
+}
+
+BroadcastStrategy::~BroadcastStrategy()
+{
+}
+
+void
+BroadcastStrategy::afterReceiveInterest(const Face& inFace,
+                   const Interest& interest,
+                   shared_ptr<fib::Entry> fibEntry,
+                   shared_ptr<pit::Entry> pitEntry)
+{
+  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()) {
+      this->sendInterest(pitEntry, outFace);
+      isPropagated = true;
+    }
+  }
+
+  if (!isPropagated) {
+    this->rebuffPendingInterest(pitEntry);
+  }
+}
+
+} // namespace fw
+} // namespace nfd
diff --git a/daemon/fw/broadcast-strategy.hpp b/daemon/fw/broadcast-strategy.hpp
new file mode 100644
index 0000000..7818e90
--- /dev/null
+++ b/daemon/fw/broadcast-strategy.hpp
@@ -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.
+ */
+
+#ifndef NFD_FW_BROADCAST_STRATEGY_HPP
+#define NFD_FW_BROADCAST_STRATEGY_HPP
+
+#include "strategy.hpp"
+#include "forwarder.hpp"
+
+namespace nfd {
+namespace fw {
+
+/** \class BroadcastStrategy
+ *  \brief a forwarding strategy that forwards Interest
+ *         to all nexthops
+ */
+class BroadcastStrategy : public Strategy
+{
+public:
+  explicit
+  BroadcastStrategy(Forwarder& forwarder);
+  
+  virtual
+  ~BroadcastStrategy();
+  
+  virtual void
+  afterReceiveInterest(const Face& inFace,
+                       const Interest& interest,
+                       shared_ptr<fib::Entry> fibEntry,
+                       shared_ptr<pit::Entry> pitEntry);
+};
+
+} // namespace fw
+} // namespace nfd
+
+#endif // NFD_FW_BROADCAST_STRATEGY_HPP
diff --git a/daemon/fw/strategy.hpp b/daemon/fw/strategy.hpp
index c565bd5..1ecbfc9 100644
--- a/daemon/fw/strategy.hpp
+++ b/daemon/fw/strategy.hpp
@@ -99,7 +99,7 @@
   
 protected: // actions
   /// send Interest to outFace
-  void
+  VIRTUAL_WITH_TESTS void
   sendInterest(shared_ptr<pit::Entry> pitEntry,
                     shared_ptr<Face> outFace);
   
@@ -108,7 +108,7 @@
    *  This shall not be called if the pending Interest has been
    *  forwarded earlier, and does not need to be resent now.
    */
-  void
+  VIRTUAL_WITH_TESTS void
   rebuffPendingInterest(shared_ptr<pit::Entry> pitEntry);
   
 private:
diff --git a/tests/fw/broadcast-strategy.cpp b/tests/fw/broadcast-strategy.cpp
new file mode 100644
index 0000000..3976f62
--- /dev/null
+++ b/tests/fw/broadcast-strategy.cpp
@@ -0,0 +1,88 @@
+/* -*- 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/broadcast-strategy.hpp"
+#include "strategy-tester.hpp"
+#include "../face/dummy-face.hpp"
+
+#include <boost/test/unit_test.hpp>
+
+namespace nfd {
+
+BOOST_AUTO_TEST_SUITE(FwBroadcastStrategy)
+
+BOOST_AUTO_TEST_CASE(ForwardTwo)
+{
+  boost::asio::io_service io;
+  Forwarder forwarder(io);
+  typedef StrategyTester<fw::BroadcastStrategy> BroadcastStrategyTester;
+  BroadcastStrategyTester strategy(forwarder);
+
+  shared_ptr<DummyFace> face1 = make_shared<DummyFace>();
+  shared_ptr<DummyFace> face2 = make_shared<DummyFace>();
+  shared_ptr<DummyFace> face3 = make_shared<DummyFace>();
+  forwarder.addFace(face1);
+  forwarder.addFace(face2);
+  forwarder.addFace(face3);
+
+  Fib& fib = forwarder.getFib();
+  std::pair<shared_ptr<fib::Entry>, bool> fibInsertResult = fib.insert(Name());
+  shared_ptr<fib::Entry> fibEntry = fibInsertResult.first;
+  fibEntry->addNextHop(face1, 0);
+  fibEntry->addNextHop(face2, 0);
+  fibEntry->addNextHop(face3, 0);
+
+  Interest interest(Name("ndn:/H0D6i5fc"));
+  Pit& pit = forwarder.getPit();
+  std::pair<shared_ptr<pit::Entry>, bool> pitInsertResult = pit.insert(interest);
+  shared_ptr<pit::Entry> pitEntry = pitInsertResult.first;
+
+  strategy.afterReceiveInterest(*face3, interest, fibEntry, pitEntry);
+  BOOST_CHECK_EQUAL(strategy.m_rebuffPendingInterestHistory.size(), 0);
+  BOOST_CHECK_EQUAL(strategy.m_sendInterestHistory.size(), 2);
+  bool hasFace1 = false;
+  bool hasFace2 = false;
+  for (std::vector<BroadcastStrategyTester::SendInterestArgs>::iterator it =
+       strategy.m_sendInterestHistory.begin();
+       it != strategy.m_sendInterestHistory.end(); ++it) {
+    if (it->get<1>() == face1) {
+      hasFace1 = true;
+    }
+    if (it->get<1>() == face2) {
+      hasFace2 = true;
+    }
+  }
+  BOOST_CHECK(hasFace1 && hasFace2);
+}
+
+BOOST_AUTO_TEST_CASE(Rebuff)
+{
+  boost::asio::io_service io;
+  Forwarder forwarder(io);
+  typedef StrategyTester<fw::BroadcastStrategy> BroadcastStrategyTester;
+  BroadcastStrategyTester strategy(forwarder);
+
+  shared_ptr<DummyFace> face1 = make_shared<DummyFace>();
+  forwarder.addFace(face1);
+
+  Fib& fib = forwarder.getFib();
+  std::pair<shared_ptr<fib::Entry>, bool> fibInsertResult = fib.insert(Name());
+  shared_ptr<fib::Entry> fibEntry = fibInsertResult.first;
+  fibEntry->addNextHop(face1, 0);
+
+  Interest interest(Name("ndn:/H0D6i5fc"));
+  Pit& pit = forwarder.getPit();
+  std::pair<shared_ptr<pit::Entry>, bool> pitInsertResult = pit.insert(interest);
+  shared_ptr<pit::Entry> pitEntry = pitInsertResult.first;
+
+  strategy.afterReceiveInterest(*face1, interest, fibEntry, pitEntry);
+  BOOST_CHECK_EQUAL(strategy.m_rebuffPendingInterestHistory.size(), 1);
+  BOOST_CHECK_EQUAL(strategy.m_sendInterestHistory.size(), 0);
+}
+
+BOOST_AUTO_TEST_SUITE_END()
+
+} // namespace nfd
diff --git a/tests/fw/strategy-tester.hpp b/tests/fw/strategy-tester.hpp
new file mode 100644
index 0000000..785fd18
--- /dev/null
+++ b/tests/fw/strategy-tester.hpp
@@ -0,0 +1,64 @@
+/* -*- 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_TEST_FW_STRATEGY_TESTER_HPP
+#define NFD_TEST_FW_STRATEGY_TESTER_HPP
+
+#include <boost/tuple/tuple_comparison.hpp>
+#include "fw/strategy.hpp"
+
+namespace nfd {
+
+/** \class StrategyTester
+ *  \brief extends strategy S for unit testing
+ *
+ *  Actions invoked by S are recorded but not passed to forwarder
+ */
+template<typename S>
+class StrategyTester : public S
+{
+public:
+  explicit
+  StrategyTester(Forwarder& forwarder)
+    : S(forwarder)
+  {
+  }
+
+protected:
+  virtual void
+  sendInterest(shared_ptr<pit::Entry> pitEntry,shared_ptr<Face> outFace);
+
+  virtual void
+  rebuffPendingInterest(shared_ptr<pit::Entry> pitEntry);
+
+public:
+  typedef boost::tuple<shared_ptr<pit::Entry>, shared_ptr<Face> > SendInterestArgs;
+  std::vector<SendInterestArgs> m_sendInterestHistory;
+
+  typedef boost::tuple<shared_ptr<pit::Entry> > RebuffPendingInterestArgs;
+  std::vector<RebuffPendingInterestArgs> m_rebuffPendingInterestHistory;
+};
+
+
+template<typename S>
+inline void
+StrategyTester<S>::sendInterest(shared_ptr<pit::Entry> pitEntry,
+                                shared_ptr<Face> outFace)
+{
+  m_sendInterestHistory.push_back(SendInterestArgs(pitEntry, outFace));
+}
+
+template<typename S>
+inline void
+StrategyTester<S>::rebuffPendingInterest(shared_ptr<pit::Entry> pitEntry)
+{
+  m_rebuffPendingInterestHistory.push_back(RebuffPendingInterestArgs(pitEntry));
+}
+
+
+} // namespace nfd
+
+#endif // TEST_FW_STRATEGY_TESTER_HPP