Fix api implementation
authorSangyoon Jang <jeremy.jang@samsung.com>
Mon, 12 May 2025 07:23:37 +0000 (16:23 +0900)
committer장상윤/Tizen Platform Lab(SR)/삼성전자 <jeremy.jang@samsung.com>
Tue, 13 May 2025 05:44:41 +0000 (14:44 +0900)
- Use connector class as api handle.
- Pass execution id on user callback.

Signed-off-by: Sangyoon Jang <jeremy.jang@samsung.com>
src/api/api_stub.cc
src/api/connector.cc
src/api/connector.hh

index 0dafbd17a73628b11ec1933c3abf5b28fb99cc5b..6d40ec0f8e9252a030fa623703c32c38faa399dd 100644 (file)
  * limitations under the License.
  */
 
-#include "api/action.h"
+#include <string>
 
-#include "connector.hh"
+#include "api/action.h"
+#include "api/connector.hh"
+#include "api/tizen_action_service_proxy.h"
 #include "common/action_model.h"
 #include "common/action_schema.h"
 #include "common/action_parameter.h"
@@ -35,9 +37,7 @@ class Reply : public api::rpc::proxy::ActionService::ActionReplyCb {
     : cb_(cb), user_data_(user_data) {}
 
   void OnReceived(int execution_id, std::string result) {
-    LOG(WARNING) << "@@@ reply OnReceived : " << result;
-
-    cb_(100, result.c_str(), user_data_);
+    cb_(execution_id, result.c_str(), user_data_);
   }
 
  private:
@@ -45,167 +45,86 @@ class Reply : public api::rpc::proxy::ActionService::ActionReplyCb {
   void* user_data_;
 };
 
-class ActionClient {
- public:
-  ActionClient(std::unique_ptr<api::Connector> connector)
-    : connector_(std::move(connector)) {}
-
-  api::rpc::ActionSchema GetAction(std::string name) {
-    LOG(WARNING) << "ActionClient GetAction";
-
-    auto* conn = connector_->GetConnection();
-    if (conn == nullptr) {
-      LOG(WARNING) << "get action conn null";
-      throw std::invalid_argument("connection is nullptr");
-    }
-
-    auto action_schema = conn->GetAction(name);
-
-    return action_schema;
-  }
-
-  std::vector<api::rpc::ActionSchema> ListActions() {
-    LOG(WARNING) << "ActionClient ListActions";
-
-    auto* conn = connector_->GetConnection();
-    if (conn == nullptr) {
-      LOG(WARNING) << "foreach action conn null";
-      throw std::invalid_argument("connection is nullptr");
-    }
-
-    auto actions = conn->ListActions();
-
-    return actions;
-  };
-
-  int Execute(std::string param, action_result_cb cb, void* user_data) {
-    LOG(WARNING) << "ActionClient Execute";
-
-    auto* conn = connector_->GetConnection();
-    if (conn == nullptr) {
-      LOG(WARNING) << "get action conn null";
-      throw std::invalid_argument("connection is nullptr");
-    }
-
-    common::ActionModel model_(std::move(param));
-    api::rpc::ActionModel action;
-    action.Setaction_id(model_.GetId());
-    action.Setjson(model_.GetJson());
-
-    cb_id_ = conn->RegisterActionReplyCb(std::make_unique<Reply>(cb, user_data));
-    int ret = conn->Execute(action);
-
-    LOG(WARNING) << "execute cb : " << std::to_string(cb_id_) << ", ret : " << std::to_string(ret);
-
-    return ret;
-  }
-
-  void Dispose() {
-    LOG(WARNING) << "ActionClient Dispose";
-
-    if (cb_id_ > -1) {
-      LOG(WARNING) << "unregister replycb";
-
-      auto* conn = connector_->GetConnection();
-      if (conn == nullptr) {
-        LOG(WARNING) << "get action conn null";
-        throw std::invalid_argument("connection is nullptr");
-      }
-
-      conn->UnregisterActionReplyCb(cb_id_);
-    }
-  }
-
- private:
-  std::unique_ptr<api::Connector> connector_;
-  int cb_id_ = -1;
-};
-
 }
 
 API int action_client_create(action_client_h* client) {
-  LOG(WARNING) << "action_client_create";
-
   if (client == nullptr) {
-    LOG(WARNING) << "Invalid Parameter";
+    LOG(ERROR) << "Invalid Parameter";
     return ACTION_ERROR_INVALID_PARAMETER;
   }
 
-  auto connector = std::make_unique<api::Connector>();
-  ActionClient* client_ = new (std::nothrow) ActionClient(std::move(connector));
-  if (client_ == nullptr) {
-    LOG(WARNING) << "Out-of-memory";
+  auto con = new (std::nothrow) api::Connector();
+  if (!con) {
+    LOG(ERROR) << "Out-of-memory";
     return ACTION_ERROR_OUT_OF_MEMORY;
   }
 
-  *client = static_cast<action_client_h>(client_);
+  if (!con->Connect()) {
+    LOG(ERROR) << "Failed to connect to action service";
+    delete con;
+    return ACTION_ERROR_IO_ERROR;
+  }
+
+  *client = static_cast<action_client_h>(con);
 
   return ACTION_ERROR_NONE;
 }
 
 API int action_client_destroy(action_client_h client) {
-  LOG(WARNING) << "action_client_destroy";
-
   if (client == nullptr) {
-    LOG(WARNING) << "Invalid Parameter";
+    LOG(ERROR) << "Invalid Parameter";
     return ACTION_ERROR_INVALID_PARAMETER;
   }
 
-  ActionClient* client_ = static_cast<ActionClient*>(client);
-  client_->Dispose();
-  delete client_;
+  auto con = static_cast<api::Connector*>(client);
+  // TODO: need to unregister all callbacks
+  // auto proxy = con->GetProxy();
+  // proxy->UnregisterAll();
+  delete con;
 
   return ACTION_ERROR_NONE;
 }
 
 API int action_client_get_action(action_client_h client,
     const char* name, action_h* action) {
-  LOG(WARNING) << "action_client_get_action";
-
   if (client == nullptr || name == nullptr || action == nullptr) {
     LOG(WARNING) << "Invalid Parameter";
     return ACTION_ERROR_INVALID_PARAMETER;
   }
 
-  ActionClient* client_ = static_cast<ActionClient*>(client);
-  try {
-    auto action_schema = client_->GetAction(name);
-
-    common::ActionSchema* schema = new (std::nothrow) common::ActionSchema(action_schema.Getjson());
+  auto con = static_cast<api::Connector*>(client);
+  auto proxy = con->GetProxy();
+  auto action_schema = proxy->GetAction(name);
+  // TODO: check action_schema is exist
+  // return ACTION_ERROR_NO_SUCH_ACTION
 
-    *action = static_cast<action_h>(schema);
-  } catch (...) {
-    LOG(WARNING) << "get action exception";
-    return -1;
+  common::ActionSchema* schema =
+      new (std::nothrow) common::ActionSchema(action_schema.Getjson());
+  if (!schema) {
+    LOG(ERROR) << "Out-of-memory";
+    return ACTION_ERROR_OUT_OF_MEMORY;
   }
 
+  *action = static_cast<action_h>(schema);
+
   return ACTION_ERROR_NONE;
 }
 
 API int action_client_foreach_action(action_client_h client,
     action_foreach_action_cb cb, void* user_data) {
-  LOG(WARNING) << "action_client_foreach_action";
-
   if (client == nullptr || cb == nullptr) {
-    LOG(WARNING) << "Invalid Parameter";
+    LOG(ERROR) << "Invalid Parameter";
     return ACTION_ERROR_INVALID_PARAMETER;
   }
 
-  ActionClient* client_ = static_cast<ActionClient*>(client);
-  try {
-    auto actions = client_->ListActions();
-
-    for (auto action : actions) {
-      common::ActionSchema schema(action.Getjson());
-
-      if (!cb(static_cast<action_h>(&schema), user_data)) {
-        LOG(WARNING) << "callback returns false";
-        break;
-      }
-    }
-  } catch (...) {
-    LOG(WARNING) << "exception foreach action";
-    return -1;
+  auto con = static_cast<api::Connector*>(client);
+  auto proxy = con->GetProxy();
+  auto actions = proxy->ListActions();
+
+  for (auto action : actions) {
+    common::ActionSchema schema(action.Getjson());
+    if (!cb(static_cast<action_h>(&schema), user_data))
+      break;
   }
 
   return ACTION_ERROR_NONE;
@@ -213,57 +132,63 @@ API int action_client_foreach_action(action_client_h client,
 
 API int action_client_execute(action_client_h client, const char* param,
     action_result_cb cb, void* user_data, int* execution_id) {
-  LOG(WARNING) << "action_client_execute";
   if (client == nullptr || param == nullptr) {
     LOG(WARNING) << "Invalid Parameter";
     return ACTION_ERROR_INVALID_PARAMETER;
   }
 
-  ActionClient* client_ = static_cast<ActionClient*>(client);
-  try {
-    client_->Execute(param, cb, user_data);
-  } catch (...) {
-    LOG(WARNING) << "execute exception3";
-    return -1;
+  auto con = static_cast<api::Connector*>(client);
+  auto proxy = con->GetProxy();
+
+  common::ActionModel model_(std::move(param));
+  api::rpc::ActionModel action;
+  // TODO: remove Setaction_id, Setjson
+  action.Setaction_id(model_.GetId());
+  action.Setjson(model_.GetJson());
+
+  // TODO: unregister cb using returned cb_id
+  proxy->RegisterActionReplyCb(std::make_unique<Reply>(cb, user_data));
+
+  *execution_id = proxy->Execute(action);
+  if (*execution_id) {
+    LOG(ERROR) << "Failed to execute action";
+    return ACTION_ERROR_IO_ERROR;
   }
 
   return ACTION_ERROR_NONE;
 }
 
 API int action_get_name(action_h action, const char** name) {
-  LOG(WARNING) << "action_get_name";
   if (action == nullptr || name == nullptr) {
     LOG(WARNING) << "Invalid Parameter";
     return ACTION_ERROR_INVALID_PARAMETER;
   }
 
-  common::ActionSchema* action_schema = static_cast<common::ActionSchema*>(action);
+  auto action_schema = static_cast<common::ActionSchema*>(action);
   *name = strdup(action_schema->GetActionId().c_str());
 
   return ACTION_ERROR_NONE;
 }
 
 API int action_get_schema(action_h action, const char** json_schema) {
-  LOG(WARNING) << "action_get_schema";
   if (action == nullptr || json_schema == nullptr) {
     LOG(WARNING) << "Invalid Parameter";
     return ACTION_ERROR_INVALID_PARAMETER;
   }
 
-  common::ActionSchema* action_schema = static_cast<common::ActionSchema*>(action);
+  auto action_schema = static_cast<common::ActionSchema*>(action);
   *json_schema = strdup(action_schema->GetJsonString().c_str());
 
   return ACTION_ERROR_NONE;
 }
 
 API int action_destroy(action_h action) {
-  LOG(WARNING) << "action_destroy";
   if (action == nullptr) {
     LOG(WARNING) << "Invalid Parameter";
     return ACTION_ERROR_INVALID_PARAMETER;
   }
 
-  common::ActionSchema* action_schema = static_cast<common::ActionSchema*>(action);
+  auto action_schema = static_cast<common::ActionSchema*>(action);
   delete action_schema;
 
   return ACTION_ERROR_NONE;
index ae285800050e9383d3232c41b47b5a43c9ef0be7..71259897c940a922034e28ef6fee71cbe50a7845 100644 (file)
@@ -86,7 +86,7 @@ void Connector::OnRejected() {
   LOG(WARNING) << "Rejected";
 }
 
-rpc::proxy::ActionService* Connector::GetConnection() {
+rpc::proxy::ActionService* Connector::GetProxy() {
   if (!Connect(true))
     return nullptr;
 
index c080621c3f868e4673f98f6adb1344f18c874ce8..1e5159d6b1545d2b7039a5df0df6b1356674d404 100644 (file)
@@ -17,8 +17,7 @@
 #ifndef API_CONNECTOR_HH_
 #define API_CONNECTOR_HH_
 
-#include "action.h"
-#include "tizen_action_service_proxy.h"
+#include "api/tizen_action_service_proxy.h"
 
 namespace api {
 
@@ -29,15 +28,14 @@ class Connector : public rpc::proxy::ActionService::IEventListener {
   Connector();
   virtual ~Connector();
 
-  rpc::proxy::ActionService* GetConnection();
+  rpc::proxy::ActionService* GetProxy();
   void OnConnected() override;
   void OnDisconnected() override;
   void OnRejected() override;
-
- private:
-  bool Connect(bool sync);
+  bool Connect(bool sync = true);
   void Disconnect();
 
+ private:
   std::unique_ptr<rpc::proxy::ActionService> proxy_;
   bool connected_ = false;
 };