LOG(ERROR) << "DefaultActionExecutor::Execute";
return -1;
}
- void SetResultHandler(common::IResultHandler* handler) override {
- LOG(ERROR) << "DefaultActionExecutor::SetResultHandler";
- }
};
std::unique_ptr<common::AbstractActionExecutor>
* limitations under the License.
*/
+#include <algorithm>
#include <string>
#include "action/action_executor_factory.hh"
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
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;
private:
rpc_port::tizen_action_service_stub::stub::ActionService service_;
std::vector<std::unique_ptr<common::AbstractActionExecutor>> executors_;
+ std::map<std::string, RequestReplyHandler*> reply_handlers_;
};
} // namespace action
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) {
return true;
}
-void AppControlExecutor::SetResultHandler(common::IResultHandler* handler) {
- result_handler_ = handler;
-}
-
} // namespace action
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);
common::ActionModel model_;
app_control_h app_control_;
- common::IResultHandler* result_handler_ = nullptr;
};
} // namespace action
}
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() {
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:
#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;
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
--- /dev/null
+/*
+ * 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 <string>
+
+namespace action {
+
+class RequestReplyHandler {
+ public:
+ virtual ~RequestReplyHandler() = default;
+ virtual void SendRequestReply(const std::string& reply) = 0;
+};
+
+} // namespace action
+
+#endif // ACTION_REQUEST_REPLY_HANDLER_HH_
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<rpc::Action> Service::ListActions() {
return handler_.OnListActions();
}
int Service::RegisterActionReplyCb(std::unique_ptr<ActionReplyCb> 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
#include <vector>
#include "action/request_handler.hh"
+#include "action/request_reply_handler.hh"
#include "action/tizen_action_service_stub.h"
using ActionReplyCb =
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:
Service(std::string sender, std::string instance, IRequestHandler& handler);
~Service();
- void OnCreate() override {}
- void OnTerminate() override {}
+ void OnCreate() override;
+ void OnTerminate() override;
std::vector<rpc::Action> ListActions() override;
rpc::Action GetAction(std::string action_id) override;
std::vector<rpc::VectorDbResult> GetActionId(std::string user_description,
int Execute(rpc::Action action) override;
int RegisterActionReplyCb(std::unique_ptr<ActionReplyCb> cb) override;
bool UnregisterActionReplyCb(int id) override;
+ void SendRequestReply(const std::string& reply) override;
private:
IRequestHandler& handler_;
- std::map<std::string, std::unique_ptr<ActionReplyCb>> reply_cbs_;
+ std::vector<std::unique_ptr<ActionReplyCb>> reply_cbs_;
};
} // namespace action
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