1 /*
2 * Copyright (C) 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 "distributed_send_adapter.h"
17
18 #include "ans_inner_errors.h"
19 #include "softbus_error_code.h"
20 #include "distributed_data_define.h"
21 namespace OHOS {
22 namespace Notification {
23
24 const int32_t DEFAULT_RETRY_TIME = 1;
25
PackageInfo(const std::shared_ptr<BoxBase> & box,DistributedDeviceInfo deviceInfo,TransDataType dataType,int32_t eventType)26 PackageInfo::PackageInfo(const std::shared_ptr<BoxBase>& box, DistributedDeviceInfo deviceInfo,
27 TransDataType dataType, int32_t eventType)
28 {
29 boxInfo_ = box;
30 deviceInfo_ = deviceInfo;
31 dataType_ = dataType;
32 eventType_ = eventType;
33 if (boxInfo_ != nullptr && boxInfo_->box_ != nullptr) {
34 boxInfo_->box_->GetMessageType(messageType_);
35 }
36 }
37
GetInstance()38 DistributedSendAdapter& DistributedSendAdapter::GetInstance()
39 {
40 static DistributedSendAdapter distributedSendAdapter;
41 return distributedSendAdapter;
42 }
43
DoSendPackage(const std::shared_ptr<PackageInfo> & packageInfo)44 void DistributedSendAdapter::DoSendPackage(const std::shared_ptr<PackageInfo>& packageInfo)
45 {
46 int32_t result = DistributedClient::GetInstance().SendMessage(packageInfo->boxInfo_,
47 packageInfo->dataType_, packageInfo->deviceInfo_.deviceId_, packageInfo->eventType_);
48 ANS_LOGI("Dans adapter send %{public}s %{public}u %{public}d %{public}d %{public}d.",
49 StringAnonymous(packageInfo->deviceInfo_.deviceId_).c_str(), packageInfo->deviceInfo_.deviceType_,
50 packageInfo->messageType_, packageInfo->dataType_, result);
51
52 std::shared_ptr<PackageInfo> nextPackageInfo;
53 std::lock_guard<ffrt::mutex> lock(lock_);
54 if (packageCached_.empty()) {
55 isRunning.store(false);
56 ANS_LOGI("Dans submit end.");
57 return;
58 }
59 nextPackageInfo = packageCached_.front();
60 packageCached_.pop_front();
61 std::function<void()> sendTask = std::bind([nextPackageInfo]() {
62 DistributedSendAdapter::GetInstance().DoSendPackage(nextPackageInfo);
63 });
64 ffrt::submit(sendTask);
65 }
66
SendPackage(const std::shared_ptr<PackageInfo> & packageInfo)67 void DistributedSendAdapter::SendPackage(const std::shared_ptr<PackageInfo>& packageInfo)
68 {
69 if (packageInfo == nullptr) {
70 return;
71 }
72 ANS_LOGI("Dans adapter add %{public}s %{public}u %{public}d %{public}d.",
73 StringAnonymous(packageInfo->deviceInfo_.deviceId_).c_str(), packageInfo->deviceInfo_.deviceType_,
74 packageInfo->messageType_, packageInfo->dataType_);
75 std::lock_guard<ffrt::mutex> lock(lock_);
76 if (isRunning.load() || !packageCached_.empty()) {
77 packageCached_.push_back(packageInfo);
78 return;
79 }
80
81 ANS_LOGI("Dans submit start.");
82 isRunning.store(true);
83 std::function<void()> sendTask = std::bind([packageInfo]() {
84 DistributedSendAdapter::GetInstance().DoSendPackage(packageInfo);
85 });
86 ffrt::submit(sendTask);
87 }
88
89 }
90 }
91