• 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 #include "online_sync_table.h"
17 
18 #include "device_manager.h"
19 #include "device_profile_errors.h"
20 #include "device_profile_log.h"
21 #include "device_profile_utils.h"
22 #include "sync_coordinator.h"
23 
24 namespace OHOS {
25 namespace DeviceProfile {
26 using namespace OHOS::DistributedKv;
27 
28 namespace {
29 const std::string TAG = "OnlineSyncTable";
30 
31 const std::string APP_ID = "distributed_device_profile_service";
32 const std::string STORE_ID = "online_sync_storage";
33 constexpr int32_t MAX_RETRY_SYNC_TIMES = 30;
34 constexpr int32_t INTERVAL_RETRY_SYNC_MS = 100;
35 }
36 
OnlineSyncTable()37 OnlineSyncTable::OnlineSyncTable() : DeviceProfileStorage(APP_ID, STORE_ID)
38 {
39 }
40 
Init()41 void OnlineSyncTable::Init()
42 {
43     HILOGD("called");
44     Options options = {
45         .createIfMissing = true,
46         .encrypt = false,
47         .autoSync = false,
48         .kvStoreType = KvStoreType::SINGLE_VERSION,
49     };
50     SetOptions(options);
51     DeviceProfileStorage::Init();
52     int32_t errCode = DeviceProfileStorage::RegisterSyncCallback(shared_from_this());
53     if (errCode != ERR_OK) {
54         HILOGE("register sync callback failed, errCode = %{public}d", errCode);
55     }
56 }
57 
RegisterSyncCallback(const std::shared_ptr<KvStoreSyncCallback> & syncCb)58 int32_t OnlineSyncTable::RegisterSyncCallback(const std::shared_ptr<KvStoreSyncCallback>& syncCb)
59 {
60     HILOGI("called");
61     if (syncCb == nullptr) {
62         return ERR_DP_INVALID_PARAMS;
63     }
64     std::lock_guard<std::mutex> autoLock(tableLock_);
65     syncCallback_ = syncCb;
66     return ERR_OK;
67 }
68 
UnRegisterSyncCallback()69 int32_t  OnlineSyncTable::UnRegisterSyncCallback()
70 {
71     HILOGI("called");
72     std::lock_guard<std::mutex> autoLock(tableLock_);
73     syncCallback_ = nullptr;
74     return ERR_OK;
75 }
76 
SyncDeviceProfile(const std::vector<std::string> & deviceIds,SyncMode syncMode)77 int32_t OnlineSyncTable::SyncDeviceProfile(const std::vector<std::string>& deviceIds,
78     SyncMode syncMode)
79 {
80     HILOGI("called");
81     auto syncTask = [this, deviceIds = std::move(deviceIds), syncMode]() {
82         HILOGI("start sync task");
83         retrySyncTimes_ = 0;
84         int32_t errCode = DeviceProfileStorage::SyncDeviceProfile(deviceIds, syncMode);
85         if (errCode != ERR_OK) {
86             SyncCoordinator::GetInstance().ReleaseSync();
87             HILOGE("sync errCode = %{public}d", errCode);
88         }
89     };
90     if (!SyncCoordinator::GetInstance().DispatchSyncTask(syncTask)) {
91         HILOGE("post online sync task failed");
92         return ERR_DP_POST_TASK_FAILED;
93     }
94     return ERR_OK;
95 }
96 
SyncCompleted(const std::map<std::string,Status> & results)97 void OnlineSyncTable::SyncCompleted(const std::map<std::string, Status>& results)
98 {
99     if (!SyncCoordinator::GetInstance().IsOnlineSync()) {
100         HILOGI("manual sync callback");
101         NotifySyncCompleted(results);
102         return;
103     }
104 
105     HILOGI("online sync callback");
106     std::vector<std::string> failedDeviceIds;
107     for (const auto& [deviceId, result] : results) {
108         HILOGI("deviceId = %{public}s, result = %{public}d",
109             DeviceProfileUtils::AnonymizeDeviceId(deviceId).c_str(), result);
110         if (result != Status::SUCCESS) {
111             std::string networkId;
112             if (!DeviceManager::GetInstance().TransformDeviceId(deviceId, networkId,
113                 DeviceIdType::NETWORKID)) {
114                 HILOGE("transform to networkid failed");
115                 continue;
116             }
117             failedDeviceIds.emplace_back(std::move(networkId));
118         }
119     }
120 
121     HILOGI("retry times = %{public}d", retrySyncTimes_.load());
122     if ((retrySyncTimes_++ < MAX_RETRY_SYNC_TIMES) && !failedDeviceIds.empty()) {
123         auto retrySyncTask = [this, deviceIds = std::move(failedDeviceIds)]() {
124             HILOGI("retrying sync...");
125             DeviceProfileStorage::SyncDeviceProfile(deviceIds, SyncMode::PUSH);
126         };
127         if (SyncCoordinator::GetInstance().DispatchSyncTask(retrySyncTask,
128             INTERVAL_RETRY_SYNC_MS)) {
129             return;
130         } else {
131             HILOGE("post online sync retry task failed");
132         }
133     }
134     // notify and release when there is no retry
135     NotifySyncCompleted(results);
136     SyncCoordinator::GetInstance().ReleaseSync();
137 }
138 
NotifySyncCompleted(const std::map<std::string,Status> & results)139 void OnlineSyncTable::NotifySyncCompleted(const std::map<std::string, Status>& results)
140 {
141     std::lock_guard<std::mutex> autoLock(tableLock_);
142     if (syncCallback_ != nullptr) {
143         HILOGI("notify sync callback");
144         syncCallback_->SyncCompleted(results);
145     }
146 }
147 } // namespace DeviceProfile
148 } // namespace OHOS