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 SA_ASYNC_HANDLER_H
17 #define SA_ASYNC_HANDLER_H
18 
19 #include <map>
20 #include <mutex>
21 
22 #include "communication_adapter/include/client_listener_handler.h"
23 #include "platform/lock/include/rw_lock.h"
24 #include "server_executor/include/i_future_listener.h"
25 #include "utils/aie_macros.h"
26 
27 namespace OHOS {
28 namespace AI {
29 class SaAsyncHandler {
30     FORBID_COPY_AND_ASSIGN(SaAsyncHandler);
31     FORBID_CREATE_BY_SELF(SaAsyncHandler);
32 public:
33     static SaAsyncHandler *GetInstance();
34 
35     /**
36      * Save the response for the client async handler.
37      *
38      * @param clientId Client of this handler.
39      * @param response The media of asynchronous result.
40      */
41     void PushAsyncResponse(int clientId, IResponse *response);
42 
43     /**
44      * Allocate client listener handler for certain client ID.
45      *
46      * @param clientId Client ID.
47      * @return Returns 0 if the operation is successful, returns a non-zero value otherwise.
48      */
49     int RegisterAsyncHandler(int clientId);
50 
51     /**
52      * Start client async handler thread to execute algorithm.
53      *
54      * @param clientId Client ID.
55      * @param adapter The adapter of above client.
56      * @return Returns 0 if the operation is successful, returns a non-zero value otherwise.
57      */
58     int StartAsyncProcess(int clientId, SaServerAdapter *adapter);
59 
60     /**
61      * Stop client async handler thread to execute algorithm.
62      *
63      * @param clientId The client to be stopped.
64      */
65     void StopAsyncProcess(int clientId);
66 
67     /**
68      * Make pair of transaction ID and FutureListener.
69      *
70      * @param transactionId Transaction ID.
71      * @param clientId Client ID.
72      * @return Returns 0 if the operation is successful, returns a non-zero value otherwise.
73      */
74     int StartAsyncTransaction(long long transactionId, int clientId);
75 
76     /**
77      * Remove transaction ID and its FutureListener.
78      *
79      * @param transactionId.
80      */
81     void StopAsyncTransaction(long long transactionId);
82 
83 private:
84 
85     /**
86      * Stop the thread of client listener handler by client ID and remove it.
87      * {@link StopClientListenerHandler(clientId)}.
88      *
89      * @param clientId Client ID.
90      */
91     void StopClientListenerHandler(int clientId);
92 
93     /**
94      * Remove client listener handler from the map.
95      *
96      * @param clientId Client ID.
97      */
98     void RemoveClientListenerHandler(int clientId);
99     ClientListenerHandler *FindClientListenerHandler(int clientId);
100     ClientListenerHandler *AddClientListenerHandler(int clientId);
101 
102     /**
103      * Remove transaction ID from a set<transactionId> {@link TransactionIds}.
104      *
105      * @param transactionId transaction ID.
106      */
107     void RemoveTransaction(long long transactionId);
108 
109     /**
110      * Check if transaction ID exists.
111      *
112      * @param transactionId transaction ID.
113      * @return 1-exist, 0-not exist.
114      */
115     bool IsExistTransaction(long long transactionId);
116 
117     /**
118      * Save transaction ID in a set<transactionId> {@link TransactionIds}.
119      *
120      * @param transactionId transaction ID.
121      */
122     void SaveTransaction(long long transactionId);
123 
124 private:
125     static std::mutex mutex_;
126     static SaAsyncHandler *instance_;
127     RwLock rwLock_;
128 
129     using ClientListenerHandlerMap = std::map<int, ClientListenerHandler*>;
130     ClientListenerHandlerMap clients_;
131 
132     using TransactionIds = std::set<long long>;
133     TransactionIds transactions_;
134 };
135 } // namespace AI
136 } // namespace OHOS
137 
138 #endif // SA_ASYNC_HANDLER_H