| 554 | } |
| 555 | |
| 556 | bool NetworkService::downloadResource(std::string& mainData, std::string& auxiliaryData, std::string& statistics, std::string const& simId) |
| 557 | { |
| 558 | try { |
| 559 | if (auto cachedEntry = _downloadCache.find(simId)) { |
| 560 | log(Priority::Important, "network: get resource with id=" + simId + " from download cache"); |
| 561 | mainData = cachedEntry->content; |
| 562 | auxiliaryData = cachedEntry->auxiliaryData; |
| 563 | statistics = cachedEntry->statistics; |
| 564 | incDownloadCounter(simId); |
| 565 | return true; |
| 566 | } else { |
| 567 | log(Priority::Important, "network: download resource with id=" + simId); |
| 568 | |
| 569 | httplib::SSLClient client(_serverAddress); |
| 570 | configureClient(client); |
| 571 | |
| 572 | httplib::Params params; |
| 573 | params.emplace("id", simId); |
| 574 | { |
| 575 | |
| 576 | for (int chunkIndex = 0; chunkIndex < 6; ++chunkIndex) { |
| 577 | auto paramsClone = params; |
| 578 | paramsClone.emplace("chunkIndex", std::to_string(chunkIndex)); |
| 579 | auto result = executeRequest([&] { return client.Get("/alien-server/downloadcontent.php", paramsClone, {}); }); |
| 580 | if (result->body.empty()) { |
| 581 | break; |
| 582 | } |
| 583 | mainData.append(result->body); |
| 584 | } |
| 585 | } |
| 586 | { |
| 587 | auto result = executeRequest([&] { return client.Get("/alien-server/downloadsettings.php", params, {}); }); |
| 588 | auxiliaryData = result->body; |
| 589 | } |
| 590 | { |
| 591 | auto result = executeRequest([&] { return client.Get("/alien-server/downloadstatistics.php", params, {}); }); |
| 592 | statistics = result->body; |
| 593 | } |
| 594 | _downloadCache.insertOrAssign(simId, ResourceData{mainData, auxiliaryData, statistics}); |
| 595 | return true; |
| 596 | } |
| 597 | } catch (...) { |
| 598 | logNetworkError(); |
| 599 | return false; |
| 600 | } |
| 601 | } |
| 602 | |
| 603 | void NetworkService::incDownloadCounter(std::string const& simId) |
| 604 | { |
no test coverage detected