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 #include "notifier_impl.h"
17
18 #include "anonymous.h"
19 #include "logger.h"
20 #include "objectstore_errors.h"
21
22 namespace OHOS::ObjectStore {
GetInstance()23 std::shared_ptr<NotifierImpl> NotifierImpl::GetInstance()
24 {
25 static std::shared_ptr<NotifierImpl> instance;
26 static std::mutex instanceLock;
27 if (instance == nullptr) {
28 std::lock_guard<std::mutex> lockGuard(instanceLock);
29 if (instance == nullptr) {
30 instance = std::make_shared<NotifierImpl>();
31 DistributedObjectStore *storeInstance = DistributedObjectStore::GetInstance();
32 if (storeInstance == nullptr) {
33 LOG_ERROR("Get store instance nullptr");
34 return instance;
35 }
36 auto ret = storeInstance->SetStatusNotifier(instance);
37 if (ret != SUCCESS) {
38 LOG_ERROR("SetStatusNotifier %{public}d error", ret);
39 } else {
40 LOG_INFO("SetStatusNotifier success");
41 }
42 }
43 }
44 return instance;
45 }
46
AddWatcher(std::string & sessionId,std::weak_ptr<JSWatcher> watcher)47 void NotifierImpl::AddWatcher(std::string &sessionId, std::weak_ptr<JSWatcher> watcher)
48 {
49 std::lock_guard<std::mutex> lock(mutex_);
50 watchers_.insert_or_assign(sessionId, watcher);
51 }
52
DelWatcher(std::string & sessionId)53 void NotifierImpl::DelWatcher(std::string &sessionId)
54 {
55 std::lock_guard<std::mutex> lock(mutex_);
56 watchers_.erase(sessionId);
57 }
58
OnChanged(const std::string & sessionId,const std::string & networkId,const std::string & onlineStatus)59 void NotifierImpl::OnChanged(
60 const std::string &sessionId, const std::string &networkId, const std::string &onlineStatus)
61 {
62 LOG_INFO("status changed %{public}s %{public}s %{public}s", sessionId.c_str(), Anonymous::Change(networkId).c_str(),
63 onlineStatus.c_str());
64 std::lock_guard<std::mutex> lock(mutex_);
65 if (watchers_.count(sessionId) != 0) {
66 LOG_INFO("start emit %{public}s %{public}s %{public}s", sessionId.c_str(), Anonymous::Change(networkId).c_str(),
67 onlineStatus.c_str());
68 std::shared_ptr<JSWatcher> lockedWatcher = watchers_.at(sessionId).lock();
69 if (lockedWatcher) {
70 lockedWatcher->Emit("status", sessionId, networkId, onlineStatus);
71 LOG_INFO("end emit %{public}s %{public}s %{public}s", sessionId.c_str(),
72 Anonymous::Change(networkId).c_str(), onlineStatus.c_str());
73 } else {
74 LOG_ERROR("watcher expired");
75 }
76 }
77 }
~NotifierImpl()78 NotifierImpl::~NotifierImpl()
79 {
80 }
81 } // namespace OHOS::ObjectStore
82