Add Error handling in RequestHandler (#117)
[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 "abstract_parcelable.hh"
27 #include "command_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[pkgmgr_common::ReqType::MAX];
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::SET_PKG_INFO].reset(
66       new request_handler::SetPkginfoRequestHandler());
67   handler[pkgmgr_common::ReqType::SET_CERT_INFO].reset(
68       new request_handler::SetCertRequestHandler());
69   handler[pkgmgr_common::ReqType::GET_CERT_INFO].reset(
70       new request_handler::GetCertRequestHandler());
71   handler[pkgmgr_common::ReqType::QUERY].reset(
72       new request_handler::QueryRequestHandler());
73   handler[pkgmgr_common::ReqType::COMMAND].reset(
74       new request_handler::CommandRequestHandler());
75
76   LOGD("Initialize request handlers");
77   while (true) {
78     std::shared_ptr<PkgRequest> req;
79     {
80       std::unique_lock<std::mutex> u(lock_);
81       cv_.wait(u, [this] { return !this->queue_.empty() || stop_all_; });
82       if (stop_all_ && queue_.empty()) return;
83       req = PopQueue();
84     }
85
86     if (req->ReceiveData() == false) {
87       LOGE("Failed to ReceiveData");
88       continue;
89     }
90     pkgmgr_common::ReqType type = req->GetRequestType();
91     LOGD("Request type(%d)", static_cast<int>(type));
92     if (type <= pkgmgr_common::ReqType::REQ_TYPE_NONE
93             || type >= pkgmgr_common::ReqType::MAX) {
94       LOGE("Request type is invalid (%d)", static_cast<int>(type));
95
96       pkgmgr_common::parcel::AbstractParcelable parcelable(
97           0, pkgmgr_common::parcel::ParcelableType::Unknown, PMINFO_R_ERROR);
98       tizen_base::Parcel p;
99       p.WriteParcelable(parcelable);
100       std::vector<uint8_t> raw = p.GetRaw();
101       req->SendData(&raw[0], raw.size());
102       continue;
103     }
104
105     try {
106       if (!handler[type]->HandleRequest(req->GetData(), req->GetSize(),
107                                         locale_.GetObject()))
108         LOGE("Failed to handle request");
109     } catch (const std::exception& err) {
110       LOGE("Exception occurred (%s)", err.what());
111       pkgmgr_common::parcel::AbstractParcelable parcelable(
112           0, pkgmgr_common::parcel::ParcelableType::Unknown, PMINFO_R_ERROR);
113       tizen_base::Parcel p;
114       p.WriteParcelable(parcelable);
115       std::vector<uint8_t> raw = p.GetRaw();
116       req->SendData(&raw[0], raw.size());
117       continue;
118     } catch (...) {
119       LOGE("Exception occurred");
120       pkgmgr_common::parcel::AbstractParcelable parcelable(
121           0, pkgmgr_common::parcel::ParcelableType::Unknown, PMINFO_R_ERROR);
122       tizen_base::Parcel p;
123       p.WriteParcelable(parcelable);
124       std::vector<uint8_t> raw = p.GetRaw();
125       req->SendData(&raw[0], raw.size());
126       continue;
127     }
128
129     std::vector<uint8_t> result_data = handler[type]->GetResult();
130     req->SendData(result_data.data(), result_data.size());
131   }
132 }
133
134 std::shared_ptr<PkgRequest> WorkerThread::PopQueue() {
135   auto req = queue_.front();
136   queue_.pop();
137   return req;
138 }
139
140 void WorkerThread::SetLocale(std::string locale) {
141   LOGD("Change locale (%s) -> (%s)", locale_.GetObject().c_str(),
142        locale.c_str());
143   locale_.SetObject(std::move(locale));
144 }
145
146 }  // namespace pkgmgr_server