1 /* 2 * Copyright (c) 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 OHOS_DISTRIBUTED_HARDWARE_VERSION_INFO_MANAGER_H 17 #define OHOS_DISTRIBUTED_HARDWARE_VERSION_INFO_MANAGER_H 18 19 #include <condition_variable> 20 #include <map> 21 #include <set> 22 23 #include "kvstore_observer.h" 24 25 #include "db_adapter.h" 26 #include "event.h" 27 #include "eventbus_handler.h" 28 #include "event_bus.h" 29 #include "event_sender.h" 30 #include "impl_utils.h" 31 #include "single_instance.h" 32 #include "version_info.h" 33 #include "version_info_event.h" 34 35 class DBAdapter; 36 namespace OHOS { 37 namespace DistributedHardware { 38 class VersionInfoManager : public std::enable_shared_from_this<VersionInfoManager>, 39 public EventSender, 40 public DistributedKv::KvStoreObserver, 41 public EventBusHandler<VersionInfoEvent> { 42 public: 43 VersionInfoManager(const VersionInfoManager &) = delete; 44 VersionInfoManager &operator = (const VersionInfoManager &) = delete; 45 VersionInfoManager(VersionInfoManager &&) = delete; 46 VersionInfoManager &operator = (VersionInfoManager &&) = delete; 47 static std::shared_ptr<VersionInfoManager> GetInstance(); 48 virtual ~VersionInfoManager(); 49 50 int32_t Init(); 51 int32_t UnInit(); 52 53 int32_t AddVersion(const VersionInfo &versionInfo); 54 int32_t GetVersionInfoByDeviceId(const std::string &deviceId, VersionInfo &versionInfo); 55 int32_t RemoveVersionInfoByDeviceId(const std::string &deviceId); 56 int32_t SyncVersionInfoFromDB(const std::string &deviceId); 57 int32_t SyncRemoteVersionInfos(); 58 59 void CreateManualSyncCount(const std::string &deviceId); 60 void RemoveManualSyncCount(const std::string &deviceId); 61 int32_t ManualSync(const std::string &networkId); 62 63 void OnChange(const DistributedKv::ChangeNotification &changeNotification) override; 64 void OnEvent(VersionInfoEvent &ev) override; 65 66 private: 67 VersionInfoManager(); 68 void UpdateVersionCache(const VersionInfo &versionInfo); 69 void HandleVersionAddChange(const std::vector<DistributedKv::Entry> &insertRecords); 70 void HandleVersionUpdateChange(const std::vector<DistributedKv::Entry> &updateRecords); 71 void HandleVersionDeleteChange(const std::vector<DistributedKv::Entry> &deleteRecords); 72 73 private: 74 mutable std::mutex verInfoMgrMutex_; 75 std::shared_ptr<DBAdapter> dbAdapterPtr_; 76 }; 77 } // namespace DistributedHardware 78 } // namespace OHOS 79 #endif 80