face: Stop using shared_ptr to manage acceptors
This commit also includes a major cleanup of all channels.
Change-Id: I10db9709e0cba6a0691a86482c60b5dbb2956f68
Refs: #2613
diff --git a/daemon/face/websocket-channel.cpp b/daemon/face/websocket-channel.cpp
index 016dcac..855dd99 100644
--- a/daemon/face/websocket-channel.cpp
+++ b/daemon/face/websocket-channel.cpp
@@ -24,6 +24,8 @@
*/
#include "websocket-channel.hpp"
+#include "core/global-io.hpp"
+#include "core/scheduler.hpp"
#include <boost/date_time/posix_time/posix_time.hpp>
@@ -31,13 +33,13 @@
NFD_LOG_INIT("WebSocketChannel");
-using namespace boost::asio;
-
WebSocketChannel::WebSocketChannel(const websocket::Endpoint& localEndpoint)
: m_localEndpoint(localEndpoint)
, m_isListening(false)
, m_pingInterval(10000)
{
+ setUri(FaceUri(m_localEndpoint, "ws"));
+
// Setup WebSocket server
m_server.clear_access_channels(websocketpp::log::alevel::all);
m_server.clear_error_channels(websocketpp::log::alevel::all);
@@ -46,19 +48,19 @@
m_server.set_open_handler(bind(&WebSocketChannel::handleOpen, this, _1));
m_server.set_close_handler(bind(&WebSocketChannel::handleClose, this, _1));
m_server.init_asio(&getGlobalIoService());
+
+ // Detect disconnections using ping-pong messages
+ m_server.set_pong_handler(bind(&WebSocketChannel::handlePong, this, _1, _2));
+ m_server.set_pong_timeout_handler(bind(&WebSocketChannel::handlePongTimeout, this, _1, _2));
+
// Always set SO_REUSEADDR flag
m_server.set_reuse_addr(true);
-
- // Detect disconnection using PONG message
- m_server.set_pong_handler(bind(&WebSocketChannel::handlePong, this, _1, _2));
- m_server.set_pong_timeout_handler(bind(&WebSocketChannel::handlePongTimeout,
- this, _1, _2));
-
- this->setUri(FaceUri(localEndpoint, "ws"));
}
-WebSocketChannel::~WebSocketChannel()
+void
+WebSocketChannel::setPingInterval(time::milliseconds interval)
{
+ m_pingInterval = interval;
}
void
@@ -70,56 +72,53 @@
void
WebSocketChannel::handlePongTimeout(websocketpp::connection_hdl hdl, std::string msg)
{
- ChannelFaceMap::iterator it = m_channelFaces.find(hdl);
- if (it != m_channelFaces.end())
- {
- it->second->close();
- NFD_LOG_DEBUG("handlePongTimeout: remove " << it->second->getRemoteUri());
- m_channelFaces.erase(it);
- }
+ auto it = m_channelFaces.find(hdl);
+ if (it != m_channelFaces.end()) {
+ NFD_LOG_TRACE(__func__ << ": " << it->second->getRemoteUri());
+ it->second->close();
+ m_channelFaces.erase(it);
+ }
}
void
WebSocketChannel::handlePong(websocketpp::connection_hdl hdl, std::string msg)
{
- ChannelFaceMap::iterator it = m_channelFaces.find(hdl);
- if (it != m_channelFaces.end())
- {
- NFD_LOG_TRACE("handlePong: from " << it->second->getRemoteUri());
- }
+ auto it = m_channelFaces.find(hdl);
+ if (it != m_channelFaces.end()) {
+ NFD_LOG_TRACE("Pong from " << it->second->getRemoteUri());
+ }
}
void
WebSocketChannel::handleMessage(websocketpp::connection_hdl hdl,
websocket::Server::message_ptr msg)
{
- ChannelFaceMap::iterator it = m_channelFaces.find(hdl);
- if (it != m_channelFaces.end())
- {
- it->second->handleReceive(msg->get_payload());
- }
+ auto it = m_channelFaces.find(hdl);
+ if (it != m_channelFaces.end()) {
+ it->second->handleReceive(msg->get_payload());
+ }
}
void
WebSocketChannel::handleOpen(websocketpp::connection_hdl hdl)
{
std::string remote;
- try
- {
- remote = "wsclient://" + m_server.get_con_from_hdl(hdl)->get_remote_endpoint();
- }
- catch (websocketpp::lib::error_code&)
- {
- NFD_LOG_DEBUG("handleOpen: cannot get remote uri");
- websocketpp::lib::error_code ecode;
- m_server.close(hdl, websocketpp::close::status::normal, "closed by channel", ecode);
- }
- shared_ptr<WebSocketFace> face = ndn::make_shared<WebSocketFace>(FaceUri(remote), this->getUri(),
- hdl, ref(m_server));
+ try {
+ remote = "wsclient://" + m_server.get_con_from_hdl(hdl)->get_remote_endpoint();
+ }
+ catch (const websocketpp::lib::error_code& e) {
+ NFD_LOG_WARN("Cannot get remote URI");
+ websocketpp::lib::error_code error;
+ m_server.close(hdl, websocketpp::close::status::normal, "closed by channel", error);
+ return;
+ }
+
+ auto face = make_shared<WebSocketFace>(FaceUri(remote), this->getUri(),
+ hdl, ref(m_server));
m_onFaceCreatedCallback(face);
m_channelFaces[hdl] = face;
- // Schedule PING message
+ // Schedule ping message
scheduler::EventId pingEvent = scheduler::schedule(m_pingInterval,
bind(&WebSocketChannel::sendPing, this, hdl));
face->setPingEventId(pingEvent);
@@ -128,62 +127,49 @@
void
WebSocketChannel::sendPing(websocketpp::connection_hdl hdl)
{
- ChannelFaceMap::iterator it = m_channelFaces.find(hdl);
- if (it != m_channelFaces.end())
- {
- try
- {
- m_server.ping(hdl, "NFD-WebSocket");
- }
- catch (websocketpp::lib::error_code&)
- {
- it->second->close();
- NFD_LOG_DEBUG("sendPing: failed to ping " << it->second->getRemoteUri());
- m_channelFaces.erase(it);
- }
+ auto it = m_channelFaces.find(hdl);
+ if (it != m_channelFaces.end()) {
+ NFD_LOG_TRACE("Sending ping to " << it->second->getRemoteUri());
- NFD_LOG_TRACE("sendPing: to " << it->second->getRemoteUri());
-
- // Schedule next PING message
- scheduler::EventId pingEvent = scheduler::schedule(m_pingInterval,
- bind(&WebSocketChannel::sendPing, this, hdl));
- it->second->setPingEventId(pingEvent);
+ try {
+ m_server.ping(hdl, "NFD-WebSocket");
}
+ catch (const websocketpp::lib::error_code& e) {
+ NFD_LOG_WARN("Failed to ping " << it->second->getRemoteUri());
+ it->second->close();
+ m_channelFaces.erase(it);
+ return;
+ }
+
+ // Schedule next ping message
+ scheduler::EventId pingEvent = scheduler::schedule(m_pingInterval,
+ bind(&WebSocketChannel::sendPing, this, hdl));
+ it->second->setPingEventId(pingEvent);
+ }
}
void
WebSocketChannel::handleClose(websocketpp::connection_hdl hdl)
{
- ChannelFaceMap::iterator it = m_channelFaces.find(hdl);
- if (it != m_channelFaces.end())
- {
- it->second->close();
- NFD_LOG_DEBUG("handleClose: remove " << it->second->getRemoteUri());
- m_channelFaces.erase(it);
- }
+ auto it = m_channelFaces.find(hdl);
+ if (it != m_channelFaces.end()) {
+ NFD_LOG_TRACE(__func__ << ": " << it->second->getRemoteUri());
+ it->second->close();
+ m_channelFaces.erase(it);
+ }
}
-
void
WebSocketChannel::listen(const FaceCreatedCallback& onFaceCreated)
{
- if (m_isListening)
- {
- throw Error("Listen already called on this channel");
- }
+ if (isListening()) {
+ NFD_LOG_WARN("[" << m_localEndpoint << "] Already listening");
+ return;
+ }
m_isListening = true;
m_onFaceCreatedCallback = onFaceCreated;
-
- try
- {
- m_server.listen(m_localEndpoint);
- }
- catch (websocketpp::lib::error_code ec)
- {
- throw Error("Failed to listen on local endpoint");
- }
-
+ m_server.listen(m_localEndpoint);
m_server.start_accept();
}