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