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 FUTURE_H
17 #define FUTURE_H
18 
19 #include <memory>
20 #include <ctime>
21 
22 #include "platform/semaphore/include/i_semaphore.h"
23 #include "plugin/i_plugin_callback.h"
24 #include "protocol/data_channel/include/i_request.h"
25 #include "protocol/data_channel/include/i_response.h"
26 #include "server_executor/include/i_future.h"
27 
28 namespace OHOS {
29 namespace AI {
30 class FutureFactory;
31 
32 class Future : public IFuture {
33     friend class FutureFactory;
34 public:
35     Future(IRequest *request, long long sequenceId, long long transactionId);
36 
37     ~Future() override;
38 
39     IResponse *GetResponse(int timeOut) const override;
40 
41     /**
42      * Release the future.
43      */
44     void Release() override;
45 
46     /**
47      * Get future status.
48      *
49      * @return Future status.
50      */
51     FutureStatus Status() const override;
52 
53     /**
54      * Convert the event type to future status, {@code FUTURE_OK} or {@code FUTURE_ERROR}.
55      *
56      * @param [in] event Plugin event, {@code ON_PLUGIN_SUCCEED} or {@code ON_PLUGIN_FAIL}.
57      * @return Future status.
58      */
59     static FutureStatus ConvertPluginStatus(PluginEvent event);
60 
61 private:
62     void DetachResponse();
63 
64     void SetResponse(FutureStatus status, IResponse *response);
65 
66     long long GetTransactionId() const;
67 
68     long long GetSequenceId() const;
69 
70     long long GetCreateTime() const;
71 
72     IRequest *GetRequest() const;
73 
74 private:
75     long long sequenceId_;
76     long long transactionId_;
77     time_t createTime_;
78     IRequest *request_;
79     IResponse *response_;
80     FutureStatus status_;
81 
82     std::shared_ptr<ISemaphore> semaphore_;
83 };
84 } // namespace AI
85 } // namespace OHOS
86 
87 #endif // FUTURE_H