1 /*
2 * Copyright (c) 2021 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
16 #include "client_executor/include/async_handler.h"
17
18 #include "client_executor/include/i_client_cb.h"
19 #include "protocol/retcode_inner/aie_retcode_inner.h"
20 #include "utils/log/aie_log.h"
21
22 namespace OHOS {
23 namespace AI {
24 AsyncHandler::AsyncHandler() = default;
25
26 AsyncHandler::~AsyncHandler() = default;
27
RegisterCb(int sessionId,IClientCb * cb)28 int AsyncHandler::RegisterCb(int sessionId, IClientCb *cb)
29 {
30 if (cb != nullptr) {
31 mapCbMsg_.insert(std::make_pair(sessionId, cb));
32 }
33 return RETCODE_SUCCESS;
34 }
35
36 std::mutex AsyncHandler::cbMutex_;
37
UnRegisterCb(int sessionId)38 int AsyncHandler::UnRegisterCb(int sessionId)
39 {
40 std::lock_guard<std::mutex> guard(cbMutex_);
41 if (mapCbMsg_.erase(sessionId) == 0) {
42 HILOGE("[AsyncHandler][sessionId:%d]session does not exist", sessionId);
43 return RETCODE_FAILURE;
44 }
45 return RETCODE_SUCCESS;
46 }
47
RegisterDeadCb(int sessionId,IServiceDeadCb * cb)48 int AsyncHandler::RegisterDeadCb(int sessionId, IServiceDeadCb *cb)
49 {
50 if (cb != nullptr) {
51 deadCbs_.insert(std::make_pair(sessionId, cb));
52 }
53 return RETCODE_SUCCESS;
54 }
55
UnRegisterDeadCb(int sessionId)56 int AsyncHandler::UnRegisterDeadCb(int sessionId)
57 {
58 std::lock_guard<std::mutex> guard(deadCbMutex_);
59 deadCbs_.erase(sessionId); // nullptr deadCb won't register in RegisterDeadCb, ignore return value
60 return RETCODE_SUCCESS;
61 }
62
OnResult(int sessionId,const DataInfo & result,int resultCode,int requestId)63 void AsyncHandler::OnResult(int sessionId, const DataInfo &result, int resultCode, int requestId)
64 {
65 IClientCb *cb = nullptr;
66 FindCb(sessionId, cb);
67 if (cb == nullptr) {
68 HILOGE("[AsyncHandler]Can't find callback, throw away the msg of session id[%d]", sessionId);
69 return;
70 }
71 cb->OnResult(result, resultCode, requestId);
72 }
73
OnDead()74 void AsyncHandler::OnDead()
75 {
76 for (auto &iter : deadCbs_) {
77 IServiceDeadCb *deadCb = iter.second;
78 if (deadCb != nullptr) {
79 deadCb->OnServiceDead();
80 }
81 }
82 HILOGW("[AsyncHandler]On Service Dead");
83 }
84
FindCb(int sessionId,IClientCb * & cb)85 void AsyncHandler::FindCb(int sessionId, IClientCb *&cb)
86 {
87 std::lock_guard<std::mutex> guard(cbMutex_);
88 ClientCbs::iterator iter = mapCbMsg_.find(sessionId);
89 if (iter == mapCbMsg_.end()) {
90 HILOGW("[AsyncHandler][sessionId:%d]Can't find callback.", sessionId);
91 return;
92 }
93 cb = iter->second;
94 }
95
IsCallbackEmpty()96 bool AsyncHandler::IsCallbackEmpty()
97 {
98 return mapCbMsg_.empty();
99 }
100
GetAsyncCbSize()101 int AsyncHandler::GetAsyncCbSize()
102 {
103 return mapCbMsg_.size();
104 }
105 } // namespace AI
106 } // namespace OHOS