face: Refactor Face as LinkService+Transport
LpFace: new Face implementation
LpFaceWrapper: allows new Face system to work with old Face system
Eventually, LpFace will be renamed to Face and LpFaceWrapper will be removed.
refs #3088 #3179
Change-Id: Ia4ad7c84631e65b444d4f24e1d7593392927c8db
diff --git a/.waf-tools/compiler-features.py b/.waf-tools/compiler-features.py
index 1fd529e..8fa6aa7 100644
--- a/.waf-tools/compiler-features.py
+++ b/.waf-tools/compiler-features.py
@@ -23,5 +23,41 @@
define_name='HAVE_CXX_OVERRIDE',
features='cxx', mandatory=False)
+FINAL = '''
+class Base
+{
+ virtual void
+ f(int a);
+};
+
+class Derived : public Base
+{
+ virtual void
+ f(int a) final;
+};
+'''
+
+@conf
+def check_final(self):
+ self.check_cxx(msg='Checking for final specifier on method',
+ fragment=FINAL,
+ define_name='HAVE_CXX_FINAL',
+ features='cxx', mandatory=False)
+
+CLASS_FINAL = '''
+class A final
+{
+};
+'''
+
+@conf
+def check_class_final(self):
+ self.check_cxx(msg='Checking for final specifier on class',
+ fragment=CLASS_FINAL,
+ define_name='HAVE_CXX_CLASS_FINAL',
+ features='cxx', mandatory=False)
+
def configure(conf):
conf.check_override()
+ conf.check_final()
+ conf.check_class_final()
diff --git a/common.hpp b/common.hpp
index 3e4f44f..c64dd37 100644
--- a/common.hpp
+++ b/common.hpp
@@ -41,7 +41,8 @@
#endif
/** \def DECL_OVERRIDE
- * \brief expands to 'override' if compiler supports this feature, otherwise expands to nothing
+ * \brief expands to 'override' if compiler supports 'override' specifier,
+ * otherwise expands to nothing
*/
#ifdef HAVE_CXX_OVERRIDE
#define DECL_OVERRIDE override
@@ -49,6 +50,26 @@
#define DECL_OVERRIDE
#endif
+/** \def DECL_FINAL
+ * \brief expands to 'final' if compiler supports 'final' specifier on method,
+ * otherwise expands to nothing
+ */
+#ifdef HAVE_CXX_FINAL
+#define DECL_FINAL final
+#else
+#define DECL_FINAL
+#endif
+
+/** \def DECL_CLASS_FINAL
+ * \brief expands to 'final' if compiler supports 'final' specifier on class,
+ * otherwise expands to nothing
+ */
+#ifdef HAVE_CXX_CLASS_FINAL
+#define DECL_CLASS_FINAL final
+#else
+#define DECL_CLASS_FINAL
+#endif
+
#include <cstddef>
#include <cstdint>
#include <functional>
@@ -67,6 +88,7 @@
#include <ndn-cxx/data.hpp>
#include <ndn-cxx/name.hpp>
#include <ndn-cxx/encoding/block.hpp>
+#include <ndn-cxx/lp/nack.hpp>
#include <ndn-cxx/util/backports.hpp>
#include <ndn-cxx/util/face-uri.hpp>
#include <ndn-cxx/util/signal.hpp>
@@ -117,6 +139,7 @@
using namespace ndn::tlv;
} // namespace tlv
+namespace lp = ndn::lp;
namespace name = ndn::name;
namespace time = ndn::time;
namespace signal = ndn::util::signal;
diff --git a/daemon/face/face-log.hpp b/daemon/face/face-log.hpp
new file mode 100644
index 0000000..6e0b3fd
--- /dev/null
+++ b/daemon/face/face-log.hpp
@@ -0,0 +1,93 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * 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.
+ *
+ * NFD is free software: you can redistribute it and/or modify it under the terms
+ * of the GNU General Public License as published by the Free Software Foundation,
+ * either version 3 of the License, or (at your option) any later version.
+ *
+ * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
+ * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
+ * PURPOSE. See the GNU General Public License for more details.
+ *
+ * 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_FACE_FACE_LOG_HPP
+#define NFD_DAEMON_FACE_FACE_LOG_HPP
+
+#include "core/logger.hpp"
+
+namespace nfd {
+namespace face {
+
+/** \brief for internal use by FaceLogging macros
+ *
+ * FaceLogHelper wraps a Face, LinkService, or Transport object.
+ *
+ * std::ostream& operator<<(std::ostream& os, const FaceLogHelper<T>& flh)
+ * should be specialized to print "[id=888,local=scheme://local/uri,remote=scheme://remote/uri] "
+ * which appears as part of the log message.
+ */
+template<typename T>
+class FaceLogHelper
+{
+public:
+ explicit
+ FaceLogHelper(const T& obj1)
+ : obj(obj1)
+ {
+ }
+
+public:
+ const T& obj;
+};
+
+/** \defgroup FaceLogging Face logging macros
+ *
+ * These macros augment the log message with some face-specific information,
+ * such as the face ID, that are useful to distinguish which face produced the
+ * message. It is strongly recommended to use these macros instead of the
+ * generic ones for all logging inside Face, LinkService, Transport subclasses.
+ * @{
+ */
+
+#define NFD_LOG_FACE(level, msg) NFD_LOG_##level( \
+ ::nfd::face::FaceLogHelper< \
+ typename std::remove_cv< \
+ typename std::remove_reference<decltype(*this)>::type \
+ >::type \
+ >(*this) \
+ << msg)
+
+/** \brief Log a message at TRACE level */
+#define NFD_LOG_FACE_TRACE(msg) NFD_LOG_FACE(TRACE, msg)
+
+/** \brief Log a message at DEBUG level */
+#define NFD_LOG_FACE_DEBUG(msg) NFD_LOG_FACE(DEBUG, msg)
+
+/** \brief Log a message at INFO level */
+#define NFD_LOG_FACE_INFO(msg) NFD_LOG_FACE(INFO, msg)
+
+/** \brief Log a message at WARN level */
+#define NFD_LOG_FACE_WARN(msg) NFD_LOG_FACE(WARN, msg)
+
+/** \brief Log a message at ERROR level */
+#define NFD_LOG_FACE_ERROR(msg) NFD_LOG_FACE(ERROR, msg)
+
+/** @} */
+
+} // namespace face
+} // namespace nfd
+
+#endif // NFD_DAEMON_FACE_FACE_LOG_HPP
diff --git a/daemon/face/face.hpp b/daemon/face/face.hpp
index a5923d2..9c2f2e6 100644
--- a/daemon/face/face.hpp
+++ b/daemon/face/face.hpp
@@ -29,6 +29,7 @@
#include "common.hpp"
#include "core/logger.hpp"
#include "face-counters.hpp"
+#include "face-log.hpp"
#include <ndn-cxx/management/nfd-face-status.hpp>
@@ -82,12 +83,18 @@
/// fires when a Data is received
signal::Signal<Face, Data> onReceiveData;
+ /// fires when a Nack is received
+ signal::Signal<Face, lp::Nack> onReceiveNack;
+
/// fires when an Interest is sent out
signal::Signal<Face, Interest> onSendInterest;
/// fires when a Data is sent out
signal::Signal<Face, Data> onSendData;
+ /// fires when a Nack is sent out
+ signal::Signal<Face, lp::Nack> onSendNack;
+
/// fires when face disconnects or fails to perform properly
signal::Signal<Face, std::string/*reason*/> onFail;
@@ -99,6 +106,12 @@
virtual void
sendData(const Data& data) = 0;
+ /// send a Nack
+ virtual void
+ sendNack(const ndn::lp::Nack& nack)
+ {
+ }
+
/** \brief Close the face
*
* This terminates all communication on the face and cause
@@ -133,6 +146,10 @@
ndn::nfd::FacePersistency
getPersistency() const;
+ // 'virtual' to allow override in LpFaceWrapper
+ virtual void
+ setPersistency(ndn::nfd::FacePersistency persistency);
+
/** \brief Get whether packets sent by this face may reach multiple peers
*/
bool
@@ -145,7 +162,8 @@
virtual bool
isUp() const;
- const FaceCounters&
+ // 'virtual' to allow override in LpFaceWrapper
+ virtual const FaceCounters&
getCounters() const;
/** \return a FaceUri that represents the remote endpoint
@@ -169,10 +187,6 @@
virtual ndn::nfd::FaceStatus
getFaceStatus() const;
-PUBLIC_WITH_TESTS_ELSE_PROTECTED:
- void
- setPersistency(ndn::nfd::FacePersistency persistency);
-
protected:
bool
decodeAndDispatchInput(const Block& element);
@@ -187,12 +201,14 @@
DECLARE_SIGNAL_EMIT(onReceiveInterest)
DECLARE_SIGNAL_EMIT(onReceiveData)
+ DECLARE_SIGNAL_EMIT(onReceiveNack)
DECLARE_SIGNAL_EMIT(onSendInterest)
DECLARE_SIGNAL_EMIT(onSendData)
+ DECLARE_SIGNAL_EMIT(onSendNack)
-private:
// this method should be used only by the FaceTable
- void
+ // 'virtual' to allow override in LpFaceWrapper
+ virtual void
setId(FaceId faceId);
private:
@@ -282,38 +298,15 @@
return m_localUri;
}
-
-/** \defgroup FaceLogging Face logging macros
- *
- * These macros augment the log message with some face-specific information,
- * such as the face ID, that are useful to distinguish which face produced the
- * message. It is strongly recommended to use these macros instead of the
- * generic ones for all logging inside Face subclasses.
- * @{
- */
-
-#define NFD_LOG_FACE(level, msg) \
- NFD_LOG_##level("[id=" << this->getId() << \
- ",local=" << this->getLocalUri() << \
- ",remote=" << this->getRemoteUri() << \
- "] " << msg)
-
-/** \brief Log a message at TRACE level */
-#define NFD_LOG_FACE_TRACE(msg) NFD_LOG_FACE(TRACE, msg)
-
-/** \brief Log a message at DEBUG level */
-#define NFD_LOG_FACE_DEBUG(msg) NFD_LOG_FACE(DEBUG, msg)
-
-/** \brief Log a message at INFO level */
-#define NFD_LOG_FACE_INFO(msg) NFD_LOG_FACE(INFO, msg)
-
-/** \brief Log a message at WARN level */
-#define NFD_LOG_FACE_WARN(msg) NFD_LOG_FACE(WARN, msg)
-
-/** \brief Log a message at ERROR level */
-#define NFD_LOG_FACE_ERROR(msg) NFD_LOG_FACE(ERROR, msg)
-
-/** @} */
+template<typename T>
+typename std::enable_if<std::is_base_of<Face, T>::value, std::ostream&>::type
+operator<<(std::ostream& os, const face::FaceLogHelper<T>& flh)
+{
+ const Face& face = flh.obj;
+ os << "[id=" << face.getId() << ",local=" << face.getLocalUri() <<
+ ",remote=" << face.getRemoteUri() << "] ";
+ return os;
+}
} // namespace nfd
diff --git a/daemon/face/generic-link-service.cpp b/daemon/face/generic-link-service.cpp
new file mode 100644
index 0000000..79010e6
--- /dev/null
+++ b/daemon/face/generic-link-service.cpp
@@ -0,0 +1,93 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * 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.
+ *
+ * NFD is free software: you can redistribute it and/or modify it under the terms
+ * of the GNU General Public License as published by the Free Software Foundation,
+ * either version 3 of the License, or (at your option) any later version.
+ *
+ * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
+ * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
+ * PURPOSE. See the GNU General Public License for more details.
+ *
+ * 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 "generic-link-service.hpp"
+
+namespace nfd {
+namespace face {
+
+NFD_LOG_INIT("GenericLinkService");
+
+void
+GenericLinkService::doSendInterest(const Interest& interest)
+{
+ lp::Packet lpPacket(interest.wireEncode());
+ Transport::Packet packet;
+ packet.packet = lpPacket.wireEncode();
+ sendPacket(std::move(packet));
+}
+
+void
+GenericLinkService::doSendData(const Data& data)
+{
+ lp::Packet lpPacket(data.wireEncode());
+ Transport::Packet packet;
+ packet.packet = lpPacket.wireEncode();
+ sendPacket(std::move(packet));
+}
+
+void
+GenericLinkService::doSendNack(const lp::Nack& nack)
+{
+ lp::Packet lpPacket(nack.getInterest().wireEncode());
+ lpPacket.add<lp::NackField>(nack.getHeader());
+ Transport::Packet packet;
+ packet.packet = lpPacket.wireEncode();
+ sendPacket(std::move(packet));
+}
+
+void
+GenericLinkService::doReceivePacket(Transport::Packet&& packet)
+{
+ lp::Packet lpPacket(packet.packet);
+ ndn::Buffer::const_iterator fragBegin, fragEnd;
+ std::tie(fragBegin, fragEnd) = lpPacket.get<lp::FragmentField>();
+ Block netPacket(&*fragBegin, std::distance(fragBegin, fragEnd));
+
+ // Forwarding expects Interest and Data to be created with make_shared,
+ // but has no such requirement on Nack.
+ switch (netPacket.type()) {
+ case tlv::Interest: {
+ auto interest = make_shared<Interest>(netPacket);
+ if (lpPacket.has<lp::NackField>()) {
+ lp::Nack nack(std::move(*interest));
+ nack.setHeader(lpPacket.get<lp::NackField>());
+ receiveNack(nack);
+ }
+ else {
+ receiveInterest(*interest);
+ }
+ break;
+ }
+ case tlv::Data: {
+ auto data = make_shared<Data>(netPacket);
+ receiveData(*data);
+ break;
+ }
+ }
+}
+
+} // namespace face
+} // namespace nfd
diff --git a/daemon/face/generic-link-service.hpp b/daemon/face/generic-link-service.hpp
new file mode 100644
index 0000000..8d91f88
--- /dev/null
+++ b/daemon/face/generic-link-service.hpp
@@ -0,0 +1,70 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * 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.
+ *
+ * NFD is free software: you can redistribute it and/or modify it under the terms
+ * of the GNU General Public License as published by the Free Software Foundation,
+ * either version 3 of the License, or (at your option) any later version.
+ *
+ * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
+ * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
+ * PURPOSE. See the GNU General Public License for more details.
+ *
+ * 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_FACE_GENERIC_LINK_SERVICE_HPP
+#define NFD_DAEMON_FACE_GENERIC_LINK_SERVICE_HPP
+
+#include "common.hpp"
+#include "core/logger.hpp"
+
+#include "link-service.hpp"
+
+#include <ndn-cxx/lp/packet.hpp>
+
+namespace nfd {
+namespace face {
+
+/** \brief generic LinkService
+ */
+class GenericLinkService : public LinkService
+{
+private: // send path entrypoint
+ /** \brief sends Interest
+ */
+ void
+ doSendInterest(const Interest& interest) DECL_OVERRIDE;
+
+ /** \brief sends Data
+ */
+ void
+ doSendData(const Data& data) DECL_OVERRIDE;
+
+ /** \brief sends Nack
+ * This class does not send out a Nack.
+ */
+ void
+ doSendNack(const ndn::lp::Nack& nack) DECL_OVERRIDE;
+
+private: // receive path entrypoint
+ /** \brief receives Packet
+ */
+ void
+ doReceivePacket(Transport::Packet&& packet) DECL_OVERRIDE;
+};
+
+} // namespace face
+} // namespace nfd
+
+#endif // NFD_DAEMON_FACE_GENERIC_LINK_SERVICE_HPP
\ No newline at end of file
diff --git a/daemon/face/link-service.cpp b/daemon/face/link-service.cpp
new file mode 100644
index 0000000..3f19388
--- /dev/null
+++ b/daemon/face/link-service.cpp
@@ -0,0 +1,135 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * 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.
+ *
+ * NFD is free software: you can redistribute it and/or modify it under the terms
+ * of the GNU General Public License as published by the Free Software Foundation,
+ * either version 3 of the License, or (at your option) any later version.
+ *
+ * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
+ * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
+ * PURPOSE. See the GNU General Public License for more details.
+ *
+ * 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 "link-service.hpp"
+#include "lp-face.hpp"
+#include "transport.hpp"
+
+namespace nfd {
+namespace face {
+
+NFD_LOG_INIT("LinkService");
+
+LinkService::LinkService()
+ : m_face(nullptr)
+ , m_transport(nullptr)
+ , m_counters(nullptr)
+{
+}
+
+LinkService::~LinkService()
+{
+}
+
+void
+LinkService::setFaceAndTransport(LpFace& face, Transport& transport)
+{
+ BOOST_ASSERT(m_face == nullptr);
+ BOOST_ASSERT(m_transport == nullptr);
+
+ m_face = &face;
+ m_transport = &transport;
+ m_counters = &m_face->getMutableCounters();
+}
+
+void
+LinkService::sendInterest(const Interest& interest)
+{
+ BOOST_ASSERT(m_transport != nullptr);
+ NFD_LOG_FACE_TRACE(__func__);
+
+ ++m_counters->getNOutInterests();
+
+ doSendInterest(interest);
+}
+
+void
+LinkService::sendData(const Data& data)
+{
+ BOOST_ASSERT(m_transport != nullptr);
+ NFD_LOG_FACE_TRACE(__func__);
+
+ ++m_counters->getNOutDatas();
+
+ doSendData(data);
+}
+
+void
+LinkService::sendNack(const ndn::lp::Nack& nack)
+{
+ BOOST_ASSERT(m_transport != nullptr);
+ NFD_LOG_FACE_TRACE(__func__);
+
+ // TODO#3177 increment counter
+
+ doSendNack(nack);
+}
+
+void
+LinkService::receiveInterest(const Interest& interest)
+{
+ NFD_LOG_FACE_TRACE(__func__);
+
+ ++m_counters->getNInInterests();
+
+ afterReceiveInterest(interest);
+}
+
+void
+LinkService::receiveData(const Data& data)
+{
+ NFD_LOG_FACE_TRACE(__func__);
+
+ ++m_counters->getNInDatas();
+
+ afterReceiveData(data);
+}
+
+void
+LinkService::receiveNack(const ndn::lp::Nack& nack)
+{
+ NFD_LOG_FACE_TRACE(__func__);
+
+ // TODO#3177 increment counter
+
+ afterReceiveNack(nack);
+}
+
+std::ostream&
+operator<<(std::ostream& os, const FaceLogHelper<LinkService>& flh)
+{
+ const LpFace* face = flh.obj.getFace();
+ if (face == nullptr) {
+ os << "[id=0,local=unknown,remote=unknown] ";
+ }
+ else {
+ os << "[id=" << face->getId() << ",local=" << face->getLocalUri()
+ << ",remote=" << face->getRemoteUri() << "] ";
+ }
+ return os;
+}
+
+} // namespace face
+} // namespace nfd
diff --git a/daemon/face/link-service.hpp b/daemon/face/link-service.hpp
new file mode 100644
index 0000000..1b7bda8
--- /dev/null
+++ b/daemon/face/link-service.hpp
@@ -0,0 +1,198 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * 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.
+ *
+ * NFD is free software: you can redistribute it and/or modify it under the terms
+ * of the GNU General Public License as published by the Free Software Foundation,
+ * either version 3 of the License, or (at your option) any later version.
+ *
+ * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
+ * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
+ * PURPOSE. See the GNU General Public License for more details.
+ *
+ * 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_FACE_LINK_SERVICE_HPP
+#define NFD_DAEMON_FACE_LINK_SERVICE_HPP
+
+#include "transport.hpp"
+#include "face-log.hpp"
+
+namespace nfd {
+namespace face {
+
+class LpFace;
+
+/** \brief the upper part of an LpFace
+ * \sa LpFace
+ */
+class LinkService : noncopyable
+{
+public:
+ LinkService();
+
+ virtual
+ ~LinkService();
+
+ /** \brief set Face and Transport for LinkService
+ * \pre setFaceAndTransport has not been called
+ */
+ void
+ setFaceAndTransport(LpFace& face, Transport& transport);
+
+ /** \return Face to which this LinkService is attached
+ */
+ const LpFace*
+ getFace() const;
+
+ /** \return Transport to which this LinkService is attached
+ */
+ const Transport*
+ getTransport() const;
+
+ /** \return Transport to which this LinkService is attached
+ */
+ Transport*
+ getTransport();
+
+public: // upper interface to be used by forwarding
+ /** \brief send Interest
+ * \pre setTransport has been called
+ */
+ void
+ sendInterest(const Interest& interest);
+
+ /** \brief send Data
+ * \pre setTransport has been called
+ */
+ void
+ sendData(const Data& data);
+
+ /** \brief send Nack
+ * \pre setTransport has been called
+ */
+ void
+ sendNack(const ndn::lp::Nack& nack);
+
+ /** \brief signals on Interest received
+ */
+ signal::Signal<LinkService, Interest> afterReceiveInterest;
+
+ /** \brief signals on Data received
+ */
+ signal::Signal<LinkService, Data> afterReceiveData;
+
+ /** \brief signals on Nack received
+ */
+ signal::Signal<LinkService, lp::Nack> afterReceiveNack;
+
+private: // upper interface to be overridden in subclass (send path entrypoint)
+ /** \brief performs LinkService specific operations to send an Interest
+ */
+ virtual void
+ doSendInterest(const Interest& interest) = 0;
+
+ /** \brief performs LinkService specific operations to send a Data
+ */
+ virtual void
+ doSendData(const Data& data) = 0;
+
+ /** \brief performs LinkService specific operations to send a Nack
+ */
+ virtual void
+ doSendNack(const lp::Nack& nack) = 0;
+
+protected: // upper interface to be invoked in subclass (receive path termination)
+ /** \brief delivers received Interest to forwarding
+ */
+ void
+ receiveInterest(const Interest& interest);
+
+ /** \brief delivers received Data to forwarding
+ */
+ void
+ receiveData(const Data& data);
+
+ /** \brief delivers received Nack to forwarding
+ */
+ void
+ receiveNack(const lp::Nack& nack);
+
+public: // lower interface to be invoked by Transport
+ /** \brief performs LinkService specific operations to receive a lower-layer packet
+ */
+ void
+ receivePacket(Transport::Packet&& packet);
+
+protected: // lower interface to be invoked in subclass (send path termination)
+ /** \brief sends a lower-layer packet via Transport
+ */
+ void
+ sendPacket(Transport::Packet&& packet);
+
+private: // lower interface to be overridden in subclass
+ virtual void
+ doReceivePacket(Transport::Packet&& packet) = 0;
+
+private:
+ LpFace* m_face;
+ Transport* m_transport;
+ NetworkLayerCounters* m_counters; // TODO#3177 change into NetCounters
+};
+
+inline const LpFace*
+LinkService::getFace() const
+{
+ return m_face;
+}
+
+inline const Transport*
+LinkService::getTransport() const
+{
+ return m_transport;
+}
+
+inline Transport*
+LinkService::getTransport()
+{
+ return m_transport;
+}
+
+inline void
+LinkService::receivePacket(Transport::Packet&& packet)
+{
+ doReceivePacket(std::move(packet));
+}
+
+inline void
+LinkService::sendPacket(Transport::Packet&& packet)
+{
+ m_transport->send(std::move(packet));
+}
+
+std::ostream&
+operator<<(std::ostream& os, const FaceLogHelper<LinkService>& flh);
+
+template<typename T>
+typename std::enable_if<std::is_base_of<LinkService, T>::value &&
+ !std::is_same<LinkService, T>::value, std::ostream&>::type
+operator<<(std::ostream& os, const FaceLogHelper<T>& flh)
+{
+ return os << FaceLogHelper<LinkService>(flh.obj);
+}
+
+} // namespace face
+} // namespace nfd
+
+#endif // NFD_DAEMON_FACE_LINK_SERVICE_HPP
diff --git a/daemon/face/lp-face-wrapper.cpp b/daemon/face/lp-face-wrapper.cpp
new file mode 100644
index 0000000..920997a
--- /dev/null
+++ b/daemon/face/lp-face-wrapper.cpp
@@ -0,0 +1,102 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * 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.
+ *
+ * NFD is free software: you can redistribute it and/or modify it under the terms
+ * of the GNU General Public License as published by the Free Software Foundation,
+ * either version 3 of the License, or (at your option) any later version.
+ *
+ * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
+ * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
+ * PURPOSE. See the GNU General Public License for more details.
+ *
+ * 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 "lp-face-wrapper.hpp"
+
+namespace nfd {
+namespace face {
+
+LpFaceWrapper::LpFaceWrapper(unique_ptr<LpFace> face)
+ : Face(face->getRemoteUri(), face->getLocalUri(),
+ face->getScope() == ndn::nfd::FACE_SCOPE_LOCAL,
+ face->getLinkType() == ndn::nfd::LINK_TYPE_MULTI_ACCESS)
+ , m_face(std::move(face))
+{
+ this->Face::setPersistency(m_face->getPersistency());
+ m_face->afterReceiveInterest.connect(bind(&LpFaceWrapper::dispatchInterest, this, _1));
+ m_face->afterReceiveData.connect(bind(&LpFaceWrapper::dispatchData, this, _1));
+ m_face->afterReceiveNack.connect(bind(&LpFaceWrapper::dispatchNack, this, _1));
+ m_face->afterStateChange.connect(bind(&LpFaceWrapper::handleStateChange, this, _1, _2));
+}
+
+void
+LpFaceWrapper::setPersistency(ndn::nfd::FacePersistency persistency)
+{
+ this->Face::setPersistency(persistency);
+ m_face->setPersistency(persistency);
+}
+
+void
+LpFaceWrapper::setId(nfd::FaceId faceId)
+{
+ this->Face::setId(faceId);
+
+ FaceId lpId = static_cast<FaceId>(faceId);
+ if (faceId == nfd::INVALID_FACEID) {
+ lpId = INVALID_FACEID;
+ }
+ m_face->setId(lpId);
+}
+
+void
+LpFaceWrapper::dispatchInterest(const Interest& interest)
+{
+ this->emitSignal(onReceiveInterest, interest);
+}
+
+void
+LpFaceWrapper::dispatchData(const Data& data)
+{
+ this->emitSignal(onReceiveData, data);
+}
+
+void
+LpFaceWrapper::dispatchNack(const ndn::lp::Nack& nack)
+{
+ this->emitSignal(onReceiveNack, nack);
+}
+
+void
+LpFaceWrapper::handleStateChange(FaceState oldState, FaceState newState)
+{
+ if (newState != FaceState::CLOSED) {
+ return;
+ }
+
+ switch (oldState) {
+ case FaceState::CLOSING:
+ this->fail("LpFace closed");
+ break;
+ case FaceState::FAILED:
+ this->fail("LpFace failed");
+ break;
+ default:
+ BOOST_ASSERT(false);
+ break;
+ }
+}
+
+} // namespace face
+} // namespace nfd
diff --git a/daemon/face/lp-face-wrapper.hpp b/daemon/face/lp-face-wrapper.hpp
new file mode 100644
index 0000000..084094f
--- /dev/null
+++ b/daemon/face/lp-face-wrapper.hpp
@@ -0,0 +1,140 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * 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.
+ *
+ * NFD is free software: you can redistribute it and/or modify it under the terms
+ * of the GNU General Public License as published by the Free Software Foundation,
+ * either version 3 of the License, or (at your option) any later version.
+ *
+ * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
+ * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
+ * PURPOSE. See the GNU General Public License for more details.
+ *
+ * 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_FACE_LP_FACE_WRAPPER_HPP
+#define NFD_DAEMON_FACE_LP_FACE_WRAPPER_HPP
+
+#include "common.hpp"
+#include "face.hpp"
+#include "lp-face.hpp"
+
+namespace nfd {
+namespace face {
+
+/** \brief an adaptor to provide old Face APIs from an LpFace
+ * \sa LpFace
+ * \note When LpFace is adapted by LpFaceWrapper,
+ * FaceId and counters will come from old Face rather than LpFace.
+ */
+class LpFaceWrapper : public Face
+{
+public:
+ explicit
+ LpFaceWrapper(unique_ptr<LpFace> face);
+
+ LpFace*
+ getLpFace();
+
+ virtual void
+ sendInterest(const Interest& interest) DECL_OVERRIDE;
+
+ virtual void
+ sendData(const Data& data) DECL_OVERRIDE;
+
+ virtual void
+ sendNack(const lp::Nack& nack) DECL_OVERRIDE;
+
+ virtual void
+ close() DECL_OVERRIDE;
+
+ virtual bool
+ isUp() const DECL_OVERRIDE;
+
+ virtual void
+ setPersistency(ndn::nfd::FacePersistency persistency) DECL_OVERRIDE;
+
+ virtual const FaceCounters&
+ getCounters() const DECL_OVERRIDE;
+
+protected:
+ virtual void
+ setId(nfd::FaceId faceId) DECL_OVERRIDE;
+
+private:
+ void
+ dispatchInterest(const Interest& interest);
+
+ void
+ dispatchData(const Data& data);
+
+ void
+ dispatchNack(const lp::Nack& nack);
+
+ void
+ handleStateChange(FaceState oldState, FaceState newState);
+
+private:
+ unique_ptr<LpFace> m_face;
+};
+
+inline LpFace*
+LpFaceWrapper::getLpFace()
+{
+ return m_face.get();
+}
+
+inline void
+LpFaceWrapper::sendInterest(const Interest& interest)
+{
+ this->emitSignal(onSendInterest, interest);
+ m_face->sendInterest(interest);
+}
+
+inline void
+LpFaceWrapper::sendData(const Data& data)
+{
+ this->emitSignal(onSendData, data);
+ m_face->sendData(data);
+}
+
+inline void
+LpFaceWrapper::sendNack(const lp::Nack& nack)
+{
+ this->emitSignal(onSendNack, nack);
+ m_face->sendNack(nack);
+}
+
+inline void
+LpFaceWrapper::close()
+{
+ m_face->close();
+}
+
+inline bool
+LpFaceWrapper::isUp() const
+{
+ return m_face->getState() == FaceState::UP;
+}
+
+inline const FaceCounters&
+LpFaceWrapper::getCounters() const
+{
+ return m_face->getCounters();
+}
+
+} // namespace face
+} // namespace nfd
+
+#endif // NFD_DAEMON_FACE_LP_FACE_WRAPPER_HPP
diff --git a/daemon/face/lp-face.cpp b/daemon/face/lp-face.cpp
new file mode 100644
index 0000000..0b2e826
--- /dev/null
+++ b/daemon/face/lp-face.cpp
@@ -0,0 +1,45 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * 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.
+ *
+ * NFD is free software: you can redistribute it and/or modify it under the terms
+ * of the GNU General Public License as published by the Free Software Foundation,
+ * either version 3 of the License, or (at your option) any later version.
+ *
+ * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
+ * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
+ * PURPOSE. See the GNU General Public License for more details.
+ *
+ * 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 "lp-face.hpp"
+
+namespace nfd {
+namespace face {
+
+LpFace::LpFace(unique_ptr<LinkService> service, unique_ptr<Transport> transport)
+ : afterReceiveInterest(service->afterReceiveInterest)
+ , afterReceiveData(service->afterReceiveData)
+ , afterReceiveNack(service->afterReceiveNack)
+ , afterStateChange(transport->afterStateChange)
+ , m_id(INVALID_FACEID)
+ , m_service(std::move(service))
+ , m_transport(std::move(transport))
+{
+ m_service->setFaceAndTransport(*this, *m_transport);
+ m_transport->setFaceAndLinkService(*this, *m_service);
+}
+
+} // namespace face
+} // namespace nfd
diff --git a/daemon/face/lp-face.hpp b/daemon/face/lp-face.hpp
new file mode 100644
index 0000000..d825928
--- /dev/null
+++ b/daemon/face/lp-face.hpp
@@ -0,0 +1,311 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * 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.
+ *
+ * NFD is free software: you can redistribute it and/or modify it under the terms
+ * of the GNU General Public License as published by the Free Software Foundation,
+ * either version 3 of the License, or (at your option) any later version.
+ *
+ * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
+ * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
+ * PURPOSE. See the GNU General Public License for more details.
+ *
+ * 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_LP_FACE_HPP
+#define NFD_DAEMON_LP_FACE_HPP
+
+#include "transport.hpp"
+#include "link-service.hpp"
+#include "face-log.hpp"
+
+namespace nfd {
+namespace face {
+
+/** \brief identifies a face
+ */
+typedef uint64_t FaceId;
+
+/// indicates an invalid FaceId
+const FaceId INVALID_FACEID = 0;
+/// identifies the InternalFace used in management
+const FaceId FACEID_INTERNAL_FACE = 1;
+/// identifies a packet comes from the ContentStore, in LocalControlHeader incomingFaceId
+const FaceId FACEID_CONTENT_STORE = 254;
+/// identifies the NullFace that drops every packet
+const FaceId FACEID_NULL = 255;
+/// upper bound of reserved FaceIds
+const FaceId FACEID_RESERVED_MAX = 255;
+
+/** \brief indicates the state of a face
+ */
+typedef TransportState FaceState;
+
+/** \brief generalization of a network interface
+ *
+ * A face generalizes a network interface.
+ * It provides network-layer packet delivery services on a physical interface,
+ * an overlay tunnel, or a link to a local application, with best-effort.
+ *
+ * A face combines two parts: LinkService and Transport.
+ * Transport is the lower part, which provides TLV block delivery services with best-effort.
+ * LinkService is the upper part, which translates between network-layer packets
+ * and TLV blocks, and may provide additional services such as fragmentation and reassembly.
+ *
+ * We are in the process of refactoring face system to use this LinkService+Transport
+ * architecture. During this process, the "face" is named LpFace, and we implement a
+ * LpFaceWrapper class as an adaptor to the old Face APIs. After the completion of refactoring,
+ * LpFace will be renamed to Face.
+ */
+class LpFace
+#ifndef WITH_TESTS
+DECL_CLASS_FINAL
+#endif
+ : noncopyable
+{
+public:
+ LpFace(unique_ptr<LinkService> service, unique_ptr<Transport> transport);
+
+ LinkService*
+ getLinkService();
+
+ Transport*
+ getTransport();
+
+public: // upper interface connected to forwarding
+ /** \brief sends Interest on Face
+ */
+ void
+ sendInterest(const Interest& interest);
+
+ /** \brief sends Data on Face
+ */
+ void
+ sendData(const Data& data);
+
+ /** \brief sends Nack on Face
+ */
+ void
+ sendNack(const lp::Nack& nack);
+
+ /** \brief signals on Interest received
+ */
+ signal::Signal<LinkService, Interest>& afterReceiveInterest;
+
+ /** \brief signals on Data received
+ */
+ signal::Signal<LinkService, Data>& afterReceiveData;
+
+ /** \brief signals on Nack received
+ */
+ signal::Signal<LinkService, lp::Nack>& afterReceiveNack;
+
+public: // static properties
+ /** \return face ID
+ */
+ FaceId
+ getId() const;
+
+ /** \brief sets face ID
+ * \note Normally, this should only be invoked by FaceTable.
+ */
+ void
+ setId(FaceId id);
+
+ /** \return a FaceUri representing local endpoint
+ */
+ FaceUri
+ getLocalUri() const;
+
+ /** \return a FaceUri representing remote endpoint
+ */
+ FaceUri
+ getRemoteUri() const;
+
+ /** \return whether face is local or non-local for scope control purpose
+ */
+ ndn::nfd::FaceScope
+ getScope() const;
+
+ /** \return face persistency setting
+ */
+ ndn::nfd::FacePersistency
+ getPersistency() const;
+
+ /** \brief changes face persistency setting
+ */
+ void
+ setPersistency(ndn::nfd::FacePersistency persistency);
+
+ /** \return whether face is point-to-point or multi-access
+ */
+ ndn::nfd::LinkType
+ getLinkType() const;
+
+public: // dynamic properties
+ /** \return face state
+ */
+ FaceState
+ getState() const;
+
+ /** \brief signals after face state changed
+ */
+ signal::Signal<Transport, FaceState/*old*/, FaceState/*new*/>& afterStateChange;
+
+ /** \brief request the face to be closed
+ *
+ * This operation is effective only if face is in UP or DOWN state,
+ * otherwise it has no effect.
+ * The face changes state to CLOSING, and performs cleanup procedure.
+ * The state will be changed to CLOSED when cleanup is complete, which may
+ * happen synchronously or asynchronously.
+ *
+ * \warning the face must not be deallocated until its state changes to CLOSED
+ */
+ void
+ close();
+
+ const FaceCounters&
+ getCounters() const;
+
+ FaceCounters&
+ getMutableCounters();
+
+private:
+ FaceId m_id;
+ unique_ptr<LinkService> m_service;
+ unique_ptr<Transport> m_transport;
+ FaceCounters m_counters;
+};
+
+inline LinkService*
+LpFace::getLinkService()
+{
+ return m_service.get();
+}
+
+inline Transport*
+LpFace::getTransport()
+{
+ return m_transport.get();
+}
+
+inline void
+LpFace::sendInterest(const Interest& interest)
+{
+ m_service->sendInterest(interest);
+}
+
+inline void
+LpFace::sendData(const Data& data)
+{
+ m_service->sendData(data);
+}
+
+inline void
+LpFace::sendNack(const lp::Nack& nack)
+{
+ m_service->sendNack(nack);
+}
+
+inline FaceId
+LpFace::getId() const
+{
+ return m_id;
+}
+
+inline void
+LpFace::setId(FaceId id)
+{
+ m_id = id;
+}
+
+inline FaceUri
+LpFace::getLocalUri() const
+{
+ return m_transport->getLocalUri();
+}
+
+inline FaceUri
+LpFace::getRemoteUri() const
+{
+ return m_transport->getRemoteUri();
+}
+
+inline ndn::nfd::FaceScope
+LpFace::getScope() const
+{
+ return m_transport->getScope();
+}
+
+inline ndn::nfd::FacePersistency
+LpFace::getPersistency() const
+{
+ return m_transport->getPersistency();
+}
+
+inline void
+LpFace::setPersistency(ndn::nfd::FacePersistency persistency)
+{
+ return m_transport->setPersistency(persistency);
+}
+
+inline ndn::nfd::LinkType
+LpFace::getLinkType() const
+{
+ return m_transport->getLinkType();
+}
+
+inline FaceState
+LpFace::getState() const
+{
+ return m_transport->getState();
+}
+
+inline void
+LpFace::close()
+{
+ m_transport->close();
+}
+
+inline const FaceCounters&
+LpFace::getCounters() const
+{
+ return m_counters;
+}
+
+inline FaceCounters&
+LpFace::getMutableCounters()
+{
+ return m_counters;
+}
+
+template<typename T>
+typename std::enable_if<std::is_base_of<LpFace, T>::value, std::ostream&>::type
+operator<<(std::ostream& os, const FaceLogHelper<T>& flh)
+{
+ const LpFace& face = flh.obj;
+ os << "[id=" << face.getId() << ",local=" << face.getLocalUri() <<
+ ",remote=" << face.getRemoteUri() << "] ";
+ return os;
+}
+
+} // namespace face
+
+// using face::FaceId; // TODO uncomment in #3172
+using face::LpFace;
+
+} // namespace nfd
+
+#endif // NFD_DAEMON_LP_FACE_HPP
diff --git a/daemon/face/transport.cpp b/daemon/face/transport.cpp
new file mode 100644
index 0000000..fa1159d
--- /dev/null
+++ b/daemon/face/transport.cpp
@@ -0,0 +1,169 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * 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.
+ *
+ * NFD is free software: you can redistribute it and/or modify it under the terms
+ * of the GNU General Public License as published by the Free Software Foundation,
+ * either version 3 of the License, or (at your option) any later version.
+ *
+ * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
+ * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
+ * PURPOSE. See the GNU General Public License for more details.
+ *
+ * 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 "transport.hpp"
+#include "lp-face.hpp"
+#include "link-service.hpp"
+
+namespace nfd {
+namespace face {
+
+NFD_LOG_INIT("Transport");
+
+std::ostream&
+operator<<(std::ostream& os, TransportState state)
+{
+ switch (state) {
+ case TransportState::UP:
+ return os << "UP";
+ case TransportState::DOWN:
+ return os << "DOWN";
+ case TransportState::CLOSING:
+ return os << "CLOSING";
+ case TransportState::FAILED:
+ return os << "FAILED";
+ case TransportState::CLOSED:
+ return os << "CLOSED";
+ default:
+ return os << "NONE";
+ }
+}
+
+Transport::Packet::Packet(Block&& packet1)
+ : packet(std::move(packet1))
+{
+}
+
+Transport::Transport()
+ : m_face(nullptr)
+ , m_service(nullptr)
+ , m_scope(ndn::nfd::FACE_SCOPE_NON_LOCAL)
+ , m_persistency(ndn::nfd::FACE_PERSISTENCY_PERSISTENT)
+ , m_linkType(ndn::nfd::LINK_TYPE_POINT_TO_POINT)
+ , m_state(TransportState::UP)
+ , m_counters(nullptr)
+{
+}
+
+Transport::~Transport()
+{
+}
+
+void
+Transport::setFaceAndLinkService(LpFace& face, LinkService& service)
+{
+ BOOST_ASSERT(m_face == nullptr);
+ BOOST_ASSERT(m_service == nullptr);
+
+ m_face = &face;
+ m_service = &service;
+ m_counters = &m_face->getMutableCounters();
+}
+
+void
+Transport::close()
+{
+ if (m_state != TransportState::UP && m_state != TransportState::DOWN) {
+ return;
+ }
+
+ this->setState(TransportState::CLOSING);
+ this->doClose();
+ // warning: don't access any fields after this:
+ // the Transport may be deallocated if doClose changes state to CLOSED
+}
+
+void
+Transport::send(Transport::Packet&& packet)
+{
+ // TODO#3177 increment LpPacket counter
+ m_counters->getNOutBytes() += packet.packet.size();
+
+ this->doSend(std::move(packet));
+}
+
+void
+Transport::receive(Transport::Packet&& packet)
+{
+ // TODO#3177 increment LpPacket counter
+ m_counters->getNInBytes() += packet.packet.size();
+
+ m_service->receivePacket(std::move(packet));
+}
+
+void
+Transport::setState(TransportState newState)
+{
+ if (m_state == newState) {
+ return;
+ }
+
+ bool isValid = false;
+ switch (m_state) {
+ case TransportState::UP:
+ isValid = newState == TransportState::DOWN ||
+ newState == TransportState::CLOSING ||
+ newState == TransportState::FAILED;
+ break;
+ case TransportState::DOWN:
+ isValid = newState == TransportState::UP ||
+ newState == TransportState::CLOSING ||
+ newState == TransportState::FAILED;
+ break;
+ case TransportState::CLOSING:
+ case TransportState::FAILED:
+ isValid = newState == TransportState::CLOSED;
+ break;
+ default:
+ break;
+ }
+
+ if (!isValid) {
+ throw std::runtime_error("invalid state transition");
+ }
+
+ NFD_LOG_FACE_INFO("setState " << m_state << " -> " << newState);
+
+ TransportState oldState = m_state;
+ m_state = newState;
+ afterStateChange(oldState, newState);
+ // warning: don't access any fields after this:
+ // the Transport may be deallocated in the signal handler if newState is CLOSED
+}
+
+std::ostream&
+operator<<(std::ostream& os, const FaceLogHelper<Transport>& flh)
+{
+ const Transport& transport = flh.obj;
+ const LpFace* face = transport.getFace();
+ FaceId faceId = face == nullptr ? INVALID_FACEID : face->getId();
+
+ os << "[id=" << faceId << ",local=" << transport.getLocalUri()
+ << ",remote=" << transport.getRemoteUri() << "] ";
+ return os;
+}
+
+} // namespace face
+} // namespace nfd
diff --git a/daemon/face/transport.hpp b/daemon/face/transport.hpp
new file mode 100644
index 0000000..144e0dc
--- /dev/null
+++ b/daemon/face/transport.hpp
@@ -0,0 +1,340 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * 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.
+ *
+ * NFD is free software: you can redistribute it and/or modify it under the terms
+ * of the GNU General Public License as published by the Free Software Foundation,
+ * either version 3 of the License, or (at your option) any later version.
+ *
+ * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
+ * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
+ * PURPOSE. See the GNU General Public License for more details.
+ *
+ * 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_FACE_TRANSPORT_HPP
+#define NFD_DAEMON_FACE_TRANSPORT_HPP
+
+#include "common.hpp"
+
+#include "face-counters.hpp"
+#include "face-log.hpp"
+
+namespace nfd {
+namespace face {
+
+class LpFace;
+class LinkService;
+
+/** \brief indicates the state of a transport
+ */
+enum class TransportState {
+ NONE,
+ UP, ///< the transport is up
+ DOWN, ///< the transport is down temporarily, and is being recovered
+ CLOSING, ///< the transport is requested to be closed
+ FAILED, ///< the transport is being closed due to a failure
+ CLOSED ///< the transport is closed, and can be safely deallocated
+};
+
+std::ostream&
+operator<<(std::ostream& os, TransportState state);
+
+/** \brief the lower part of an LpFace
+ * \sa LpFace
+ */
+class Transport : noncopyable
+{
+public:
+ /** \brief identifies an endpoint on the link
+ */
+ typedef uint64_t EndpointId;
+
+ /** \brief stores a packet along with the remote endpoint
+ */
+ class Packet
+ {
+ public:
+ Packet() = default;
+
+ explicit
+ Packet(Block&& packet);
+
+ public:
+ /** \brief the packet as a TLV block
+ */
+ Block packet;
+
+ /** \brief identifies the remote endpoint
+ *
+ * This ID is only meaningful in the context of the same Transport.
+ * Incoming packets from the same remote endpoint have the same EndpointId,
+ * and incoming packets from different remote endpoints have different EndpointIds.
+ */
+ EndpointId remoteEndpoint;
+ };
+
+ Transport();
+
+ virtual
+ ~Transport();
+
+public:
+ /** \brief set Face and LinkService for Transport
+ * \pre setFaceAndLinkService has not been called
+ */
+ void
+ setFaceAndLinkService(LpFace& face, LinkService& service);
+
+ /** \return Face to which this Transport is attached
+ */
+ const LpFace*
+ getFace() const;
+
+ /** \return LinkService to which this Transport is attached
+ */
+ const LinkService*
+ getLinkService() const;
+
+ /** \return LinkService to which this Transport is attached
+ */
+ LinkService*
+ getLinkService();
+
+public: // upper interface
+ /** \brief request the transport to be closed
+ *
+ * This operation is effective only if transport is in UP or DOWN state,
+ * otherwise it has no effect.
+ * The transport changes state to CLOSING, and performs cleanup procedure.
+ * The state will be changed to CLOSED when cleanup is complete, which may
+ * happen synchronously or asynchronously.
+ */
+ void
+ close();
+
+ /** \brief send a link-layer packet
+ */
+ void
+ send(Packet&& packet);
+
+protected: // upper interface to be invoked by subclass
+ /** \brief receive a link-layer packet
+ */
+ void
+ receive(Packet&& packet);
+
+public: // static properties
+ /** \return a FaceUri representing local endpoint
+ */
+ FaceUri
+ getLocalUri() const;
+
+ /** \return a FaceUri representing remote endpoint
+ */
+ FaceUri
+ getRemoteUri() const;
+
+ /** \return whether face is local or non-local for scope control purpose
+ */
+ ndn::nfd::FaceScope
+ getScope() const;
+
+ /** \return face persistency setting
+ */
+ ndn::nfd::FacePersistency
+ getPersistency() const;
+
+ /** \brief changes face persistency setting
+ */
+ void
+ setPersistency(ndn::nfd::FacePersistency persistency);
+
+ /** \return whether face is point-to-point or multi-access
+ */
+ ndn::nfd::LinkType
+ getLinkType() const;
+
+public: // dynamic properties
+ /** \return transport state
+ */
+ TransportState
+ getState() const;
+
+ /** \brief signals when transport state changes
+ */
+ signal::Signal<Transport, TransportState/*old*/, TransportState/*new*/> afterStateChange;
+
+protected: // properties to be set by subclass
+ void
+ setLocalUri(const FaceUri& uri);
+
+ void
+ setRemoteUri(const FaceUri& uri);
+
+ void
+ setScope(ndn::nfd::FaceScope scope);
+
+ void
+ setLinkType(ndn::nfd::LinkType linkType);
+
+ /** \brief set transport state
+ *
+ * Only the following transitions are valid:
+ * UP->DOWN, DOWN->UP, UP/DOWN->CLOSING/FAILED, CLOSING/FAILED->CLOSED
+ *
+ * \throw std::runtime_error transition is invalid.
+ */
+ void
+ setState(TransportState newState);
+
+protected: // to be overridden by subclass
+ /** \brief invoked before persistency is changed
+ * \throw std::invalid_argument new persistency is not supported
+ * \throw std::runtime_error transition is disallowed
+ *
+ * Base class implementation does nothing.
+ */
+ virtual void
+ beforeChangePersistency(ndn::nfd::FacePersistency newPersistency)
+ {
+ }
+
+ /** \brief performs Transport specific operations to close the transport
+ *
+ * When the cleanup procedure is complete, this method should change state to CLOSED.
+ * This can happen synchronously or asynchronously.
+ */
+ virtual void
+ doClose() = 0;
+
+private: // to be overridden by subclass
+ /** \brief performs Transport specific operations to send a packet
+ * \param packet the packet, which must be a well-formed TLV block
+ */
+ virtual void
+ doSend(Packet&& packet) = 0;
+
+private:
+ LpFace* m_face;
+ LinkService* m_service;
+ FaceUri m_localUri;
+ FaceUri m_remoteUri;
+ ndn::nfd::FaceScope m_scope;
+ ndn::nfd::FacePersistency m_persistency;
+ ndn::nfd::LinkType m_linkType;
+ TransportState m_state;
+ LinkLayerCounters* m_counters; // TODO#3177 change into LinkCounters
+};
+
+inline const LpFace*
+Transport::getFace() const
+{
+ return m_face;
+}
+
+inline const LinkService*
+Transport::getLinkService() const
+{
+ return m_service;
+}
+
+inline LinkService*
+Transport::getLinkService()
+{
+ return m_service;
+}
+
+inline FaceUri
+Transport::getLocalUri() const
+{
+ return m_localUri;
+}
+
+inline FaceUri
+Transport::getRemoteUri() const
+{
+ return m_remoteUri;
+}
+
+inline ndn::nfd::FaceScope
+Transport::getScope() const
+{
+ return m_scope;
+}
+
+inline ndn::nfd::FacePersistency
+Transport::getPersistency() const
+{
+ return m_persistency;
+}
+
+inline void
+Transport::setPersistency(ndn::nfd::FacePersistency persistency)
+{
+ this->beforeChangePersistency(persistency);
+ m_persistency = persistency;
+}
+
+inline ndn::nfd::LinkType
+Transport::getLinkType() const
+{
+ return m_linkType;
+}
+
+inline TransportState
+Transport::getState() const
+{
+ return m_state;
+}
+
+inline void
+Transport::setLocalUri(const FaceUri& uri)
+{
+ m_localUri = uri;
+}
+
+inline void
+Transport::setRemoteUri(const FaceUri& uri)
+{
+ m_remoteUri = uri;
+}
+
+inline void
+Transport::setScope(ndn::nfd::FaceScope scope)
+{
+ m_scope = scope;
+}
+
+inline void
+Transport::setLinkType(ndn::nfd::LinkType linkType)
+{
+ m_linkType = linkType;
+}
+
+std::ostream&
+operator<<(std::ostream& os, const FaceLogHelper<Transport>& flh);
+
+template<typename T>
+typename std::enable_if<std::is_base_of<Transport, T>::value &&
+ !std::is_same<Transport, T>::value, std::ostream&>::type
+operator<<(std::ostream& os, const FaceLogHelper<T>& flh)
+{
+ return os << FaceLogHelper<Transport>(flh.obj);
+}
+
+} // namespace face
+} // namespace nfd
+
+#endif // NFD_DAEMON_FACE_TRANSPORT_HPP
diff --git a/tests/daemon/face/dummy-lp-face.cpp b/tests/daemon/face/dummy-lp-face.cpp
new file mode 100644
index 0000000..20fb20d
--- /dev/null
+++ b/tests/daemon/face/dummy-lp-face.cpp
@@ -0,0 +1,140 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * 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.
+ *
+ * NFD is free software: you can redistribute it and/or modify it under the terms
+ * of the GNU General Public License as published by the Free Software Foundation,
+ * either version 3 of the License, or (at your option) any later version.
+ *
+ * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
+ * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
+ * PURPOSE. See the GNU General Public License for more details.
+ *
+ * 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 "dummy-lp-face.hpp"
+#include "dummy-transport.hpp"
+
+namespace nfd {
+namespace face {
+namespace tests {
+
+class DummyLpFace::LinkService : public face::LinkService
+{
+public:
+ void
+ receiveInterest(const Interest& interest)
+ {
+ this->face::LinkService::receiveInterest(interest);
+ }
+
+ void
+ receiveData(const Data& data)
+ {
+ this->face::LinkService::receiveData(data);
+ }
+
+ void
+ receiveNack(const lp::Nack& nack)
+ {
+ this->face::LinkService::receiveNack(nack);
+ }
+
+ signal::Signal<LinkService> afterSend;
+
+private:
+ virtual void
+ doSendInterest(const Interest& interest) DECL_OVERRIDE
+ {
+ this->sentInterests.push_back(interest);
+ this->afterSend();
+ }
+
+ virtual void
+ doSendData(const Data& data) DECL_OVERRIDE
+ {
+ this->sentData.push_back(data);
+ this->afterSend();
+ }
+
+ virtual void
+ doSendNack(const lp::Nack& nack) DECL_OVERRIDE
+ {
+ this->sentNacks.push_back(nack);
+ this->afterSend();
+ }
+
+ virtual void
+ doReceivePacket(Transport::Packet&& packet) DECL_OVERRIDE
+ {
+ BOOST_ASSERT(false);
+ }
+
+public:
+ std::vector<Interest> sentInterests;
+ std::vector<Data> sentData;
+ std::vector<lp::Nack> sentNacks;
+};
+
+DummyLpFace::DummyLpFace(const std::string& localUri, const std::string& remoteUri,
+ ndn::nfd::FaceScope scope, ndn::nfd::FacePersistency persistency,
+ ndn::nfd::LinkType linkType)
+ : LpFace(make_unique<LinkService>(),
+ make_unique<DummyTransport>(localUri, remoteUri, scope, persistency, linkType))
+ , afterSend(this->getLinkServiceInternal()->afterSend)
+ , sentInterests(this->getLinkServiceInternal()->sentInterests)
+ , sentData(this->getLinkServiceInternal()->sentData)
+ , sentNacks(this->getLinkServiceInternal()->sentNacks)
+{
+}
+
+void
+DummyLpFace::setState(FaceState state)
+{
+ this->getTransportInternal()->setState(state);
+}
+
+void
+DummyLpFace::receiveInterest(const Interest& interest)
+{
+ this->getLinkServiceInternal()->receiveInterest(interest);
+}
+
+void
+DummyLpFace::receiveData(const Data& data)
+{
+ this->getLinkServiceInternal()->receiveData(data);
+}
+
+void
+DummyLpFace::receiveNack(const lp::Nack& nack)
+{
+ this->getLinkServiceInternal()->receiveNack(nack);
+}
+
+DummyLpFace::LinkService*
+DummyLpFace::getLinkServiceInternal()
+{
+ return static_cast<LinkService*>(this->getLinkService());
+}
+
+DummyTransport*
+DummyLpFace::getTransportInternal()
+{
+ return static_cast<DummyTransport*>(this->getTransport());
+}
+
+} // namespace tests
+} // namespace face
+} // namespace nfd
diff --git a/tests/daemon/face/dummy-lp-face.hpp b/tests/daemon/face/dummy-lp-face.hpp
new file mode 100644
index 0000000..588cb59
--- /dev/null
+++ b/tests/daemon/face/dummy-lp-face.hpp
@@ -0,0 +1,99 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * 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.
+ *
+ * NFD is free software: you can redistribute it and/or modify it under the terms
+ * of the GNU General Public License as published by the Free Software Foundation,
+ * either version 3 of the License, or (at your option) any later version.
+ *
+ * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
+ * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
+ * PURPOSE. See the GNU General Public License for more details.
+ *
+ * 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_TESTS_DAEMON_FACE_DUMMY_LP_FACE_HPP
+#define NFD_TESTS_DAEMON_FACE_DUMMY_LP_FACE_HPP
+
+#include "face/lp-face.hpp"
+
+namespace nfd {
+namespace face {
+namespace tests {
+
+class DummyTransport;
+
+/** \brief a LpFace for unit testing
+ *
+ * The DummyLpFace allows observing outgoing network-layer packets,
+ * and allows incoming network-layer packets to be injected from a test suite.
+ * It's primarily used for forwarding test suites, but can be used in other tests as well.
+ */
+class DummyLpFace : public LpFace
+{
+public:
+ class LinkService;
+
+ DummyLpFace(const std::string& localUri = "dummy://", const std::string& remoteUri = "dummy://",
+ ndn::nfd::FaceScope scope = ndn::nfd::FACE_SCOPE_NON_LOCAL,
+ ndn::nfd::FacePersistency persistency = ndn::nfd::FACE_PERSISTENCY_PERSISTENT,
+ ndn::nfd::LinkType linkType = ndn::nfd::LINK_TYPE_POINT_TO_POINT);
+
+ /** \brief changes face state
+ * \pre current state is not CLOSED or FAILED
+ */
+ void
+ setState(FaceState state);
+
+ /** \brief causes the face to receive an Interest
+ */
+ void
+ receiveInterest(const Interest& interest);
+
+ /** \brief causes the face to receive a Data
+ */
+ void
+ receiveData(const Data& data);
+
+ /** \brief causes the face to receive a Nack
+ */
+ void
+ receiveNack(const lp::Nack& nack);
+
+ /** \brief signals after any network-layer packet is sent
+ */
+ signal::Signal<LinkService>& afterSend;
+
+private:
+ LinkService*
+ getLinkServiceInternal();
+
+ DummyTransport*
+ getTransportInternal();
+
+public:
+ std::vector<Interest>& sentInterests;
+ std::vector<Data>& sentData;
+ std::vector<lp::Nack>& sentNacks;
+};
+
+} // namespace tests
+} // namespace face
+
+namespace tests {
+using nfd::face::tests::DummyLpFace;
+} // namespace tests
+} // namespace nfd
+
+#endif // NFD_TESTS_DAEMON_FACE_DUMMY_LP_FACE_HPP
diff --git a/tests/daemon/face/dummy-transport.hpp b/tests/daemon/face/dummy-transport.hpp
new file mode 100644
index 0000000..df8fa54
--- /dev/null
+++ b/tests/daemon/face/dummy-transport.hpp
@@ -0,0 +1,96 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * 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.
+ *
+ * NFD is free software: you can redistribute it and/or modify it under the terms
+ * of the GNU General Public License as published by the Free Software Foundation,
+ * either version 3 of the License, or (at your option) any later version.
+ *
+ * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
+ * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
+ * PURPOSE. See the GNU General Public License for more details.
+ *
+ * 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_TESTS_DAEMON_FACE_DUMMY_TRANSPORT_HPP
+#define NFD_TESTS_DAEMON_FACE_DUMMY_TRANSPORT_HPP
+
+#include "common.hpp"
+
+#include "face/transport.hpp"
+
+namespace nfd {
+namespace face {
+namespace tests {
+
+/** \brief dummy Transport used in unit tests
+ */
+class DummyTransport : public Transport
+{
+public:
+ DummyTransport(const std::string& localUri = "dummy://",
+ const std::string& remoteUri = "dummy://",
+ ndn::nfd::FaceScope scope = ndn::nfd::FACE_SCOPE_NON_LOCAL,
+ ndn::nfd::FacePersistency persistency = ndn::nfd::FACE_PERSISTENCY_PERSISTENT,
+ ndn::nfd::LinkType linkType = ndn::nfd::LINK_TYPE_POINT_TO_POINT)
+ : isClosed(false)
+ {
+ this->setLocalUri(FaceUri(localUri));
+ this->setRemoteUri(FaceUri(remoteUri));
+ this->setScope(scope);
+ this->setPersistency(persistency);
+ this->setLinkType(linkType);
+ }
+
+ void
+ setState(FaceState state)
+ {
+ this->Transport::setState(state);
+ }
+
+ void
+ receivePacket(Packet&& packet)
+ {
+ this->receive(std::move(packet));
+ }
+
+ void
+ receivePacket(Block block)
+ {
+ this->receive(Packet(std::move(block)));
+ }
+
+private:
+ virtual void
+ doClose() DECL_OVERRIDE
+ {
+ isClosed = true;
+ }
+
+ virtual void
+ doSend(Packet&& packet) DECL_OVERRIDE
+ {
+ sentPackets.push_back(std::move(packet));
+ }
+
+public:
+ bool isClosed;
+ std::vector<Packet> sentPackets;
+};
+
+} // namespace tests
+} // namespace face
+} // namespace nfd
+
+#endif // NFD_TESTS_DAEMON_FACE_DUMMY_TRANSPORT_HPP
diff --git a/tests/daemon/face/generic-link-service.t.cpp b/tests/daemon/face/generic-link-service.t.cpp
new file mode 100644
index 0000000..a918b26
--- /dev/null
+++ b/tests/daemon/face/generic-link-service.t.cpp
@@ -0,0 +1,285 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * 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.
+ *
+ * NFD is free software: you can redistribute it and/or modify it under the terms
+ * of the GNU General Public License as published by the Free Software Foundation,
+ * either version 3 of the License, or (at your option) any later version.
+ *
+ * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
+ * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
+ * PURPOSE. See the GNU General Public License for more details.
+ *
+ * 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 "face/generic-link-service.hpp"
+#include "face/lp-face.hpp"
+#include "dummy-transport.hpp"
+
+#include "tests/test-common.hpp"
+
+namespace nfd {
+namespace face {
+namespace tests {
+
+using namespace nfd::tests;
+
+BOOST_AUTO_TEST_SUITE(Face)
+
+class GenericLinkServiceFixture : public BaseFixture
+{
+protected:
+ GenericLinkServiceFixture()
+ : service(nullptr)
+ , transport(nullptr)
+ {
+ this->initialize();
+ // By default, GenericLinkService is created with default options.
+ // Test cases may invoke .initialize with alternate options.
+ }
+
+ void
+ initialize()
+ {
+ // TODO#3104 add GenericLinkService::Options parameter,
+ // and create GenericLinkService with options
+ face.reset(new LpFace(make_unique<GenericLinkService>(),
+ make_unique<DummyTransport>()));
+ service = static_cast<GenericLinkService*>(face->getLinkService());
+ transport = static_cast<DummyTransport*>(face->getTransport());
+
+ face->afterReceiveInterest.connect(
+ [this] (const Interest& interest) { receivedInterests.push_back(interest); });
+ face->afterReceiveData.connect(
+ [this] (const Data& data) { receivedData.push_back(data); });
+ face->afterReceiveNack.connect(
+ [this] (const lp::Nack& nack) { receivedNacks.push_back(nack); });
+ }
+
+protected:
+ unique_ptr<LpFace> face;
+ GenericLinkService* service;
+ DummyTransport* transport;
+ std::vector<Interest> receivedInterests;
+ std::vector<Data> receivedData;
+ std::vector<lp::Nack> receivedNacks;
+};
+
+BOOST_FIXTURE_TEST_SUITE(TestGenericLinkService, GenericLinkServiceFixture)
+
+
+BOOST_AUTO_TEST_SUITE(SimpleSendReceive) // send and receive without other fields
+
+BOOST_AUTO_TEST_CASE(SendInterest)
+{
+ // TODO#3104 initialize with Options that disables all services
+
+ shared_ptr<Interest> interest1 = makeInterest("/localhost/test");
+
+ face->sendInterest(*interest1);
+
+ BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 1);
+ BOOST_CHECK(transport->sentPackets.back().packet == interest1->wireEncode());
+}
+
+BOOST_AUTO_TEST_CASE(SendData)
+{
+ // TODO#3104 initialize with Options that disables all services
+
+ shared_ptr<Data> data1 = makeData("/localhost/test");
+
+ face->sendData(*data1);
+
+ BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 1);
+ BOOST_CHECK(transport->sentPackets.back().packet == data1->wireEncode());
+}
+
+BOOST_AUTO_TEST_CASE(SendNack)
+{
+ // TODO#3104 initialize with Options that disables all services
+
+ lp::Nack nack1 = makeNack("/localhost/test", 323, lp::NackReason::NO_ROUTE);
+
+ face->sendNack(nack1);
+
+ BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 1);
+ lp::Packet nack1pkt;
+ BOOST_REQUIRE_NO_THROW(nack1pkt.wireDecode(transport->sentPackets.back().packet));
+ BOOST_CHECK_EQUAL(nack1pkt.has<lp::NackField>(), true);
+ BOOST_CHECK_EQUAL(nack1pkt.has<lp::FragmentField>(), true);
+}
+
+BOOST_AUTO_TEST_CASE(ReceiveBareInterest)
+{
+ // TODO#3104 initialize with Options that disables all services
+
+ shared_ptr<Interest> interest1 = makeInterest("/23Rd9hEiR");
+
+ transport->receivePacket(interest1->wireEncode());
+
+ BOOST_REQUIRE_EQUAL(receivedInterests.size(), 1);
+ BOOST_CHECK_EQUAL(receivedInterests.back(), *interest1);
+}
+
+BOOST_AUTO_TEST_CASE(ReceiveInterest)
+{
+ // TODO#3104 initialize with Options that disables all services
+
+ shared_ptr<Interest> interest1 = makeInterest("/23Rd9hEiR");
+ lp::Packet lpPacket;
+ lpPacket.set<lp::FragmentField>(std::make_pair(
+ interest1->wireEncode().begin(), interest1->wireEncode().end()));
+ lpPacket.set<lp::SequenceField>(0); // force LpPacket encoding
+
+ transport->receivePacket(lpPacket.wireEncode());
+
+ BOOST_REQUIRE_EQUAL(receivedInterests.size(), 1);
+ BOOST_CHECK_EQUAL(receivedInterests.back(), *interest1);
+}
+
+BOOST_AUTO_TEST_CASE(ReceiveBareData)
+{
+ // TODO#3104 initialize with Options that disables all services
+
+ shared_ptr<Data> data1 = makeData("/12345678");
+
+ transport->receivePacket(data1->wireEncode());
+
+ BOOST_REQUIRE_EQUAL(receivedData.size(), 1);
+ BOOST_CHECK_EQUAL(receivedData.back(), *data1);
+}
+
+BOOST_AUTO_TEST_CASE(ReceiveData)
+{
+ // TODO#3104 initialize with Options that disables all services
+
+ shared_ptr<Data> data1 = makeData("/12345689");
+ lp::Packet lpPacket;
+ lpPacket.set<lp::FragmentField>(std::make_pair(
+ data1->wireEncode().begin(), data1->wireEncode().end()));
+ lpPacket.set<lp::SequenceField>(0); // force LpPacket encoding
+
+ transport->receivePacket(lpPacket.wireEncode());
+
+ BOOST_REQUIRE_EQUAL(receivedData.size(), 1);
+ BOOST_CHECK_EQUAL(receivedData.back(), *data1);
+}
+
+BOOST_AUTO_TEST_CASE(ReceiveNack)
+{
+ // TODO#3104 initialize with Options that disables all services
+
+ lp::Nack nack1 = makeNack("/localhost/test", 323, lp::NackReason::NO_ROUTE);
+ lp::Packet lpPacket;
+ lpPacket.set<lp::FragmentField>(std::make_pair(
+ nack1.getInterest().wireEncode().begin(), nack1.getInterest().wireEncode().end()));
+ lpPacket.set<lp::NackField>(nack1.getHeader());
+
+ transport->receivePacket(lpPacket.wireEncode());
+
+ BOOST_REQUIRE_EQUAL(receivedNacks.size(), 1);
+}
+
+BOOST_AUTO_TEST_SUITE_END() // SimpleSendReceive
+
+
+BOOST_AUTO_TEST_SUITE(LocalFields)
+
+BOOST_AUTO_TEST_CASE(SendIncomingFaceId)
+{
+ // TODO#3104 initialize with Options that enables local fields
+ // TODO#3104 send Interest with IncomingFaceId
+ // expect transport->sentPackets.back() has IncomingFaceId field
+}
+
+BOOST_AUTO_TEST_CASE(SendIncomingFaceIdDisabled)
+{
+ // TODO#3104 initialize with Options that disables local fields
+ // TODO#3104 send Interest with IncomingFaceId
+ // expect transport->sentPackets.back() has no IncomingFaceId field
+}
+
+BOOST_AUTO_TEST_CASE(ReceiveIncomingFaceIdIgnore)
+{
+ // TODO#3104 initialize with Options that enables local fields
+ // TODO#3104 receive Interest with IncomingFaceId
+ // expect receivedInterests.back() has no IncomingFaceId tag
+}
+
+BOOST_AUTO_TEST_CASE(ReceiveNextHopFaceId)
+{
+ // TODO#3104 initialize with Options that enables local fields
+ // TODO#3104 receive Interest with NextHopFaceId
+ // expect receivedInterests.back() has NextHopFaceId tag
+}
+
+BOOST_AUTO_TEST_CASE(ReceiveNextHopFaceIdDisabled)
+{
+ // TODO#3104 initialize with Options that disables local fields
+ // TODO#3104 receive Interest with NextHopFaceId
+ // expect receivedInterests.empty()
+ // or, expect receivedInterests.back() has no NextHopFaceId tag
+}
+
+BOOST_AUTO_TEST_CASE(ReceiveNextHopFaceIdDropData)
+{
+ // TODO#3104 initialize with Options that enables local fields
+ // TODO#3104 receive Data with NextHopFaceId
+ // expect receivedData.empty()
+}
+
+BOOST_AUTO_TEST_CASE(ReceiveNextHopFaceIdDropNack)
+{
+ // TODO#3104 initialize with Options that enables local fields
+ // TODO#3104 receive Nack with NextHopFaceId
+ // expect receivedNacks.empty()
+}
+
+BOOST_AUTO_TEST_CASE(ReceiveCacheControl)
+{
+ // TODO#3104 initialize with Options that enables local fields
+ // TODO#3104 receive Data with CacheControl
+ // expect receivedData.back() has CacheControl tag
+}
+
+BOOST_AUTO_TEST_CASE(ReceiveCacheControlDisabled)
+{
+ // TODO#3104 initialize with Options that disables local fields
+ // TODO#3104 receive Data with CacheControl
+ // expect receivedData.back() has no CacheControl tag
+}
+
+BOOST_AUTO_TEST_CASE(ReceiveCacheControlDropInterest)
+{
+ // TODO#3104 initialize with Options that enables local fields
+ // TODO#3104 receive Interest with CacheControl
+ // expect receivedInterests.empty()
+}
+
+BOOST_AUTO_TEST_CASE(ReceiveCacheControlDropNack)
+{
+ // TODO#3104 initialize with Options that enables local fields
+ // TODO#3104 receive Nack with CacheControl
+ // expect receivedNacks.empty()
+}
+
+BOOST_AUTO_TEST_SUITE_END() // LocalFields
+
+
+BOOST_AUTO_TEST_SUITE_END()
+BOOST_AUTO_TEST_SUITE_END()
+
+} // namespace tests
+} // namespace face
+} // namespace nfd
diff --git a/tests/daemon/face/lp-face-wrapper.t.cpp b/tests/daemon/face/lp-face-wrapper.t.cpp
new file mode 100644
index 0000000..182d2fc
--- /dev/null
+++ b/tests/daemon/face/lp-face-wrapper.t.cpp
@@ -0,0 +1,173 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * 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.
+ *
+ * NFD is free software: you can redistribute it and/or modify it under the terms
+ * of the GNU General Public License as published by the Free Software Foundation,
+ * either version 3 of the License, or (at your option) any later version.
+ *
+ * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
+ * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
+ * PURPOSE. See the GNU General Public License for more details.
+ *
+ * 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 "face/lp-face-wrapper.hpp"
+#include "fw/forwarder.hpp"
+
+#include "tests/test-common.hpp"
+#include "dummy-lp-face.hpp"
+
+namespace nfd {
+namespace face {
+namespace tests {
+
+using namespace nfd::tests;
+
+BOOST_AUTO_TEST_SUITE(Face)
+BOOST_FIXTURE_TEST_SUITE(TestLpFaceWrapper, BaseFixture)
+
+BOOST_AUTO_TEST_CASE(SetId)
+{
+ Forwarder forwarder;
+ auto face1w = make_shared<face::LpFaceWrapper>(make_unique<DummyLpFace>());
+ auto face1 = static_cast<DummyLpFace*>(face1w->getLpFace());
+
+ BOOST_CHECK_EQUAL(face1->getId(), nfd::face::INVALID_FACEID);
+ BOOST_CHECK_EQUAL(face1w->getId(), nfd::INVALID_FACEID);
+
+ forwarder.addFace(face1w);
+
+ BOOST_CHECK_NE(face1->getId(), nfd::face::INVALID_FACEID);
+ BOOST_CHECK_NE(face1w->getId(), nfd::INVALID_FACEID);
+ BOOST_CHECK_EQUAL(face1->getId(), static_cast<face::FaceId>(face1w->getId()));
+}
+
+BOOST_AUTO_TEST_CASE(SetPersistency)
+{
+ unique_ptr<LpFace> face1u = make_unique<DummyLpFace>();
+ face1u->setPersistency(ndn::nfd::FACE_PERSISTENCY_ON_DEMAND);
+
+ auto face1w = make_shared<face::LpFaceWrapper>(std::move(face1u));
+ auto face1 = static_cast<DummyLpFace*>(face1w->getLpFace());
+
+ BOOST_CHECK_EQUAL(face1->getPersistency(), ndn::nfd::FACE_PERSISTENCY_ON_DEMAND);
+ BOOST_CHECK_EQUAL(face1w->getPersistency(), ndn::nfd::FACE_PERSISTENCY_ON_DEMAND);
+
+ face1w->setPersistency(ndn::nfd::FACE_PERSISTENCY_PERMANENT);
+
+ BOOST_CHECK_EQUAL(face1->getPersistency(), ndn::nfd::FACE_PERSISTENCY_PERMANENT);
+ BOOST_CHECK_EQUAL(face1w->getPersistency(), ndn::nfd::FACE_PERSISTENCY_PERMANENT);
+}
+
+BOOST_AUTO_TEST_CASE(FailSignal)
+{
+ auto face1w = make_shared<face::LpFaceWrapper>(make_unique<DummyLpFace>());
+ auto face1 = static_cast<DummyLpFace*>(face1w->getLpFace());
+
+ bool isFailed = false;
+ face1w->onFail.connect(bind([&isFailed] { isFailed = true; }));
+
+ face1->setState(FaceState::DOWN);
+ BOOST_CHECK(!isFailed);
+
+ face1->setState(FaceState::FAILED);
+ BOOST_CHECK(!isFailed);
+
+ face1->setState(FaceState::CLOSED);
+ BOOST_CHECK(isFailed);
+}
+
+BOOST_AUTO_TEST_CASE(SendReceive)
+{
+ auto face1w = make_shared<face::LpFaceWrapper>(make_unique<DummyLpFace>());
+ auto face1 = static_cast<DummyLpFace*>(face1w->getLpFace());
+
+ const size_t nInInterests = 192;
+ const size_t nInData = 91;
+ const size_t nInNacks = 29;
+ const size_t nOutInterests = 202;
+ const size_t nOutData = 128;
+ const size_t nOutNacks = 84;
+
+ size_t nReceivedInterests = 0;
+ size_t nReceivedData = 0;
+ size_t nReceivedNacks = 0;
+ face1w->onReceiveInterest.connect(bind([&nReceivedInterests] { ++nReceivedInterests; }));
+ face1w->onReceiveData.connect(bind([&nReceivedData] { ++nReceivedData; }));
+ face1w->onReceiveNack.connect(bind([&nReceivedNacks] { ++nReceivedNacks; }));
+
+ BOOST_CHECK_EQUAL(face1->getCounters().getNInInterests(), 0);
+ BOOST_CHECK_EQUAL(face1->getCounters().getNInDatas(), 0);
+ BOOST_CHECK_EQUAL(face1->getCounters().getNOutInterests(), 0);
+ BOOST_CHECK_EQUAL(face1->getCounters().getNOutDatas(), 0);
+ BOOST_CHECK_EQUAL(face1w->getCounters().getNInInterests(), 0);
+ BOOST_CHECK_EQUAL(face1w->getCounters().getNInDatas(), 0);
+ BOOST_CHECK_EQUAL(face1w->getCounters().getNOutInterests(), 0);
+ BOOST_CHECK_EQUAL(face1w->getCounters().getNOutDatas(), 0);
+ // There's no counters for NACK for now.
+
+ for (size_t i = 0; i < nInInterests; ++i) {
+ shared_ptr<Interest> interest = makeInterest("/JSQdqward4");
+ face1->receiveInterest(*interest);
+ }
+
+ for (size_t i = 0; i < nInData; ++i) {
+ shared_ptr<Data> data = makeData("/hT8FDigWn1");
+ face1->receiveData(*data);
+ }
+
+ for (size_t i = 0; i < nInNacks; ++i) {
+ lp::Nack nack = makeNack("/StnEVTj4Ex", 561, lp::NackReason::CONGESTION);
+ face1->receiveNack(nack);
+ }
+
+ for (size_t i = 0; i < nOutInterests; ++i) {
+ shared_ptr<Interest> interest = makeInterest("/XyUAFYQDmd");
+ face1w->sendInterest(*interest);
+ }
+
+ for (size_t i = 0; i < nOutData; ++i) {
+ shared_ptr<Data> data = makeData("/GigPEtPH6");
+ face1w->sendData(*data);
+ }
+
+ for (size_t i = 0; i < nOutNacks; ++i) {
+ lp::Nack nack = makeNack("/9xK6FbwIBM", 365, lp::NackReason::CONGESTION);
+ face1w->sendNack(nack);
+ }
+
+ BOOST_CHECK_EQUAL(face1->getCounters().getNInInterests(), nInInterests);
+ BOOST_CHECK_EQUAL(face1->getCounters().getNInDatas(), nInData);
+ BOOST_CHECK_EQUAL(face1->getCounters().getNOutInterests(), nOutInterests);
+ BOOST_CHECK_EQUAL(face1->getCounters().getNOutDatas(), nOutData);
+ BOOST_CHECK_EQUAL(face1w->getCounters().getNInInterests(), nInInterests);
+ BOOST_CHECK_EQUAL(face1w->getCounters().getNInDatas(), nInData);
+ BOOST_CHECK_EQUAL(face1w->getCounters().getNOutInterests(), nOutInterests);
+ BOOST_CHECK_EQUAL(face1w->getCounters().getNOutDatas(), nOutData);
+
+ BOOST_CHECK_EQUAL(nReceivedInterests, nInInterests);
+ BOOST_CHECK_EQUAL(nReceivedData, nInData);
+ BOOST_CHECK_EQUAL(nReceivedNacks, nInNacks);
+ BOOST_CHECK_EQUAL(face1->sentInterests.size(), nOutInterests);
+ BOOST_CHECK_EQUAL(face1->sentData.size(), nOutData);
+ BOOST_CHECK_EQUAL(face1->sentNacks.size(), nOutNacks);
+}
+
+BOOST_AUTO_TEST_SUITE_END()
+BOOST_AUTO_TEST_SUITE_END()
+
+} // namespace tests
+} // namespace face
+} // namespace nfd
diff --git a/tests/test-common.hpp b/tests/test-common.hpp
index 784ee2d..c7b7c36 100644
--- a/tests/test-common.hpp
+++ b/tests/test-common.hpp
@@ -135,9 +135,13 @@
};
inline shared_ptr<Interest>
-makeInterest(const Name& name)
+makeInterest(const Name& name, uint32_t nonce = 0)
{
- return make_shared<Interest>(name);
+ auto interest = make_shared<Interest>(name);
+ if (nonce != 0) {
+ interest->setNonce(nonce);
+ }
+ return interest;
}
inline shared_ptr<Data>
@@ -166,6 +170,16 @@
return link;
}
+inline lp::Nack
+makeNack(const Name& name, uint32_t nonce, lp::NackReason reason)
+{
+ Interest interest(name);
+ interest.setNonce(nonce);
+ lp::Nack nack(std::move(interest));
+ nack.setReason(reason);
+ return nack;
+}
+
} // namespace tests
} // namespace nfd