1 /*
2  * Copyright (c) 2024 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 "dsched_continue_sink_wait_end_state.h"
17 
18 #include "dsched_continue.h"
19 #include "dsched_continue_event.h"
20 #include "dtbschedmgr_log.h"
21 
22 namespace OHOS {
23 namespace DistributedSchedule {
24 namespace {
25 const std::string TAG = "DSchedContinueSinkWaitEndState";
26 }
DSchedContinueSinkWaitEndState(std::shared_ptr<DSchedContinueStateMachine> stateMachine)27 DSchedContinueSinkWaitEndState::DSchedContinueSinkWaitEndState(std::shared_ptr<DSchedContinueStateMachine> stateMachine)
28     : stateMachine_(stateMachine)
29 {
30     memberFuncMap_[DSCHED_CONTINUE_COMPLETE_EVENT] = &DSchedContinueSinkWaitEndState::DoNotifyCompleteTask;
31     memberFuncMap_[DSCHED_CONTINUE_END_EVENT] = &DSchedContinueSinkWaitEndState::DoContinueEndTask;
32 }
33 
~DSchedContinueSinkWaitEndState()34 DSchedContinueSinkWaitEndState::~DSchedContinueSinkWaitEndState()
35 {
36 }
37 
Execute(std::shared_ptr<DSchedContinue> dContinue,const AppExecFwk::InnerEvent::Pointer & event)38 int32_t DSchedContinueSinkWaitEndState::Execute(std::shared_ptr<DSchedContinue> dContinue,
39     const AppExecFwk::InnerEvent::Pointer &event)
40 {
41     if (event == nullptr) {
42         HILOGE("event is null");
43         return INVALID_PARAMETERS_ERR;
44     }
45     auto iterFunc = memberFuncMap_.find(event->GetInnerEventId());
46     if (iterFunc == memberFuncMap_.end()) {
47         HILOGI("DSchedContinueSinkWaitEndState execute %{public}d in wrong state", event->GetInnerEventId());
48         return CONTINUE_STATE_MACHINE_INVALID_STATE;
49     }
50 
51     auto memberFunc = iterFunc->second;
52     int32_t ret = (this->*memberFunc)(dContinue, event);
53     if (ret != ERR_OK) {
54         HILOGI("DSchedContinueSinkWaitEndState execute %{public}d failed, ret: %{public}d",
55             event->GetInnerEventId(), ret);
56     }
57     return ret;
58 }
59 
GetStateType()60 DSchedContinueStateType DSchedContinueSinkWaitEndState::GetStateType()
61 {
62     return DSCHED_CONTINUE_SINK_WAIT_END_STATE;
63 }
64 
DoNotifyCompleteTask(std::shared_ptr<DSchedContinue> dContinue,const AppExecFwk::InnerEvent::Pointer & event)65 int32_t DSchedContinueSinkWaitEndState::DoNotifyCompleteTask(std::shared_ptr<DSchedContinue> dContinue,
66     const AppExecFwk::InnerEvent::Pointer &event)
67 {
68     if (dContinue == nullptr || event == nullptr) {
69         HILOGE("dContinue or event is null");
70         return INVALID_PARAMETERS_ERR;
71     }
72     auto syncContinueData = event->GetSharedObject<int32_t>();
73     int32_t ret = dContinue->ExecuteNotifyComplete(*syncContinueData);
74     if (ret != ERR_OK) {
75         HILOGE("DSchedContinueSinkWaitEndState sync continue data failed, datat is null");
76     }
77     return ret;
78 }
79 
DoContinueEndTask(std::shared_ptr<DSchedContinue> dContinue,const AppExecFwk::InnerEvent::Pointer & event)80 int32_t DSchedContinueSinkWaitEndState::DoContinueEndTask(std::shared_ptr<DSchedContinue> dContinue,
81     const AppExecFwk::InnerEvent::Pointer &event)
82 {
83     if (dContinue == nullptr || event == nullptr) {
84         HILOGE("dContinue or event is null");
85         return INVALID_PARAMETERS_ERR;
86     }
87     auto syncContinueData = event->GetSharedObject<int32_t>();
88     int32_t ret = dContinue->ExecuteContinueError(*syncContinueData);
89     if (ret != ERR_OK) {
90         HILOGE("DSchedContinueSinkWaitEndState ExecuteContinueSend failed, ret: %{public}d", ret);
91     }
92     return ret;
93 }
94 }  // namespace DistributedSchedule
95 }  // namespace OHOS
96