• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 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 DISTRIBUTEDSCHED_MISSION_MANAGER_H
17 #define DISTRIBUTEDSCHED_MISSION_MANAGER_H
18 
19 #include <set>
20 #include <string>
21 #include <vector>
22 
23 #include "distributed_data_storage.h"
24 #include "distributed_mission_change_listener.h"
25 #include "distributed_mission_info.h"
26 #include "distributed_sched_interface.h"
27 #include "event_handler.h"
28 #include "single_instance.h"
29 #include "snapshot.h"
30 
31 namespace OHOS {
32 namespace DistributedSchedule {
33 struct ListenerInfo {
34     bool called = false;
35     std::set<sptr<IRemoteObject>> listenerSet;
36 
EmplaceListenerInfo37     bool Emplace(sptr<IRemoteObject> listener)
38     {
39         auto pairRet = listenerSet.emplace(listener);
40         if (!pairRet.second) {
41             return false;
42         }
43         return true;
44     }
45 
FindListenerInfo46     bool Find(sptr<IRemoteObject> listener)
47     {
48         auto iter = listenerSet.find(listener);
49         if (iter == listenerSet.end()) {
50             return false;
51         }
52         return true;
53     }
54 
EraseListenerInfo55     void Erase(sptr<IRemoteObject> listener)
56     {
57         listenerSet.erase(listener);
58     }
59 
SizeListenerInfo60     int32_t Size() const
61     {
62         return listenerSet.size();
63     }
64 
EmptyListenerInfo65     bool Empty() const
66     {
67         return listenerSet.empty();
68     }
69 };
70 class DistributedSchedMissionManager {
71     DECLARE_SINGLE_INSTANCE(DistributedSchedMissionManager);
72 
73 public:
74     void Init();
75     int32_t GetMissionInfos(const std::string& deviceId, int32_t numMissions,
76         std::vector<AAFwk::MissionInfo>& missionInfos);
77     int32_t InitDataStorage();
78     int32_t StopDataStorage();
79     int32_t StoreSnapshotInfo(const std::string& deviceId, int32_t missionId,
80         const uint8_t* byteStream, size_t len);
81     int32_t RemoveSnapshotInfo(const std::string& deviceId, int32_t missionId);
82     int32_t GetRemoteMissionSnapshotInfo(const std::string& networkId, int32_t missionId,
83         std::unique_ptr<AAFwk::MissionSnapshot>& missionSnapshot);
84     void DeviceOnlineNotify(const std::string& deviceId);
85     void DeviceOfflineNotify(const std::string& deviceId);
86     void DeleteDataStorage(const std::string& deviceId, bool isDelayed);
87     int32_t RegisterMissionListener(const std::u16string& devId, const sptr<IRemoteObject>& obj);
88     int32_t UnRegisterMissionListener(const std::u16string& devId, const sptr<IRemoteObject>& obj);
89     int32_t StartSyncRemoteMissions(const std::string& devId, bool fixConflict, int64_t tag);
90     int32_t StartSyncMissionsFromRemote(const CallerInfo& callerInfo, std::vector<DstbMissionInfo>& missionInfos);
91     int32_t StopSyncRemoteMissions(const std::string& dstDevId, bool offline, bool exit = false);
92     void StopSyncMissionsFromRemote(const std::string& deviceId);
93     bool NeedSyncDevice(const std::string& deviceId);
94 
95     void NotifySnapshotChanged(const std::string& networkId, int32_t missionId);
96     void OnRemoteDied(const wptr<IRemoteObject>& remote);
97 
98     void EnqueueCachedSnapshotInfo(const std::string& deviceId, int32_t missionId, std::unique_ptr<Snapshot> snapshot);
99     std::unique_ptr<Snapshot> DequeueCachedSnapshotInfo(const std::string& deviceId, int32_t missionId);
100     int32_t NotifyMissionsChangedToRemote(const std::vector<DstbMissionInfo>& missionInfos);
101     int32_t NotifyMissionsChangedFromRemote(const CallerInfo& callerInfo,
102         const std::vector<DstbMissionInfo>& missionInfos);
103     void OnRemoteDmsDied(const wptr<IRemoteObject>& remote);
104     void NotifyDmsProxyProcessDied();
105     void OnDnetDied();
106     void NotifyLocalMissionsChanged();
107     void NotifyMissionSnapshotCreated(int32_t missionId);
108     void NotifyMissionSnapshotChanged(int32_t missionId);
109     void NotifyMissionSnapshotDestroyed(int32_t missionId);
110     void NotifyRemoteDied(const wptr<IRemoteObject>& remote);
111 private:
112     std::map<std::string, std::shared_ptr<AppExecFwk::EventHandler>> deviceHandle_;
113     mutable std::mutex remoteMissionInfosLock_;
114     std::map<std::string, std::vector<DstbMissionInfo>> deviceMissionInfos_;
115     sptr<IDistributedSched> GetRemoteDms(const std::string& deviceId);
116     bool IsDeviceIdValidated(const std::string& deviceId);
117     std::shared_ptr<AppExecFwk::EventHandler> FetchDeviceHandler(const std::string& deviceId);
118     bool GenerateCallerInfo(CallerInfo& callerInfo);
119     void NotifyMissionsChangedToRemoteInner(const std::string& remoteUuid,
120         const std::vector<DstbMissionInfo>& missionInfos, const CallerInfo& callerInfo);
GenerateKeyInfo(const std::string & devId,int32_t missionId)121     std::string GenerateKeyInfo(const std::string& devId, int32_t missionId)
122     {
123         return devId + "_" + std::to_string(missionId);
124     }
125     int32_t StartSyncRemoteMissions(const std::string& dstDevId, const std::string& localDevId);
126     int32_t StartSyncRemoteMissions(const std::string& dstDevId, const sptr<IDistributedSched>& remoteDms);
127     void CleanMissionResources(const std::string& dstDevId);
128     void RetryStartSyncRemoteMissions(const std::string& dstDeviceId, const std::string& localDevId,
129         int32_t retryTimes);
130     bool HasSyncListener(const std::string& networkId);
131     void DeleteCachedSnapshotInfo(const std::string& networkId);
132     int32_t FetchCachedRemoteMissions(const std::string& srcId, int32_t numMissions,
133         std::vector<DstbMissionInfo>& missionInfos);
134     void RebornMissionCache(const std::string& deviceId, const std::vector<DstbMissionInfo>& missionInfos);
135     void CleanMissionCache(const std::string& deviceId);
136     void OnMissionListenerDied(const sptr<IRemoteObject>& remote);
137     void OnRemoteDmsDied(const sptr<IRemoteObject>& remote);
138     void RetryRegisterMissionChange(int32_t retryTimes);
139     void InitAllSnapshots(const std::vector<DstbMissionInfo>& missionInfos);
140     int32_t MissionSnapshotChanged(int32_t missionId);
141     int32_t MissionSnapshotDestroyed(int32_t missionId);
142     int32_t MissionSnapshotSequence(const Snapshot& snapshot, MessageParcel& data);
143 
144     class ListenerDeathRecipient : public IRemoteObject::DeathRecipient {
145     public:
146         void OnRemoteDied(const wptr<IRemoteObject>& remote) override;
147     };
148     sptr<ListenerDeathRecipient> listenerDeath_;
149 
150     std::set<std::string> remoteSyncDeviceSet_;
151     std::mutex remoteSyncDeviceLock_;
152 
153     std::map<std::string, std::unique_ptr<Snapshot>> cachedSnapshotInfos_;
154     std::map<std::u16string, ListenerInfo> listenDeviceMap_;
155     std::mutex listenDeviceLock_;
156     std::shared_ptr<DistributedDataStorage> distributedDataStorage_;
157 
158     std::set<int32_t> allowMissionUids_;
159     std::mutex allowMissionUidsLock_;
160     std::atomic<bool> isRegMissionChange_ = false;
161     sptr<DistributedMissionChangeListener> missonChangeListener_;
162     std::shared_ptr<AppExecFwk::EventHandler> missionChangeHandler_;
163 
164     class RemoteDmsDeathRecipient : public IRemoteObject::DeathRecipient {
165     public:
166         void OnRemoteDied(const wptr<IRemoteObject>& remote) override;
167     };
168     sptr<RemoteDmsDeathRecipient> remoteDmsRecipient_;
169     std::map<std::string, sptr<IDistributedSched>> remoteDmsMap_;
170     std::mutex remoteDmsLock_;
171     std::shared_ptr<AppExecFwk::EventHandler> missionHandler_;
172     std::shared_ptr<AppExecFwk::EventHandler> updateHandler_;
173 };
174 }
175 }
176 #endif // DISTRIBUTEDSCHED_MISSION_MANAGER_H
177