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