Skip to content

Commit

Permalink
[app] make size of CASE client and device pool configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
gjc13 authored and woody-apple committed Dec 6, 2021
1 parent a3312eb commit 4048ea4
Show file tree
Hide file tree
Showing 12 changed files with 137 additions and 39 deletions.
1 change: 1 addition & 0 deletions src/app/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ static_library("app") {
"MessageDef/WriteResponseMessage.cpp",
"OperationalDeviceProxy.cpp",
"OperationalDeviceProxy.h",
"OperationalDeviceProxyPool.h",
"ReadClient.cpp",
"ReadHandler.cpp",
"StatusResponse.cpp",
Expand Down
5 changes: 5 additions & 0 deletions src/app/CASEClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ namespace chip {

CASEClient::CASEClient(const CASEClientInitParams & params) : mInitParams(params) {}

void CASEClient::SetMRPIntervals(const ReliableMessageProtocolConfig & mrpConfig)
{
mCASESession.SetMRPConfig(mrpConfig);
}

CHIP_ERROR CASEClient::EstablishSession(PeerId peer, const Transport::PeerAddress & peerAddress,
const ReliableMessageProtocolConfig & mrpConfig, OnCASEConnected onConnection,
OnCASEConnectionFailure onFailure, void * context)
Expand Down
2 changes: 1 addition & 1 deletion src/app/CASEClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class DLL_EXPORT CASEClient : public SessionEstablishmentDelegate
public:
CASEClient(const CASEClientInitParams & params);

void SetMRPIntervals(uint32_t idleInterval, uint32_t activeInterval);
void SetMRPIntervals(const ReliableMessageProtocolConfig & mrpConfig);

CHIP_ERROR EstablishSession(PeerId peer, const Transport::PeerAddress & peerAddress,
const ReliableMessageProtocolConfig & mrpConfig, OnCASEConnected onConnection,
Expand Down
19 changes: 15 additions & 4 deletions src/app/CASEClientPool.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,26 @@

namespace chip {

class CASEClientPool
class CASEClientPoolDelegate
{
public:
CASEClient * Allocate(CASEClientInitParams params) { return mClientPool.CreateObject(params); }
virtual CASEClient * Allocate(CASEClientInitParams params) = 0;

void Release(CASEClient * client) { mClientPool.ReleaseObject(client); }
virtual void Release(CASEClient * client) = 0;

virtual ~CASEClientPoolDelegate() {}
};

template <size_t N>
class CASEClientPool : public CASEClientPoolDelegate
{
public:
CASEClient * Allocate(CASEClientInitParams params) override { return mClientPool.CreateObject(params); }

void Release(CASEClient * client) override { mClientPool.ReleaseObject(client); }

private:
BitMapObjectPool<CASEClient, CHIP_CONFIG_MAX_ACTIVE_CASE_CLIENTS> mClientPool;
BitMapObjectPool<CASEClient, N> mClientPool;
};

}; // namespace chip
30 changes: 5 additions & 25 deletions src/app/CASESessionManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ CHIP_ERROR CASESessionManager::FindOrEstablishSession(NodeId nodeId, Callback::C
// TODO - Implement LRU to evict least recently used session to handle mActiveSessions pool exhaustion
if (nodeIDWasResolved)
{
session = mActiveSessions.CreateObject(mConfig.sessionInitParams, peerId, resolutionData);
session = mConfig.devicePool->Allocate(mConfig.sessionInitParams, peerId, resolutionData);
}
else
{
session = mActiveSessions.CreateObject(mConfig.sessionInitParams, peerId);
session = mConfig.devicePool->Allocate(mConfig.sessionInitParams, peerId);
}

if (session == nullptr)
Expand Down Expand Up @@ -120,39 +120,19 @@ void CASESessionManager::OnSessionReleased(SessionHandle sessionHandle)

OperationalDeviceProxy * CASESessionManager::FindSession(SessionHandle session)
{
OperationalDeviceProxy * foundSession = nullptr;
mActiveSessions.ForEachActiveObject([&](auto * activeSession) {
if (activeSession->MatchesSession(session))
{
foundSession = activeSession;
return Loop::Break;
}
return Loop::Continue;
});

return foundSession;
return mConfig.devicePool->FindDevice(session);
}

OperationalDeviceProxy * CASESessionManager::FindExistingSession(NodeId id)
{
OperationalDeviceProxy * foundSession = nullptr;
mActiveSessions.ForEachActiveObject([&](auto * activeSession) {
if (activeSession->GetDeviceId() == id)
{
foundSession = activeSession;
return Loop::Break;
}
return Loop::Continue;
});

return foundSession;
return mConfig.devicePool->FindDevice(id);
}

void CASESessionManager::ReleaseSession(OperationalDeviceProxy * session)
{
if (session != nullptr)
{
mActiveSessions.ReleaseObject(session);
mConfig.devicePool->Release(session);
}
}

Expand Down
6 changes: 2 additions & 4 deletions src/app/CASESessionManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

#include <app/CASEClientPool.h>
#include <app/OperationalDeviceProxy.h>
#include <app/OperationalDeviceProxyPool.h>
#include <lib/core/CHIPConfig.h>
#include <lib/core/CHIPCore.h>
#include <lib/dnssd/DnssdCache.h>
Expand All @@ -34,6 +35,7 @@ struct CASESessionManagerConfig
{
DeviceProxyInitParams sessionInitParams;
Dnssd::DnssdCache<CHIP_CONFIG_MDNS_CACHE_SIZE> * dnsCache = nullptr;
OperationalDeviceProxyPoolDelegate * devicePool = nullptr;
};

/**
Expand Down Expand Up @@ -104,10 +106,6 @@ class CASESessionManager : public SessionReleaseDelegate, public Dnssd::Resolver
OperationalDeviceProxy * FindSession(SessionHandle session);
void ReleaseSession(OperationalDeviceProxy * device);

BitMapObjectPool<OperationalDeviceProxy, CHIP_CONFIG_CONTROLLER_MAX_ACTIVE_DEVICES,
OnObjectPoolDestruction::IgnoreUnsafeDoNotUseInNewCode>
mActiveSessions;

CASESessionManagerConfig mConfig;
};

Expand Down
5 changes: 4 additions & 1 deletion src/app/OperationalDeviceProxy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,10 @@ CHIP_ERROR OperationalDeviceProxy::UpdateDeviceData(const Transport::PeerAddress

// Initialize CASE session state with any MRP parameters that DNS-SD has provided.
// It can be overridden by CASE session protocol messages that include MRP parameters.
mCASESession.SetMRPConfig(mMRPConfig);
if (mCASEClient)
{
mCASEClient->SetMRPIntervals(mMRPConfig);
}

if (mState == State::NeedsAddress)
{
Expand Down
2 changes: 1 addition & 1 deletion src/app/OperationalDeviceProxy.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ struct DeviceProxyInitParams
Messaging::ExchangeManager * exchangeMgr = nullptr;
SessionIDAllocator * idAllocator = nullptr;
FabricInfo * fabricInfo = nullptr;
CASEClientPool * clientPool = nullptr;
CASEClientPoolDelegate * clientPool = nullptr;

Controller::DeviceControllerInteractionModelDelegate * imDelegate = nullptr;

Expand Down
94 changes: 94 additions & 0 deletions src/app/OperationalDeviceProxyPool.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
*
* Copyright (c) 2021 Project CHIP Authors
*
* 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.
*/

#pragma once

#include <app/OperationalDeviceProxy.h>
#include <lib/support/Pool.h>
#include <transport/SessionHandle.h>

namespace chip {

class OperationalDeviceProxyPoolDelegate
{
public:
virtual OperationalDeviceProxy * Allocate(DeviceProxyInitParams & params, PeerId peerId) = 0;

virtual OperationalDeviceProxy * Allocate(DeviceProxyInitParams & params, PeerId peerId,
const Dnssd::ResolvedNodeData & nodeResolutionData) = 0;

virtual void Release(OperationalDeviceProxy * device) = 0;

virtual OperationalDeviceProxy * FindDevice(SessionHandle session) = 0;

virtual OperationalDeviceProxy * FindDevice(NodeId id) = 0;

virtual ~OperationalDeviceProxyPoolDelegate() {}
};

template <size_t N>
class OperationalDeviceProxyPool : public OperationalDeviceProxyPoolDelegate
{
public:
OperationalDeviceProxy * Allocate(DeviceProxyInitParams & params, PeerId peerId) override
{
return mDevicePool.CreateObject(params, peerId);
}

OperationalDeviceProxy * Allocate(DeviceProxyInitParams & params, PeerId peerId,
const Dnssd::ResolvedNodeData & nodeResolutionData) override
{
return mDevicePool.CreateObject(params, peerId, nodeResolutionData);
}

void Release(OperationalDeviceProxy * device) override { mDevicePool.ReleaseObject(device); }

OperationalDeviceProxy * FindDevice(SessionHandle session) override
{
OperationalDeviceProxy * foundDevice = nullptr;
mDevicePool.ForEachActiveObject([&](auto * activeDevice) {
if (activeDevice->MatchesSession(session))
{
foundDevice = activeDevice;
return Loop::Break;
}
return Loop::Continue;
});

return foundDevice;
}

OperationalDeviceProxy * FindDevice(NodeId id) override
{
OperationalDeviceProxy * foundDevice = nullptr;
mDevicePool.ForEachActiveObject([&](auto * activeDevice) {
if (activeDevice->GetDeviceId() == id)
{
foundDevice = activeDevice;
return Loop::Break;
}
return Loop::Continue;
});

return foundDevice;
}

private:
BitMapObjectPool<OperationalDeviceProxy, N, OnObjectPoolDestruction::IgnoreUnsafeDoNotUseInNewCode> mDevicePool;
};

}; // namespace chip
2 changes: 2 additions & 0 deletions src/controller/CHIPDeviceController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,12 +151,14 @@ CHIP_ERROR DeviceController::Init(ControllerInitParams params)
.exchangeMgr = params.systemState->ExchangeMgr(),
.idAllocator = &mIDAllocator,
.fabricInfo = params.systemState->Fabrics()->FindFabricWithIndex(mFabricIndex),
.clientPool = &mCASEClientPool,
.imDelegate = params.systemState->IMDelegate(),
};

CASESessionManagerConfig sessionManagerConfig = {
.sessionInitParams = deviceInitParams,
.dnsCache = &mDNSCache,
.devicePool = &mDevicePool,
};

mCASESessionManager = chip::Platform::New<CASESessionManager>(sessionManagerConfig);
Expand Down
4 changes: 4 additions & 0 deletions src/controller/CHIPDeviceController.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,12 @@

#pragma once

#include <app/CASEClientPool.h>
#include <app/CASESessionManager.h>
#include <app/DeviceControllerInteractionModelDelegate.h>
#include <app/InteractionModelDelegate.h>
#include <app/OperationalDeviceProxy.h>
#include <app/OperationalDeviceProxyPool.h>
#include <controller-clusters/zap-generated/CHIPClientCallbacks.h>
#include <controller/AbstractDnssdDiscoveryController.h>
#include <controller/CHIPDeviceControllerSystemState.h>
Expand Down Expand Up @@ -352,6 +354,8 @@ class DLL_EXPORT DeviceController : public SessionReleaseDelegate,
CASESessionManager * mCASESessionManager = nullptr;

Dnssd::DnssdCache<CHIP_CONFIG_MDNS_CACHE_SIZE> mDNSCache;
CASEClientPool<CHIP_CONFIG_CONTROLLER_MAX_ACTIVE_CASE_CLIENTS> mCASEClientPool;
OperationalDeviceProxyPool<CHIP_CONFIG_CONTROLLER_MAX_ACTIVE_DEVICES> mDevicePool;

SerializableU64Set<kNumMaxPairedDevices> mPairedDevices;
bool mPairedDevicesInitialized;
Expand Down
6 changes: 3 additions & 3 deletions src/lib/core/CHIPConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -2562,12 +2562,12 @@ extern const char CHIP_NON_PRODUCTION_MARKER[];
#endif

/**
* @def CHIP_CONFIG_MAX_ACTIVE_CASE_CLIENTS
* @def CHIP_CONFIG_CONTROLLER_MAX_ACTIVE_CASE_CLIENTS
*
* @brief Number of outgoing CASE sessions can be simutaneously negotiated.
*/
#ifndef CHIP_CONFIG_MAX_ACTIVE_CASE_CLIENTS
#define CHIP_CONFIG_MAX_ACTIVE_CASE_CLIENTS 2
#ifndef CHIP_CONFIG_CONTROLLER_MAX_ACTIVE_CASE_CLIENTS
#define CHIP_CONFIG_CONTROLLER_MAX_ACTIVE_CASE_CLIENTS 16
#endif

/**
Expand Down

0 comments on commit 4048ea4

Please sign in to comment.