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 | |
| 137 | BMCWEB_LOG_DEBUG << __FUNCTION__ << "(): " << host << ":" << port; |
| 138 | |
| 139 | req.version(static_cast<int>(11)); // HTTP 1.1 |
| 140 | req.target(uri); |
| 141 | req.method(boost::beast::http::verb::post); |
| 142 | |
AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 143 | req.set(boost::beast::http::field::host, host); |
| 144 | req.keep_alive(true); |
| 145 | |
| 146 | req.body() = data; |
| 147 | req.prepare_payload(); |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 148 | |
| 149 | // Set a timeout on the operation |
| 150 | conn.expires_after(std::chrono::seconds(30)); |
| 151 | |
| 152 | // Send the HTTP request to the remote host |
| 153 | boost::beast::http::async_write( |
| 154 | conn, req, |
AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 155 | [self(shared_from_this())](const boost::beast::error_code& ec, |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 156 | const std::size_t& bytesTransferred) { |
| 157 | if (ec) |
| 158 | { |
| 159 | BMCWEB_LOG_ERROR << "sendMessage() failed: " |
| 160 | << ec.message(); |
AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 161 | self->state = ConnState::sendFailed; |
Sunitha Harish | 6eaa1d2 | 2021-02-19 13:38:31 +0530 | [diff] [blame] | 162 | self->handleConnState(); |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 163 | return; |
| 164 | } |
| 165 | BMCWEB_LOG_DEBUG << "sendMessage() bytes transferred: " |
| 166 | << bytesTransferred; |
| 167 | boost::ignore_unused(bytesTransferred); |
| 168 | |
AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 169 | self->recvMessage(); |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 170 | }); |
| 171 | } |
| 172 | |
| 173 | void recvMessage() |
| 174 | { |
Sunitha Harish | 6eaa1d2 | 2021-02-19 13:38:31 +0530 | [diff] [blame] | 175 | state = ConnState::recvInProgress; |
| 176 | |
| 177 | parser.emplace(std::piecewise_construct, std::make_tuple()); |
| 178 | parser->body_limit(httpReadBodyLimit); |
| 179 | |
| 180 | // Check only for the response header |
| 181 | parser->skip(true); |
| 182 | |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 183 | // Receive the HTTP response |
| 184 | boost::beast::http::async_read( |
Sunitha Harish | 6eaa1d2 | 2021-02-19 13:38:31 +0530 | [diff] [blame] | 185 | conn, buffer, *parser, |
AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 186 | [self(shared_from_this())](const boost::beast::error_code& ec, |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 187 | const std::size_t& bytesTransferred) { |
| 188 | if (ec) |
| 189 | { |
| 190 | BMCWEB_LOG_ERROR << "recvMessage() failed: " |
| 191 | << ec.message(); |
AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 192 | self->state = ConnState::recvFailed; |
Sunitha Harish | 6eaa1d2 | 2021-02-19 13:38:31 +0530 | [diff] [blame] | 193 | self->handleConnState(); |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 194 | return; |
| 195 | } |
| 196 | BMCWEB_LOG_DEBUG << "recvMessage() bytes transferred: " |
| 197 | << bytesTransferred; |
Sunitha Harish | 6eaa1d2 | 2021-02-19 13:38:31 +0530 | [diff] [blame] | 198 | BMCWEB_LOG_DEBUG << "recvMessage() data: " |
| 199 | << self->parser->get(); |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 200 | |
Sunitha Harish | 6eaa1d2 | 2021-02-19 13:38:31 +0530 | [diff] [blame] | 201 | unsigned int respCode = self->parser->get().result_int(); |
| 202 | BMCWEB_LOG_DEBUG << "recvMessage() Header Response Code: " |
| 203 | << respCode; |
| 204 | |
| 205 | // 2XX response is considered to be successful |
| 206 | if ((respCode < 200) || (respCode >= 300)) |
| 207 | { |
| 208 | // The listener failed to receive the Sent-Event |
| 209 | BMCWEB_LOG_ERROR << "recvMessage() Listener Failed to " |
| 210 | "receive Sent-Event"; |
| 211 | self->state = ConnState::recvFailed; |
| 212 | self->handleConnState(); |
| 213 | return; |
| 214 | } |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 215 | |
AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 216 | // Send is successful, Lets remove data from queue |
| 217 | // check for next request data in queue. |
Sunitha Harish | 7de9f81 | 2021-08-24 02:50:30 -0500 | [diff] [blame] | 218 | if (!self->requestDataQueue.empty()) |
| 219 | { |
| 220 | self->requestDataQueue.pop_front(); |
| 221 | } |
AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 222 | self->state = ConnState::idle; |
Sunitha Harish | 6eaa1d2 | 2021-02-19 13:38:31 +0530 | [diff] [blame] | 223 | |
| 224 | // Keep the connection alive if server supports it |
| 225 | // Else close the connection |
| 226 | BMCWEB_LOG_DEBUG << "recvMessage() keepalive : " |
| 227 | << self->parser->keep_alive(); |
| 228 | if (!self->parser->keep_alive()) |
| 229 | { |
| 230 | // Abort the connection since server is not keep-alive |
| 231 | // enabled |
| 232 | self->state = ConnState::abortConnection; |
| 233 | } |
| 234 | |
| 235 | self->handleConnState(); |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 236 | }); |
| 237 | } |
| 238 | |
| 239 | void doClose() |
| 240 | { |
Sunitha Harish | 6eaa1d2 | 2021-02-19 13:38:31 +0530 | [diff] [blame] | 241 | state = ConnState::closeInProgress; |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 242 | boost::beast::error_code ec; |
| 243 | conn.socket().shutdown(boost::asio::ip::tcp::socket::shutdown_both, ec); |
Sunitha Harish | 6eaa1d2 | 2021-02-19 13:38:31 +0530 | [diff] [blame] | 244 | conn.close(); |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 245 | |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 246 | // not_connected happens sometimes so don't bother reporting it. |
| 247 | if (ec && ec != boost::beast::errc::not_connected) |
| 248 | { |
| 249 | BMCWEB_LOG_ERROR << "shutdown failed: " << ec.message(); |
| 250 | return; |
| 251 | } |
| 252 | BMCWEB_LOG_DEBUG << "Connection closed gracefully"; |
Sunitha Harish | 6eaa1d2 | 2021-02-19 13:38:31 +0530 | [diff] [blame] | 253 | if ((state != ConnState::suspended) && (state != ConnState::terminated)) |
| 254 | { |
| 255 | state = ConnState::closed; |
| 256 | handleConnState(); |
| 257 | } |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 258 | } |
| 259 | |
Sunitha Harish | 6eaa1d2 | 2021-02-19 13:38:31 +0530 | [diff] [blame] | 260 | void waitAndRetry() |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 261 | { |
AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 262 | if (retryCount >= maxRetryAttempts) |
| 263 | { |
Sunitha Harish | 6eaa1d2 | 2021-02-19 13:38:31 +0530 | [diff] [blame] | 264 | BMCWEB_LOG_ERROR << "Maximum number of retries reached."; |
AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 265 | |
| 266 | // Clear queue. |
| 267 | while (!requestDataQueue.empty()) |
| 268 | { |
Sunitha Harish | 7de9f81 | 2021-08-24 02:50:30 -0500 | [diff] [blame] | 269 | requestDataQueue.pop_front(); |
AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 270 | } |
| 271 | |
Sunitha Harish | 6eaa1d2 | 2021-02-19 13:38:31 +0530 | [diff] [blame] | 272 | BMCWEB_LOG_DEBUG << "Retry policy: " << retryPolicyAction; |
Ayushi Smriti | fe44eb0 | 2020-05-15 15:24:45 +0530 | [diff] [blame] | 273 | if (retryPolicyAction == "TerminateAfterRetries") |
| 274 | { |
| 275 | // TODO: delete subscription |
| 276 | state = ConnState::terminated; |
Ayushi Smriti | fe44eb0 | 2020-05-15 15:24:45 +0530 | [diff] [blame] | 277 | } |
Ed Tanous | 3174e4d | 2020-10-07 11:41:22 -0700 | [diff] [blame] | 278 | if (retryPolicyAction == "SuspendRetries") |
Ayushi Smriti | fe44eb0 | 2020-05-15 15:24:45 +0530 | [diff] [blame] | 279 | { |
| 280 | state = ConnState::suspended; |
Ayushi Smriti | fe44eb0 | 2020-05-15 15:24:45 +0530 | [diff] [blame] | 281 | } |
Sunitha Harish | 6eaa1d2 | 2021-02-19 13:38:31 +0530 | [diff] [blame] | 282 | // Reset the retrycount to zero so that client can try connecting |
| 283 | // again if needed |
Ed Tanous | 3174e4d | 2020-10-07 11:41:22 -0700 | [diff] [blame] | 284 | retryCount = 0; |
Sunitha Harish | 6eaa1d2 | 2021-02-19 13:38:31 +0530 | [diff] [blame] | 285 | handleConnState(); |
Ayushi Smriti | fe44eb0 | 2020-05-15 15:24:45 +0530 | [diff] [blame] | 286 | return; |
AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 287 | } |
AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 288 | |
Sunitha Harish | 6eaa1d2 | 2021-02-19 13:38:31 +0530 | [diff] [blame] | 289 | if (runningTimer) |
| 290 | { |
| 291 | BMCWEB_LOG_DEBUG << "Retry timer is already running."; |
| 292 | return; |
| 293 | } |
| 294 | runningTimer = true; |
| 295 | |
| 296 | retryCount++; |
| 297 | |
| 298 | BMCWEB_LOG_DEBUG << "Attempt retry after " << retryIntervalSecs |
| 299 | << " seconds. RetryCount = " << retryCount; |
| 300 | timer.expires_after(std::chrono::seconds(retryIntervalSecs)); |
| 301 | timer.async_wait( |
| 302 | [self = shared_from_this()](const boost::system::error_code ec) { |
| 303 | if (ec == boost::asio::error::operation_aborted) |
| 304 | { |
| 305 | BMCWEB_LOG_DEBUG |
| 306 | << "async_wait failed since the operation is aborted" |
| 307 | << ec.message(); |
| 308 | } |
| 309 | else if (ec) |
| 310 | { |
| 311 | BMCWEB_LOG_ERROR << "async_wait failed: " << ec.message(); |
| 312 | // Ignore the error and continue the retry loop to attempt |
| 313 | // sending the event as per the retry policy |
| 314 | } |
| 315 | self->runningTimer = false; |
| 316 | |
| 317 | // Lets close connection and start from resolve. |
| 318 | self->doClose(); |
| 319 | }); |
Ayushi Smriti | fe44eb0 | 2020-05-15 15:24:45 +0530 | [diff] [blame] | 320 | return; |
| 321 | } |
| 322 | |
Sunitha Harish | 6eaa1d2 | 2021-02-19 13:38:31 +0530 | [diff] [blame] | 323 | void handleConnState() |
Ayushi Smriti | fe44eb0 | 2020-05-15 15:24:45 +0530 | [diff] [blame] | 324 | { |
AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 325 | switch (state) |
| 326 | { |
Sunitha Harish | 29a82b0 | 2021-02-18 15:54:16 +0530 | [diff] [blame] | 327 | case ConnState::resolveInProgress: |
AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 328 | case ConnState::connectInProgress: |
| 329 | case ConnState::sendInProgress: |
Sunitha Harish | 6eaa1d2 | 2021-02-19 13:38:31 +0530 | [diff] [blame] | 330 | case ConnState::recvInProgress: |
| 331 | case ConnState::closeInProgress: |
| 332 | { |
| 333 | BMCWEB_LOG_DEBUG << "Async operation is already in progress"; |
AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 334 | break; |
Sunitha Harish | 6eaa1d2 | 2021-02-19 13:38:31 +0530 | [diff] [blame] | 335 | } |
AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 336 | case ConnState::initialized: |
| 337 | case ConnState::closed: |
Sunitha Harish | 6eaa1d2 | 2021-02-19 13:38:31 +0530 | [diff] [blame] | 338 | { |
| 339 | if (requestDataQueue.empty()) |
| 340 | { |
| 341 | BMCWEB_LOG_DEBUG << "requestDataQueue is empty"; |
| 342 | return; |
| 343 | } |
| 344 | doResolve(); |
| 345 | break; |
| 346 | } |
| 347 | case ConnState::suspended: |
| 348 | case ConnState::terminated: |
| 349 | { |
| 350 | doClose(); |
| 351 | break; |
| 352 | } |
| 353 | case ConnState::resolveFailed: |
AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 354 | case ConnState::connectFailed: |
| 355 | case ConnState::sendFailed: |
AppaRao Puli | 92a74e5 | 2020-06-04 11:12:28 +0530 | [diff] [blame] | 356 | case ConnState::recvFailed: |
Sunitha Harish | 6eaa1d2 | 2021-02-19 13:38:31 +0530 | [diff] [blame] | 357 | case ConnState::retry: |
AppaRao Puli | 92a74e5 | 2020-06-04 11:12:28 +0530 | [diff] [blame] | 358 | { |
Sunitha Harish | 6eaa1d2 | 2021-02-19 13:38:31 +0530 | [diff] [blame] | 359 | // In case of failures during connect and handshake |
| 360 | // the retry policy will be applied |
| 361 | waitAndRetry(); |
AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 362 | break; |
| 363 | } |
| 364 | case ConnState::connected: |
AppaRao Puli | 92a74e5 | 2020-06-04 11:12:28 +0530 | [diff] [blame] | 365 | case ConnState::idle: |
| 366 | { |
Sunitha Harish | 6eaa1d2 | 2021-02-19 13:38:31 +0530 | [diff] [blame] | 367 | // State idle means, previous attempt is successful |
| 368 | // State connected means, client connection is established |
| 369 | // successfully |
| 370 | if (requestDataQueue.empty()) |
| 371 | { |
| 372 | BMCWEB_LOG_DEBUG << "requestDataQueue is empty"; |
| 373 | return; |
| 374 | } |
AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 375 | std::string data = requestDataQueue.front(); |
| 376 | sendMessage(data); |
| 377 | break; |
| 378 | } |
Sunitha Harish | 6eaa1d2 | 2021-02-19 13:38:31 +0530 | [diff] [blame] | 379 | case ConnState::abortConnection: |
| 380 | { |
| 381 | // Server did not want to keep alive the session |
| 382 | doClose(); |
| 383 | break; |
| 384 | } |
| 385 | default: |
| 386 | break; |
AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 387 | } |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | public: |
Ayushi Smriti | fe44eb0 | 2020-05-15 15:24:45 +0530 | [diff] [blame] | 391 | explicit HttpClient(boost::asio::io_context& ioc, const std::string& id, |
| 392 | const std::string& destIP, const std::string& destPort, |
AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 393 | const std::string& destUri) : |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 394 | conn(ioc), |
Ayushi Smriti | fe44eb0 | 2020-05-15 15:24:45 +0530 | [diff] [blame] | 395 | timer(ioc), subId(id), host(destIP), port(destPort), uri(destUri), |
Ed Tanous | f23b729 | 2020-10-15 09:41:17 -0700 | [diff] [blame] | 396 | retryCount(0), maxRetryAttempts(5), retryIntervalSecs(0), |
Ayushi Smriti | fe44eb0 | 2020-05-15 15:24:45 +0530 | [diff] [blame] | 397 | retryPolicyAction("TerminateAfterRetries"), runningTimer(false) |
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 | state = ConnState::initialized; |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 400 | } |
| 401 | |
AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 402 | void sendData(const std::string& data) |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 403 | { |
Sunitha Harish | 6eaa1d2 | 2021-02-19 13:38:31 +0530 | [diff] [blame] | 404 | if ((state == ConnState::suspended) || (state == ConnState::terminated)) |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 405 | { |
AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 406 | return; |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 407 | } |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 408 | |
AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 409 | if (requestDataQueue.size() <= maxRequestQueueSize) |
| 410 | { |
Sunitha Harish | 7de9f81 | 2021-08-24 02:50:30 -0500 | [diff] [blame] | 411 | requestDataQueue.push_back(data); |
Sunitha Harish | 6eaa1d2 | 2021-02-19 13:38:31 +0530 | [diff] [blame] | 412 | handleConnState(); |
AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 413 | } |
| 414 | else |
| 415 | { |
| 416 | BMCWEB_LOG_ERROR << "Request queue is full. So ignoring data."; |
| 417 | } |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 418 | |
AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 419 | return; |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 420 | } |
| 421 | |
Ed Tanous | 601c71a | 2021-09-08 16:40:12 -0700 | [diff] [blame] | 422 | void setHeaders(const boost::beast::http::fields& httpHeaders) |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 423 | { |
Ed Tanous | 601c71a | 2021-09-08 16:40:12 -0700 | [diff] [blame] | 424 | req.base() = boost::beast::http::header<true>(httpHeaders); |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 425 | } |
Ayushi Smriti | fe44eb0 | 2020-05-15 15:24:45 +0530 | [diff] [blame] | 426 | |
| 427 | void setRetryConfig(const uint32_t retryAttempts, |
| 428 | const uint32_t retryTimeoutInterval) |
| 429 | { |
| 430 | maxRetryAttempts = retryAttempts; |
| 431 | retryIntervalSecs = retryTimeoutInterval; |
| 432 | } |
| 433 | |
| 434 | void setRetryPolicy(const std::string& retryPolicy) |
| 435 | { |
| 436 | retryPolicyAction = retryPolicy; |
| 437 | } |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 438 | }; |
| 439 | |
| 440 | } // namespace crow |