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/future.h"
17
18 #include "platform/semaphore/include/i_semaphore.h"
19 #include "platform/time/include/time.h"
20 #include "protocol/data_channel/include/i_request.h"
21 #include "protocol/data_channel/include/i_response.h"
22 #include "protocol/retcode_inner/aie_retcode_inner.h"
23 #include "server_executor/include/future_factory.h"
24 #include "utils/log/aie_log.h"
25
26 namespace OHOS {
27 namespace AI {
Future(IRequest * request,long long sequenceId,long long transactionId)28 Future::Future(IRequest *request, long long sequenceId, long long transactionId)
29 : sequenceId_(sequenceId),
30 transactionId_(transactionId),
31 request_(request),
32 response_(nullptr),
33 status_(FUTURE_OK)
34 {
35 createTime_ = GetCurTimeSec();
36 semaphore_ = ISemaphore::MakeShared(0);
37 }
38
~Future()39 Future::~Future()
40 {
41 request_ = nullptr;
42
43 if (response_ != nullptr) {
44 IResponse::Destroy(response_);
45 }
46 }
47
GetResponse(int timeOut) const48 IResponse *Future::GetResponse(int timeOut) const
49 {
50 semaphore_->Wait(timeOut);
51 return response_;
52 }
53
Release()54 void Future::Release()
55 {
56 FutureFactory *futureFactory = FutureFactory::GetInstance();
57 CHK_RET_NONE(futureFactory == nullptr);
58 futureFactory->Release(sequenceId_);
59 }
60
GetRequest() const61 IRequest *Future::GetRequest() const
62 {
63 return request_;
64 }
65
Status() const66 FutureStatus Future::Status() const
67 {
68 return status_;
69 }
70
ConvertPluginStatus(PluginEvent event)71 FutureStatus Future::ConvertPluginStatus(PluginEvent event)
72 {
73 if (event == ON_PLUGIN_SUCCEED) {
74 return FUTURE_OK;
75 }
76 return FUTURE_ERROR;
77 }
78
DetachResponse()79 void Future::DetachResponse()
80 {
81 response_ = nullptr;
82 }
83
SetResponse(FutureStatus status,IResponse * response)84 void Future::SetResponse(FutureStatus status, IResponse *response)
85 {
86 if (response_ != nullptr) {
87 IResponse::Destroy(response_);
88 }
89
90 status_ = status;
91 response_ = response;
92 semaphore_->Signal();
93 }
94
GetTransactionId() const95 long long Future::GetTransactionId() const
96 {
97 return transactionId_;
98 }
99
GetSequenceId() const100 long long Future::GetSequenceId() const
101 {
102 return sequenceId_;
103 }
104
GetCreateTime() const105 long long Future::GetCreateTime() const
106 {
107 return createTime_;
108 }
109 } // namespace AI
110 } // namespace OHOS