blob: 234faff482dbab656bd687b2a4a94314e1c8fcc6 [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
29namespace nfd {
30
31NFD_LOG_INIT("WebSocketChannel");
32
33using namespace boost::asio;
34
35WebSocketChannel::WebSocketChannel(const websocket::Endpoint& localEndpoint)
36 : m_localEndpoint(localEndpoint)
37 , m_isListening(false)
38{
39 // Setup WebSocket server
40 m_server.clear_access_channels(websocketpp::log::alevel::all);
41 m_server.clear_error_channels(websocketpp::log::alevel::all);
42
43 m_server.set_message_handler(bind(&WebSocketChannel::handleMessage, this, _1, _2));
44 m_server.set_open_handler(bind(&WebSocketChannel::handleOpen, this, _1));
45 m_server.set_close_handler(bind(&WebSocketChannel::handleClose, this, _1));
46 m_server.init_asio(&getGlobalIoService());
Wentao Shang93ef6c92014-06-19 11:59:17 -040047 // Always set SO_REUSEADDR flag
48 m_server.set_reuse_addr(true);
Wentao Shang53df1632014-04-21 12:01:32 -070049
50 this->setUri(FaceUri(localEndpoint, "ws"));
51}
52
53WebSocketChannel::~WebSocketChannel()
54{
55}
56
57void
58WebSocketChannel::handleMessage(websocketpp::connection_hdl hdl,
59 websocket::Server::message_ptr msg)
60{
61 ChannelFaceMap::iterator it = m_channelFaces.find(hdl);
62 if (it != m_channelFaces.end())
63 {
64 it->second->handleReceive(msg->get_payload());
65 }
66}
67
68void
69WebSocketChannel::handleOpen(websocketpp::connection_hdl hdl)
70{
71 std::string remote;
72 try
73 {
74 remote = "wsclient://" + m_server.get_con_from_hdl(hdl)->get_remote_endpoint();
75 }
76 catch (websocketpp::lib::error_code ec)
77 {
78 NFD_LOG_DEBUG("handleOpen: cannot get remote uri");
79 websocketpp::lib::error_code ecode;
80 m_server.close(hdl, websocketpp::close::status::normal, "closed by channel", ecode);
81 }
82 shared_ptr<WebSocketFace> face = make_shared<WebSocketFace>(FaceUri(remote), this->getUri(),
Alexander Afanasyevf6980282014-05-13 18:28:40 -070083 hdl, ref(m_server));
Wentao Shang53df1632014-04-21 12:01:32 -070084 m_onFaceCreatedCallback(face);
85 m_channelFaces[hdl] = face;
86}
87
88void
89WebSocketChannel::handleClose(websocketpp::connection_hdl hdl)
90{
91 ChannelFaceMap::iterator it = m_channelFaces.find(hdl);
92 if (it != m_channelFaces.end())
93 {
Alexander Afanasyevc87bbf82014-06-05 08:10:56 +030094 it->second->close();
Wentao Shang53df1632014-04-21 12:01:32 -070095 NFD_LOG_DEBUG("handleClose: remove client");
96 m_channelFaces.erase(it);
97 }
98}
99
100
101void
102WebSocketChannel::listen(const FaceCreatedCallback& onFaceCreated)
103{
104 if (m_isListening)
105 {
106 throw Error("Listen already called on this channel");
107 }
108 m_isListening = true;
109
110 m_onFaceCreatedCallback = onFaceCreated;
111
112 try
113 {
114 m_server.listen(m_localEndpoint);
115 }
116 catch (websocketpp::lib::error_code ec)
117 {
118 throw Error("Failed to listen on local endpoint");
119 }
120
121 m_server.start_accept();
122}
123
124size_t
125WebSocketChannel::size() const
126{
127 return m_channelFaces.size();
128}
129
130} // namespace nfd