diff --git a/src/app/CASESessionManager.cpp b/src/app/CASESessionManager.cpp index aa7be9ef58476d..7f964f2b7d96c3 100644 --- a/src/app/CASESessionManager.cpp +++ b/src/app/CASESessionManager.cpp @@ -24,19 +24,20 @@ CHIP_ERROR CASESessionManager::FindOrEstablishSession(NodeId nodeId, Callback::C Callback::Callback * onFailure) { VerifyOrReturnError(mInitialized, CHIP_ERROR_INCORRECT_STATE); - OperationalDeviceProxy * session = FindExistingSession(nodeId); - if (session == nullptr) - { - Dnssd::ResolvedNodeData * nodeResolutionData = nullptr; - Dnssd::ResolvedNodeData cachedResolutionData; - PeerId peerId = GetFabricInfo()->GetPeerIdForNode(nodeId); + Dnssd::ResolvedNodeData * nodeResolutionData = nullptr; + Dnssd::ResolvedNodeData cachedResolutionData; - if (mConfig.dnsCache != nullptr && mConfig.dnsCache->Lookup(peerId, cachedResolutionData) == CHIP_NO_ERROR) - { - nodeResolutionData = &cachedResolutionData; - } + PeerId peerId = GetFabricInfo()->GetPeerIdForNode(nodeId); + if (mConfig.dnsCache != nullptr && mConfig.dnsCache->Lookup(peerId, cachedResolutionData) == CHIP_NO_ERROR) + { + nodeResolutionData = &cachedResolutionData; + } + + OperationalDeviceProxy * session = FindExistingSession(nodeId); + if (session == nullptr) + { // TODO - Implement LRU to evict least recently used session to handle mActiveSessions pool exhaustion session = mActiveSessions.CreateObject(mConfig.sessionInitParams, peerId, nodeResolutionData); if (session == nullptr) @@ -45,6 +46,10 @@ CHIP_ERROR CASESessionManager::FindOrEstablishSession(NodeId nodeId, Callback::C return CHIP_ERROR_NO_MEMORY; } } + else if (nodeResolutionData != nullptr) + { + session->OnNodeIdResolved(nodeResolutionData); + } CHIP_ERROR err = session->Connect(onConnection, onFailure); if (err != CHIP_NO_ERROR) diff --git a/src/app/OperationalDeviceProxy.h b/src/app/OperationalDeviceProxy.h index 77c21180012d08..c7c0e456312806 100644 --- a/src/app/OperationalDeviceProxy.h +++ b/src/app/OperationalDeviceProxy.h @@ -80,20 +80,8 @@ class DLL_EXPORT OperationalDeviceProxy : public DeviceProxy, public SessionEsta mInitParams = params; mPeerId = peerId; - if (nodeResolutionData != nullptr) - { - mDeviceAddress = ToPeerAddress(*nodeResolutionData); - - mMrpIdleInterval = nodeResolutionData->GetMrpRetryIntervalIdle().ValueOr(CHIP_CONFIG_MRP_DEFAULT_IDLE_RETRY_INTERVAL); - mMrpActiveInterval = - nodeResolutionData->GetMrpRetryIntervalActive().ValueOr(CHIP_CONFIG_MRP_DEFAULT_ACTIVE_RETRY_INTERVAL); - - mState = State::Initialized; - } - else - { - mState = State::NeedsAddress; - } + mState = State::NeedsAddress; + OnNodeIdResolved(nodeResolutionData); } void Clear(); @@ -122,6 +110,23 @@ class DLL_EXPORT OperationalDeviceProxy : public DeviceProxy, public SessionEsta */ void OnConnectionExpired(SessionHandle session) override; + void OnNodeIdResolved(const Dnssd::ResolvedNodeData * nodeResolutionData) + { + if (nodeResolutionData != nullptr) + { + mDeviceAddress = ToPeerAddress(*nodeResolutionData); + + mMrpIdleInterval = nodeResolutionData->GetMrpRetryIntervalIdle().ValueOr(CHIP_CONFIG_MRP_DEFAULT_IDLE_RETRY_INTERVAL); + mMrpActiveInterval = + nodeResolutionData->GetMrpRetryIntervalActive().ValueOr(CHIP_CONFIG_MRP_DEFAULT_ACTIVE_RETRY_INTERVAL); + + if (mState == State::NeedsAddress) + { + mState = State::Initialized; + } + } + } + /** * Mark any open session with the device as expired. */