1 /*
2  * Copyright (c) 2023-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 #ifndef I_COOPERATE_STATE_H
17 #define I_COOPERATE_STATE_H
18 
19 #include "cooperate_context.h"
20 
21 namespace OHOS {
22 namespace Msdp {
23 namespace DeviceStatus {
24 namespace Cooperate {
25 namespace {
26     constexpr int32_t PRIORITY { 1 };
27 }
28 class IStateMachine {
29 public:
30     IStateMachine() = default;
31     virtual ~IStateMachine() = default;
32 
33     virtual void TransiteTo(Context &context, CooperateState state) = 0;
34 };
35 
36 class ICooperateState {
37 public:
ICooperateState(IStateMachine & parent)38     ICooperateState(IStateMachine &parent) : parent_(parent) {}
39     virtual ~ICooperateState() = default;
40 
41     virtual void OnEvent(Context &context, const CooperateEvent &event) = 0;
42     virtual void OnEnterState(Context &context) = 0;
43     virtual void OnLeaveState(Context &context) = 0;
44 
45 protected:
46     class ICooperateStep {
47     public:
48         ICooperateStep(ICooperateState &parent, std::shared_ptr<ICooperateStep> prev);
49         virtual ~ICooperateStep() = default;
50 
51         virtual void OnEvent(Context &context, const CooperateEvent &event);
52         virtual void OnProgress(Context &context, const CooperateEvent &event) = 0;
53         virtual void OnReset(Context &context, const CooperateEvent &event) = 0;
54 
55         void SetNext(std::shared_ptr<ICooperateStep> next);
56 
57     protected:
AddHandler(CooperateEventType event,std::function<void (Context &,const CooperateEvent &)> handler)58         void AddHandler(CooperateEventType event, std::function<void(Context&, const CooperateEvent&)> handler)
59         {
60             handlers_.emplace(event, handler);
61         }
62 
63         void TransiteTo(Context &context, CooperateState state);
64         void Switch(std::shared_ptr<ICooperateStep> step);
65         void Proceed(Context &context, const CooperateEvent &event);
66         void Reset(Context &context, const CooperateEvent &event);
67 
68         ICooperateState &parent_;
69         std::shared_ptr<ICooperateStep> prev_ { nullptr };
70         std::shared_ptr<ICooperateStep> next_ { nullptr };
71         std::map<CooperateEventType, std::function<void(Context&, const CooperateEvent&)>> handlers_;
72     };
73 
74     class Process final {
75     public:
76         Process() = default;
77         ~Process() = default;
78 
79         std::string Peer() const;
80         int32_t StartDeviceId() const;
81 
82         bool IsPeer(const std::string &networkId) const;
83 
84         void StartCooperate(Context &context, const StartCooperateEvent &event);
85         void RemoteStart(Context &context, const DSoftbusStartCooperate &event);
86         void RelayCooperate(Context &context, const DSoftbusRelayCooperate &event);
87 
88     private:
89         std::string remoteNetworkId_;
90         int32_t startDeviceId_ { -1 };
91     };
92 
93     void TransiteTo(Context &context, CooperateState state);
94     void Switch(std::shared_ptr<ICooperateStep> step);
95 
96     IStateMachine &parent_;
97     std::shared_ptr<ICooperateStep> current_ { nullptr };
98     Process process_;
99 };
100 } // namespace Cooperate
101 } // namespace DeviceStatus
102 } // namespace Msdp
103 } // namespace OHOS
104 #endif // I_COOPERATE_STATE_H
105