blob: e6e1e9f08eebb797bf49ee2710f5c9f2893dd58c [file] [log] [blame]
Davide Pesavento096f86a2018-11-10 19:51:45 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
Junxiao Shia6286a92021-02-23 06:43:52 -07003 * Copyright (c) 2014-2021, Regents of the University of California,
Davide Pesavento096f86a2018-11-10 19:51:45 -05004 * 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:
Alexander Afanasyev3a2339a2020-05-27 23:05:06 -040040 shared_ptr<WebSocketChannel>
Davide Pesavento412c9822021-07-02 00:21:05 -040041 makeChannel(const boost::asio::ip::address& addr, uint16_t port = 0,
42 optional<size_t> mtu = nullopt) final
Davide Pesavento096f86a2018-11-10 19:51:45 -050043 {
44 if (port == 0)
45 port = getNextPort();
46
Alexander Afanasyev3a2339a2020-05-27 23:05:06 -040047 return std::make_shared<WebSocketChannel>(websocket::Endpoint(addr, port));
Davide Pesavento096f86a2018-11-10 19:51:45 -050048 }
49
50 void
51 listen(const boost::asio::ip::address& addr,
52 const time::milliseconds& pingInterval = 10_s,
53 const time::milliseconds& pongTimeout = 1_s)
54 {
55 listenerEp = websocket::Endpoint(addr, 20030);
56 listenerChannel = makeChannel(addr, 20030);
57 listenerChannel->setPingInterval(pingInterval);
58 listenerChannel->setPongTimeout(pongTimeout);
Davide Pesavento412c9822021-07-02 00:21:05 -040059 listenerChannel->listen(std::bind(&WebSocketChannelFixture::listenerOnFaceCreated, this, _1));
Davide Pesavento096f86a2018-11-10 19:51:45 -050060 }
61
62 void
63 clientConnect(websocket::Client& client)
64 {
65 client.clear_access_channels(websocketpp::log::alevel::all);
66 client.clear_error_channels(websocketpp::log::elevel::all);
67
68 client.init_asio(&g_io);
Davide Pesavento412c9822021-07-02 00:21:05 -040069 client.set_open_handler(std::bind(&WebSocketChannelFixture::clientHandleOpen, this, _1));
70 client.set_message_handler(std::bind(&WebSocketChannelFixture::clientHandleMessage, this, _1, _2));
71 client.set_ping_handler(std::bind(&WebSocketChannelFixture::clientHandlePing, this, _1, _2));
Davide Pesavento096f86a2018-11-10 19:51:45 -050072
73 websocketpp::lib::error_code ec;
74 auto con = client.get_connection(FaceUri(listenerEp, "ws").toString(), ec);
75 BOOST_REQUIRE_EQUAL(ec, websocketpp::lib::error_code());
76
77 client.connect(con);
78 }
79
80 void
81 initialize(const boost::asio::ip::address& addr,
82 const time::milliseconds& pingInterval = 10_s,
83 const time::milliseconds& pongTimeout = 1_s)
84 {
85 listen(addr, pingInterval, pongTimeout);
86 clientConnect(client);
87 BOOST_REQUIRE_EQUAL(limitedIo.run(2, // listenerOnFaceCreated, clientHandleOpen
88 1_s), LimitedIo::EXCEED_OPS);
89 BOOST_REQUIRE_EQUAL(listenerChannel->size(), 1);
90 }
91
92 void
93 clientSendInterest(const Interest& interest)
94 {
95 const Block& payload = interest.wireEncode();
96 client.send(clientHandle, payload.wire(), payload.size(), websocketpp::frame::opcode::binary);
97 }
98
99private:
100 void
101 listenerOnFaceCreated(const shared_ptr<Face>& newFace)
102 {
103 BOOST_REQUIRE(newFace != nullptr);
Davide Pesavento412c9822021-07-02 00:21:05 -0400104 newFace->afterReceiveInterest.connect([this] (const auto& interest, const auto&) {
105 faceReceivedInterests.push_back(interest);
106 limitedIo.afterOp();
107 });
Davide Pesavento096f86a2018-11-10 19:51:45 -0500108 connectFaceClosedSignal(*newFace, [this] { limitedIo.afterOp(); });
109 listenerFaces.push_back(newFace);
110 limitedIo.afterOp();
111 }
112
113 void
Davide Pesavento096f86a2018-11-10 19:51:45 -0500114 clientHandleOpen(websocketpp::connection_hdl hdl)
115 {
116 clientHandle = hdl;
117 limitedIo.afterOp();
118 }
119
120 void
121 clientHandleMessage(websocketpp::connection_hdl, websocket::Client::message_ptr msg)
122 {
123 clientReceivedMessages.push_back(msg->get_payload());
124 limitedIo.afterOp();
125 }
126
127 bool
128 clientHandlePing(websocketpp::connection_hdl, std::string)
129 {
130 auto now = time::steady_clock::now();
Davide Pesavento412c9822021-07-02 00:21:05 -0400131 if (m_prevPingRecvTime != time::steady_clock::time_point()) {
132 measuredPingIntervals.push_back(now - m_prevPingRecvTime);
Davide Pesavento096f86a2018-11-10 19:51:45 -0500133 }
134 m_prevPingRecvTime = now;
135
136 limitedIo.afterOp();
137 return clientShouldPong;
138 }
139
140protected:
141 std::vector<Interest> faceReceivedInterests;
142
143 websocket::Client client;
144 websocketpp::connection_hdl clientHandle;
145 std::vector<std::string> clientReceivedMessages;
146
Davide Pesavento412c9822021-07-02 00:21:05 -0400147 std::vector<time::nanoseconds> measuredPingIntervals;
Davide Pesavento096f86a2018-11-10 19:51:45 -0500148 // set clientShouldPong to false to disable the pong response,
149 // which will eventually cause a timeout in listenerChannel
150 bool clientShouldPong = true;
151
152private:
Davide Pesavento412c9822021-07-02 00:21:05 -0400153 time::steady_clock::time_point m_prevPingRecvTime;
Davide Pesavento096f86a2018-11-10 19:51:45 -0500154};
155
156} // namespace tests
157} // namespace face
158} // namespace nfd
159
160#endif // NFD_TESTS_DAEMON_FACE_WEBSOCKET_CHANNEL_FIXTURE_HPP