30 #include <boost/date_time/posix_time/posix_time.hpp> 37 : m_localEndpoint(localEndpoint)
38 , m_isListening(false)
39 , m_pingInterval(10000)
44 m_server.clear_access_channels(websocketpp::log::alevel::all);
45 m_server.clear_error_channels(websocketpp::log::alevel::all);
47 m_server.set_message_handler(bind(&WebSocketChannel::handleMessage,
this, _1, _2));
48 m_server.set_open_handler(bind(&WebSocketChannel::handleOpen,
this, _1));
49 m_server.set_close_handler(bind(&WebSocketChannel::handleClose,
this, _1));
53 m_server.set_pong_handler(bind(&WebSocketChannel::handlePong,
this, _1, _2));
54 m_server.set_pong_timeout_handler(bind(&WebSocketChannel::handlePongTimeout,
this, _1, _2));
57 m_server.set_reuse_addr(
true);
61 WebSocketChannel::setPingInterval(time::milliseconds interval)
63 m_pingInterval = interval;
67 WebSocketChannel::setPongTimeout(time::milliseconds timeout)
69 m_server.set_pong_timeout(static_cast<long>(timeout.count()));
73 WebSocketChannel::handlePongTimeout(websocketpp::connection_hdl hdl, std::string msg)
75 auto it = m_channelFaces.find(hdl);
76 if (it != m_channelFaces.end()) {
77 NFD_LOG_TRACE(__func__ <<
": " << it->second->getRemoteUri());
79 m_channelFaces.erase(it);
84 WebSocketChannel::handlePong(websocketpp::connection_hdl hdl, std::string msg)
86 auto it = m_channelFaces.find(hdl);
87 if (it != m_channelFaces.end()) {
93 WebSocketChannel::handleMessage(websocketpp::connection_hdl hdl,
94 websocket::Server::message_ptr msg)
96 auto it = m_channelFaces.find(hdl);
97 if (it != m_channelFaces.end()) {
98 it->second->handleReceive(msg->get_payload());
103 WebSocketChannel::handleOpen(websocketpp::connection_hdl hdl)
106 std::string remote =
"wsclient://" + m_server.get_con_from_hdl(hdl)->get_remote_endpoint();
107 auto face = make_shared<WebSocketFace>(
FaceUri(remote), this->
getUri(),
109 m_onFaceCreatedCallback(face);
110 m_channelFaces[hdl] = face;
113 bind(&WebSocketChannel::sendPing,
115 face->setPingEventId(pingEvent);
119 websocketpp::lib::error_code ec;
120 m_server.close(hdl, websocketpp::close::status::normal,
"closed by channel", ec);
123 catch (
const websocketpp::exception& e) {
124 NFD_LOG_WARN(
"Cannot get remote connection: " << e.what());
125 websocketpp::lib::error_code ec;
126 m_server.close(hdl, websocketpp::close::status::normal,
"closed by channel", ec);
132 WebSocketChannel::sendPing(websocketpp::connection_hdl hdl)
134 auto it = m_channelFaces.find(hdl);
135 if (it != m_channelFaces.end()) {
136 NFD_LOG_TRACE(
"Sending ping to " << it->second->getRemoteUri());
138 websocketpp::lib::error_code ec;
139 m_server.ping(hdl,
"NFD-WebSocket", ec);
142 NFD_LOG_WARN(
"Failed to ping " << it->second->getRemoteUri() <<
": " << ec.message());
144 m_channelFaces.erase(it);
150 bind(&WebSocketChannel::sendPing,
152 it->second->setPingEventId(pingEvent);
157 WebSocketChannel::handleClose(websocketpp::connection_hdl hdl)
159 auto it = m_channelFaces.find(hdl);
160 if (it != m_channelFaces.end()) {
161 NFD_LOG_TRACE(__func__ <<
": " << it->second->getRemoteUri());
163 m_channelFaces.erase(it);
171 NFD_LOG_WARN(
"[" << m_localEndpoint <<
"] Already listening");
174 m_isListening =
true;
176 m_onFaceCreatedCallback = onFaceCreated;
177 m_server.listen(m_localEndpoint);
178 m_server.start_accept();
184 return m_channelFaces.size();
represents the underlying protocol and address used by a Face
std::shared_ptr< ns3::EventId > EventId
#define NFD_LOG_WARN(expression)
WebSocketChannel(const websocket::Endpoint &localEndpoint)
Create WebSocket channel for the local endpoint.
const FaceUri & getUri() const
Copyright (c) 2011-2015 Regents of the University of California.
size_t size() const
Get number of faces in the channel.
EventId schedule(const time::nanoseconds &after, const std::function< void()> &event)
schedule an event
void listen(const FaceCreatedCallback &onFaceCreated)
Enable listening on the local endpoint, accept connections, and create faces when remote host makes a...
void setUri(const FaceUri &uri)
#define NFD_LOG_INIT(name)
#define NFD_LOG_TRACE(expression)
boost::asio::io_service & getGlobalIoService()
boost::asio::ip::tcp::endpoint Endpoint
function< void(const shared_ptr< Face > &newFace)> FaceCreatedCallback
Prototype for the callback called when face is created (as a response to incoming connection or after...