Skip to content

refactor: remove localhost references in command destination settings across multiple files #840

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@
"request": "launch",
"program": "${workspaceFolder}/out/linux/x64/tests/standalone/ten_runtime_smoke_test",
"args": [
"--gtest_filter=ExtensionTest.ExtensionWithSameName"
"--gtest_filter=ExtensionTest.HttpServerExtensionTwoExtensions"
],
"cwd": "${workspaceFolder}/out/linux/x64/tests/standalone/",
"env": {
Expand Down
2 changes: 1 addition & 1 deletion core/include/ten_runtime/binding/cpp/detail/extension.h
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ class extension_t : public binding_handle_t {
ten_shared_ptr_t *stop_graph_cmd = ten_cmd_stop_graph_create();
TEN_ASSERT(stop_graph_cmd, "Should not happen.");

ten_msg_clear_and_set_dest(stop_graph_cmd, "localhost", nullptr, nullptr,
ten_msg_clear_and_set_dest(stop_graph_cmd, nullptr, nullptr, nullptr,
nullptr);
ten_env_send_cmd(ten_env.get_c_ten_env(), stop_graph_cmd, nullptr, nullptr,
nullptr, nullptr);
Expand Down
6 changes: 3 additions & 3 deletions core/include/ten_runtime/binding/cpp/detail/msg/msg.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ class msg_t {
return ten_msg_get_name(c_msg);
}

bool set_dest(const char *uri, const char *graph, const char *extension_name,
error_t *err = nullptr) const {
bool set_dest(const char *app_uri, const char *graph_id,
const char *extension_name, error_t *err = nullptr) const {
TEN_ASSERT(c_msg, "Should not happen.");

if (c_msg == nullptr) {
Expand All @@ -69,7 +69,7 @@ class msg_t {
}

return ten_msg_clear_and_set_dest(
c_msg, uri, graph, extension_name,
c_msg, app_uri, graph_id, extension_name,
err != nullptr ? err->get_c_error() : nullptr);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,6 @@ TEN_RUNTIME_PRIVATE_API bool ten_extensions_info_clone(ten_list_t *from,
TEN_RUNTIME_PRIVATE_API bool ten_extension_info_check_integrity(
ten_extension_info_t *self, bool check_thread);

TEN_RUNTIME_PRIVATE_API void ten_extension_info_translate_localhost_to_app_uri(
ten_extension_info_t *self, const char *app_uri);

TEN_RUNTIME_PRIVATE_API ten_shared_ptr_t *get_extension_info_in_extensions_info(
ten_list_t *extensions_info, const char *app_uri, const char *graph_id,
const char *extension_group_name, const char *extension_addon_name,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,3 @@ TEN_RUNTIME_PRIVATE_API void ten_all_msg_type_dest_info_init(

TEN_RUNTIME_PRIVATE_API void ten_all_msg_type_dest_info_deinit(
ten_all_msg_type_dest_info_t *self);

TEN_RUNTIME_PRIVATE_API void
ten_all_msg_type_dest_info_translate_localhost_to_app_uri(
ten_all_msg_type_dest_info_t *self, const char *uri);
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,5 @@ TEN_RUNTIME_PRIVATE_API ten_shared_ptr_t *ten_msg_dest_info_clone(
TEN_RUNTIME_PRIVATE_API void ten_msg_dest_info_destroy(
ten_msg_dest_info_t *self);

TEN_RUNTIME_PRIVATE_API void ten_msg_dest_info_translate_localhost_to_app_uri(
ten_msg_dest_info_t *self, const char *uri);

TEN_RUNTIME_PRIVATE_API bool ten_msg_dest_info_qualified(
ten_msg_dest_info_t *self, const char *msg_name);
6 changes: 3 additions & 3 deletions core/src/ten_runtime/addon/protocol/protocol.c
Original file line number Diff line number Diff line change
Expand Up @@ -217,10 +217,10 @@ static void ten_app_create_protocol_with_uri_task(void *self_, void *arg) {
}

bool ten_addon_create_protocol_with_uri(
ten_env_t *ten_env, const char *uri, TEN_PROTOCOL_ROLE role,
ten_env_t *ten_env, const char *app_uri, TEN_PROTOCOL_ROLE role,
ten_env_addon_on_create_protocol_async_cb_t cb, void *user_data,
ten_error_t *err) {
TEN_ASSERT(uri && role > TEN_PROTOCOL_ROLE_INVALID, "Should not happen.");
TEN_ASSERT(app_uri && role > TEN_PROTOCOL_ROLE_INVALID, "Should not happen.");
TEN_ASSERT(ten_env, "Should not happen.");
TEN_ASSERT(ten_env_check_integrity(ten_env, true), "Should not happen.");
TEN_ASSERT(err, "Invalid argument.");
Expand Down Expand Up @@ -253,7 +253,7 @@ bool ten_addon_create_protocol_with_uri(
"Should not happen.");

ten_addon_create_protocol_ctx_t *ctx =
ten_addon_create_protocol_ctx_create(uri, role, cb, user_data);
ten_addon_create_protocol_ctx_create(app_uri, role, cb, user_data);
TEN_ASSERT(ctx, "Failed to allocate memory.");

ten_addon_context_t *addon_context = ten_addon_context_create();
Expand Down
13 changes: 3 additions & 10 deletions core/src/ten_runtime/app/metadata.c
Original file line number Diff line number Diff line change
Expand Up @@ -98,16 +98,9 @@ bool ten_app_init_uri(ten_app_t *self, ten_value_t *value) {
return false;
}

ten_string_t default_url;
ten_string_init_formatted(&default_url, TEN_STR_LOCALHOST);

const char *url_str = ten_value_peek_raw_str(value, NULL)
? ten_value_peek_raw_str(value, NULL)
: ten_string_get_raw_str(&default_url);

ten_string_set_from_c_str(&self->uri, url_str);

ten_string_deinit(&default_url);
if (ten_value_peek_raw_str(value, NULL)) {
ten_string_set_from_c_str(&self->uri, ten_value_peek_raw_str(value, NULL));
}

return true;
}
Expand Down
6 changes: 1 addition & 5 deletions core/src/ten_runtime/app/msg_interface/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -408,8 +408,6 @@ bool ten_app_dispatch_msg(ten_app_t *self, ten_shared_ptr_t *msg,
TEN_ASSERT(dest_loc, "Should not happen.");
TEN_ASSERT(ten_loc_check_integrity(dest_loc), "Should not happen.");
TEN_ASSERT(ten_msg_get_dest_cnt(msg) == 1, "Should not happen.");
TEN_ASSERT(!ten_string_is_empty(&dest_loc->app_uri),
"App URI should not be empty.");

if (!ten_string_is_equal_c_str(&dest_loc->app_uri, ten_app_get_uri(self))) {
TEN_ASSERT(0, "Handle this condition, msg dest '%s', app '%s'",
Expand Down Expand Up @@ -619,9 +617,7 @@ void ten_app_push_to_in_msgs_queue(ten_app_t *self, ten_shared_ptr_t *msg) {
TEN_ASSERT(ten_app_check_integrity(self, false), "Should not happen.");
TEN_ASSERT(msg && ten_msg_is_cmd_and_result(msg), "Invalid argument.");
TEN_ASSERT(!ten_cmd_base_cmd_id_is_empty(msg), "Invalid argument.");
TEN_ASSERT(
ten_msg_get_src_app_uri(msg) && strlen(ten_msg_get_src_app_uri(msg)),
"Invalid argument.");
TEN_ASSERT(ten_msg_get_src_app_uri(msg), "Invalid argument.");
TEN_ASSERT((ten_msg_get_dest_cnt(msg) == 1), "Invalid argument.");

TEN_UNUSED bool rc = ten_mutex_lock(self->in_msgs_lock);
Expand Down
6 changes: 1 addition & 5 deletions core/src/ten_runtime/app/ten_env/on_xxx.c
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ static void ten_app_continue_run_after_load_all_extensions_if_specified(
goto error;
}

if (!ten_string_is_equal_c_str(&self->uri, TEN_STR_LOCALHOST) &&
if (!ten_string_is_empty(&self->uri) &&
!ten_string_starts_with(&self->uri, TEN_STR_CLIENT)) {
// Create the app listening endpoint protocol if specifying one.
bool rc = ten_addon_create_protocol_with_uri(
Expand Down Expand Up @@ -253,10 +253,6 @@ void ten_app_on_configure_done(ten_env_t *ten_env) {
ten_metadata_init_schema_store(&self->manifest, &self->schema_store);
ten_app_adjust_and_validate_property_on_configure_done(self);

if (ten_string_is_empty(&self->uri)) {
ten_string_set_from_c_str(&self->uri, TEN_STR_LOCALHOST);
}

ten_addon_manager_t *manager = ten_addon_manager_get_instance();
TEN_ASSERT(manager, "Should not happen.");

Expand Down
2 changes: 0 additions & 2 deletions core/src/ten_runtime/engine/msg_interface/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,6 @@ static void ten_engine_handle_in_msgs_sync(ten_engine_t *self) {
ten_shared_ptr_t *msg = ten_smart_ptr_listnode_get(iter.node);
TEN_ASSERT(msg, "Should not happen.");
TEN_ASSERT(ten_msg_check_integrity(msg), "Should not happen.");
TEN_ASSERT(!ten_msg_src_is_empty(msg),
"The message source should have been set.");

if (ten_msg_is_cmd_and_result(msg)) {
const char *src_uri = ten_msg_get_src_app_uri(msg);
Expand Down
32 changes: 4 additions & 28 deletions core/src/ten_runtime/extension/extension_info/extension_info.c
Original file line number Diff line number Diff line change
Expand Up @@ -372,19 +372,6 @@ bool ten_extension_info_check_integrity(ten_extension_info_t *self,
return true;
}

void ten_extension_info_translate_localhost_to_app_uri(
ten_extension_info_t *self, const char *uri) {
TEN_ASSERT(self, "Should not happen.");
TEN_ASSERT(ten_extension_info_check_integrity(self, false),
"Should not happen.");
TEN_ASSERT(uri, "Should not happen.");

if (ten_string_is_equal_c_str(&self->loc.app_uri, TEN_STR_LOCALHOST) ||
ten_string_is_empty(&self->loc.app_uri)) {
ten_string_init_from_c_str_with_size(&self->loc.app_uri, uri, strlen(uri));
}
}

ten_extension_info_t *ten_extension_info_from_smart_ptr(
ten_smart_ptr_t *extension_info_smart_ptr) {
TEN_ASSERT(extension_info_smart_ptr, "Invalid argument.");
Expand Down Expand Up @@ -448,8 +435,7 @@ static void ten_extension_info_fill_loc_info(ten_extension_info_t *self,
ten_string_set_formatted(&self->loc.graph_id, "%s", graph_id);
}

if (ten_string_is_empty(&self->loc.app_uri) ||
ten_string_is_equal_c_str(&self->loc.app_uri, TEN_STR_LOCALHOST)) {
if (ten_string_is_empty(&self->loc.app_uri)) {
ten_string_set_formatted(&self->loc.app_uri, app_uri);
}

Expand All @@ -466,8 +452,7 @@ static void ten_extension_info_fill_loc_info(ten_extension_info_t *self,
graph_id);
}

if (ten_string_is_empty(&self->loc.app_uri) ||
ten_string_is_equal_c_str(&self->loc.app_uri, TEN_STR_LOCALHOST)) {
if (ten_string_is_empty(&self->loc.app_uri)) {
ten_string_set_formatted(&self->loc.app_uri, app_uri);
}
}
Expand Down Expand Up @@ -501,19 +486,10 @@ void ten_extensions_info_fill_loc_info(ten_list_t *extensions_info,
ten_list_foreach (&dest_info->dest, dest_iter) {
ten_extension_info_t *dest_extension_info =
ten_smart_ptr_get_data(ten_smart_ptr_listnode_get(dest_iter.node));
if (ten_string_is_empty(&dest_extension_info->loc.app_uri)) {
TEN_ASSERT(0, "extension_info->loc.app_uri should not be empty.");
return;
}

if (ten_string_is_equal_c_str(&dest_extension_info->loc.app_uri,
TEN_STR_LOCALHOST)) {
if (!ten_string_is_equal_c_str(&dest_extension_info->loc.app_uri,
app_uri)) {
TEN_ASSERT(0,
"extension_info->loc.app_uri should not be localhost.");
return;
}
TEN_ASSERT(0, "extension_info->loc.app_uri should not be localhost.");
return;
}

if (ten_string_is_empty(&dest_extension_info->loc.graph_id)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
//
#include "include_internal/ten_runtime/extension/msg_dest_info/all_msg_type_dest_info.h"

#include "include_internal/ten_runtime/extension/msg_dest_info/msg_dest_info.h"
#include "ten_utils/lib/smart_ptr.h"
#include "ten_utils/macro/check.h"

void ten_all_msg_type_dest_info_init(ten_all_msg_type_dest_info_t *self) {
Expand All @@ -27,47 +25,3 @@ void ten_all_msg_type_dest_info_deinit(ten_all_msg_type_dest_info_t *self) {
ten_list_clear(&self->audio_frame);
ten_list_clear(&self->data);
}

static void translate_localhost_to_app_uri_for_msg_dest(ten_shared_ptr_t *dest,
const char *uri) {
ten_msg_dest_info_t *raw_dest = ten_shared_ptr_get_data(dest);
ten_msg_dest_info_translate_localhost_to_app_uri(raw_dest, uri);
}

static void translate_localhost_to_app_uri_for_dest(
ten_listnode_t *node, const char *uri,
void (*translate_func)(ten_shared_ptr_t *dest, const char *uri)) {
TEN_ASSERT(node, "Invalid argument.");
TEN_ASSERT(uri, "Invalid argument.");
TEN_ASSERT(translate_func, "Invalid argument.");

ten_shared_ptr_t *shared_dest = ten_smart_ptr_listnode_get(node);
TEN_ASSERT(shared_dest, "Should not happen.");

translate_func(shared_dest, uri);
}

void ten_all_msg_type_dest_info_translate_localhost_to_app_uri(
ten_all_msg_type_dest_info_t *self, const char *uri) {
TEN_ASSERT(self && uri, "Invalid argument.");

ten_list_foreach (&self->cmd, iter) {
translate_localhost_to_app_uri_for_dest(
iter.node, uri, translate_localhost_to_app_uri_for_msg_dest);
}

ten_list_foreach (&self->data, iter) {
translate_localhost_to_app_uri_for_dest(
iter.node, uri, translate_localhost_to_app_uri_for_msg_dest);
}

ten_list_foreach (&self->video_frame, iter) {
translate_localhost_to_app_uri_for_dest(
iter.node, uri, translate_localhost_to_app_uri_for_msg_dest);
}

ten_list_foreach (&self->audio_frame, iter) {
translate_localhost_to_app_uri_for_dest(
iter.node, uri, translate_localhost_to_app_uri_for_msg_dest);
}
}
15 changes: 0 additions & 15 deletions core/src/ten_runtime/extension/msg_dest_info/msg_dest_info.c
Original file line number Diff line number Diff line change
Expand Up @@ -96,21 +96,6 @@ ten_shared_ptr_t *ten_msg_dest_info_clone(ten_shared_ptr_t *self,
return ten_shared_ptr_create(new_self, ten_msg_dest_info_destroy);
}

void ten_msg_dest_info_translate_localhost_to_app_uri(ten_msg_dest_info_t *self,
const char *uri) {
TEN_ASSERT(self && uri, "Should not happen.");

ten_list_foreach (&self->dest, iter) {
ten_shared_ptr_t *shared_dest =
ten_weak_ptr_lock(ten_smart_ptr_listnode_get(iter.node));

ten_extension_info_t *extension_info = ten_shared_ptr_get_data(shared_dest);
ten_extension_info_translate_localhost_to_app_uri(extension_info, uri);

ten_shared_ptr_destroy(shared_dest);
}
}

bool ten_msg_dest_info_qualified(ten_msg_dest_info_t *self,
const char *msg_name) {
TEN_ASSERT(self && ten_msg_dest_info_check_integrity(self) && msg_name,
Expand Down
35 changes: 9 additions & 26 deletions core/src/ten_runtime/msg/msg.c
Original file line number Diff line number Diff line change
Expand Up @@ -105,29 +105,28 @@ void ten_msg_set_src_to_loc(ten_shared_ptr_t *self, ten_loc_t *loc) {

// The semantics of the following function is to replace the destination
// information to one which is specified through the parameters.
static bool ten_raw_msg_clear_and_set_dest(ten_msg_t *self, const char *uri,
static bool ten_raw_msg_clear_and_set_dest(ten_msg_t *self, const char *app_uri,
const char *graph_id,
const char *extension_name,
TEN_UNUSED ten_error_t *err) {
TEN_ASSERT(self, "Should not happen.");
TEN_ASSERT(ten_raw_msg_check_integrity(self), "Should not happen.");
TEN_ASSERT((uri != NULL || extension_name != NULL), "Should not happen.");

ten_list_clear(&self->dest_loc);
ten_list_push_ptr_back(&self->dest_loc,
ten_loc_create(uri, graph_id, extension_name),
ten_loc_create(app_uri, graph_id, extension_name),
(ten_ptr_listnode_destroy_func_t)ten_loc_destroy);

return true;
}

void ten_raw_msg_add_dest(ten_msg_t *self, const char *uri,
void ten_raw_msg_add_dest(ten_msg_t *self, const char *app_uri,
const char *graph_id, const char *extension_name) {
TEN_ASSERT(self, "Should not happen.");
TEN_ASSERT(ten_raw_msg_check_integrity(self), "Should not happen.");

ten_list_push_ptr_back(&self->dest_loc,
ten_loc_create(uri, graph_id, extension_name),
ten_loc_create(app_uri, graph_id, extension_name),
(ten_ptr_listnode_destroy_func_t)ten_loc_destroy);
}

Expand Down Expand Up @@ -332,13 +331,13 @@ void ten_msg_set_src_engine_if_unspecified(ten_shared_ptr_t *self,
}
}

bool ten_msg_clear_and_set_dest(ten_shared_ptr_t *self, const char *uri,
bool ten_msg_clear_and_set_dest(ten_shared_ptr_t *self, const char *app_uri,
const char *graph_id,
const char *extension_name, ten_error_t *err) {
TEN_ASSERT(self, "Should not happen.");
TEN_ASSERT(ten_msg_check_integrity(self), "Should not happen.");

return ten_raw_msg_clear_and_set_dest(ten_msg_get_raw_msg(self), uri,
return ten_raw_msg_clear_and_set_dest(ten_msg_get_raw_msg(self), app_uri,
graph_id, extension_name, err);
}

Expand Down Expand Up @@ -984,11 +983,9 @@ void ten_msg_correct_dest(ten_shared_ptr_t *msg, ten_engine_t *engine) {

if (ten_string_is_equal_c_str(&dest_loc->app_uri, app_uri)) {
is_local_app = true;
} else if (ten_string_is_equal_c_str(&dest_loc->app_uri,
TEN_STR_LOCALHOST)) {
// When an extension uses 'localhost' to indicate the destination is the
// local app, we need to replace 'localhost' with the actual URI of the
// app.
} else if (ten_string_is_empty(&dest_loc->app_uri)) {
// When an extension uses empty string to indicate the destination is the
// local app, we need to replace it with the actual URI of the app.
ten_string_set_from_c_str(&dest_loc->app_uri, app_uri);
is_local_app = true;
}
Expand Down Expand Up @@ -1017,20 +1014,6 @@ void ten_msg_correct_dest(ten_shared_ptr_t *msg, ten_engine_t *engine) {
}
}
}

// Special handling for start_graph commands:
// In a start_graph command, extension URIs might be specified as 'localhost',
// indicating they should be located in the local app.
// We need to replace these 'localhost' references with the actual app URI.
if (ten_msg_get_type(msg) == TEN_MSG_TYPE_CMD_START_GRAPH) {
ten_list_t *extensions_info = ten_cmd_start_graph_get_extensions_info(msg);
ten_list_foreach (extensions_info, iter) {
ten_extension_info_t *extension_info =
ten_shared_ptr_get_data(ten_smart_ptr_listnode_get(iter.node));
ten_extension_info_translate_localhost_to_app_uri(extension_info,
app_uri);
}
}
}

static bool ten_raw_msg_dump_internal(ten_msg_t *msg, ten_error_t *err,
Expand Down
Loading
Loading