Server/runner (#54)
[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_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"
28
29 #ifdef LOG_TAG
30 #undef LOG_TAG
31 #endif
32 #define LOG_TAG "PKGMGR_INFO"
33
34 namespace pkgmgr_server {
35
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);
41 }
42
43 WorkerThread::~WorkerThread() {
44   stop_all_ = true;
45   cv_.notify_all();
46
47   for (auto& t : threads_) t.join();
48 }
49
50 bool WorkerThread::PushQueue(std::shared_ptr<PkgRequest> req) {
51   {
52     std::unique_lock<std::mutex> u(lock_);
53     queue_.push(req);
54     cv_.notify_one();
55   }
56   return true;
57 }
58
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());
77
78   LOGD("Initialize request handlers");
79   while (true) {
80     std::shared_ptr<PkgRequest> req;
81     {
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;
85       req = PopQueue();
86     }
87
88     if (req->ReceiveData() == false) {
89       LOGE("Failed to ReceiveData");
90       continue;
91     }
92
93     pkgmgr_common::ReqType type = req->GetRequestType();
94     LOGD("Request type(%d)", static_cast<int>(type));
95
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");
99       continue;
100     }
101
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());
105   }
106 }
107
108 std::shared_ptr<PkgRequest> WorkerThread::PopQueue() {
109   auto req = queue_.front();
110   queue_.pop();
111   return req;
112 }
113
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));
117 }
118
119 }  // namespace pkgmgr_server