face: InternalForwarderTransport & InternalClientTransport

InternalForwarderTransport and InternalClientTransport are a pair of forwarder-side
and client-side transports that can be connected with each other, so that link-layer
packets sent by one transport can be received by the other.

They are used together with LpFace, GenericLinkService, and ndn::Face to replace
InternalFace and InternalClientFace used by NFD management.

They also replace TopologyForwarderTransport and TopologyClientTransport used by
TopologyTester of forwarding unit tests.

refs #3225

Change-Id: I5b6b579c43dfd0b1b9def5100be2ce516219cb74
diff --git a/tests/daemon/fw/topology-tester.cpp b/tests/daemon/fw/topology-tester.cpp
index 10a0b31..8b94abc 100644
--- a/tests/daemon/fw/topology-tester.cpp
+++ b/tests/daemon/fw/topology-tester.cpp
@@ -31,64 +31,29 @@
 namespace fw {
 namespace tests {
 
+using face::InternalTransportBase;
+using face::InternalForwarderTransport;
+using face::InternalClientTransport;
 using face::LpFaceWrapper;
+using face::GenericLinkService;
 
-TopologyForwarderTransport::TopologyForwarderTransport(
-    const FaceUri& localUri, const FaceUri& remoteUri,
-    ndn::nfd::FaceScope scope, ndn::nfd::LinkType linkType)
-{
-  this->setLocalUri(localUri);
-  this->setRemoteUri(remoteUri);
-  this->setScope(scope);
-  this->setPersistency(ndn::nfd::FACE_PERSISTENCY_PERMANENT);
-  this->setLinkType(linkType);
-}
-
-void
-TopologyForwarderTransport::receiveFromTopology(const Block& packet)
-{
-  Packet p;
-  p.packet = packet;
-  this->receive(std::move(p));
-}
-
-void
-TopologyForwarderTransport::doSend(Packet&& packet)
-{
-  this->emitSignal(afterSend, packet.packet);
-}
-
-void
-TopologyClientTransport::receiveFromTopology(const Block& packet)
-{
-  if (m_receiveCallback) {
-    m_receiveCallback(packet);
-  }
-}
-
-void
-TopologyClientTransport::send(const Block& wire)
-{
-  this->emitSignal(afterSend, wire);
-}
-
-void
-TopologyClientTransport::send(const Block& header, const Block& payload)
-{
-  ndn::EncodingBuffer encoder(header.size() + payload.size(), header.size() + payload.size());
-  encoder.appendByteArray(header.wire(), header.size());
-  encoder.appendByteArray(payload.wire(), payload.size());
-
-  this->send(encoder.block());
-}
-
-TopologyLinkBase::TopologyLinkBase()
+TopologyLink::TopologyLink(const time::nanoseconds& delay)
   : m_isUp(true)
+  , m_delay(delay)
 {
+  BOOST_ASSERT(delay > time::nanoseconds::zero());
+  // zero delay does not work on OSX
 }
 
 void
-TopologyLinkBase::attachTransport(TopologyNode i, TopologyTransportBase* transport)
+TopologyLink::addFace(TopologyNode i, shared_ptr<LpFaceWrapper> face)
+{
+  this->attachTransport(i, dynamic_cast<InternalTransportBase*>(face->getLpFace()->getTransport()));
+  m_faces[i] = face;
+}
+
+void
+TopologyLink::attachTransport(TopologyNode i, InternalTransportBase* transport)
 {
   BOOST_ASSERT(transport != nullptr);
   BOOST_ASSERT(m_transports.count(i) == 0);
@@ -98,7 +63,7 @@
 }
 
 void
-TopologyLinkBase::transmit(TopologyNode i, const Block& packet)
+TopologyLink::transmit(TopologyNode i, const Block& packet)
 {
   if (!m_isUp) {
     return;
@@ -109,49 +74,38 @@
       continue;
     }
 
-    TopologyTransportBase* recipient = p.second;
+    InternalTransportBase* recipient = p.second;
     this->scheduleReceive(recipient, packet);
   }
 }
 
-TopologyLink::TopologyLink(const time::nanoseconds& delay)
-  : m_delay(delay)
-{
-  BOOST_ASSERT(delay > time::nanoseconds::zero());
-  // zero delay does not work on OSX
-}
-
 void
-TopologyLink::addFace(TopologyNode i, shared_ptr<LpFaceWrapper> face)
-{
-  this->attachTransport(i, dynamic_cast<TopologyTransportBase*>(face->getLpFace()->getTransport()));
-  m_faces[i] = face;
-}
-
-void
-TopologyLink::scheduleReceive(TopologyTransportBase* recipient, const Block& packet)
+TopologyLink::scheduleReceive(InternalTransportBase* recipient, const Block& packet)
 {
   scheduler::schedule(m_delay, [packet, recipient] {
-    recipient->receiveFromTopology(packet);
+    recipient->receiveFromLink(packet);
   });
 }
 
-TopologyAppLink::TopologyAppLink(shared_ptr<LpFaceWrapper> face)
-  : m_face(face)
+TopologyAppLink::TopologyAppLink(shared_ptr<LpFaceWrapper> forwarderFace)
+  : m_face(forwarderFace)
+  , m_forwarderTransport(static_cast<InternalForwarderTransport*>(forwarderFace->getLpFace()->getTransport()))
+  , m_clientTransport(make_shared<InternalClientTransport>())
+  , m_client(make_shared<ndn::Face>(m_clientTransport, getGlobalIoService()))
 {
-  this->attachTransport(0, dynamic_cast<TopologyTransportBase*>(face->getLpFace()->getTransport()));
-
-  auto clientTransport = make_shared<TopologyClientTransport>();
-  m_client = make_shared<ndn::Face>(clientTransport, getGlobalIoService());
-  this->attachTransport(1, clientTransport.get());
+  this->recover();
 }
 
 void
-TopologyAppLink::scheduleReceive(TopologyTransportBase* recipient, const Block& packet)
+TopologyAppLink::fail()
 {
-  getGlobalIoService().post([packet, recipient] {
-    recipient->receiveFromTopology(packet);
-  });
+  m_clientTransport->connectToForwarder(nullptr);
+}
+
+void
+TopologyAppLink::recover()
+{
+  m_clientTransport->connectToForwarder(m_forwarderTransport);
 }
 
 TopologyNode
@@ -179,8 +133,8 @@
     Forwarder& forwarder = this->getForwarder(i);
     FaceUri localUri("topology://" + m_forwarderLabels.at(i) + "/" + label);
 
-    auto service = make_unique<face::GenericLinkService>();
-    auto transport = make_unique<TopologyForwarderTransport>(localUri, remoteUri,
+    auto service = make_unique<GenericLinkService>();
+    auto transport = make_unique<InternalForwarderTransport>(localUri, remoteUri,
                      ndn::nfd::FACE_SCOPE_NON_LOCAL, linkType);
     auto face = make_unique<LpFace>(std::move(service), std::move(transport));
     auto faceW = make_shared<LpFaceWrapper>(std::move(face));
@@ -200,8 +154,8 @@
   FaceUri localUri("topology://" + m_forwarderLabels.at(i) + "/local/" + label);
   FaceUri remoteUri("topology://" + m_forwarderLabels.at(i) + "/app/" + label);
 
-  auto service = make_unique<face::GenericLinkService>();
-  auto transport = make_unique<TopologyForwarderTransport>(localUri, remoteUri,
+  auto service = make_unique<GenericLinkService>();
+  auto transport = make_unique<InternalForwarderTransport>(localUri, remoteUri,
                    ndn::nfd::FACE_SCOPE_LOCAL, ndn::nfd::LINK_TYPE_POINT_TO_POINT);
   auto face = make_unique<LpFace>(std::move(service), std::move(transport));
   auto faceW = make_shared<LpFaceWrapper>(std::move(face));
diff --git a/tests/daemon/fw/topology-tester.hpp b/tests/daemon/fw/topology-tester.hpp
index dd6d77d..9a10ad7 100644
--- a/tests/daemon/fw/topology-tester.hpp
+++ b/tests/daemon/fw/topology-tester.hpp
@@ -31,7 +31,7 @@
 #define NFD_TESTS_NFD_FW_TOPOLOGY_TESTER_HPP
 
 #include <ndn-cxx/face.hpp>
-#include <ndn-cxx/transport/transport.hpp>
+#include "face/internal-transport.hpp"
 #include "face/lp-face-wrapper.hpp"
 #include "fw/strategy.hpp"
 #include "tests/test-common.hpp"
@@ -42,84 +42,17 @@
 
 using namespace nfd::tests;
 
-/** \brief abstracts a Transport used in TopologyTester
- */
-class TopologyTransportBase
-{
-public:
-  /** \brief causes the transport to receive a link-layer packet
-   */
-  virtual void
-  receiveFromTopology(const Block& packet) = 0;
-
-  signal::Signal<TopologyTransportBase, Block> afterSend;
-
-protected:
-  DECLARE_SIGNAL_EMIT(afterSend)
-};
-
-/** \brief implements a forwarder-side Transport used in TopologyTester
- */
-class TopologyForwarderTransport : public face::Transport, public TopologyTransportBase
-{
-public:
-  TopologyForwarderTransport(const FaceUri& localUri, const FaceUri& remoteUri,
-                             ndn::nfd::FaceScope scope, ndn::nfd::LinkType linkType);
-
-  virtual void
-  receiveFromTopology(const Block& packet) DECL_OVERRIDE;
-
-protected:
-  virtual void
-  doClose() DECL_OVERRIDE
-  {
-  }
-
-private:
-  virtual void
-  doSend(Packet&& packet) DECL_OVERRIDE;
-};
-
-/** \brief implements a client-side Transport used in TopologyTester
- */
-class TopologyClientTransport : public ndn::Transport, public TopologyTransportBase
-{
-public:
-  virtual void
-  receiveFromTopology(const Block& packet) DECL_OVERRIDE;
-
-  virtual void
-  close() DECL_OVERRIDE
-  {
-  }
-
-  virtual void
-  pause() DECL_OVERRIDE
-  {
-  }
-
-  virtual void
-  resume() DECL_OVERRIDE
-  {
-  }
-
-  virtual void
-  send(const Block& wire) DECL_OVERRIDE;
-
-  virtual void
-  send(const Block& header, const Block& payload) DECL_OVERRIDE;
-};
-
 /** \brief identifies a node (forwarder) in the topology
  */
 typedef size_t TopologyNode;
 
-/** \brief represents a network or local-app link
+/** \brief represents a network link in the topology which connects two or more nodes
  */
-class TopologyLinkBase : noncopyable
+class TopologyLink : noncopyable
 {
 public:
-  TopologyLinkBase();
+  explicit
+  TopologyLink(const time::nanoseconds& delay);
 
   /** \brief fail the link, cause packets to be dropped silently
    */
@@ -137,32 +70,6 @@
     m_isUp = true;
   }
 
-protected:
-  /** \brief attach a Transport onto this link
-   */
-  void
-  attachTransport(TopologyNode i, TopologyTransportBase* transport);
-
-private:
-  void
-  transmit(TopologyNode i, const Block& packet);
-
-  virtual void
-  scheduleReceive(TopologyTransportBase* recipient, const Block& packet) = 0;
-
-protected:
-  bool m_isUp;
-  std::unordered_map<TopologyNode, TopologyTransportBase*> m_transports;
-};
-
-/** \brief represents a network link in the topology which connects two or more nodes
- */
-class TopologyLink : public TopologyLinkBase
-{
-public:
-  explicit
-  TopologyLink(const time::nanoseconds& delay);
-
   void
   addFace(TopologyNode i, shared_ptr<face::LpFaceWrapper> face);
 
@@ -174,22 +81,43 @@
     return *m_faces.at(i);
   }
 
-private:
-  virtual void
-  scheduleReceive(TopologyTransportBase* recipient, const Block& packet) DECL_OVERRIDE;
+protected:
+  /** \brief attach a Transport onto this link
+   */
+  void
+  attachTransport(TopologyNode i, face::InternalTransportBase* transport);
 
 private:
+  void
+  transmit(TopologyNode i, const Block& packet);
+
+  void
+  scheduleReceive(face::InternalTransportBase* recipient, const Block& packet);
+
+private:
+  bool m_isUp;
   time::nanoseconds m_delay;
+  std::unordered_map<TopologyNode, face::InternalTransportBase*> m_transports;
   std::unordered_map<TopologyNode, shared_ptr<face::LpFaceWrapper>> m_faces;
 };
 
 /** \brief represents a link to a local application
  */
-class TopologyAppLink : public TopologyLinkBase
+class TopologyAppLink : noncopyable
 {
 public:
   explicit
-  TopologyAppLink(shared_ptr<face::LpFaceWrapper> face);
+  TopologyAppLink(shared_ptr<face::LpFaceWrapper> forwarderFace);
+
+  /** \brief fail the link, cause packets to be dropped silently
+   */
+  void
+  fail();
+
+  /** \brief recover the link from a failure
+   */
+  void
+  recover();
 
   /** \return face on forwarder side
    */
@@ -208,11 +136,9 @@
   }
 
 private:
-  virtual void
-  scheduleReceive(TopologyTransportBase* recipient, const Block& packet) DECL_OVERRIDE;
-
-private:
-  shared_ptr<face::LpFaceWrapper> m_face;
+  shared_ptr<Face> m_face;
+  face::InternalForwarderTransport* m_forwarderTransport;
+  shared_ptr<face::InternalClientTransport> m_clientTransport;
   shared_ptr<ndn::Face> m_client;
 };