!100 将ConnectionManagerListener定义放入头文件

Merge pull request !100 from LongestDistance/master
This commit is contained in:
openharmony_ci 2024-11-10 11:40:43 +00:00 committed by Gitee
commit 77c535f0ef
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
2 changed files with 147 additions and 132 deletions

View File

@ -104,6 +104,21 @@ private:
void ClearListenersLocked();
bool HasListenerLocked();
};
class ConnectionManagerListener : public IConnectionManagerListener {
public:
ConnectionManagerListener(sptr<CastSessionManagerService> service) : service_(service) {}
int NotifySessionIsReady() override;
bool NotifyRemoteDeviceIsReady(int castSessionId, const CastInnerRemoteDevice &device) override;
void NotifyDeviceIsOffline(const std::string &deviceId) override;
void GrabDevice(int32_t sessionId) override;
void OnEvent(const std::string &deviceId, ReasonCode currentEventCode) override;
int32_t GetSessionProtocolType(int sessionId, ProtocolType &protocolType) override;
int32_t SetSessionProtocolType(int sessionId, ProtocolType protocolType) override;
private:
wptr<CastSessionManagerService> service_;
};
} // namespace CastEngineService
} // namespace CastEngine
} // namespace OHOS

View File

@ -219,153 +219,153 @@ public:
service->ReportDeviceFound(remoteDevices);
}
private:
wptr<CastSessionManagerService> service_;
};
class ConnectionManagerListener : public IConnectionManagerListener {
public:
ConnectionManagerListener(sptr<CastSessionManagerService> service) : service_(service) {}
int NotifySessionIsReady() override
{
auto service = service_.promote();
if (!service) {
CLOGE("service is null");
return INVALID_ID;
}
sptr<ICastSessionImpl> session;
service->CreateCastSession({}, session);
if (session == nullptr) {
return INVALID_ID;
}
service->ReportSessionCreate(session);
std::string sessionId{};
session->GetSessionId(sessionId);
return Utils::StringToInt((sessionId));
}
bool NotifyRemoteDeviceIsReady(int castSessionId, const CastInnerRemoteDevice &device) override
{
auto service = service_.promote();
if (!service) {
CLOGE("service is null");
return false;
}
sptr<ICastSessionImpl> session;
service->GetCastSession(std::to_string(castSessionId), session);
if (session == nullptr) {
CLOGE("Session is null when consultation data comes!");
return false;
}
if (!session->AddDevice(device)) {
CLOGE("Session addDevice fail");
return false;
}
return true;
}
void NotifyDeviceIsOffline(const std::string &deviceId) override
{
auto service = service_.promote();
if (!service) {
CLOGE("service is null");
return;
}
service->ReportDeviceOffline(deviceId);
CLOGD("OnDeviceOffline out");
}
void OnEvent(const std::string &deviceId, ReasonCode currentEventCode) override
{
auto service = service_.promote();
if (!service) {
CLOGE("service is null");
return;
}
int sessionId = CastDeviceDataManager::GetInstance().GetSessionIdByDeviceId(deviceId);
if (sessionId == INVALID_ID) {
CLOGE("The obtained sessionId is null");
return;
}
sptr<ICastSessionImpl> session;
service->GetCastSession(std::to_string(sessionId), session);
if (!session) {
CLOGE("The session is null. Failed to obtain the session.");
return;
}
session->OnSessionEvent(deviceId, currentEventCode);
}
void GrabDevice(int32_t sessionId) override
{
auto service = service_.promote();
if (!service) {
CLOGE("service is null");
return;
}
sptr<ICastSessionImpl> session;
service->GetCastSession(std::to_string(sessionId), session);
if (!session) {
CLOGE("The session is null. Failed to obtain the session.");
return;
}
session->Release();
}
int32_t GetSessionProtocolType(int sessionId, ProtocolType &protocolType) override
{
CLOGI("GetSessionProtocolType in");
auto service = service_.promote();
if (!service) {
CLOGE("service is null");
return CAST_ENGINE_ERROR;
}
sptr<ICastSessionImpl> session;
service->GetCastSession(std::to_string(sessionId), session);
if (!session) {
CLOGE("The session is null. Failed to obtain the session.");
return CAST_ENGINE_ERROR;
}
return session->GetSessionProtocolType(protocolType);
}
int32_t SetSessionProtocolType(int sessionId, ProtocolType protocolType) override
{
CLOGI("SetSessionProtocolType in");
auto service = service_.promote();
if (!service) {
CLOGE("service is null");
return CAST_ENGINE_ERROR;
}
sptr<ICastSessionImpl> session;
service->GetCastSession(std::to_string(sessionId), session);
if (!session) {
CLOGE("The session is null. Failed to obtain the session.");
return CAST_ENGINE_ERROR;
}
session->SetSessionProtocolType(protocolType);
return CAST_ENGINE_SUCCESS;
}
private:
wptr<CastSessionManagerService> service_;
};
} // namespace
int ConnectionManagerListener::NotifySessionIsReady()
{
auto service = service_.promote();
if (!service) {
CLOGE("service is null");
return INVALID_ID;
}
sptr<ICastSessionImpl> session;
service->CreateCastSession({}, session);
if (session == nullptr) {
return INVALID_ID;
}
service->ReportSessionCreate(session);
std::string sessionId{};
session->GetSessionId(sessionId);
return Utils::StringToInt((sessionId));
}
bool ConnectionManagerListener::NotifyRemoteDeviceIsReady(int castSessionId, const CastInnerRemoteDevice &device)
{
auto service = service_.promote();
if (!service) {
CLOGE("service is null");
return false;
}
sptr<ICastSessionImpl> session;
service->GetCastSession(std::to_string(castSessionId), session);
if (session == nullptr) {
CLOGE("Session is null when consultation data comes!");
return false;
}
if (!session->AddDevice(device)) {
CLOGE("Session addDevice fail");
return false;
}
return true;
}
void ConnectionManagerListener::NotifyDeviceIsOffline(const std::string &deviceId)
{
auto service = service_.promote();
if (!service) {
CLOGE("service is null");
return;
}
service->ReportDeviceOffline(deviceId);
CLOGD("OnDeviceOffline out");
}
void ConnectionManagerListener::OnEvent(const std::string &deviceId, ReasonCode currentEventCode)
{
auto service = service_.promote();
if (!service) {
CLOGE("service is null");
return;
}
int sessionId = CastDeviceDataManager::GetInstance().GetSessionIdByDeviceId(deviceId);
if (sessionId == INVALID_ID) {
CLOGE("The obtained sessionId is null");
return;
}
sptr<ICastSessionImpl> session;
service->GetCastSession(std::to_string(sessionId), session);
if (!session) {
CLOGE("The session is null. Failed to obtain the session.");
return;
}
session->OnSessionEvent(deviceId, currentEventCode);
}
void ConnectionManagerListener::GrabDevice(int32_t sessionId)
{
auto service = service_.promote();
if (!service) {
CLOGE("service is null");
return;
}
sptr<ICastSessionImpl> session;
service->GetCastSession(std::to_string(sessionId), session);
if (!session) {
CLOGE("The session is null. Failed to obtain the session.");
return;
}
session->Release();
}
int32_t ConnectionManagerListener::GetSessionProtocolType(int sessionId, ProtocolType &protocolType)
{
CLOGI("GetSessionProtocolType in");
auto service = service_.promote();
if (!service) {
CLOGE("service is null");
return CAST_ENGINE_ERROR;
}
sptr<ICastSessionImpl> session;
service->GetCastSession(std::to_string(sessionId), session);
if (!session) {
CLOGE("The session is null. Failed to obtain the session.");
return CAST_ENGINE_ERROR;
}
return session->GetSessionProtocolType(protocolType);
}
int32_t ConnectionManagerListener::SetSessionProtocolType(int sessionId, ProtocolType protocolType)
{
CLOGI("SetSessionProtocolType in");
auto service = service_.promote();
if (!service) {
CLOGE("service is null");
return CAST_ENGINE_ERROR;
}
sptr<ICastSessionImpl> session;
service->GetCastSession(std::to_string(sessionId), session);
if (!session) {
CLOGE("The session is null. Failed to obtain the session.");
return CAST_ENGINE_ERROR;
}
session->SetSessionProtocolType(protocolType);
return CAST_ENGINE_SUCCESS;
}
int32_t CastSessionManagerService::RegisterListener(sptr<ICastServiceListenerImpl> listener)
{
CLOGI("RegisterListener in");
HiSysEventWrite(CAST_ENGINE_DFX_DOMAIN_NAME, "CAST_ENGINE_EVE", HiviewDFX::HiSysEvent::EventType::STATISTIC,
"SEQUENTIAL_ID", CastEngineDfx::GetSequentialId(), "BIZ_PACKAGE_NAME", CastEngineDfx::GetBizPackageName());
SharedWLock lock(mutex_);
if (listener == nullptr) {
CLOGE("RegisterListener failed, listener is null");
return CAST_ENGINE_ERROR;
}
bool needInitMore = !HasListenerLocked();
if (!AddListenerLocked(listener)) {
return CAST_ENGINE_ERROR;