face: make logging more uniform across channel types
Change-Id: I21db45f736e07464260dcc902c673d098ba3c549
Refs: #2561
diff --git a/daemon/face/channel-log.hpp b/daemon/face/channel-log.hpp
new file mode 100644
index 0000000..6092dad
--- /dev/null
+++ b/daemon/face/channel-log.hpp
@@ -0,0 +1,61 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2014-2017, 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_CHANNEL_LOG_HPP
+#define NFD_DAEMON_FACE_CHANNEL_LOG_HPP
+
+#include "core/logger.hpp"
+
+/** \defgroup ChannelLogging Channel logging macros
+ *
+ * These macros augment the log message with some channel-specific information,
+ * such as the local URI, that are useful to distinguish which channel produced
+ * the message. It is strongly recommended to use these macros instead of the
+ * generic ones for all logging inside Channel subclasses.
+ * @{
+ */
+
+/** \internal */
+#define NFD_LOG_CHAN(level, msg) NFD_LOG_##level( \
+ "[" << this->getUri() << "] " << msg)
+
+/** \brief Log a message at TRACE level */
+#define NFD_LOG_CHAN_TRACE(msg) NFD_LOG_CHAN(TRACE, msg)
+
+/** \brief Log a message at DEBUG level */
+#define NFD_LOG_CHAN_DEBUG(msg) NFD_LOG_CHAN(DEBUG, msg)
+
+/** \brief Log a message at INFO level */
+#define NFD_LOG_CHAN_INFO(msg) NFD_LOG_CHAN(INFO, msg)
+
+/** \brief Log a message at WARN level */
+#define NFD_LOG_CHAN_WARN(msg) NFD_LOG_CHAN(WARN, msg)
+
+/** \brief Log a message at ERROR level */
+#define NFD_LOG_CHAN_ERROR(msg) NFD_LOG_CHAN(ERROR, msg)
+
+/** @} */
+
+#endif // NFD_DAEMON_FACE_CHANNEL_LOG_HPP
diff --git a/daemon/face/channel.hpp b/daemon/face/channel.hpp
index ae3c583..a999b72 100644
--- a/daemon/face/channel.hpp
+++ b/daemon/face/channel.hpp
@@ -26,6 +26,7 @@
#ifndef NFD_DAEMON_FACE_CHANNEL_HPP
#define NFD_DAEMON_FACE_CHANNEL_HPP
+#include "channel-log.hpp"
#include "face.hpp"
namespace nfd {
diff --git a/daemon/face/ethernet-factory.cpp b/daemon/face/ethernet-factory.cpp
index cf0b34c..44282c0 100644
--- a/daemon/face/ethernet-factory.cpp
+++ b/daemon/face/ethernet-factory.cpp
@@ -26,7 +26,6 @@
#include "ethernet-factory.hpp"
#include "generic-link-service.hpp"
#include "multicast-ethernet-transport.hpp"
-#include "core/logger.hpp"
#include <boost/range/adaptors.hpp>
#include <boost/range/algorithm/copy.hpp>
diff --git a/daemon/face/face-log.hpp b/daemon/face/face-log.hpp
index 6e0b3fd..51ff4c6 100644
--- a/daemon/face/face-log.hpp
+++ b/daemon/face/face-log.hpp
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/**
- * Copyright (c) 2014-2015, Regents of the University of California,
+ * Copyright (c) 2014-2017, Regents of the University of California,
* Arizona Board of Regents,
* Colorado State University,
* University Pierre & Marie Curie, Sorbonne University,
@@ -53,6 +53,9 @@
const T& obj;
};
+} // namespace face
+} // namespace nfd
+
/** \defgroup FaceLogging Face logging macros
*
* These macros augment the log message with some face-specific information,
@@ -62,6 +65,7 @@
* @{
*/
+/** \internal */
#define NFD_LOG_FACE(level, msg) NFD_LOG_##level( \
::nfd::face::FaceLogHelper< \
typename std::remove_cv< \
@@ -87,7 +91,4 @@
/** @} */
-} // namespace face
-} // namespace nfd
-
#endif // NFD_DAEMON_FACE_FACE_LOG_HPP
diff --git a/daemon/face/face-system.cpp b/daemon/face/face-system.cpp
index a955d97..ee44591 100644
--- a/daemon/face/face-system.cpp
+++ b/daemon/face/face-system.cpp
@@ -25,7 +25,6 @@
#include "face-system.hpp"
#include "protocol-factory.hpp"
-#include "core/logger.hpp"
#include "fw/face-table.hpp"
namespace nfd {
diff --git a/daemon/face/face.hpp b/daemon/face/face.hpp
index 5486a43..4b99d2f 100644
--- a/daemon/face/face.hpp
+++ b/daemon/face/face.hpp
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/**
- * Copyright (c) 2014-2016, Regents of the University of California,
+ * Copyright (c) 2014-2017, Regents of the University of California,
* Arizona Board of Regents,
* Colorado State University,
* University Pierre & Marie Curie, Sorbonne University,
@@ -293,13 +293,6 @@
std::ostream&
operator<<(std::ostream& os, const FaceLogHelper<Face>& flh);
-template<typename T>
-typename std::enable_if<std::is_base_of<Face, T>::value, std::ostream&>::type
-operator<<(std::ostream& os, const FaceLogHelper<T>& flh)
-{
- return os << FaceLogHelper<Face>(flh.obj);
-}
-
} // namespace face
using face::FaceId;
diff --git a/daemon/face/generic-link-service.hpp b/daemon/face/generic-link-service.hpp
index 89d4a69..8a8622f 100644
--- a/daemon/face/generic-link-service.hpp
+++ b/daemon/face/generic-link-service.hpp
@@ -26,9 +26,6 @@
#ifndef NFD_DAEMON_FACE_GENERIC_LINK_SERVICE_HPP
#define NFD_DAEMON_FACE_GENERIC_LINK_SERVICE_HPP
-#include "core/common.hpp"
-#include "core/logger.hpp"
-
#include "link-service.hpp"
#include "lp-fragmenter.hpp"
#include "lp-reassembler.hpp"
diff --git a/daemon/face/lp-reliability.cpp b/daemon/face/lp-reliability.cpp
index fb95099..aee7357 100644
--- a/daemon/face/lp-reliability.cpp
+++ b/daemon/face/lp-reliability.cpp
@@ -298,12 +298,5 @@
{
}
-std::ostream&
-operator<<(std::ostream& os, const FaceLogHelper<LpReliability>& flh)
-{
- os << FaceLogHelper<LinkService>(*flh.obj.getLinkService());
- return os;
-}
-
} // namespace face
} // namespace nfd
diff --git a/daemon/face/lp-reliability.hpp b/daemon/face/lp-reliability.hpp
index 47d5973..b3627a9 100644
--- a/daemon/face/lp-reliability.hpp
+++ b/daemon/face/lp-reliability.hpp
@@ -26,12 +26,9 @@
#ifndef NFD_DAEMON_FACE_LP_RELIABILITY_HPP
#define NFD_DAEMON_FACE_LP_RELIABILITY_HPP
-#include "core/common.hpp"
#include "core/rtt-estimator.hpp"
#include "core/scheduler.hpp"
-#include "face-log.hpp"
-
#include <ndn-cxx/lp/packet.hpp>
#include <ndn-cxx/lp/sequence.hpp>
diff --git a/daemon/face/tcp-channel.cpp b/daemon/face/tcp-channel.cpp
index 978f540..f9a8eb7 100644
--- a/daemon/face/tcp-channel.cpp
+++ b/daemon/face/tcp-channel.cpp
@@ -38,9 +38,10 @@
TcpChannel::TcpChannel(const tcp::Endpoint& localEndpoint)
: m_localEndpoint(localEndpoint)
, m_acceptor(getGlobalIoService())
- , m_acceptSocket(getGlobalIoService())
+ , m_socket(getGlobalIoService())
{
setUri(FaceUri(m_localEndpoint));
+ NFD_LOG_CHAN_INFO("Creating channel");
}
void
@@ -49,20 +50,20 @@
int backlog/* = tcp::acceptor::max_connections*/)
{
if (isListening()) {
- NFD_LOG_WARN("[" << m_localEndpoint << "] Already listening");
+ NFD_LOG_CHAN_WARN("Already listening");
return;
}
m_acceptor.open(m_localEndpoint.protocol());
m_acceptor.set_option(ip::tcp::acceptor::reuse_address(true));
- if (m_localEndpoint.address().is_v6())
+ if (m_localEndpoint.address().is_v6()) {
m_acceptor.set_option(ip::v6_only(true));
-
+ }
m_acceptor.bind(m_localEndpoint);
m_acceptor.listen(backlog);
- // start accepting connections
accept(onFaceCreated, onAcceptFailed);
+ NFD_LOG_CHAN_DEBUG("Started listening");
}
void
@@ -74,25 +75,26 @@
{
auto it = m_channelFaces.find(remoteEndpoint);
if (it != m_channelFaces.end()) {
+ NFD_LOG_CHAN_TRACE("Reusing existing face for " << remoteEndpoint);
onFaceCreated(it->second);
return;
}
auto clientSocket = make_shared<ip::tcp::socket>(ref(getGlobalIoService()));
+ auto timeoutEvent = scheduler::schedule(timeout, bind(&TcpChannel::handleConnectTimeout, this,
+ remoteEndpoint, clientSocket, onConnectFailed));
- scheduler::EventId connectTimeoutEvent = scheduler::schedule(timeout,
- bind(&TcpChannel::handleConnectTimeout, this, clientSocket, onConnectFailed));
-
+ NFD_LOG_CHAN_TRACE("Connecting to " << remoteEndpoint);
clientSocket->async_connect(remoteEndpoint,
bind(&TcpChannel::handleConnect, this,
- boost::asio::placeholders::error, clientSocket,
- wantLocalFieldsEnabled, connectTimeoutEvent,
+ boost::asio::placeholders::error, remoteEndpoint,
+ clientSocket, wantLocalFieldsEnabled, timeoutEvent,
onFaceCreated, onConnectFailed));
}
void
TcpChannel::createFace(ip::tcp::socket&& socket,
- bool isOnDemand,
+ ndn::nfd::FacePersistency persistency,
bool wantLocalFieldsEnabled,
const FaceCreatedCallback& onFaceCreated)
{
@@ -101,27 +103,21 @@
auto it = m_channelFaces.find(remoteEndpoint);
if (it == m_channelFaces.end()) {
- auto persistency = isOnDemand ? ndn::nfd::FACE_PERSISTENCY_ON_DEMAND
- : ndn::nfd::FACE_PERSISTENCY_PERSISTENT;
auto linkService = make_unique<GenericLinkService>();
auto options = linkService->getOptions();
options.allowLocalFields = wantLocalFieldsEnabled;
linkService->setOptions(options);
auto transport = make_unique<TcpTransport>(std::move(socket), persistency);
-
face = make_shared<Face>(std::move(linkService), std::move(transport));
m_channelFaces[remoteEndpoint] = face;
- connectFaceClosedSignal(*face,
- [this, remoteEndpoint] {
- NFD_LOG_TRACE("Erasing " << remoteEndpoint << " from channel face map");
- m_channelFaces.erase(remoteEndpoint);
- });
+ connectFaceClosedSignal(*face, [this, remoteEndpoint] { m_channelFaces.erase(remoteEndpoint); });
}
else {
// we already have a face for this endpoint, just reuse it
face = it->second;
+ NFD_LOG_CHAN_TRACE("Reusing existing face for " << remoteEndpoint);
boost::system::error_code error;
socket.shutdown(ip::tcp::socket::shutdown_both, error);
@@ -137,9 +133,9 @@
TcpChannel::accept(const FaceCreatedCallback& onFaceCreated,
const FaceCreationFailedCallback& onAcceptFailed)
{
- m_acceptor.async_accept(m_acceptSocket, bind(&TcpChannel::handleAccept, this,
- boost::asio::placeholders::error,
- onFaceCreated, onAcceptFailed));
+ m_acceptor.async_accept(m_socket, bind(&TcpChannel::handleAccept, this,
+ boost::asio::placeholders::error,
+ onFaceCreated, onAcceptFailed));
}
void
@@ -148,18 +144,18 @@
const FaceCreationFailedCallback& onAcceptFailed)
{
if (error) {
- if (error == boost::asio::error::operation_aborted) // when the socket is closed by someone
- return;
-
- NFD_LOG_DEBUG("[" << m_localEndpoint << "] Accept failed: " << error.message());
- if (onAcceptFailed)
- onAcceptFailed(500, "Accept failed: " + error.message());
+ if (error != boost::asio::error::operation_aborted) {
+ NFD_LOG_CHAN_DEBUG("Accept failed: " << error.message());
+ if (onAcceptFailed)
+ onAcceptFailed(500, "Accept failed: " + error.message());
+ }
return;
}
- NFD_LOG_DEBUG("[" << m_localEndpoint << "] Connection from " << m_acceptSocket.remote_endpoint());
+ NFD_LOG_CHAN_TRACE("Incoming connection from " << m_socket.remote_endpoint());
- createFace(std::move(m_acceptSocket), true, false, onFaceCreated);
+ createFace(std::move(m_socket), ndn::nfd::FACE_PERSISTENCY_ON_DEMAND,
+ false, onFaceCreated);
// prepare accepting the next connection
accept(onFaceCreated, onAcceptFailed);
@@ -167,6 +163,7 @@
void
TcpChannel::handleConnect(const boost::system::error_code& error,
+ const tcp::Endpoint& remoteEndpoint,
const shared_ptr<ip::tcp::socket>& socket,
bool wantLocalFieldsEnabled,
const scheduler::EventId& connectTimeoutEvent,
@@ -184,35 +181,32 @@
#else
if (error) {
#endif
- if (error == boost::asio::error::operation_aborted) // when the socket is closed by someone
- return;
-
- NFD_LOG_WARN("[" << m_localEndpoint << "] Connect failed: " << error.message());
-
- socket->close();
-
- if (onConnectFailed)
- onConnectFailed(504, "Connect failed: " + error.message());
+ if (error != boost::asio::error::operation_aborted) {
+ NFD_LOG_CHAN_DEBUG("Connection to " << remoteEndpoint << " failed: " << error.message());
+ if (onConnectFailed)
+ onConnectFailed(504, "Connection failed: " + error.message());
+ }
return;
}
- NFD_LOG_DEBUG("[" << m_localEndpoint << "] Connected to " << socket->remote_endpoint());
-
- createFace(std::move(*socket), false, wantLocalFieldsEnabled, onFaceCreated);
+ NFD_LOG_CHAN_TRACE("Connected to " << socket->remote_endpoint());
+ createFace(std::move(*socket), ndn::nfd::FACE_PERSISTENCY_PERSISTENT,
+ wantLocalFieldsEnabled, onFaceCreated);
}
void
-TcpChannel::handleConnectTimeout(const shared_ptr<ip::tcp::socket>& socket,
+TcpChannel::handleConnectTimeout(const tcp::Endpoint& remoteEndpoint,
+ const shared_ptr<ip::tcp::socket>& socket,
const FaceCreationFailedCallback& onConnectFailed)
{
- NFD_LOG_DEBUG("Connect to remote endpoint timed out");
+ NFD_LOG_CHAN_DEBUG("Connection to " << remoteEndpoint << " timed out");
// abort the connection attempt
boost::system::error_code error;
socket->close(error);
if (onConnectFailed)
- onConnectFailed(504, "Connect to remote endpoint timed out");
+ onConnectFailed(504, "Connection timed out");
}
} // namespace face
diff --git a/daemon/face/tcp-channel.hpp b/daemon/face/tcp-channel.hpp
index 400cdf9..ea28c3e 100644
--- a/daemon/face/tcp-channel.hpp
+++ b/daemon/face/tcp-channel.hpp
@@ -95,7 +95,7 @@
private:
void
createFace(boost::asio::ip::tcp::socket&& socket,
- bool isOnDemand,
+ ndn::nfd::FacePersistency persistency,
bool wantLocalFieldsEnabled,
const FaceCreatedCallback& onFaceCreated);
@@ -110,6 +110,7 @@
void
handleConnect(const boost::system::error_code& error,
+ const tcp::Endpoint& remoteEndpoint,
const shared_ptr<boost::asio::ip::tcp::socket>& socket,
bool wantLocalFieldsEnabled,
const scheduler::EventId& connectTimeoutEvent,
@@ -117,15 +118,15 @@
const FaceCreationFailedCallback& onConnectFailed);
void
- handleConnectTimeout(const shared_ptr<boost::asio::ip::tcp::socket>& socket,
+ handleConnectTimeout(const tcp::Endpoint& remoteEndpoint,
+ const shared_ptr<boost::asio::ip::tcp::socket>& socket,
const FaceCreationFailedCallback& onConnectFailed);
private:
- std::map<tcp::Endpoint, shared_ptr<Face>> m_channelFaces;
-
- tcp::Endpoint m_localEndpoint;
+ const tcp::Endpoint m_localEndpoint;
boost::asio::ip::tcp::acceptor m_acceptor;
- boost::asio::ip::tcp::socket m_acceptSocket;
+ boost::asio::ip::tcp::socket m_socket;
+ std::map<tcp::Endpoint, shared_ptr<Face>> m_channelFaces;
};
} // namespace face
diff --git a/daemon/face/tcp-factory.cpp b/daemon/face/tcp-factory.cpp
index f87fbb6..5e8f3fb 100644
--- a/daemon/face/tcp-factory.cpp
+++ b/daemon/face/tcp-factory.cpp
@@ -24,7 +24,6 @@
*/
#include "tcp-factory.hpp"
-#include "core/logger.hpp"
namespace nfd {
namespace face {
@@ -229,8 +228,6 @@
channel = make_shared<TcpChannel>(endpoint);
m_channels[endpoint] = channel;
prohibitEndpoint(endpoint);
-
- NFD_LOG_DEBUG("Channel [" << endpoint << "] created");
return channel;
}
diff --git a/daemon/face/udp-channel.cpp b/daemon/face/udp-channel.cpp
index d22b23c..e1bcd35 100644
--- a/daemon/face/udp-channel.cpp
+++ b/daemon/face/udp-channel.cpp
@@ -42,6 +42,7 @@
, m_idleFaceTimeout(timeout)
{
setUri(FaceUri(m_localEndpoint));
+ NFD_LOG_CHAN_INFO("Creating channel");
}
void
@@ -55,9 +56,9 @@
face = createFace(remoteEndpoint, persistency).second;
}
catch (const boost::system::system_error& e) {
- NFD_LOG_WARN("[" << m_localEndpoint << "] Connect failed: " << e.what());
+ NFD_LOG_CHAN_DEBUG("Face creation for " << remoteEndpoint << " failed: " << e.what());
if (onConnectFailed)
- onConnectFailed(504, std::string("Connect failed: ") + e.what());
+ onConnectFailed(504, std::string("Face creation failed: ") + e.what());
return;
}
@@ -68,20 +69,22 @@
void
UdpChannel::listen(const FaceCreatedCallback& onFaceCreated,
- const FaceCreationFailedCallback& onReceiveFailed)
+ const FaceCreationFailedCallback& onFaceCreationFailed)
{
if (isListening()) {
- NFD_LOG_WARN("[" << m_localEndpoint << "] Already listening");
+ NFD_LOG_CHAN_WARN("Already listening");
return;
}
m_socket.open(m_localEndpoint.protocol());
m_socket.set_option(ip::udp::socket::reuse_address(true));
- if (m_localEndpoint.address().is_v6())
+ if (m_localEndpoint.address().is_v6()) {
m_socket.set_option(ip::v6_only(true));
-
+ }
m_socket.bind(m_localEndpoint);
- this->waitForNewPeer(onFaceCreated, onReceiveFailed);
+
+ waitForNewPeer(onFaceCreated, onFaceCreationFailed);
+ NFD_LOG_CHAN_DEBUG("Started listening");
}
void
@@ -103,16 +106,15 @@
const FaceCreationFailedCallback& onReceiveFailed)
{
if (error) {
- if (error == boost::asio::error::operation_aborted) // when the socket is closed by someone
- return;
-
- NFD_LOG_DEBUG("[" << m_localEndpoint << "] Receive failed: " << error.message());
- if (onReceiveFailed)
- onReceiveFailed(500, "Receive failed: " + error.message());
+ if (error != boost::asio::error::operation_aborted) {
+ NFD_LOG_CHAN_DEBUG("Receive failed: " << error.message());
+ if (onReceiveFailed)
+ onReceiveFailed(500, "Receive failed: " + error.message());
+ }
return;
}
- NFD_LOG_DEBUG("[" << m_localEndpoint << "] New peer " << m_remoteEndpoint);
+ NFD_LOG_CHAN_TRACE("New peer " << m_remoteEndpoint);
bool isCreated = false;
shared_ptr<Face> face;
@@ -120,10 +122,9 @@
std::tie(isCreated, face) = createFace(m_remoteEndpoint, ndn::nfd::FACE_PERSISTENCY_ON_DEMAND);
}
catch (const boost::system::system_error& e) {
- NFD_LOG_WARN("[" << m_localEndpoint << "] Failed to create face for peer "
- << m_remoteEndpoint << ": " << e.what());
+ NFD_LOG_CHAN_DEBUG("Face creation for " << m_remoteEndpoint << " failed: " << e.what());
if (onReceiveFailed)
- onReceiveFailed(504, "Failed to create face for peer");
+ onReceiveFailed(504, std::string("Face creation failed: ") + e.what());
return;
}
@@ -131,17 +132,20 @@
onFaceCreated(face);
// dispatch the datagram to the face for processing
- static_cast<UnicastUdpTransport*>(face->getTransport())->receiveDatagram(m_inputBuffer, nBytesReceived, error);
+ auto* transport = static_cast<UnicastUdpTransport*>(face->getTransport());
+ transport->receiveDatagram(m_inputBuffer, nBytesReceived, error);
- this->waitForNewPeer(onFaceCreated, onReceiveFailed);
+ waitForNewPeer(onFaceCreated, onReceiveFailed);
}
std::pair<bool, shared_ptr<Face>>
-UdpChannel::createFace(const udp::Endpoint& remoteEndpoint, ndn::nfd::FacePersistency persistency)
+UdpChannel::createFace(const udp::Endpoint& remoteEndpoint,
+ ndn::nfd::FacePersistency persistency)
{
auto it = m_channelFaces.find(remoteEndpoint);
if (it != m_channelFaces.end()) {
// we already have a face for this endpoint, so reuse it
+ NFD_LOG_CHAN_TRACE("Reusing existing face for " << remoteEndpoint);
return {false, it->second};
}
@@ -156,11 +160,7 @@
auto face = make_shared<Face>(std::move(linkService), std::move(transport));
m_channelFaces[remoteEndpoint] = face;
- connectFaceClosedSignal(*face,
- [this, remoteEndpoint] {
- NFD_LOG_TRACE("Erasing " << remoteEndpoint << " from channel face map");
- m_channelFaces.erase(remoteEndpoint);
- });
+ connectFaceClosedSignal(*face, [this, remoteEndpoint] { m_channelFaces.erase(remoteEndpoint); });
return {true, face};
}
diff --git a/daemon/face/udp-channel.hpp b/daemon/face/udp-channel.hpp
index fe849a6..c4d261e 100644
--- a/daemon/face/udp-channel.hpp
+++ b/daemon/face/udp-channel.hpp
@@ -77,18 +77,16 @@
/**
* \brief Enable listening on the local endpoint, accept connections,
* and create faces when remote host makes a connection
- * \param onFaceCreated Callback to notify successful creation of the face
- * \param onReceiveFailed Callback to notify when channel fails
+ *
+ * \param onFaceCreated Callback to notify successful creation of a face
+ * \param onFaceCreationFailed Callback to notify errors
*
* Once a face is created, if it doesn't send/receive anything for
* a period of time equal to timeout, it will be destroyed
- * \todo this functionality has to be implemented
- *
- * \throws UdpChannel::Error if called multiple times
*/
void
listen(const FaceCreatedCallback& onFaceCreated,
- const FaceCreationFailedCallback& onReceiveFailed);
+ const FaceCreationFailedCallback& onFaceCreationFailed);
private:
void
@@ -106,29 +104,16 @@
const FaceCreationFailedCallback& onReceiveFailed);
std::pair<bool, shared_ptr<Face>>
- createFace(const udp::Endpoint& remoteEndpoint, ndn::nfd::FacePersistency persistency);
+ createFace(const udp::Endpoint& remoteEndpoint,
+ ndn::nfd::FacePersistency persistency);
private:
+ const udp::Endpoint m_localEndpoint;
+ udp::Endpoint m_remoteEndpoint; ///< The latest peer that started communicating with us
+ boost::asio::ip::udp::socket m_socket; ///< Socket used to "accept" new peers
std::map<udp::Endpoint, shared_ptr<Face>> m_channelFaces;
-
- udp::Endpoint m_localEndpoint;
-
- /**
- * \brief The latest peer that started communicating with us
- */
- udp::Endpoint m_remoteEndpoint;
-
- /**
- * \brief Socket used to "accept" new communication
- */
- boost::asio::ip::udp::socket m_socket;
-
- /**
- * \brief When this timeout expires, all idle on-demand faces will be closed
- */
- time::seconds m_idleFaceTimeout;
-
uint8_t m_inputBuffer[ndn::MAX_NDN_PACKET_SIZE];
+ time::seconds m_idleFaceTimeout; ///< Timeout for automatic closure of idle on-demand faces
};
} // namespace face
diff --git a/daemon/face/udp-factory.cpp b/daemon/face/udp-factory.cpp
index 3cc1818..a544edf 100644
--- a/daemon/face/udp-factory.cpp
+++ b/daemon/face/udp-factory.cpp
@@ -310,8 +310,6 @@
UdpFactory::createChannel(const udp::Endpoint& endpoint,
const time::seconds& timeout)
{
- NFD_LOG_DEBUG("Creating unicast channel " << endpoint);
-
auto channel = findChannel(endpoint);
if (channel)
return channel;
@@ -331,7 +329,6 @@
channel = std::make_shared<UdpChannel>(endpoint, timeout);
m_channels[endpoint] = channel;
prohibitEndpoint(endpoint);
-
return channel;
}
diff --git a/daemon/face/unix-stream-channel.cpp b/daemon/face/unix-stream-channel.cpp
index b253a70..87af308 100644
--- a/daemon/face/unix-stream-channel.cpp
+++ b/daemon/face/unix-stream-channel.cpp
@@ -43,6 +43,7 @@
, m_size(0)
{
setUri(FaceUri(m_endpoint));
+ NFD_LOG_CHAN_INFO("Creating channel");
}
UnixStreamChannel::~UnixStreamChannel()
@@ -52,7 +53,7 @@
// and ignore any errors
boost::system::error_code error;
m_acceptor.close(error);
- NFD_LOG_DEBUG("[" << m_endpoint << "] Removing socket file");
+ NFD_LOG_CHAN_DEBUG("Removing socket file");
boost::filesystem::remove(m_endpoint.path(), error);
}
}
@@ -63,7 +64,7 @@
int backlog/* = acceptor::max_connections*/)
{
if (isListening()) {
- NFD_LOG_WARN("[" << m_endpoint << "] Already listening");
+ NFD_LOG_CHAN_WARN("Already listening");
return;
}
@@ -76,8 +77,7 @@
boost::system::error_code error;
boost::asio::local::stream_protocol::socket socket(getGlobalIoService());
socket.connect(m_endpoint, error);
- NFD_LOG_TRACE("[" << m_endpoint << "] connect() on existing socket file returned: "
- + error.message());
+ NFD_LOG_CHAN_TRACE("connect() on existing socket file returned: " << error.message());
if (!error) {
// someone answered, leave the socket alone
BOOST_THROW_EXCEPTION(Error("Socket file at " + m_endpoint.path()
@@ -87,7 +87,7 @@
error == boost::asio::error::timed_out) {
// no one is listening on the remote side,
// we can safely remove the stale socket
- NFD_LOG_DEBUG("[" << m_endpoint << "] Removing stale socket file");
+ NFD_LOG_CHAN_DEBUG("Removing stale socket file");
fs::remove(socketPath);
}
}
@@ -100,12 +100,11 @@
m_acceptor.listen(backlog);
if (::chmod(m_endpoint.path().c_str(), 0666) < 0) {
- BOOST_THROW_EXCEPTION(Error("chmod(" + m_endpoint.path() + ") failed: " +
- std::strerror(errno)));
+ BOOST_THROW_EXCEPTION(Error("chmod(" + m_endpoint.path() + ") failed: " + std::strerror(errno)));
}
- // start accepting connections
accept(onFaceCreated, onAcceptFailed);
+ NFD_LOG_CHAN_DEBUG("Started listening");
}
void
@@ -123,16 +122,15 @@
const FaceCreationFailedCallback& onAcceptFailed)
{
if (error) {
- if (error == boost::asio::error::operation_aborted) // when the socket is closed by someone
- return;
-
- NFD_LOG_DEBUG("[" << m_endpoint << "] Accept failed: " << error.message());
- if (onAcceptFailed)
- onAcceptFailed(500, "Accept failed: " + error.message());
+ if (error != boost::asio::error::operation_aborted) {
+ NFD_LOG_CHAN_DEBUG("Accept failed: " << error.message());
+ if (onAcceptFailed)
+ onAcceptFailed(500, "Accept failed: " + error.message());
+ }
return;
}
- NFD_LOG_DEBUG("[" << m_endpoint << "] Incoming connection");
+ NFD_LOG_CHAN_TRACE("Incoming connection via fd " << m_socket.native_handle());
auto linkService = make_unique<GenericLinkService>();
auto transport = make_unique<UnixStreamTransport>(std::move(m_socket));
diff --git a/daemon/face/unix-stream-channel.hpp b/daemon/face/unix-stream-channel.hpp
index 8f30db5..310030b 100644
--- a/daemon/face/unix-stream-channel.hpp
+++ b/daemon/face/unix-stream-channel.hpp
@@ -106,7 +106,7 @@
const FaceCreationFailedCallback& onAcceptFailed);
private:
- unix_stream::Endpoint m_endpoint;
+ const unix_stream::Endpoint m_endpoint;
boost::asio::local::stream_protocol::acceptor m_acceptor;
boost::asio::local::stream_protocol::socket m_socket;
size_t m_size;
diff --git a/daemon/face/unix-stream-factory.cpp b/daemon/face/unix-stream-factory.cpp
index 33a488f..a7aeadb 100644
--- a/daemon/face/unix-stream-factory.cpp
+++ b/daemon/face/unix-stream-factory.cpp
@@ -24,7 +24,6 @@
*/
#include "unix-stream-factory.hpp"
-#include "core/logger.hpp"
#include <boost/filesystem.hpp>
diff --git a/daemon/face/websocket-channel.cpp b/daemon/face/websocket-channel.cpp
index daf362c..02ae079 100644
--- a/daemon/face/websocket-channel.cpp
+++ b/daemon/face/websocket-channel.cpp
@@ -35,9 +35,10 @@
WebSocketChannel::WebSocketChannel(const websocket::Endpoint& localEndpoint)
: m_localEndpoint(localEndpoint)
- , m_pingInterval(10000)
+ , m_pingInterval(time::seconds(10))
{
setUri(FaceUri(m_localEndpoint, "ws"));
+ NFD_LOG_CHAN_INFO("Creating channel");
// Be quiet
m_server.clear_access_channels(websocketpp::log::alevel::all);
@@ -81,7 +82,7 @@
static_cast<WebSocketTransport*>(it->second->getTransport())->handlePongTimeout();
}
else {
- NFD_LOG_WARN("Pong timeout on unknown transport");
+ NFD_LOG_CHAN_WARN("Pong timeout on unknown transport");
}
}
@@ -93,7 +94,7 @@
static_cast<WebSocketTransport*>(it->second->getTransport())->handlePong();
}
else {
- NFD_LOG_WARN("Pong received on unknown transport");
+ NFD_LOG_CHAN_WARN("Pong received on unknown transport");
}
}
@@ -106,17 +107,20 @@
static_cast<WebSocketTransport*>(it->second->getTransport())->receiveMessage(msg->get_payload());
}
else {
- NFD_LOG_WARN("Message received on unknown transport");
+ NFD_LOG_CHAN_WARN("Message received on unknown transport");
}
}
void
WebSocketChannel::handleOpen(websocketpp::connection_hdl hdl)
{
+ NFD_LOG_CHAN_TRACE("Incoming connection from " << m_server.get_con_from_hdl(hdl)->get_remote_endpoint());
+
auto linkService = make_unique<GenericLinkService>();
auto transport = make_unique<WebSocketTransport>(hdl, ref(m_server), m_pingInterval);
auto face = make_shared<Face>(std::move(linkService), std::move(transport));
+ BOOST_ASSERT(m_channelFaces.count(hdl) == 0);
m_channelFaces[hdl] = face;
connectFaceClosedSignal(*face, [this, hdl] { m_channelFaces.erase(hdl); });
@@ -131,21 +135,23 @@
it->second->close();
}
else {
- NFD_LOG_WARN("Close on unknown transport");
+ NFD_LOG_CHAN_WARN("Close on unknown transport");
}
}
void
WebSocketChannel::listen(const FaceCreatedCallback& onFaceCreated)
{
- if (m_server.is_listening()) {
- NFD_LOG_WARN("[" << m_localEndpoint << "] Already listening");
+ if (isListening()) {
+ NFD_LOG_CHAN_WARN("Already listening");
return;
}
m_onFaceCreatedCallback = onFaceCreated;
+
m_server.listen(m_localEndpoint);
m_server.start_accept();
+ NFD_LOG_CHAN_DEBUG("Started listening");
}
} // namespace face
diff --git a/daemon/face/websocket-channel.hpp b/daemon/face/websocket-channel.hpp
index 7da5647..c1b2805 100644
--- a/daemon/face/websocket-channel.hpp
+++ b/daemon/face/websocket-channel.hpp
@@ -103,12 +103,10 @@
handleClose(websocketpp::connection_hdl hdl);
private:
- websocket::Endpoint m_localEndpoint;
+ const websocket::Endpoint m_localEndpoint;
websocket::Server m_server;
-
std::map<websocketpp::connection_hdl, shared_ptr<Face>,
std::owner_less<websocketpp::connection_hdl>> m_channelFaces;
-
FaceCreatedCallback m_onFaceCreatedCallback;
time::milliseconds m_pingInterval;
};
diff --git a/daemon/face/websocket-factory.cpp b/daemon/face/websocket-factory.cpp
index 3b813c0..a0b58e5 100644
--- a/daemon/face/websocket-factory.cpp
+++ b/daemon/face/websocket-factory.cpp
@@ -24,7 +24,6 @@
*/
#include "websocket-factory.hpp"
-#include "core/logger.hpp"
namespace nfd {
namespace face {