blob: 5500281c97766eca2349d57c06d7e02f290951bf [file] [log] [blame]
Davide Pesavento096f86a2018-11-10 19:51:45 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
3 * Copyright (c) 2014-2018, 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#ifndef NFD_TESTS_DAEMON_FACE_WEBSOCKET_CHANNEL_FIXTURE_HPP
27#define NFD_TESTS_DAEMON_FACE_WEBSOCKET_CHANNEL_FIXTURE_HPP
28
29#include "face/websocket-channel.hpp"
30
31#include "channel-fixture.hpp"
32
33namespace nfd {
34namespace face {
35namespace tests {
36
37class WebSocketChannelFixture : public ChannelFixture<WebSocketChannel, websocket::Endpoint>
38{
39protected:
40 unique_ptr<WebSocketChannel>
41 makeChannel(const boost::asio::ip::address& addr, uint16_t port = 0) final
42 {
43 if (port == 0)
44 port = getNextPort();
45
46 return make_unique<WebSocketChannel>(websocket::Endpoint(addr, port));
47 }
48
49 void
50 listen(const boost::asio::ip::address& addr,
51 const time::milliseconds& pingInterval = 10_s,
52 const time::milliseconds& pongTimeout = 1_s)
53 {
54 listenerEp = websocket::Endpoint(addr, 20030);
55 listenerChannel = makeChannel(addr, 20030);
56 listenerChannel->setPingInterval(pingInterval);
57 listenerChannel->setPongTimeout(pongTimeout);
58 listenerChannel->listen(bind(&WebSocketChannelFixture::listenerOnFaceCreated, this, _1));
59 }
60
61 void
62 clientConnect(websocket::Client& client)
63 {
64 client.clear_access_channels(websocketpp::log::alevel::all);
65 client.clear_error_channels(websocketpp::log::elevel::all);
66
67 client.init_asio(&g_io);
68 client.set_open_handler(bind(&WebSocketChannelFixture::clientHandleOpen, this, _1));
69 client.set_message_handler(bind(&WebSocketChannelFixture::clientHandleMessage, this, _1, _2));
70 client.set_ping_handler(bind(&WebSocketChannelFixture::clientHandlePing, this, _1, _2));
71
72 websocketpp::lib::error_code ec;
73 auto con = client.get_connection(FaceUri(listenerEp, "ws").toString(), ec);
74 BOOST_REQUIRE_EQUAL(ec, websocketpp::lib::error_code());
75
76 client.connect(con);
77 }
78
79 void
80 initialize(const boost::asio::ip::address& addr,
81 const time::milliseconds& pingInterval = 10_s,
82 const time::milliseconds& pongTimeout = 1_s)
83 {
84 listen(addr, pingInterval, pongTimeout);
85 clientConnect(client);
86 BOOST_REQUIRE_EQUAL(limitedIo.run(2, // listenerOnFaceCreated, clientHandleOpen
87 1_s), LimitedIo::EXCEED_OPS);
88 BOOST_REQUIRE_EQUAL(listenerChannel->size(), 1);
89 }
90
91 void
92 clientSendInterest(const Interest& interest)
93 {
94 const Block& payload = interest.wireEncode();
95 client.send(clientHandle, payload.wire(), payload.size(), websocketpp::frame::opcode::binary);
96 }
97
98private:
99 void
100 listenerOnFaceCreated(const shared_ptr<Face>& newFace)
101 {
102 BOOST_REQUIRE(newFace != nullptr);
103 newFace->afterReceiveInterest.connect(bind(&WebSocketChannelFixture::faceAfterReceiveInterest, this, _1));
104 connectFaceClosedSignal(*newFace, [this] { limitedIo.afterOp(); });
105 listenerFaces.push_back(newFace);
106 limitedIo.afterOp();
107 }
108
109 void
110 faceAfterReceiveInterest(const Interest& interest)
111 {
112 faceReceivedInterests.push_back(interest);
113 limitedIo.afterOp();
114 }
115
116 void
117 clientHandleOpen(websocketpp::connection_hdl hdl)
118 {
119 clientHandle = hdl;
120 limitedIo.afterOp();
121 }
122
123 void
124 clientHandleMessage(websocketpp::connection_hdl, websocket::Client::message_ptr msg)
125 {
126 clientReceivedMessages.push_back(msg->get_payload());
127 limitedIo.afterOp();
128 }
129
130 bool
131 clientHandlePing(websocketpp::connection_hdl, std::string)
132 {
133 auto now = time::steady_clock::now();
134 if (m_prevPingRecvTime != time::steady_clock::TimePoint()) {
135 measuredPingInterval = now - m_prevPingRecvTime;
136 }
137 m_prevPingRecvTime = now;
138
139 limitedIo.afterOp();
140 return clientShouldPong;
141 }
142
143protected:
144 std::vector<Interest> faceReceivedInterests;
145
146 websocket::Client client;
147 websocketpp::connection_hdl clientHandle;
148 std::vector<std::string> clientReceivedMessages;
149
150 time::steady_clock::Duration measuredPingInterval;
151 // set clientShouldPong to false to disable the pong response,
152 // which will eventually cause a timeout in listenerChannel
153 bool clientShouldPong = true;
154
155private:
156 time::steady_clock::TimePoint m_prevPingRecvTime;
157};
158
159} // namespace tests
160} // namespace face
161} // namespace nfd
162
163#endif // NFD_TESTS_DAEMON_FACE_WEBSOCKET_CHANNEL_FIXTURE_HPP