From: Sangyoon Jang Date: Tue, 8 Apr 2025 05:11:56 +0000 (+0900) Subject: Implement action request reply X-Git-Tag: accepted/tizen/unified/20250514.114137~32 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=c151b60b9cabeae6e97b904194238fb305a448b8;p=platform%2Fcore%2Fappfw%2Ftizen-action.git Implement action request reply Signed-off-by: Sangyoon Jang --- diff --git a/src/action/action_executor_factory.cc b/src/action/action_executor_factory.cc index 965b700..9d34f9b 100644 --- a/src/action/action_executor_factory.cc +++ b/src/action/action_executor_factory.cc @@ -15,9 +15,6 @@ class DefaultActionExecutor : public common::AbstractActionExecutor { LOG(ERROR) << "DefaultActionExecutor::Execute"; return -1; } - void SetResultHandler(common::IResultHandler* handler) override { - LOG(ERROR) << "DefaultActionExecutor::SetResultHandler"; - } }; std::unique_ptr diff --git a/src/action/action_request_handler.cc b/src/action/action_request_handler.cc index 6a26867..e0635bb 100644 --- a/src/action/action_request_handler.cc +++ b/src/action/action_request_handler.cc @@ -14,6 +14,7 @@ * limitations under the License. */ +#include #include #include "action/action_executor_factory.hh" @@ -122,9 +123,33 @@ void ActionRequestHandler::OnExecute(const std::string& instance, executors_.emplace_back(std::move(executor)); } +void ActionRequestHandler::RegisterRequestReplyHandler(const std::string& requester, + RequestReplyHandler* handler) { + reply_handlers_.emplace(requester, handler); +} + +void ActionRequestHandler::UnregisterRequestReplyHandler(const std::string& requester) { + auto it = std::find_if(reply_handlers_.begin(), reply_handlers_.end(), + [requester](const auto& iterator) { + return iterator.first == requester; + }); + if (it != reply_handlers_.end()) + reply_handlers_.erase(it); + else + LOG(WARNING) << "No handler found for requester :" << requester; +} + void ActionRequestHandler::OnResult(const std::string& requester, const std::string& result) { LOG(DEBUG) << "OnResult : " << result << ", from : " << requester; + auto it = std::find_if(reply_handlers_.begin(), reply_handlers_.end(), + [requester](const auto& iterator) { + return iterator.first == requester; + }); + if (it!= reply_handlers_.end()) + it->second->SendRequestReply(result); + else + LOG(WARNING) << "No handler found for requester :" << requester; } } // namespace action diff --git a/src/action/action_request_handler.hh b/src/action/action_request_handler.hh index 023c110..67f4ca6 100644 --- a/src/action/action_request_handler.hh +++ b/src/action/action_request_handler.hh @@ -41,6 +41,9 @@ class ActionRequestHandler : public IRequestHandler, common::IResultHandler { float search_threshold) override; void OnExecute(const std::string& instance, rpc::Action& model) override; + void RegisterRequestReplyHandler(const std::string& requester, + RequestReplyHandler* handler) override; + void UnregisterRequestReplyHandler(const std::string& requester) override; void OnResult(const std::string& requester, const std::string& result) override; @@ -48,6 +51,7 @@ class ActionRequestHandler : public IRequestHandler, common::IResultHandler { private: rpc_port::tizen_action_service_stub::stub::ActionService service_; std::vector> executors_; + std::map reply_handlers_; }; } // namespace action diff --git a/src/action/app_control_executor.cc b/src/action/app_control_executor.cc index 5e277b4..f547236 100644 --- a/src/action/app_control_executor.cc +++ b/src/action/app_control_executor.cc @@ -95,7 +95,7 @@ int AppControlExecutor::Execute(const common::ActionModel& model) { void AppControlExecutor::OnAppControlReply(const std::string& reply) { // TODO: Handle reply LOG(DEBUG) << "reply from instance: " << GetId() << ", reply: " << reply; - result_handler_->OnResult(GetId(), reply); + NotifyResult(reply); } void AppControlExecutor::AddExtraData(const common::ActionModel& model) { @@ -119,8 +119,4 @@ bool AppControlExecutor::SendAppControl() { return true; } -void AppControlExecutor::SetResultHandler(common::IResultHandler* handler) { - result_handler_ = handler; -} - } // namespace action diff --git a/src/action/app_control_executor.hh b/src/action/app_control_executor.hh index aa35822..5c4f75d 100644 --- a/src/action/app_control_executor.hh +++ b/src/action/app_control_executor.hh @@ -31,7 +31,6 @@ class AppControlExecutor : public common::AbstractActionExecutor { int Execute(const common::ActionModel& model) override; void OnAppControlReply(const std::string& reply); - void SetResultHandler(common::IResultHandler* handler) override; private: void AddExtraData(const common::ActionModel& model); @@ -39,7 +38,6 @@ class AppControlExecutor : public common::AbstractActionExecutor { common::ActionModel model_; app_control_h app_control_; - common::IResultHandler* result_handler_ = nullptr; }; } // namespace action diff --git a/src/action/plugin_executor.cc b/src/action/plugin_executor.cc index 5b2bd98..cc98c2a 100644 --- a/src/action/plugin_executor.cc +++ b/src/action/plugin_executor.cc @@ -79,13 +79,8 @@ int PluginExecutor::Execute(const common::ActionModel& model) { } void PluginExecutor::OnResultReceived(const std::string& result) { - // TODO: Handle result LOG(DEBUG) << "result from instance: " << GetId() << ", result: " << result; - result_handler_->OnResult(GetId(), result); -} - -void PluginExecutor::SetResultHandler(common::IResultHandler* handler) { - result_handler_ = handler; + NotifyResult(result); } bool PluginExecutor::Connect() { diff --git a/src/action/plugin_executor.hh b/src/action/plugin_executor.hh index ac9777d..aa4c3d4 100644 --- a/src/action/plugin_executor.hh +++ b/src/action/plugin_executor.hh @@ -32,7 +32,6 @@ class PluginExecutor : public common::AbstractActionExecutor, explicit PluginExecutor(std::string instance); ~PluginExecutor(); int Execute(const common::ActionModel& model) override; - void SetResultHandler(common::IResultHandler* handler) override; void OnResultReceived(const std::string& result); private: diff --git a/src/action/request_handler.hh b/src/action/request_handler.hh index 7311fdf..b26bf20 100644 --- a/src/action/request_handler.hh +++ b/src/action/request_handler.hh @@ -21,6 +21,7 @@ #include "common/action_model.h" +#include "action/request_reply_handler.hh" #include "action/tizen_action_service_stub.h" namespace rpc = rpc_port::tizen_action_service_stub; @@ -35,6 +36,9 @@ class IRequestHandler { virtual void OnGetActionId(const std::string& user_description, int top_k, float search_threshold) = 0; virtual void OnExecute(const std::string& requester, rpc::Action& model) = 0; + virtual void RegisterRequestReplyHandler(const std::string& requester, + RequestReplyHandler* handler) = 0; + virtual void UnregisterRequestReplyHandler(const std::string& requester) = 0; }; } // namespace action diff --git a/src/action/request_reply_handler.hh b/src/action/request_reply_handler.hh new file mode 100644 index 0000000..8dfa3fe --- /dev/null +++ b/src/action/request_reply_handler.hh @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2025 Samsung Electronics Co., Ltd All Rights Reserved + * + * 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. + */ + +#ifndef ACTION_REQUEST_REPLY_HANDLER_HH_ +#define ACTION_REQUEST_REPLY_HANDLER_HH_ + +#include + +namespace action { + +class RequestReplyHandler { + public: + virtual ~RequestReplyHandler() = default; + virtual void SendRequestReply(const std::string& reply) = 0; +}; + +} // namespace action + +#endif // ACTION_REQUEST_REPLY_HANDLER_HH_ diff --git a/src/action/service.cc b/src/action/service.cc index 77f8334..11561c3 100644 --- a/src/action/service.cc +++ b/src/action/service.cc @@ -34,6 +34,16 @@ Service::Service(std::string sender, std::string instance, Service::~Service() { } +void Service::OnCreate() { + LOG(DEBUG) << "OnCreate: " << GetInstance(); + handler_.RegisterRequestReplyHandler(GetInstance(), this); +} + +void Service::OnTerminate() { + LOG(DEBUG) << "OnTerminate: " << GetInstance(); + handler_.UnregisterRequestReplyHandler(GetInstance()); +} + std::vector Service::ListActions() { return handler_.OnListActions(); } @@ -58,23 +68,27 @@ int Service::Execute(rpc::Action action) { int Service::RegisterActionReplyCb(std::unique_ptr cb) { int id = cb->GetSeqId(); - reply_cbs_.emplace(GetInstance(), std::move(cb)); + reply_cbs_.emplace_back(std::move(cb)); return id; } bool Service::UnregisterActionReplyCb(int id) { auto it = std::find_if(reply_cbs_.begin(), reply_cbs_.end(), [id](const auto& iterator) { - return iterator.second->GetSeqId() == id; + return iterator->GetSeqId() == id; }); if (it != reply_cbs_.end()) { reply_cbs_.erase(it); return true; } else { - LOG(ERROR) << "Not found callback for unregistration for instance " - << GetInstance(); + LOG(ERROR) << "Not found callback for unregistration for id: " << id; return false; } } +void Service::SendRequestReply(const std::string& reply) { + for (const auto& cb : reply_cbs_) + cb->Invoke(reply); +} + } // namespace action diff --git a/src/action/service.hh b/src/action/service.hh index ba39e1e..85efc65 100644 --- a/src/action/service.hh +++ b/src/action/service.hh @@ -22,6 +22,7 @@ #include #include "action/request_handler.hh" +#include "action/request_reply_handler.hh" #include "action/tizen_action_service_stub.h" using ActionReplyCb = @@ -31,7 +32,8 @@ namespace action { namespace rpc = rpc_port::tizen_action_service_stub; -class Service : public rpc::stub::ActionService::ServiceBase { +class Service : public rpc::stub::ActionService::ServiceBase, + public RequestReplyHandler { public: class Factory : public rpc::stub::ActionService::ServiceBase::Factory { public: @@ -49,8 +51,8 @@ class Service : public rpc::stub::ActionService::ServiceBase { Service(std::string sender, std::string instance, IRequestHandler& handler); ~Service(); - void OnCreate() override {} - void OnTerminate() override {} + void OnCreate() override; + void OnTerminate() override; std::vector ListActions() override; rpc::Action GetAction(std::string action_id) override; std::vector GetActionId(std::string user_description, @@ -58,10 +60,11 @@ class Service : public rpc::stub::ActionService::ServiceBase { int Execute(rpc::Action action) override; int RegisterActionReplyCb(std::unique_ptr cb) override; bool UnregisterActionReplyCb(int id) override; + void SendRequestReply(const std::string& reply) override; private: IRequestHandler& handler_; - std::map> reply_cbs_; + std::vector> reply_cbs_; }; } // namespace action diff --git a/src/common/action_executor.hh b/src/common/action_executor.hh index 08878d9..b1e4eea 100644 --- a/src/common/action_executor.hh +++ b/src/common/action_executor.hh @@ -30,11 +30,15 @@ class AbstractActionExecutor { explicit AbstractActionExecutor(std::string id) : id_(std::move(id)) {} virtual ~AbstractActionExecutor() = default; virtual int Execute(const ActionModel& model) = 0; - virtual std::string GetId() { return id_; } - virtual void SetResultHandler(IResultHandler* handler) = 0; + std::string GetId() { return id_; } + void SetResultHandler(IResultHandler* handler) { handler_ = handler; } + void NotifyResult(const std::string& result) { + handler_->OnResult(id_, result); + } private: std::string id_; + IResultHandler* handler_; }; } // namespace common