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