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_MSG_HANDLER_H
17 #define ASYNC_MSG_HANDLER_H
18 
19 #include "platform/queuepool/queue.h"
20 #include "plugin/i_plugin.h"
21 #include "plugin/i_plugin_callback.h"
22 #include "protocol/data_channel/include/i_request.h"
23 #include "protocol/data_channel/include/i_response.h"
24 #include "server_executor/include/future.h"
25 #include "server_executor/include/i_handler.h"
26 #include "server_executor/include/task.h"
27 
28 namespace OHOS {
29 namespace AI {
30 class AsyncMsgHandler : public IHandler, public IPluginCallback {
31 public:
32     AsyncMsgHandler(Queue<Task> &queue, IPlugin *pluginAlgorithm);
33     ~AsyncMsgHandler() override = default;
34 
35     /**
36      * Asynchronous task processing.
37      *
38      * @param [in] task Asynchronous task.
39      * @return Returns 0 if the operation is successful, returns a non-zero value otherwise.
40      */
41     int Process(const Task &task) override;
42 
43     /**
44      * Called when asynchronous task processing is completed.
45      *
46      * @param [in] event The event type of the asynchronous task processing result.
47      * @param [in] response Response of asynchronous task processing.
48      * @return Returns 0 if the operation is successful, returns a non-zero value otherwise.
49      */
50     int OnEvent(PluginEvent event, IResponse *response) override;
51 
52     /**
53      * Set plugin algorithm for {@code AsyncMsgHandler}.
54      *
55      * @param [in] pluginAlgorithm Function symbol corresponding to plugin algorithm.
56      */
57     void SetPluginAlgorithm(IPlugin *pluginAlgorithm) override;
58 
59     /**
60      * Encapsulates the request as a task and puts it in the task queue.
61      *
62      * @param [in] request Request information.
63      * @return Returns 0 if the operation is successful, returns a non-zero value otherwise.
64      */
65     int SendRequest(IRequest *request);
66 
67 private:
68     Queue<Task> &queue_;
69     IPlugin *pluginAlgorithm_;
70 };
71 } // namespace AI
72 } // namespace OHOS
73 
74 #endif // ASYNC_MSG_HANDLER_H