Skip to content

Commit

Permalink
Templatization
Browse files Browse the repository at this point in the history
  • Loading branch information
sharadb-amazon committed Dec 15, 2023
1 parent 430c3c1 commit 6c5ab20
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 80 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,71 +27,8 @@ void ContentLauncherCluster::LaunchURL(
SuccessCallbackType<chip::app::Clusters::ContentLauncher::Commands::LaunchURL::Type::ResponseType> successCb,
FailureCallbackType failureCb, const chip::Optional<uint16_t> & timedInvokeTimeoutMs)
{
memory::Strong<core::Endpoint> endpoint = this->GetEndpoint().lock();
if (endpoint)
{
LaunchURLContext * interactionContext =
new LaunchURLContext(endpoint, request, context, successCb, failureCb, timedInvokeTimeoutMs);

endpoint->GetCastingPlayer()->FindOrEstablishSession(
interactionContext,
// FindOrEstablishSession success handler
[](void * context, chip::Messaging::ExchangeManager & exchangeMgr, const chip::SessionHandle & sessionHandle) {
LaunchURLContext * _interactionContext = static_cast<LaunchURLContext *>(context);
ChipLogProgress(AppServer, "ContentLauncherCluster::LaunchURL found or established session");

// Initiate interaction
support::MediaClusterBase cluster(exchangeMgr, sessionHandle, _interactionContext->mEndpoint->GetId());
CHIP_ERROR err = cluster.template InvokeCommand(
_interactionContext->mRequest, _interactionContext,
// Interaction success handler
[](void * context,
const chip::app::Clusters::ContentLauncher::Commands::LauncherResponse::DecodableType & response) {
LaunchURLContext * _interactionContext = static_cast<LaunchURLContext *>(context);
ChipLogProgress(AppServer, "ContentLauncherCluster::LaunchURL success");
_interactionContext->mSuccessCb(_interactionContext->mClientContext, response);
delete _interactionContext;
},
// Interaction failure handler
[](void * context, CHIP_ERROR error) {
LaunchURLContext * _interactionContext = static_cast<LaunchURLContext *>(context);
ChipLogError(
AppServer,
"ContentLauncherCluster::LaunchURL failure response on EndpointId: %d with error: %" CHIP_ERROR_FORMAT,
_interactionContext->mEndpoint->GetId(), error.Format());
_interactionContext->mFailureCb(_interactionContext->mClientContext, error);
delete _interactionContext;
},
_interactionContext->mTimedInvokeTimeoutMs);

// error in initiating the interaction
if (err != CHIP_NO_ERROR)
{
ChipLogError(AppServer,
"ContentLauncherCluster::LaunchURL failure in starting interaction on EndpointId: %d with error: "
"%" CHIP_ERROR_FORMAT,
_interactionContext->mEndpoint->GetId(), err.Format());
_interactionContext->mFailureCb(_interactionContext->mClientContext, err);
delete _interactionContext;
}
},
// FindOrEstablishSession failure handler
[](void * context, const chip::ScopedNodeId & peerId, CHIP_ERROR error) {
LaunchURLContext * _interactionContext = static_cast<LaunchURLContext *>(context);
ChipLogError(
AppServer,
"ContentLauncherCluster::LaunchURL failure in retrieving session info for peerId.nodeId: 0x" ChipLogFormatX64
", peer.fabricIndex: %d with error: %" CHIP_ERROR_FORMAT,
ChipLogValueX64(peerId.GetNodeId()), peerId.GetFabricIndex(), error.Format());
_interactionContext->mFailureCb(_interactionContext->mClientContext, error);
delete _interactionContext;
});
}
else
{
ChipLogError(AppServer, "ContentLauncherCluster::LaunchURL failure in retrieving Endpoint");
failureCb(context, CHIP_ERROR_INCORRECT_STATE);
}
ContentLauncher_LaunchURL command(this->GetEndpoint());
command.Invoke(request, context, successCb, failureCb, timedInvokeTimeoutMs);
}

}; // namespace clusters
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,111 @@ template <typename ResponseType>
using SuccessCallbackType = std::function<void(void * context, const ResponseType & responseObject)>;
using FailureCallbackType = std::function<void(void * context, CHIP_ERROR err)>;

template <typename RequestType, typename ResponseType>
struct CommandContext
{
CommandContext(memory::Strong<core::Endpoint> endpoint, RequestType request, void * context,
SuccessCallbackType<ResponseType> successCb, FailureCallbackType failureCb,
const chip::Optional<uint16_t> & timedInvokeTimeoutMs) :
mSuccessCb(successCb),
mFailureCb(failureCb)
{
mEndpoint = endpoint;
mRequest = request;
mClientContext = context;
mTimedInvokeTimeoutMs = timedInvokeTimeoutMs;
}

memory::Strong<core::Endpoint> mEndpoint;
RequestType mRequest;
void * mClientContext;
SuccessCallbackType<ResponseType> mSuccessCb;
FailureCallbackType mFailureCb;
chip::Optional<uint16_t> mTimedInvokeTimeoutMs;
};

template <typename RequestType, typename ResponseType, typename CommandContext>
class Command
{
public:
Command(memory::Weak<core::Endpoint> endpoint) { this->mEndpoint = endpoint; }

void Invoke(RequestType request, void * context, SuccessCallbackType<ResponseType> successCb, FailureCallbackType failureCb,
const chip::Optional<uint16_t> & timedInvokeTimeoutMs)
{
memory::Strong<core::Endpoint> endpoint = this->GetEndpoint().lock();
if (endpoint)
{
CommandContext * commandContext =
new CommandContext(endpoint, request, context, successCb, failureCb, timedInvokeTimeoutMs);

endpoint->GetCastingPlayer()->FindOrEstablishSession(
commandContext,
// FindOrEstablishSession success handler
[](void * context, chip::Messaging::ExchangeManager & exchangeMgr, const chip::SessionHandle & sessionHandle) {
CommandContext * _commandContext = static_cast<CommandContext *>(context);
ChipLogProgress(AppServer, "<Command>::Invoke() Found or established session");

// Invoke command
support::MediaClusterBase cluster(exchangeMgr, sessionHandle, _commandContext->mEndpoint->GetId());
CHIP_ERROR err = cluster.template InvokeCommand(
_commandContext->mRequest, _commandContext,
// Command success handler
[](void * context,
const chip::app::Clusters::ContentLauncher::Commands::LauncherResponse::DecodableType & response) {
CommandContext * _commandContext = static_cast<CommandContext *>(context);
ChipLogProgress(AppServer, "<Command>::Invoke() response success");
_commandContext->mSuccessCb(_commandContext->mClientContext, response);
delete _commandContext;
},
// Command failure handler
[](void * context, CHIP_ERROR error) {
CommandContext * _commandContext = static_cast<CommandContext *>(context);
ChipLogError(AppServer,
"<Command>::Invoke() failure response on EndpointId: %d with error: "
"%" CHIP_ERROR_FORMAT,
_commandContext->mEndpoint->GetId(), error.Format());
_commandContext->mFailureCb(_commandContext->mClientContext, error);
delete _commandContext;
},
_commandContext->mTimedInvokeTimeoutMs);

// error in invoking the command
if (err != CHIP_NO_ERROR)
{
ChipLogError(AppServer,
"<Command>::Invoke() failure in invoking command on EndpointId: %d with error: "
"%" CHIP_ERROR_FORMAT,
_commandContext->mEndpoint->GetId(), err.Format());
_commandContext->mFailureCb(_commandContext->mClientContext, err);
delete _commandContext;
}
},
// FindOrEstablishSession failure handler
[](void * context, const chip::ScopedNodeId & peerId, CHIP_ERROR error) {
CommandContext * _commandContext = static_cast<CommandContext *>(context);
ChipLogError(AppServer,
"<Command>::Invoke() failure in retrieving session info for peerId.nodeId: "
"0x" ChipLogFormatX64 ", peer.fabricIndex: %d with error: %" CHIP_ERROR_FORMAT,
ChipLogValueX64(peerId.GetNodeId()), peerId.GetFabricIndex(), error.Format());
_commandContext->mFailureCb(_commandContext->mClientContext, error);
delete _commandContext;
});
}
else
{
ChipLogError(AppServer, "<Command>::Invoke() failure in retrieving Endpoint");
failureCb(context, CHIP_ERROR_INCORRECT_STATE);
}
}

protected:
memory::Weak<core::Endpoint> GetEndpoint() const { return mEndpoint.lock(); }
memory::Weak<core::Endpoint> mEndpoint;
};

// end of templates

class ContentLauncherCluster : public core::BaseCluster
{
public:
Expand All @@ -41,27 +146,23 @@ class ContentLauncherCluster : public core::BaseCluster
FailureCallbackType failureCb, const chip::Optional<uint16_t> & timedInvokeTimeoutMs);
};

struct LaunchURLContext
struct LaunchURLContext : public CommandContext<chip::app::Clusters::ContentLauncher::Commands::LaunchURL::Type,
chip::app::Clusters::ContentLauncher::Commands::LaunchURL::Type::ResponseType>
{
LaunchURLContext(memory::Strong<core::Endpoint> endpoint,
chip::app::Clusters::ContentLauncher::Commands::LaunchURL::Type request, void * context,
SuccessCallbackType<chip::app::Clusters::ContentLauncher::Commands::LaunchURL::Type::ResponseType> successCb,
FailureCallbackType failureCb, const chip::Optional<uint16_t> & timedInvokeTimeoutMs) :
mSuccessCb(successCb),
mFailureCb(failureCb)
{
mEndpoint = endpoint;
mRequest = request;
mClientContext = context;
mTimedInvokeTimeoutMs = timedInvokeTimeoutMs;
}
CommandContext(endpoint, request, context, successCb, failureCb, timedInvokeTimeoutMs)
{}
};

memory::Strong<core::Endpoint> mEndpoint;
chip::app::Clusters::ContentLauncher::Commands::LaunchURL::Type mRequest;
void * mClientContext;
SuccessCallbackType<chip::app::Clusters::ContentLauncher::Commands::LaunchURL::Type::ResponseType> mSuccessCb;
FailureCallbackType mFailureCb;
chip::Optional<uint16_t> mTimedInvokeTimeoutMs;
class ContentLauncher_LaunchURL
: public Command<chip::app::Clusters::ContentLauncher::Commands::LaunchURL::Type,
chip::app::Clusters::ContentLauncher::Commands::LaunchURL::Type::ResponseType, LaunchURLContext>
{
public:
ContentLauncher_LaunchURL(memory::Weak<core::Endpoint> endpoint) : Command(endpoint) {}
};

}; // namespace clusters
Expand Down

0 comments on commit 6c5ab20

Please sign in to comment.