Fix executor
authorSangyoon Jang <jeremy.jang@samsung.com>
Tue, 4 Mar 2025 10:16:54 +0000 (19:16 +0900)
committer장상윤/Tizen Platform Lab(SR)/삼성전자 <jeremy.jang@samsung.com>
Mon, 10 Mar 2025 05:52:24 +0000 (14:52 +0900)
- Move logic of AppControlSender into AppControlExectutor
- Add ActionResultHandler

Signed-off-by: Sangyoon Jang <jeremy.jang@samsung.com>
src/action/action_request_handler.cc
src/action/action_request_handler.hh
src/action/app_control_executor.cc
src/action/app_control_executor.hh
src/action/app_control_sender.cc [deleted file]
src/action/app_control_sender.hh [deleted file]
src/action/request_handler.hh
src/action/service.cc
src/common/action_executor.hh
src/common/action_result_handler.hh [new file with mode: 0644]

index 12dbb4b41de000407cbd507a0442f12b5c90c73c..aa8483c87058163211f56c8423c8f432b6b88d5d 100644 (file)
@@ -20,6 +20,7 @@
 #include "action/app_control_executor.hh"
 #include "action/service.hh"
 #include "action/sqlite_db.hh"
+#include "common/action_executor.hh"
 #include "common/utils/logging.hh"
 
 namespace action {
@@ -45,21 +46,30 @@ void ActionRequestHandler::OnGetAction(std::string id) {
   db->GetAction(id);
 }
 
-void ActionRequestHandler::OnGetActionId(std::string user_description, int top_k,
-    float search_threshold) {
+void ActionRequestHandler::OnGetActionId(std::string user_description,
+    int top_k, float search_threshold) {
 }
 
-void ActionRequestHandler::OnExecute(common::ActionModel& model) {
-  LOG(DEBUG) << "OnExecute action : " << model.GetActionId() << ", appid : " << model.GetAppId();
+void ActionRequestHandler::OnExecute(std::string instance,
+    common::ActionModel& model) {
+  LOG(DEBUG) << "OnExecute action : " << model.GetActionId()
+      << ", appid : " << model.GetAppId();
 
   if (model.GetType() == common::ActionType::AppControl) {
     LOG(DEBUG) << "execute appcontrol";
 
-    AppControlExecutor executor;
-    executor.Execute(model);
+    auto executor = std::make_unique<AppControlExecutor>(instance, model);
+    executor->SetResultHandler(this);
+    executor->Execute(model);
+    executors_.emplace_back(std::move(executor));
   } else if (model.GetType() == common::ActionType::Plugin) {
     LOG(DEBUG) << "execute plugin";
   }
 }
 
+void ActionRequestHandler::OnResult(std::string executor_id,
+    std::string result) {
+  LOG(DEBUG) << "OnResult : " << result << ", from : " << executor_id;
+}
+
 }  // namespace action
index 5ec8d12d5f38e0d1a3d4ab07c8c3bcebeb4c1e30..ea4529095dc5de53bc8684bf3afa024c6d1c52d7 100644 (file)
 #ifndef ACTION_ACTION_REQUEST_HANDLER_H_
 #define ACTION_ACTION_REQUEST_HANDLER_H_
 
+#include <memory>
 #include <vector>
 
 #include "action/request_handler.hh"
 #include "action/tizen_action_service_stub.h"
+#include "common/action_executor.hh"
 #include "common/action_model.h"
-
-using Action = rpc_port::tizen_action_service_stub::Action;
+#include "common/action_result_handler.hh"
 
 namespace action {
 
-class ActionRequestHandler : public IRequestHandler {
+class ActionRequestHandler : public IRequestHandler, public IActionResultHandler {
  public:
   ActionRequestHandler();
   ~ActionRequestHandler();
@@ -37,10 +38,13 @@ class ActionRequestHandler : public IRequestHandler {
   void OnGetAction(std::string id) override;
   void OnGetActionId(std::string user_description, int top_k,
       float search_threshold) override;
-  void OnExecute(common::ActionModel& model) override;
+  void OnExecute(std::string instance, common::ActionModel& model) override;
+
+  void OnResult(std::string executor_id, std::string result) override;
 
  private:
   rpc_port::tizen_action_service_stub::stub::ActionService service_;
+  std::vector<std::unique_ptr<common::AbstractActionExecutor>> executors_;
 };
 
 }  // namespace action
index d8365102948f1bbac121d7386377820387630902..e3c106b737597b6dd8283645c1cdf899a2fbaa36 100644 (file)
@@ -21,7 +21,6 @@
 
 #include <memory>
 
-#include "action/app_control_sender.hh"
 #include "common/action_model.h"
 #include "common/utils/logging.hh"
 
@@ -42,34 +41,86 @@ app_control_h ConvertToAppControl(const common::ActionModel& action_model) {
   auto params = action_model.GetParameters();
 
   for (auto const& iter : params)
-    LOG(DEBUG) << "param name : " << iter.GetName() << ", val : " << iter.GetValue() << ", type : " << static_cast<int>(iter.GetType());
+    LOG(DEBUG) << "param name : " << iter.GetName() << ", val : "
+        << iter.GetValue() << ", type : " << static_cast<int>(iter.GetType());
 
   return handle;
 }
 
+void ResultCb(app_control_h request, app_control_error_e result,
+    void* user_data) {
+  LOG(DEBUG) << "ResultCb: " << result;
+}
+
+void ReplyCb(app_control_h request, app_control_h reply,
+    app_control_result_e result, void* user_data) {
+  // TODO: return reply to caller
+  auto on_reply = static_cast<action::AppControlExecutor*>(user_data);
+
+  // TODO: return what
+  std::string result_str = "result";
+  on_reply->OnAppControlReply(result_str);
+}
+
 }  // namespace
 
 namespace action {
 
-int AppControlExecutor::Execute(const common::ActionModel& model) {
-  LOG(ERROR) << "AppControl Execute : " << model.GetAppId();
+AppControlExecutor::AppControlExecutor(std::string id,
+    const common::ActionModel& model)
+    : AbstractActionExecutor(std::move(id)) {
+  model_ = model;
+  app_control_ = ConvertToAppControl(model);
+}
 
-  app_control_h app_control = ConvertToAppControl(model);
+AppControlExecutor::~AppControlExecutor() {
+  int ret = app_control_destroy(app_control_);
+  if (ret != APP_CONTROL_ERROR_NONE)
+    LOG(ERROR) << "Failed to destroy app_control: " << ret;
+}
 
-  std::unique_ptr<AppControlSender> sender =
-      std::make_unique<AppControlSender>(app_control, this);
+int AppControlExecutor::Execute(const common::ActionModel& model) {
+  LOG(ERROR) << "AppControl Execute : " << model.GetAppId();
 
-  // TODO: handle result?
-  sender->Send();
+  AddExtraData(model);
 
-  senders_.emplace_back(std::move(sender));
+  if (!SendAppControl()) {
+    LOG(ERROR) << "Failed to send app control";
+    return -1;
+  }
 
   return 0;
 }
 
-void AppControlExecutor::OnReply(const std::string& reply) {
+void AppControlExecutor::OnAppControlReply(const std::string& reply) {
   // TODO: Handle reply
-  LOG(DEBUG) << reply;
+  LOG(DEBUG) << "reply from instance: " << GetId() << ", reply: " << reply;
+  result_handler_->OnResult(GetId(), reply);
+}
+
+void AppControlExecutor::AddExtraData(const common::ActionModel& model) {
+  for (auto const& iter : model.GetParameters()) {
+    auto key = iter.GetName();
+    auto val = iter.GetValue();
+    int ret = app_control_add_extra_data(app_control_, key.c_str(),
+        val.c_str());
+    if (ret != APP_CONTROL_ERROR_NONE)
+      LOG(ERROR) << "Failed to add extra data: " << ret;
+  }
+}
+
+bool AppControlExecutor::SendAppControl() {
+  int ret = app_control_send_launch_request_async(app_control_, ResultCb,
+      ReplyCb, static_cast<void*>(this));
+  if (ret != APP_CONTROL_ERROR_NONE) {
+    LOG(ERROR) << "Failed to send launch request: " << ret;
+    return false;
+  }
+  return true;
+}
+
+void AppControlExecutor::SetResultHandler(IActionResultHandler* handler) {
+  result_handler_ = handler;
 }
 
 }  // namespace action
index d33083ee4f6d8e3b850baee611281e1b976ac807..dbb547ecb4f6f13339abed7959a3acc83c55e23a 100644 (file)
 #ifndef ACTION_APP_CONTROL_EXECUTOR_HH_
 #define ACTION_APP_CONTROL_EXECUTOR_HH_
 
-#include <memory>
+#include <app_control.h>
 
-#include "action/app_control_sender.hh"
 #include "common/action_executor.hh"
 #include "common/action_model.h"
+#include "common/action_result_handler.hh"
 
 namespace action {
 
-class AppControlExecutor : public common::IActionExecutor {
+class AppControlExecutor : public common::AbstractActionExecutor {
  public:
-  AppControlExecutor() = default;
-  ~AppControlExecutor() = default;
+  explicit AppControlExecutor(std::string id, const common::ActionModel& model);
+  ~AppControlExecutor();
   int Execute(const common::ActionModel& model) override;
-  void OnReply(const std::string& reply) override;
+
+  void OnAppControlReply(const std::string& reply);
+  void SetResultHandler(IActionResultHandler* result_handler);
 
  private:
-  std::vector<std::unique_ptr<AppControlSender>> senders_;
+  void AddExtraData(const common::ActionModel& model);
+  bool SendAppControl();
+
+  common::ActionModel model_;
+  app_control_h app_control_;
+  IActionResultHandler* result_handler_;
 };
 
 }  // namespace action
diff --git a/src/action/app_control_sender.cc b/src/action/app_control_sender.cc
deleted file mode 100644 (file)
index e8e7a69..0000000
+++ /dev/null
@@ -1,67 +0,0 @@
-/*
- * 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.
- */
-
-#include "action/app_control_sender.hh"
-
-#include <app_control.h>
-
-#include "common/utils/logging.hh"
-
-namespace {
-
-void ResultCb(app_control_h request, app_control_error_e result,
-    void* user_data) {
-  LOG(DEBUG) << "ResultCb: " << result;
-}
-
-void ReplyCb(app_control_h request, app_control_h reply,
-    app_control_result_e result, void* user_data) {
-  // TODO: return reply to caller
-  common::IActionExecutor* on_reply =
-      static_cast<common::IActionExecutor*>(user_data);
-
-  // TODO: return what
-  std::string result_str = "result";
-  on_reply->OnReply(result_str);
-}
-
-}  // namespace
-
-namespace action {
-
-AppControlSender::AppControlSender(app_control_h app_control,
-    common::IActionExecutor* executor)
-    : app_control_(app_control), executor_(executor) {
-}
-
-AppControlSender::~AppControlSender() {
-  int ret = app_control_destroy(app_control_);
-  if (ret != APP_CONTROL_ERROR_NONE)
-    LOG(ERROR) << "Failed to destroy app_control: " << ret;
-}
-
-bool AppControlSender::Send() {
-  int ret = app_control_send_launch_request_async(app_control_, ResultCb,
-      ReplyCb, static_cast<void*>(executor_));
-  if (ret != APP_CONTROL_ERROR_NONE) {
-    LOG(ERROR) << "Failed to send launch request: " << ret;
-    return false;
-  }
-
-  return true;
-}
-
-}  // namespace action
diff --git a/src/action/app_control_sender.hh b/src/action/app_control_sender.hh
deleted file mode 100644 (file)
index 262cea6..0000000
+++ /dev/null
@@ -1,40 +0,0 @@
-/*
- * 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_EXECUTOR_APP_CONTROL_SENDER_HH_
-#define ACTION_EXECUTOR_APP_CONTROL_SENDER_HH_
-
-#include <app_control.h>
-
-#include "common/action_executor.hh"
-
-namespace action {
-
-class AppControlSender {
- public:
-  AppControlSender(app_control_h app_control,
-      common::IActionExecutor* executor);
-  ~AppControlSender();
-  bool Send();
-
- private:
-  app_control_h app_control_;
-  common::IActionExecutor* executor_;
-};
-
-}  // action
-
-#endif  // ACTION_EXECUTOR_APP_CONTROL_SENDER_HH_
index d271133dac397e9ff91732d54f80069c736f5f69..153eda7bf713d863ac7a1022c10095e41c2c3ebd 100644 (file)
@@ -21,6 +21,9 @@
 
 #include "common/action_model.h"
 
+// TODO: dependency?
+#include "action/tizen_action_service_stub.h"
+
 namespace action {
 
 class IRequestHandler {
@@ -30,7 +33,7 @@ class IRequestHandler {
   virtual void OnGetAction(std::string id) = 0;
   virtual void OnGetActionId(std::string user_description, int top_k,
       float search_threshold) = 0;
-  virtual void OnExecute(common::ActionModel& model) = 0;
+  virtual void OnExecute(std::string instance, common::ActionModel& model) = 0;
 };
 
 }  // namespace action
index 53ab4cdde1ae6fbda236b9862d144e017f37648d..a523942f6a6918e6628e7df08465d8855e92faef 100644 (file)
@@ -153,7 +153,7 @@ int Service::Execute(rs::Action action) {
   }
   model.SetParameters(actionparams);
 
-  handler_.OnExecute(model);
+  handler_.OnExecute(GetInstance(), model);
 
   return 0;
 }
index 8d30f487fc01dd6f890b17ae6892f9ea6dce474f..e810ff628308978fce54d5a58c5e9a7ca1c477a8 100644 (file)
 #define COMMON_ACTION_EXECUTOR_HH_
 
 #include <string>
+#include <utility>
 
 #include "common/action_model.h"
 
 namespace common {
 
-class IActionExecutor {
+class AbstractActionExecutor {
  public:
-  virtual ~IActionExecutor() = default;
+  explicit AbstractActionExecutor(std::string id) : id_(std::move(id)) {}
+  virtual ~AbstractActionExecutor() = default;
   virtual int Execute(const ActionModel& model) = 0;
-  virtual void OnReply(const std::string& result) = 0;
+  virtual std::string GetId() { return id_; }
+
+ private:
+  std::string id_;
 };
 
 }  // namespace common
diff --git a/src/common/action_result_handler.hh b/src/common/action_result_handler.hh
new file mode 100644 (file)
index 0000000..c6cc429
--- /dev/null
@@ -0,0 +1,26 @@
+/*
+ * 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 COMMON_ACTION_RESULT_HANDLER_HH_
+#define COMMON_ACTION_RESULT_HANDLER_HH_
+
+class IActionResultHandler {
+ public:
+  virtual ~IActionResultHandler() = default;
+  virtual void OnResult(std::string executor_id, std::string result) = 0;
+};
+
+#endif  // COMMON_ACTION_RESULT_HANDLER_HH_