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