Merge pull request #70 from jusung07-son/master
[platform/core/appfw/pkgmgr-info.git] / src / server / worker_thread.cc
1 /*
2  * Copyright (c) 2021 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include "worker_thread.hh"
18
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
27 #ifdef LOG_TAG
28 #undef LOG_TAG
29 #endif
30 #define LOG_TAG "PKGMGR_INFO"
31
32 namespace pkgmgr_server {
33
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);
39 }
40
41 WorkerThread::~WorkerThread() {
42   stop_all_ = true;
43   cv_.notify_all();
44
45   for (auto& t : threads_) t.join();
46 }
47
48 bool WorkerThread::PushQueue(std::shared_ptr<PkgRequest> req) {
49   {
50     std::unique_lock<std::mutex> u(lock_);
51     queue_.push(req);
52     cv_.notify_one();
53   }
54   return true;
55 }
56
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());
71
72   LOGD("Initialize request handlers");
73   while (true) {
74     std::shared_ptr<PkgRequest> req;
75     {
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;
79       req = PopQueue();
80     }
81
82     if (req->ReceiveData() == false) {
83       LOGE("Failed to ReceiveData");
84       continue;
85     }
86
87     pkgmgr_common::ReqType type = req->GetRequestType();
88     LOGD("Request type(%d)", static_cast<int>(type));
89
90     if (!handler[type]->HandleRequest(req->GetData(), req->GetSize(), locale_.GetString())) {
91       LOGE("Failed to handle request");
92       continue;
93     }
94
95     std::vector<uint8_t> result_data = handler[type]->GetResult();
96     req->SendData(result_data.data(), result_data.size());
97   }
98 }
99
100 std::shared_ptr<PkgRequest> WorkerThread::PopQueue() {
101   auto req = queue_.front();
102   queue_.pop();
103   return req;
104 }
105
106 void WorkerThread::SetLocale(std::string locale) {
107   LOGD("Change locale (%s) -> (%s)", locale_.GetString().c_str(),
108        locale.c_str());
109   locale_.SetString(std::move(locale));
110 }
111
112 }  // namespace pkgmgr_server