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/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