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