1 /*
2 * Copyright (c) 2023 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 "CommunicatorContext"
17 #include "communicator_context.h"
18 #include "log_print.h"
19 #include "kvstore_utils.h"
20
21 namespace OHOS::DistributedData {
22 using KvUtils = OHOS::DistributedKv::KvStoreUtils;
23 using Status = OHOS::DistributedKv::Status;
24
GetInstance()25 CommunicatorContext &CommunicatorContext::GetInstance()
26 {
27 static CommunicatorContext context;
28 return context;
29 }
30
SetThreadPool(std::shared_ptr<ExecutorPool> executors)31 void CommunicatorContext::SetThreadPool(std::shared_ptr<ExecutorPool> executors)
32 {
33 executors_ = executors;
34 }
35
GetThreadPool()36 std::shared_ptr<ExecutorPool> CommunicatorContext::GetThreadPool()
37 {
38 return executors_;
39 }
40
RegSessionListener(const DevChangeListener * observer)41 Status CommunicatorContext::RegSessionListener(const DevChangeListener *observer)
42 {
43 if (observer == nullptr) {
44 ZLOGE("observer is nullptr");
45 return Status::INVALID_ARGUMENT;
46 }
47 std::lock_guard<decltype(mutex_)> lock(mutex_);
48 auto it = std::find(observers_.begin(), observers_.end(), observer);
49 if (it == observers_.end()) {
50 observers_.emplace_back(observer);
51 }
52 return Status::SUCCESS;
53 }
54
SetSessionListener(const OnCloseAble & closeAbleCallback)55 void CommunicatorContext::SetSessionListener(const OnCloseAble &closeAbleCallback)
56 {
57 std::lock_guard<decltype(sessionMutex_)> sessionLockGard(sessionMutex_);
58 closeListener_ = closeAbleCallback;
59 }
60
UnRegSessionListener(const DevChangeListener * observer)61 Status CommunicatorContext::UnRegSessionListener(const DevChangeListener *observer)
62 {
63 if (observer == nullptr) {
64 ZLOGE("observer is nullptr");
65 return Status::INVALID_ARGUMENT;
66 }
67 std::lock_guard<decltype(mutex_)> lock(mutex_);
68 auto it = std::find(observers_.begin(), observers_.end(), observer);
69 if (it != observers_.end()) {
70 observers_.erase(it);
71 }
72 return Status::SUCCESS;
73 }
74
NotifySessionReady(const std::string & deviceId)75 void CommunicatorContext::NotifySessionReady(const std::string &deviceId)
76 {
77 if (deviceId.empty()) {
78 ZLOGE("deviceId empty");
79 return;
80 }
81 devices_.Insert(deviceId, deviceId);
82 DeviceInfo devInfo;
83 devInfo.uuid = deviceId;
84 {
85 std::lock_guard<decltype(mutex_)> lock(mutex_);
86 for (const auto &observer : observers_) {
87 if (observer != nullptr) {
88 observer->OnSessionReady(devInfo);
89 }
90 }
91 ZLOGI("Notify session begin, deviceId:%{public}s, observer count:%{public}zu",
92 KvUtils::ToBeAnonymous(deviceId).c_str(), observers_.size());
93 }
94 std::lock_guard<decltype(sessionMutex_)> sessionLockGard(sessionMutex_);
95 if (closeListener_) {
96 closeListener_(deviceId);
97 }
98 }
99
NotifySessionClose(const std::string & deviceId)100 void CommunicatorContext::NotifySessionClose(const std::string &deviceId)
101 {
102 if (deviceId.empty()) {
103 ZLOGE("deviceId empty");
104 return;
105 }
106 devices_.Erase(deviceId);
107 }
108
IsSessionReady(const std::string & deviceId)109 bool CommunicatorContext::IsSessionReady(const std::string &deviceId)
110 {
111 if (deviceId.empty()) {
112 ZLOGE("deviceId empty");
113 return false;
114 }
115 return devices_.Contains(deviceId);
116 }
117 } // namespace OHOS::DistributedData