1 /*
2 * Copyright (c) 2025 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 #define LOG_TAG "SwitchObserverBridge"
17
18 #include "kvdb_service_client.h"
19 #include "kvstore_service_death_notifier.h"
20 #include "log_print.h"
21 #include "switch_observer_bridge.h"
22
23 namespace OHOS::DistributedKv {
24 static constexpr int32_t INTERVAL = 500; // ms
SwitchObserverBridge(const AppId & appId)25 SwitchObserverBridge::SwitchObserverBridge(const AppId &appId)
26 {
27 switchAppId_ = appId;
28 }
29
AddSwitchCallback(std::shared_ptr<KvStoreObserver> observer)30 void SwitchObserverBridge::AddSwitchCallback(std::shared_ptr<KvStoreObserver> observer)
31 {
32 if (observer == nullptr) {
33 return;
34 }
35 switchObservers_.InsertOrAssign(uintptr_t(observer.get()), observer);
36 }
37
DeleteSwitchCallback(std::shared_ptr<KvStoreObserver> observer)38 void SwitchObserverBridge::DeleteSwitchCallback(std::shared_ptr<KvStoreObserver> observer)
39 {
40 if (observer == nullptr) {
41 return;
42 }
43 switchObservers_.Erase(uintptr_t(observer.get()));
44 }
45
OnRemoteDied()46 void SwitchObserverBridge::OnRemoteDied()
47 {
48 std::lock_guard<decltype(switchMutex_)> lock(switchMutex_);
49 if (!switchAppId_.IsValid() || switchObservers_.Empty() || taskId_ != ExecutorPool::INVALID_TASK_ID) {
50 ZLOGI("appId is :%{public}s, observers size is %{public}zu", switchAppId_.appId.c_str(),
51 switchObservers_.Size());
52 return;
53 }
54 RestartRegisterTimer();
55 }
56
RegisterSwitchObserver()57 void SwitchObserverBridge::RegisterSwitchObserver()
58 {
59 std::lock_guard<decltype(switchMutex_)> lock(switchMutex_);
60 auto service = KVDBServiceClient::GetInstance();
61 if (service == nullptr) {
62 RestartRegisterTimer();
63 return;
64 }
65 auto serviceAgent = service->GetServiceAgent(switchAppId_);
66 if (serviceAgent == nullptr) {
67 RestartRegisterTimer();
68 return;
69 }
70 auto status = service->SubscribeSwitchData(switchAppId_);
71 if (status != SUCCESS) {
72 RestartRegisterTimer();
73 return;
74 }
75 registerRetryCount_ = 0;
76 taskId_ = ExecutorPool::INVALID_TASK_ID;
77 switchObservers_.ForEach([&](auto &, auto &switchObserver) {
78 if (switchObserver != nullptr) {
79 serviceAgent->AddSwitchCallback(switchAppId_, switchObserver);
80 return true;
81 }
82 return false;
83 });
84 }
85
RestartRegisterTimer()86 void SwitchObserverBridge::RestartRegisterTimer()
87 {
88 registerRetryCount_ ++;
89 ZLOGI("restart register timer, appId is :%{public}s, observers size is %{public}zu, retry count_ is %{public}d",
90 switchAppId_.appId.c_str(), switchObservers_.Size(), registerRetryCount_.load());
91 taskId_ = TaskExecutor::GetInstance().Schedule(std::chrono::milliseconds(INTERVAL), [this]() {
92 RegisterSwitchObserver();
93 });
94 }
95 } // namespace OHOS::DistributedKv