• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "server_executor/include/async_msg_handler.h"
17 
18 #include "protocol/retcode_inner/aie_retcode_inner.h"
19 #include "protocol/struct_definition/aie_info_define.h"
20 #include "server_executor/include/engine_manager.h"
21 #include "server_executor/include/future_factory.h"
22 #include "utils/aie_guard.h"
23 #include "utils/aie_macros.h"
24 #include "utils/log/aie_log.h"
25 
26 namespace OHOS {
27 namespace AI {
AsyncMsgHandler(Queue<Task> & g_queue,IPlugin * pluginAlgorithm)28 AsyncMsgHandler::AsyncMsgHandler(Queue<Task> &g_queue, IPlugin *pluginAlgorithm)
29     : queue_(g_queue), pluginAlgorithm_(pluginAlgorithm)
30 {
31 }
32 
Process(const Task & task)33 int AsyncMsgHandler::Process(const Task &task)
34 {
35     if (pluginAlgorithm_ == nullptr) {
36         HILOGI("[AsyncMsgHandler]The pluginAlgorithm_ is null.");
37         return RETCODE_PLUGIN_LOAD_FAILED;
38     }
39 
40     IRequest *request = task.request;
41     if (request == nullptr) {
42         HILOGE("[AsyncMsgHandler]Invalid request param");
43         return RETCODE_NULL_PARAM;
44     }
45 
46     ResGuard<IRequest> guardReq(request);
47     return pluginAlgorithm_->AsyncProcess(request, this);
48 }
49 
OnEvent(PluginEvent event,IResponse * response)50 int AsyncMsgHandler::OnEvent(PluginEvent event, IResponse *response)
51 {
52     if (response == nullptr) {
53         HILOGE("[AsyncMsgHandler]Receive Event[%d], but response is nullptr", event);
54         return RETCODE_NULL_PARAM;
55     }
56     FutureFactory *futureFactory = FutureFactory::GetInstance();
57     CHK_RET(futureFactory == nullptr, RETCODE_NULL_PARAM);
58     return futureFactory->ProcessResponse(event, response);
59 }
60 
SetPluginAlgorithm(IPlugin * pluginAlgorithm)61 void AsyncMsgHandler::SetPluginAlgorithm(IPlugin *pluginAlgorithm)
62 {
63     pluginAlgorithm_ = pluginAlgorithm;
64 }
65 
GetAsyncMsgQueueCapacity()66 size_t GetAsyncMsgQueueCapacity()
67 {
68     return MAX_ASYNC_MSG_NUM;
69 }
70 
SendRequest(IRequest * request)71 int AsyncMsgHandler::SendRequest(IRequest *request)
72 {
73     if (request == nullptr) {
74         HILOGE("[AsyncMsgHandler]Invalid request param");
75         return RETCODE_NULL_PARAM;
76     }
77 
78     if (queue_.Count() >= GetAsyncMsgQueueCapacity()) {
79         HILOGE("[AsyncMsgHandler]Task queue overload");
80         return RETCODE_QUEUE_FULL;
81     }
82 
83     FutureFactory *futureFactory = FutureFactory::GetInstance();
84     CHK_RET(futureFactory == nullptr, RETCODE_NULL_PARAM);
85 
86     int retCode = futureFactory->CreateFuture(request);
87     if (retCode != RETCODE_SUCCESS) {
88         HILOGE("[AsyncMsgHandler]Create future for async msg failed");
89         return retCode;
90     }
91 
92     Task task(this, request, nullptr);
93     return queue_.PushBack(task);
94 }
95 } // namespace AI
96 } // namespace OHOS