blob: 016dcacee0cc72b663b7370495e1099d5f591bf7 [file] [log] [blame]
Wentao Shang53df1632014-04-21 12:01:32 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Junxiao Shi1e46be32015-01-08 20:18:05 -07003 * Copyright (c) 2014-2015, Regents of the University of California,
4 * Arizona Board of Regents,
5 * Colorado State University,
6 * University Pierre & Marie Curie, Sorbonne University,
7 * Washington University in St. Louis,
8 * Beijing Institute of Technology,
9 * The University of Memphis.
Wentao Shang53df1632014-04-21 12:01:32 -070010 *
11 * This file is part of NFD (Named Data Networking Forwarding Daemon).
12 * See AUTHORS.md for complete list of NFD authors and contributors.
13 *
14 * NFD is free software: you can redistribute it and/or modify it under the terms
15 * of the GNU General Public License as published by the Free Software Foundation,
16 * either version 3 of the License, or (at your option) any later version.
17 *
18 * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
19 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
20 * PURPOSE. See the GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License along with
23 * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
Junxiao Shia1937bf2014-11-06 11:43:40 -070024 */
Wentao Shang53df1632014-04-21 12:01:32 -070025
26#include "websocket-channel.hpp"
Wentao Shang53df1632014-04-21 12:01:32 -070027
Wentao Shang98733142014-09-17 12:13:57 -070028#include <boost/date_time/posix_time/posix_time.hpp>
29
Wentao Shang53df1632014-04-21 12:01:32 -070030namespace nfd {
31
32NFD_LOG_INIT("WebSocketChannel");
33
34using namespace boost::asio;
35
36WebSocketChannel::WebSocketChannel(const websocket::Endpoint& localEndpoint)
37 : m_localEndpoint(localEndpoint)
38 , m_isListening(false)
Wentao Shang98733142014-09-17 12:13:57 -070039 , m_pingInterval(10000)
Wentao Shang53df1632014-04-21 12:01:32 -070040{
41 // Setup WebSocket server
42 m_server.clear_access_channels(websocketpp::log::alevel::all);
43 m_server.clear_error_channels(websocketpp::log::alevel::all);
44
45 m_server.set_message_handler(bind(&WebSocketChannel::handleMessage, this, _1, _2));
46 m_server.set_open_handler(bind(&WebSocketChannel::handleOpen, this, _1));
47 m_server.set_close_handler(bind(&WebSocketChannel::handleClose, this, _1));
48 m_server.init_asio(&getGlobalIoService());
Wentao Shang93ef6c92014-06-19 11:59:17 -040049 // Always set SO_REUSEADDR flag
50 m_server.set_reuse_addr(true);
Wentao Shang53df1632014-04-21 12:01:32 -070051
Wentao Shang98733142014-09-17 12:13:57 -070052 // Detect disconnection using PONG message
53 m_server.set_pong_handler(bind(&WebSocketChannel::handlePong, this, _1, _2));
54 m_server.set_pong_timeout_handler(bind(&WebSocketChannel::handlePongTimeout,
55 this, _1, _2));
56
Wentao Shang53df1632014-04-21 12:01:32 -070057 this->setUri(FaceUri(localEndpoint, "ws"));
58}
59
60WebSocketChannel::~WebSocketChannel()
61{
62}
63
64void
Wentao Shang98733142014-09-17 12:13:57 -070065WebSocketChannel::setPongTimeout(time::milliseconds timeout)
66{
67 m_server.set_pong_timeout(static_cast<long>(timeout.count()));
68}
69
70void
71WebSocketChannel::handlePongTimeout(websocketpp::connection_hdl hdl, std::string msg)
72{
73 ChannelFaceMap::iterator it = m_channelFaces.find(hdl);
74 if (it != m_channelFaces.end())
75 {
76 it->second->close();
77 NFD_LOG_DEBUG("handlePongTimeout: remove " << it->second->getRemoteUri());
78 m_channelFaces.erase(it);
79 }
80}
81
82void
83WebSocketChannel::handlePong(websocketpp::connection_hdl hdl, std::string msg)
84{
85 ChannelFaceMap::iterator it = m_channelFaces.find(hdl);
86 if (it != m_channelFaces.end())
87 {
88 NFD_LOG_TRACE("handlePong: from " << it->second->getRemoteUri());
89 }
90}
91
92void
Wentao Shang53df1632014-04-21 12:01:32 -070093WebSocketChannel::handleMessage(websocketpp::connection_hdl hdl,
94 websocket::Server::message_ptr msg)
95{
96 ChannelFaceMap::iterator it = m_channelFaces.find(hdl);
97 if (it != m_channelFaces.end())
98 {
99 it->second->handleReceive(msg->get_payload());
100 }
101}
102
103void
104WebSocketChannel::handleOpen(websocketpp::connection_hdl hdl)
105{
106 std::string remote;
107 try
108 {
109 remote = "wsclient://" + m_server.get_con_from_hdl(hdl)->get_remote_endpoint();
110 }
Wentao Shang98733142014-09-17 12:13:57 -0700111 catch (websocketpp::lib::error_code&)
Wentao Shang53df1632014-04-21 12:01:32 -0700112 {
113 NFD_LOG_DEBUG("handleOpen: cannot get remote uri");
114 websocketpp::lib::error_code ecode;
115 m_server.close(hdl, websocketpp::close::status::normal, "closed by channel", ecode);
116 }
Davide Pesaventoab1e8f22014-10-21 22:45:33 +0200117 shared_ptr<WebSocketFace> face = ndn::make_shared<WebSocketFace>(FaceUri(remote), this->getUri(),
118 hdl, ref(m_server));
Wentao Shang53df1632014-04-21 12:01:32 -0700119 m_onFaceCreatedCallback(face);
120 m_channelFaces[hdl] = face;
Wentao Shang98733142014-09-17 12:13:57 -0700121
122 // Schedule PING message
Junxiao Shi1e46be32015-01-08 20:18:05 -0700123 scheduler::EventId pingEvent = scheduler::schedule(m_pingInterval,
124 bind(&WebSocketChannel::sendPing, this, hdl));
Wentao Shang98733142014-09-17 12:13:57 -0700125 face->setPingEventId(pingEvent);
126}
127
128void
129WebSocketChannel::sendPing(websocketpp::connection_hdl hdl)
130{
131 ChannelFaceMap::iterator it = m_channelFaces.find(hdl);
132 if (it != m_channelFaces.end())
133 {
134 try
135 {
136 m_server.ping(hdl, "NFD-WebSocket");
137 }
138 catch (websocketpp::lib::error_code&)
139 {
140 it->second->close();
141 NFD_LOG_DEBUG("sendPing: failed to ping " << it->second->getRemoteUri());
142 m_channelFaces.erase(it);
143 }
144
145 NFD_LOG_TRACE("sendPing: to " << it->second->getRemoteUri());
146
147 // Schedule next PING message
Junxiao Shi1e46be32015-01-08 20:18:05 -0700148 scheduler::EventId pingEvent = scheduler::schedule(m_pingInterval,
149 bind(&WebSocketChannel::sendPing, this, hdl));
Wentao Shang98733142014-09-17 12:13:57 -0700150 it->second->setPingEventId(pingEvent);
151 }
Wentao Shang53df1632014-04-21 12:01:32 -0700152}
153
154void
155WebSocketChannel::handleClose(websocketpp::connection_hdl hdl)
156{
157 ChannelFaceMap::iterator it = m_channelFaces.find(hdl);
158 if (it != m_channelFaces.end())
159 {
Alexander Afanasyevc87bbf82014-06-05 08:10:56 +0300160 it->second->close();
Wentao Shang98733142014-09-17 12:13:57 -0700161 NFD_LOG_DEBUG("handleClose: remove " << it->second->getRemoteUri());
Wentao Shang53df1632014-04-21 12:01:32 -0700162 m_channelFaces.erase(it);
163 }
164}
165
166
167void
168WebSocketChannel::listen(const FaceCreatedCallback& onFaceCreated)
169{
170 if (m_isListening)
171 {
172 throw Error("Listen already called on this channel");
173 }
174 m_isListening = true;
175
176 m_onFaceCreatedCallback = onFaceCreated;
177
178 try
179 {
180 m_server.listen(m_localEndpoint);
181 }
182 catch (websocketpp::lib::error_code ec)
183 {
184 throw Error("Failed to listen on local endpoint");
185 }
186
187 m_server.start_accept();
188}
189
190size_t
191WebSocketChannel::size() const
192{
193 return m_channelFaces.size();
194}
195
196} // namespace nfd