From cb5a3ac2dd390841f7840c4e5be4dbf138a5d27a Mon Sep 17 00:00:00 2001 From: sunxilin Date: Mon, 2 Dec 2024 15:40:27 +0800 Subject: [PATCH 1/2] fix: remove is_cmd_connected api --- .../ten_runtime/binding/cpp/detail/ten_env.h | 6 - .../binding/python/ten_env/ten_env.h | 3 - .../binding/go/interface/ten/ten_env.go | 11 -- .../binding/go/interface/ten/ten_env.h | 3 - .../native/ten_env/ten_env_is_cmd_connected.c | 94 --------------- .../interface/ten/libten_runtime_python.pyi | 1 - .../binding/python/interface/ten/ten_env.py | 3 - .../binding/python/native/ten_env/ten_env.c | 1 - .../native/ten_env/ten_env_is_cmd_connected.c | 110 ------------------ .../simple_http_server_cpp/src/main.cc | 29 +++-- .../extension/extension_b/extension.go | 5 - .../extension.py | 3 - .../command/command_check_cmd_out.cc | 9 +- 13 files changed, 17 insertions(+), 261 deletions(-) delete mode 100644 core/src/ten_runtime/binding/go/native/ten_env/ten_env_is_cmd_connected.c delete mode 100644 core/src/ten_runtime/binding/python/native/ten_env/ten_env_is_cmd_connected.c diff --git a/core/include/ten_runtime/binding/cpp/detail/ten_env.h b/core/include/ten_runtime/binding/cpp/detail/ten_env.h index 0f8ce7495..ce72c8cea 100644 --- a/core/include/ten_runtime/binding/cpp/detail/ten_env.h +++ b/core/include/ten_runtime/binding/cpp/detail/ten_env.h @@ -640,12 +640,6 @@ class ten_env_t { return set_property_impl(path, ten_value_create_buf_with_move(buf), err); } - bool is_cmd_connected(const char *cmd_name, error_t *err = nullptr) { - TEN_ASSERT(c_ten_env, "Should not happen."); - return ten_env_is_cmd_connected( - c_ten_env, cmd_name, err != nullptr ? err->get_c_error() : nullptr); - } - bool on_configure_done(error_t *err = nullptr) { TEN_ASSERT(c_ten_env, "Should not happen."); diff --git a/core/include_internal/ten_runtime/binding/python/ten_env/ten_env.h b/core/include_internal/ten_runtime/binding/python/ten_env/ten_env.h index 25ab64786..145a64f4d 100644 --- a/core/include_internal/ten_runtime/binding/python/ten_env/ten_env.h +++ b/core/include_internal/ten_runtime/binding/python/ten_env/ten_env.h @@ -87,9 +87,6 @@ TEN_RUNTIME_PRIVATE_API PyObject *ten_py_ten_env_set_property_from_json( TEN_RUNTIME_PRIVATE_API PyObject *ten_py_ten_env_is_property_exist( PyObject *self, PyObject *args); -TEN_RUNTIME_PRIVATE_API PyObject *ten_py_ten_env_is_cmd_connected( - PyObject *self, PyObject *args); - TEN_RUNTIME_PRIVATE_API PyObject *ten_py_ten_env_get_property_int( PyObject *self, PyObject *args); diff --git a/core/src/ten_runtime/binding/go/interface/ten/ten_env.go b/core/src/ten_runtime/binding/go/interface/ten/ten_env.go index 722481f97..b59bbd894 100644 --- a/core/src/ten_runtime/binding/go/interface/ten/ten_env.go +++ b/core/src/ten_runtime/binding/go/interface/ten/ten_env.go @@ -43,8 +43,6 @@ type TenEnv interface { OnDeinitDone() error OnCreateInstanceDone(instance any, context uintptr) error - IsCmdConnected(cmdName string) (bool, error) - iProperty InitPropertyFromJSONBytes(value []byte) error @@ -359,15 +357,6 @@ func (p *tenEnv) OnCreateInstanceDone(instance any, context uintptr) error { return nil } -func (p *tenEnv) IsCmdConnected(cmdName string) (bool, error) { - return p.process(func() any { - cName := C.CString(cmdName) - defer C.free(unsafe.Pointer(cName)) - - return bool(C.ten_go_ten_env_is_cmd_connected(p.cPtr, cName)) - }).(bool), nil -} - func (p *tenEnv) String() string { cString := C.ten_go_ten_env_debug_info(p.cPtr) defer C.free(unsafe.Pointer(cString)) diff --git a/core/src/ten_runtime/binding/go/interface/ten/ten_env.h b/core/src/ten_runtime/binding/go/interface/ten/ten_env.h index f3887de0d..928b66a00 100644 --- a/core/src/ten_runtime/binding/go/interface/ten/ten_env.h +++ b/core/src/ten_runtime/binding/go/interface/ten/ten_env.h @@ -58,9 +58,6 @@ ten_go_error_t ten_go_ten_env_send_audio_frame( uintptr_t bridge_addr, uintptr_t audio_frame_bridge_addr, ten_go_handle_t handler_id); -bool ten_go_ten_env_is_cmd_connected(uintptr_t bridge_addr, - const char *cmd_name); - bool ten_go_ten_env_addon_create_extension(uintptr_t bridge_addr, const char *addon_name, const char *instance_name, diff --git a/core/src/ten_runtime/binding/go/native/ten_env/ten_env_is_cmd_connected.c b/core/src/ten_runtime/binding/go/native/ten_env/ten_env_is_cmd_connected.c deleted file mode 100644 index ff6025b5b..000000000 --- a/core/src/ten_runtime/binding/go/native/ten_env/ten_env_is_cmd_connected.c +++ /dev/null @@ -1,94 +0,0 @@ -// -// Copyright © 2024 Agora -// This file is part of TEN Framework, an open source project. -// Licensed under the Apache License, Version 2.0, with certain conditions. -// Refer to the "LICENSE" file in the root directory for more information. -// -#include "include_internal/ten_runtime/binding/go/ten_env/ten_env.h" -#include "include_internal/ten_runtime/binding/go/ten_env/ten_env_internal.h" -#include "ten_runtime/binding/go/interface/ten/ten_env.h" -#include "ten_runtime/ten_env_proxy/ten_env_proxy.h" -#include "ten_utils/lib/alloc.h" -#include "ten_utils/macro/check.h" - -typedef struct ten_env_notify_is_cmd_connected_info_t { - bool result; - ten_string_t name; - ten_event_t *completed; -} ten_env_notify_is_cmd_connected_info_t; - -static ten_env_notify_is_cmd_connected_info_t * -ten_env_notify_is_cmd_connected_info_create(const char *name) { - ten_env_notify_is_cmd_connected_info_t *info = - TEN_MALLOC(sizeof(ten_env_notify_is_cmd_connected_info_t)); - TEN_ASSERT(info, "Failed to allocate memory."); - - info->result = true; - ten_string_init_formatted(&info->name, "%s", name); - info->completed = ten_event_create(0, 1); - - return info; -} - -static void ten_env_notify_is_cmd_connected_info_destroy( - ten_env_notify_is_cmd_connected_info_t *info) { - TEN_ASSERT(info, "Invalid argument."); - - ten_string_deinit(&info->name); - ten_event_destroy(info->completed); - - TEN_FREE(info); -} - -static void ten_env_proxy_notify_is_cmd_connected(ten_env_t *ten_env, - void *user_data) { - TEN_ASSERT(user_data, "Invalid argument."); - TEN_ASSERT(ten_env && ten_env_check_integrity(ten_env, true), - "Should not happen."); - - ten_env_notify_is_cmd_connected_info_t *info = user_data; - TEN_ASSERT(info, "Should not happen."); - - ten_error_t err; - ten_error_init(&err); - - info->result = ten_env_is_cmd_connected( - ten_env, ten_string_get_raw_str(&info->name), &err); - - ten_event_set(info->completed); - - ten_error_deinit(&err); -} - -bool ten_go_ten_env_is_cmd_connected(uintptr_t bridge_addr, const char *name) { - ten_go_ten_env_t *self = ten_go_ten_env_reinterpret(bridge_addr); - TEN_ASSERT(self && ten_go_ten_env_check_integrity(self), - "Should not happen."); - - bool result = true; - - TEN_GO_TEN_ENV_IS_ALIVE_REGION_BEGIN(self, result = false;); - - ten_error_t err; - ten_error_init(&err); - ten_env_notify_is_cmd_connected_info_t *info = - ten_env_notify_is_cmd_connected_info_create(name); - - if (!ten_env_proxy_notify(self->c_ten_env_proxy, - ten_env_proxy_notify_is_cmd_connected, info, false, - &err)) { - TEN_LOGD("TEN/GO failed to is_cmd_connected."); - result = false; - goto done; - } - - ten_event_wait(info->completed, -1); - result = info->result; - -done: - ten_error_deinit(&err); - ten_env_notify_is_cmd_connected_info_destroy(info); - TEN_GO_TEN_ENV_IS_ALIVE_REGION_END(self); -ten_is_close: - return result; -} diff --git a/core/src/ten_runtime/binding/python/interface/ten/libten_runtime_python.pyi b/core/src/ten_runtime/binding/python/interface/ten/libten_runtime_python.pyi index 2398834ee..fd225ff4e 100644 --- a/core/src/ten_runtime/binding/python/interface/ten/libten_runtime_python.pyi +++ b/core/src/ten_runtime/binding/python/interface/ten/libten_runtime_python.pyi @@ -115,7 +115,6 @@ class _TenEnv: def return_result(self, result: _CmdResult, target_cmd: _Cmd) -> None: ... def return_result_directly(self, result: _CmdResult) -> None: ... def is_property_exist(self, path: str) -> bool: ... - def is_cmd_connected(self, msg_name: str) -> bool: ... def init_property_from_json(self, json_str: str) -> None: ... def log( self, diff --git a/core/src/ten_runtime/binding/python/interface/ten/ten_env.py b/core/src/ten_runtime/binding/python/interface/ten/ten_env.py index 93a943696..771c00c5b 100644 --- a/core/src/ten_runtime/binding/python/interface/ten/ten_env.py +++ b/core/src/ten_runtime/binding/python/interface/ten/ten_env.py @@ -98,9 +98,6 @@ def return_result_directly(self, result: CmdResult) -> None: def is_property_exist(self, path: str) -> bool: return self._internal.is_property_exist(path) - def is_cmd_connected(self, msg_name: str) -> bool: - return self._internal.is_cmd_connected(msg_name) - def get_property_int(self, path: str) -> int: return self._internal.get_property_int(path) diff --git a/core/src/ten_runtime/binding/python/native/ten_env/ten_env.c b/core/src/ten_runtime/binding/python/native/ten_env/ten_env.c index 1c6bfb703..438c80386 100644 --- a/core/src/ten_runtime/binding/python/native/ten_env/ten_env.c +++ b/core/src/ten_runtime/binding/python/native/ten_env/ten_env.c @@ -173,7 +173,6 @@ PyTypeObject *ten_py_ten_env_type(void) { NULL}, {"is_property_exist", ten_py_ten_env_is_property_exist, METH_VARARGS, NULL}, - {"is_cmd_connected", ten_py_ten_env_is_cmd_connected, METH_VARARGS, NULL}, {"init_property_from_json", ten_py_ten_env_init_property_from_json, METH_VARARGS, NULL}, {"log", ten_py_ten_env_log, METH_VARARGS, NULL}, diff --git a/core/src/ten_runtime/binding/python/native/ten_env/ten_env_is_cmd_connected.c b/core/src/ten_runtime/binding/python/native/ten_env/ten_env_is_cmd_connected.c deleted file mode 100644 index babc6887d..000000000 --- a/core/src/ten_runtime/binding/python/native/ten_env/ten_env_is_cmd_connected.c +++ /dev/null @@ -1,110 +0,0 @@ -// -// Copyright © 2024 Agora -// This file is part of TEN Framework, an open source project. -// Licensed under the Apache License, Version 2.0, with certain conditions. -// Refer to the "LICENSE" file in the root directory for more information. -// -#include "include_internal/ten_runtime/binding/python/common/error.h" -#include "include_internal/ten_runtime/binding/python/ten_env/ten_env.h" -#include "ten_runtime/ten_env_proxy/ten_env_proxy.h" -#include "ten_utils/macro/memory.h" - -typedef struct ten_env_notify_is_cmd_connected_info_t { - bool result; - ten_string_t name; - ten_event_t *completed; -} ten_env_notify_is_cmd_connected_info_t; - -static ten_env_notify_is_cmd_connected_info_t * -ten_env_notify_is_cmd_connected_info_create(const char *name) { - ten_env_notify_is_cmd_connected_info_t *info = - TEN_MALLOC(sizeof(ten_env_notify_is_cmd_connected_info_t)); - TEN_ASSERT(info, "Failed to allocate memory."); - - info->result = true; - ten_string_init_formatted(&info->name, "%s", name); - info->completed = ten_event_create(0, 1); - - return info; -} - -static void ten_env_notify_is_cmd_connected_info_destroy( - ten_env_notify_is_cmd_connected_info_t *info) { - TEN_ASSERT(info, "Invalid argument."); - - ten_string_deinit(&info->name); - ten_event_destroy(info->completed); - - TEN_FREE(info); -} - -static void ten_env_proxy_notify_is_cmd_connected(ten_env_t *ten_env, - void *user_data) { - TEN_ASSERT(user_data, "Invalid argument."); - TEN_ASSERT(ten_env && ten_env_check_integrity(ten_env, true), - "Should not happen."); - - ten_env_notify_is_cmd_connected_info_t *info = user_data; - TEN_ASSERT(info, "Should not happen."); - - ten_error_t err; - ten_error_init(&err); - - info->result = ten_env_is_cmd_connected( - ten_env, ten_string_get_raw_str(&info->name), &err); - - ten_event_set(info->completed); - - ten_error_deinit(&err); -} - -PyObject *ten_py_ten_env_is_cmd_connected(PyObject *self, PyObject *args) { - ten_py_ten_env_t *py_ten_env = (ten_py_ten_env_t *)self; - TEN_ASSERT(py_ten_env && ten_py_ten_env_check_integrity(py_ten_env), - "Invalid argument."); - - if (PyTuple_GET_SIZE(args) != 1) { - return ten_py_raise_py_value_error_exception( - "Invalid argument count when ten_env.is_cmd_connected."); - } - - const char *cmd_name = NULL; - if (!PyArg_ParseTuple(args, "s", &cmd_name)) { - return ten_py_raise_py_value_error_exception( - "Failed to parse argument when ten_env.is_cmd_connected."); - } - - ten_error_t err; - ten_error_init(&err); - - ten_env_notify_is_cmd_connected_info_t *info = - ten_env_notify_is_cmd_connected_info_create(cmd_name); - - if (!ten_env_proxy_notify(py_ten_env->c_ten_env_proxy, - ten_env_proxy_notify_is_cmd_connected, info, false, - &err)) { - ten_error_deinit(&err); - ten_env_notify_is_cmd_connected_info_destroy(info); - - return ten_py_raise_py_value_error_exception( - "Failed to notify is command connected."); - } - - PyThreadState *saved_py_thread_state = PyEval_SaveThread(); - ten_event_wait(info->completed, -1); - PyEval_RestoreThread(saved_py_thread_state); - - bool is_connected = info->result; - - ten_env_notify_is_cmd_connected_info_destroy(info); - - if (!ten_error_is_success(&err)) { - ten_error_deinit(&err); - - return ten_py_raise_py_value_error_exception( - "Failed to check if command is connected."); - } - - ten_error_deinit(&err); - return PyBool_FromLong(is_connected); -} diff --git a/packages/example_extensions/simple_http_server_cpp/src/main.cc b/packages/example_extensions/simple_http_server_cpp/src/main.cc index cd5785fe6..7c7717520 100644 --- a/packages/example_extensions/simple_http_server_cpp/src/main.cc +++ b/packages/example_extensions/simple_http_server_cpp/src/main.cc @@ -634,19 +634,18 @@ void send_ten_msg_with_req_body( // command. cmd->from_json(cmd_json.dump().c_str()); - if (cmd->get_type() == TEN_MSG_TYPE_CMD && - !ten_env.is_cmd_connected(cmd->get_name())) { - prepare_response_data_from_ten_world(http_session_data, - "The command is not supported."); - return; - } - // Send out the command to the TEN runtime. ten_env.send_cmd( std::move(cmd), [http_session_data](ten::ten_env_t &ten_env, std::unique_ptr cmd, - ten::error_t * /*error*/) { + ten::error_t *error) { + if (error != nullptr) { + prepare_response_data_from_ten_world( + http_session_data, "The command is not supported. err:" + + std::string(error->errmsg())); + return; + } auto *ext = static_cast( ten_env.get_attached_target()); assert(ext && "Failed to get the attached extension."); @@ -673,19 +672,17 @@ void send_ten_msg_without_req_body( cmd->set_property("method", method); cmd->set_property("url", http_session_data->url); - if (cmd->get_type() == TEN_MSG_TYPE_CMD && - !ten_env.is_cmd_connected(cmd->get_name())) { - prepare_response_data_from_ten_world(http_session_data, - "The command is not supported."); - return; - } - // Send out the command to the TEN runtime. ten_env.send_cmd( std::move(cmd), [http_session_data](ten::ten_env_t &ten_env, std::unique_ptr cmd, - ten::error_t * /*error*/) { + ten::error_t *error) { + if (error != nullptr) { + prepare_response_data_from_ten_world( + http_session_data, "The command is not supported. err:" + + std::string(error->errmsg())); + } auto *ext = static_cast( ten_env.get_attached_target()); assert(ext && "Failed to get the attached extension."); diff --git a/tests/ten_runtime/integration/go/frequently_cgo_call_go/frequently_cgo_call_go_app/ten_packages/extension/extension_b/extension.go b/tests/ten_runtime/integration/go/frequently_cgo_call_go/frequently_cgo_call_go_app/ten_packages/extension/extension_b/extension.go index acad8f016..ac215e0c3 100644 --- a/tests/ten_runtime/integration/go/frequently_cgo_call_go/frequently_cgo_call_go_app/ten_packages/extension/extension_b/extension.go +++ b/tests/ten_runtime/integration/go/frequently_cgo_call_go/frequently_cgo_call_go_app/ten_packages/extension/extension_b/extension.go @@ -31,11 +31,6 @@ func (p *extensionB) OnCmd( go func() { fmt.Println("extensionB OnCmd") - connected, err := tenEnv.IsCmdConnected("cmd_not_exist") - if err != nil || connected { - panic("Should not happen.") - } - cmdName, _ := cmd.GetName() if cmdName == "B" { var count uint32 = 0 diff --git a/tests/ten_runtime/integration/python/async_extension_basic_python/async_extension_basic_python_app/ten_packages/extension/default_async_extension_python/extension.py b/tests/ten_runtime/integration/python/async_extension_basic_python/async_extension_basic_python_app/ten_packages/extension/default_async_extension_python/extension.py index 39c07d975..9d1bb21f4 100644 --- a/tests/ten_runtime/integration/python/async_extension_basic_python/async_extension_basic_python_app/ten_packages/extension/default_async_extension_python/extension.py +++ b/tests/ten_runtime/integration/python/async_extension_basic_python/async_extension_basic_python_app/ten_packages/extension/default_async_extension_python/extension.py @@ -38,9 +38,6 @@ async def on_cmd(self, ten_env: AsyncTenEnv, cmd: Cmd) -> None: # Mock async operation, e.g. network, file I/O. await asyncio.sleep(0.5) - assert ten_env.is_cmd_connected("hello") is True - assert ten_env.is_cmd_connected("unknown_cmd") is False - # Send a new command to other extensions and wait for the result. The # result will be returned to the original sender. new_cmd = Cmd.create("hello") diff --git a/tests/ten_runtime/smoke/extension_test/command/command_check_cmd_out.cc b/tests/ten_runtime/smoke/extension_test/command/command_check_cmd_out.cc index f22b8fda8..8a8d891fc 100644 --- a/tests/ten_runtime/smoke/extension_test/command/command_check_cmd_out.cc +++ b/tests/ten_runtime/smoke/extension_test/command/command_check_cmd_out.cc @@ -21,11 +21,10 @@ class test_extension_1 : public ten::extension_t { void on_cmd(ten::ten_env_t &ten_env, std::unique_ptr cmd) override { - const auto *cmd_name = cmd->get_name(); - auto res = ten_env.is_cmd_connected(cmd_name); - if (res) { - ten_env.send_cmd(std::move(cmd)); - } else { + ten::error_t error; + + auto rc = ten_env.send_cmd(std::move(cmd), nullptr, &error); + if (!rc) { auto cmd_result = ten::cmd_result_t::create(TEN_STATUS_CODE_OK); cmd_result->set_property("detail", "can not find a way out"); ten_env.return_result(std::move(cmd_result), std::move(cmd)); From aed208e5f8fa442c1ec345c8445e10ecd6668e41 Mon Sep 17 00:00:00 2001 From: Hu Yueh-Wei Date: Mon, 2 Dec 2024 23:27:45 +0800 Subject: [PATCH 2/2] fix: refine codes --- .../example_extensions/simple_http_server_cpp/src/main.cc | 3 +++ .../smoke/extension_test/command/command_check_cmd_out.cc | 4 +--- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/packages/example_extensions/simple_http_server_cpp/src/main.cc b/packages/example_extensions/simple_http_server_cpp/src/main.cc index 7c7717520..d7b65e4a2 100644 --- a/packages/example_extensions/simple_http_server_cpp/src/main.cc +++ b/packages/example_extensions/simple_http_server_cpp/src/main.cc @@ -646,6 +646,7 @@ void send_ten_msg_with_req_body( std::string(error->errmsg())); return; } + auto *ext = static_cast( ten_env.get_attached_target()); assert(ext && "Failed to get the attached extension."); @@ -682,7 +683,9 @@ void send_ten_msg_without_req_body( prepare_response_data_from_ten_world( http_session_data, "The command is not supported. err:" + std::string(error->errmsg())); + return; } + auto *ext = static_cast( ten_env.get_attached_target()); assert(ext && "Failed to get the attached extension."); diff --git a/tests/ten_runtime/smoke/extension_test/command/command_check_cmd_out.cc b/tests/ten_runtime/smoke/extension_test/command/command_check_cmd_out.cc index 8a8d891fc..ed9ee3e0d 100644 --- a/tests/ten_runtime/smoke/extension_test/command/command_check_cmd_out.cc +++ b/tests/ten_runtime/smoke/extension_test/command/command_check_cmd_out.cc @@ -21,9 +21,7 @@ class test_extension_1 : public ten::extension_t { void on_cmd(ten::ten_env_t &ten_env, std::unique_ptr cmd) override { - ten::error_t error; - - auto rc = ten_env.send_cmd(std::move(cmd), nullptr, &error); + auto rc = ten_env.send_cmd(std::move(cmd)); if (!rc) { auto cmd_result = ten::cmd_result_t::create(TEN_STATUS_CODE_OK); cmd_result->set_property("detail", "can not find a way out");