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; |
| 37 | |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 38 | enum class ConnState |
| 39 | { |
AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 40 | initialized, |
Sunitha Harish | 29a82b0 | 2021-02-18 15:54:16 +0530 | [diff] [blame] | 41 | resolveInProgress, |
| 42 | resolveFailed, |
AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 43 | connectInProgress, |
| 44 | connectFailed, |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 45 | connected, |
AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 46 | sendInProgress, |
| 47 | sendFailed, |
| 48 | recvFailed, |
| 49 | idle, |
| 50 | suspended, |
Ayushi Smriti | fe44eb0 | 2020-05-15 15:24:45 +0530 | [diff] [blame] | 51 | closed, |
| 52 | terminated |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 53 | }; |
| 54 | |
| 55 | class HttpClient : public std::enable_shared_from_this<HttpClient> |
| 56 | { |
| 57 | private: |
Sunitha Harish | 29a82b0 | 2021-02-18 15:54:16 +0530 | [diff] [blame] | 58 | crow::async_resolve::Resolver resolver; |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 59 | boost::beast::tcp_stream conn; |
Ayushi Smriti | fe44eb0 | 2020-05-15 15:24:45 +0530 | [diff] [blame] | 60 | boost::asio::steady_timer timer; |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 61 | boost::beast::flat_buffer buffer; |
| 62 | boost::beast::http::request<boost::beast::http::string_body> req; |
| 63 | boost::beast::http::response<boost::beast::http::string_body> res; |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 64 | std::vector<std::pair<std::string, std::string>> headers; |
AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 65 | std::queue<std::string> requestDataQueue; |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 66 | ConnState state; |
Ayushi Smriti | fe44eb0 | 2020-05-15 15:24:45 +0530 | [diff] [blame] | 67 | std::string subId; |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 68 | std::string host; |
| 69 | std::string port; |
AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 70 | std::string uri; |
Ayushi Smriti | fe44eb0 | 2020-05-15 15:24:45 +0530 | [diff] [blame] | 71 | uint32_t retryCount; |
| 72 | uint32_t maxRetryAttempts; |
| 73 | uint32_t retryIntervalSecs; |
| 74 | std::string retryPolicyAction; |
| 75 | bool runningTimer; |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 76 | |
Sunitha Harish | 29a82b0 | 2021-02-18 15:54:16 +0530 | [diff] [blame] | 77 | void doResolve() |
| 78 | { |
| 79 | if (state == ConnState::resolveInProgress) |
| 80 | { |
| 81 | return; |
| 82 | } |
| 83 | state = ConnState::resolveInProgress; |
| 84 | |
| 85 | BMCWEB_LOG_DEBUG << "Trying to resolve: " << host << ":" << port; |
| 86 | |
| 87 | auto respHandler = |
| 88 | [self(shared_from_this())]( |
| 89 | const boost::beast::error_code ec, |
| 90 | const std::vector<boost::asio::ip::tcp::endpoint>& |
| 91 | endpointList) { |
| 92 | if (ec) |
| 93 | { |
| 94 | BMCWEB_LOG_ERROR << "Resolve failed: " << ec.message(); |
| 95 | self->state = ConnState::resolveFailed; |
| 96 | self->checkQueue(); |
| 97 | return; |
| 98 | } |
| 99 | BMCWEB_LOG_DEBUG << "Resolved"; |
| 100 | self->doConnect(endpointList); |
| 101 | }; |
| 102 | resolver.asyncResolve(host, port, std::move(respHandler)); |
| 103 | } |
| 104 | |
| 105 | void doConnect( |
| 106 | const std::vector<boost::asio::ip::tcp::endpoint>& endpointList) |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 107 | { |
AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 108 | if (state == ConnState::connectInProgress) |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 109 | { |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 110 | return; |
| 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 | { |
| 123 | BMCWEB_LOG_ERROR << "Connect " << endpoint |
| 124 | << " failed: " << ec.message(); |
| 125 | self->state = ConnState::connectFailed; |
| 126 | self->checkQueue(); |
| 127 | return; |
| 128 | } |
| 129 | self->state = ConnState::connected; |
| 130 | BMCWEB_LOG_DEBUG << "Connected to: " << endpoint; |
Ed Tanous | b00dcc2 | 2021-02-23 12:52:50 -0800 | [diff] [blame] | 131 | |
AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 132 | self->checkQueue(); |
Sunitha Harish | 29a82b0 | 2021-02-18 15:54:16 +0530 | [diff] [blame] | 133 | }); |
AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | void sendMessage(const std::string& data) |
| 137 | { |
| 138 | if (state == ConnState::sendInProgress) |
| 139 | { |
| 140 | return; |
| 141 | } |
| 142 | state = ConnState::sendInProgress; |
| 143 | |
| 144 | BMCWEB_LOG_DEBUG << __FUNCTION__ << "(): " << host << ":" << port; |
| 145 | |
| 146 | req.version(static_cast<int>(11)); // HTTP 1.1 |
| 147 | req.target(uri); |
| 148 | req.method(boost::beast::http::verb::post); |
| 149 | |
| 150 | // Set headers |
| 151 | for (const auto& [key, value] : headers) |
| 152 | { |
| 153 | req.set(key, value); |
| 154 | } |
| 155 | req.set(boost::beast::http::field::host, host); |
| 156 | req.keep_alive(true); |
| 157 | |
| 158 | req.body() = data; |
| 159 | req.prepare_payload(); |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 160 | |
| 161 | // Set a timeout on the operation |
| 162 | conn.expires_after(std::chrono::seconds(30)); |
| 163 | |
| 164 | // Send the HTTP request to the remote host |
| 165 | boost::beast::http::async_write( |
| 166 | conn, req, |
AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 167 | [self(shared_from_this())](const boost::beast::error_code& ec, |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 168 | const std::size_t& bytesTransferred) { |
| 169 | if (ec) |
| 170 | { |
| 171 | BMCWEB_LOG_ERROR << "sendMessage() failed: " |
| 172 | << ec.message(); |
AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 173 | self->state = ConnState::sendFailed; |
| 174 | self->checkQueue(); |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 175 | return; |
| 176 | } |
| 177 | BMCWEB_LOG_DEBUG << "sendMessage() bytes transferred: " |
| 178 | << bytesTransferred; |
| 179 | boost::ignore_unused(bytesTransferred); |
| 180 | |
AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 181 | self->recvMessage(); |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 182 | }); |
| 183 | } |
| 184 | |
| 185 | void recvMessage() |
| 186 | { |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 187 | // Receive the HTTP response |
| 188 | boost::beast::http::async_read( |
| 189 | conn, buffer, res, |
AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 190 | [self(shared_from_this())](const boost::beast::error_code& ec, |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 191 | const std::size_t& bytesTransferred) { |
| 192 | if (ec) |
| 193 | { |
| 194 | BMCWEB_LOG_ERROR << "recvMessage() failed: " |
| 195 | << ec.message(); |
AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 196 | self->state = ConnState::recvFailed; |
| 197 | self->checkQueue(); |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 198 | return; |
| 199 | } |
| 200 | BMCWEB_LOG_DEBUG << "recvMessage() bytes transferred: " |
| 201 | << bytesTransferred; |
| 202 | boost::ignore_unused(bytesTransferred); |
| 203 | |
| 204 | // Discard received data. We are not interested. |
AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 205 | BMCWEB_LOG_DEBUG << "recvMessage() data: " << self->res; |
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. |
| 209 | self->requestDataQueue.pop(); |
| 210 | self->state = ConnState::idle; |
| 211 | self->checkQueue(); |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 212 | }); |
| 213 | } |
| 214 | |
| 215 | void doClose() |
| 216 | { |
| 217 | boost::beast::error_code ec; |
| 218 | conn.socket().shutdown(boost::asio::ip::tcp::socket::shutdown_both, ec); |
| 219 | |
| 220 | state = ConnState::closed; |
| 221 | // not_connected happens sometimes so don't bother reporting it. |
| 222 | if (ec && ec != boost::beast::errc::not_connected) |
| 223 | { |
| 224 | BMCWEB_LOG_ERROR << "shutdown failed: " << ec.message(); |
| 225 | return; |
| 226 | } |
| 227 | BMCWEB_LOG_DEBUG << "Connection closed gracefully"; |
| 228 | } |
| 229 | |
AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 230 | void checkQueue(const bool newRecord = false) |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 231 | { |
AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 232 | if (requestDataQueue.empty()) |
| 233 | { |
| 234 | // TODO: Having issue in keeping connection alive. So lets close if |
Gunnar Mills | caa3ce3 | 2020-07-08 14:46:53 -0500 | [diff] [blame] | 235 | // nothing to be transferred. |
AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 236 | doClose(); |
| 237 | |
| 238 | BMCWEB_LOG_DEBUG << "requestDataQueue is empty\n"; |
| 239 | return; |
| 240 | } |
| 241 | |
| 242 | if (retryCount >= maxRetryAttempts) |
| 243 | { |
| 244 | BMCWEB_LOG_ERROR << "Maximum number of retries is reached."; |
| 245 | |
| 246 | // Clear queue. |
| 247 | while (!requestDataQueue.empty()) |
| 248 | { |
| 249 | requestDataQueue.pop(); |
| 250 | } |
| 251 | |
Ayushi Smriti | fe44eb0 | 2020-05-15 15:24:45 +0530 | [diff] [blame] | 252 | BMCWEB_LOG_DEBUG << "Retry policy is set to " << retryPolicyAction; |
| 253 | if (retryPolicyAction == "TerminateAfterRetries") |
| 254 | { |
| 255 | // TODO: delete subscription |
| 256 | state = ConnState::terminated; |
| 257 | return; |
| 258 | } |
Ed Tanous | 3174e4d | 2020-10-07 11:41:22 -0700 | [diff] [blame] | 259 | if (retryPolicyAction == "SuspendRetries") |
Ayushi Smriti | fe44eb0 | 2020-05-15 15:24:45 +0530 | [diff] [blame] | 260 | { |
| 261 | state = ConnState::suspended; |
| 262 | return; |
| 263 | } |
Ed Tanous | 3174e4d | 2020-10-07 11:41:22 -0700 | [diff] [blame] | 264 | // keep retrying, reset count and continue. |
| 265 | retryCount = 0; |
AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | if ((state == ConnState::connectFailed) || |
| 269 | (state == ConnState::sendFailed) || |
| 270 | (state == ConnState::recvFailed)) |
| 271 | { |
| 272 | if (newRecord) |
| 273 | { |
| 274 | // We are already running async wait and retry. |
| 275 | // Since record is added to queue, it gets the |
| 276 | // turn in FIFO. |
| 277 | return; |
| 278 | } |
| 279 | |
Ayushi Smriti | fe44eb0 | 2020-05-15 15:24:45 +0530 | [diff] [blame] | 280 | if (runningTimer) |
| 281 | { |
| 282 | BMCWEB_LOG_DEBUG << "Retry timer is already running."; |
| 283 | return; |
| 284 | } |
| 285 | runningTimer = true; |
| 286 | |
AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 287 | retryCount++; |
Ayushi Smriti | fe44eb0 | 2020-05-15 15:24:45 +0530 | [diff] [blame] | 288 | |
| 289 | BMCWEB_LOG_DEBUG << "Attempt retry after " << retryIntervalSecs |
| 290 | << " seconds. RetryCount = " << retryCount; |
| 291 | timer.expires_after(std::chrono::seconds(retryIntervalSecs)); |
Ed Tanous | cb13a39 | 2020-07-25 19:02:03 +0000 | [diff] [blame] | 292 | timer.async_wait( |
| 293 | [self = shared_from_this()](const boost::system::error_code&) { |
| 294 | self->runningTimer = false; |
| 295 | self->connStateCheck(); |
| 296 | }); |
Ayushi Smriti | fe44eb0 | 2020-05-15 15:24:45 +0530 | [diff] [blame] | 297 | return; |
AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 298 | } |
Ed Tanous | 3174e4d | 2020-10-07 11:41:22 -0700 | [diff] [blame] | 299 | // reset retry count. |
| 300 | retryCount = 0; |
Ayushi Smriti | fe44eb0 | 2020-05-15 15:24:45 +0530 | [diff] [blame] | 301 | connStateCheck(); |
AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 302 | |
Ayushi Smriti | fe44eb0 | 2020-05-15 15:24:45 +0530 | [diff] [blame] | 303 | return; |
| 304 | } |
| 305 | |
| 306 | void connStateCheck() |
| 307 | { |
AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 308 | switch (state) |
| 309 | { |
Sunitha Harish | 29a82b0 | 2021-02-18 15:54:16 +0530 | [diff] [blame] | 310 | case ConnState::resolveInProgress: |
AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 311 | case ConnState::connectInProgress: |
| 312 | case ConnState::sendInProgress: |
| 313 | case ConnState::suspended: |
Ayushi Smriti | fe44eb0 | 2020-05-15 15:24:45 +0530 | [diff] [blame] | 314 | case ConnState::terminated: |
AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 315 | // do nothing |
| 316 | break; |
| 317 | case ConnState::initialized: |
| 318 | case ConnState::closed: |
| 319 | case ConnState::connectFailed: |
| 320 | case ConnState::sendFailed: |
AppaRao Puli | 92a74e5 | 2020-06-04 11:12:28 +0530 | [diff] [blame] | 321 | case ConnState::recvFailed: |
Sunitha Harish | 29a82b0 | 2021-02-18 15:54:16 +0530 | [diff] [blame] | 322 | case ConnState::resolveFailed: |
AppaRao Puli | 92a74e5 | 2020-06-04 11:12:28 +0530 | [diff] [blame] | 323 | { |
Sunitha Harish | 29a82b0 | 2021-02-18 15:54:16 +0530 | [diff] [blame] | 324 | doResolve(); |
AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 325 | break; |
| 326 | } |
| 327 | case ConnState::connected: |
AppaRao Puli | 92a74e5 | 2020-06-04 11:12:28 +0530 | [diff] [blame] | 328 | case ConnState::idle: |
| 329 | { |
AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 330 | std::string data = requestDataQueue.front(); |
| 331 | sendMessage(data); |
| 332 | break; |
| 333 | } |
AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 334 | } |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 335 | } |
| 336 | |
| 337 | public: |
Ayushi Smriti | fe44eb0 | 2020-05-15 15:24:45 +0530 | [diff] [blame] | 338 | explicit HttpClient(boost::asio::io_context& ioc, const std::string& id, |
| 339 | const std::string& destIP, const std::string& destPort, |
AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 340 | const std::string& destUri) : |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 341 | conn(ioc), |
Ayushi Smriti | fe44eb0 | 2020-05-15 15:24:45 +0530 | [diff] [blame] | 342 | timer(ioc), subId(id), host(destIP), port(destPort), uri(destUri), |
Ed Tanous | f23b729 | 2020-10-15 09:41:17 -0700 | [diff] [blame] | 343 | retryCount(0), maxRetryAttempts(5), retryIntervalSecs(0), |
Ayushi Smriti | fe44eb0 | 2020-05-15 15:24:45 +0530 | [diff] [blame] | 344 | retryPolicyAction("TerminateAfterRetries"), runningTimer(false) |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 345 | { |
AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 346 | state = ConnState::initialized; |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 347 | } |
| 348 | |
AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 349 | void sendData(const std::string& data) |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 350 | { |
AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 351 | if (state == ConnState::suspended) |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 352 | { |
AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 353 | return; |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 354 | } |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 355 | |
AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 356 | if (requestDataQueue.size() <= maxRequestQueueSize) |
| 357 | { |
| 358 | requestDataQueue.push(data); |
| 359 | checkQueue(true); |
| 360 | } |
| 361 | else |
| 362 | { |
| 363 | BMCWEB_LOG_ERROR << "Request queue is full. So ignoring data."; |
| 364 | } |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 365 | |
AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 366 | return; |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 367 | } |
| 368 | |
| 369 | void setHeaders( |
| 370 | const std::vector<std::pair<std::string, std::string>>& httpHeaders) |
| 371 | { |
| 372 | headers = httpHeaders; |
| 373 | } |
Ayushi Smriti | fe44eb0 | 2020-05-15 15:24:45 +0530 | [diff] [blame] | 374 | |
| 375 | void setRetryConfig(const uint32_t retryAttempts, |
| 376 | const uint32_t retryTimeoutInterval) |
| 377 | { |
| 378 | maxRetryAttempts = retryAttempts; |
| 379 | retryIntervalSecs = retryTimeoutInterval; |
| 380 | } |
| 381 | |
| 382 | void setRetryPolicy(const std::string& retryPolicy) |
| 383 | { |
| 384 | retryPolicyAction = retryPolicy; |
| 385 | } |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 386 | }; |
| 387 | |
| 388 | } // namespace crow |