2 * Copyright (c) 2021 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include "worker_thread.hh"
19 #include "pkgmgrinfo_debug.h"
20 #include "get_appinfo_request_handler.hh"
21 #include "get_cert_request_handler.hh"
22 #include "get_pkginfo_request_handler.hh"
23 #include "query_request_handler.hh"
24 #include "set_cert_request_handler.hh"
25 #include "set_pkginfo_request_handler.hh"
26 #include "command_request_handler.hh"
31 #define LOG_TAG "PKGMGR_INFO"
33 namespace pkgmgr_server {
35 WorkerThread::WorkerThread(int num) : stop_all_(false) {
36 threads_.reserve(num);
37 for (int i = 0; i < num; ++i)
38 threads_.emplace_back([this]() -> void { this->Run(); });
39 LOGD("%d Worker threads are created", num);
42 WorkerThread::~WorkerThread() {
46 for (auto& t : threads_) t.join();
49 bool WorkerThread::PushQueue(std::shared_ptr<PkgRequest> req) {
51 std::unique_lock<std::mutex> u(lock_);
58 void WorkerThread::Run() {
59 std::unique_ptr<request_handler::AbstractRequestHandler> handler[pkgmgr_common::ReqType::MAX];
60 handler[pkgmgr_common::ReqType::GET_PKG_INFO].reset(
61 new request_handler::GetPkginfoRequestHandler());
62 handler[pkgmgr_common::ReqType::GET_APP_INFO].reset(
63 new request_handler::GetAppinfoRequestHandler());
64 handler[pkgmgr_common::ReqType::SET_PKG_INFO].reset(
65 new request_handler::SetPkginfoRequestHandler());
66 handler[pkgmgr_common::ReqType::SET_CERT_INFO].reset(
67 new request_handler::SetCertRequestHandler());
68 handler[pkgmgr_common::ReqType::GET_CERT_INFO].reset(
69 new request_handler::GetCertRequestHandler());
70 handler[pkgmgr_common::ReqType::QUERY].reset(
71 new request_handler::QueryRequestHandler());
72 handler[pkgmgr_common::ReqType::COMMAND].reset(
73 new request_handler::CommandRequestHandler());
75 LOGD("Initialize request handlers");
77 std::shared_ptr<PkgRequest> req;
79 std::unique_lock<std::mutex> u(lock_);
80 cv_.wait(u, [this] { return !this->queue_.empty() || stop_all_; });
81 if (stop_all_ && queue_.empty()) return;
85 if (req->ReceiveData() == false) {
86 LOGE("Failed to ReceiveData");
90 pkgmgr_common::ReqType type = req->GetRequestType();
91 LOGD("Request type(%d)", static_cast<int>(type));
93 if (type <= pkgmgr_common::ReqType::REQ_TYPE_NONE
94 || type >= pkgmgr_common::ReqType::MAX) {
95 LOGE("Request type is invalid (%d)", static_cast<int>(type));
99 if (!handler[type]->HandleRequest(req->GetData(), req->GetSize(),
100 locale_.GetObject())) {
101 LOGE("Failed to handle request");
105 std::vector<uint8_t> result_data = handler[type]->GetResult();
106 req->SendData(result_data.data(), result_data.size());
110 std::shared_ptr<PkgRequest> WorkerThread::PopQueue() {
111 auto req = queue_.front();
116 void WorkerThread::SetLocale(std::string locale) {
117 LOGD("Change locale (%s) -> (%s)", locale_.GetObject().c_str(),
119 locale_.SetObject(std::move(locale));
122 } // namespace pkgmgr_server