1 /*
2  * Copyright (c) 2023-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 #include "listener/kv_sync_completed_listener.h"
17 
18 #include <cinttypes>
19 
20 #include "datetime_ex.h"
21 #include "string_ex.h"
22 
23 #include "distributed_device_profile_log.h"
24 #include "profile_utils.h"
25 #include "profile_cache.h"
26 #include "i_sync_completed_callback.h"
27 #include "event_handler_factory.h"
28 
29 namespace OHOS {
30 namespace DistributedDeviceProfile {
31 namespace {
32     const std::string TAG = "KvSyncCompletedListener";
33     const std::string ON_SYNC_TASK_ID = "on_sync_task";
34 }
35 
KvSyncCompletedListener(const std::string & storeId)36 KvSyncCompletedListener::KvSyncCompletedListener(const std::string& storeId)
37 {
38     HILOGD("construct!");
39     storeId_ = storeId;
40     {
41         std::lock_guard<std::mutex> lock(reInitMutex_);
42         onSyncHandler_ = EventHandlerFactory::GetInstance().GetEventHandler();
43     }
44 }
45 
~KvSyncCompletedListener()46 KvSyncCompletedListener::~KvSyncCompletedListener()
47 {
48     HILOGD("destruct!");
49     {
50         std::lock_guard<std::mutex> lock(reInitMutex_);
51         if (onSyncHandler_ == nullptr) {
52             HILOGE("onSyncHandler is nullptr!");
53             return;
54         }
55         onSyncHandler_->RemoveTask(ON_SYNC_TASK_ID);
56         onSyncHandler_ = nullptr;
57     }
58 }
59 
SyncCompleted(const std::map<std::string,DistributedKv::Status> & results)60 void KvSyncCompletedListener::SyncCompleted(const std::map<std::string, DistributedKv::Status>& results)
61 {
62     HILOGD("called!");
63 
64     SyncResults syncResults;
65     for (const auto& [deviceId, status] : results) {
66         HILOGD("deviceId = %{public}s, status = %{public}d", ProfileUtils::GetAnonyString(deviceId).c_str(), status);
67         SyncStatus syncStatus = (status == DistributedKv::Status::SUCCESS) ? SyncStatus::SUCCEEDED : SyncStatus::FAILED;
68         syncResults.emplace(deviceId, syncStatus);
69     }
70     auto notifyTask = [this, syncResults = std::move(syncResults)]() {
71         NotifySyncCompleted(syncResults);
72     };
73     {
74         std::lock_guard<std::mutex> lock(reInitMutex_);
75         if (onSyncHandler_ == nullptr) {
76             HILOGE("Create EventHandler is nullptr");
77             return;
78         }
79         if (!onSyncHandler_->PostTask(notifyTask, ON_SYNC_TASK_ID, 0)) {
80             HILOGE("Post task fail!");
81             return;
82         }
83     }
84 }
85 
NotifySyncCompleted(const SyncResults & syncResults)86 void KvSyncCompletedListener::NotifySyncCompleted(const SyncResults& syncResults)
87 {
88     int64_t beginTime = GetTickCount();
89     std::map<std::string, sptr<IRemoteObject>> syncListeners;
90     ProfileCache::GetInstance().GetSyncListeners(syncListeners);
91     for (const auto& [_, syncListenerStub] : syncListeners) {
92         sptr<ISyncCompletedCallback> syncListenerProxy = iface_cast<ISyncCompletedCallback>(syncListenerStub);
93         if (syncListenerProxy == nullptr) {
94             HILOGE("Cast to ISyncCompletedCallback failed");
95             continue;
96         }
97         syncListenerProxy->OnSyncCompleted(syncResults);
98     }
99     ProfileCache::GetInstance().RemoveSyncListeners(syncListeners);
100     int64_t endTime = GetTickCount();
101     HILOGI("spend %{public}" PRId64 " ms", endTime - beginTime);
102 }
103 } // namespace DeviceProfile
104 } // namespace OHOS
105 
106