• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "dm_publish_manager.h"
17 #include "dm_anonymous.h"
18 #include "dm_constants.h"
19 #include "dm_log.h"
20 
21 namespace OHOS {
22 namespace DistributedHardware {
23 constexpr const char*  PUBLISH_DISCOVERY_TIMEOUT_TASK = "deviceManagerTimer:publish";
24 const int32_t PUBLISH_DISCOVERY_TIMEOUT = 120;
25 
DmPublishManager(std::shared_ptr<SoftbusConnector> softbusConnector,std::shared_ptr<IDeviceManagerServiceListener> listener)26 DmPublishManager::DmPublishManager(std::shared_ptr<SoftbusConnector> softbusConnector,
27                                    std::shared_ptr<IDeviceManagerServiceListener> listener)
28     : softbusConnector_(softbusConnector), listener_(listener)
29 {
30     LOGI("DmPublishManager constructor");
31 }
32 
~DmPublishManager()33 DmPublishManager::~DmPublishManager()
34 {
35     LOGI("DmPublishManager destructor");
36 }
37 
CfgPublishTimer()38 void DmPublishManager::CfgPublishTimer()
39 {
40     if (timer_ == nullptr) {
41         timer_ = std::make_shared<DmTimer>();
42     }
43     timer_->StartTimer(std::string(PUBLISH_DISCOVERY_TIMEOUT_TASK), PUBLISH_DISCOVERY_TIMEOUT,
44         [this] (std::string name) {
45             DmPublishManager::HandlePublishTimeout(name);
46         });
47 }
48 
CheckPublishQueue(const std::string & pkgName)49 int32_t DmPublishManager::CheckPublishQueue(const std::string &pkgName)
50 {
51     if (publishQueue_.empty()) {
52         return DM_OK;
53     }
54 
55     if (pkgName == publishQueue_.front()) {
56         LOGE("DmPublishManager::pkgName : %s PublishDeviceDiscovery repeated", pkgName.c_str());
57         return ERR_DM_PUBLISH_REPEATED;
58     } else {
59         LOGI("DmPublishManager::UnPublishDeviceDiscovery the preview pkgName : %s", publishQueue_.front().c_str());
60         UnPublishDeviceDiscovery(publishQueue_.front(),
61                                  publishContextMap_[publishQueue_.front()].publishInfo.publishId);
62         return DM_OK;
63     }
64 }
65 
PublishDeviceDiscovery(const std::string & pkgName,const DmPublishInfo & publishInfo)66 int32_t DmPublishManager::PublishDeviceDiscovery(const std::string &pkgName, const DmPublishInfo &publishInfo)
67 {
68     int32_t ret = CheckPublishQueue(pkgName);
69     if (ret != DM_OK) {
70         return ret;
71     }
72     std::lock_guard<std::mutex> autoLock(locks_);
73     publishQueue_.push(pkgName);
74     DmPublishContext context = {pkgName, publishInfo};
75     publishContextMap_.emplace(pkgName, context);
76     softbusConnector_->RegisterSoftbusPublishCallback(pkgName,
77                                                       std::shared_ptr<ISoftbusPublishCallback>(shared_from_this()));
78     CfgPublishTimer();
79     return softbusConnector_->PublishDiscovery(publishInfo);
80 }
81 
UnPublishDeviceDiscovery(const std::string & pkgName,int32_t publishId)82 int32_t DmPublishManager::UnPublishDeviceDiscovery(const std::string &pkgName, int32_t publishId)
83 {
84     if (pkgName.empty()) {
85         LOGE("Invalid parameter, pkgName is empty.");
86         return ERR_DM_INPUT_PARA_INVALID;
87     }
88     std::lock_guard<std::mutex> autoLock(locks_);
89     if (!publishQueue_.empty()) {
90         publishQueue_.pop();
91     }
92     if (!publishContextMap_.empty()) {
93         publishContextMap_.erase(pkgName);
94         softbusConnector_->UnRegisterSoftbusPublishCallback(pkgName);
95         timer_->DeleteTimer(std::string(PUBLISH_DISCOVERY_TIMEOUT_TASK));
96     }
97     return softbusConnector_->UnPublishDiscovery(publishId);
98 }
99 
OnPublishResult(const std::string & pkgName,int32_t publishId,int32_t publishResult)100 void DmPublishManager::OnPublishResult(const std::string &pkgName, int32_t publishId, int32_t publishResult)
101 {
102     if (pkgName.empty()) {
103         LOGE("Invalid parameter, pkgName is empty.");
104         return;
105     }
106     LOGI("DmPublishManager::OnPublishResult, publishId = %d, publishResult = %d", publishId, publishResult);
107     if (publishResult != 0) {
108         UnPublishDeviceDiscovery(pkgName, publishId);
109     }
110     listener_->OnPublishResult(pkgName, publishId, publishResult);
111 }
112 
HandlePublishTimeout(std::string name)113 void DmPublishManager::HandlePublishTimeout(std::string name)
114 {
115     (void)name;
116     LOGI("DmPublishManager::HandlePublishDiscoveryTimeout");
117     if (publishQueue_.empty()) {
118         LOGE("HandlePublishDiscoveryTimeout: Publish discovery queue is empty.");
119         return;
120     }
121 
122     std::string pkgName = publishQueue_.front();
123     auto iter = publishContextMap_.find(pkgName);
124     if (iter == publishContextMap_.end()) {
125         LOGE("HandleDiscoveryTimeout:pkgName %s fail to publish", GetAnonyString(pkgName).c_str());
126         return;
127     }
128     UnPublishDeviceDiscovery(pkgName, iter->second.publishInfo.publishId);
129 }
130 } // namespace DistributedHardware
131 } // namespace OHOS
132