Merge pull request #116 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 #include "command_request_handler.hh"
27
28 #ifdef LOG_TAG
29 #undef LOG_TAG
30 #endif
31 #define LOG_TAG "PKGMGR_INFO"
32
33 namespace pkgmgr_server {
34
35 WorkerThread::WorkerThread(int num) : stop_all_(false) {
36   threads_.reserve(num);
37   for (int i = 0; i < num; ++i)
38     threads_.emplace_back([this]() -> void { this->Run(); });
39   LOGD("%d Worker threads are created", num);
40 }
41
42 WorkerThread::~WorkerThread() {
43   stop_all_ = true;
44   cv_.notify_all();
45
46   for (auto& t : threads_) t.join();
47 }
48
49 bool WorkerThread::PushQueue(std::shared_ptr<PkgRequest> req) {
50   {
51     std::unique_lock<std::mutex> u(lock_);
52     queue_.push(req);
53     cv_.notify_one();
54   }
55   return true;
56 }
57
58 void WorkerThread::Run() {
59   std::unique_ptr<request_handler::AbstractRequestHandler> handler[pkgmgr_common::ReqType::MAX];
60   handler[pkgmgr_common::ReqType::GET_PKG_INFO].reset(
61       new request_handler::GetPkginfoRequestHandler());
62   handler[pkgmgr_common::ReqType::GET_APP_INFO].reset(
63       new request_handler::GetAppinfoRequestHandler());
64   handler[pkgmgr_common::ReqType::SET_PKG_INFO].reset(
65       new request_handler::SetPkginfoRequestHandler());
66   handler[pkgmgr_common::ReqType::SET_CERT_INFO].reset(
67       new request_handler::SetCertRequestHandler());
68   handler[pkgmgr_common::ReqType::GET_CERT_INFO].reset(
69       new request_handler::GetCertRequestHandler());
70   handler[pkgmgr_common::ReqType::QUERY].reset(
71       new request_handler::QueryRequestHandler());
72   handler[pkgmgr_common::ReqType::COMMAND].reset(
73       new request_handler::CommandRequestHandler());
74
75   LOGD("Initialize request handlers");
76   while (true) {
77     std::shared_ptr<PkgRequest> req;
78     {
79       std::unique_lock<std::mutex> u(lock_);
80       cv_.wait(u, [this] { return !this->queue_.empty() || stop_all_; });
81       if (stop_all_ && queue_.empty()) return;
82       req = PopQueue();
83     }
84
85     if (req->ReceiveData() == false) {
86       LOGE("Failed to ReceiveData");
87       continue;
88     }
89
90     pkgmgr_common::ReqType type = req->GetRequestType();
91     LOGD("Request type(%d)", static_cast<int>(type));
92
93     if (type <= pkgmgr_common::ReqType::REQ_TYPE_NONE
94             || type >= pkgmgr_common::ReqType::MAX) {
95       LOGE("Request type is invalid (%d)", static_cast<int>(type));
96       continue;
97     }
98
99     if (!handler[type]->HandleRequest(req->GetData(), req->GetSize(),
100                                       locale_.GetObject())) {
101       LOGE("Failed to handle request");
102       continue;
103     }
104
105     std::vector<uint8_t> result_data = handler[type]->GetResult();
106     req->SendData(result_data.data(), result_data.size());
107   }
108 }
109
110 std::shared_ptr<PkgRequest> WorkerThread::PopQueue() {
111   auto req = queue_.front();
112   queue_.pop();
113   return req;
114 }
115
116 void WorkerThread::SetLocale(std::string locale) {
117   LOGD("Change locale (%s) -> (%s)", locale_.GetObject().c_str(),
118        locale.c_str());
119   locale_.SetObject(std::move(locale));
120 }
121
122 }  // namespace pkgmgr_server