• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #include "concurrent_task_client.h"
16 #include <cinttypes>
17 #include "if_system_ability_manager.h"
18 #include "iservice_registry.h"
19 #include "concurrent_task_log.h"
20 #include "concurrent_task_errors.h"
21 #include "system_ability_definition.h"
22 
23 namespace OHOS {
24 namespace ConcurrentTask {
GetInstance()25 ConcurrentTaskClient& ConcurrentTaskClient::GetInstance()
26 {
27     static ConcurrentTaskClient instance;
28     return instance;
29 }
30 
ReportData(uint32_t resType,int64_t value,const std::unordered_map<std::string,std::string> & mapPayload)31 void ConcurrentTaskClient::ReportData(uint32_t resType, int64_t value,
32                                       const std::unordered_map<std::string, std::string>& mapPayload)
33 {
34     CONCUR_LOGD("ConcurrentTaskClient::ReportData receive resType = %{public}u, value = %{public}" PRId64 ".",
35                   resType, value);
36     if (TryConnect() != ERR_OK) {
37         return;
38     }
39     Json::Value payload;
40     for (auto it = mapPayload.begin(); it != mapPayload.end(); ++it) {
41         payload[it->first] = it->second;
42     }
43     clientService_->ReportData(resType, value, payload);
44 }
45 
QueryInterval(int queryItem,IntervalReply & queryRs)46 void ConcurrentTaskClient::QueryInterval(int queryItem, IntervalReply& queryRs)
47 {
48     if (TryConnect() != ERR_OK) {
49         CONCUR_LOGE("QueryInterval connnect fail");
50         return;
51     }
52     clientService_->QueryInterval(queryItem, queryRs);
53     return;
54 }
55 
TryConnect()56 ErrCode ConcurrentTaskClient::TryConnect()
57 {
58     std::lock_guard<std::mutex> lock(mutex_);
59     if (clientService_) {
60         return ERR_OK;
61     }
62 
63     sptr<ISystemAbilityManager> systemManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
64     if (!systemManager) {
65         CONCUR_LOGE("ConcurrentTaskClient::Fail to get registry.");
66         return GET_CONCURRENT_TASK_SERVICE_FAILED;
67     }
68 
69     remoteObject_ = systemManager->GetSystemAbility(CONCURRENT_TASK_SERVICE_ID);
70     if (!remoteObject_) {
71         CONCUR_LOGE("ConcurrentTaskClient::Fail to connect concurrent task schedule service.");
72         return GET_CONCURRENT_TASK_SERVICE_FAILED;
73     }
74 
75     clientService_ = iface_cast<IConcurrentTaskService>(remoteObject_);
76     if (!clientService_) {
77         return GET_CONCURRENT_TASK_SERVICE_FAILED;
78     }
79     try {
80         recipient_ = new ConcurrentTaskDeathRecipient(*this);
81     } catch (const std::bad_alloc& e) {
82         CONCUR_LOGE("ConcurrentTaskClient::New ConcurrentTaskDeathRecipient fail.");
83     }
84     if (!recipient_) {
85         return GET_CONCURRENT_TASK_SERVICE_FAILED;
86     }
87     clientService_->AsObject()->AddDeathRecipient(recipient_);
88     CONCUR_LOGD("ConcurrentTaskClient::Connect concurrent task service success.");
89     return ERR_OK;
90 }
91 
StopRemoteObject()92 void ConcurrentTaskClient::StopRemoteObject()
93 {
94     if (clientService_ && clientService_->AsObject()) {
95         clientService_->AsObject()->RemoveDeathRecipient(recipient_);
96     }
97     clientService_ = nullptr;
98 }
99 
ConcurrentTaskDeathRecipient(ConcurrentTaskClient & concurrentTaskClient)100 ConcurrentTaskClient::ConcurrentTaskDeathRecipient::ConcurrentTaskDeathRecipient(
101     ConcurrentTaskClient& concurrentTaskClient) : concurrentTaskClient_(concurrentTaskClient) {}
102 
~ConcurrentTaskDeathRecipient()103 ConcurrentTaskClient::ConcurrentTaskDeathRecipient::~ConcurrentTaskDeathRecipient() {}
104 
OnRemoteDied(const wptr<IRemoteObject> & object)105 void ConcurrentTaskClient::ConcurrentTaskDeathRecipient::OnRemoteDied(const wptr<IRemoteObject>& object)
106 {
107     concurrentTaskClient_.StopRemoteObject();
108 }
109 } // namespace ConcurrentTask
110 } // namespace OHOS