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"
30 #define LOG_TAG "PKGMGR_INFO"
32 namespace pkgmgr_server {
34 WorkerThread::WorkerThread(int num) : stop_all_(false) {
35 threads_.reserve(num);
36 for (int i = 0; i < num; ++i)
37 threads_.emplace_back([this]() -> void { this->Run(); });
38 LOGD("%d Worker threads are created", num);
41 WorkerThread::~WorkerThread() {
45 for (auto& t : threads_) t.join();
48 bool WorkerThread::PushQueue(std::shared_ptr<PkgRequest> req) {
50 std::unique_lock<std::mutex> u(lock_);
57 void WorkerThread::Run() {
58 std::unique_ptr<request_handler::AbstractRequestHandler> handler[8];
59 handler[pkgmgr_common::ReqType::GET_PKG_INFO].reset(
60 new request_handler::GetPkginfoRequestHandler());
61 handler[pkgmgr_common::ReqType::GET_APP_INFO].reset(
62 new request_handler::GetAppinfoRequestHandler());
63 handler[pkgmgr_common::ReqType::SET_PKG_INFO].reset(
64 new request_handler::SetPkginfoRequestHandler());
65 handler[pkgmgr_common::ReqType::SET_CERT_INFO].reset(
66 new request_handler::SetCertRequestHandler());
67 handler[pkgmgr_common::ReqType::GET_CERT_INFO].reset(
68 new request_handler::GetCertRequestHandler());
69 handler[pkgmgr_common::ReqType::QUERY].reset(
70 new request_handler::QueryRequestHandler());
72 LOGD("Initialize request handlers");
74 std::shared_ptr<PkgRequest> req;
76 std::unique_lock<std::mutex> u(lock_);
77 cv_.wait(u, [this] { return !this->queue_.empty() || stop_all_; });
78 if (stop_all_ && queue_.empty()) return;
82 if (req->ReceiveData() == false) {
83 LOGE("Failed to ReceiveData");
87 pkgmgr_common::ReqType type = req->GetRequestType();
88 LOGD("Request type(%d)", static_cast<int>(type));
90 auto request_data_str =
91 std::string(reinterpret_cast<char*>(req->GetData()));
92 if (!handler[type]->HandleRequest(request_data_str, locale_.GetString())) {
93 LOGE("Failed to handle request");
97 std::string result_data_str = handler[type]->GetResult();
98 req->SendData(reinterpret_cast<unsigned char*>(
99 const_cast<char*>(result_data_str.c_str())),
100 result_data_str.size());
104 std::shared_ptr<PkgRequest> WorkerThread::PopQueue() {
105 auto req = queue_.front();
110 void WorkerThread::SetLocale(std::string locale) {
111 LOGD("Change locale (%s) -> (%s)", locale_.GetString().c_str(),
113 locale_.SetString(std::move(locale));
116 } // namespace pkgmgr_server