• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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         virtual void OnProgressWithOptions(Context &context, const CooperateEvent &event) = 0;
55 
56         void SetNext(std::shared_ptr<ICooperateStep> next);
57 
58     protected:
AddHandler(CooperateEventType event,std::function<void (Context &,const CooperateEvent &)> handler)59         void AddHandler(CooperateEventType event, std::function<void(Context&, const CooperateEvent&)> handler)
60         {
61             handlers_.emplace(event, handler);
62         }
63 
64         void TransiteTo(Context &context, CooperateState state);
65         void Switch(std::shared_ptr<ICooperateStep> step);
66         void Proceed(Context &context, const CooperateEvent &event);
67         void Reset(Context &context, const CooperateEvent &event);
68 
69         ICooperateState &parent_;
70         std::shared_ptr<ICooperateStep> prev_ { nullptr };
71         std::shared_ptr<ICooperateStep> next_ { nullptr };
72         std::map<CooperateEventType, std::function<void(Context&, const CooperateEvent&)>> handlers_;
73     };
74 
75     class Process final {
76     public:
77         Process() = default;
78         ~Process() = default;
79 
80         std::string Peer() const;
81         int32_t StartDeviceId() const;
82 
83         bool IsPeer(const std::string &networkId) const;
84 
85         void StartCooperate(Context &context, const StartCooperateEvent &event);
86         void StartCooperateWithOptions(Context &context, const StartWithOptionsEvent &event);
87         void RemoteStart(Context &context, const DSoftbusStartCooperate &event);
88         void RelayCooperate(Context &context, const DSoftbusRelayCooperate &event);
89 
90     private:
91         std::string remoteNetworkId_;
92         int32_t startDeviceId_ { -1 };
93     };
94 
95     void TransiteTo(Context &context, CooperateState state);
96     void Switch(std::shared_ptr<ICooperateStep> step);
97 
98     IStateMachine &parent_;
99     std::shared_ptr<ICooperateStep> current_ { nullptr };
100     Process process_;
101 };
102 } // namespace Cooperate
103 } // namespace DeviceStatus
104 } // namespace Msdp
105 } // namespace OHOS
106 #endif // I_COOPERATE_STATE_H
107