From d898d6200b2b3fe4681694e13bf71eeac1146362 Mon Sep 17 00:00:00 2001 From: Hu Yueh-Wei Date: Thu, 21 Nov 2024 19:42:42 +0800 Subject: [PATCH] feat!: remove send_json (#307) --- .vscode/launch.json | 4 +- .../cpp/internal/msg/cmd/start_graph.h | 14 + .../binding/cpp/internal/ten_env.h | 75 +--- .../ten_runtime/msg/cmd/start_graph/cmd.h | 8 + .../ten_runtime/ten_env/internal/send.h | 5 - .../binding/cpp/internal/msg/cmd/timer.h | 12 + .../ten_runtime/binding/python/msg/msg.h | 3 + .../binding/python/ten_env/ten_env.h | 8 +- .../msg/cmd_base/cmd/start_graph/cmd.h | 3 - .../ten_runtime/msg/cmd_base/cmd/timer/cmd.h | 17 +- .../ten_runtime/app/msg_interface/common.c | 14 - .../binding/go/interface/ten/ten_env.go | 63 ---- .../binding/go/interface/ten/ten_env.h | 76 ---- .../go/native/ten_env/ten_env_return_value.c | 357 ------------------ .../go/native/ten_env/ten_env_send_json.c | 121 ------ .../python/interface/ten/async_ten_env.py | 16 - .../interface/ten/libten_runtime_python.pyi | 8 +- .../binding/python/interface/ten/msg.py | 11 + .../binding/python/interface/ten/ten_env.py | 3 - .../binding/python/native/msg/msg.c | 44 +++ .../binding/python/native/msg/type.c | 1 + .../binding/python/native/ten_env/ten_env.c | 1 - .../python/native/ten_env/ten_env_send_json.c | 176 --------- .../engine/msg_interface/start_graph.c | 3 +- .../msg/cmd_base/cmd/start_graph/cmd.c | 38 +- .../ten_runtime/msg/cmd_base/cmd/timer/cmd.c | 33 +- core/src/ten_runtime/ten_env/internal/send.c | 33 -- .../aio_http_server_python/main.py | 10 +- .../default_extension_cpp/src/main.cc | 27 +- tests/ten_runtime/integration/go/BUILD.gn | 1 - .../integration/go/send_json_go/BUILD.gn | 65 ---- .../integration/go/send_json_go/__init__.py | 0 .../go/send_json_go/client/client.cc | 38 -- .../send_json_go_app/manifest.json | 24 -- .../send_json_go_app/property.json | 65 ---- .../extension/extension_a/extension.go | 78 ---- .../ten_packages/extension/extension_a/go.mod | 7 - .../extension/extension_a/manifest.json | 18 - .../extension/extension_a/property.json | 1 - .../extension/extension_b/extension.go | 74 ---- .../ten_packages/extension/extension_b/go.mod | 7 - .../extension/extension_b/manifest.json | 18 - .../extension/extension_b/property.json | 1 - .../extension/extension_c/extension.go | 58 --- .../ten_packages/extension/extension_c/go.mod | 7 - .../extension/extension_c/manifest.json | 18 - .../extension/extension_c/property.json | 1 - .../integration/go/send_json_go/test_case.py | 96 ----- tests/ten_runtime/integration/python/BUILD.gn | 1 - .../default_extension_python/extension.py | 14 +- .../python/send_json_python/BUILD.gn | 50 --- .../python/send_json_python/__init__.py | 0 .../send_json_python_app/manifest.json | 24 -- .../send_json_python_app/property.json | 66 ---- .../default_extension_python/extension.py | 64 ---- .../python/send_json_python/test_case.py | 130 ------- .../smoke/extension_test/basic/basic_loop.cc | 63 ++-- .../extension_test/basic/basic_loop_cmd.cc | 23 +- .../basic/basic_loop_cmd_snap_shot.cc | 41 +- .../command/command_invalid_extension_2.cc | 20 +- .../command/command_stop_graph_actively.cc | 13 +- ...nd_stop_graph_actively_through_cmd_dest.cc | 2 +- .../extension_send_msg_to_incorrect_engine.cc | 20 +- .../error/failed_to_connect_to_remote.cc | 22 +- .../multi_dest/multi_dest_y_topo.cc | 10 +- .../128_threads_attempt_to_suspend_1.cc | 21 +- .../32_threads_attempt_to_suspend_5.cc | 22 +- .../32_threads_attempt_to_suspend_6.cc | 21 +- .../predefined_graph_basic_2.cc | 65 ++-- .../resp_handler/resp_handler_async_basic.cc | 12 +- .../resp_handler/resp_handler_basic.cc | 16 +- .../resp_handler_basic_wait_in_on_start.cc | 20 +- .../start_graph_and_communicate.cc | 58 ++- .../start_graph/start_graph_from_extension.cc | 35 +- .../start_predefined_graph_cross_app.cc | 33 +- .../start_predefined_graph_from_extension.cc | 47 +-- .../start_two_predefined_graphs.cc | 62 ++- .../extension_test/timer/timer_one_shot.cc | 21 +- .../extension_test/timer/timer_ten_shot.cc | 21 +- .../extension_test/timer/timer_two_shot.cc | 20 +- tests/ten_runtime/smoke/msg_test/msg_1.cc | 21 +- tests/ten_runtime/smoke/msg_test/msg_2.cc | 22 +- tests/ten_runtime/smoke/msg_test/msg_6.cc | 22 +- tests/ten_runtime/smoke/msg_test/msg_7.cc | 21 +- .../ten_runtime/smoke/msg_test/msg_fail_1.cc | 38 +- 85 files changed, 506 insertions(+), 2416 deletions(-) delete mode 100644 core/src/ten_runtime/binding/go/native/ten_env/ten_env_return_value.c delete mode 100644 core/src/ten_runtime/binding/go/native/ten_env/ten_env_send_json.c delete mode 100644 core/src/ten_runtime/binding/python/native/ten_env/ten_env_send_json.c delete mode 100644 tests/ten_runtime/integration/go/send_json_go/BUILD.gn delete mode 100644 tests/ten_runtime/integration/go/send_json_go/__init__.py delete mode 100644 tests/ten_runtime/integration/go/send_json_go/client/client.cc delete mode 100644 tests/ten_runtime/integration/go/send_json_go/send_json_go_app/manifest.json delete mode 100644 tests/ten_runtime/integration/go/send_json_go/send_json_go_app/property.json delete mode 100644 tests/ten_runtime/integration/go/send_json_go/send_json_go_app/ten_packages/extension/extension_a/extension.go delete mode 100644 tests/ten_runtime/integration/go/send_json_go/send_json_go_app/ten_packages/extension/extension_a/go.mod delete mode 100644 tests/ten_runtime/integration/go/send_json_go/send_json_go_app/ten_packages/extension/extension_a/manifest.json delete mode 100644 tests/ten_runtime/integration/go/send_json_go/send_json_go_app/ten_packages/extension/extension_a/property.json delete mode 100644 tests/ten_runtime/integration/go/send_json_go/send_json_go_app/ten_packages/extension/extension_b/extension.go delete mode 100644 tests/ten_runtime/integration/go/send_json_go/send_json_go_app/ten_packages/extension/extension_b/go.mod delete mode 100644 tests/ten_runtime/integration/go/send_json_go/send_json_go_app/ten_packages/extension/extension_b/manifest.json delete mode 100644 tests/ten_runtime/integration/go/send_json_go/send_json_go_app/ten_packages/extension/extension_b/property.json delete mode 100644 tests/ten_runtime/integration/go/send_json_go/send_json_go_app/ten_packages/extension/extension_c/extension.go delete mode 100644 tests/ten_runtime/integration/go/send_json_go/send_json_go_app/ten_packages/extension/extension_c/go.mod delete mode 100644 tests/ten_runtime/integration/go/send_json_go/send_json_go_app/ten_packages/extension/extension_c/manifest.json delete mode 100644 tests/ten_runtime/integration/go/send_json_go/send_json_go_app/ten_packages/extension/extension_c/property.json delete mode 100644 tests/ten_runtime/integration/go/send_json_go/test_case.py delete mode 100644 tests/ten_runtime/integration/python/send_json_python/BUILD.gn delete mode 100644 tests/ten_runtime/integration/python/send_json_python/__init__.py delete mode 100644 tests/ten_runtime/integration/python/send_json_python/send_json_python_app/manifest.json delete mode 100644 tests/ten_runtime/integration/python/send_json_python/send_json_python_app/property.json delete mode 100644 tests/ten_runtime/integration/python/send_json_python/send_json_python_app/ten_packages/extension/default_extension_python/extension.py delete mode 100644 tests/ten_runtime/integration/python/send_json_python/test_case.py diff --git a/.vscode/launch.json b/.vscode/launch.json index f8f8d374be..22b7f1f8d9 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -440,9 +440,9 @@ "name": "app (C/C++) (lldb, launch)", "type": "lldb", "request": "launch", - "program": "${workspaceFolder}/out/linux/x64/tests/ten_runtime/integration/cpp/hello_world/hello_world_app/bin/hello_world_app_source", + "program": "${workspaceFolder}/out/linux/x64/tests/ten_runtime/integration/cpp/check_start_graph/check_start_graph_app/bin/check_start_graph_source", "args": [], - "cwd": "${workspaceFolder}/out/linux/x64/tests/ten_runtime/integration/cpp/hello_world/hello_world_app", + "cwd": "${workspaceFolder}/out/linux/x64/tests/ten_runtime/integration/cpp/check_start_graph/check_start_graph_app", "env": { "ASAN_OPTIONS": "use_sigaltstack=0", }, diff --git a/core/include/ten_runtime/binding/cpp/internal/msg/cmd/start_graph.h b/core/include/ten_runtime/binding/cpp/internal/msg/cmd/start_graph.h index c8ddbda343..a9930c7bcd 100644 --- a/core/include/ten_runtime/binding/cpp/internal/msg/cmd/start_graph.h +++ b/core/include/ten_runtime/binding/cpp/internal/msg/cmd/start_graph.h @@ -40,6 +40,20 @@ class cmd_start_graph_t : public cmd_t { : cmd_t(ten_cmd_start_graph_create()) {}; ~cmd_start_graph_t() override = default; + bool set_predefined_graph_name(const char *predefined_graph_name, + error_t *err = nullptr) { + return ten_cmd_start_graph_set_predefined_graph_name( + c_msg, predefined_graph_name, + err != nullptr ? err->get_internal_representation() : nullptr); + } + + bool set_nodes_and_connections_from_json(const char *json_str, + error_t *err = nullptr) { + return ten_cmd_start_graph_init_from_json_str( + c_msg, json_str, + err != nullptr ? err->get_internal_representation() : nullptr); + } + // @{ cmd_start_graph_t(cmd_start_graph_t &other) = delete; cmd_start_graph_t(cmd_start_graph_t &&other) = delete; diff --git a/core/include/ten_runtime/binding/cpp/internal/ten_env.h b/core/include/ten_runtime/binding/cpp/internal/ten_env.h index 6233c077ec..73b76928e5 100644 --- a/core/include/ten_runtime/binding/cpp/internal/ten_env.h +++ b/core/include/ten_runtime/binding/cpp/internal/ten_env.h @@ -69,13 +69,6 @@ class ten_env_t { ten_env_t &operator=(const ten_env_t &&) = delete; // @} - bool send_cmd_ex(std::unique_ptr &&cmd, - result_handler_func_t &&result_handler = nullptr, - error_t *err = nullptr) { - return send_cmd_internal(std::move(cmd), std::move(result_handler), true, - err); - } - bool send_cmd(std::unique_ptr &&cmd, result_handler_func_t &&result_handler = nullptr, error_t *err = nullptr) { @@ -83,69 +76,11 @@ class ten_env_t { err); } - bool send_json_ex(const char *json_str, - result_handler_func_t &&result_handler = nullptr, - error_t *err = nullptr) { - TEN_ASSERT(c_ten_env, "Should not happen."); - - if (json_str == nullptr) { - TEN_ASSERT(0, "Invalid argument."); - return false; - } - - ten_json_t *c_json = ten_json_from_string( - json_str, - err != nullptr ? err->get_internal_representation() : nullptr); - if (c_json == nullptr) { - return false; - } - - bool rc = false; - - if (result_handler == nullptr) { - rc = ten_env_send_json( - c_ten_env, c_json, nullptr, nullptr, - err != nullptr ? err->get_internal_representation() : nullptr); - } else { - auto *result_handler_ptr = - new result_handler_func_t(std::move(result_handler)); - - rc = ten_env_send_json( - c_ten_env, c_json, proxy_handle_result, result_handler_ptr, - err != nullptr ? err->get_internal_representation() : nullptr); - - if (!rc) { - delete result_handler_ptr; - } - } - - ten_json_destroy(c_json); - - if (!rc) { - TEN_LOGE("Failed to send_json: %s", json_str); - } - - return rc; - } - - bool send_json(const char *json_str, - result_handler_func_t &&result_handler = nullptr, - error_t *err = nullptr) { - TEN_ASSERT(c_ten_env, "Should not happen."); - - if (result_handler) { - return send_json_ex( - json_str, - [result_handler](ten::ten_env_t &ten_env, - std::unique_ptr cmd_result) { - if (cmd_result->is_completed()) { - result_handler(ten_env, std::move(cmd_result)); - } - }, - err); - } else { - return send_json_ex(json_str, nullptr, err); - } + bool send_cmd_ex(std::unique_ptr &&cmd, + result_handler_func_t &&result_handler = nullptr, + error_t *err = nullptr) { + return send_cmd_internal(std::move(cmd), std::move(result_handler), true, + err); } bool send_data(std::unique_ptr &&data, error_t *err = nullptr) { diff --git a/core/include/ten_runtime/msg/cmd/start_graph/cmd.h b/core/include/ten_runtime/msg/cmd/start_graph/cmd.h index 60999b0cf6..06a7286003 100644 --- a/core/include/ten_runtime/msg/cmd/start_graph/cmd.h +++ b/core/include/ten_runtime/msg/cmd/start_graph/cmd.h @@ -8,9 +8,17 @@ #include "ten_runtime/ten_config.h" +#include "ten_utils/lib/error.h" #include "ten_utils/lib/smart_ptr.h" typedef struct ten_msg_t ten_msg_t; typedef struct ten_cmd_start_graph_t ten_cmd_start_graph_t; TEN_RUNTIME_API ten_shared_ptr_t *ten_cmd_start_graph_create(void); + +TEN_RUNTIME_API bool ten_cmd_start_graph_set_predefined_graph_name( + ten_shared_ptr_t *self, const char *predefined_graph_name, + ten_error_t *err); + +TEN_RUNTIME_API bool ten_cmd_start_graph_init_from_json_str( + ten_shared_ptr_t *self, const char *json_str, ten_error_t *err); diff --git a/core/include/ten_runtime/ten_env/internal/send.h b/core/include/ten_runtime/ten_env/internal/send.h index 1c2626fd74..ad6288b906 100644 --- a/core/include/ten_runtime/ten_env/internal/send.h +++ b/core/include/ten_runtime/ten_env/internal/send.h @@ -36,11 +36,6 @@ TEN_RUNTIME_API bool ten_env_send_cmd_ex( ten_env_cmd_result_handler_func_t result_handler, void *result_handler_user_data, ten_error_t *err); -TEN_RUNTIME_API bool ten_env_send_json( - ten_env_t *self, ten_json_t *json, - ten_env_cmd_result_handler_func_t result_handler, - void *result_handler_user_data, ten_error_t *err); - TEN_RUNTIME_API bool ten_env_send_data(ten_env_t *self, ten_shared_ptr_t *data, ten_error_t *err); diff --git a/core/include_internal/ten_runtime/binding/cpp/internal/msg/cmd/timer.h b/core/include_internal/ten_runtime/binding/cpp/internal/msg/cmd/timer.h index 17efcf80d9..48f60ff7cb 100644 --- a/core/include_internal/ten_runtime/binding/cpp/internal/msg/cmd/timer.h +++ b/core/include_internal/ten_runtime/binding/cpp/internal/msg/cmd/timer.h @@ -40,6 +40,18 @@ class cmd_timer_t : public cmd_t { : cmd_t(ten_cmd_timer_create()) {} ~cmd_timer_t() override = default; + bool set_timer_id(uint32_t timer_id) { + return ten_cmd_timer_set_timer_id(c_msg, timer_id); + } + + bool set_times(int32_t times) { + return ten_cmd_timer_set_times(c_msg, times); + } + + bool set_timeout_in_us(int64_t timeout_in_us) { + return ten_cmd_timer_set_timeout_in_us(c_msg, timeout_in_us); + } + // @{ cmd_timer_t(cmd_timer_t &other) = delete; cmd_timer_t(cmd_timer_t &&other) = delete; diff --git a/core/include_internal/ten_runtime/binding/python/msg/msg.h b/core/include_internal/ten_runtime/binding/python/msg/msg.h index f83b721319..c629699b76 100644 --- a/core/include_internal/ten_runtime/binding/python/msg/msg.h +++ b/core/include_internal/ten_runtime/binding/python/msg/msg.h @@ -34,6 +34,9 @@ TEN_RUNTIME_PRIVATE_API PyObject *ten_py_msg_get_name(PyObject *self, TEN_RUNTIME_PRIVATE_API PyObject *ten_py_msg_set_name(PyObject *self, PyObject *args); +TEN_RUNTIME_PRIVATE_API PyObject *ten_py_msg_set_dest(PyObject *self, + PyObject *args); + TEN_RUNTIME_PRIVATE_API PyObject *ten_py_msg_set_property_string( PyObject *self, PyObject *args); 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 8e45716db4..25ab647867 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 @@ -26,7 +26,7 @@ typedef struct ten_py_ten_env_t { // Mark whether the gil state need to be released after 'on_deinit_done'. bool need_to_release_gil_state; - PyThreadState* py_thread_state; + PyThreadState *py_thread_state; } ten_py_ten_env_t; TEN_RUNTIME_PRIVATE_API bool ten_py_ten_env_check_integrity( @@ -60,9 +60,6 @@ TEN_RUNTIME_PRIVATE_API PyObject *ten_py_ten_env_on_deinit_done(PyObject *self, TEN_RUNTIME_PRIVATE_API PyObject *ten_py_ten_env_on_create_instance_done( PyObject *self, PyObject *args); -TEN_RUNTIME_PRIVATE_API PyObject *ten_py_ten_env_send_json(PyObject *self, - PyObject *args); - TEN_RUNTIME_PRIVATE_API PyObject *ten_py_ten_env_send_cmd(PyObject *self, PyObject *args); @@ -75,9 +72,6 @@ TEN_RUNTIME_PRIVATE_API PyObject *ten_py_ten_env_send_video_frame( TEN_RUNTIME_PRIVATE_API PyObject *ten_py_ten_env_send_audio_frame( PyObject *self, PyObject *args); -TEN_RUNTIME_PRIVATE_API PyObject *ten_py_ten_return_json(PyObject *self, - PyObject *args); - TEN_RUNTIME_PRIVATE_API PyObject *ten_py_ten_env_return_result(PyObject *self, PyObject *args); diff --git a/core/include_internal/ten_runtime/msg/cmd_base/cmd/start_graph/cmd.h b/core/include_internal/ten_runtime/msg/cmd_base/cmd/start_graph/cmd.h index 3ee2f91a3d..7de901a2d8 100644 --- a/core/include_internal/ten_runtime/msg/cmd_base/cmd/start_graph/cmd.h +++ b/core/include_internal/ten_runtime/msg/cmd_base/cmd/start_graph/cmd.h @@ -56,9 +56,6 @@ TEN_RUNTIME_PRIVATE_API bool ten_raw_cmd_start_graph_get_long_running_mode( TEN_RUNTIME_PRIVATE_API bool ten_cmd_start_graph_get_long_running_mode( ten_shared_ptr_t *self); -TEN_RUNTIME_PRIVATE_API void ten_cmd_start_graph_set_predefined_graph_name( - ten_shared_ptr_t *self, const char *predefined_graph_name); - TEN_RUNTIME_PRIVATE_API ten_string_t * ten_raw_cmd_start_graph_get_predefined_graph_name(ten_cmd_start_graph_t *self); diff --git a/core/include_internal/ten_runtime/msg/cmd_base/cmd/timer/cmd.h b/core/include_internal/ten_runtime/msg/cmd_base/cmd/timer/cmd.h index d20cc648c4..f368079bf0 100644 --- a/core/include_internal/ten_runtime/msg/cmd_base/cmd/timer/cmd.h +++ b/core/include_internal/ten_runtime/msg/cmd_base/cmd/timer/cmd.h @@ -35,11 +35,11 @@ TEN_RUNTIME_PRIVATE_API bool ten_raw_cmd_timer_check_type_and_name( ten_msg_t *self, const char *type_str, const char *name_str, ten_error_t *err); -TEN_RUNTIME_PRIVATE_API void ten_raw_cmd_timer_set_timer_id( +TEN_RUNTIME_PRIVATE_API bool ten_raw_cmd_timer_set_timer_id( ten_cmd_timer_t *self, uint32_t timer_id); -TEN_RUNTIME_PRIVATE_API void ten_raw_cmd_timer_set_times(ten_cmd_timer_t *self, - int32_t times); +TEN_RUNTIME_API bool ten_raw_cmd_timer_set_times(ten_cmd_timer_t *self, + int32_t times); TEN_RUNTIME_PRIVATE_API uint32_t ten_raw_cmd_timer_get_timer_id(ten_cmd_timer_t *self); @@ -47,8 +47,8 @@ ten_raw_cmd_timer_get_timer_id(ten_cmd_timer_t *self); TEN_RUNTIME_PRIVATE_API uint32_t ten_cmd_timer_get_timer_id(ten_shared_ptr_t *self); -TEN_RUNTIME_PRIVATE_API void ten_cmd_timer_set_timer_id(ten_shared_ptr_t *self, - uint32_t timer_id); +TEN_RUNTIME_API bool ten_cmd_timer_set_timer_id(ten_shared_ptr_t *self, + uint32_t timer_id); TEN_RUNTIME_PRIVATE_API uint64_t ten_raw_cmd_timer_get_timeout_in_us(ten_cmd_timer_t *self); @@ -56,13 +56,16 @@ ten_raw_cmd_timer_get_timeout_in_us(ten_cmd_timer_t *self); TEN_RUNTIME_PRIVATE_API uint64_t ten_cmd_timer_get_timeout_in_us(ten_shared_ptr_t *self); +TEN_RUNTIME_API bool ten_cmd_timer_set_timeout_in_us(ten_shared_ptr_t *self, + uint64_t timeout_in_us); + TEN_RUNTIME_PRIVATE_API int32_t ten_raw_cmd_timer_get_times(ten_cmd_timer_t *self); TEN_RUNTIME_PRIVATE_API int32_t ten_cmd_timer_get_times(ten_shared_ptr_t *self); -TEN_RUNTIME_PRIVATE_API void ten_cmd_timer_set_times(ten_shared_ptr_t *self, - int32_t times); +TEN_RUNTIME_API bool ten_cmd_timer_set_times(ten_shared_ptr_t *self, + int32_t times); TEN_RUNTIME_PRIVATE_API bool ten_raw_cmd_timer_as_msg_init_from_json( ten_msg_t *self, ten_json_t *json, ten_error_t *err); diff --git a/core/src/ten_runtime/app/msg_interface/common.c b/core/src/ten_runtime/app/msg_interface/common.c index 32f7a8e96c..40352b0025 100644 --- a/core/src/ten_runtime/app/msg_interface/common.c +++ b/core/src/ten_runtime/app/msg_interface/common.c @@ -374,20 +374,6 @@ static void ten_app_handle_in_msgs_sync(ten_app_t *self) { // NULL. Ex: send the following cmd from extension in engine whose // 'graph_id' is A. // - // ten_env.send_json( - // { - // "_ten": { - // "name": "demo", - // "dest": [ - // { - // "app": "localhost", - // "graph": "graph_id_of_another_engine" - // } - // ] - // } - // } - // ) - // // * If the 'cmd' is sent to another engine (whose 'graph_id' is B) from // the client side, after the physical connection between the client and // current engine (whose 'graph_id' is A) has been established, the 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 fc7afd0b22..5a8453eb68 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 @@ -28,8 +28,6 @@ type TenEnv interface { postSyncJob(payload job) any postAsyncJob(payload job) any - SendJSON(json string, handler ResultHandler) error - SendJSONBytes(json []byte, handler ResultHandler) error SendCmd(cmd Cmd, handler ResultHandler) error SendData(data Data) error SendVideoFrame(videoFrame VideoFrame) error @@ -137,67 +135,6 @@ func (p *tenEnv) postAsyncJob(payload job) any { return p.process(payload) } -func (p *tenEnv) sendJSON( - bytes unsafe.Pointer, - size int, - handler ResultHandler, -) error { - cb := goHandleNil - if handler != nil { - cb = newGoHandle(handler) - } - - apiStatus := C.ten_go_ten_env_send_json( - p.cPtr, - bytes, - C.int(size), - cHandle(cb), - ) - err := withGoStatus(&apiStatus) - - return err -} - -// SendJSON sends a json string to TEN runtime, and the handler function will be -// called when the result (i.e., CmdResult) is received. The result will be -// discarded if the handler is nil. -func (p *tenEnv) SendJSON(json string, handler ResultHandler) error { - if len(json) == 0 { - return newTenError( - ErrnoInvalidArgument, - "json data is required.", - ) - } - - return withCGO(func() error { - return p.sendJSON( - unsafe.Pointer(unsafe.StringData(json)), - len(json), - handler, - ) - }) -} - -// SendJSONBytes sends a json bytes to TEN runtime, and the handler function -// will be called when the result (i.e., CmdResult) is received. The result -// will be discarded if the handler is nil. -func (p *tenEnv) SendJSONBytes(json []byte, handler ResultHandler) error { - if len(json) == 0 { - return newTenError( - ErrnoInvalidArgument, - "json data is required.", - ) - } - - return withCGO(func() error { - return p.sendJSON( - unsafe.Pointer(unsafe.SliceData(json)), - len(json), - handler, - ) - }) -} - func (p *tenEnv) SendCmd(cmd Cmd, handler ResultHandler) error { if cmd == nil { return newTenError( 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 7f1c55bed7..29b16e972e 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 @@ -48,82 +48,6 @@ ten_go_status_t ten_go_ten_env_return_result(uintptr_t bridge_addr, ten_go_status_t ten_go_ten_env_return_result_directly( uintptr_t bridge_addr, uintptr_t cmd_result_bridge_addr); -ten_go_status_t ten_go_ten_env_return_json_bytes(uintptr_t bridge_addr, - int status_code, - const void *detail, - int detail_len, - uintptr_t cmd_bridge_addr); - -ten_go_status_t ten_go_ten_env_return_string(uintptr_t bridge_addr, - int status_code, - const void *detail, int detail_len, - uintptr_t cmd_bridge_addr); - -ten_go_status_t ten_go_ten_env_return_bool(uintptr_t bridge_addr, - int status_code, bool detail, - uintptr_t cmd_bridge_addr); - -ten_go_status_t ten_go_ten_env_return_int8(uintptr_t bridge_addr, - int status_code, int8_t detail, - uintptr_t cmd_bridge_addr); - -ten_go_status_t ten_go_ten_env_return_int16(uintptr_t bridge_addr, - int status_code, int16_t detail, - uintptr_t cmd_bridge_addr); - -ten_go_status_t ten_go_ten_env_return_int32(uintptr_t bridge_addr, - int status_code, int32_t detail, - uintptr_t cmd_bridge_addr); - -ten_go_status_t ten_go_ten_env_return_int64(uintptr_t bridge_addr, - int status_code, int64_t detail, - uintptr_t cmd_bridge_addr); - -ten_go_status_t ten_go_ten_env_return_uint8(uintptr_t bridge_addr, - int status_code, uint8_t detail, - uintptr_t cmd_bridge_addr); - -ten_go_status_t ten_go_ten_env_return_uint16(uintptr_t bridge_addr, - int status_code, uint16_t detail, - uintptr_t cmd_bridge_addr); - -ten_go_status_t ten_go_ten_env_return_uint32(uintptr_t bridge_addr, - int status_code, uint32_t detail, - uintptr_t cmd_bridge_addr); - -ten_go_status_t ten_go_ten_env_return_uint64(uintptr_t bridge_addr, - int status_code, uint64_t detail, - uintptr_t cmd_bridge_addr); - -ten_go_status_t ten_go_ten_env_return_float32(uintptr_t bridge_addr, - int status_code, float detail, - uintptr_t cmd_bridge_addr); - -ten_go_status_t ten_go_ten_env_return_float64(uintptr_t bridge_addr, - int status_code, double detail, - uintptr_t cmd_bridge_addr); - -ten_go_status_t ten_go_ten_env_return_buf(uintptr_t bridge_addr, - int status_code, void *detail, - int detail_len, - uintptr_t cmd_bridge_addr); - -/** - * @param json_bytes The pointer to the underlying buffer of the GO string or - * slice, which is passed with unsafe.Pointer in GO world, so the type of @a - * json_bytes is void*, not char*. Only the read operation is permitted. And the - * buffer is managed by GO, do not read it after the blocking cgo call. - * - * @param json_bytes_len The length of @a json_bytes. - * - * @param handler_id The index pointing to the callback function in the handle - * map in GO world. Note that the value `0` represents the callback is NULL. - */ -ten_go_status_t ten_go_ten_env_send_json(uintptr_t bridge_addr, - const void *json_bytes, - int json_bytes_len, - ten_go_handle_t handler_id); - ten_go_status_t ten_go_ten_env_send_cmd(uintptr_t bridge_addr, uintptr_t cmd_bridge_addr, ten_go_handle_t handler_id, bool is_ex); diff --git a/core/src/ten_runtime/binding/go/native/ten_env/ten_env_return_value.c b/core/src/ten_runtime/binding/go/native/ten_env/ten_env_return_value.c deleted file mode 100644 index ff241f9a76..0000000000 --- a/core/src/ten_runtime/binding/go/native/ten_env/ten_env_return_value.c +++ /dev/null @@ -1,357 +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/internal/common.h" -#include "include_internal/ten_runtime/binding/go/internal/json.h" -#include "include_internal/ten_runtime/binding/go/msg/msg.h" -#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 "include_internal/ten_runtime/binding/go/ten_env/ten_env_return_result.h" -#include "include_internal/ten_runtime/binding/go/value/value.h" -#include "ten_utils/macro/check.h" -#include "ten_runtime/binding/go/interface/ten/msg.h" -#include "ten_runtime/binding/go/interface/ten/ten_env.h" -#include "ten_utils/lib/error.h" -#include "ten_utils/value/value.h" - -ten_go_status_t ten_go_ten_env_return_string(uintptr_t bridge_addr, - int status_code, - const void *detail, int detail_len, - uintptr_t cmd_bridge_addr) { - 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."); - - ten_go_msg_t *cmd = ten_go_msg_reinterpret(cmd_bridge_addr); - TEN_ASSERT(cmd && ten_go_msg_check_integrity(cmd), "Should not happen."); - - ten_go_status_t api_status; - ten_go_status_init_with_errno(&api_status, TEN_ERRNO_OK); - - if (!detail) { - detail = ""; - detail_len = 0; - } - - ten_value_t *detail_value = - ten_value_create_string_with_size(detail, detail_len); - TEN_ASSERT(detail_value && ten_value_check_integrity(detail_value), - "Should not happen."); - - ten_go_ten_return_status_value(self, cmd, status_code, detail_value, - &api_status); - - return api_status; -} - -ten_go_status_t ten_go_ten_env_return_json_bytes(uintptr_t bridge_addr, - int status_code, - const void *detail, - int detail_len, - uintptr_t cmd_bridge_addr) { - 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."); - - ten_go_msg_t *cmd = ten_go_msg_reinterpret(cmd_bridge_addr); - TEN_ASSERT(cmd && ten_go_msg_check_integrity(cmd), "Should not happen."); - - ten_go_status_t api_status; - ten_go_status_init_with_errno(&api_status, TEN_ERRNO_OK); - - ten_json_t *json = ten_go_json_loads(detail, detail_len, &api_status); - if (json == NULL) { - return api_status; - } - - ten_value_t *detail_value = ten_value_from_json(json); - ten_json_destroy(json); - TEN_ASSERT(detail_value && ten_value_check_integrity(detail_value), - "Should not happen."); - - ten_go_ten_return_status_value(self, cmd, status_code, detail_value, - &api_status); - - return api_status; -} - -ten_go_status_t ten_go_ten_env_return_bool(uintptr_t bridge_addr, - int status_code, bool detail, - uintptr_t cmd_bridge_addr) { - 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."); - - ten_go_msg_t *cmd = ten_go_msg_reinterpret(cmd_bridge_addr); - TEN_ASSERT(cmd && ten_go_msg_check_integrity(cmd), "Should not happen."); - - ten_go_status_t api_status; - ten_go_status_init_with_errno(&api_status, TEN_ERRNO_OK); - - ten_value_t *detail_value = ten_value_create_bool(detail); - TEN_ASSERT(detail_value && ten_value_check_integrity(detail_value), - "Should not happen."); - - ten_go_ten_return_status_value(self, cmd, status_code, detail_value, - &api_status); - - return api_status; -} - -ten_go_status_t ten_go_ten_env_return_int8(uintptr_t bridge_addr, - int status_code, int8_t detail, - uintptr_t cmd_bridge_addr) { - 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."); - - ten_go_msg_t *cmd = ten_go_msg_reinterpret(cmd_bridge_addr); - TEN_ASSERT(cmd && ten_go_msg_check_integrity(cmd), "Should not happen."); - - ten_go_status_t api_status; - ten_go_status_init_with_errno(&api_status, TEN_ERRNO_OK); - - ten_value_t *detail_value = ten_value_create_int8(detail); - TEN_ASSERT(detail_value && ten_value_check_integrity(detail_value), - "Should not happen."); - - ten_go_ten_return_status_value(self, cmd, status_code, detail_value, - &api_status); - - return api_status; -} - -ten_go_status_t ten_go_ten_env_return_int16(uintptr_t bridge_addr, - int status_code, int16_t detail, - uintptr_t cmd_bridge_addr) { - 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."); - - ten_go_msg_t *cmd = ten_go_msg_reinterpret(cmd_bridge_addr); - TEN_ASSERT(cmd && ten_go_msg_check_integrity(cmd), "Should not happen."); - - ten_go_status_t api_status; - ten_go_status_init_with_errno(&api_status, TEN_ERRNO_OK); - - ten_value_t *detail_value = ten_value_create_int16(detail); - TEN_ASSERT(detail_value && ten_value_check_integrity(detail_value), - "Should not happen."); - - ten_go_ten_return_status_value(self, cmd, status_code, detail_value, - &api_status); - - return api_status; -} - -ten_go_status_t ten_go_ten_env_return_int32(uintptr_t bridge_addr, - int status_code, int32_t detail, - uintptr_t cmd_bridge_addr) { - 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."); - - ten_go_msg_t *cmd = ten_go_msg_reinterpret(cmd_bridge_addr); - TEN_ASSERT(cmd && ten_go_msg_check_integrity(cmd), "Should not happen."); - - ten_go_status_t api_status; - ten_go_status_init_with_errno(&api_status, TEN_ERRNO_OK); - - ten_value_t *detail_value = ten_value_create_int32(detail); - TEN_ASSERT(detail_value && ten_value_check_integrity(detail_value), - "Should not happen."); - - ten_go_ten_return_status_value(self, cmd, status_code, detail_value, - &api_status); - - return api_status; -} - -ten_go_status_t ten_go_ten_env_return_int64(uintptr_t bridge_addr, - int status_code, int64_t detail, - uintptr_t cmd_bridge_addr) { - 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."); - - ten_go_msg_t *cmd = ten_go_msg_reinterpret(cmd_bridge_addr); - TEN_ASSERT(cmd && ten_go_msg_check_integrity(cmd), "Should not happen."); - - ten_go_status_t api_status; - ten_go_status_init_with_errno(&api_status, TEN_ERRNO_OK); - - ten_value_t *detail_value = ten_value_create_int64(detail); - TEN_ASSERT(detail_value && ten_value_check_integrity(detail_value), - "Should not happen."); - - ten_go_ten_return_status_value(self, cmd, status_code, detail_value, - &api_status); - - return api_status; -} - -ten_go_status_t ten_go_ten_env_return_uint8(uintptr_t bridge_addr, - int status_code, uint8_t detail, - uintptr_t cmd_bridge_addr) { - 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."); - - ten_go_msg_t *cmd = ten_go_msg_reinterpret(cmd_bridge_addr); - TEN_ASSERT(cmd && ten_go_msg_check_integrity(cmd), "Should not happen."); - - ten_go_status_t api_status; - ten_go_status_init_with_errno(&api_status, TEN_ERRNO_OK); - - ten_value_t *detail_value = ten_value_create_uint8(detail); - TEN_ASSERT(detail_value && ten_value_check_integrity(detail_value), - "Should not happen."); - - ten_go_ten_return_status_value(self, cmd, status_code, detail_value, - &api_status); - - return api_status; -} - -ten_go_status_t ten_go_ten_env_return_uint16(uintptr_t bridge_addr, - int status_code, uint16_t detail, - uintptr_t cmd_bridge_addr) { - 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."); - - ten_go_msg_t *cmd = ten_go_msg_reinterpret(cmd_bridge_addr); - TEN_ASSERT(cmd && ten_go_msg_check_integrity(cmd), "Should not happen."); - - ten_go_status_t api_status; - ten_go_status_init_with_errno(&api_status, TEN_ERRNO_OK); - - ten_value_t *detail_value = ten_value_create_uint16(detail); - TEN_ASSERT(detail_value && ten_value_check_integrity(detail_value), - "Should not happen."); - - ten_go_ten_return_status_value(self, cmd, status_code, detail_value, - &api_status); - - return api_status; -} - -ten_go_status_t ten_go_ten_env_return_uint32(uintptr_t bridge_addr, - int status_code, uint32_t detail, - uintptr_t cmd_bridge_addr) { - 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."); - - ten_go_msg_t *cmd = ten_go_msg_reinterpret(cmd_bridge_addr); - TEN_ASSERT(cmd && ten_go_msg_check_integrity(cmd), "Should not happen."); - - ten_go_status_t api_status; - ten_go_status_init_with_errno(&api_status, TEN_ERRNO_OK); - - ten_value_t *detail_value = ten_value_create_uint32(detail); - TEN_ASSERT(detail_value && ten_value_check_integrity(detail_value), - "Should not happen."); - - ten_go_ten_return_status_value(self, cmd, status_code, detail_value, - &api_status); - - return api_status; -} - -ten_go_status_t ten_go_ten_env_return_uint64(uintptr_t bridge_addr, - int status_code, uint64_t detail, - uintptr_t cmd_bridge_addr) { - 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."); - - ten_go_msg_t *cmd = ten_go_msg_reinterpret(cmd_bridge_addr); - TEN_ASSERT(cmd && ten_go_msg_check_integrity(cmd), "Should not happen."); - - ten_go_status_t api_status; - ten_go_status_init_with_errno(&api_status, TEN_ERRNO_OK); - - ten_value_t *detail_value = ten_value_create_uint64(detail); - TEN_ASSERT(detail_value && ten_value_check_integrity(detail_value), - "Should not happen."); - - ten_go_ten_return_status_value(self, cmd, status_code, detail_value, - &api_status); - - return api_status; -} - -ten_go_status_t ten_go_ten_env_return_float32(uintptr_t bridge_addr, - int status_code, float detail, - uintptr_t cmd_bridge_addr) { - 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."); - - ten_go_msg_t *cmd = ten_go_msg_reinterpret(cmd_bridge_addr); - TEN_ASSERT(cmd && ten_go_msg_check_integrity(cmd), "Should not happen."); - - ten_go_status_t api_status; - ten_go_status_init_with_errno(&api_status, TEN_ERRNO_OK); - - ten_value_t *detail_value = ten_value_create_float32(detail); - TEN_ASSERT(detail_value && ten_value_check_integrity(detail_value), - "Should not happen."); - - ten_go_ten_return_status_value(self, cmd, status_code, detail_value, - &api_status); - - return api_status; -} - -ten_go_status_t ten_go_ten_env_return_float64(uintptr_t bridge_addr, - int status_code, double detail, - uintptr_t cmd_bridge_addr) { - 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."); - - ten_go_msg_t *cmd = ten_go_msg_reinterpret(cmd_bridge_addr); - TEN_ASSERT(cmd && ten_go_msg_check_integrity(cmd), "Should not happen."); - - ten_go_status_t api_status; - ten_go_status_init_with_errno(&api_status, TEN_ERRNO_OK); - - ten_value_t *detail_value = ten_value_create_float64(detail); - TEN_ASSERT(detail_value && ten_value_check_integrity(detail_value), - "Should not happen."); - - ten_go_ten_return_status_value(self, cmd, status_code, detail_value, - &api_status); - - return api_status; -} - -ten_go_status_t ten_go_ten_env_return_buf(uintptr_t bridge_addr, - int status_code, void *detail, - int detail_len, - uintptr_t cmd_bridge_addr) { - 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."); - TEN_ASSERT(detail, "Should not happen"); - - ten_go_msg_t *cmd = ten_go_msg_reinterpret(cmd_bridge_addr); - TEN_ASSERT(cmd && ten_go_msg_check_integrity(cmd), "Should not happen."); - - ten_go_status_t api_status; - ten_go_status_init_with_errno(&api_status, TEN_ERRNO_OK); - - ten_value_t *detail_value = ten_go_ten_value_create_buf(detail, detail_len); - TEN_ASSERT(detail_value && ten_value_check_integrity(detail_value), - "Should not happen."); - - ten_go_ten_return_status_value(self, cmd, status_code, detail_value, - &api_status); - - return api_status; -} diff --git a/core/src/ten_runtime/binding/go/native/ten_env/ten_env_send_json.c b/core/src/ten_runtime/binding/go/native/ten_env/ten_env_send_json.c deleted file mode 100644 index f76a5335f9..0000000000 --- a/core/src/ten_runtime/binding/go/native/ten_env/ten_env_send_json.c +++ /dev/null @@ -1,121 +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 "include_internal/ten_runtime/binding/go/internal/common.h" -#include "include_internal/ten_runtime/binding/go/internal/json.h" -#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/common.h" -#include "ten_runtime/binding/go/interface/ten/ten_env.h" -#include "ten_runtime/common/errno.h" -#include "ten_runtime/ten_env_proxy/ten_env_proxy.h" -#include "ten_utils/lib/alloc.h" -#include "ten_utils/lib/error.h" -#include "ten_utils/lib/json.h" -#include "ten_utils/macro/check.h" -#include "ten_utils/macro/mark.h" - -typedef struct ten_env_notify_send_json_info_t { - ten_json_t *c_json; - ten_go_handle_t handler_id; -} ten_env_notify_send_json_info_t; - -static ten_env_notify_send_json_info_t *ten_env_notify_send_json_info_create( - ten_json_t *c_json, ten_go_handle_t handler_id) { - TEN_ASSERT(c_json, "Invalid argument."); - - ten_env_notify_send_json_info_t *info = - TEN_MALLOC(sizeof(ten_env_notify_send_json_info_t)); - TEN_ASSERT(info, "Failed to allocate memory."); - - info->c_json = ten_json_incref(c_json); - info->handler_id = handler_id; - - return info; -} - -static void ten_env_notify_send_json_info_destroy( - ten_env_notify_send_json_info_t *info) { - TEN_ASSERT(info, "Invalid argument."); - - if (info->c_json) { - ten_json_destroy(info->c_json); - info->c_json = NULL; - } - - info->handler_id = 0; - - TEN_FREE(info); -} - -static void ten_env_proxy_notify_send_json(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_send_json_info_t *notify_info = user_data; - - ten_error_t err; - ten_error_init(&err); - - TEN_UNUSED bool res = false; - if (notify_info->handler_id == TEN_GO_NO_RESPONSE_HANDLER) { - res = ten_env_send_json(ten_env, notify_info->c_json, NULL, NULL, NULL); - } else { - ten_go_callback_info_t *info = - ten_go_callback_info_create(notify_info->handler_id); - res = ten_env_send_json(ten_env, notify_info->c_json, - proxy_send_xxx_callback, info, NULL); - } - - ten_error_deinit(&err); - - ten_env_notify_send_json_info_destroy(notify_info); -} - -ten_go_status_t ten_go_ten_env_send_json(uintptr_t bridge_addr, - const void *json_bytes, - int json_bytes_len, - ten_go_handle_t handler_id) { - 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."); - TEN_ASSERT(json_bytes && json_bytes_len > 0, "Should not happen."); - - ten_go_status_t status; - ten_go_status_init_with_errno(&status, TEN_ERRNO_OK); - - TEN_GO_TEN_ENV_IS_ALIVE_REGION_BEGIN( - self, { ten_go_status_set_errno(&status, TEN_ERRNO_TEN_IS_CLOSED); }); - - ten_json_t *json = ten_go_json_loads(json_bytes, json_bytes_len, &status); - if (json == NULL) { - return status; - } - - ten_env_notify_send_json_info_t *notify_info = - ten_env_notify_send_json_info_create( - json, handler_id <= 0 ? TEN_GO_NO_RESPONSE_HANDLER : handler_id); - ten_json_destroy(json); - - ten_error_t err; - ten_error_init(&err); - if (!ten_env_proxy_notify(self->c_ten_env_proxy, - ten_env_proxy_notify_send_json, notify_info, false, - &err)) { - ten_env_notify_send_json_info_destroy(notify_info); - ten_go_status_from_error(&status, &err); - } - - ten_error_deinit(&err); - TEN_GO_TEN_ENV_IS_ALIVE_REGION_END(self); - -ten_is_close: - return status; -} diff --git a/core/src/ten_runtime/binding/python/interface/ten/async_ten_env.py b/core/src/ten_runtime/binding/python/interface/ten/async_ten_env.py index 5bf9b71534..0a6083c646 100644 --- a/core/src/ten_runtime/binding/python/interface/ten/async_ten_env.py +++ b/core/src/ten_runtime/binding/python/interface/ten/async_ten_env.py @@ -57,22 +57,6 @@ async def send_cmd_ex(self, cmd: Cmd) -> AsyncGenerator[CmdResult, None]: break yield result - async def send_json(self, json_str: str) -> AsyncGenerator[CmdResult, None]: - q = asyncio.Queue(maxsize=10) - self._internal.send_json( - json_str, - lambda ten_env, result: asyncio.run_coroutine_threadsafe( - q.put(result), self._ten_loop - ), - ) - while True: - result: CmdResult = await q.get() - if result.is_completed(): - yield result - # This is the final result, so break the while loop. - break - yield result - def _deinit_routine(self) -> None: # Wait for the internal thread to finish. self._ten_thread.join() 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 34a5ab9824..0cc5bdd5c1 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 @@ -12,6 +12,13 @@ class _Msg: def from_json(self, json_str: str): ... def get_name(self) -> str: ... def set_name(self, name: str) -> None: ... + def set_dest( + self, + app_uri: str | None, + graph_id: str | None, + extension_group: str | None, + extension: str | None, + ) -> None: ... def set_property_from_json(self, key: str, json_str: str) -> None: ... def get_property_to_json(self, key: str) -> str: ... def get_property_int(self, path: str) -> int: ... @@ -104,7 +111,6 @@ class _TenEnv: def send_data(self, data: _Data) -> None: ... def send_video_frame(self, video_frame: _VideoFrame) -> None: ... def send_audio_frame(self, audio_frame: _AudioFrame) -> None: ... - def send_json(self, json_str: str, result_handler) -> None: ... 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: ... diff --git a/core/src/ten_runtime/binding/python/interface/ten/msg.py b/core/src/ten_runtime/binding/python/interface/ten/msg.py index 00a17d8f9b..743ecbf470 100644 --- a/core/src/ten_runtime/binding/python/interface/ten/msg.py +++ b/core/src/ten_runtime/binding/python/interface/ten/msg.py @@ -17,6 +17,17 @@ def from_json(self, json_str: str): def get_name(self) -> str: return _Msg.get_name(self) + def set_dest( + self, + app_uri: str | None, + graph_id: str | None, + extension_group: str | None, + extension: str | None, + ) -> None: + return _Msg.set_dest( + self, app_uri, graph_id, extension_group, extension + ) + def set_property_from_json(self, key: str, json: str) -> None: return _Msg.set_property_from_json(self, key, json) 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 06b726687a..09612cf853 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 @@ -81,9 +81,6 @@ def send_video_frame(self, video_frame: VideoFrame) -> None: def send_audio_frame(self, audio_frame: AudioFrame) -> None: return self._internal.send_audio_frame(audio_frame) - def send_json(self, json_str: str, result_handler: ResultHandler) -> None: - return self._internal.send_json(json_str, result_handler) - def return_result(self, result: CmdResult, target_cmd: Cmd) -> None: return self._internal.return_result(result, target_cmd) diff --git a/core/src/ten_runtime/binding/python/native/msg/msg.c b/core/src/ten_runtime/binding/python/native/msg/msg.c index 90ac709b0f..51d9b3107a 100644 --- a/core/src/ten_runtime/binding/python/native/msg/msg.c +++ b/core/src/ten_runtime/binding/python/native/msg/msg.c @@ -174,6 +174,50 @@ PyObject *ten_py_msg_set_name(PyObject *self, TEN_UNUSED PyObject *args) { } } +PyObject *ten_py_msg_set_dest(PyObject *self, TEN_UNUSED PyObject *args) { + ten_py_msg_t *py_msg = (ten_py_msg_t *)self; + + TEN_ASSERT(py_msg && ten_py_msg_check_integrity(py_msg), "Invalid argument."); + + if (PyTuple_GET_SIZE(args) != 4) { + return ten_py_raise_py_value_error_exception( + "Invalid argument count when set_dest."); + } + + ten_shared_ptr_t *c_msg = py_msg->c_msg; + if (!c_msg) { + TEN_ASSERT(0, "Should not happen."); + return ten_py_raise_py_value_error_exception("Msg is invalidated."); + } + + const char *app_uri = NULL; + const char *graph_id = NULL; + const char *extension_group_name = NULL; + const char *extension_name = NULL; + if (!PyArg_ParseTuple(args, "zzzz", &app_uri, &graph_id, + &extension_group_name, &extension_name)) { + return ten_py_raise_py_value_error_exception("Failed to parse arguments."); + } + + ten_error_t err; + ten_error_init(&err); + + bool rc = ten_msg_clear_and_set_dest( + c_msg, app_uri, graph_id, extension_group_name, extension_name, &err); + + if (!rc) { + ten_py_raise_py_value_error_exception(ten_error_errmsg(&err)); + } + + ten_error_deinit(&err); + + if (rc) { + Py_RETURN_NONE; + } else { + return NULL; + } +} + PyObject *ten_py_msg_set_property_string(PyObject *self, PyObject *args) { ten_py_msg_t *py_msg = (ten_py_msg_t *)self; diff --git a/core/src/ten_runtime/binding/python/native/msg/type.c b/core/src/ten_runtime/binding/python/native/msg/type.c index e97270573a..70b86740b0 100644 --- a/core/src/ten_runtime/binding/python/native/msg/type.c +++ b/core/src/ten_runtime/binding/python/native/msg/type.c @@ -17,6 +17,7 @@ static PyMethodDef ten_py_msg_type_methods[] = { {"from_json", ten_py_msg_from_json, METH_VARARGS, NULL}, {"get_name", ten_py_msg_get_name, METH_VARARGS, NULL}, {"set_name", ten_py_msg_set_name, METH_VARARGS, NULL}, + {"set_dest", ten_py_msg_set_dest, METH_VARARGS, NULL}, {"get_property_string", ten_py_msg_get_property_string, METH_VARARGS, NULL}, {"set_property_string", ten_py_msg_set_property_string, METH_VARARGS, NULL}, {"get_property_to_json", ten_py_msg_get_property_to_json, METH_VARARGS, 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 ff8d1615bd..1c6bfb7031 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 @@ -150,7 +150,6 @@ PyTypeObject *ten_py_ten_env_type(void) { {"send_data", ten_py_ten_env_send_data, METH_VARARGS, NULL}, {"send_video_frame", ten_py_ten_env_send_video_frame, METH_VARARGS, NULL}, {"send_audio_frame", ten_py_ten_env_send_audio_frame, METH_VARARGS, NULL}, - {"send_json", ten_py_ten_env_send_json, METH_VARARGS, NULL}, {"get_property_to_json", ten_py_ten_env_get_property_to_json, METH_VARARGS, NULL}, {"set_property_from_json", ten_py_ten_env_set_property_from_json, diff --git a/core/src/ten_runtime/binding/python/native/ten_env/ten_env_send_json.c b/core/src/ten_runtime/binding/python/native/ten_env/ten_env_send_json.c deleted file mode 100644 index 2fe0d2aa93..0000000000 --- a/core/src/ten_runtime/binding/python/native/ten_env/ten_env_send_json.c +++ /dev/null @@ -1,176 +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/common.h" -#include "include_internal/ten_runtime/binding/python/common/error.h" -#include "include_internal/ten_runtime/binding/python/msg/cmd_result.h" -#include "include_internal/ten_runtime/binding/python/ten_env/ten_env.h" -#include "include_internal/ten_runtime/msg/cmd_base/cmd_base.h" -#include "object.h" -#include "ten_runtime/extension/extension.h" -#include "ten_runtime/msg/cmd_result/cmd_result.h" -#include "ten_utils/lib/json.h" -#include "ten_utils/macro/mark.h" -#include "ten_utils/macro/memory.h" - -typedef struct ten_env_notify_send_json_info_t { - ten_json_t *c_json; - PyObject *py_cb_func; -} ten_env_notify_send_json_info_t; - -static ten_env_notify_send_json_info_t *ten_env_notify_send_json_info_create( - ten_json_t *c_json, PyObject *py_cb_func) { - TEN_ASSERT(c_json, "Invalid argument."); - - ten_env_notify_send_json_info_t *info = - TEN_MALLOC(sizeof(ten_env_notify_send_json_info_t)); - TEN_ASSERT(info, "Failed to allocate memory."); - - info->c_json = ten_json_incref(c_json); - info->py_cb_func = py_cb_func; - - if (py_cb_func != NULL) { - Py_INCREF(py_cb_func); - } - - return info; -} - -static void proxy_send_xxx_callback(ten_extension_t *extension, - ten_env_t *ten_env, - ten_shared_ptr_t *cmd_result, - void *callback_info) { - TEN_ASSERT(extension && ten_extension_check_integrity(extension, true), - "Should not happen."); - TEN_ASSERT(ten_env && ten_env_check_integrity(ten_env, true), - "Should not happen."); - TEN_ASSERT(cmd_result && ten_cmd_base_check_integrity(cmd_result), - "Should not happen."); - TEN_ASSERT(callback_info, "Should not happen."); - - // About to call the Python function, so it's necessary to ensure that the GIL - // has been acquired. - // - // Allows C codes to work safely with Python objects. - PyGILState_STATE prev_state = ten_py_gil_state_ensure(); - - ten_py_ten_env_t *py_ten_env = ten_py_ten_env_wrap(ten_env); - ten_py_cmd_result_t *cmd_result_bridge = ten_py_cmd_result_wrap(cmd_result); - - PyObject *cb_func = callback_info; - PyObject *arglist = - Py_BuildValue("(OO)", py_ten_env->actual_py_ten_env, cmd_result_bridge); - - PyObject *result = PyObject_CallObject(cb_func, arglist); - if (!result || ten_py_check_and_clear_py_error()) { - Py_XDECREF(result); // Ensure cleanup if an error occurred. - } - - Py_XDECREF(arglist); - - bool is_final = ten_cmd_result_is_final(cmd_result, NULL); - if (is_final) { - Py_XDECREF(cb_func); - } - - ten_py_cmd_result_invalidate(cmd_result_bridge); - - ten_py_gil_state_release(prev_state); -} - -static void ten_env_notify_send_json_info_destroy( - ten_env_notify_send_json_info_t *info) { - TEN_ASSERT(info, "Invalid argument."); - - if (info->c_json) { - ten_json_destroy(info->c_json); - info->c_json = NULL; - } - - info->py_cb_func = NULL; - - TEN_FREE(info); -} - -static void ten_env_proxy_notify_send_json(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_send_json_info_t *notify_info = user_data; - - ten_error_t err; - ten_error_init(&err); - - TEN_UNUSED bool res = false; - if (notify_info->py_cb_func == NULL) { - res = ten_env_send_json(ten_env, notify_info->c_json, NULL, NULL, NULL); - } else { - res = - ten_env_send_json(ten_env, notify_info->c_json, proxy_send_xxx_callback, - notify_info->py_cb_func, NULL); - } - - ten_error_deinit(&err); - - ten_env_notify_send_json_info_destroy(notify_info); -} - -PyObject *ten_py_ten_env_send_json(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."); - - bool success = true; - - ten_error_t err; - ten_error_init(&err); - - char *json_str = NULL; - PyObject *cb_func = NULL; - - if (!PyArg_ParseTuple(args, "sO", &json_str, &cb_func)) { - success = false; - goto done; - } - - // Check if cb_func is callable. - if (!PyCallable_Check(cb_func)) { - cb_func = NULL; - } - - ten_json_t *json = ten_json_from_string(json_str, &err); - if (!json) { - ten_py_raise_py_value_error_exception(ten_error_errmsg(&err)); - - success = false; - goto done; - } - - ten_env_notify_send_json_info_t *notify_info = - ten_env_notify_send_json_info_create(json, cb_func); - ten_json_destroy(json); - - if (!ten_env_proxy_notify(py_ten_env->c_ten_env_proxy, - ten_env_proxy_notify_send_json, notify_info, false, - &err)) { - ten_py_raise_py_runtime_error_exception(ten_error_errmsg(&err)); - - ten_env_notify_send_json_info_destroy(notify_info); - success = false; - goto done; - } - -done: - ten_error_deinit(&err); - - if (success) { - Py_RETURN_NONE; - } else { - return NULL; - } -} diff --git a/core/src/ten_runtime/engine/msg_interface/start_graph.c b/core/src/ten_runtime/engine/msg_interface/start_graph.c index 3a1f2978b6..6435d031d0 100644 --- a/core/src/ten_runtime/engine/msg_interface/start_graph.c +++ b/core/src/ten_runtime/engine/msg_interface/start_graph.c @@ -20,6 +20,7 @@ #include "include_internal/ten_runtime/remote/remote.h" #include "include_internal/ten_utils/log/log.h" #include "include_internal/ten_utils/value/value.h" +#include "ten_runtime/msg/cmd/start_graph/cmd.h" #include "ten_runtime/msg/cmd_result/cmd_result.h" #include "ten_utils/container/list.h" #include "ten_utils/container/list_ptr.h" @@ -77,7 +78,7 @@ void ten_engine_handle_cmd_start_graph(ten_engine_t *self, // The remote app does not recognize the local app's // 'predefined_graph_name', so this field should not be included in the // 'start_graph' command which will be sent to the remote app. - ten_cmd_start_graph_set_predefined_graph_name(child_cmd, ""); + ten_cmd_start_graph_set_predefined_graph_name(child_cmd, "", err); // Use the uri of the local app to fill/override the value of 'from' // field (even if there is any old value in the 'from' field), so that diff --git a/core/src/ten_runtime/msg/cmd_base/cmd/start_graph/cmd.c b/core/src/ten_runtime/msg/cmd_base/cmd/start_graph/cmd.c index 956c7206c9..cd7c8d6741 100644 --- a/core/src/ten_runtime/msg/cmd_base/cmd/start_graph/cmd.c +++ b/core/src/ten_runtime/msg/cmd_base/cmd/start_graph/cmd.c @@ -103,6 +103,35 @@ bool ten_raw_cmd_start_graph_as_msg_init_from_json(ten_msg_t *self, json, err); } +static bool ten_raw_cmd_start_graph_as_msg_init_from_json_str( + ten_msg_t *self, const char *json_str, ten_error_t *err) { + TEN_ASSERT(self && ten_raw_cmd_check_integrity((ten_cmd_t *)self), + "Invalid argument."); + TEN_ASSERT(json_str, "Invalid argument."); + + ten_json_t *json = ten_json_from_string(json_str, err); + if (!json) { + return false; + } + + bool rc = ten_raw_cmd_start_graph_init_from_json( + (ten_cmd_start_graph_t *)self, json, err); + + ten_json_destroy(json); + + return rc; +} + +bool ten_cmd_start_graph_init_from_json_str(ten_shared_ptr_t *self, + const char *json_str, + ten_error_t *err) { + TEN_ASSERT(self && ten_cmd_check_integrity(self), "Invalid argument."); + TEN_ASSERT(json_str, "Invalid argument."); + + return ten_raw_cmd_start_graph_as_msg_init_from_json_str( + ten_msg_get_raw_msg(self), json_str, err); +} + static ten_cmd_start_graph_t *ten_raw_cmd_start_graph_create_from_json( ten_json_t *json, ten_error_t *err) { TEN_ASSERT(json, "Should not happen."); @@ -377,14 +406,15 @@ bool ten_cmd_start_graph_get_long_running_mode(ten_shared_ptr_t *self) { return ten_raw_cmd_start_graph_get_long_running_mode(get_raw_cmd(self)); } -void ten_cmd_start_graph_set_predefined_graph_name( - ten_shared_ptr_t *self, const char *predefined_graph_name) { +bool ten_cmd_start_graph_set_predefined_graph_name( + ten_shared_ptr_t *self, const char *predefined_graph_name, + ten_error_t *err) { TEN_ASSERT(self && ten_cmd_base_check_integrity(self) && ten_msg_get_type(self) == TEN_MSG_TYPE_CMD_START_GRAPH, "Should not happen."); - ten_value_set_string(&get_raw_cmd(self)->predefined_graph_name, - predefined_graph_name); + return ten_value_set_string(&get_raw_cmd(self)->predefined_graph_name, + predefined_graph_name); } ten_string_t *ten_raw_cmd_start_graph_get_predefined_graph_name( diff --git a/core/src/ten_runtime/msg/cmd_base/cmd/timer/cmd.c b/core/src/ten_runtime/msg/cmd_base/cmd/timer/cmd.c index 5ce964ee64..904cc8e9ac 100644 --- a/core/src/ten_runtime/msg/cmd_base/cmd/timer/cmd.c +++ b/core/src/ten_runtime/msg/cmd_base/cmd/timer/cmd.c @@ -31,20 +31,20 @@ static ten_cmd_timer_t *get_raw_cmd(ten_shared_ptr_t *self) { return (ten_cmd_timer_t *)ten_shared_ptr_get_data(self); } -void ten_raw_cmd_timer_set_timer_id(ten_cmd_timer_t *self, uint32_t timer_id) { +bool ten_raw_cmd_timer_set_timer_id(ten_cmd_timer_t *self, uint32_t timer_id) { TEN_ASSERT( self && ten_raw_msg_get_type((ten_msg_t *)self) == TEN_MSG_TYPE_CMD_TIMER, "Should not happen."); - ten_value_set_uint32(&self->timer_id, timer_id); + return ten_value_set_uint32(&self->timer_id, timer_id); } -void ten_raw_cmd_timer_set_times(ten_cmd_timer_t *self, int32_t times) { +bool ten_raw_cmd_timer_set_times(ten_cmd_timer_t *self, int32_t times) { TEN_ASSERT( self && ten_raw_msg_get_type((ten_msg_t *)self) == TEN_MSG_TYPE_CMD_TIMER, "Should not happen."); - ten_value_set_int32(&self->times, times); + return ten_value_set_int32(&self->times, times); } static void ten_raw_cmd_timer_destroy(ten_cmd_timer_t *self) { @@ -206,6 +206,23 @@ uint64_t ten_cmd_timer_get_timeout_in_us(ten_shared_ptr_t *self) { return ten_raw_cmd_timer_get_timeout_in_us(get_raw_cmd(self)); } +static bool ten_raw_cmd_timer_set_timeout_in_us(ten_cmd_timer_t *self, + uint64_t timeout_in_us) { + TEN_ASSERT( + self && ten_raw_msg_get_type((ten_msg_t *)self) == TEN_MSG_TYPE_CMD_TIMER, + "Should not happen."); + + return ten_value_set_uint64(&self->timeout_in_us, timeout_in_us); +} + +bool ten_cmd_timer_set_timeout_in_us(ten_shared_ptr_t *self, + uint64_t timeout_in_us) { + TEN_ASSERT(self && ten_msg_get_type(self) == TEN_MSG_TYPE_CMD_TIMER, + "Should not happen."); + + return ten_raw_cmd_timer_set_timeout_in_us(get_raw_cmd(self), timeout_in_us); +} + int32_t ten_raw_cmd_timer_get_times(ten_cmd_timer_t *self) { TEN_ASSERT( self && ten_raw_msg_get_type((ten_msg_t *)self) == TEN_MSG_TYPE_CMD_TIMER, @@ -318,10 +335,10 @@ int32_t ten_cmd_timer_get_times(ten_shared_ptr_t *self) { return ten_raw_cmd_timer_get_times(get_raw_cmd(self)); } -void ten_cmd_timer_set_timer_id(ten_shared_ptr_t *self, uint32_t timer_id) { - ten_raw_cmd_timer_set_timer_id(get_raw_cmd(self), timer_id); +bool ten_cmd_timer_set_timer_id(ten_shared_ptr_t *self, uint32_t timer_id) { + return ten_raw_cmd_timer_set_timer_id(get_raw_cmd(self), timer_id); } -void ten_cmd_timer_set_times(ten_shared_ptr_t *self, int32_t times) { - ten_raw_cmd_timer_set_times(get_raw_cmd(self), times); +bool ten_cmd_timer_set_times(ten_shared_ptr_t *self, int32_t times) { + return ten_raw_cmd_timer_set_times(get_raw_cmd(self), times); } diff --git a/core/src/ten_runtime/ten_env/internal/send.c b/core/src/ten_runtime/ten_env/internal/send.c index dd87f6839e..38baf0ed3d 100644 --- a/core/src/ten_runtime/ten_env/internal/send.c +++ b/core/src/ten_runtime/ten_env/internal/send.c @@ -192,39 +192,6 @@ bool ten_env_send_cmd_ex(ten_env_t *self, ten_shared_ptr_t *cmd, result_handler_user_data, err); } -static bool ten_env_send_json_internal( - ten_env_t *self, ten_json_t *json, - ten_env_cmd_result_handler_func_t result_handler, - void *result_handler_user_data, ten_error_t *err) { - TEN_ASSERT(self, "Invalid argument."); - TEN_ASSERT(ten_env_check_integrity(self, true), "Invalid use of ten_env %p.", - self); - TEN_ASSERT(json, "Should not happen."); - - bool success = false; - - ten_shared_ptr_t *cmd = ten_msg_create_from_json(json, err); - if (cmd) { - success = ten_send_msg_internal(self, cmd, result_handler, - result_handler_user_data, err); - ten_shared_ptr_destroy(cmd); - } - - return success; -} - -bool ten_env_send_json(ten_env_t *self, ten_json_t *json, - ten_env_cmd_result_handler_func_t result_handler, - void *result_handler_user_data, ten_error_t *err) { - TEN_ASSERT(self, "Invalid argument."); - TEN_ASSERT(ten_env_check_integrity(self, true), "Invalid use of ten_env %p.", - self); - TEN_ASSERT(json, "Should not happen."); - - return ten_env_send_json_internal(self, json, result_handler, - result_handler_user_data, err); -} - bool ten_env_send_data(ten_env_t *self, ten_shared_ptr_t *data, ten_error_t *err) { TEN_ASSERT(self, "Invalid argument."); diff --git a/packages/example_extensions/aio_http_server_python/main.py b/packages/example_extensions/aio_http_server_python/main.py index eb949ada05..42dfdf40b6 100644 --- a/packages/example_extensions/aio_http_server_python/main.py +++ b/packages/example_extensions/aio_http_server_python/main.py @@ -37,13 +37,9 @@ async def default_handler(self, request: web_request.Request): else: # If the command is a 'close_app' command, send it to the app. if "type" in data["_ten"] and data["_ten"]["type"] == "close_app": - close_app_cmd_json = ( - '{"_ten":{"type":"close_app",' - '"dest":[{"app":"localhost"}]}}' - ) - asyncio.create_task( - anext(self.ten_env.send_json(close_app_cmd_json)) - ) + close_app_cmd = Cmd.create("ten:close_app") + close_app_cmd.set_dest("localhost", None, None, None) + asyncio.create_task(self.ten_env.send_cmd(close_app_cmd)) return web.Response(status=200, text="OK") elif "name" in data["_ten"]: # Send the command to the TEN runtime. diff --git a/tests/ten_runtime/integration/cpp/check_start_graph/check_start_graph_source/ten_packages/extension/default_extension_cpp/src/main.cc b/tests/ten_runtime/integration/cpp/check_start_graph/check_start_graph_source/ten_packages/extension/default_extension_cpp/src/main.cc index 76ab4bb81a..80c7de741c 100644 --- a/tests/ten_runtime/integration/cpp/check_start_graph/check_start_graph_source/ten_packages/extension/default_extension_cpp/src/main.cc +++ b/tests/ten_runtime/integration/cpp/check_start_graph/check_start_graph_source/ten_packages/extension/default_extension_cpp/src/main.cc @@ -9,6 +9,7 @@ #include #include "ten_runtime/binding/cpp/internal/msg/cmd/close_app.h" +#include "ten_runtime/binding/cpp/internal/msg/cmd/start_graph.h" #include "ten_runtime/binding/cpp/internal/ten_env.h" #include "ten_runtime/binding/cpp/ten.h" @@ -24,13 +25,10 @@ class test_extension : public ten::extension_t { void send_invalid_graph(ten::ten_env_t &ten_env) { ten::error_t err; - bool result = ten_env.send_json( - R"({ + auto start_graph_cmd = ten::cmd_start_graph_t::create(); + start_graph_cmd->set_dest("localhost", nullptr, nullptr, nullptr); + bool result = start_graph_cmd->set_nodes_and_connections_from_json(R"({ "_ten": { - "type": "start_graph", - "dest": [{ - "app": "localhost" - }], "nodes": [ { "type": "extension", @@ -64,8 +62,7 @@ class test_extension : public ten::extension_t { ] } })", - nullptr, &err); - + &err); assert(!result && "The graph should be invalid."); // The extension_info is not found, extension_group: @@ -85,13 +82,10 @@ class test_extension : public ten::extension_t { send_invalid_graph(ten_env); - ten_env.send_json( - R"({ + auto start_graph_cmd = ten::cmd_start_graph_t::create(); + start_graph_cmd->set_dest("localhost", nullptr, nullptr, nullptr); + start_graph_cmd->set_nodes_and_connections_from_json(R"({ "_ten": { - "type": "start_graph", - "dest": [{ - "app": "localhost" - }], "nodes": [ { "type": "extension", @@ -101,7 +95,10 @@ class test_extension : public ten::extension_t { } ] } - })", + })"); + + ten_env.send_cmd( + std::move(start_graph_cmd), [](ten::ten_env_t &env, std::unique_ptr result) { // The graph check should be passed. if (result->get_status_code() == TEN_STATUS_CODE_OK) { diff --git a/tests/ten_runtime/integration/go/BUILD.gn b/tests/ten_runtime/integration/go/BUILD.gn index 54a66de261..8a2b65501c 100644 --- a/tests/ten_runtime/integration/go/BUILD.gn +++ b/tests/ten_runtime/integration/go/BUILD.gn @@ -20,7 +20,6 @@ group("go") { "prepare_to_stop_go", "return_result_go", "return_value_go", - "send_json_go", "start_app_sync_go", "three_extension_cmd_go", "transfer_pointer_go", diff --git a/tests/ten_runtime/integration/go/send_json_go/BUILD.gn b/tests/ten_runtime/integration/go/send_json_go/BUILD.gn deleted file mode 100644 index b05ea6eaea..0000000000 --- a/tests/ten_runtime/integration/go/send_json_go/BUILD.gn +++ /dev/null @@ -1,65 +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. -# -import("//build/ten_runtime/feature/test.gni") -import("//build/ten_runtime/ten.gni") - -ten_package_test_prepare_app("send_json_go_app") { - src_app = "default_app_go" - src_app_language = "go" - generated_app_src_root_dir_name = "send_json_go_app" - - replace_files_after_install_app = [ - "send_json_go_app/manifest.json", - "send_json_go_app/property.json", - "send_json_go_app/ten_packages", - ] - - if (ten_enable_package_manager) { - deps = [ - "//core/src/ten_manager", - "//packages/core_apps/default_app_go:upload_default_app_go_to_server", - "//packages/core_extensions/default_extension_go:upload_default_extension_go_to_server", - "//packages/core_protocols/msgpack:upload_protocol_msgpack_to_server", - ] - } -} - -ten_package_test_prepare_client("send_json_go_app_client") { - sources = [ "client/client.cc" ] - include_dirs = [ - "//core/src", - "//core", - "//packages", - "//tests", - ] - deps = [ - "//core/src/ten_runtime", - "//packages/core_protocols/msgpack:msgpack_files", - "//tests/common/client:msgpack_client", - "//third_party/msgpack:msgpackc", - "//third_party/nlohmann_json", - ] -} - -ten_package_test_prepare_auxiliary_resources("send_json_go_app_test_files") { - resources = [ - "//tests/ten_runtime/integration/common=>common", - "__init__.py", - "test_case.py", - ] - if (enable_sanitizer && !is_clang) { - resources += [ "//tests/ten_runtime/integration/tools/use_asan_lib_marker=>use_asan_lib_marker" ] - } -} - -group("send_json_go") { - deps = [ - ":send_json_go_app", - ":send_json_go_app_client", - ":send_json_go_app_test_files", - ] -} diff --git a/tests/ten_runtime/integration/go/send_json_go/__init__.py b/tests/ten_runtime/integration/go/send_json_go/__init__.py deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/tests/ten_runtime/integration/go/send_json_go/client/client.cc b/tests/ten_runtime/integration/go/send_json_go/client/client.cc deleted file mode 100644 index bb8517e7c7..0000000000 --- a/tests/ten_runtime/integration/go/send_json_go/client/client.cc +++ /dev/null @@ -1,38 +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 "ten_utils/macro/mark.h" -#include "tests/common/client/cpp/msgpack_tcp.h" - -int main(TEN_UNUSED int argc, TEN_UNUSED char **argv) { - // Create a client and connect to the app. - auto *client = new ten::msgpack_tcp_client_t("msgpack://127.0.0.1:8007/"); - - nlohmann::json resp = client->send_json_and_recv_resp_in_json( - R"({ - "_ten": { - "name": "A", - "seq_id": "238", - "dest": [{ - "app": "msgpack://127.0.0.1:8007/", - "graph": "default", - "extension_group": "nodetest_group", - "extension": "A" - }] - } - })"_json); - TEN_ASSERT(TEN_STATUS_CODE_OK == resp["_ten"]["status_code"], - "Should not happen."); - - std::string resp_str = resp["detail"]; - TEN_LOGD("got result: %s", resp_str.c_str()); - TEN_ASSERT(resp_str == std::string("I am cExtension"), "Should not happen."); - - // NOTE the order: client destroy, then connection lost, then nodejs exits - delete client; -} diff --git a/tests/ten_runtime/integration/go/send_json_go/send_json_go_app/manifest.json b/tests/ten_runtime/integration/go/send_json_go/send_json_go_app/manifest.json deleted file mode 100644 index 93f155f436..0000000000 --- a/tests/ten_runtime/integration/go/send_json_go/send_json_go_app/manifest.json +++ /dev/null @@ -1,24 +0,0 @@ -{ - "dependencies": [ - { - "type": "extension", - "name": "extension_a", - "version": "0.3.0-alpha" - }, - { - "type": "extension", - "name": "extension_b", - "version": "0.3.0-alpha" - }, - { - "type": "extension", - "name": "extension_c", - "version": "0.3.0-alpha" - }, - { - "type": "protocol", - "name": "msgpack", - "version": "0.3.0" - } - ] -} \ No newline at end of file diff --git a/tests/ten_runtime/integration/go/send_json_go/send_json_go_app/property.json b/tests/ten_runtime/integration/go/send_json_go/send_json_go_app/property.json deleted file mode 100644 index 178fd94f3a..0000000000 --- a/tests/ten_runtime/integration/go/send_json_go/send_json_go_app/property.json +++ /dev/null @@ -1,65 +0,0 @@ -{ - "_ten": { - "uri": "msgpack://127.0.0.1:8007/", - "log_level": 2, - "predefined_graphs": [ - { - "name": "default", - "auto_start": true, - "singleton": true, - "nodes": [ - { - "type": "extension", - "name": "A", - "addon": "extension_a", - "extension_group": "nodetest_group" - }, - { - "type": "extension", - "name": "B", - "addon": "extension_b", - "extension_group": "nodetest_group" - }, - { - "type": "extension", - "name": "C", - "addon": "extension_c", - "extension_group": "nodetest_group" - } - ], - "connections": [ - { - "extension_group": "nodetest_group", - "extension": "A", - "cmd": [ - { - "name": "B", - "dest": [ - { - "extension_group": "nodetest_group", - "extension": "B" - } - ] - } - ] - }, - { - "extension_group": "nodetest_group", - "extension": "B", - "cmd": [ - { - "name": "C", - "dest": [ - { - "extension_group": "nodetest_group", - "extension": "C" - } - ] - } - ] - } - ] - } - ] - } -} \ No newline at end of file diff --git a/tests/ten_runtime/integration/go/send_json_go/send_json_go_app/ten_packages/extension/extension_a/extension.go b/tests/ten_runtime/integration/go/send_json_go/send_json_go_app/ten_packages/extension/extension_a/extension.go deleted file mode 100644 index cb31bb16d0..0000000000 --- a/tests/ten_runtime/integration/go/send_json_go/send_json_go_app/ten_packages/extension/extension_a/extension.go +++ /dev/null @@ -1,78 +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 https://github.com/TEN-framework/ten_framework/LICENSE for more -// information. -// - -package default_extension_go - -import ( - "fmt" - - "ten_framework/ten" -) - -type aExtension struct { - aCmd ten.Cmd - name string - ten.DefaultExtension -} - -func newAExtension(name string) ten.Extension { - return &aExtension{name: name} -} - -func (p *aExtension) OnCmd( - tenEnv ten.TenEnv, - cmd ten.Cmd, -) { - go func() { - cmdName, _ := cmd.GetName() - fmt.Println( - "aExtension receive command: ", - cmdName, - ) - - if cmdName == "A" { - // Cache the cmd for returning it in the future. - p.aCmd = cmd - err := tenEnv.SendJSON(`{ - "_ten": { - "name": "B" - } - }`, func(tenEnv ten.TenEnv, cmdStatus ten.CmdResult) { - detail, err := cmdStatus.GetPropertyString("detail") - if err != nil { - panic("Should not happen.") - } - - cmdResult, _ := ten.NewCmdResult(ten.StatusCodeOk) - cmdResult.SetPropertyString("detail", detail) - err = tenEnv.ReturnResult(cmdResult, p.aCmd) - if err != nil { - panic("Should not happen.") - } - }) - if err != nil { - panic("aExtension sendCmd B failed") - } - - return - } - }() -} - -func init() { - fmt.Println("call init") - - // Register addon - err := ten.RegisterAddonAsExtension( - "extension_a", - ten.NewDefaultExtensionAddon(newAExtension), - ) - if err != nil { - fmt.Println("register addon failed", err) - } -} diff --git a/tests/ten_runtime/integration/go/send_json_go/send_json_go_app/ten_packages/extension/extension_a/go.mod b/tests/ten_runtime/integration/go/send_json_go/send_json_go_app/ten_packages/extension/extension_a/go.mod deleted file mode 100644 index 9c3192343b..0000000000 --- a/tests/ten_runtime/integration/go/send_json_go/send_json_go_app/ten_packages/extension/extension_a/go.mod +++ /dev/null @@ -1,7 +0,0 @@ -module extension_a - -go 1.18 - -replace ten_framework => ../../../ten_packages/system/ten_runtime_go/interface - -require ten_framework v0.0.0-00010101000000-000000000000 diff --git a/tests/ten_runtime/integration/go/send_json_go/send_json_go_app/ten_packages/extension/extension_a/manifest.json b/tests/ten_runtime/integration/go/send_json_go/send_json_go_app/ten_packages/extension/extension_a/manifest.json deleted file mode 100644 index 71f733b27f..0000000000 --- a/tests/ten_runtime/integration/go/send_json_go/send_json_go_app/ten_packages/extension/extension_a/manifest.json +++ /dev/null @@ -1,18 +0,0 @@ -{ - "type": "extension", - "name": "extension_a", - "version": "0.3.0-alpha", - "dependencies": [ - { - "type": "system", - "name": "ten_runtime_go", - "version": "0.3.0" - } - ], - "package": { - "include": [ - "**" - ] - }, - "api": {} -} \ No newline at end of file diff --git a/tests/ten_runtime/integration/go/send_json_go/send_json_go_app/ten_packages/extension/extension_a/property.json b/tests/ten_runtime/integration/go/send_json_go/send_json_go_app/ten_packages/extension/extension_a/property.json deleted file mode 100644 index 9e26dfeeb6..0000000000 --- a/tests/ten_runtime/integration/go/send_json_go/send_json_go_app/ten_packages/extension/extension_a/property.json +++ /dev/null @@ -1 +0,0 @@ -{} \ No newline at end of file diff --git a/tests/ten_runtime/integration/go/send_json_go/send_json_go_app/ten_packages/extension/extension_b/extension.go b/tests/ten_runtime/integration/go/send_json_go/send_json_go_app/ten_packages/extension/extension_b/extension.go deleted file mode 100644 index 35dd88879c..0000000000 --- a/tests/ten_runtime/integration/go/send_json_go/send_json_go_app/ten_packages/extension/extension_b/extension.go +++ /dev/null @@ -1,74 +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 https://github.com/TEN-framework/ten_framework/LICENSE for more -// information. -// - -package default_extension_go - -import ( - "fmt" - - "ten_framework/ten" -) - -type bExtension struct { - ten.DefaultExtension -} - -func newBExtension(name string) ten.Extension { - return &bExtension{} -} - -func (p *bExtension) OnCmd( - tenEnv ten.TenEnv, - cmd ten.Cmd, -) { - go func() { - cmdName, _ := cmd.GetName() - fmt.Println( - "bExtension receive command: ", - cmdName, - ) - - if cmdName == "B" { - err := tenEnv.SendJSON(`{ - "_ten": { - "name": "C" - } - }`, func(tenEnv ten.TenEnv, cmdStatus ten.CmdResult) { - detail, err := cmdStatus.GetPropertyString("detail") - if err != nil { - panic("should not happen") - } - - cmdResult, _ := ten.NewCmdResult(ten.StatusCodeOk) - cmdResult.SetPropertyString("detail", detail) - err = tenEnv.ReturnResult(cmdResult, cmd) - if err != nil { - panic("should not happen") - } - }) - if err != nil { - panic("bExtension sendCmd C failed") - } - - return - } - }() -} - -func init() { - fmt.Println("call init") - - // Register addon - err := ten.RegisterAddonAsExtension( - "extension_b", - ten.NewDefaultExtensionAddon(newBExtension), - ) - if err != nil { - fmt.Println("register addon failed", err) - } -} diff --git a/tests/ten_runtime/integration/go/send_json_go/send_json_go_app/ten_packages/extension/extension_b/go.mod b/tests/ten_runtime/integration/go/send_json_go/send_json_go_app/ten_packages/extension/extension_b/go.mod deleted file mode 100644 index 8bfda874b6..0000000000 --- a/tests/ten_runtime/integration/go/send_json_go/send_json_go_app/ten_packages/extension/extension_b/go.mod +++ /dev/null @@ -1,7 +0,0 @@ -module extension_b - -go 1.18 - -replace ten_framework => ../../../ten_packages/system/ten_runtime_go/interface - -require ten_framework v0.0.0-00010101000000-000000000000 diff --git a/tests/ten_runtime/integration/go/send_json_go/send_json_go_app/ten_packages/extension/extension_b/manifest.json b/tests/ten_runtime/integration/go/send_json_go/send_json_go_app/ten_packages/extension/extension_b/manifest.json deleted file mode 100644 index d8f3b40a14..0000000000 --- a/tests/ten_runtime/integration/go/send_json_go/send_json_go_app/ten_packages/extension/extension_b/manifest.json +++ /dev/null @@ -1,18 +0,0 @@ -{ - "type": "extension", - "name": "extension_b", - "version": "0.3.0-alpha", - "dependencies": [ - { - "type": "system", - "name": "ten_runtime_go", - "version": "0.3.0" - } - ], - "package": { - "include": [ - "**" - ] - }, - "api": {} -} \ No newline at end of file diff --git a/tests/ten_runtime/integration/go/send_json_go/send_json_go_app/ten_packages/extension/extension_b/property.json b/tests/ten_runtime/integration/go/send_json_go/send_json_go_app/ten_packages/extension/extension_b/property.json deleted file mode 100644 index 9e26dfeeb6..0000000000 --- a/tests/ten_runtime/integration/go/send_json_go/send_json_go_app/ten_packages/extension/extension_b/property.json +++ /dev/null @@ -1 +0,0 @@ -{} \ No newline at end of file diff --git a/tests/ten_runtime/integration/go/send_json_go/send_json_go_app/ten_packages/extension/extension_c/extension.go b/tests/ten_runtime/integration/go/send_json_go/send_json_go_app/ten_packages/extension/extension_c/extension.go deleted file mode 100644 index c4db9a54bf..0000000000 --- a/tests/ten_runtime/integration/go/send_json_go/send_json_go_app/ten_packages/extension/extension_c/extension.go +++ /dev/null @@ -1,58 +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 https://github.com/TEN-framework/ten_framework/LICENSE for more -// information. -// - -package default_extension_go - -import ( - "fmt" - - "ten_framework/ten" -) - -type cExtension struct { - ten.DefaultExtension -} - -func newCExtension(name string) ten.Extension { - return &cExtension{} -} - -func (p *cExtension) OnCmd( - tenEnv ten.TenEnv, - cmd ten.Cmd, -) { - go func() { - cmdName, _ := cmd.GetName() - fmt.Println( - "cExtension receive command: ", - cmdName, - ) - - if cmdName == "C" { - cmdResult, _ := ten.NewCmdResult(ten.StatusCodeOk) - cmdResult.SetPropertyString("detail", "I am cExtension") - err := tenEnv.ReturnResult(cmdResult, cmd) - if err != nil { - panic("should not happen") - } - } - }() -} - -func init() { - fmt.Println("call init") - - // Register addon - err := ten.RegisterAddonAsExtension( - "extension_c", - ten.NewDefaultExtensionAddon(newCExtension), - ) - if err != nil { - fmt.Println("register addon failed", err) - } -} diff --git a/tests/ten_runtime/integration/go/send_json_go/send_json_go_app/ten_packages/extension/extension_c/go.mod b/tests/ten_runtime/integration/go/send_json_go/send_json_go_app/ten_packages/extension/extension_c/go.mod deleted file mode 100644 index 78962b0f75..0000000000 --- a/tests/ten_runtime/integration/go/send_json_go/send_json_go_app/ten_packages/extension/extension_c/go.mod +++ /dev/null @@ -1,7 +0,0 @@ -module extension_c - -go 1.18 - -replace ten_framework => ../../../ten_packages/system/ten_runtime_go/interface - -require ten_framework v0.0.0-00010101000000-000000000000 diff --git a/tests/ten_runtime/integration/go/send_json_go/send_json_go_app/ten_packages/extension/extension_c/manifest.json b/tests/ten_runtime/integration/go/send_json_go/send_json_go_app/ten_packages/extension/extension_c/manifest.json deleted file mode 100644 index 4ea40570b5..0000000000 --- a/tests/ten_runtime/integration/go/send_json_go/send_json_go_app/ten_packages/extension/extension_c/manifest.json +++ /dev/null @@ -1,18 +0,0 @@ -{ - "type": "extension", - "name": "extension_c", - "version": "0.3.0-alpha", - "dependencies": [ - { - "type": "system", - "name": "ten_runtime_go", - "version": "0.3.0" - } - ], - "package": { - "include": [ - "**" - ] - }, - "api": {} -} \ No newline at end of file diff --git a/tests/ten_runtime/integration/go/send_json_go/send_json_go_app/ten_packages/extension/extension_c/property.json b/tests/ten_runtime/integration/go/send_json_go/send_json_go_app/ten_packages/extension/extension_c/property.json deleted file mode 100644 index 9e26dfeeb6..0000000000 --- a/tests/ten_runtime/integration/go/send_json_go/send_json_go_app/ten_packages/extension/extension_c/property.json +++ /dev/null @@ -1 +0,0 @@ -{} \ No newline at end of file diff --git a/tests/ten_runtime/integration/go/send_json_go/test_case.py b/tests/ten_runtime/integration/go/send_json_go/test_case.py deleted file mode 100644 index 98dd12b7b1..0000000000 --- a/tests/ten_runtime/integration/go/send_json_go/test_case.py +++ /dev/null @@ -1,96 +0,0 @@ -""" -Test send_json_go -""" - -import subprocess -import os -import sys -from sys import stdout -from .common import msgpack - - -def test_send_json_go(): - """Test client and app server.""" - base_path = os.path.dirname(os.path.abspath(__file__)) - root_dir = os.path.join(base_path, "../../../../../") - - my_env = os.environ.copy() - - app_root_path = os.path.join(base_path, "send_json_go_app") - - tman_install_cmd = [ - os.path.join(root_dir, "ten_manager/bin/tman"), - "--config-file", - os.path.join(root_dir, "tests/local_registry/config.json"), - "install", - ] - - tman_install_process = subprocess.Popen( - tman_install_cmd, - stdout=stdout, - stderr=subprocess.STDOUT, - env=my_env, - cwd=app_root_path, - ) - tman_install_process.wait() - - if sys.platform == "win32": - print("test_send_json_go doesn't support win32") - assert False - elif sys.platform == "darwin": - # client depends on some libraries in the TEN app. - my_env["DYLD_LIBRARY_PATH"] = os.path.join( - base_path, "send_json_go_app/ten_packages/system/ten_runtime/lib" - ) - else: - # client depends on some libraries in the TEN app. - my_env["LD_LIBRARY_PATH"] = os.path.join( - base_path, "send_json_go_app/ten_packages/system/ten_runtime/lib" - ) - - if os.path.exists(os.path.join(base_path, "use_asan_lib_marker")): - libasan_path = os.path.join( - base_path, - "send_json_go_app/ten_packages/system/ten_runtime/lib/libasan.so", - ) - if os.path.exists(libasan_path): - my_env["LD_PRELOAD"] = libasan_path - - server_cmd = os.path.join(base_path, "send_json_go_app/bin/start") - client_cmd = os.path.join(base_path, "send_json_go_app_client") - server = subprocess.Popen( - server_cmd, - stdout=stdout, - stderr=subprocess.STDOUT, - env=my_env, - cwd=app_root_path, - ) - - is_started, sock = msgpack.is_app_started("127.0.0.1", 8007, 10) - if not is_started: - print("The send_json_go is not started after 30 seconds.") - - server.kill() - exit_code = server.wait() - print("The exit code of send_json_go: ", exit_code) - - assert exit_code == 0 - assert 0 - - return - - client = subprocess.Popen( - client_cmd, stdout=stdout, stderr=subprocess.STDOUT, env=my_env - ) - - client_rc = client.wait() - if client_rc != 0: - server.kill() - - sock.close() - - server_rc = server.wait() - print("server: ", server_rc) - print("client: ", client_rc) - assert server_rc == 0 - assert client_rc == 0 diff --git a/tests/ten_runtime/integration/python/BUILD.gn b/tests/ten_runtime/integration/python/BUILD.gn index e9c7494ce4..8974f47c45 100644 --- a/tests/ten_runtime/integration/python/BUILD.gn +++ b/tests/ten_runtime/integration/python/BUILD.gn @@ -33,7 +33,6 @@ group("python") { "send_cmd_discard_result_python", "send_cmd_python", "send_data_python", - "send_json_python", "send_recv_image_python", "send_recv_pcm_python", "standalone_test_python", diff --git a/tests/ten_runtime/integration/python/send_cmd_discard_result_python/send_cmd_discard_result_python_app/ten_packages/extension/default_extension_python/extension.py b/tests/ten_runtime/integration/python/send_cmd_discard_result_python/send_cmd_discard_result_python_app/ten_packages/extension/default_extension_python/extension.py index c8a2a1fd1e..3e03b487c0 100644 --- a/tests/ten_runtime/integration/python/send_cmd_discard_result_python/send_cmd_discard_result_python_app/ten_packages/extension/default_extension_python/extension.py +++ b/tests/ten_runtime/integration/python/send_cmd_discard_result_python/send_cmd_discard_result_python_app/ten_packages/extension/default_extension_python/extension.py @@ -19,14 +19,12 @@ def __init__(self, name: str) -> None: self.name = name def on_init(self, ten_env: TenEnv) -> None: - print(f"{self.name} on_init") + ten_env.log_info("on_init") ten_env.on_init_done() def on_cmd(self, ten_env: TenEnv, cmd: Cmd) -> None: - print(f"{self.name} on_cmd") - cmd_json = cmd.to_json() - print(f"{self.name} on_cmd json: {cmd_json}") + ten_env.log_info(f"on_cmd json: {cmd_json}") if self.name == "default_extension_python_1": if cmd.get_name() == "test": @@ -38,9 +36,9 @@ def on_cmd(self, ten_env: TenEnv, cmd: Cmd) -> None: cmd_result.set_property_string("detail", "nbnb") ten_env.return_result(cmd_result, self.cached_cmd) elif self.name == "default_extension_python_2": - print(f"{self.name} create respCmd 1") + ten_env.log_info("create respCmd 1") ten_env.return_result(CmdResult.create(StatusCode.OK), cmd) - print(f"{self.name} create respCmd 2") - json_str = """{"_ten": {"name": "hello2"}}""" - ten_env.send_json(json_str, None) + ten_env.log_info("create respCmd 2") + hello2_cmd = Cmd.create("hello2") + ten_env.send_cmd(hello2_cmd, None) diff --git a/tests/ten_runtime/integration/python/send_json_python/BUILD.gn b/tests/ten_runtime/integration/python/send_json_python/BUILD.gn deleted file mode 100644 index e1bb744e88..0000000000 --- a/tests/ten_runtime/integration/python/send_json_python/BUILD.gn +++ /dev/null @@ -1,50 +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. -# -import("//build/ten_runtime/feature/test.gni") -import("//build/ten_runtime/ten.gni") - -ten_package_test_prepare_app("send_json_python_app") { - src_app = "default_app_python" - src_app_language = "python" - generated_app_src_root_dir_name = "send_json_python_app" - - replace_files_after_install_app = [ - "send_json_python_app/manifest.json", - "send_json_python_app/property.json", - ] - - replace_files_after_install_all = [ "send_json_python_app/ten_packages/extension/default_extension_python/extension.py" ] - - if (ten_enable_package_manager) { - deps = [ - "//core/src/ten_manager", - "//packages/core_apps/default_app_python:upload_default_app_python_to_server", - "//packages/core_extensions/default_extension_python:upload_default_extension_python_to_server", - "//packages/example_extensions/simple_echo_cpp:upload_simple_echo_cpp_to_server", - "//packages/example_extensions/simple_http_server_cpp:upload_simple_http_server_cpp_to_server", - ] - } -} - -ten_package_test_prepare_auxiliary_resources("send_json_python_test_files") { - resources = [ - "//tests/ten_runtime/integration/common=>common", - "__init__.py", - "test_case.py", - ] - if (enable_sanitizer) { - resources += [ "//tests/ten_runtime/integration/tools/use_asan_lib_marker=>use_asan_lib_marker" ] - } -} - -group("send_json_python") { - deps = [ - ":send_json_python_app", - ":send_json_python_test_files", - "//tests/ten_runtime/integration/python:copy_pytest_ini_for_ten_runtime_python_integration_test", - ] -} diff --git a/tests/ten_runtime/integration/python/send_json_python/__init__.py b/tests/ten_runtime/integration/python/send_json_python/__init__.py deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/tests/ten_runtime/integration/python/send_json_python/send_json_python_app/manifest.json b/tests/ten_runtime/integration/python/send_json_python/send_json_python_app/manifest.json deleted file mode 100644 index 56b751b982..0000000000 --- a/tests/ten_runtime/integration/python/send_json_python/send_json_python_app/manifest.json +++ /dev/null @@ -1,24 +0,0 @@ -{ - "dependencies": [ - { - "type": "system", - "name": "ten_runtime", - "version": "0.3.0" - }, - { - "type": "extension", - "name": "simple_http_server_cpp", - "version": "0.1.0" - }, - { - "type": "extension", - "name": "default_extension_python", - "version": "0.3.0" - }, - { - "type": "extension", - "name": "simple_echo_cpp", - "version": "0.1.0" - } - ] -} \ No newline at end of file diff --git a/tests/ten_runtime/integration/python/send_json_python/send_json_python_app/property.json b/tests/ten_runtime/integration/python/send_json_python/send_json_python_app/property.json deleted file mode 100644 index 3a8f23fac0..0000000000 --- a/tests/ten_runtime/integration/python/send_json_python/send_json_python_app/property.json +++ /dev/null @@ -1,66 +0,0 @@ -{ - "_ten": { - "log_level": 2, - "predefined_graphs": [ - { - "name": "default", - "auto_start": true, - "nodes": [ - { - "type": "extension", - "name": "simple_http_server_cpp", - "addon": "simple_http_server_cpp", - "extension_group": "default_extension_group", - "property": { - "server_port": 8002 - } - }, - { - "type": "extension", - "name": "default_extension_python", - "addon": "default_extension_python", - "extension_group": "test" - }, - { - "type": "extension", - "name": "simple_echo_cpp", - "addon": "simple_echo_cpp", - "extension_group": "default_extension_group" - } - ], - "connections": [ - { - "extension_group": "default_extension_group", - "extension": "simple_http_server_cpp", - "cmd": [ - { - "name": "test", - "dest": [ - { - "extension_group": "test", - "extension": "default_extension_python" - } - ] - } - ] - }, - { - "extension_group": "test", - "extension": "default_extension_python", - "cmd": [ - { - "name": "hello", - "dest": [ - { - "extension_group": "default_extension_group", - "extension": "simple_echo_cpp" - } - ] - } - ] - } - ] - } - ] - } -} \ No newline at end of file diff --git a/tests/ten_runtime/integration/python/send_json_python/send_json_python_app/ten_packages/extension/default_extension_python/extension.py b/tests/ten_runtime/integration/python/send_json_python/send_json_python_app/ten_packages/extension/default_extension_python/extension.py deleted file mode 100644 index 5168e852b7..0000000000 --- a/tests/ten_runtime/integration/python/send_json_python/send_json_python_app/ten_packages/extension/default_extension_python/extension.py +++ /dev/null @@ -1,64 +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. -# -from ten import ( - Extension, - TenEnv, - Cmd, - StatusCode, - CmdResult, -) - - -class DefaultExtension(Extension): - def on_configure(self, ten_env: TenEnv) -> None: - ten_env.log_debug("on_init") - ten_env.init_property_from_json('{"testKey": "testValue"}') - ten_env.on_configure_done() - - def on_start(self, ten_env: TenEnv) -> None: - ten_env.log_debug("on_start") - ten_env.set_property_from_json("testKey2", '"testValue2"') - testValue = ten_env.get_property_to_json("testKey") - testValue2 = ten_env.get_property_to_json("testKey2") - print("testValue: ", testValue, " testValue2: ", testValue2) - ten_env.on_start_done() - - def on_stop(self, ten_env: TenEnv) -> None: - print("DefaultExtension on_stop") - ten_env.on_stop_done() - - def on_deinit(self, ten_env: TenEnv) -> None: - print("DefaultExtension on_deinit") - ten_env.on_deinit_done() - - def check_hello(self, ten_env: TenEnv, result: CmdResult, receivedCmd: Cmd): - statusCode = result.get_status_code() - detail = result.get_property_string("detail") - print( - "DefaultExtension check_hello: status:" - + str(statusCode) - + " detail:" - + detail - ) - - respCmd = CmdResult.create(StatusCode.OK) - respCmd.set_property_string("detail", detail + " nbnb") - print("DefaultExtension create respCmd") - - ten_env.return_result(respCmd, receivedCmd) - - def on_cmd(self, ten_env: TenEnv, cmd: Cmd) -> None: - print("DefaultExtension on_cmd") - cmd_json = cmd.to_json() - print("DefaultExtension on_cmd json: " + cmd_json) - - json_str = """{"_ten": {"name": "hello"}}""" - - ten_env.send_json( - json_str, - lambda ten_env, result: self.check_hello(ten_env, result, cmd), - ) diff --git a/tests/ten_runtime/integration/python/send_json_python/test_case.py b/tests/ten_runtime/integration/python/send_json_python/test_case.py deleted file mode 100644 index e125f9d085..0000000000 --- a/tests/ten_runtime/integration/python/send_json_python/test_case.py +++ /dev/null @@ -1,130 +0,0 @@ -""" -Test send_json_python. -""" - -import subprocess -import os -import sys -from sys import stdout -from .common import http - - -def http_request(): - return http.post( - "http://127.0.0.1:8002/", - { - "_ten": { - "name": "test", - }, - }, - ) - - -def test_send_json_python(): - """Test client and app server.""" - base_path = os.path.dirname(os.path.abspath(__file__)) - root_dir = os.path.join(base_path, "../../../../../") - - # Create virtual environment. - venv_dir = os.path.join(base_path, "venv") - subprocess.run([sys.executable, "-m", "venv", venv_dir]) - - my_env = os.environ.copy() - - # Set the required environment variables for the test. - my_env["PYTHONMALLOC"] = "malloc" - my_env["PYTHONDEVMODE"] = "1" - - # Launch virtual environment. - my_env["VIRTUAL_ENV"] = venv_dir - my_env["PATH"] = os.path.join(venv_dir, "bin") + os.pathsep + my_env["PATH"] - - if sys.platform == "win32": - print("test_send_json_python doesn't support win32") - assert False - elif sys.platform == "darwin": - # client depends on some libraries in the TEN app. - my_env["DYLD_LIBRARY_PATH"] = os.path.join( - base_path, "send_json_python_app/lib" - ) - else: - # client depends on some libraries in the TEN app. - my_env["LD_LIBRARY_PATH"] = os.path.join( - base_path, "send_json_python_app/lib" - ) - - app_root_path = os.path.join(base_path, "send_json_python_app") - - tman_install_cmd = [ - os.path.join(root_dir, "ten_manager/bin/tman"), - "--config-file", - os.path.join(root_dir, "tests/local_registry/config.json"), - "install", - ] - - tman_install_process = subprocess.Popen( - tman_install_cmd, - stdout=stdout, - stderr=subprocess.STDOUT, - env=my_env, - cwd=app_root_path, - ) - tman_install_process.wait() - - bootstrap_cmd = os.path.join( - base_path, "send_json_python_app/bin/bootstrap" - ) - - bootstrap_process = subprocess.Popen( - bootstrap_cmd, stdout=stdout, stderr=subprocess.STDOUT, env=my_env - ) - bootstrap_process.wait() - - if sys.platform == "linux": - if os.path.exists(os.path.join(base_path, "use_asan_lib_marker")): - libasan_path = os.path.join( - base_path, - "send_json_python_app/ten_packages/system/ten_runtime/lib/libasan.so", - ) - - if os.path.exists(libasan_path): - my_env["LD_PRELOAD"] = libasan_path - - server_cmd = os.path.join(base_path, "send_json_python_app/bin/start") - - server = subprocess.Popen( - server_cmd, - stdout=stdout, - stderr=subprocess.STDOUT, - env=my_env, - cwd=app_root_path, - ) - - is_started = http.is_app_started("127.0.0.1", 8002, 30) - if not is_started: - print("The send_json_python is not started after 30 seconds.") - - server.kill() - exit_code = server.wait() - print("The exit code of send_json_python: ", exit_code) - - assert exit_code == 0 - assert 0 - - return - - try: - resp = http_request() - assert resp != 500 - print(resp) - - finally: - is_stopped = http.stop_app("127.0.0.1", 8002, 30) - if not is_stopped: - print("The send_json_python can not stop after 30 seconds.") - server.kill() - - exit_code = server.wait() - print("The exit code of send_json_python: ", exit_code) - - assert exit_code == 0 diff --git a/tests/ten_runtime/smoke/extension_test/basic/basic_loop.cc b/tests/ten_runtime/smoke/extension_test/basic/basic_loop.cc index 62d0ddc800..9d33d7407f 100644 --- a/tests/ten_runtime/smoke/extension_test/basic/basic_loop.cc +++ b/tests/ten_runtime/smoke/extension_test/basic/basic_loop.cc @@ -6,7 +6,6 @@ // #include #include -#include #include "gtest/gtest.h" #include "include_internal/ten_runtime/binding/cpp/ten.h" @@ -25,22 +24,18 @@ class test_extension_1 : public ten::extension_t { nlohmann::json json = nlohmann::json::parse(cmd->to_json()); if (json["_ten"]["name"] == "hello_world") { hello_world_cmd = std::move(cmd); - ten_env.send_json( - R"({ - "_ten": { - "name": "hello_world_1" - } - })"_json.dump() - .c_str(), - [this](ten::ten_env_t &ten_env, - std::unique_ptr result) { - // Got result of 'hello world 1', - // Now return result for 'hello world' - auto cmd_result = ten::cmd_result_t::create(TEN_STATUS_CODE_OK); - cmd_result->set_property("detail", "hello world, too"); - ten_env.return_result(std::move(cmd_result), - std::move(hello_world_cmd)); - }); + auto hello_world_1_cmd = ten::cmd_t::create("hello_world_1"); + ten_env.send_cmd(std::move(hello_world_1_cmd), + [this](ten::ten_env_t &ten_env, + std::unique_ptr result) { + // Got result of 'hello world 1', + // Now return result for 'hello world' + auto cmd_result = + ten::cmd_result_t::create(TEN_STATUS_CODE_OK); + cmd_result->set_property("detail", "hello world, too"); + ten_env.return_result(std::move(cmd_result), + std::move(hello_world_cmd)); + }); return; } else if (json["_ten"]["name"] == "hello_world_2") { auto cmd_result = ten::cmd_result_t::create(TEN_STATUS_CODE_OK); @@ -63,22 +58,18 @@ class test_extension_2 : public ten::extension_t { if (json["_ten"]["name"] == "hello_world_1") { // waiting for result pending_request = std::move(cmd); - ten_env.send_json( - R"({ - "_ten": { - "name": "hello_world_2" - } - })"_json.dump() - .c_str(), - [this](ten::ten_env_t &ten_env, - std::unique_ptr result) { - // Got result of 'hello world 2'. - // Now return result for 'hello world 1' - auto cmd_result = ten::cmd_result_t::create(TEN_STATUS_CODE_OK); - cmd_result->set_property("detail", "hello world, too"); - ten_env.return_result(std::move(cmd_result), - std::move(pending_request)); - }); + auto hello_world_2_cmd = ten::cmd_t::create("hello_world_2"); + ten_env.send_cmd(std::move(hello_world_2_cmd), + [this](ten::ten_env_t &ten_env, + std::unique_ptr result) { + // Got result of 'hello world 2'. + // Now return result for 'hello world 1' + auto cmd_result = + ten::cmd_result_t::create(TEN_STATUS_CODE_OK); + cmd_result->set_property("detail", "hello world, too"); + ten_env.return_result(std::move(cmd_result), + std::move(pending_request)); + }); } } @@ -114,10 +105,8 @@ void *test_app_thread_main(TEN_UNUSED void *args) { return nullptr; } -TEN_CPP_REGISTER_ADDON_AS_EXTENSION(basic_loop__extension_1, - test_extension_1); -TEN_CPP_REGISTER_ADDON_AS_EXTENSION(basic_loop__extension_2, - test_extension_2); +TEN_CPP_REGISTER_ADDON_AS_EXTENSION(basic_loop__extension_1, test_extension_1); +TEN_CPP_REGISTER_ADDON_AS_EXTENSION(basic_loop__extension_2, test_extension_2); } // namespace diff --git a/tests/ten_runtime/smoke/extension_test/basic/basic_loop_cmd.cc b/tests/ten_runtime/smoke/extension_test/basic/basic_loop_cmd.cc index 3818480494..53d4bc7caa 100644 --- a/tests/ten_runtime/smoke/extension_test/basic/basic_loop_cmd.cc +++ b/tests/ten_runtime/smoke/extension_test/basic/basic_loop_cmd.cc @@ -6,7 +6,6 @@ // #include #include -#include #include "gtest/gtest.h" #include "include_internal/ten_runtime/binding/cpp/ten.h" @@ -25,14 +24,9 @@ class test_extension_1 : public ten::extension_t { if (std::string(cmd->get_name()) == "hello_world") { // Save the command for later using. hello_world_cmd = std::move(cmd); - - ten_env.send_json( - R"({ - "_ten": { - "name": "hello_world_1" - } - })"_json.dump() - .c_str(), + auto hello_world_1_cmd = ten::cmd_t::create("hello_world_1"); + ten_env.send_cmd( + std::move(hello_world_1_cmd), [this](ten::ten_env_t &ten_env, TEN_UNUSED std::unique_ptr cmd) { // Got result of 'hello world 1', @@ -63,14 +57,9 @@ class test_extension_2 : public ten::extension_t { if (std::string(cmd->get_name()) == "hello_world_1") { // waiting for result pending_request = std::move(cmd); - - ten_env.send_json( - R"({ - "_ten": { - "name": "hello_world_2" - } - })"_json.dump() - .c_str(), + auto hello_world_2_cmd = ten::cmd_t::create("hello_world_2"); + ten_env.send_cmd( + std::move(hello_world_2_cmd), [this](TEN_UNUSED ten::ten_env_t &ten_env, TEN_UNUSED std::unique_ptr cmd) { // Got result of 'hello world 2'. diff --git a/tests/ten_runtime/smoke/extension_test/basic/basic_loop_cmd_snap_shot.cc b/tests/ten_runtime/smoke/extension_test/basic/basic_loop_cmd_snap_shot.cc index 7f08c2b322..1b29530566 100644 --- a/tests/ten_runtime/smoke/extension_test/basic/basic_loop_cmd_snap_shot.cc +++ b/tests/ten_runtime/smoke/extension_test/basic/basic_loop_cmd_snap_shot.cc @@ -6,7 +6,6 @@ // #include #include -#include #include "gtest/gtest.h" #include "include_internal/ten_runtime/binding/cpp/ten.h" @@ -27,23 +26,18 @@ class test_extension_1 : public ten::extension_t { if (std::string(cmd_name) == "hello_world") { // Save the command for later using. hello_world_cmd = std::move(cmd); - - ten_env.send_json( - R"({ - "_ten": { - "name": "hello_world_1" - } - })"_json.dump() - .c_str(), - [this](ten::ten_env_t &ten_env, - std::unique_ptr cmd) { - // Got result of 'hello world 1', - // Now return result for 'hello world' - auto cmd_result = ten::cmd_result_t::create(TEN_STATUS_CODE_OK); - cmd_result->set_property("detail", "hello world, too"); - ten_env.return_result(std::move(cmd_result), - std::move(hello_world_cmd)); - }); + auto hello_world_1_cmd = ten::cmd_t::create("hello_world_1"); + ten_env.send_cmd(std::move(hello_world_1_cmd), + [this](ten::ten_env_t &ten_env, + std::unique_ptr cmd) { + // Got result of 'hello world 1', + // Now return result for 'hello world' + auto cmd_result = + ten::cmd_result_t::create(TEN_STATUS_CODE_OK); + cmd_result->set_property("detail", "hello world, too"); + ten_env.return_result(std::move(cmd_result), + std::move(hello_world_cmd)); + }); } else if (std::string(cmd_name) == "hello_world_2") { auto cmd_result = ten::cmd_result_t::create(TEN_STATUS_CODE_OK); cmd_result->set_property("detail", "hello world, too"); @@ -64,14 +58,9 @@ class test_extension_2 : public ten::extension_t { if (std::string(cmd->get_name()) == "hello_world_1") { // waiting for result pending_request = std::move(cmd); - - ten_env.send_json( - R"({ - "_ten": { - "name": "hello_world_2" - } - })"_json.dump() - .c_str(), + auto hello_world_2_cmd = ten::cmd_t::create("hello_world_2"); + ten_env.send_cmd( + std::move(hello_world_2_cmd), [this](TEN_UNUSED ten::ten_env_t &ten_env, TEN_UNUSED std::unique_ptr cmd) { // Got result of 'hello world 2'. diff --git a/tests/ten_runtime/smoke/extension_test/command/command_invalid_extension_2.cc b/tests/ten_runtime/smoke/extension_test/command/command_invalid_extension_2.cc index 47f8a3f922..c567a20548 100644 --- a/tests/ten_runtime/smoke/extension_test/command/command_invalid_extension_2.cc +++ b/tests/ten_runtime/smoke/extension_test/command/command_invalid_extension_2.cc @@ -7,7 +7,6 @@ #include #include #include -#include #include "gtest/gtest.h" #include "include_internal/ten_runtime/binding/cpp/ten.h" @@ -30,20 +29,10 @@ class test_extension : public ten::extension_t { // We send out a request with invalid extension, the extension thread will // return the error result. - nlohmann::json request = R"({ - "_ten": { - "name": "test", - "dest": [ - { - "app": "localhost", - "extension": "a", - "extension_group": "test_extension_group" - } - ] - } - })"_json; - ten_env.send_json( - request.dump().c_str(), + auto test_cmd = ten::cmd_t::create("test"); + test_cmd->set_dest("localhost", nullptr, "test_extension_group", "a"); + ten_env.send_cmd( + std::move(test_cmd), [this](ten::ten_env_t &ten_env, std::unique_ptr result) { nlohmann::json json = nlohmann::json::parse(result->to_json()); @@ -53,7 +42,6 @@ class test_extension : public ten::extension_t { ten_env.return_result(std::move(cmd_result), std::move(requested_cmd)); }); - return; } } diff --git a/tests/ten_runtime/smoke/extension_test/command/command_stop_graph_actively.cc b/tests/ten_runtime/smoke/extension_test/command/command_stop_graph_actively.cc index 0260ae804f..6558e8fcd9 100644 --- a/tests/ten_runtime/smoke/extension_test/command/command_stop_graph_actively.cc +++ b/tests/ten_runtime/smoke/extension_test/command/command_stop_graph_actively.cc @@ -9,6 +9,7 @@ #include "gtest/gtest.h" #include "include_internal/ten_runtime/binding/cpp/ten.h" +#include "ten_runtime/binding/cpp/internal/msg/cmd/stop_graph.h" #include "ten_utils/lib/thread.h" #include "ten_utils/lib/time.h" #include "tests/common/client/cpp/msgpack_tcp.h" @@ -72,15 +73,9 @@ class test_extension_4 : public ten::extension_t { "must return result before close engine"); ten_env.return_result(std::move(cmd_result), std::move(cmd)); - ten_env.send_json(R"({ - "_ten": { - "type": "stop_graph", - "dest": [{ - "app": "localhost" - }] - } - })"_json.dump() - .c_str()); + auto stop_graph_cmd = ten::cmd_stop_graph_t::create(); + stop_graph_cmd->set_dest("localhost", nullptr, nullptr, nullptr); + ten_env.send_cmd(std::move(stop_graph_cmd)); } } }; diff --git a/tests/ten_runtime/smoke/extension_test/command/command_stop_graph_actively_through_cmd_dest.cc b/tests/ten_runtime/smoke/extension_test/command/command_stop_graph_actively_through_cmd_dest.cc index e3d10f6c8b..05832024e4 100644 --- a/tests/ten_runtime/smoke/extension_test/command/command_stop_graph_actively_through_cmd_dest.cc +++ b/tests/ten_runtime/smoke/extension_test/command/command_stop_graph_actively_through_cmd_dest.cc @@ -69,7 +69,7 @@ class test_extension_4 : public ten::extension_t { ten_env.return_result(std::move(cmd_result), std::move(cmd)); auto stop_graph_cmd = ten::cmd_stop_graph_t::create(); - stop_graph_cmd->set_dest("localhost", "", "", ""); + stop_graph_cmd->set_dest("localhost", nullptr, nullptr, nullptr); ten_env.send_cmd(std::move(stop_graph_cmd)); } } diff --git a/tests/ten_runtime/smoke/extension_test/engine/extension_send_msg_to_incorrect_engine.cc b/tests/ten_runtime/smoke/extension_test/engine/extension_send_msg_to_incorrect_engine.cc index 0554e49b74..10b6474cca 100644 --- a/tests/ten_runtime/smoke/extension_test/engine/extension_send_msg_to_incorrect_engine.cc +++ b/tests/ten_runtime/smoke/extension_test/engine/extension_send_msg_to_incorrect_engine.cc @@ -7,7 +7,6 @@ #include #include #include -#include #include "gtest/gtest.h" #include "include_internal/ten_runtime/binding/cpp/ten.h" @@ -28,19 +27,12 @@ class test_extension : public ten::extension_t { auto cmd_shared = std::make_shared>(std::move(cmd)); - ten_env.send_json( - R"({ - "_ten": { - "name": "test", - "dest":[{ - "app": "msgpack://127.0.0.1:8001/", - "graph": "incorrect_graph_id", - "extension_group": "extension_send_msg_to_incorrect_engine", - "extension": "test_extension" - }] - } - })"_json.dump() - .c_str(), + auto test_cmd = ten::cmd_t::create("test"); + test_cmd->set_dest("msgpack://127.0.0.1:8001/", "incorrect_graph_id", + "extension_send_msg_to_incorrect_engine", + "test_extension"); + ten_env.send_cmd( + std::move(test_cmd), [cmd_shared](ten::ten_env_t &ten_env, std::unique_ptr cmd_result) { ten_env.return_result(std::move(cmd_result), diff --git a/tests/ten_runtime/smoke/extension_test/error/failed_to_connect_to_remote.cc b/tests/ten_runtime/smoke/extension_test/error/failed_to_connect_to_remote.cc index c241009a64..ca62fc4565 100644 --- a/tests/ten_runtime/smoke/extension_test/error/failed_to_connect_to_remote.cc +++ b/tests/ten_runtime/smoke/extension_test/error/failed_to_connect_to_remote.cc @@ -6,6 +6,7 @@ // #include "gtest/gtest.h" #include "include_internal/ten_runtime/binding/cpp/ten.h" +#include "ten_runtime/binding/cpp/internal/msg/cmd/start_graph.h" #include "ten_runtime/common/status_code.h" #include "tests/common/client/cpp/msgpack_tcp.h" #include "tests/ten_runtime/smoke/extension_test/util/binding/cpp/check.h" @@ -17,21 +18,12 @@ class test_predefined_graph : public ten::extension_t { : ten::extension_t(name) {} void on_start(ten::ten_env_t &ten_env) override { - std::string start_graph_json = R"({ - "_ten": { - "type": "start_graph", - "seq_id": "222", - "dest": [{ - "app": "localhost" - }], - "predefined_graph": "graph_1" - } - })"_json.dump(); - - ten_env.send_json( - start_graph_json.c_str(), - [start_graph_json](ten::ten_env_t &ten_env, - std::unique_ptr cmd) { + auto start_graph_cmd = ten::cmd_start_graph_t::create(); + start_graph_cmd->set_dest("localhost", nullptr, nullptr, nullptr); + start_graph_cmd->set_predefined_graph_name("graph_1"); + ten_env.send_cmd( + std::move(start_graph_cmd), + [](ten::ten_env_t &ten_env, std::unique_ptr cmd) { auto status_code = cmd->get_status_code(); ASSERT_EQ(status_code, TEN_STATUS_CODE_ERROR); diff --git a/tests/ten_runtime/smoke/extension_test/multi_dest/multi_dest_y_topo.cc b/tests/ten_runtime/smoke/extension_test/multi_dest/multi_dest_y_topo.cc index 0a7436092b..75c8c0acf6 100644 --- a/tests/ten_runtime/smoke/extension_test/multi_dest/multi_dest_y_topo.cc +++ b/tests/ten_runtime/smoke/extension_test/multi_dest/multi_dest_y_topo.cc @@ -28,13 +28,9 @@ class test_extension_1 : public ten::extension_t { // result back to the client. client_cmd = std::move(cmd); - ten_env.send_json( - R"({ - "_ten": { - "name": "hello_world" - } - })"_json.dump() - .c_str(), + auto hello_world_cmd = ten::cmd_t::create("hello_world"); + ten_env.send_cmd( + std::move(hello_world_cmd), [&](ten::ten_env_t &ten_env, std::unique_ptr /*cmd*/) { // Return to the client to notify that the whole process diff --git a/tests/ten_runtime/smoke/extension_test/outer_thread/128_threads_attempt_to_suspend_1.cc b/tests/ten_runtime/smoke/extension_test/outer_thread/128_threads_attempt_to_suspend_1.cc index 57bf703f2d..1e0f8113e9 100644 --- a/tests/ten_runtime/smoke/extension_test/outer_thread/128_threads_attempt_to_suspend_1.cc +++ b/tests/ten_runtime/smoke/extension_test/outer_thread/128_threads_attempt_to_suspend_1.cc @@ -861,19 +861,14 @@ class test_extension_2 : public ten::extension_t { auto cmd_result = ten::cmd_result_t::create(TEN_STATUS_CODE_OK); cmd_result->set_property("detail", "ok"); ten_env.return_result(std::move(cmd_result), std::move(cmd)); - nlohmann::json timer_cmd_json = - R"({ - "_ten": { - "type": "timer", - "dest": [{ - "app": "localhost" - }], - "timer_id": 55, - "timeout_in_us": 1000, - "times": 1 - } - })"_json; - ten_env.send_json(timer_cmd_json.dump().c_str()); + + auto timer_cmd = ten::cmd_timer_t::create(); + timer_cmd->set_dest("localhost", nullptr, nullptr, nullptr); + timer_cmd->set_timer_id(55); + timer_cmd->set_timeout_in_us(1000); + timer_cmd->set_times(1); + + ten_env.send_cmd(std::move(timer_cmd)); } else if (cmd->get_type() == TEN_MSG_TYPE_CMD_TIMEOUT && static_cast(cmd.get())->get_timer_id() == 55) { diff --git a/tests/ten_runtime/smoke/extension_test/outer_thread/32_threads_attempt_to_suspend_5.cc b/tests/ten_runtime/smoke/extension_test/outer_thread/32_threads_attempt_to_suspend_5.cc index 5172c228b7..8585339d59 100644 --- a/tests/ten_runtime/smoke/extension_test/outer_thread/32_threads_attempt_to_suspend_5.cc +++ b/tests/ten_runtime/smoke/extension_test/outer_thread/32_threads_attempt_to_suspend_5.cc @@ -12,6 +12,7 @@ #include #include "gtest/gtest.h" +#include "include_internal/ten_runtime/binding/cpp/internal/msg/cmd/timer.h" #include "include_internal/ten_runtime/binding/cpp/ten.h" #include "ten_runtime/msg/msg.h" #include "ten_utils/lang/cpp/lib/value.h" @@ -355,19 +356,14 @@ class test_extension_2 : public ten::extension_t { auto cmd_result = ten::cmd_result_t::create(TEN_STATUS_CODE_OK); cmd_result->set_property("detail", "ok"); ten_env.return_result(std::move(cmd_result), std::move(cmd)); - nlohmann::json timer_cmd_json = - R"({ - "_ten": { - "type": "timer", - "dest": [{ - "app": "localhost" - }], - "timer_id": 55, - "timeout_in_us": 100, - "times": 1 - } - })"_json; - ten_env.send_json(timer_cmd_json.dump().c_str()); + + auto timer_cmd = ten::cmd_timer_t::create(); + timer_cmd->set_dest("localhost", nullptr, nullptr, nullptr); + timer_cmd->set_timer_id(55); + timer_cmd->set_timeout_in_us(100); + timer_cmd->set_times(1); + + ten_env.send_cmd(std::move(timer_cmd)); } else if (cmd->get_type() == TEN_MSG_TYPE_CMD_TIMEOUT && static_cast(cmd.get())->get_timer_id() == 55) { diff --git a/tests/ten_runtime/smoke/extension_test/outer_thread/32_threads_attempt_to_suspend_6.cc b/tests/ten_runtime/smoke/extension_test/outer_thread/32_threads_attempt_to_suspend_6.cc index be5408ce7e..aa5b8c35f9 100644 --- a/tests/ten_runtime/smoke/extension_test/outer_thread/32_threads_attempt_to_suspend_6.cc +++ b/tests/ten_runtime/smoke/extension_test/outer_thread/32_threads_attempt_to_suspend_6.cc @@ -360,19 +360,14 @@ class test_extension_2 : public ten::extension_t { auto cmd_result = ten::cmd_result_t::create(TEN_STATUS_CODE_OK); cmd_result->set_property("detail", "ok"); ten_env.return_result(std::move(cmd_result), std::move(cmd)); - nlohmann::json timer_cmd_json = - R"({ - "_ten": { - "type": "timer", - "dest": [{ - "app": "localhost" - }], - "timer_id": 55, - "timeout_in_us": 100, - "times": 1 - } - })"_json; - ten_env.send_json(timer_cmd_json.dump().c_str()); + + auto timer_cmd = ten::cmd_timer_t::create(); + timer_cmd->set_dest("localhost", nullptr, nullptr, nullptr); + timer_cmd->set_timer_id(55); + timer_cmd->set_timeout_in_us(100); + timer_cmd->set_times(1); + + ten_env.send_cmd(std::move(timer_cmd)); } else if (cmd->get_type() == TEN_MSG_TYPE_CMD_TIMEOUT && static_cast(cmd.get())->get_timer_id() == 55) { diff --git a/tests/ten_runtime/smoke/extension_test/predefined_graph/predefined_graph_basic_2.cc b/tests/ten_runtime/smoke/extension_test/predefined_graph/predefined_graph_basic_2.cc index c228f7dd2d..a96d5f4f06 100644 --- a/tests/ten_runtime/smoke/extension_test/predefined_graph/predefined_graph_basic_2.cc +++ b/tests/ten_runtime/smoke/extension_test/predefined_graph/predefined_graph_basic_2.cc @@ -33,42 +33,35 @@ class test_predefined_graph : public ten::extension_t { : ten::extension_t(name) {} void on_start(ten::ten_env_t &ten_env) override { - ten_env.send_json( - R"({ - "_ten": { - "type": "start_graph", - "dest": [{ - "app": "msgpack://127.0.0.1:8001/" - }], - "nodes": [{ - "type": "extension", - "name": "normal_extension", - "addon": "predefined_graph_basic_2__normal_extension", - "app": "msgpack://127.0.0.1:8001/", - "extension_group": "normal_extension_group" - }] - } - })"_json.dump() - .c_str(), + auto start_graph_cmd = ten::cmd_start_graph_t::create(); + start_graph_cmd->set_dest("msgpack://127.0.0.1:8001/", nullptr, nullptr, + nullptr); + start_graph_cmd->set_nodes_and_connections_from_json(R"({ + "_ten": { + "nodes": [{ + "type": "extension", + "name": "normal_extension", + "addon": "predefined_graph_basic_2__normal_extension", + "app": "msgpack://127.0.0.1:8001/", + "extension_group": "normal_extension_group" + }] + } + })"_json.dump() + .c_str()); + + ten_env.send_cmd( + std::move(start_graph_cmd), [&](ten::ten_env_t &ten_env, std::unique_ptr cmd_result) { nlohmann::json json = nlohmann::json::parse(cmd_result->to_json()); if (cmd_result->get_status_code() == TEN_STATUS_CODE_OK) { - nlohmann::json cmd = - R"({ - "_ten": { - "name": "hello_world", - "dest":[{ - "app": "msgpack://127.0.0.1:8001/", - "extension_group": "normal_extension_group", - "extension": "normal_extension" - }] - } - })"_json; - cmd["_ten"]["dest"][0]["graph"] = json["detail"]; - - ten_env.send_json( - cmd.dump().c_str(), + auto hello_world_cmd = ten::cmd_t::create("hello_world"); + hello_world_cmd->set_dest("msgpack://127.0.0.1:8001/", + json["detail"].get().c_str(), + "normal_extension_group", + "normal_extension"); + ten_env.send_cmd( + std::move(hello_world_cmd), [&](ten::ten_env_t &ten_env, std::unique_ptr cmd_result) { nlohmann::json json = @@ -165,10 +158,10 @@ void *app_thread_main(TEN_UNUSED void *args) { return nullptr; } -TEN_CPP_REGISTER_ADDON_AS_EXTENSION( - predefined_graph_basic_2__predefined_graph, test_predefined_graph); -TEN_CPP_REGISTER_ADDON_AS_EXTENSION( - predefined_graph_basic_2__normal_extension, test_normal_extension); +TEN_CPP_REGISTER_ADDON_AS_EXTENSION(predefined_graph_basic_2__predefined_graph, + test_predefined_graph); +TEN_CPP_REGISTER_ADDON_AS_EXTENSION(predefined_graph_basic_2__normal_extension, + test_normal_extension); } // namespace diff --git a/tests/ten_runtime/smoke/extension_test/resp_handler/resp_handler_async_basic.cc b/tests/ten_runtime/smoke/extension_test/resp_handler/resp_handler_async_basic.cc index eafd0aa820..3a30788012 100644 --- a/tests/ten_runtime/smoke/extension_test/resp_handler/resp_handler_async_basic.cc +++ b/tests/ten_runtime/smoke/extension_test/resp_handler/resp_handler_async_basic.cc @@ -6,7 +6,6 @@ // #include #include -#include #include "gtest/gtest.h" #include "include_internal/ten_runtime/binding/cpp/ten.h" @@ -61,8 +60,10 @@ class test_extension_1 : public ten::extension_t { }); } else if (json["_ten"]["name"] == "hello_world_4") { hello_world_4_cmd = std::move(cmd); - ten_env.send_json( - R"({"_ten": { "name": "hello_world_5" }})"_json.dump().c_str(), + + auto hello_world_5_cmd = ten::cmd_t::create("hello_world_5"); + ten_env.send_cmd( + std::move(hello_world_5_cmd), [&](ten::ten_env_t &ten_env, std::unique_ptr cmd) { nlohmann::json json = nlohmann::json::parse(cmd->to_json()); if (json.value("detail", "") == "hello world, too") { @@ -77,8 +78,9 @@ class test_extension_1 : public ten::extension_t { auto cmd_shared = std::make_shared>(std::move(cmd)); - ten_env.send_json( - R"({"_ten": { "name": "hello_world_6" }})"_json.dump().c_str(), + auto hello_world_6_cmd = ten::cmd_t::create("hello_world_6"); + ten_env.send_cmd( + std::move(hello_world_6_cmd), [cmd_shared](ten::ten_env_t &ten_env, std::unique_ptr cmd_result) { nlohmann::json json = nlohmann::json::parse(cmd_result->to_json()); diff --git a/tests/ten_runtime/smoke/extension_test/resp_handler/resp_handler_basic.cc b/tests/ten_runtime/smoke/extension_test/resp_handler/resp_handler_basic.cc index 44ce710024..168c18b46a 100644 --- a/tests/ten_runtime/smoke/extension_test/resp_handler/resp_handler_basic.cc +++ b/tests/ten_runtime/smoke/extension_test/resp_handler/resp_handler_basic.cc @@ -6,7 +6,6 @@ // #include #include -#include #include "gtest/gtest.h" #include "include_internal/ten_runtime/binding/cpp/ten.h" @@ -60,8 +59,10 @@ class test_extension_1 : public ten::extension_t { }); } else if (json["_ten"]["name"] == "hello_world_4") { hello_world_4_cmd = std::move(cmd); - ten_env.send_json( - R"({"_ten": { "name": "hello_world_5" }})"_json.dump().c_str(), + + auto hello_world_5_cmd = ten::cmd_t::create("hello_world_5"); + ten_env.send_cmd( + std::move(hello_world_5_cmd), [&](ten::ten_env_t &ten_env, std::unique_ptr cmd) { nlohmann::json json = nlohmann::json::parse(cmd->to_json()); if (json.value("detail", "") == "hello world 5, too") { @@ -75,8 +76,9 @@ class test_extension_1 : public ten::extension_t { auto cmd_shared = std::make_shared>(std::move(cmd)); - ten_env.send_json( - R"({"_ten": { "name": "hello_world_6" }})"_json.dump().c_str(), + auto hello_world_6_cmd = ten::cmd_t::create("hello_world_6"); + ten_env.send_cmd( + std::move(hello_world_6_cmd), [cmd_shared](ten::ten_env_t &ten_env, std::unique_ptr cmd_result) { nlohmann::json json = nlohmann::json::parse(cmd_result->to_json()); @@ -154,9 +156,9 @@ void *test_app_thread_main(TEN_UNUSED void *args) { } TEN_CPP_REGISTER_ADDON_AS_EXTENSION(resp_handler_basic__extension_1, - test_extension_1); + test_extension_1); TEN_CPP_REGISTER_ADDON_AS_EXTENSION(resp_handler_basic__extension_2, - test_extension_2); + test_extension_2); } // namespace diff --git a/tests/ten_runtime/smoke/extension_test/resp_handler/resp_handler_basic_wait_in_on_start.cc b/tests/ten_runtime/smoke/extension_test/resp_handler/resp_handler_basic_wait_in_on_start.cc index d87db721ad..7d31028420 100644 --- a/tests/ten_runtime/smoke/extension_test/resp_handler/resp_handler_basic_wait_in_on_start.cc +++ b/tests/ten_runtime/smoke/extension_test/resp_handler/resp_handler_basic_wait_in_on_start.cc @@ -6,7 +6,6 @@ // #include #include -#include #include "gtest/gtest.h" #include "include_internal/ten_runtime/binding/cpp/ten.h" @@ -66,8 +65,10 @@ class test_extension_1 : public ten::extension_t { }); } else if (json["_ten"]["name"] == "hello_world_4") { hello_world_4_cmd = std::move(cmd); - ten_env.send_json( - R"({"_ten": { "name": "hello_world_5" }})"_json.dump().c_str(), + + auto hello_world_5_cmd = ten::cmd_t::create("hello_world_5"); + ten_env.send_cmd( + std::move(hello_world_5_cmd), [&](ten::ten_env_t &ten_env, std::unique_ptr cmd) { nlohmann::json json = nlohmann::json::parse(cmd->to_json()); if (json.value("detail", "") == "hello world 5, too") { @@ -81,8 +82,9 @@ class test_extension_1 : public ten::extension_t { auto cmd_shared = std::make_shared>(std::move(cmd)); - ten_env.send_json( - R"({"_ten": { "name": "hello_world_6" }})"_json.dump().c_str(), + auto hello_world_6_cmd = ten::cmd_t::create("hello_world_6"); + ten_env.send_cmd( + std::move(hello_world_6_cmd), [cmd_shared](ten::ten_env_t &ten_env, std::unique_ptr cmd_result) { nlohmann::json json = nlohmann::json::parse(cmd_result->to_json()); @@ -159,10 +161,10 @@ void *test_app_thread_main(TEN_UNUSED void *args) { return nullptr; } -TEN_CPP_REGISTER_ADDON_AS_EXTENSION(resp_handler_basic_wait_in_on_start__extension_1, - test_extension_1); -TEN_CPP_REGISTER_ADDON_AS_EXTENSION(resp_handler_basic_wait_in_on_start__extension_2, - test_extension_2); +TEN_CPP_REGISTER_ADDON_AS_EXTENSION( + resp_handler_basic_wait_in_on_start__extension_1, test_extension_1); +TEN_CPP_REGISTER_ADDON_AS_EXTENSION( + resp_handler_basic_wait_in_on_start__extension_2, test_extension_2); } // namespace diff --git a/tests/ten_runtime/smoke/extension_test/start_graph/start_graph_and_communicate.cc b/tests/ten_runtime/smoke/extension_test/start_graph/start_graph_and_communicate.cc index 6b58484851..40df8bbc2b 100644 --- a/tests/ten_runtime/smoke/extension_test/start_graph/start_graph_and_communicate.cc +++ b/tests/ten_runtime/smoke/extension_test/start_graph/start_graph_and_communicate.cc @@ -33,44 +33,34 @@ class test_predefined_graph : public ten::extension_t { : ten::extension_t(name) {} void on_start(ten::ten_env_t &ten_env) override { - ten_env.send_json( - R"({ - "_ten": { - "type": "start_graph", - "seq_id": "222", - "dest": [{ - "app": "localhost" - }], - "nodes": [{ - "type": "extension", - "name": "normal_extension", - "addon": "start_graph_and_communication__normal_extension", - "app": "msgpack://127.0.0.1:8001/", - "extension_group": "start_graph_and_communication__normal_extension_group" - }] - } - })"_json.dump() - .c_str(), + auto start_graph_cmd = ten::cmd_start_graph_t::create(); + start_graph_cmd->set_dest("localhost", nullptr, nullptr, nullptr); + start_graph_cmd->set_nodes_and_connections_from_json(R"({ + "_ten": { + "nodes": [{ + "type": "extension", + "name": "normal_extension", + "addon": "start_graph_and_communication__normal_extension", + "app": "msgpack://127.0.0.1:8001/", + "extension_group": "start_graph_and_communication__normal_extension_group" + }] + } + })"_json.dump() + .c_str()); + + ten_env.send_cmd( + std::move(start_graph_cmd), [this](ten::ten_env_t &ten_env, std::unique_ptr cmd) { auto graph_id = cmd->get_property_string("detail"); - nlohmann::json hello_cmd = - R"({ - "_ten": { - "name": "hello_world", - "seq_id": "137", - "dest":[{ - "app": "msgpack://127.0.0.1:8001/", - "extension_group": "start_graph_and_communication__normal_extension_group", - "extension": "normal_extension" - }] - } - })"_json; - hello_cmd["_ten"]["dest"][0]["graph"] = graph_id; - - ten_env.send_json( - hello_cmd.dump().c_str(), + auto hello_world_cmd = ten::cmd_t::create("hello_world"); + hello_world_cmd->set_dest( + "msgpack://127.0.0.1:8001/", graph_id.c_str(), + "start_graph_and_communication__normal_extension_group", + "normal_extension"); + ten_env.send_cmd( + std::move(hello_world_cmd), [this](ten::ten_env_t &ten_env, std::unique_ptr cmd) { received_hello_world_resp = true; diff --git a/tests/ten_runtime/smoke/extension_test/start_graph/start_graph_from_extension.cc b/tests/ten_runtime/smoke/extension_test/start_graph/start_graph_from_extension.cc index 661ab785a1..be187c0242 100644 --- a/tests/ten_runtime/smoke/extension_test/start_graph/start_graph_from_extension.cc +++ b/tests/ten_runtime/smoke/extension_test/start_graph/start_graph_from_extension.cc @@ -23,24 +23,23 @@ class test_predefined_graph : public ten::extension_t { : ten::extension_t(name) {} void on_start(ten::ten_env_t &ten_env) override { - ten_env.send_json( - R"({ - "_ten": { - "type": "start_graph", - "seq_id": "222", - "dest": [{ - "app": "localhost" - }], - "nodes": [{ - "type": "extension", - "name": "normal_extension", - "addon": "start_graph_from_extension__normal_extension", - "app": "msgpack://127.0.0.1:8001/", - "extension_group": "start_graph_from_extension__normal_extension_group" - }] - } - })"_json.dump() - .c_str(), + auto start_graph_cmd = ten::cmd_start_graph_t::create(); + start_graph_cmd->set_dest("localhost", nullptr, nullptr, nullptr); + start_graph_cmd->set_nodes_and_connections_from_json(R"({ + "_ten": { + "nodes": [{ + "type": "extension", + "name": "normal_extension", + "addon": "start_graph_from_extension__normal_extension", + "app": "msgpack://127.0.0.1:8001/", + "extension_group": "start_graph_from_extension__normal_extension_group" + }] + } + })"_json.dump() + .c_str()); + + ten_env.send_cmd( + std::move(start_graph_cmd), [this](ten::ten_env_t &ten_env, std::unique_ptr cmd) { // result for the 'start_graph' command diff --git a/tests/ten_runtime/smoke/extension_test/start_graph/start_predefined_graph_cross_app.cc b/tests/ten_runtime/smoke/extension_test/start_graph/start_predefined_graph_cross_app.cc index 2adf2e33cb..550ef3add9 100644 --- a/tests/ten_runtime/smoke/extension_test/start_graph/start_predefined_graph_cross_app.cc +++ b/tests/ten_runtime/smoke/extension_test/start_graph/start_predefined_graph_cross_app.cc @@ -6,6 +6,7 @@ // #include "gtest/gtest.h" #include "include_internal/ten_runtime/binding/cpp/ten.h" +#include "ten_runtime/binding/cpp/internal/msg/cmd/start_graph.h" #include "tests/common/client/cpp/msgpack_tcp.h" #include "tests/ten_runtime/smoke/extension_test/util/binding/cpp/check.h" @@ -45,21 +46,13 @@ class test_predefined_graph : public ten::extension_t { : ten::extension_t(name) {} void on_start(ten::ten_env_t &ten_env) override { - std::string start_graph_json = R"({ - "_ten": { - "type": "start_graph", - "seq_id": "222", - "dest": [{ - "app": "localhost" - }], - "predefined_graph": "graph_1" - } - })"_json.dump(); - - ten_env.send_json( - start_graph_json.c_str(), - [this, start_graph_json](ten::ten_env_t &ten_env, - std::unique_ptr cmd) { + auto start_graph_cmd = ten::cmd_start_graph_t::create(); + start_graph_cmd->set_dest("localhost", nullptr, nullptr, nullptr); + start_graph_cmd->set_predefined_graph_name("graph_1"); + ten_env.send_cmd( + std::move(start_graph_cmd), + [this](ten::ten_env_t &ten_env, + std::unique_ptr cmd) { auto status_code = cmd->get_status_code(); ASSERT_EQ(status_code, TEN_STATUS_CODE_OK); @@ -79,8 +72,14 @@ class test_predefined_graph : public ten::extension_t { })"_json; hello_cmd["_ten"]["dest"][0]["graph"] = graph_id; - ten_env.send_json( - hello_cmd.dump().c_str(), + auto hello_world_cmd = ten::cmd_t::create("hello_world"); + hello_world_cmd->set_dest( + "msgpack://127.0.0.1:8001/", graph_id.c_str(), + "start_predefined_graph_cross_app__normal_extension_group", + "normal_extension_1"); + + ten_env.send_cmd( + std::move(hello_world_cmd), [this](ten::ten_env_t &ten_env, std::unique_ptr cmd) { auto status_code = cmd->get_status_code(); diff --git a/tests/ten_runtime/smoke/extension_test/start_graph/start_predefined_graph_from_extension.cc b/tests/ten_runtime/smoke/extension_test/start_graph/start_predefined_graph_from_extension.cc index 3b279f1be0..7b6d9700da 100644 --- a/tests/ten_runtime/smoke/extension_test/start_graph/start_predefined_graph_from_extension.cc +++ b/tests/ten_runtime/smoke/extension_test/start_graph/start_predefined_graph_from_extension.cc @@ -33,42 +33,27 @@ class test_predefined_graph : public ten::extension_t { : ten::extension_t(name) {} void on_start(ten::ten_env_t &ten_env) override { - std::string start_graph_json = R"({ - "_ten": { - "type": "start_graph", - "seq_id": "222", - "dest": [{ - "app": "localhost" - }], - "predefined_graph": "graph_1" - } - })"_json.dump(); - - ten_env.send_json( - start_graph_json.c_str(), - [this, start_graph_json](ten::ten_env_t &ten_env, - std::unique_ptr cmd) { + auto start_graph_cmd = ten::cmd_start_graph_t::create(); + start_graph_cmd->set_dest("localhost", nullptr, nullptr, nullptr); + start_graph_cmd->set_predefined_graph_name("graph_1"); + + ten_env.send_cmd( + std::move(start_graph_cmd), + [this](ten::ten_env_t &ten_env, + std::unique_ptr cmd) { auto status_code = cmd->get_status_code(); ASSERT_EQ(status_code, TEN_STATUS_CODE_OK); auto graph_id = cmd->get_property_string("detail"); - nlohmann::json hello_cmd = - R"({ - "_ten": { - "name": "hello_world", - "seq_id": "137", - "dest":[{ - "app": "msgpack://127.0.0.1:8001/", - "extension_group": "start_predefined_graph_from_extension__normal_extension_group", - "extension": "normal_extension" - }] - } - })"_json; - hello_cmd["_ten"]["dest"][0]["graph"] = graph_id; - - ten_env.send_json( - hello_cmd.dump().c_str(), + auto hello_world_cmd = ten::cmd_t::create("hello_world"); + hello_world_cmd->set_dest( + "msgpack://127.0.0.1:8001/", graph_id.c_str(), + "start_predefined_graph_from_extension__normal_extension_group", + "normal_extension"); + + ten_env.send_cmd( + std::move(hello_world_cmd), [this](ten::ten_env_t &ten_env, std::unique_ptr cmd) { received_hello_world_resp = true; diff --git a/tests/ten_runtime/smoke/extension_test/start_graph/start_two_predefined_graphs.cc b/tests/ten_runtime/smoke/extension_test/start_graph/start_two_predefined_graphs.cc index 397163ffcd..878e7e34c4 100644 --- a/tests/ten_runtime/smoke/extension_test/start_graph/start_two_predefined_graphs.cc +++ b/tests/ten_runtime/smoke/extension_test/start_graph/start_two_predefined_graphs.cc @@ -4,8 +4,11 @@ // 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 "gtest/gtest.h" #include "include_internal/ten_runtime/binding/cpp/ten.h" +#include "ten_runtime/binding/cpp/internal/msg/cmd/start_graph.h" #include "tests/common/client/cpp/msgpack_tcp.h" #include "tests/ten_runtime/smoke/extension_test/util/binding/cpp/check.h" @@ -47,50 +50,35 @@ class test_predefined_graph : public ten::extension_t { static void start_graph_and_greet( std::string graph_name, ten::ten_env_t &ten_env, const std::function &cb) { - auto start_graph_json = R"({ - "_ten": { - "type": "start_graph", - "seq_id": "222", - "dest": [{ - "app": "localhost" - }] - } - })"_json; - start_graph_json["_ten"]["predefined_graph"] = graph_name; + auto start_graph_cmd = ten::cmd_start_graph_t::create(); + start_graph_cmd->set_dest("localhost", nullptr, nullptr, nullptr); + start_graph_cmd->set_predefined_graph_name(graph_name.c_str()); - ten_env.send_json( - start_graph_json.dump().c_str(), + ten_env.send_cmd( + std::move(start_graph_cmd), [cb](ten::ten_env_t &ten_env, std::unique_ptr cmd) { auto status_code = cmd->get_status_code(); ASSERT_EQ(status_code, TEN_STATUS_CODE_OK); auto graph_id = cmd->get_property_string("detail"); - nlohmann::json hello_cmd = - R"({ - "_ten": { - "name": "hello_world", - "seq_id": "137", - "dest":[{ - "app": "msgpack://127.0.0.1:8001/", - "extension_group": "start_two_predefined_graphs__normal_extension_group", - "extension": "normal_extension_1" - }] - } - })"_json; - hello_cmd["_ten"]["dest"][0]["graph"] = graph_id; - - ten_env.send_json(hello_cmd.dump().c_str(), - [cb](ten::ten_env_t &ten_env, - std::unique_ptr cmd) { - auto status_code = cmd->get_status_code(); - ASSERT_EQ(status_code, TEN_STATUS_CODE_OK); - - auto detail = cmd->get_property_string("detail"); - ASSERT_EQ(detail, "hello world, too"); - - cb(ten_env); - }); + auto hello_world_cmd = ten::cmd_t::create("hello_world"); + hello_world_cmd->set_dest( + "msgpack://127.0.0.1:8001/", graph_id.c_str(), + "start_two_predefined_graphs__normal_extension_group", + "normal_extension_1"); + + ten_env.send_cmd(std::move(hello_world_cmd), + [cb](ten::ten_env_t &ten_env, + std::unique_ptr cmd) { + auto status_code = cmd->get_status_code(); + ASSERT_EQ(status_code, TEN_STATUS_CODE_OK); + + auto detail = cmd->get_property_string("detail"); + ASSERT_EQ(detail, "hello world, too"); + + cb(ten_env); + }); }); } diff --git a/tests/ten_runtime/smoke/extension_test/timer/timer_one_shot.cc b/tests/ten_runtime/smoke/extension_test/timer/timer_one_shot.cc index d822ff668d..3500c4065b 100644 --- a/tests/ten_runtime/smoke/extension_test/timer/timer_one_shot.cc +++ b/tests/ten_runtime/smoke/extension_test/timer/timer_one_shot.cc @@ -29,20 +29,13 @@ class test_extension : public ten::extension_t { hello_world_cmd = std::move(cmd); // Start a timer. - nlohmann::json timer_cmd_json = - R"({ - "_ten": { - "type": "timer", - "dest": [{ - "app": "localhost" - }], - "timer_id": 55, - "timeout_in_us": 100 - } - })"_json; - timer_cmd_json["_ten"]["times"] = TIMER_TIMES; - - ten_env.send_json(timer_cmd_json.dump().c_str()); + auto timer_cmd = ten::cmd_timer_t::create(); + timer_cmd->set_dest("localhost", nullptr, nullptr, nullptr); + timer_cmd->set_timer_id(55); + timer_cmd->set_timeout_in_us(100); + timer_cmd->set_times(TIMER_TIMES); + + ten_env.send_cmd(std::move(timer_cmd)); return; } else if (json["_ten"]["type"] == "timeout" && json["_ten"]["timer_id"].get() == 55) { diff --git a/tests/ten_runtime/smoke/extension_test/timer/timer_ten_shot.cc b/tests/ten_runtime/smoke/extension_test/timer/timer_ten_shot.cc index 80ba0f754e..43158f9bf8 100644 --- a/tests/ten_runtime/smoke/extension_test/timer/timer_ten_shot.cc +++ b/tests/ten_runtime/smoke/extension_test/timer/timer_ten_shot.cc @@ -28,20 +28,13 @@ class test_extension : public ten::extension_t { if (json["_ten"]["name"] == "hello_world") { hello_world_cmd = std::move(cmd); - nlohmann::json timer_cmd_json = - R"({ - "_ten": { - "type": "timer", - "dest": [{ - "app": "localhost" - }], - "timer_id": 55, - "timeout_in_us": 100 - } - })"_json; - timer_cmd_json["_ten"]["times"] = TIMER_TIMES; - - ten_env.send_json(timer_cmd_json.dump().c_str()); + auto timer_cmd = ten::cmd_timer_t::create(); + timer_cmd->set_dest("localhost", nullptr, nullptr, nullptr); + timer_cmd->set_timer_id(55); + timer_cmd->set_timeout_in_us(100); + timer_cmd->set_times(TIMER_TIMES); + + ten_env.send_cmd(std::move(timer_cmd)); return; } else if (json["_ten"]["type"] == "timeout" && json["_ten"]["timer_id"].get() == 55) { diff --git a/tests/ten_runtime/smoke/extension_test/timer/timer_two_shot.cc b/tests/ten_runtime/smoke/extension_test/timer/timer_two_shot.cc index ce424f2574..5b6a4f6601 100644 --- a/tests/ten_runtime/smoke/extension_test/timer/timer_two_shot.cc +++ b/tests/ten_runtime/smoke/extension_test/timer/timer_two_shot.cc @@ -28,19 +28,13 @@ class test_extension : public ten::extension_t { if (json["_ten"]["name"] == "hello_world") { hello_world_cmd = std::move(cmd); - nlohmann::json timer_cmd_json = R"({ - "_ten": { - "type": "timer", - "dest": [{ - "app": "localhost" - }], - "timer_id": 55, - "timeout_in_us": 100 - } - })"_json; - timer_cmd_json["_ten"]["times"] = TIMER_TIMES; - - ten_env.send_json(timer_cmd_json.dump().c_str()); + auto timer_cmd = ten::cmd_timer_t::create(); + timer_cmd->set_dest("localhost", nullptr, nullptr, nullptr); + timer_cmd->set_timer_id(55); + timer_cmd->set_timeout_in_us(100); + timer_cmd->set_times(TIMER_TIMES); + + ten_env.send_cmd(std::move(timer_cmd)); return; } else if (json["_ten"]["type"] == "timeout" && json["_ten"]["timer_id"].get() == 55) { diff --git a/tests/ten_runtime/smoke/msg_test/msg_1.cc b/tests/ten_runtime/smoke/msg_test/msg_1.cc index 726544a322..447456ed02 100644 --- a/tests/ten_runtime/smoke/msg_test/msg_1.cc +++ b/tests/ten_runtime/smoke/msg_test/msg_1.cc @@ -28,20 +28,13 @@ class test_extension : public ten::extension_t { hello_world_cmd = std::move(cmd); // Start a timer. - nlohmann::json timer_cmd_json = - R"({ - "_ten": { - "type": "timer", - "dest": [{ - "app": "localhost" - }], - "timer_id": 55, - "timeout_in_us": 100 - } - })"_json; - timer_cmd_json["_ten"]["times"] = TIMER_TIMES; - - bool success = ten_env.send_json(timer_cmd_json.dump().c_str()); + auto timer_cmd = ten::cmd_timer_t::create(); + timer_cmd->set_dest("localhost", nullptr, nullptr, nullptr); + timer_cmd->set_timer_id(55); + timer_cmd->set_timeout_in_us(100); + timer_cmd->set_times(TIMER_TIMES); + + bool success = ten_env.send_cmd(std::move(timer_cmd)); EXPECT_EQ(success, true); } else if (json["_ten"]["type"] == "timeout" && json["_ten"]["timer_id"].get() == 55) { diff --git a/tests/ten_runtime/smoke/msg_test/msg_2.cc b/tests/ten_runtime/smoke/msg_test/msg_2.cc index 8e7bd2c5cd..cc89771570 100644 --- a/tests/ten_runtime/smoke/msg_test/msg_2.cc +++ b/tests/ten_runtime/smoke/msg_test/msg_2.cc @@ -29,21 +29,13 @@ class test_extension : public ten::extension_t { hello_world_cmd = std::move(cmd); // Start a timer. - nlohmann::json timer_cmd_json = - R"({ - "_ten": { - "type": "cmd", - "name": "ten:timer", - "dest": [{ - "app": "localhost" - }], - "timer_id": 55, - "timeout_in_us": 100 - } - })"_json; - timer_cmd_json["_ten"]["times"] = TIMER_TIMES; - - bool success = ten_env.send_json(timer_cmd_json.dump().c_str()); + auto timer_cmd = ten::cmd_timer_t::create(); + timer_cmd->set_dest("localhost", nullptr, nullptr, nullptr); + timer_cmd->set_timer_id(55); + timer_cmd->set_timeout_in_us(100); + timer_cmd->set_times(TIMER_TIMES); + + bool success = ten_env.send_cmd(std::move(timer_cmd)); EXPECT_EQ(success, true); } else if (json["_ten"]["type"] == "timeout" && json["_ten"]["timer_id"].get() == 55) { diff --git a/tests/ten_runtime/smoke/msg_test/msg_6.cc b/tests/ten_runtime/smoke/msg_test/msg_6.cc index 9e4bcbbb9a..9c75293cbe 100644 --- a/tests/ten_runtime/smoke/msg_test/msg_6.cc +++ b/tests/ten_runtime/smoke/msg_test/msg_6.cc @@ -29,21 +29,13 @@ class test_extension : public ten::extension_t { hello_world_cmd = std::move(cmd); // Start a timer. - nlohmann::json timer_cmd_json = - R"({ - "_ten": { - "type": "timer", - "name": "ten:timer", - "dest": [{ - "app": "localhost" - }], - "timer_id": 55, - "timeout_in_us": 100 - } - })"_json; - timer_cmd_json["_ten"]["times"] = TIMER_TIMES; - - bool success = ten_env.send_json(timer_cmd_json.dump().c_str()); + auto timer_cmd = ten::cmd_timer_t::create(); + timer_cmd->set_dest("localhost", nullptr, nullptr, nullptr); + timer_cmd->set_timer_id(55); + timer_cmd->set_timeout_in_us(100); + timer_cmd->set_times(TIMER_TIMES); + + bool success = ten_env.send_cmd(std::move(timer_cmd)); EXPECT_EQ(success, true); } else if (json["_ten"]["type"] == "timeout" && json["_ten"]["timer_id"].get() == 55) { diff --git a/tests/ten_runtime/smoke/msg_test/msg_7.cc b/tests/ten_runtime/smoke/msg_test/msg_7.cc index 94cc373f27..9c39e4aba1 100644 --- a/tests/ten_runtime/smoke/msg_test/msg_7.cc +++ b/tests/ten_runtime/smoke/msg_test/msg_7.cc @@ -29,20 +29,13 @@ class test_extension : public ten::extension_t { hello_world_cmd = std::move(cmd); // Start a timer. - nlohmann::json timer_cmd_json = - R"({ - "_ten": { - "type": "timer", - "dest": [{ - "app": "localhost" - }], - "timer_id": 55, - "timeout_in_us": 100 - } - })"_json; - timer_cmd_json["_ten"]["times"] = TIMER_TIMES; - - bool success = ten_env.send_json(timer_cmd_json.dump().c_str()); + auto timer_cmd = ten::cmd_timer_t::create(); + timer_cmd->set_dest("localhost", nullptr, nullptr, nullptr); + timer_cmd->set_timer_id(55); + timer_cmd->set_timeout_in_us(100); + timer_cmd->set_times(TIMER_TIMES); + + bool success = ten_env.send_cmd(std::move(timer_cmd)); EXPECT_EQ(success, true); } else if (json["_ten"]["type"] == "timeout" && json["_ten"]["timer_id"].get() == 55) { diff --git a/tests/ten_runtime/smoke/msg_test/msg_fail_1.cc b/tests/ten_runtime/smoke/msg_test/msg_fail_1.cc index aa31a24417..9863e6ad5b 100644 --- a/tests/ten_runtime/smoke/msg_test/msg_fail_1.cc +++ b/tests/ten_runtime/smoke/msg_test/msg_fail_1.cc @@ -29,39 +29,13 @@ class test_extension : public ten::extension_t { hello_world_cmd = std::move(cmd); // Start a timer. - nlohmann::json timer_cmd_json = - R"({ - "_ten": { - "type": "incorrect_type", - "name": "ten:timer", - "dest": [{ - "app": "localhost" - }], - "timer_id": 55, - "timeout_in_us": 100 - } - })"_json; - timer_cmd_json["_ten"]["times"] = TIMER_TIMES; - - bool success = ten_env.send_json(timer_cmd_json.dump().c_str()); - EXPECT_EQ(success, false); + auto timer_cmd = ten::cmd_timer_t::create(); + timer_cmd->set_dest("localhost", nullptr, nullptr, nullptr); + timer_cmd->set_timer_id(55); + timer_cmd->set_timeout_in_us(100); + timer_cmd->set_times(TIMER_TIMES); - // Start a timer. - timer_cmd_json = - R"({ - "_ten": { - "type": "timer", - "name": "ten:timer", - "dest": [{ - "app": "localhost" - }], - "timer_id": 55, - "timeout_in_us": 100 - } - })"_json; - timer_cmd_json["_ten"]["times"] = TIMER_TIMES; - - success = ten_env.send_json(timer_cmd_json.dump().c_str()); + bool success = ten_env.send_cmd(std::move(timer_cmd)); EXPECT_EQ(success, true); } else if (json["_ten"]["type"] == "timeout" && json["_ten"]["timer_id"].get() == 55) {