1 /*
2  * Copyright (c) 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 DSCHED_SYNC_E2E_H
17 #define DSCHED_SYNC_E2E_H
18 
19 #include "distributed_sched_utils.h"
20 #include "dtbschedmgr_device_info_storage.h"
21 #include "mission/distributed_bm_storage.h"
22 
23 namespace OHOS {
24 namespace DistributedSchedule {
25 using namespace AppExecFwk;
26 using namespace DistributedKv;
27 
28 class DmsKvSyncCB : public OHOS::DistributedKv::KvStoreSyncCallback {
29 public:
30     DmsKvSyncCB();
31     virtual ~DmsKvSyncCB();
32     void SyncCompleted(const std::map<std::string, DistributedKv::Status> &result) override;
33 };
34 
35 class DmsKvSyncE2E {
36 public:
37     DmsKvSyncE2E();
38     ~DmsKvSyncE2E();
39     static std::shared_ptr<DmsKvSyncE2E> GetInstance();
40     bool PushAndPullData();
41     bool PushAndPullData(const std::string &networkId);
42     void SetDeviceCfg();
43     bool CheckDeviceCfg();
44     void SetSyncRecord(const std::string &networkId);
45     void ClearSyncRecord(const std::string &networkId);
46     bool IsSynchronized(const std::string &networkId);
47 
48 private:
49     void TryTwice(const std::function<DistributedKv::Status()> &func) const;
50     bool CheckKvStore();
51     DistributedKv::Status GetKvStore();
52 
53     static std::mutex mutex_;
54     static std::shared_ptr<DmsKvSyncE2E> instance_;
55     const DistributedKv::AppId appId_ {DMS_BM_APP_ID};
56     const DistributedKv::StoreId storeId_ {DISTRIBUTE_BM_STORE_ID};
57     DistributedKv::DistributedKvDataManager dataManager_;
58     std::shared_ptr<DistributedKv::SingleKvStore> kvStorePtr_;
59     mutable std::mutex kvStorePtrMutex_;
60     std::atomic<bool> isCfgDevices_ = false;
61     std::map<std::string, bool> deviceSyncRecord_;
62 };
63 }  // namespace DistributedSchedule
64 }  // namespace OHOS
65 #endif  // DSCHED_SYNC_E2E_H
66