* Copyright (c) 2021-2025 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "network/session_pool.h"
#include "dm_device_info.h"
#include "device/device_manager_agent.h"
#include "dfs_error.h"
#include "ipc/i_daemon.h"
namespace OHOS {
namespace Storage {
namespace DistributedFile {
using namespace std;
void SessionPool::HoldSession(shared_ptr<BaseSession> session, const std::string backStage)
{
if (talker_ == nullptr) {
LOGE("talker_ is nullptr.");
return;
}
lock_guard lock(sessionPoolLock_);
talker_->SinkSessionTokernel(session, backStage);
AddSessionToPool(session);
}
void SessionPool::ReleaseSession(const int32_t fd)
{
lock_guard lock(sessionPoolLock_);
LOGI("ReleaseSession start, fd=%{public}d", fd);
for (auto iter = usrSpaceSessionPool_.begin(); iter != usrSpaceSessionPool_.end(); ++iter) {
if ((*iter) != nullptr && (*iter)->GetHandle() == fd) {
(*iter)->Release();
iter = usrSpaceSessionPool_.erase(iter);
break;
}
}
}
bool SessionPool::CheckIfGetSession(const int32_t fd, bool &isServer)
{
lock_guard lock(sessionPoolLock_);
std::shared_ptr<BaseSession> session = nullptr;
for (auto iter = usrSpaceSessionPool_.begin(); iter != usrSpaceSessionPool_.end(); ++iter) {
if ((*iter) != nullptr && (*iter)->GetHandle() == fd) {
session = *iter;
break;
}
}
if (session == nullptr) {
isServer = false;
return false;
}
isServer = session->IsFromServer();
return !session->IsFromServer();
}
void SessionPool::SinkOffline(const std::string &cid)
{
lock_guard lock(sessionPoolLock_);
if (!FindCid(cid) && talker_ != nullptr) {
talker_->SinkOfflineCmdToKernel(cid);
}
}
bool SessionPool::FindSocketId(int32_t socketId)
{
lock_guard lock(sessionPoolLock_);
for (auto iter = usrSpaceSessionPool_.begin(); iter != usrSpaceSessionPool_.end(); ++iter) {
if ((*iter) != nullptr && (*iter)->GetSessionId() == socketId) {
return true;
}
}
return false;
}
void SessionPool::ReleaseSession(const std::string &cid, bool isReleaseAll)
{
lock_guard lock(sessionPoolLock_);
std::vector<std::shared_ptr<BaseSession>> sessions;
LOGI("ReleaseSession, cid:%{public}s", Utils::GetAnonyString(cid).c_str());
for (auto iter = usrSpaceSessionPool_.begin(); iter != usrSpaceSessionPool_.end();) {
if ((*iter) == nullptr) {
LOGE("Value is nullptr");
++iter;
continue;
}
if (((*iter)->GetCid() != cid || ((*iter)->IsFromServer() && !isReleaseAll))) {
++iter;
continue;
}
sessions.push_back(*iter);
iter = usrSpaceSessionPool_.erase(iter);
}
for (const auto &session : sessions) {
session->Release();
}
if (talker_ == nullptr) {
LOGE("talker_ is nullptr.");
return;
}
if (!FindCid(cid)) {
talker_->SinkOfflineCmdToKernel(cid);
}
}
bool SessionPool::FindCid(const std::string &cid)
{
lock_guard lock(sessionPoolLock_);
for (auto iter = usrSpaceSessionPool_.begin(); iter != usrSpaceSessionPool_.end(); ++iter) {
if ((*iter) != nullptr && (*iter)->GetCid() == cid) {
return true;
}
}
return false;
}
void SessionPool::ReleaseAllSession()
{
if (talker_ == nullptr) {
LOGE("talker_ is nullptr.");
return;
}
lock_guard lock(sessionPoolLock_);
for (auto iter = usrSpaceSessionPool_.begin(); iter != usrSpaceSessionPool_.end();) {
if (*iter != nullptr) {
talker_->SinkOfflineCmdToKernel((*iter)->GetCid());
}
iter = usrSpaceSessionPool_.erase(iter);
}
}
void SessionPool::AddSessionToPool(shared_ptr<BaseSession> session)
{
lock_guard lock(sessionPoolLock_);
usrSpaceSessionPool_.push_back(session);
}
}
}
}