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_appctrlpriv_request_handler.hh"
21 #include "get_appinfo_request_handler.hh"
22 #include "get_cert_request_handler.hh"
23 #include "get_datacontrol_request_handler.hh"
24 #include "get_pkginfo_request_handler.hh"
25 #include "query_request_handler.hh"
26 #include "set_cert_request_handler.hh"
27 #include "set_pkginfo_request_handler.hh"
32 #define LOG_TAG "PKGMGR_INFO"
34 namespace pkgmgr_server {
36 WorkerThread::WorkerThread(int num) : stop_all_(false) {
37 threads_.reserve(num);
38 for (int i = 0; i < num; ++i)
39 threads_.emplace_back([this]()->void { this->Run(); });
40 LOGD("%d Worker threads are created", num);
43 WorkerThread::~WorkerThread() {
47 for (auto& t : threads_) t.join();
50 bool WorkerThread::PushQueue(std::shared_ptr<PkgRequest> req) {
52 std::unique_lock<std::mutex> u(lock_);
59 void WorkerThread::Run() {
60 std::unique_ptr<request_handler::AbstractRequestHandler> handler[8];
61 handler[pkgmgr_common::ReqType::GET_PKG_INFO].reset(
62 new request_handler::GetPkginfoRequestHandler());
63 handler[pkgmgr_common::ReqType::GET_APP_INFO].reset(
64 new request_handler::GetAppinfoRequestHandler());
65 handler[pkgmgr_common::ReqType::GET_APPCTRL_PRIV].reset(
66 new request_handler::GetAppctrlprivRequestHandler());
67 handler[pkgmgr_common::ReqType::GET_DATA_CTRL].reset(
68 new request_handler::GetDatacontrolRequestHandler());
69 handler[pkgmgr_common::ReqType::SET_PKG_INFO].reset(
70 new request_handler::SetPkginfoRequestHandler());
71 handler[pkgmgr_common::ReqType::SET_CERT_INFO].reset(
72 new request_handler::SetCertRequestHandler());
73 handler[pkgmgr_common::ReqType::GET_CERT_INFO].reset(
74 new request_handler::GetCertRequestHandler());
75 handler[pkgmgr_common::ReqType::QUERY].reset(
76 new request_handler::QueryRequestHandler());
78 LOGD("Initialize request handlers");
80 std::shared_ptr<PkgRequest> req;
82 std::unique_lock<std::mutex> u(lock_);
83 cv_.wait(u, [this] { return !this->queue_.empty() || stop_all_; });
84 if (stop_all_ && queue_.empty()) return;
88 if (req->ReceiveData() == false) {
89 LOGE("Failed to ReceiveData");
93 pkgmgr_common::ReqType type = req->GetRequestType();
94 LOGD("Request type(%d)", static_cast<int>(type));
96 auto request_data_str = std::string(reinterpret_cast<char*>(req->GetData()));
97 if (!handler[type]->HandleRequest(request_data_str, locale_.GetString())) {
98 LOGE("Failed to handle request");
102 std::string result_data_str = handler[type]->GetResult();
103 req->SendData(reinterpret_cast<unsigned char*>(const_cast<char*>(
104 result_data_str.c_str())), result_data_str.size());
108 std::shared_ptr<PkgRequest> WorkerThread::PopQueue() {
109 auto req = queue_.front();
114 void WorkerThread::SetLocale(std::string locale) {
115 LOGD("Change locale (%s) -> (%s)", locale_.GetString().c_str(), locale.c_str());
116 locale_.SetString(std::move(locale));
119 } // namespace pkgmgr_server