Skip to content

Commit

Permalink
Refactor exchange usage in IM to improve cirque stability (#6730)
Browse files Browse the repository at this point in the history
* Refactor exhange usage in IM to improve cirque stability

* Update src/app/ReadClient.cpp

Co-authored-by: Boris Zbarsky <[email protected]>

* Update src/app/CommandSender.cpp

Co-authored-by: Boris Zbarsky <[email protected]>

* Rename ClearExistingExchangeContext to AbortExistingExchangeContext

Co-authored-by: Boris Zbarsky <[email protected]>
  • Loading branch information
2 people authored and pull[bot] committed Jul 13, 2021
1 parent 3b0714a commit 05cfa9f
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 34 deletions.
2 changes: 1 addition & 1 deletion scripts/tests/cirque_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ OT_SIMULATION_CACHE="$CIRQUE_CACHE_PATH/ot-simulation.tgz"
CIRQUE_TESTS=(
"EchoTest"
"MobileDeviceTest"
"InteractionModel"
"InteractionModelTest"
)

BOLD_GREEN_TEXT="\033[1;32m"
Expand Down
7 changes: 3 additions & 4 deletions src/app/Command.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ CHIP_ERROR Command::Init(Messaging::ExchangeManager * apExchangeMgr, Interaction
// Error if already initialized.
VerifyOrExit(apExchangeMgr != nullptr, err = CHIP_ERROR_INCORRECT_STATE);
VerifyOrExit(mpExchangeMgr == nullptr, err = CHIP_ERROR_INCORRECT_STATE);
VerifyOrExit(mpExchangeCtx == nullptr, err = CHIP_ERROR_INCORRECT_STATE);

mpExchangeMgr = apExchangeMgr;
mpDelegate = apDelegate;
Expand All @@ -53,7 +52,7 @@ CHIP_ERROR Command::Reset()
{
CHIP_ERROR err = CHIP_NO_ERROR;
CommandList::Builder commandListBuilder;
ClearExistingExchangeContext();
AbortExistingExchangeContext();

if (mCommandMessageBuf.IsNull())
{
Expand Down Expand Up @@ -133,7 +132,7 @@ void Command::Shutdown()
mCommandMessageWriter.Reset();
mCommandMessageBuf = nullptr;

ClearExistingExchangeContext();
AbortExistingExchangeContext();

mpExchangeMgr = nullptr;
mpDelegate = nullptr;
Expand Down Expand Up @@ -213,7 +212,7 @@ CHIP_ERROR Command::ConstructCommandPath(const CommandPathParams & aCommandPathP
return commandPath.GetError();
}

CHIP_ERROR Command::ClearExistingExchangeContext()
CHIP_ERROR Command::AbortExistingExchangeContext()
{
// Discard any existing exchange context. Effectively we can only have one Echo exchange with
// a single node at any one time.
Expand Down
2 changes: 1 addition & 1 deletion src/app/Command.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ class Command
virtual CHIP_ERROR ProcessCommandDataElement(CommandDataElement::Parser & aCommandElement) = 0;

protected:
CHIP_ERROR ClearExistingExchangeContext();
CHIP_ERROR AbortExistingExchangeContext();
void MoveToState(const CommandState aTargetState);
CHIP_ERROR ProcessCommandMessage(System::PacketBufferHandle && payload, CommandRoleId aCommandRoleId);
CHIP_ERROR ConstructCommandPath(const CommandPathParams & aCommandPathParams, CommandDataElement::Builder aCommandDataElement);
Expand Down
30 changes: 14 additions & 16 deletions src/app/CommandSender.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ CHIP_ERROR CommandSender::SendCommandRequest(NodeId aNodeId, Transport::AdminId
err = FinalizeCommandsMessage();
SuccessOrExit(err);

ClearExistingExchangeContext();
// Discard any existing exchange context. Effectively we can only have one exchange per CommandSender
// at any one time.
AbortExistingExchangeContext();

// Create a new exchange context.
// TODO: temprary create a SecureSessionHandle from node id, will be fix in PR 3602
Expand All @@ -59,7 +61,7 @@ CHIP_ERROR CommandSender::SendCommandRequest(NodeId aNodeId, Transport::AdminId
exit:
if (err != CHIP_NO_ERROR)
{
ClearExistingExchangeContext();
AbortExistingExchangeContext();
}
ChipLogFunctError(err);

Expand All @@ -70,27 +72,23 @@ void CommandSender::OnMessageReceived(Messaging::ExchangeContext * apExchangeCon
const PayloadHeader & aPayloadHeader, System::PacketBufferHandle aPayload)
{
CHIP_ERROR err = CHIP_NO_ERROR;
// Assert that the exchange context matches the client's current context.
// This should never fail because even if SendCommandRequest is called
// back-to-back, the second call will call Close() on the first exchange,
// which clears the OnMessageReceived callback.

VerifyOrDie(apExchangeContext == mpExchangeCtx);

// Verify that the message is an Invoke Command Response.
// If not, close the exchange and free the payload.
if (!aPayloadHeader.HasMessageType(Protocols::InteractionModel::MsgType::InvokeCommandResponse))
{
apExchangeContext->Close();
mpExchangeCtx = nullptr;
goto exit;
}
VerifyOrExit(apExchangeContext == mpExchangeCtx, err = CHIP_ERROR_INCORRECT_STATE);
VerifyOrExit(aPayloadHeader.HasMessageType(Protocols::InteractionModel::MsgType::InvokeCommandResponse),
err = CHIP_ERROR_INVALID_MESSAGE_TYPE);

err = ProcessCommandMessage(std::move(aPayload), CommandRoleId::SenderId);
SuccessOrExit(err);

exit:
ChipLogFunctError(err);

// Close the exchange cleanly so that the ExchangeManager will send an ack for the message we just received.
// This needs to be done before the Reset() call, because Reset() aborts mpExchangeCtx if its not null.
mpExchangeCtx->Close();
mpExchangeCtx = nullptr;
Reset();

if (mpDelegate != nullptr)
{
if (err != CHIP_NO_ERROR)
Expand Down
29 changes: 21 additions & 8 deletions src/app/ReadClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,21 @@ CHIP_ERROR ReadClient::Init(Messaging::ExchangeManager * apExchangeMgr, Interact
// Error if already initialized.
VerifyOrExit(apExchangeMgr != nullptr, err = CHIP_ERROR_INCORRECT_STATE);
VerifyOrExit(mpExchangeMgr == nullptr, err = CHIP_ERROR_INCORRECT_STATE);
VerifyOrExit(mpExchangeCtx == nullptr, err = CHIP_ERROR_INCORRECT_STATE);

mpExchangeMgr = apExchangeMgr;
mpExchangeCtx = nullptr;
mpDelegate = apDelegate;
mState = ClientState::Initialized;

AbortExistingExchangeContext();

exit:
ChipLogFunctError(err);
return err;
}

void ReadClient::Shutdown()
{
ClearExistingExchangeContext();
AbortExistingExchangeContext();
mpExchangeMgr = nullptr;
mpDelegate = nullptr;
MoveToState(ClientState::Uninitialized);
Expand Down Expand Up @@ -88,7 +88,10 @@ CHIP_ERROR ReadClient::SendReadRequest(NodeId aNodeId, Transport::AdminId aAdmin
InteractionModelEngine::GetInstance()->GetReadClientArrayIndex(this), GetStateStr());
VerifyOrExit(ClientState::Initialized == mState, err = CHIP_ERROR_INCORRECT_STATE);
VerifyOrExit(mpDelegate != nullptr, err = CHIP_ERROR_INCORRECT_STATE);
VerifyOrExit(mpExchangeCtx == nullptr, err = CHIP_ERROR_INCORRECT_STATE);

// Discard any existing exchange context. Effectively we can only have one exchange per ReadClient
// at any one time.
AbortExistingExchangeContext();

{
System::PacketBufferTLVWriter writer;
Expand Down Expand Up @@ -175,23 +178,33 @@ CHIP_ERROR ReadClient::SendReadRequest(NodeId aNodeId, Transport::AdminId aAdmin

exit:
ChipLogFunctError(err);

if (err != CHIP_NO_ERROR)
{
AbortExistingExchangeContext();
}

return err;
}

void ReadClient::OnMessageReceived(Messaging::ExchangeContext * apExchangeContext, const PacketHeader & aPacketHeader,
const PayloadHeader & aPayloadHeader, System::PacketBufferHandle aPayload)
{
CHIP_ERROR err = CHIP_NO_ERROR;

VerifyOrExit(apExchangeContext == mpExchangeCtx, err = CHIP_ERROR_INCORRECT_STATE);
VerifyOrExit(aPayloadHeader.HasMessageType(Protocols::InteractionModel::MsgType::ReportData),
err = CHIP_ERROR_INVALID_MESSAGE_TYPE);
VerifyOrExit(apExchangeContext == mpExchangeCtx, err = CHIP_ERROR_INCORRECT_STATE);
err = ProcessReportData(std::move(aPayload));

exit:
ChipLogFunctError(err);

ClearExistingExchangeContext();
// Close the exchange cleanly so that the ExchangeManager will send an ack for the message we just received.
mpExchangeCtx->Close();
mpExchangeCtx = nullptr;
MoveToState(ClientState::Initialized);

if (mpDelegate != nullptr)
{
if (err != CHIP_NO_ERROR)
Expand All @@ -207,7 +220,7 @@ void ReadClient::OnMessageReceived(Messaging::ExchangeContext * apExchangeContex
return;
}

CHIP_ERROR ReadClient::ClearExistingExchangeContext()
CHIP_ERROR ReadClient::AbortExistingExchangeContext()
{
if (mpExchangeCtx != nullptr)
{
Expand Down Expand Up @@ -302,7 +315,7 @@ void ReadClient::OnResponseTimeout(Messaging::ExchangeContext * apExchangeContex
{
ChipLogProgress(DataManagement, "Time out! failed to receive report data from Exchange: %d",
apExchangeContext->GetExchangeId());
ClearExistingExchangeContext();
AbortExistingExchangeContext();
MoveToState(ClientState::Initialized);
if (nullptr != mpDelegate)
{
Expand Down
2 changes: 1 addition & 1 deletion src/app/ReadClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ class ReadClient : public Messaging::ExchangeDelegate

void MoveToState(const ClientState aTargetState);
CHIP_ERROR ProcessReportData(System::PacketBufferHandle aPayload);
CHIP_ERROR ClearExistingExchangeContext();
CHIP_ERROR AbortExistingExchangeContext();
const char * GetStateStr() const;

Messaging::ExchangeManager * mpExchangeMgr = nullptr;
Expand Down
4 changes: 2 additions & 2 deletions src/app/ReadHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,15 @@ void ReadHandler::Shutdown()
{
InteractionModelEngine::GetInstance()->ReleaseClusterInfoList(mpAttributeClusterInfoList);
InteractionModelEngine::GetInstance()->ReleaseClusterInfoList(mpEventClusterInfoList);
ClearExistingExchangeContext();
AbortExistingExchangeContext();
MoveToState(HandlerState::Uninitialized);
mpDelegate = nullptr;
mpAttributeClusterInfoList = nullptr;
mpEventClusterInfoList = nullptr;
mCurrentPriority = PriorityLevel::Invalid;
}

CHIP_ERROR ReadHandler::ClearExistingExchangeContext()
CHIP_ERROR ReadHandler::AbortExistingExchangeContext()
{
if (mpExchangeCtx != nullptr)
{
Expand Down
2 changes: 1 addition & 1 deletion src/app/ReadHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ class ReadHandler
void MoveToState(const HandlerState aTargetState);

const char * GetStateStr() const;
CHIP_ERROR ClearExistingExchangeContext();
CHIP_ERROR AbortExistingExchangeContext();

Messaging::ExchangeContext * mpExchangeCtx = nullptr;
InteractionModelDelegate * mpDelegate = nullptr;
Expand Down

0 comments on commit 05cfa9f

Please sign in to comment.