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