diff --git a/apps/ll-builder/src/main.cpp b/apps/ll-builder/src/main.cpp index 93af5f937..94c7997bf 100644 --- a/apps/ll-builder/src/main.cpp +++ b/apps/ll-builder/src/main.cpp @@ -8,6 +8,7 @@ #include "linglong/builder/config.h" #include "linglong/builder/linglong_builder.h" #include "linglong/package/architecture.h" +#include "linglong/repo/client_factory.h" #include "linglong/repo/config.h" #include "linglong/utils/command/env.h" #include "linglong/utils/configure.h" @@ -400,15 +401,10 @@ int main(int argc, char **argv) qCritical() << repoCfg.error(); return -1; } - - QNetworkAccessManager networkAccessManager; - - linglong::api::client::ClientApi api; - api.setTimeOut(10 * 60 * 1000); - api.setNetworkAccessManager(&networkAccessManager); - api.setNewServerForAllOperations(QString::fromStdString(repoCfg->repos[repoCfg->defaultRepo])); - - linglong::repo::OSTreeRepo repo(QString::fromStdString(builderCfg->repo), *repoCfg, api); + linglong::repo::ClientFactory clientFactory(repoCfg->repos[repoCfg->defaultRepo]); + linglong::repo::OSTreeRepo repo(QString::fromStdString(builderCfg->repo), + *repoCfg, + clientFactory); auto containerBuidler = new linglong::runtime::ContainerBuilder(**ociRuntime); containerBuidler->setParent(QCoreApplication::instance()); diff --git a/apps/ll-cli/src/main.cpp b/apps/ll-cli/src/main.cpp index 74dc11491..e1aa37f98 100644 --- a/apps/ll-cli/src/main.cpp +++ b/apps/ll-cli/src/main.cpp @@ -185,7 +185,6 @@ int main(int argc, char **argv) printer = std::make_unique(); } - linglong::api::client::ClientApi api; auto config = linglong::repo::loadConfig( { LINGLONG_ROOT "/config.yaml", LINGLONG_DATA_DIR "/config.yaml" }); if (!config) { @@ -193,7 +192,8 @@ int main(int argc, char **argv) QCoreApplication::exit(-1); return; } - auto *repo = new linglong::repo::OSTreeRepo(QDir(LINGLONG_ROOT), *config, api); + linglong::repo::ClientFactory clientFactory(config->repos[config->defaultRepo]); + auto *repo = new linglong::repo::OSTreeRepo(QDir(LINGLONG_ROOT), *config, clientFactory); repo->setParent(QCoreApplication::instance()); auto ociRuntimeCLI = qgetenv("LINGLONG_OCI_RUNTIME"); diff --git a/apps/ll-package-manager/src/main.cpp b/apps/ll-package-manager/src/main.cpp index 8b370fd7d..789ed299f 100644 --- a/apps/ll-package-manager/src/main.cpp +++ b/apps/ll-package-manager/src/main.cpp @@ -28,13 +28,8 @@ void withDBusDaemon() return; } - auto api = new linglong::api::client::ClientApi; - api->setTimeOut(5000); - api->setNewServerForAllOperations( - QString::fromStdString(config->repos.at(config->defaultRepo))); - api->setParent(QCoreApplication::instance()); - - auto ostreeRepo = new linglong::repo::OSTreeRepo(QDir(LINGLONG_ROOT), *config, *api); + linglong::repo::ClientFactory clientFactory(config->repos[config->defaultRepo]); + auto ostreeRepo = new linglong::repo::OSTreeRepo(QDir(LINGLONG_ROOT), *config, clientFactory); ostreeRepo->setParent(QCoreApplication::instance()); auto packageManager = @@ -82,12 +77,8 @@ void withoutDBusDaemon() return; } - auto api = new linglong::api::client::ClientApi; - api->setParent(QCoreApplication::instance()); - api->setNewServerForAllOperations( - QString::fromStdString(config->repos.at(config->defaultRepo))); - - auto ostreeRepo = new linglong::repo::OSTreeRepo(QDir(LINGLONG_ROOT), *config, *api); + linglong::repo::ClientFactory clientFactory(config->repos[config->defaultRepo]); + auto ostreeRepo = new linglong::repo::OSTreeRepo(QDir(LINGLONG_ROOT), *config, clientFactory); ostreeRepo->setParent(QCoreApplication::instance()); auto packageManager = diff --git a/libs/linglong/CMakeLists.txt b/libs/linglong/CMakeLists.txt index 53da08e06..9bc475e2b 100644 --- a/libs/linglong/CMakeLists.txt +++ b/libs/linglong/CMakeLists.txt @@ -83,6 +83,8 @@ pfl_add_library( src/linglong/package/version_range.h src/linglong/repo/config.cpp src/linglong/repo/config.h + src/linglong/repo/client_factory.h + src/linglong/repo/client_factory.cpp src/linglong/repo/ostree_repo.cpp src/linglong/repo/ostree_repo.h src/linglong/runtime/container_builder.cpp diff --git a/libs/linglong/src/linglong/repo/client_factory.cpp b/libs/linglong/src/linglong/repo/client_factory.cpp new file mode 100644 index 000000000..e0bf4140d --- /dev/null +++ b/libs/linglong/src/linglong/repo/client_factory.cpp @@ -0,0 +1,35 @@ +/* + * SPDX-FileCopyrightText: 2022 UnionTech Software Technology Co., Ltd. + * + * SPDX-License-Identifier: LGPL-3.0-or-later + */ + +#include "client_factory.h" + +namespace linglong::repo { + +ClientFactory::ClientFactory(const QString &server) + : m_server(server) +{ +} + +ClientFactory::ClientFactory(const std::string &server) + : m_server(QString::fromStdString(server)) +{ +} + +QSharedPointer ClientFactory::createClient() const +{ + auto api = QSharedPointer::create(); + api->setTimeOut(5000); + api->setNewServerForAllOperations(m_server); + api->setParent(QCoreApplication::instance()); + return api; +} + +void ClientFactory::setServer(QString server) +{ + m_server = server; +} + +} // namespace linglong::repo diff --git a/libs/linglong/src/linglong/repo/client_factory.h b/libs/linglong/src/linglong/repo/client_factory.h new file mode 100644 index 000000000..30fd4fd94 --- /dev/null +++ b/libs/linglong/src/linglong/repo/client_factory.h @@ -0,0 +1,32 @@ +/* + * SPDX-FileCopyrightText: 2022 UnionTech Software Technology Co., Ltd. + * + * SPDX-License-Identifier: LGPL-3.0-or-later + */ + +#ifndef LINGLONG_SRC_MODULE_REPO_CLIENT_FACTORY_H_ +#define LINGLONG_SRC_MODULE_REPO_CLIENT_FACTORY_H_ +#include "ClientApi.h" + +#include +#include + +namespace linglong::repo { + +class ClientFactory : public QObject +{ + Q_OBJECT +public: + ClientFactory(const QString &server); + ClientFactory(const std::string &server); + + QSharedPointer createClient() const; + + void setServer(QString server); + +private: + QString m_server; +}; +} // namespace linglong::repo + +#endif // LINGLONG_SRC_MODULE_REPO_CLIENT_FACTORY_H_ diff --git a/libs/linglong/src/linglong/repo/ostree_repo.cpp b/libs/linglong/src/linglong/repo/ostree_repo.cpp index a598202e8..ba1f04725 100644 --- a/libs/linglong/src/linglong/repo/ostree_repo.cpp +++ b/libs/linglong/src/linglong/repo/ostree_repo.cpp @@ -16,9 +16,9 @@ #include "linglong/utils/command/env.h" #include "linglong/utils/error/error.h" #include "linglong/utils/finally/finally.h" +#include "linglong/utils/packageinfo_handler.h" #include "linglong/utils/serialize/json.h" #include "linglong/utils/transaction.h" -#include "linglong/utils/packageinfo_handler.h" #include #include @@ -28,9 +28,7 @@ #include #include -#include #include -#include #include @@ -244,8 +242,6 @@ void progress_changed(OstreeAsyncProgress *progress, gpointer user_data) QString ostreeSpecFromReference(const package::Reference &ref, bool develop = false) noexcept { - - // TODO(wurongjie) 在推送develop版的base和runtime后,应该将devel改为develop if (develop) { return QString("%1/%2/%3/%4/develop") .arg(ref.channel, ref.id, ref.version.toString(), ref.arch.toString()); @@ -638,19 +634,16 @@ utils::error::Result clearReferenceRemote(const package::Fuz ref = *currentRef; } return; - }, - loop.thread() == api.thread() ? Qt::AutoConnection : Qt::BlockingQueuedConnection); + }); - QEventLoop::connect( - &api, - &api::client::ClientApi::fuzzySearchAppSignalEFull, - &loop, - [&](auto, auto error_type, const QString &error_str) { - LINGLONG_TRACE("fuzzySearchAppSignalEFull"); - loop.exit(); - ref = LINGLONG_ERR(error_str, error_type); - }, - loop.thread() == api.thread() ? Qt::AutoConnection : Qt::BlockingQueuedConnection); + QEventLoop::connect(&api, + &api::client::ClientApi::fuzzySearchAppSignalEFull, + &loop, + [&](auto, auto error_type, const QString &error_str) { + LINGLONG_TRACE("fuzzySearchAppSignalEFull"); + loop.exit(); + ref = LINGLONG_ERR(error_str, error_type); + }); api.fuzzySearchApp(req); loop.exec(); @@ -681,9 +674,9 @@ QDir OSTreeRepo::ostreeRepoDir() const noexcept OSTreeRepo::OSTreeRepo(const QDir &path, const api::types::v1::RepoConfig &cfg, - api::client::ClientApi &client) noexcept + ClientFactory &clientFactory) noexcept : cfg(cfg) - , apiClient(client) + , m_clientFactory(clientFactory) { if (!path.exists()) { path.mkpath("."); @@ -774,10 +767,7 @@ utils::error::Result OSTreeRepo::setConfig(const api::types::v1::RepoConfi Q_ASSERT(false); } }); - - this->apiClient.setNewServerForAllOperations( - QString::fromStdString(cfg.repos.at(cfg.defaultRepo))); - + this->m_clientFactory.setServer(QString::fromStdString(cfg.repos.at(cfg.defaultRepo))); this->cfg = cfg; transaction.commit(); @@ -858,33 +848,30 @@ utils::error::Result OSTreeRepo::push(const package::Reference &ref, auth.setUsername(env.value("LINGLONG_USERNAME")); auth.setPassword(env.value("LINGLONG_PASSWORD")); qInfo() << "use username: " << auth.getUsername(); - + auto apiClient = this->m_clientFactory.createClient().data(); + apiClient->setTimeOut(10 * 60 * 1000); QEventLoop loop; - QEventLoop::connect( - &this->apiClient, - &api::client::ClientApi::signInSignal, - &loop, - [&](api::client::SignIn_200_response resp) { - loop.exit(); - if (resp.getCode() != HTTP_OK) { - result = LINGLONG_ERR(resp.getMsg(), resp.getCode()); - return; - } - result = resp.getData().getToken(); - return; - }, - loop.thread() == apiClient.thread() ? Qt::AutoConnection : Qt::BlockingQueuedConnection); - QEventLoop::connect( - &apiClient, - &api::client::ClientApi::signInSignalEFull, - &loop, - [&](auto, auto error_type, const QString &error_str) { - loop.exit(); - result = LINGLONG_ERR(error_str, error_type); - }, - loop.thread() == apiClient.thread() ? Qt::AutoConnection : Qt::BlockingQueuedConnection); - - apiClient.signIn(auth); + QEventLoop::connect(apiClient, + &api::client::ClientApi::signInSignal, + &loop, + [&](api::client::SignIn_200_response resp) { + loop.exit(); + if (resp.getCode() != HTTP_OK) { + result = LINGLONG_ERR(resp.getMsg(), resp.getCode()); + return; + } + result = resp.getData().getToken(); + return; + }); + QEventLoop::connect(apiClient, + &api::client::ClientApi::signInSignalEFull, + &loop, + [&](auto, auto error_type, const QString &error_str) { + loop.exit(); + result = LINGLONG_ERR(error_str, error_type); + }); + + apiClient->signIn(auth); loop.exec(); if (!result) { @@ -905,31 +892,28 @@ utils::error::Result OSTreeRepo::push(const package::Reference &ref, uploadReq.setRef(ostreeSpecFromReference(ref, develop)); uploadReq.setRepoName(QString::fromStdString(this->cfg.defaultRepo)); + auto apiClient = this->m_clientFactory.createClient().data(); QEventLoop loop; - QEventLoop::connect( - &this->apiClient, - &api::client::ClientApi::newUploadTaskIDSignal, - &loop, - [&](const api::client::NewUploadTaskID_200_response &resp) { - loop.exit(); - if (resp.getCode() != HTTP_OK) { - result = LINGLONG_ERR(resp.getMsg(), resp.getCode()); - return; - } - result = resp.getData().getId(); - }, - loop.thread() == apiClient.thread() ? Qt::AutoConnection : Qt::BlockingQueuedConnection); - QEventLoop::connect( - &apiClient, - &api::client::ClientApi::newUploadTaskIDSignalEFull, - &loop, - [&](auto, auto error_type, const QString &error_str) { - loop.exit(); - result = LINGLONG_ERR(error_str, error_type); - }, - loop.thread() == apiClient.thread() ? Qt::AutoConnection : Qt::BlockingQueuedConnection); - - apiClient.newUploadTaskID(*token, uploadReq); + QEventLoop::connect(apiClient, + &api::client::ClientApi::newUploadTaskIDSignal, + &loop, + [&](const api::client::NewUploadTaskID_200_response &resp) { + loop.exit(); + if (resp.getCode() != HTTP_OK) { + result = LINGLONG_ERR(resp.getMsg(), resp.getCode()); + return; + } + result = resp.getData().getId(); + }); + QEventLoop::connect(apiClient, + &api::client::ClientApi::newUploadTaskIDSignalEFull, + &loop, + [&](auto, auto error_type, const QString &error_str) { + loop.exit(); + result = LINGLONG_ERR(error_str, error_type); + }); + + apiClient->newUploadTaskID(*token, uploadReq); loop.exec(); if (!result) { return LINGLONG_ERR(result); @@ -962,33 +946,30 @@ utils::error::Result OSTreeRepo::push(const package::Reference &ref, utils::error::Result result; + auto apiClient = this->m_clientFactory.createClient().data(); QEventLoop loop; - QEventLoop::connect( - &this->apiClient, - &api::client::ClientApi::uploadTaskFileSignal, - &loop, - [&](const api::client::Api_UploadTaskFileResp &resp) { - loop.exit(); - if (resp.getCode() != HTTP_OK) { - result = LINGLONG_ERR(resp.getMsg(), resp.getCode()); - return; - } - }, - loop.thread() == apiClient.thread() ? Qt::AutoConnection : Qt::BlockingQueuedConnection); - QEventLoop::connect( - &this->apiClient, - &api::client::ClientApi::uploadTaskFileSignalEFull, - &loop, - [&](auto, auto error_type, const QString &error_str) { - loop.exit(); - result = LINGLONG_ERR(error_str, error_type); - }, - loop.thread() == apiClient.thread() ? Qt::AutoConnection : Qt::BlockingQueuedConnection); + QEventLoop::connect(apiClient, + &api::client::ClientApi::uploadTaskFileSignal, + &loop, + [&](const api::client::Api_UploadTaskFileResp &resp) { + loop.exit(); + if (resp.getCode() != HTTP_OK) { + result = LINGLONG_ERR(resp.getMsg(), resp.getCode()); + return; + } + }); + QEventLoop::connect(apiClient, + &api::client::ClientApi::uploadTaskFileSignalEFull, + &loop, + [&](auto, auto error_type, const QString &error_str) { + loop.exit(); + result = LINGLONG_ERR(error_str, error_type); + }); api::client::HttpFileElement file; file.setFileName(tarFilePath); file.setRequestFileName(tarFilePath); - apiClient.uploadTaskFile(*token, *taskID, file); + apiClient->uploadTaskFile(*token, *taskID, file); loop.exec(); return result; @@ -1002,45 +983,41 @@ utils::error::Result OSTreeRepo::push(const package::Reference &ref, utils::error::Result isFinished; + auto apiClient = this->m_clientFactory.createClient().data(); while (true) { QEventLoop loop; - QEventLoop::connect( - &this->apiClient, - &api::client::ClientApi::uploadTaskInfoSignal, - &loop, - [&](const api::client::UploadTaskInfo_200_response &resp) { - loop.exit(); - const qint32 HTTP_OK = 200; - if (resp.getCode() != HTTP_OK) { - isFinished = LINGLONG_ERR(resp.getMsg(), resp.getCode()); - return; - } - qDebug() << "pushing" << ref.toString() << (develop ? "develop" : "runtime") - << " status: " << resp.getData().getStatus(); - if (resp.getData().getStatus() == "complete") { - isFinished = true; - return; - } - if (resp.getData().getStatus() == "failed") { - isFinished = LINGLONG_ERR(resp.getData().asJson()); - return; - } - - isFinished = false; - }, - loop.thread() == apiClient.thread() ? Qt::AutoConnection - : Qt::BlockingQueuedConnection); - QEventLoop::connect( - &apiClient, - &api::client::ClientApi::uploadTaskInfoSignalEFull, - &loop, - [&](auto, auto error_type, const QString &error_str) { - loop.exit(); - isFinished = LINGLONG_ERR(error_str, error_type); - }, - loop.thread() == apiClient.thread() ? Qt::AutoConnection - : Qt::BlockingQueuedConnection); - apiClient.uploadTaskInfo(*token, *taskID); + QEventLoop::connect(apiClient, + &api::client::ClientApi::uploadTaskInfoSignal, + &loop, + [&](const api::client::UploadTaskInfo_200_response &resp) { + loop.exit(); + const qint32 HTTP_OK = 200; + if (resp.getCode() != HTTP_OK) { + isFinished = LINGLONG_ERR(resp.getMsg(), resp.getCode()); + return; + } + qDebug() << "pushing" << ref.toString() + << (develop ? "develop" : "runtime") + << " status: " << resp.getData().getStatus(); + if (resp.getData().getStatus() == "complete") { + isFinished = true; + return; + } + if (resp.getData().getStatus() == "failed") { + isFinished = LINGLONG_ERR(resp.getData().asJson()); + return; + } + + isFinished = false; + }); + QEventLoop::connect(apiClient, + &api::client::ClientApi::uploadTaskInfoSignalEFull, + &loop, + [&](auto, auto error_type, const QString &error_str) { + loop.exit(); + isFinished = LINGLONG_ERR(error_str, error_type); + }); + apiClient->uploadTaskInfo(*token, *taskID); loop.exec(); if (!isFinished) { @@ -1173,9 +1150,9 @@ utils::error::Result OSTreeRepo::clearReference( qInfo() << reference.error(); qInfo() << "fallback to Remote"; } - + auto apiClient = this->m_clientFactory.createClient().data(); reference = - clearReferenceRemote(fuzzy, this->apiClient, QString::fromStdString(this->cfg.defaultRepo)); + clearReferenceRemote(fuzzy, *apiClient, QString::fromStdString(this->cfg.defaultRepo)); if (reference) { return reference; } @@ -1250,11 +1227,11 @@ OSTreeRepo::listRemote(const package::FuzzyReference &fuzzyRef) const noexcept utils::error::Result> pkgInfos = std::vector{}; - + auto apiClient = this->m_clientFactory.createClient().data(); QEventLoop loop; const qint32 HTTP_OK = 200; QEventLoop::connect( - &this->apiClient, + apiClient, &api::client::ClientApi::fuzzySearchAppSignal, &loop, [&](const api::client::FuzzySearchApp_200_response &resp) { @@ -1280,22 +1257,17 @@ OSTreeRepo::listRemote(const package::FuzzyReference &fuzzyRef) const noexcept pkgInfos->emplace_back(*std::move(pkgInfo)); } return; - }, - loop.thread() == this->apiClient.thread() ? Qt::AutoConnection - : Qt::BlockingQueuedConnection); + }); - QEventLoop::connect( - &this->apiClient, - &api::client::ClientApi::fuzzySearchAppSignalEFull, - &loop, - [&](auto, auto error_type, const QString &error_str) { - loop.exit(); - pkgInfos = LINGLONG_ERR(error_str, error_type); - }, - loop.thread() == this->apiClient.thread() ? Qt::AutoConnection - : Qt::BlockingQueuedConnection); + QEventLoop::connect(apiClient, + &api::client::ClientApi::fuzzySearchAppSignalEFull, + &loop, + [&](auto, auto error_type, const QString &error_str) { + loop.exit(); + pkgInfos = LINGLONG_ERR(error_str, error_type); + }); - this->apiClient.fuzzySearchApp(req); + apiClient->fuzzySearchApp(req); loop.exec(); if (!pkgInfos) { diff --git a/libs/linglong/src/linglong/repo/ostree_repo.h b/libs/linglong/src/linglong/repo/ostree_repo.h index e9686e6eb..87a640c3c 100644 --- a/libs/linglong/src/linglong/repo/ostree_repo.h +++ b/libs/linglong/src/linglong/repo/ostree_repo.h @@ -7,12 +7,12 @@ #ifndef LINGLONG_SRC_MODULE_REPO_OSTREE_REPO_H_ #define LINGLONG_SRC_MODULE_REPO_OSTREE_REPO_H_ -#include "ClientApi.h" #include "linglong/api/types/v1/RepoConfig.hpp" #include "linglong/package/fuzzy_reference.h" #include "linglong/package/layer_dir.h" #include "linglong/package/reference.h" #include "linglong/package_manager/task.h" +#include "linglong/repo/client_factory.h" #include "linglong/utils/error/error.h" #include @@ -42,7 +42,7 @@ class OSTreeRepo : public QObject OSTreeRepo &operator=(OSTreeRepo &&) = delete; OSTreeRepo(const QDir &path, const api::types::v1::RepoConfig &cfg, - api::client::ClientApi &client) noexcept; + ClientFactory &clientFactory) noexcept; ~OSTreeRepo() override; @@ -92,7 +92,7 @@ class OSTreeRepo : public QObject QDir ostreeRepoDir() const noexcept; QDir getLayerQDir(const package::Reference &ref, bool develop = false) const noexcept; - api::client::ClientApi &apiClient; + ClientFactory &m_clientFactory; }; } // namespace linglong::repo