/* * Copyright (c) 2021-2023 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef DISTRIBUTEDSCHED_MISSION_MANAGER_H #define DISTRIBUTEDSCHED_MISSION_MANAGER_H #include #include #include #include "distributed_data_storage.h" #include "distributed_mission_change_listener.h" #include "distributed_mission_info.h" #include "distributed_sched_interface.h" #include "event_handler.h" #include "single_instance.h" #include "snapshot.h" namespace OHOS { namespace DistributedSchedule { struct ListenerInfo { bool called = false; std::set> listenerSet; bool Emplace(sptr listener) { auto pairRet = listenerSet.emplace(listener); return pairRet.second; } bool Find(const sptr listener) { auto iter = listenerSet.find(listener); if (iter == listenerSet.end()) { return false; } return true; } void Erase(sptr listener) { listenerSet.erase(listener); } int32_t Size() const { return listenerSet.size(); } bool Empty() const { return listenerSet.empty(); } }; class DistributedSchedMissionManager { DECLARE_SINGLE_INSTANCE(DistributedSchedMissionManager); public: void Init(); int32_t GetMissionInfos(const std::string& deviceId, int32_t numMissions, std::vector& missionInfoSet); int32_t InitDataStorage(); int32_t StopDataStorage(); int32_t StoreSnapshotInfo(const std::string& deviceId, int32_t missionId, const uint8_t* byteStream, size_t len); int32_t RemoveSnapshotInfo(const std::string& deviceId, int32_t missionId); int32_t GetRemoteMissionSnapshotInfo(const std::string& networkId, int32_t missionId, std::unique_ptr& missionSnapshot); void DeviceOnlineNotify(const std::string& deviceId); void DeviceOfflineNotify(const std::string& deviceId); void DeleteDataStorage(const std::string& deviceId, bool isDelayed); int32_t RegisterMissionListener(const std::u16string& devId, const sptr& obj); int32_t UnRegisterMissionListener(const std::u16string& devId, const sptr& obj); int32_t StartSyncRemoteMissions(const std::string& devId, bool fixConflict, int64_t tag); int32_t StartSyncMissionsFromRemote(const CallerInfo& callerInfo, std::vector& missionInfoSet); int32_t StopSyncRemoteMissions(const std::string& dstDevId, bool offline, bool exit = false); void StopSyncMissionsFromRemote(const std::string& deviceId); bool NeedSyncDevice(const std::string& deviceId); void NotifySnapshotChanged(const std::string& networkId, int32_t missionId); void OnRemoteDied(const wptr& remote); void EnqueueCachedSnapshotInfo(const std::string& deviceId, int32_t missionId, std::unique_ptr snapshot); std::unique_ptr DequeueCachedSnapshotInfo(const std::string& deviceId, int32_t missionId); int32_t NotifyMissionsChangedToRemote(const std::vector& missionInfoSet); int32_t NotifyMissionsChangedFromRemote(const CallerInfo& callerInfo, const std::vector& missionInfoSet); void OnRemoteDmsDied(const wptr& remote); void NotifyDmsProxyProcessDied(); void OnDnetDied(); void NotifyLocalMissionsChanged(); void NotifyMissionSnapshotCreated(int32_t missionId); void NotifyMissionSnapshotChanged(int32_t missionId); void NotifyMissionSnapshotDestroyed(int32_t missionId); void NotifyRemoteDied(const wptr& remote); private: std::map> deviceHandle_; mutable std::mutex remoteMissionInfosLock_; std::map> deviceMissionInfos_; sptr GetRemoteDms(const std::string& deviceId); bool IsDeviceIdValidated(const std::string& deviceId); std::shared_ptr FetchDeviceHandler(const std::string& deviceId); bool GenerateCallerInfo(CallerInfo& callerInfo); void NotifyMissionsChangedToRemoteInner(const std::string& remoteUuid, const std::vector& missionInfoSet, const CallerInfo& callerInfo); std::string GenerateKeyInfo(const std::string& devId, int32_t missionId) { return devId + "_" + std::to_string(missionId); } int32_t StartSyncRemoteMissions(const std::string& dstDevId, const std::string& localDevId); int32_t StartSyncRemoteMissions(const std::string& dstDevId, const sptr& remoteDms); void CleanMissionResources(const std::string& dstDevId); void RetryStartSyncRemoteMissions(const std::string& dstDeviceId, const std::string& localDevId, int32_t retryTimes); bool HasSyncListener(const std::string& networkId); void DeleteCachedSnapshotInfo(const std::string& networkId); int32_t FetchCachedRemoteMissions(const std::string& srcId, int32_t numMissions, std::vector& missionInfoSet); void RebornMissionCache(const std::string& deviceId, const std::vector& missionInfoSet); void CleanMissionCache(const std::string& deviceId); void OnMissionListenerDied(const sptr& remote); void OnRemoteDmsDied(const sptr& remote); void RetryRegisterMissionChange(int32_t retryTimes); void InitAllSnapshots(const std::vector& missionInfoSet); int32_t MissionSnapshotChanged(int32_t missionId); int32_t MissionSnapshotDestroyed(int32_t missionId); int32_t MissionSnapshotSequence(const Snapshot& snapshot, MessageParcel& data); class ListenerDeathRecipient : public IRemoteObject::DeathRecipient { public: void OnRemoteDied(const wptr& remote) override; }; sptr listenerDeath_; std::set remoteSyncDeviceSet_; std::mutex remoteSyncDeviceLock_; std::map> cachedSnapshotInfos_; std::map listenDeviceMap_; std::mutex listenDeviceLock_; std::shared_ptr distributedDataStorage_; std::set allowMissionUids_; std::mutex allowMissionUidsLock_; std::atomic isRegMissionChange_ = false; sptr missonChangeListener_; std::shared_ptr missionChangeHandler_; class RemoteDmsDeathRecipient : public IRemoteObject::DeathRecipient { public: void OnRemoteDied(const wptr& remote) override; }; sptr remoteDmsRecipient_; std::map> remoteDmsMap_; std::mutex remoteDmsLock_; std::shared_ptr missionHandler_; std::shared_ptr updateHandler_; }; } // namespace DistributedSchedule } // namespace OHOS #endif // DISTRIBUTEDSCHED_MISSION_MANAGER_H