Skip to content

Commit

Permalink
Use ExchangeHandle to track ref count of ExchangeContext
Browse files Browse the repository at this point in the history
  • Loading branch information
kghost committed May 26, 2021
1 parent 5db8f44 commit d670a3f
Show file tree
Hide file tree
Showing 76 changed files with 455 additions and 429 deletions.
2 changes: 1 addition & 1 deletion examples/shell/shell_common/cmd_ping.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ CHIP_ERROR EstablishSecureSession(streamer_t * stream, Transport::PeerAddress &
return err;
}

void HandleEchoResponseReceived(Messaging::ExchangeContext * ec, System::PacketBufferHandle && payload)
void HandleEchoResponseReceived(Messaging::ExchangeHandle ec, System::PacketBufferHandle && payload)
{
uint32_t respTime = System::Timer::GetCurrentEpoch();
uint32_t transitTime = respTime - gPingArguments.GetLastEchoTime();
Expand Down
18 changes: 9 additions & 9 deletions examples/shell/shell_common/cmd_send.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ using namespace Logging;

namespace {

Messaging::ExchangeContext * gExchangeCtx = nullptr;
Messaging::ExchangeHandle gExchangeCtx;

class SendArguments
{
Expand Down Expand Up @@ -100,7 +100,7 @@ class SendArguments
class MockAppDelegate : public Messaging::ExchangeDelegate
{
public:
void OnMessageReceived(Messaging::ExchangeContext * ec, const PacketHeader & packetHeader, const PayloadHeader & payloadHeader,
void OnMessageReceived(Messaging::ExchangeHandle ec, const PacketHeader & packetHeader, const PayloadHeader & payloadHeader,
System::PacketBufferHandle && buffer) override
{
uint32_t respTime = System::Timer::GetCurrentEpoch();
Expand All @@ -111,16 +111,16 @@ class MockAppDelegate : public Messaging::ExchangeDelegate
static_cast<double>(transitTime) / 1000);

gExchangeCtx->Close();
gExchangeCtx = nullptr;
gExchangeCtx.Release();
}

void OnResponseTimeout(Messaging::ExchangeContext * ec) override
void OnResponseTimeout(Messaging::ExchangeHandle ec) override
{
streamer_t * sout = streamer_get();
streamer_printf(sout, "No response received\n");

gExchangeCtx->Close();
gExchangeCtx = nullptr;
gExchangeCtx.Release();
}
} gMockAppDelegate;

Expand All @@ -135,15 +135,15 @@ CHIP_ERROR SendMessage(streamer_t * stream)

// Discard any existing exchange context. Effectively we can only have one exchange with
// a single node at any one time.
if (gExchangeCtx != nullptr)
if (gExchangeCtx.HasValue())
{
gExchangeCtx->Abort();
gExchangeCtx = nullptr;
gExchangeCtx.Release();
}

// Create a new exchange context.
gExchangeCtx = gExchangeManager.NewContext({ kTestDeviceNodeId, 0, gAdminId }, &gMockAppDelegate);
VerifyOrExit(gExchangeCtx != nullptr, err = CHIP_ERROR_NO_MEMORY);
VerifyOrExit(gExchangeCtx.HasValue(), err = CHIP_ERROR_NO_MEMORY);

size = gSendArguments.GetPayloadSize();
VerifyOrExit(size <= kMaxPayloadSize, err = CHIP_ERROR_INVALID_MESSAGE_LENGTH);
Expand Down Expand Up @@ -177,7 +177,7 @@ CHIP_ERROR SendMessage(streamer_t * stream)
if (err != CHIP_NO_ERROR)
{
gExchangeCtx->Abort();
gExchangeCtx = nullptr;
gExchangeCtx.Release();
}

exit:
Expand Down
4 changes: 2 additions & 2 deletions src/app/Command.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -216,10 +216,10 @@ 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.
if (mpExchangeCtx != nullptr)
if (mpExchangeCtx.HasValue())
{
mpExchangeCtx->Abort();
mpExchangeCtx = nullptr;
mpExchangeCtx.Release();
}

return CHIP_NO_ERROR;
Expand Down
4 changes: 2 additions & 2 deletions src/app/Command.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ class Command
* exchange context has been assigned or the context
* has been released.
*/
Messaging::ExchangeContext * GetExchangeContext() const { return mpExchangeCtx; }
const Messaging::ExchangeHandle GetExchangeContext() const { return mpExchangeCtx; }

CHIP_ERROR Reset();

Expand All @@ -128,7 +128,7 @@ class Command

InvokeCommand::Builder mInvokeCommandBuilder;
Messaging::ExchangeManager * mpExchangeMgr = nullptr;
Messaging::ExchangeContext * mpExchangeCtx = nullptr;
Messaging::ExchangeHandle mpExchangeCtx;
InteractionModelDelegate * mpDelegate = nullptr;
chip::System::PacketBufferHandle mCommandMessageBuf;
uint8_t mCommandIndex = 0;
Expand Down
4 changes: 2 additions & 2 deletions src/app/CommandHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ using GeneralStatusCode = chip::Protocols::SecureChannel::GeneralStatusCode;

namespace chip {
namespace app {
void CommandHandler::OnMessageReceived(Messaging::ExchangeContext * ec, const PacketHeader & packetHeader,
void CommandHandler::OnMessageReceived(Messaging::ExchangeHandle ec, const PacketHeader & packetHeader,
const PayloadHeader & payloadHeader, System::PacketBufferHandle && payload)
{
CHIP_ERROR err = CHIP_NO_ERROR;
Expand Down Expand Up @@ -62,7 +62,7 @@ CHIP_ERROR CommandHandler::SendCommandResponse()
err = FinalizeCommandsMessage();
SuccessOrExit(err);

VerifyOrExit(mpExchangeCtx != nullptr, err = CHIP_ERROR_INCORRECT_STATE);
VerifyOrExit(mpExchangeCtx.HasValue(), err = CHIP_ERROR_INCORRECT_STATE);
err = mpExchangeCtx->SendMessage(Protocols::InteractionModel::MsgType::InvokeCommandResponse, std::move(mCommandMessageBuf));
SuccessOrExit(err);

Expand Down
2 changes: 1 addition & 1 deletion src/app/CommandHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ namespace app {
class CommandHandler : public Command
{
public:
void OnMessageReceived(Messaging::ExchangeContext * ec, const PacketHeader & packetHeader, const PayloadHeader & payloadHeader,
void OnMessageReceived(Messaging::ExchangeHandle ec, const PacketHeader & packetHeader, const PayloadHeader & payloadHeader,
System::PacketBufferHandle && payload);
CHIP_ERROR AddStatusCode(const CommandPathParams * apCommandPathParams,
const Protocols::SecureChannel::GeneralStatusCode aGeneralCode, const Protocols::Id aProtocolId,
Expand Down
8 changes: 4 additions & 4 deletions src/app/CommandSender.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ CHIP_ERROR CommandSender::SendCommandRequest(NodeId aNodeId, Transport::AdminId
{
mpExchangeCtx = mpExchangeMgr->NewContext(*secureSession, this);
}
VerifyOrExit(mpExchangeCtx != nullptr, err = CHIP_ERROR_NO_MEMORY);
VerifyOrExit(mpExchangeCtx.HasValue(), err = CHIP_ERROR_NO_MEMORY);
mpExchangeCtx->SetResponseTimeout(kImMessageTimeoutMsec);

err = mpExchangeCtx->SendMessage(
Expand All @@ -77,7 +77,7 @@ CHIP_ERROR CommandSender::SendCommandRequest(NodeId aNodeId, Transport::AdminId
return err;
}

void CommandSender::OnMessageReceived(Messaging::ExchangeContext * apExchangeContext, const PacketHeader & aPacketHeader,
void CommandSender::OnMessageReceived(Messaging::ExchangeHandle apExchangeContext, const PacketHeader & aPacketHeader,
const PayloadHeader & aPayloadHeader, System::PacketBufferHandle && aPayload)
{
CHIP_ERROR err = CHIP_NO_ERROR;
Expand All @@ -95,7 +95,7 @@ void CommandSender::OnMessageReceived(Messaging::ExchangeContext * apExchangeCon
// 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;
mpExchangeCtx.Release();

if (mpDelegate != nullptr)
{
Expand All @@ -112,7 +112,7 @@ void CommandSender::OnMessageReceived(Messaging::ExchangeContext * apExchangeCon
Shutdown();
}

void CommandSender::OnResponseTimeout(Messaging::ExchangeContext * apExchangeContext)
void CommandSender::OnResponseTimeout(Messaging::ExchangeHandle apExchangeContext)
{
ChipLogProgress(DataManagement, "Time out! failed to receive invoke command response from Exchange: %d",
apExchangeContext->GetExchangeId());
Expand Down
4 changes: 2 additions & 2 deletions src/app/CommandSender.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ class CommandSender : public Command, public Messaging::ExchangeDelegate
// ExchangeDelegate interface implementation. Private so people won't
// accidentally call it on us when we're not being treated as an actual
// ExchangeDelegate.
void OnMessageReceived(Messaging::ExchangeContext * apExchangeContext, const PacketHeader & aPacketHeader,
void OnMessageReceived(Messaging::ExchangeHandle apExchangeContext, const PacketHeader & aPacketHeader,
const PayloadHeader & aPayloadHeader, System::PacketBufferHandle && aPayload) override;
void OnResponseTimeout(Messaging::ExchangeContext * apExchangeContext) override;
void OnResponseTimeout(Messaging::ExchangeHandle apExchangeContext) override;

CHIP_ERROR ProcessCommandDataElement(CommandDataElement::Parser & aCommandElement) override;
};
Expand Down
2 changes: 1 addition & 1 deletion src/app/InteractionModelDelegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class InteractionModelDelegate
*
* @retval # CHIP_ERROR_NOT_IMPLEMENTED if not implemented
*/
virtual CHIP_ERROR EventStreamReceived(const Messaging::ExchangeContext * apExchangeContext, TLV::TLVReader * apEventListReader)
virtual CHIP_ERROR EventStreamReceived(const Messaging::ExchangeHandle apExchangeContext, TLV::TLVReader * apEventListReader)
{
return CHIP_ERROR_NOT_IMPLEMENTED;
}
Expand Down
26 changes: 10 additions & 16 deletions src/app/InteractionModelEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ CHIP_ERROR InteractionModelEngine::NewReadClient(ReadClient ** const apReadClien
return err;
}

void InteractionModelEngine::OnUnknownMsgType(Messaging::ExchangeContext * apExchangeContext, const PacketHeader & aPacketHeader,
void InteractionModelEngine::OnUnknownMsgType(Messaging::ExchangeHandle apExchangeContext, const PacketHeader & aPacketHeader,
const PayloadHeader & aPayloadHeader, System::PacketBufferHandle && aPayload)
{
CHIP_ERROR err = CHIP_NO_ERROR;
Expand All @@ -151,18 +151,12 @@ void InteractionModelEngine::OnUnknownMsgType(Messaging::ExchangeContext * apExc
// SuccessOrExit(err);

apExchangeContext->Close();
apExchangeContext = nullptr;
apExchangeContext.Release();

ChipLogFunctError(err);

// Todo: Fix the below check after the above status report is implemented.
if (nullptr != apExchangeContext)
{
apExchangeContext->Abort();
}
}

void InteractionModelEngine::OnInvokeCommandRequest(Messaging::ExchangeContext * apExchangeContext,
void InteractionModelEngine::OnInvokeCommandRequest(Messaging::ExchangeHandle apExchangeContext,
const PacketHeader & aPacketHeader, const PayloadHeader & aPayloadHeader,
System::PacketBufferHandle && aPayload)
{
Expand All @@ -175,21 +169,21 @@ void InteractionModelEngine::OnInvokeCommandRequest(Messaging::ExchangeContext *
err = commandHandler.Init(mpExchangeMgr, mpDelegate);
SuccessOrExit(err);
commandHandler.OnMessageReceived(apExchangeContext, aPacketHeader, aPayloadHeader, std::move(aPayload));
apExchangeContext = nullptr;
apExchangeContext.Release();
break;
}
}

exit:
ChipLogFunctError(err);

if (nullptr != apExchangeContext)
if (apExchangeContext.HasValue())
{
apExchangeContext->Abort();
}
}

void InteractionModelEngine::OnReadRequest(Messaging::ExchangeContext * apExchangeContext, const PacketHeader & aPacketHeader,
void InteractionModelEngine::OnReadRequest(Messaging::ExchangeHandle apExchangeContext, const PacketHeader & aPacketHeader,
const PayloadHeader & aPayloadHeader, System::PacketBufferHandle && aPayload)
{
CHIP_ERROR err = CHIP_NO_ERROR;
Expand All @@ -204,21 +198,21 @@ void InteractionModelEngine::OnReadRequest(Messaging::ExchangeContext * apExchan
SuccessOrExit(err);
err = readHandler.OnReadRequest(apExchangeContext, std::move(aPayload));
SuccessOrExit(err);
apExchangeContext = nullptr;
apExchangeContext.Release();
break;
}
}

exit:
ChipLogFunctError(err);

if (nullptr != apExchangeContext)
if (apExchangeContext.HasValue())
{
apExchangeContext->Abort();
}
}

void InteractionModelEngine::OnMessageReceived(Messaging::ExchangeContext * apExchangeContext, const PacketHeader & aPacketHeader,
void InteractionModelEngine::OnMessageReceived(Messaging::ExchangeHandle apExchangeContext, const PacketHeader & aPacketHeader,
const PayloadHeader & aPayloadHeader, System::PacketBufferHandle && aPayload)
{
if (aPayloadHeader.HasMessageType(Protocols::InteractionModel::MsgType::InvokeCommandRequest))
Expand All @@ -236,7 +230,7 @@ void InteractionModelEngine::OnMessageReceived(Messaging::ExchangeContext * apEx
}
}

void InteractionModelEngine::OnResponseTimeout(Messaging::ExchangeContext * ec)
void InteractionModelEngine::OnResponseTimeout(Messaging::ExchangeHandle ec)
{
ChipLogProgress(DataManagement, "Time out! failed to receive echo response from Exchange: %d", ec->GetExchangeId());
}
Expand Down
11 changes: 6 additions & 5 deletions src/app/InteractionModelEngine.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

#include <app/MessageDef/ReportData.h>
#include <core/CHIPCore.h>
#include <messaging/ExchangeHandle.h>
#include <messaging/ExchangeContext.h>
#include <messaging/ExchangeMgr.h>
#include <messaging/Flags.h>
Expand Down Expand Up @@ -136,19 +137,19 @@ class InteractionModelEngine : public Messaging::ExchangeDelegate

private:
friend class reporting::Engine;
void OnUnknownMsgType(Messaging::ExchangeContext * apExchangeContext, const PacketHeader & aPacketHeader,
void OnUnknownMsgType(Messaging::ExchangeHandle apExchangeContext, const PacketHeader & aPacketHeader,
const PayloadHeader & aPayloadHeader, System::PacketBufferHandle && aPayload);
void OnInvokeCommandRequest(Messaging::ExchangeContext * apExchangeContext, const PacketHeader & aPacketHeader,
void OnInvokeCommandRequest(Messaging::ExchangeHandle apExchangeContext, const PacketHeader & aPacketHeader,
const PayloadHeader & aPayloadHeader, System::PacketBufferHandle && aPayload);
void OnMessageReceived(Messaging::ExchangeContext * apExchangeContext, const PacketHeader & aPacketHeader,
void OnMessageReceived(Messaging::ExchangeHandle apExchangeContext, const PacketHeader & aPacketHeader,
const PayloadHeader & aPayloadHeader, System::PacketBufferHandle && aPayload);
void OnResponseTimeout(Messaging::ExchangeContext * ec);
void OnResponseTimeout(Messaging::ExchangeHandle ec);

/**
* Called when Interaction Model receives a Read Request message. Errors processing
* the Read Request are handled entirely within this function.
*/
void OnReadRequest(Messaging::ExchangeContext * apExchangeContext, const PacketHeader & aPacketHeader,
void OnReadRequest(Messaging::ExchangeHandle apExchangeContext, const PacketHeader & aPacketHeader,
const PayloadHeader & aPayloadHeader, System::PacketBufferHandle && aPayload);

Messaging::ExchangeManager * mpExchangeMgr = nullptr;
Expand Down
12 changes: 6 additions & 6 deletions src/app/ReadClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ CHIP_ERROR ReadClient::SendReadRequest(NodeId aNodeId, Transport::AdminId aAdmin
}

mpExchangeCtx = mpExchangeMgr->NewContext({ aNodeId, 0, aAdminId }, this);
VerifyOrExit(mpExchangeCtx != nullptr, err = CHIP_ERROR_NO_MEMORY);
VerifyOrExit(mpExchangeCtx.HasValue(), err = CHIP_ERROR_NO_MEMORY);
mpExchangeCtx->SetResponseTimeout(kImMessageTimeoutMsec);

err = mpExchangeCtx->SendMessage(Protocols::InteractionModel::MsgType::ReadRequest, std::move(msgBuf),
Expand All @@ -187,7 +187,7 @@ CHIP_ERROR ReadClient::SendReadRequest(NodeId aNodeId, Transport::AdminId aAdmin
return err;
}

void ReadClient::OnMessageReceived(Messaging::ExchangeContext * apExchangeContext, const PacketHeader & aPacketHeader,
void ReadClient::OnMessageReceived(Messaging::ExchangeHandle apExchangeContext, const PacketHeader & aPacketHeader,
const PayloadHeader & aPayloadHeader, System::PacketBufferHandle && aPayload)
{
CHIP_ERROR err = CHIP_NO_ERROR;
Expand All @@ -202,7 +202,7 @@ void ReadClient::OnMessageReceived(Messaging::ExchangeContext * apExchangeContex

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

if (mpDelegate != nullptr)
Expand All @@ -222,10 +222,10 @@ void ReadClient::OnMessageReceived(Messaging::ExchangeContext * apExchangeContex

CHIP_ERROR ReadClient::AbortExistingExchangeContext()
{
if (mpExchangeCtx != nullptr)
if (mpExchangeCtx.HasValue())
{
mpExchangeCtx->Abort();
mpExchangeCtx = nullptr;
mpExchangeCtx.Release();
}

return CHIP_NO_ERROR;
Expand Down Expand Up @@ -311,7 +311,7 @@ CHIP_ERROR ReadClient::ProcessReportData(System::PacketBufferHandle && aPayload)
return err;
}

void ReadClient::OnResponseTimeout(Messaging::ExchangeContext * apExchangeContext)
void ReadClient::OnResponseTimeout(Messaging::ExchangeHandle apExchangeContext)
{
ChipLogProgress(DataManagement, "Time out! failed to receive report data from Exchange: %d",
apExchangeContext->GetExchangeId());
Expand Down
6 changes: 3 additions & 3 deletions src/app/ReadClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,9 @@ class ReadClient : public Messaging::ExchangeDelegate

virtual ~ReadClient() = default;

void OnMessageReceived(Messaging::ExchangeContext * apExchangeContext, const PacketHeader & aPacketHeader,
void OnMessageReceived(Messaging::ExchangeHandle apExchangeContext, const PacketHeader & aPacketHeader,
const PayloadHeader & aPayloadHeader, System::PacketBufferHandle && aPayload) override;
void OnResponseTimeout(Messaging::ExchangeContext * apExchangeContext) override;
void OnResponseTimeout(Messaging::ExchangeHandle apExchangeContext) override;

/**
* Check if current read client is being used
Expand All @@ -119,7 +119,7 @@ class ReadClient : public Messaging::ExchangeDelegate
const char * GetStateStr() const;

Messaging::ExchangeManager * mpExchangeMgr = nullptr;
Messaging::ExchangeContext * mpExchangeCtx = nullptr;
Messaging::ExchangeHandle mpExchangeCtx;
InteractionModelDelegate * mpDelegate = nullptr;
ClientState mState = ClientState::Uninitialized;
};
Expand Down
Loading

0 comments on commit d670a3f

Please sign in to comment.