Merge pull request #63 from ilho159-kim/query_parcelable
[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     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");
94       continue;
95     }
96
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());
101   }
102 }
103
104 std::shared_ptr<PkgRequest> WorkerThread::PopQueue() {
105   auto req = queue_.front();
106   queue_.pop();
107   return req;
108 }
109
110 void WorkerThread::SetLocale(std::string locale) {
111   LOGD("Change locale (%s) -> (%s)", locale_.GetString().c_str(),
112        locale.c_str());
113   locale_.SetString(std::move(locale));
114 }
115
116 }  // namespace pkgmgr_server