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 #ifndef ASYNC_HANDLER_H
17 #define ASYNC_HANDLER_H
18 
19 #include <map>
20 #include <mutex>
21 
22 #include "client_executor/include/i_client_cb.h"
23 #include "protocol/retcode_inner/aie_retcode_inner.h"
24 #include "protocol/struct_definition/aie_info_define.h"
25 #include "utils/aie_macros.h"
26 
27 namespace OHOS {
28 namespace AI {
29 class AsyncHandler {
30 public:
31     AsyncHandler();
32     virtual ~AsyncHandler();
33 
34     int RegisterCb(int sessionId, IClientCb *cb);
35     int UnRegisterCb(int sessionId);
36 
37     int RegisterDeadCb(int sessionId, IServiceDeadCb *cb);
38     int UnRegisterDeadCb(int sessionId);
39 
40     void OnResult(int sessionId, const DataInfo &result, int resultCode, int requestId);
41     void OnDead();
42 
43     void FindCb(int sessionId, IClientCb *&cb);
44 
45     bool IsCallbackEmpty();
46     int GetAsyncCbSize();
47 
48 private:
49     virtual int RegisterAsyncClientCb() = 0;
50     virtual int UnRegisterAsyncClientCb() = 0;
51     virtual int RegisterServiceDeathCb() = 0;
52     virtual int UnRegisterServiceDeathCb() = 0;
53 
54 private:
55     static std::mutex cbMutex_;
56     using ClientCbs = std::map<int, IClientCb *>;
57     ClientCbs mapCbMsg_;
58     std::mutex deadCbMutex_;
59     using DeadCbs = std::map<int, IServiceDeadCb *>;
60     DeadCbs deadCbs_;
61 };
62 } // namespace AI
63 } // namespace OHOS
64 
65 #endif // ASYNC_HANDLER_H