AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 1 | /* |
| 2 | // Copyright (c) 2020 Intel Corporation |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | */ |
| 16 | #pragma once |
Sunitha Harish | 29a82b0 | 2021-02-18 15:54:16 +0530 | [diff] [blame] | 17 | #include <boost/asio/ip/address.hpp> |
| 18 | #include <boost/asio/ip/basic_endpoint.hpp> |
Ed Tanous | d43cd0c | 2020-09-30 20:46:53 -0700 | [diff] [blame] | 19 | #include <boost/asio/steady_timer.hpp> |
| 20 | #include <boost/beast/core/flat_buffer.hpp> |
| 21 | #include <boost/beast/core/tcp_stream.hpp> |
| 22 | #include <boost/beast/http/message.hpp> |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 23 | #include <boost/beast/version.hpp> |
Sunitha Harish | 29a82b0 | 2021-02-18 15:54:16 +0530 | [diff] [blame] | 24 | #include <include/async_resolve.hpp> |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 25 | |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 26 | #include <cstdlib> |
| 27 | #include <functional> |
| 28 | #include <iostream> |
| 29 | #include <memory> |
AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 30 | #include <queue> |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 31 | #include <string> |
| 32 | |
| 33 | namespace crow |
| 34 | { |
| 35 | |
AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 36 | static constexpr uint8_t maxRequestQueueSize = 50; |
Sunitha Harish | 7de9f81 | 2021-08-24 02:50:30 -0500 | [diff] [blame] | 37 | static constexpr unsigned int httpReadBodyLimit = 8192; |
AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 38 | |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 39 | enum class ConnState |
| 40 | { |
AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 41 | initialized, |
Sunitha Harish | 29a82b0 | 2021-02-18 15:54:16 +0530 | [diff] [blame] | 42 | resolveInProgress, |
| 43 | resolveFailed, |
AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 44 | connectInProgress, |
| 45 | connectFailed, |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 46 | connected, |
AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 47 | sendInProgress, |
| 48 | sendFailed, |
Sunitha Harish | 6eaa1d2 | 2021-02-19 13:38:31 +0530 | [diff] [blame] | 49 | recvInProgress, |
AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 50 | recvFailed, |
| 51 | idle, |
Sunitha Harish | 6eaa1d2 | 2021-02-19 13:38:31 +0530 | [diff] [blame] | 52 | closeInProgress, |
Ayushi Smriti | fe44eb0 | 2020-05-15 15:24:45 +0530 | [diff] [blame] | 53 | closed, |
Sunitha Harish | 6eaa1d2 | 2021-02-19 13:38:31 +0530 | [diff] [blame] | 54 | suspended, |
| 55 | terminated, |
| 56 | abortConnection, |
| 57 | retry |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 58 | }; |
| 59 | |
| 60 | class HttpClient : public std::enable_shared_from_this<HttpClient> |
| 61 | { |
| 62 | private: |
Sunitha Harish | 29a82b0 | 2021-02-18 15:54:16 +0530 | [diff] [blame] | 63 | crow::async_resolve::Resolver resolver; |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 64 | boost::beast::tcp_stream conn; |
Ayushi Smriti | fe44eb0 | 2020-05-15 15:24:45 +0530 | [diff] [blame] | 65 | boost::asio::steady_timer timer; |
Sunitha Harish | 7de9f81 | 2021-08-24 02:50:30 -0500 | [diff] [blame] | 66 | boost::beast::flat_static_buffer<httpReadBodyLimit> buffer; |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 67 | boost::beast::http::request<boost::beast::http::string_body> req; |
Sunitha Harish | 6eaa1d2 | 2021-02-19 13:38:31 +0530 | [diff] [blame] | 68 | std::optional< |
| 69 | boost::beast::http::response_parser<boost::beast::http::string_body>> |
| 70 | parser; |
Sunitha Harish | 7de9f81 | 2021-08-24 02:50:30 -0500 | [diff] [blame] | 71 | boost::circular_buffer_space_optimized<std::string> requestDataQueue{}; |
Sunitha Harish | 6eaa1d2 | 2021-02-19 13:38:31 +0530 | [diff] [blame] | 72 | |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 73 | ConnState state; |
Ayushi Smriti | fe44eb0 | 2020-05-15 15:24:45 +0530 | [diff] [blame] | 74 | std::string subId; |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 75 | std::string host; |
| 76 | std::string port; |
AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 77 | std::string uri; |
Ayushi Smriti | fe44eb0 | 2020-05-15 15:24:45 +0530 | [diff] [blame] | 78 | uint32_t retryCount; |
| 79 | uint32_t maxRetryAttempts; |
| 80 | uint32_t retryIntervalSecs; |
| 81 | std::string retryPolicyAction; |
| 82 | bool runningTimer; |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 83 | |
Sunitha Harish | 29a82b0 | 2021-02-18 15:54:16 +0530 | [diff] [blame] | 84 | void doResolve() |
| 85 | { |
Sunitha Harish | 29a82b0 | 2021-02-18 15:54:16 +0530 | [diff] [blame] | 86 | state = ConnState::resolveInProgress; |
Sunitha Harish | 29a82b0 | 2021-02-18 15:54:16 +0530 | [diff] [blame] | 87 | BMCWEB_LOG_DEBUG << "Trying to resolve: " << host << ":" << port; |
| 88 | |
| 89 | auto respHandler = |
| 90 | [self(shared_from_this())]( |
| 91 | const boost::beast::error_code ec, |
| 92 | const std::vector<boost::asio::ip::tcp::endpoint>& |
| 93 | endpointList) { |
Sunitha Harish | 6eaa1d2 | 2021-02-19 13:38:31 +0530 | [diff] [blame] | 94 | if (ec || (endpointList.size() == 0)) |
Sunitha Harish | 29a82b0 | 2021-02-18 15:54:16 +0530 | [diff] [blame] | 95 | { |
| 96 | BMCWEB_LOG_ERROR << "Resolve failed: " << ec.message(); |
| 97 | self->state = ConnState::resolveFailed; |
Sunitha Harish | 6eaa1d2 | 2021-02-19 13:38:31 +0530 | [diff] [blame] | 98 | self->handleConnState(); |
Sunitha Harish | 29a82b0 | 2021-02-18 15:54:16 +0530 | [diff] [blame] | 99 | return; |
| 100 | } |
| 101 | BMCWEB_LOG_DEBUG << "Resolved"; |
| 102 | self->doConnect(endpointList); |
| 103 | }; |
| 104 | resolver.asyncResolve(host, port, std::move(respHandler)); |
| 105 | } |
| 106 | |
| 107 | void doConnect( |
| 108 | const std::vector<boost::asio::ip::tcp::endpoint>& endpointList) |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 109 | { |
AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 110 | state = ConnState::connectInProgress; |
| 111 | |
| 112 | BMCWEB_LOG_DEBUG << "Trying to connect to: " << host << ":" << port; |
Sunitha Harish | 29a82b0 | 2021-02-18 15:54:16 +0530 | [diff] [blame] | 113 | |
AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 114 | conn.expires_after(std::chrono::seconds(30)); |
Sunitha Harish | 29a82b0 | 2021-02-18 15:54:16 +0530 | [diff] [blame] | 115 | conn.async_connect( |
| 116 | endpointList, [self(shared_from_this())]( |
| 117 | const boost::beast::error_code ec, |
| 118 | const boost::asio::ip::tcp::endpoint& endpoint) { |
| 119 | if (ec) |
| 120 | { |
| 121 | BMCWEB_LOG_ERROR << "Connect " << endpoint |
| 122 | << " failed: " << ec.message(); |
| 123 | self->state = ConnState::connectFailed; |
Sunitha Harish | 6eaa1d2 | 2021-02-19 13:38:31 +0530 | [diff] [blame] | 124 | self->handleConnState(); |
Sunitha Harish | 29a82b0 | 2021-02-18 15:54:16 +0530 | [diff] [blame] | 125 | return; |
| 126 | } |
Sunitha Harish | 29a82b0 | 2021-02-18 15:54:16 +0530 | [diff] [blame] | 127 | BMCWEB_LOG_DEBUG << "Connected to: " << endpoint; |
Sunitha Harish | 6eaa1d2 | 2021-02-19 13:38:31 +0530 | [diff] [blame] | 128 | self->state = ConnState::connected; |
| 129 | self->handleConnState(); |
Sunitha Harish | 29a82b0 | 2021-02-18 15:54:16 +0530 | [diff] [blame] | 130 | }); |
AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | void sendMessage(const std::string& data) |
| 134 | { |
AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 135 | state = ConnState::sendInProgress; |
| 136 | |
AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 137 | req.body() = data; |
| 138 | req.prepare_payload(); |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 139 | |
| 140 | // Set a timeout on the operation |
| 141 | conn.expires_after(std::chrono::seconds(30)); |
| 142 | |
| 143 | // Send the HTTP request to the remote host |
| 144 | boost::beast::http::async_write( |
| 145 | conn, req, |
AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 146 | [self(shared_from_this())](const boost::beast::error_code& ec, |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 147 | const std::size_t& bytesTransferred) { |
| 148 | if (ec) |
| 149 | { |
| 150 | BMCWEB_LOG_ERROR << "sendMessage() failed: " |
| 151 | << ec.message(); |
AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 152 | self->state = ConnState::sendFailed; |
Sunitha Harish | 6eaa1d2 | 2021-02-19 13:38:31 +0530 | [diff] [blame] | 153 | self->handleConnState(); |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 154 | return; |
| 155 | } |
| 156 | BMCWEB_LOG_DEBUG << "sendMessage() bytes transferred: " |
| 157 | << bytesTransferred; |
| 158 | boost::ignore_unused(bytesTransferred); |
| 159 | |
AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 160 | self->recvMessage(); |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 161 | }); |
| 162 | } |
| 163 | |
| 164 | void recvMessage() |
| 165 | { |
Sunitha Harish | 6eaa1d2 | 2021-02-19 13:38:31 +0530 | [diff] [blame] | 166 | state = ConnState::recvInProgress; |
| 167 | |
| 168 | parser.emplace(std::piecewise_construct, std::make_tuple()); |
| 169 | parser->body_limit(httpReadBodyLimit); |
| 170 | |
| 171 | // Check only for the response header |
| 172 | parser->skip(true); |
| 173 | |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 174 | // Receive the HTTP response |
| 175 | boost::beast::http::async_read( |
Sunitha Harish | 6eaa1d2 | 2021-02-19 13:38:31 +0530 | [diff] [blame] | 176 | conn, buffer, *parser, |
AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 177 | [self(shared_from_this())](const boost::beast::error_code& ec, |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 178 | const std::size_t& bytesTransferred) { |
| 179 | if (ec) |
| 180 | { |
| 181 | BMCWEB_LOG_ERROR << "recvMessage() failed: " |
| 182 | << ec.message(); |
AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 183 | self->state = ConnState::recvFailed; |
Sunitha Harish | 6eaa1d2 | 2021-02-19 13:38:31 +0530 | [diff] [blame] | 184 | self->handleConnState(); |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 185 | return; |
| 186 | } |
| 187 | BMCWEB_LOG_DEBUG << "recvMessage() bytes transferred: " |
| 188 | << bytesTransferred; |
Sunitha Harish | 6eaa1d2 | 2021-02-19 13:38:31 +0530 | [diff] [blame] | 189 | BMCWEB_LOG_DEBUG << "recvMessage() data: " |
| 190 | << self->parser->get(); |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 191 | |
Sunitha Harish | 6eaa1d2 | 2021-02-19 13:38:31 +0530 | [diff] [blame] | 192 | unsigned int respCode = self->parser->get().result_int(); |
| 193 | BMCWEB_LOG_DEBUG << "recvMessage() Header Response Code: " |
| 194 | << respCode; |
| 195 | |
| 196 | // 2XX response is considered to be successful |
| 197 | if ((respCode < 200) || (respCode >= 300)) |
| 198 | { |
| 199 | // The listener failed to receive the Sent-Event |
| 200 | BMCWEB_LOG_ERROR << "recvMessage() Listener Failed to " |
| 201 | "receive Sent-Event"; |
| 202 | self->state = ConnState::recvFailed; |
| 203 | self->handleConnState(); |
| 204 | return; |
| 205 | } |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 206 | |
AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 207 | // Send is successful, Lets remove data from queue |
| 208 | // check for next request data in queue. |
Sunitha Harish | 7de9f81 | 2021-08-24 02:50:30 -0500 | [diff] [blame] | 209 | if (!self->requestDataQueue.empty()) |
| 210 | { |
| 211 | self->requestDataQueue.pop_front(); |
| 212 | } |
AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 213 | self->state = ConnState::idle; |
Sunitha Harish | 6eaa1d2 | 2021-02-19 13:38:31 +0530 | [diff] [blame] | 214 | |
| 215 | // Keep the connection alive if server supports it |
| 216 | // Else close the connection |
| 217 | BMCWEB_LOG_DEBUG << "recvMessage() keepalive : " |
| 218 | << self->parser->keep_alive(); |
| 219 | if (!self->parser->keep_alive()) |
| 220 | { |
| 221 | // Abort the connection since server is not keep-alive |
| 222 | // enabled |
| 223 | self->state = ConnState::abortConnection; |
| 224 | } |
| 225 | |
| 226 | self->handleConnState(); |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 227 | }); |
| 228 | } |
| 229 | |
| 230 | void doClose() |
| 231 | { |
Sunitha Harish | 6eaa1d2 | 2021-02-19 13:38:31 +0530 | [diff] [blame] | 232 | state = ConnState::closeInProgress; |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 233 | boost::beast::error_code ec; |
| 234 | conn.socket().shutdown(boost::asio::ip::tcp::socket::shutdown_both, ec); |
Sunitha Harish | 6eaa1d2 | 2021-02-19 13:38:31 +0530 | [diff] [blame] | 235 | conn.close(); |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 236 | |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 237 | // not_connected happens sometimes so don't bother reporting it. |
| 238 | if (ec && ec != boost::beast::errc::not_connected) |
| 239 | { |
| 240 | BMCWEB_LOG_ERROR << "shutdown failed: " << ec.message(); |
| 241 | return; |
| 242 | } |
| 243 | BMCWEB_LOG_DEBUG << "Connection closed gracefully"; |
Sunitha Harish | 6eaa1d2 | 2021-02-19 13:38:31 +0530 | [diff] [blame] | 244 | if ((state != ConnState::suspended) && (state != ConnState::terminated)) |
| 245 | { |
| 246 | state = ConnState::closed; |
| 247 | handleConnState(); |
| 248 | } |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 249 | } |
| 250 | |
Sunitha Harish | 6eaa1d2 | 2021-02-19 13:38:31 +0530 | [diff] [blame] | 251 | void waitAndRetry() |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 252 | { |
AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 253 | if (retryCount >= maxRetryAttempts) |
| 254 | { |
Sunitha Harish | 6eaa1d2 | 2021-02-19 13:38:31 +0530 | [diff] [blame] | 255 | BMCWEB_LOG_ERROR << "Maximum number of retries reached."; |
AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 256 | |
| 257 | // Clear queue. |
| 258 | while (!requestDataQueue.empty()) |
| 259 | { |
Sunitha Harish | 7de9f81 | 2021-08-24 02:50:30 -0500 | [diff] [blame] | 260 | requestDataQueue.pop_front(); |
AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 261 | } |
| 262 | |
Sunitha Harish | 6eaa1d2 | 2021-02-19 13:38:31 +0530 | [diff] [blame] | 263 | BMCWEB_LOG_DEBUG << "Retry policy: " << retryPolicyAction; |
Ayushi Smriti | fe44eb0 | 2020-05-15 15:24:45 +0530 | [diff] [blame] | 264 | if (retryPolicyAction == "TerminateAfterRetries") |
| 265 | { |
| 266 | // TODO: delete subscription |
| 267 | state = ConnState::terminated; |
Ayushi Smriti | fe44eb0 | 2020-05-15 15:24:45 +0530 | [diff] [blame] | 268 | } |
Ed Tanous | 3174e4d | 2020-10-07 11:41:22 -0700 | [diff] [blame] | 269 | if (retryPolicyAction == "SuspendRetries") |
Ayushi Smriti | fe44eb0 | 2020-05-15 15:24:45 +0530 | [diff] [blame] | 270 | { |
| 271 | state = ConnState::suspended; |
Ayushi Smriti | fe44eb0 | 2020-05-15 15:24:45 +0530 | [diff] [blame] | 272 | } |
Sunitha Harish | 6eaa1d2 | 2021-02-19 13:38:31 +0530 | [diff] [blame] | 273 | // Reset the retrycount to zero so that client can try connecting |
| 274 | // again if needed |
Ed Tanous | 3174e4d | 2020-10-07 11:41:22 -0700 | [diff] [blame] | 275 | retryCount = 0; |
Sunitha Harish | 6eaa1d2 | 2021-02-19 13:38:31 +0530 | [diff] [blame] | 276 | handleConnState(); |
Ayushi Smriti | fe44eb0 | 2020-05-15 15:24:45 +0530 | [diff] [blame] | 277 | return; |
AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 278 | } |
AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 279 | |
Sunitha Harish | 6eaa1d2 | 2021-02-19 13:38:31 +0530 | [diff] [blame] | 280 | if (runningTimer) |
| 281 | { |
| 282 | BMCWEB_LOG_DEBUG << "Retry timer is already running."; |
| 283 | return; |
| 284 | } |
| 285 | runningTimer = true; |
| 286 | |
| 287 | retryCount++; |
| 288 | |
| 289 | BMCWEB_LOG_DEBUG << "Attempt retry after " << retryIntervalSecs |
| 290 | << " seconds. RetryCount = " << retryCount; |
| 291 | timer.expires_after(std::chrono::seconds(retryIntervalSecs)); |
| 292 | timer.async_wait( |
| 293 | [self = shared_from_this()](const boost::system::error_code ec) { |
| 294 | if (ec == boost::asio::error::operation_aborted) |
| 295 | { |
| 296 | BMCWEB_LOG_DEBUG |
| 297 | << "async_wait failed since the operation is aborted" |
| 298 | << ec.message(); |
| 299 | } |
| 300 | else if (ec) |
| 301 | { |
| 302 | BMCWEB_LOG_ERROR << "async_wait failed: " << ec.message(); |
| 303 | // Ignore the error and continue the retry loop to attempt |
| 304 | // sending the event as per the retry policy |
| 305 | } |
| 306 | self->runningTimer = false; |
| 307 | |
| 308 | // Lets close connection and start from resolve. |
| 309 | self->doClose(); |
| 310 | }); |
Ayushi Smriti | fe44eb0 | 2020-05-15 15:24:45 +0530 | [diff] [blame] | 311 | return; |
| 312 | } |
| 313 | |
Sunitha Harish | 6eaa1d2 | 2021-02-19 13:38:31 +0530 | [diff] [blame] | 314 | void handleConnState() |
Ayushi Smriti | fe44eb0 | 2020-05-15 15:24:45 +0530 | [diff] [blame] | 315 | { |
AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 316 | switch (state) |
| 317 | { |
Sunitha Harish | 29a82b0 | 2021-02-18 15:54:16 +0530 | [diff] [blame] | 318 | case ConnState::resolveInProgress: |
AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 319 | case ConnState::connectInProgress: |
| 320 | case ConnState::sendInProgress: |
Sunitha Harish | 6eaa1d2 | 2021-02-19 13:38:31 +0530 | [diff] [blame] | 321 | case ConnState::recvInProgress: |
| 322 | case ConnState::closeInProgress: |
| 323 | { |
| 324 | BMCWEB_LOG_DEBUG << "Async operation is already in progress"; |
AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 325 | break; |
Sunitha Harish | 6eaa1d2 | 2021-02-19 13:38:31 +0530 | [diff] [blame] | 326 | } |
AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 327 | case ConnState::initialized: |
| 328 | case ConnState::closed: |
Sunitha Harish | 6eaa1d2 | 2021-02-19 13:38:31 +0530 | [diff] [blame] | 329 | { |
| 330 | if (requestDataQueue.empty()) |
| 331 | { |
| 332 | BMCWEB_LOG_DEBUG << "requestDataQueue is empty"; |
| 333 | return; |
| 334 | } |
| 335 | doResolve(); |
| 336 | break; |
| 337 | } |
| 338 | case ConnState::suspended: |
| 339 | case ConnState::terminated: |
| 340 | { |
| 341 | doClose(); |
| 342 | break; |
| 343 | } |
| 344 | case ConnState::resolveFailed: |
AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 345 | case ConnState::connectFailed: |
| 346 | case ConnState::sendFailed: |
AppaRao Puli | 92a74e5 | 2020-06-04 11:12:28 +0530 | [diff] [blame] | 347 | case ConnState::recvFailed: |
Sunitha Harish | 6eaa1d2 | 2021-02-19 13:38:31 +0530 | [diff] [blame] | 348 | case ConnState::retry: |
AppaRao Puli | 92a74e5 | 2020-06-04 11:12:28 +0530 | [diff] [blame] | 349 | { |
Sunitha Harish | 6eaa1d2 | 2021-02-19 13:38:31 +0530 | [diff] [blame] | 350 | // In case of failures during connect and handshake |
| 351 | // the retry policy will be applied |
| 352 | waitAndRetry(); |
AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 353 | break; |
| 354 | } |
| 355 | case ConnState::connected: |
AppaRao Puli | 92a74e5 | 2020-06-04 11:12:28 +0530 | [diff] [blame] | 356 | case ConnState::idle: |
| 357 | { |
Sunitha Harish | 6eaa1d2 | 2021-02-19 13:38:31 +0530 | [diff] [blame] | 358 | // State idle means, previous attempt is successful |
| 359 | // State connected means, client connection is established |
| 360 | // successfully |
| 361 | if (requestDataQueue.empty()) |
| 362 | { |
| 363 | BMCWEB_LOG_DEBUG << "requestDataQueue is empty"; |
| 364 | return; |
| 365 | } |
AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 366 | std::string data = requestDataQueue.front(); |
| 367 | sendMessage(data); |
| 368 | break; |
| 369 | } |
Sunitha Harish | 6eaa1d2 | 2021-02-19 13:38:31 +0530 | [diff] [blame] | 370 | case ConnState::abortConnection: |
| 371 | { |
| 372 | // Server did not want to keep alive the session |
| 373 | doClose(); |
| 374 | break; |
| 375 | } |
| 376 | default: |
| 377 | break; |
AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 378 | } |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 379 | } |
| 380 | |
| 381 | public: |
Ayushi Smriti | fe44eb0 | 2020-05-15 15:24:45 +0530 | [diff] [blame] | 382 | explicit HttpClient(boost::asio::io_context& ioc, const std::string& id, |
| 383 | const std::string& destIP, const std::string& destPort, |
Ed Tanous | 4da0445 | 2021-09-08 19:57:44 -0700 | [diff] [blame^] | 384 | const std::string& destUri, |
| 385 | const boost::beast::http::fields& httpHeader) : |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 386 | conn(ioc), |
Ed Tanous | 4da0445 | 2021-09-08 19:57:44 -0700 | [diff] [blame^] | 387 | timer(ioc), |
| 388 | req(boost::beast::http::verb::post, destUri, 11, "", httpHeader), |
| 389 | state(ConnState::initialized), subId(id), host(destIP), port(destPort), |
| 390 | uri(destUri), retryCount(0), maxRetryAttempts(5), retryIntervalSecs(0), |
Ayushi Smriti | fe44eb0 | 2020-05-15 15:24:45 +0530 | [diff] [blame] | 391 | retryPolicyAction("TerminateAfterRetries"), runningTimer(false) |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 392 | { |
Ed Tanous | 4da0445 | 2021-09-08 19:57:44 -0700 | [diff] [blame^] | 393 | req.set(boost::beast::http::field::host, host); |
| 394 | req.keep_alive(true); |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 395 | } |
| 396 | |
AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 397 | void sendData(const std::string& data) |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 398 | { |
Sunitha Harish | 6eaa1d2 | 2021-02-19 13:38:31 +0530 | [diff] [blame] | 399 | if ((state == ConnState::suspended) || (state == ConnState::terminated)) |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 400 | { |
AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 401 | return; |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 402 | } |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 403 | |
AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 404 | if (requestDataQueue.size() <= maxRequestQueueSize) |
| 405 | { |
Sunitha Harish | 7de9f81 | 2021-08-24 02:50:30 -0500 | [diff] [blame] | 406 | requestDataQueue.push_back(data); |
Sunitha Harish | 6eaa1d2 | 2021-02-19 13:38:31 +0530 | [diff] [blame] | 407 | handleConnState(); |
AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 408 | } |
| 409 | else |
| 410 | { |
| 411 | BMCWEB_LOG_ERROR << "Request queue is full. So ignoring data."; |
| 412 | } |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 413 | |
AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 414 | return; |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 415 | } |
| 416 | |
Ayushi Smriti | fe44eb0 | 2020-05-15 15:24:45 +0530 | [diff] [blame] | 417 | void setRetryConfig(const uint32_t retryAttempts, |
| 418 | const uint32_t retryTimeoutInterval) |
| 419 | { |
| 420 | maxRetryAttempts = retryAttempts; |
| 421 | retryIntervalSecs = retryTimeoutInterval; |
| 422 | } |
| 423 | |
| 424 | void setRetryPolicy(const std::string& retryPolicy) |
| 425 | { |
| 426 | retryPolicyAction = retryPolicy; |
| 427 | } |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 428 | }; |
| 429 | |
| 430 | } // namespace crow |