face: simplify and optimize Internal{Forwarder,Client}Transport

Refs: #4528
Change-Id: Ie3246382965640e0d2cb71116b6526e68925887c
diff --git a/tests/daemon/fw/topology-tester.cpp b/tests/daemon/fw/topology-tester.cpp
index c401aef..0e19b57 100644
--- a/tests/daemon/fw/topology-tester.cpp
+++ b/tests/daemon/fw/topology-tester.cpp
@@ -24,6 +24,7 @@
  */
 
 #include "topology-tester.hpp"
+
 #include "daemon/global.hpp"
 #include "face/generic-link-service.hpp"
 
@@ -33,13 +34,19 @@
 namespace fw {
 namespace tests {
 
-using face::InternalTransportBase;
-using face::InternalForwarderTransport;
-using face::InternalClientTransport;
 using face::GenericLinkService;
+using face::InternalClientTransport;
+using face::InternalForwarderTransport;
+
+TopologyLink::NodeTransport::NodeTransport(shared_ptr<Face> f, ReceiveProxy::Callback cb)
+  : face(std::move(f))
+  , transport(dynamic_cast<InternalForwarderTransport*>(face->getTransport()))
+  , proxy(std::move(cb))
+{
+  BOOST_ASSERT(transport != nullptr);
+}
 
 TopologyLink::TopologyLink(time::nanoseconds delay)
-  : m_isUp(true)
 {
   this->setDelay(delay);
 }
@@ -66,19 +73,19 @@
 void
 TopologyLink::addFace(TopologyNode i, shared_ptr<Face> face)
 {
-  BOOST_ASSERT(m_transports.count(i) == 0);
-  auto& nodeTransport = m_transports[i];
+  auto receiveCb = [this, i] (Block&& pkt) { transmit(i, std::move(pkt)); };
 
-  nodeTransport.face = face;
+  auto ret = m_transports.emplace(std::piecewise_construct,
+                                  std::forward_as_tuple(i),
+                                  std::forward_as_tuple(std::move(face), std::move(receiveCb)));
+  BOOST_ASSERT(ret.second);
 
-  nodeTransport.transport = dynamic_cast<InternalTransportBase*>(face->getTransport());
-  BOOST_ASSERT(nodeTransport.transport != nullptr);
-  nodeTransport.transport->afterSend.connect(
-    [this, i] (const Block& packet) { this->transmit(i, packet); });
+  auto& node = ret.first->second;
+  node.transport->setPeer(&node.proxy);
 }
 
 void
-TopologyLink::transmit(TopologyNode i, const Block& packet)
+TopologyLink::transmit(TopologyNode i, Block&& packet)
 {
   if (!m_isUp) {
     return;
@@ -91,22 +98,21 @@
       continue;
     }
 
-    InternalTransportBase* recipient = p.second.transport;
-    this->scheduleReceive(recipient, packet);
+    this->scheduleReceive(p.second.transport, Block{packet});
   }
 }
 
 void
-TopologyLink::scheduleReceive(InternalTransportBase* recipient, const Block& packet)
+TopologyLink::scheduleReceive(face::InternalTransportBase* recipient, Block&& packet)
 {
-  getScheduler().schedule(m_delay, [packet, recipient] {
-    recipient->receiveFromLink(packet);
+  getScheduler().schedule(m_delay, [=, pkt = std::move(packet)] () mutable {
+    recipient->receivePacket(std::move(pkt));
   });
 }
 
 TopologyAppLink::TopologyAppLink(shared_ptr<Face> forwarderFace)
-  : m_face(forwarderFace)
-  , m_forwarderTransport(static_cast<InternalForwarderTransport*>(forwarderFace->getTransport()))
+  : m_face(std::move(forwarderFace))
+  , m_forwarderTransport(static_cast<InternalForwarderTransport*>(m_face->getTransport()))
   , m_clientTransport(make_shared<InternalClientTransport>())
   , m_client(make_shared<ndn::Face>(m_clientTransport, getGlobalIoService()))
 {
@@ -190,7 +196,7 @@
     auto face = make_shared<Face>(std::move(service), std::move(transport));
 
     forwarder.addFace(face);
-    link->addFace(i, face);
+    link->addFace(i, std::move(face));
   }
 
   m_links.push_back(link); // keep a shared_ptr so callers don't have to
@@ -212,7 +218,7 @@
 
   forwarder.addFace(face);
 
-  auto al = make_shared<TopologyAppLink>(face);
+  auto al = make_shared<TopologyAppLink>(std::move(face));
   m_appLinks.push_back(al); // keep a shared_ptr so callers don't have to
   return al;
 }
diff --git a/tests/daemon/fw/topology-tester.hpp b/tests/daemon/fw/topology-tester.hpp
index cbd8fd9..2c8d7d0 100644
--- a/tests/daemon/fw/topology-tester.hpp
+++ b/tests/daemon/fw/topology-tester.hpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /*
- * Copyright (c) 2014-2018,  Regents of the University of California,
+ * Copyright (c) 2014-2019,  Regents of the University of California,
  *                           Arizona Board of Regents,
  *                           Colorado State University,
  *                           University Pierre & Marie Curie, Sorbonne University,
@@ -108,21 +108,48 @@
 
 private:
   void
-  transmit(TopologyNode i, const Block& packet);
+  transmit(TopologyNode i, Block&& packet);
 
   void
-  scheduleReceive(face::InternalTransportBase* recipient, const Block& packet);
+  scheduleReceive(face::InternalTransportBase* recipient, Block&& packet);
 
 private:
-  bool m_isUp;
+  bool m_isUp = true;
   time::nanoseconds m_delay;
 
-  struct NodeTransport
+  class ReceiveProxy : public face::InternalTransportBase
   {
-    face::InternalTransportBase* transport;
+  public:
+    using Callback = std::function<void(Block&&)>;
+
+    explicit
+    ReceiveProxy(Callback cb)
+      : m_cb(std::move(cb))
+    {
+    }
+
+    void
+    receivePacket(Block&& packet) final
+    {
+      m_cb(std::move(packet));
+    }
+
+  private:
+    Callback m_cb;
+  };
+
+  class NodeTransport
+  {
+  public:
+    NodeTransport(shared_ptr<Face> face, ReceiveProxy::Callback receiveCallback);
+
+  public:
     shared_ptr<Face> face;
+    face::InternalForwarderTransport* transport;
+    ReceiveProxy proxy;
     std::set<TopologyNode> blockedDestinations;
   };
+
   std::unordered_map<TopologyNode, NodeTransport> m_transports;
 };