!831 geo 服务断开后,conn_置空,移除监听

Merge pull request !831 from smilebear/master
This commit is contained in:
openharmony_ci 2024-08-20 06:28:45 +00:00 committed by Gitee
commit 8d77dc7c7e
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
3 changed files with 51 additions and 41 deletions

View File

@ -42,11 +42,11 @@ public:
void ProcessEvent(const AppExecFwk::InnerEvent::Pointer& event) override;
};
class NLPServiceDeathRecipient : public IRemoteObject::DeathRecipient {
class NlpServiceDeathRecipient : public IRemoteObject::DeathRecipient {
public:
void OnRemoteDied(const wptr<IRemoteObject> &remote) override;
NLPServiceDeathRecipient();
~NLPServiceDeathRecipient() override;
NlpServiceDeathRecipient();
~NlpServiceDeathRecipient() override;
};
class NetworkAbility : public SystemAbility, public NetworkAbilityStub, public SubAbility {
@ -90,18 +90,19 @@ private:
bool CheckIfNetworkConnecting();
bool RequestNetworkLocation(WorkRecord &workRecord);
bool RemoveNetworkLocation(WorkRecord &workRecord);
void RegisterNLPServiceDeathRecipient();
void UnRegisterNLPServiceDeathRecipient();
void RegisterNlpServiceDeathRecipient();
void UnregisterNlpServiceDeathRecipient();
bool IsConnect();
ffrt::mutex mutex_;
ffrt::mutex nlpServiceMutex_;
ffrt::mutex connMutex_;
sptr<IRemoteObject> nlpServiceProxy_;
ffrt::condition_variable connectCondition_;
std::shared_ptr<NetworkHandler> networkHandler_;
size_t mockLocationIndex_ = 0;
bool registerToAbility_ = false;
sptr<IRemoteObject::DeathRecipient> nlpServiceRecipient_ = sptr<NLPServiceDeathRecipient>(new
(std::nothrow) NLPServiceDeathRecipient());
sptr<IRemoteObject::DeathRecipient> nlpServiceRecipient_ = sptr<NlpServiceDeathRecipient>(new
(std::nothrow) NlpServiceDeathRecipient());
ServiceRunningState state_ = ServiceRunningState::STATE_NOT_START;
sptr<AAFwk::IAbilityConnection> conn_;
};

View File

@ -59,6 +59,7 @@ NetworkAbility::NetworkAbility() : SystemAbility(LOCATION_NETWORK_LOCATING_SA_ID
NetworkAbility::~NetworkAbility()
{
std::unique_lock<ffrt::mutex> uniqueLock(connMutex_);
conn_ = nullptr;
}
@ -141,17 +142,21 @@ bool NetworkAbility::ConnectNlpService()
return false;
}
connectionWant.SetElementName(serviceName, abilityName);
std::unique_lock<ffrt::mutex> lock(connMutex_, std::defer_lock);
connMutex_.lock();
conn_ = sptr<AAFwk::IAbilityConnection>(new (std::nothrow) AbilityConnection());
if (conn_ == nullptr) {
LBSLOGE(NETWORK, "get connection failed!");
connMutex_.unlock();
return false;
}
int32_t ret = AAFwk::AbilityManagerClient::GetInstance()->ConnectAbility(connectionWant, conn_, -1);
connMutex_.unlock();
if (ret != ERR_OK) {
LBSLOGE(NETWORK, "Connect cloud service failed, ret = %{public}d", ret);
return false;
}
std::unique_lock<ffrt::mutex> uniqueLock(mutex_);
std::unique_lock<ffrt::mutex> uniqueLock(nlpServiceMutex_);
auto waitStatus = connectCondition_.wait_for(
uniqueLock, std::chrono::seconds(CONNECT_TIME_OUT), [this]() { return nlpServiceProxy_ != nullptr; });
if (!waitStatus) {
@ -160,7 +165,7 @@ bool NetworkAbility::ConnectNlpService()
return false;
}
}
RegisterNLPServiceDeathRecipient();
RegisterNlpServiceDeathRecipient();
return true;
}
@ -185,21 +190,21 @@ bool NetworkAbility::ReConnectNlpService()
bool NetworkAbility::ResetServiceProxy()
{
std::unique_lock<ffrt::mutex> uniqueLock(mutex_);
std::unique_lock<ffrt::mutex> uniqueLock(nlpServiceMutex_);
nlpServiceProxy_ = nullptr;
return true;
}
void NetworkAbility::NotifyConnected(const sptr<IRemoteObject>& remoteObject)
{
std::unique_lock<ffrt::mutex> uniqueLock(mutex_);
std::unique_lock<ffrt::mutex> uniqueLock(nlpServiceMutex_);
nlpServiceProxy_ = remoteObject;
connectCondition_.notify_all();
}
void NetworkAbility::NotifyDisConnected()
{
std::unique_lock<ffrt::mutex> uniqueLock(mutex_);
std::unique_lock<ffrt::mutex> uniqueLock(nlpServiceMutex_);
nlpServiceProxy_ = nullptr;
connectCondition_.notify_all();
}
@ -273,26 +278,22 @@ void NetworkAbility::RequestRecord(WorkRecord &workRecord, bool isAdded)
return;
}
}
std::unique_lock<ffrt::mutex> uniqueLock(mutex_);
if (isAdded) {
RequestNetworkLocation(workRecord);
} else {
RemoveNetworkLocation(workRecord);
std::unique_lock<ffrt::mutex> uniqueLock(connMutex_);
if (GetRequestNum() == 0 && conn_ != nullptr) {
LBSLOGI(NETWORK, "RequestRecord disconnect");
AAFwk::AbilityManagerClient::GetInstance()->DisconnectAbility(conn_);
UnRegisterNLPServiceDeathRecipient();
conn_ = nullptr ;
UnregisterNlpServiceDeathRecipient();
conn_ = nullptr;
}
}
}
bool NetworkAbility::RequestNetworkLocation(WorkRecord &workRecord)
{
if (nlpServiceProxy_ == nullptr) {
LBSLOGE(NETWORK, "nlpProxy is nullptr.");
return false;
}
if (LocationDataRdbManager::QuerySwitchState() != ENABLED) {
LBSLOGE(NETWORK, "QuerySwitchState is DISABLED");
return false;
@ -303,7 +304,13 @@ bool NetworkAbility::RequestNetworkLocation(WorkRecord &workRecord)
LBSLOGE(NETWORK, "can not get valid callback.");
return false;
}
std::unique_lock<ffrt::mutex> uniqueLock(nlpServiceMutex_);
if (nlpServiceProxy_ == nullptr) {
LBSLOGE(NETWORK, "nlpProxy is nullptr.");
return false;
}
MessageParcel data;
MessageParcel reply;
MessageOption option;
data.WriteString16(Str8ToStr16(workRecord.GetUuid(0)));
@ -325,6 +332,7 @@ bool NetworkAbility::RequestNetworkLocation(WorkRecord &workRecord)
bool NetworkAbility::RemoveNetworkLocation(WorkRecord &workRecord)
{
std::unique_lock<ffrt::mutex> uniqueLock(nlpServiceMutex_);
if (nlpServiceProxy_ == nullptr) {
LBSLOGE(NETWORK, "nlpProxy is nullptr.");
return false;
@ -482,23 +490,24 @@ void NetworkAbility::SendMessage(uint32_t code, MessageParcel &data, MessageParc
}
}
void NetworkAbility::RegisterNLPServiceDeathRecipient()
void NetworkAbility::RegisterNlpServiceDeathRecipient()
{
std::unique_lock<ffrt::mutex> uniqueLock(mutex_);
std::unique_lock<ffrt::mutex> uniqueLock(nlpServiceMutex_);
if (nlpServiceProxy_ == nullptr) {
LBSLOGE(NETWORK, "%{public}s: nlpServiceProxy_ is nullptr", __func__);
return;
}
if (nlpServiceRecipient_ != nullptr) {
nlpServiceProxy_->AddDeathRecipient(nlpServiceRecipient_);
if (nlpServiceRecipient_ == nullptr) {
nlpServiceRecipient_ = sptr<NlpServiceDeathRecipient>(new (std::nothrow) NlpServiceDeathRecipient());
}
nlpServiceProxy_->AddDeathRecipient(nlpServiceRecipient_);
LBSLOGI(NETWORK, "%{public}s: success", __func__);
}
void NetworkAbility::UnRegisterNLPServiceDeathRecipient()
void NetworkAbility::UnregisterNlpServiceDeathRecipient()
{
std::unique_lock<ffrt::mutex> uniqueLock(nlpServiceMutex_);
LBSLOGI(NETWORK, "UnRegisterNLPServiceDeathRecipient enter");
std::unique_lock<ffrt::mutex> uniqueLock(mutex_);
if (nlpServiceProxy_ == nullptr) {
LBSLOGE(NETWORK, "%{public}s: nlpServiceProxy_ is nullptr", __func__);
return;
@ -512,7 +521,7 @@ void NetworkAbility::UnRegisterNLPServiceDeathRecipient()
bool NetworkAbility::IsConnect()
{
std::unique_lock<ffrt::mutex> uniqueLock(mutex_);
std::unique_lock<ffrt::mutex> uniqueLock(nlpServiceMutex_);
return nlpServiceProxy_ != nullptr;
}
@ -591,15 +600,15 @@ void NetworkHandler::ProcessEvent(const AppExecFwk::InnerEvent::Pointer& event)
networkAbility->UnloadNetworkSystemAbility();
}
NLPServiceDeathRecipient::NLPServiceDeathRecipient()
NlpServiceDeathRecipient::NlpServiceDeathRecipient()
{
}
NLPServiceDeathRecipient::~NLPServiceDeathRecipient()
NlpServiceDeathRecipient::~NlpServiceDeathRecipient()
{
}
void NLPServiceDeathRecipient::OnRemoteDied(const wptr<IRemoteObject> &remote)
void NlpServiceDeathRecipient::OnRemoteDied(const wptr<IRemoteObject> &remote)
{
auto networkAbility = NetworkAbility::GetInstance();
if (networkAbility == nullptr) {

View File

@ -601,13 +601,13 @@ HWTEST_F(NetworkAbilityTest, ReportMockedLocation001, TestSize.Level1)
}
#endif
HWTEST_F(NetworkAbilityTest, RegisterNLPServiceDeathRecipient001, TestSize.Level1)
HWTEST_F(NetworkAbilityTest, RegisterNlpServiceDeathRecipient001, TestSize.Level1)
{
GTEST_LOG_(INFO)
<< "NetworkAbilityTest, RegisterNLPServiceDeathRecipient001, TestSize.Level1";
LBSLOGI(NETWORK, "[NetworkAbilityTest] RegisterNLPServiceDeathRecipient001 begin");
ability_->RegisterNLPServiceDeathRecipient();
LBSLOGI(NETWORK, "[NetworkAbilityTest] RegisterNLPServiceDeathRecipient001 end");
<< "NetworkAbilityTest, RegisterNlpServiceDeathRecipient001, TestSize.Level1";
LBSLOGI(NETWORK, "[NetworkAbilityTest] RegisterNlpServiceDeathRecipient001 begin");
ability_->RegisterNlpServiceDeathRecipient();
LBSLOGI(NETWORK, "[NetworkAbilityTest] RegisterNlpServiceDeathRecipient001 end");
}
HWTEST_F(NetworkAbilityTest, ReportLocationError001, TestSize.Level1)
@ -634,7 +634,7 @@ HWTEST_F(NetworkAbilityTest, OnRemoteDied001, TestSize.Level1)
GTEST_LOG_(INFO)
<< "NetworkAbilityTest, OnRemoteDied001, TestSize.Level1";
LBSLOGI(NETWORK, "[NetworkAbilityTest] OnRemoteDied001 begin");
auto deathRecipient = new (std::nothrow) NLPServiceDeathRecipient();
auto deathRecipient = new (std::nothrow) NlpServiceDeathRecipient();
const wptr<IRemoteObject> object;
deathRecipient->OnRemoteDied(object);
LBSLOGI(NETWORK, "[NetworkAbilityTest] OnRemoteDied001 end");
@ -727,17 +727,17 @@ HWTEST_F(NetworkAbilityTest, RemoveNetworkLocation002, TestSize.Level1)
LBSLOGI(NETWORK, "[NetworkAbilityTest] RemoveNetworkLocation002 end");
}
HWTEST_F(NetworkAbilityTest, RegisterNLPServiceDeathRecipient002, TestSize.Level1)
HWTEST_F(NetworkAbilityTest, RegisterNlpServiceDeathRecipient002, TestSize.Level1)
{
GTEST_LOG_(INFO)
<< "NetworkAbilityTest, RegisterNLPServiceDeathRecipient002, TestSize.Level1";
LBSLOGI(NETWORK, "[NetworkAbilityTest] RegisterNLPServiceDeathRecipient002 begin");
<< "NetworkAbilityTest, RegisterNlpServiceDeathRecipient002, TestSize.Level1";
LBSLOGI(NETWORK, "[NetworkAbilityTest] RegisterNlpServiceDeathRecipient002 begin");
sptr<MockIRemoteObject> nlpServiceProxy =
sptr<MockIRemoteObject>(new (std::nothrow) MockIRemoteObject());
EXPECT_NE(nullptr, nlpServiceProxy);
ability_->nlpServiceProxy_ = nlpServiceProxy;
ability_->RegisterNLPServiceDeathRecipient();
LBSLOGI(NETWORK, "[NetworkAbilityTest] RegisterNLPServiceDeathRecipient002 end");
ability_->RegisterNlpServiceDeathRecipient();
LBSLOGI(NETWORK, "[NetworkAbilityTest] RegisterNlpServiceDeathRecipient002 end");
}
} // namespace Location
} // namespace OHOS