blob: d6cf64b91ba2c60d2749cf7588ca38909568ddf7 [file] [log] [blame]
Wentao Shang53df1632014-04-21 12:01:32 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2014 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
10 *
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/>.
24 **/
25
26#include "websocket-channel.hpp"
27#include "core/face-uri.hpp"
28
Wentao Shang98733142014-09-17 12:13:57 -070029#include <boost/date_time/posix_time/posix_time.hpp>
30
Wentao Shang53df1632014-04-21 12:01:32 -070031namespace nfd {
32
33NFD_LOG_INIT("WebSocketChannel");
34
35using namespace boost::asio;
36
37WebSocketChannel::WebSocketChannel(const websocket::Endpoint& localEndpoint)
38 : m_localEndpoint(localEndpoint)
39 , m_isListening(false)
Wentao Shang98733142014-09-17 12:13:57 -070040 , m_pingInterval(10000)
Wentao Shang53df1632014-04-21 12:01:32 -070041{
42 // Setup WebSocket server
43 m_server.clear_access_channels(websocketpp::log::alevel::all);
44 m_server.clear_error_channels(websocketpp::log::alevel::all);
45
46 m_server.set_message_handler(bind(&WebSocketChannel::handleMessage, this, _1, _2));
47 m_server.set_open_handler(bind(&WebSocketChannel::handleOpen, this, _1));
48 m_server.set_close_handler(bind(&WebSocketChannel::handleClose, this, _1));
49 m_server.init_asio(&getGlobalIoService());
Wentao Shang93ef6c92014-06-19 11:59:17 -040050 // Always set SO_REUSEADDR flag
51 m_server.set_reuse_addr(true);
Wentao Shang53df1632014-04-21 12:01:32 -070052
Wentao Shang98733142014-09-17 12:13:57 -070053 // Detect disconnection using PONG message
54 m_server.set_pong_handler(bind(&WebSocketChannel::handlePong, this, _1, _2));
55 m_server.set_pong_timeout_handler(bind(&WebSocketChannel::handlePongTimeout,
56 this, _1, _2));
57
Wentao Shang53df1632014-04-21 12:01:32 -070058 this->setUri(FaceUri(localEndpoint, "ws"));
59}
60
61WebSocketChannel::~WebSocketChannel()
62{
63}
64
65void
Wentao Shang98733142014-09-17 12:13:57 -070066WebSocketChannel::setPongTimeout(time::milliseconds timeout)
67{
68 m_server.set_pong_timeout(static_cast<long>(timeout.count()));
69}
70
71void
72WebSocketChannel::handlePongTimeout(websocketpp::connection_hdl hdl, std::string msg)
73{
74 ChannelFaceMap::iterator it = m_channelFaces.find(hdl);
75 if (it != m_channelFaces.end())
76 {
77 it->second->close();
78 NFD_LOG_DEBUG("handlePongTimeout: remove " << it->second->getRemoteUri());
79 m_channelFaces.erase(it);
80 }
81}
82
83void
84WebSocketChannel::handlePong(websocketpp::connection_hdl hdl, std::string msg)
85{
86 ChannelFaceMap::iterator it = m_channelFaces.find(hdl);
87 if (it != m_channelFaces.end())
88 {
89 NFD_LOG_TRACE("handlePong: from " << it->second->getRemoteUri());
90 }
91}
92
93void
Wentao Shang53df1632014-04-21 12:01:32 -070094WebSocketChannel::handleMessage(websocketpp::connection_hdl hdl,
95 websocket::Server::message_ptr msg)
96{
97 ChannelFaceMap::iterator it = m_channelFaces.find(hdl);
98 if (it != m_channelFaces.end())
99 {
100 it->second->handleReceive(msg->get_payload());
101 }
102}
103
104void
105WebSocketChannel::handleOpen(websocketpp::connection_hdl hdl)
106{
107 std::string remote;
108 try
109 {
110 remote = "wsclient://" + m_server.get_con_from_hdl(hdl)->get_remote_endpoint();
111 }
Wentao Shang98733142014-09-17 12:13:57 -0700112 catch (websocketpp::lib::error_code&)
Wentao Shang53df1632014-04-21 12:01:32 -0700113 {
114 NFD_LOG_DEBUG("handleOpen: cannot get remote uri");
115 websocketpp::lib::error_code ecode;
116 m_server.close(hdl, websocketpp::close::status::normal, "closed by channel", ecode);
117 }
118 shared_ptr<WebSocketFace> face = make_shared<WebSocketFace>(FaceUri(remote), this->getUri(),
Alexander Afanasyevf6980282014-05-13 18:28:40 -0700119 hdl, ref(m_server));
Wentao Shang53df1632014-04-21 12:01:32 -0700120 m_onFaceCreatedCallback(face);
121 m_channelFaces[hdl] = face;
Wentao Shang98733142014-09-17 12:13:57 -0700122
123 // Schedule PING message
124 EventId pingEvent = scheduler::schedule(m_pingInterval,
125 bind(&WebSocketChannel::sendPing, this, hdl));
126 face->setPingEventId(pingEvent);
127}
128
129void
130WebSocketChannel::sendPing(websocketpp::connection_hdl hdl)
131{
132 ChannelFaceMap::iterator it = m_channelFaces.find(hdl);
133 if (it != m_channelFaces.end())
134 {
135 try
136 {
137 m_server.ping(hdl, "NFD-WebSocket");
138 }
139 catch (websocketpp::lib::error_code&)
140 {
141 it->second->close();
142 NFD_LOG_DEBUG("sendPing: failed to ping " << it->second->getRemoteUri());
143 m_channelFaces.erase(it);
144 }
145
146 NFD_LOG_TRACE("sendPing: to " << it->second->getRemoteUri());
147
148 // Schedule next PING message
149 EventId pingEvent = scheduler::schedule(m_pingInterval,
150 bind(&WebSocketChannel::sendPing, this, hdl));
151 it->second->setPingEventId(pingEvent);
152 }
Wentao Shang53df1632014-04-21 12:01:32 -0700153}
154
155void
156WebSocketChannel::handleClose(websocketpp::connection_hdl hdl)
157{
158 ChannelFaceMap::iterator it = m_channelFaces.find(hdl);
159 if (it != m_channelFaces.end())
160 {
Alexander Afanasyevc87bbf82014-06-05 08:10:56 +0300161 it->second->close();
Wentao Shang98733142014-09-17 12:13:57 -0700162 NFD_LOG_DEBUG("handleClose: remove " << it->second->getRemoteUri());
Wentao Shang53df1632014-04-21 12:01:32 -0700163 m_channelFaces.erase(it);
164 }
165}
166
167
168void
169WebSocketChannel::listen(const FaceCreatedCallback& onFaceCreated)
170{
171 if (m_isListening)
172 {
173 throw Error("Listen already called on this channel");
174 }
175 m_isListening = true;
176
177 m_onFaceCreatedCallback = onFaceCreated;
178
179 try
180 {
181 m_server.listen(m_localEndpoint);
182 }
183 catch (websocketpp::lib::error_code ec)
184 {
185 throw Error("Failed to listen on local endpoint");
186 }
187
188 m_server.start_accept();
189}
190
191size_t
192WebSocketChannel::size() const
193{
194 return m_channelFaces.size();
195}
196
197} // namespace nfd