Davide Pesavento | 096f86a | 2018-11-10 19:51:45 -0500 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /* |
Junxiao Shi | a6286a9 | 2021-02-23 06:43:52 -0700 | [diff] [blame] | 3 | * Copyright (c) 2014-2021, Regents of the University of California, |
Davide Pesavento | 096f86a | 2018-11-10 19:51:45 -0500 | [diff] [blame] | 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 | |
| 33 | namespace nfd { |
| 34 | namespace face { |
| 35 | namespace tests { |
| 36 | |
| 37 | class WebSocketChannelFixture : public ChannelFixture<WebSocketChannel, websocket::Endpoint> |
| 38 | { |
| 39 | protected: |
Alexander Afanasyev | 3a2339a | 2020-05-27 23:05:06 -0400 | [diff] [blame] | 40 | shared_ptr<WebSocketChannel> |
Davide Pesavento | 412c982 | 2021-07-02 00:21:05 -0400 | [diff] [blame] | 41 | makeChannel(const boost::asio::ip::address& addr, uint16_t port = 0, |
| 42 | optional<size_t> mtu = nullopt) final |
Davide Pesavento | 096f86a | 2018-11-10 19:51:45 -0500 | [diff] [blame] | 43 | { |
| 44 | if (port == 0) |
| 45 | port = getNextPort(); |
| 46 | |
Alexander Afanasyev | 3a2339a | 2020-05-27 23:05:06 -0400 | [diff] [blame] | 47 | return std::make_shared<WebSocketChannel>(websocket::Endpoint(addr, port)); |
Davide Pesavento | 096f86a | 2018-11-10 19:51:45 -0500 | [diff] [blame] | 48 | } |
| 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 Pesavento | 412c982 | 2021-07-02 00:21:05 -0400 | [diff] [blame] | 59 | listenerChannel->listen(std::bind(&WebSocketChannelFixture::listenerOnFaceCreated, this, _1)); |
Davide Pesavento | 096f86a | 2018-11-10 19:51:45 -0500 | [diff] [blame] | 60 | } |
| 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 Pesavento | 412c982 | 2021-07-02 00:21:05 -0400 | [diff] [blame] | 69 | 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 Pesavento | 096f86a | 2018-11-10 19:51:45 -0500 | [diff] [blame] | 72 | |
| 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 | |
| 99 | private: |
| 100 | void |
| 101 | listenerOnFaceCreated(const shared_ptr<Face>& newFace) |
| 102 | { |
| 103 | BOOST_REQUIRE(newFace != nullptr); |
Davide Pesavento | 412c982 | 2021-07-02 00:21:05 -0400 | [diff] [blame] | 104 | newFace->afterReceiveInterest.connect([this] (const auto& interest, const auto&) { |
| 105 | faceReceivedInterests.push_back(interest); |
| 106 | limitedIo.afterOp(); |
| 107 | }); |
Davide Pesavento | 096f86a | 2018-11-10 19:51:45 -0500 | [diff] [blame] | 108 | connectFaceClosedSignal(*newFace, [this] { limitedIo.afterOp(); }); |
| 109 | listenerFaces.push_back(newFace); |
| 110 | limitedIo.afterOp(); |
| 111 | } |
| 112 | |
| 113 | void |
Davide Pesavento | 096f86a | 2018-11-10 19:51:45 -0500 | [diff] [blame] | 114 | 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 Pesavento | 412c982 | 2021-07-02 00:21:05 -0400 | [diff] [blame] | 131 | if (m_prevPingRecvTime != time::steady_clock::time_point()) { |
| 132 | measuredPingIntervals.push_back(now - m_prevPingRecvTime); |
Davide Pesavento | 096f86a | 2018-11-10 19:51:45 -0500 | [diff] [blame] | 133 | } |
| 134 | m_prevPingRecvTime = now; |
| 135 | |
| 136 | limitedIo.afterOp(); |
| 137 | return clientShouldPong; |
| 138 | } |
| 139 | |
| 140 | protected: |
| 141 | std::vector<Interest> faceReceivedInterests; |
| 142 | |
| 143 | websocket::Client client; |
| 144 | websocketpp::connection_hdl clientHandle; |
| 145 | std::vector<std::string> clientReceivedMessages; |
| 146 | |
Davide Pesavento | 412c982 | 2021-07-02 00:21:05 -0400 | [diff] [blame] | 147 | std::vector<time::nanoseconds> measuredPingIntervals; |
Davide Pesavento | 096f86a | 2018-11-10 19:51:45 -0500 | [diff] [blame] | 148 | // set clientShouldPong to false to disable the pong response, |
| 149 | // which will eventually cause a timeout in listenerChannel |
| 150 | bool clientShouldPong = true; |
| 151 | |
| 152 | private: |
Davide Pesavento | 412c982 | 2021-07-02 00:21:05 -0400 | [diff] [blame] | 153 | time::steady_clock::time_point m_prevPingRecvTime; |
Davide Pesavento | 096f86a | 2018-11-10 19:51:45 -0500 | [diff] [blame] | 154 | }; |
| 155 | |
| 156 | } // namespace tests |
| 157 | } // namespace face |
| 158 | } // namespace nfd |
| 159 | |
| 160 | #endif // NFD_TESTS_DAEMON_FACE_WEBSOCKET_CHANNEL_FIXTURE_HPP |