Skip to content
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

refactor: make ten_addon_create_instance_async internal usage #233

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
4 changes: 2 additions & 2 deletions core/include/ten_runtime/addon/extension/extension.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ TEN_RUNTIME_API ten_addon_host_t *ten_addon_register_extension(

TEN_RUNTIME_API ten_addon_t *ten_addon_unregister_extension(const char *name);

TEN_RUNTIME_API bool ten_addon_create_extension_async(
TEN_RUNTIME_API bool ten_addon_create_extension(
ten_env_t *ten_env, const char *addon_name, const char *instance_name,
ten_env_addon_on_create_instance_async_cb_t cb, void *user_data,
ten_error_t *err);

TEN_RUNTIME_API bool ten_addon_destroy_extension_async(
TEN_RUNTIME_API bool ten_addon_destroy_extension(
ten_env_t *ten_env, ten_extension_t *extension,
ten_env_addon_on_destroy_instance_async_cb_t cb, void *user_data,
ten_error_t *err);
4 changes: 2 additions & 2 deletions core/include/ten_runtime/binding/cpp/internal/ten_env.h
Original file line number Diff line number Diff line change
Expand Up @@ -894,13 +894,13 @@ class ten_env_t {
addon_create_extension_async_cb_t &&cb,
error_t *err) {
if (cb == nullptr) {
return ten_addon_create_extension_async(
return ten_addon_create_extension(
c_ten_env, addon_name, instance_name, nullptr, nullptr,
err != nullptr ? err->get_internal_representation() : nullptr);
} else {
auto *cb_ptr = new addon_create_extension_async_cb_t(std::move(cb));

return ten_addon_create_extension_async(
return ten_addon_create_extension(
c_ten_env, addon_name, instance_name,
proxy_addon_create_extension_async_cb, cb_ptr,
err != nullptr ? err->get_internal_representation() : nullptr);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,13 @@ inline bool ten_env_t::addon_destroy_extension_async(
ten::extension_t *extension, addon_destroy_extension_async_cb_t &&cb,
error_t *err) {
if (cb == nullptr) {
return ten_addon_destroy_extension_async(
return ten_addon_destroy_extension(
c_ten_env, extension->get_c_extension(), nullptr, nullptr,
err != nullptr ? err->get_internal_representation() : nullptr);
} else {
auto *cb_ptr = new addon_destroy_extension_async_cb_t(std::move(cb));

return ten_addon_destroy_extension_async(
return ten_addon_destroy_extension(
c_ten_env, extension->get_c_extension(),
proxy_addon_destroy_extension_async_cb, cb_ptr,
err != nullptr ? err->get_internal_representation() : nullptr);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ typedef struct ten_addon_store_t ten_addon_store_t;

TEN_RUNTIME_PRIVATE_API ten_addon_store_t *ten_extension_group_get_store(void);

TEN_RUNTIME_API bool ten_addon_extension_group_create(
TEN_RUNTIME_API bool ten_addon_create_extension_group(
ten_env_t *ten_env, const char *addon_name, const char *instance_name,
ten_env_addon_on_create_instance_async_cb_t cb, void *user_data);

TEN_RUNTIME_API bool ten_addon_extension_group_destroy(
TEN_RUNTIME_API bool ten_addon_destroy_extension_group(
ten_env_t *ten_env, ten_extension_group_t *extension_group,
ten_env_addon_on_destroy_instance_async_cb_t cb, void *user_data);
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,16 @@ typedef struct ten_addon_create_protocol_info_t {
void *user_data;
} ten_addon_create_protocol_info_t;

TEN_RUNTIME_PRIVATE_API bool ten_addon_create_protocol_async(
TEN_RUNTIME_PRIVATE_API bool ten_addon_create_protocol_with_uri(
ten_env_t *ten_env, const char *uri, TEN_PROTOCOL_ROLE role,
ten_env_addon_on_create_protocol_async_cb_t cb, void *user_data,
ten_error_t *err);

TEN_RUNTIME_PRIVATE_API bool ten_addon_create_protocol(
ten_env_t *ten_env, const char *addon_name, const char *instance_name,
TEN_PROTOCOL_ROLE role, ten_env_addon_on_create_protocol_async_cb_t cb,
void *user_data, ten_error_t *err);

TEN_RUNTIME_PRIVATE_API ten_addon_store_t *ten_protocol_get_store(void);

TEN_RUNTIME_PRIVATE_API ten_addon_host_t *ten_addon_protocol_find(
Expand Down
10 changes: 5 additions & 5 deletions core/src/ten_runtime/addon/extension/extension.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ static ten_addon_store_t g_extension_store = {

ten_addon_store_t *ten_extension_get_store(void) { return &g_extension_store; }

bool ten_addon_create_extension_async(
ten_env_t *ten_env, const char *addon_name, const char *instance_name,
ten_env_addon_on_create_instance_async_cb_t cb, void *cb_data,
TEN_UNUSED ten_error_t *err) {
bool ten_addon_create_extension(ten_env_t *ten_env, const char *addon_name,
const char *instance_name,
ten_env_addon_on_create_instance_async_cb_t cb,
void *cb_data, TEN_UNUSED ten_error_t *err) {
TEN_ASSERT(addon_name && instance_name, "Should not happen.");

TEN_ASSERT(ten_env, "Invalid argument.");
Expand Down Expand Up @@ -70,7 +70,7 @@ bool ten_addon_create_extension_async(
}
}

bool ten_addon_destroy_extension_async(
bool ten_addon_destroy_extension(
ten_env_t *ten_env, ten_extension_t *extension,
ten_env_addon_on_destroy_instance_async_cb_t cb, void *cb_data,
TEN_UNUSED ten_error_t *err) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ ten_addon_t *ten_addon_unregister_extension_group(const char *name) {
return ten_addon_unregister(ten_extension_group_get_store(), name);
}

bool ten_addon_extension_group_create(
bool ten_addon_create_extension_group(
ten_env_t *ten_env, const char *addon_name, const char *instance_name,
ten_env_addon_on_create_instance_async_cb_t cb, void *user_data) {
TEN_ASSERT(addon_name && instance_name, "Should not happen.");
Expand All @@ -74,7 +74,7 @@ bool ten_addon_extension_group_create(
user_data);
}

bool ten_addon_extension_group_destroy(
bool ten_addon_destroy_extension_group(
ten_env_t *ten_env, ten_extension_group_t *extension_group,
ten_env_addon_on_destroy_instance_async_cb_t cb, void *cb_data) {
TEN_ASSERT(ten_env && ten_env_check_integrity(ten_env, true) && cb,
Expand Down
62 changes: 57 additions & 5 deletions core/src/ten_runtime/addon/protocol/protocol.c
Original file line number Diff line number Diff line change
Expand Up @@ -113,14 +113,19 @@ ten_addon_host_t *ten_addon_protocol_find(const char *protocol) {
static ten_addon_create_protocol_info_t *ten_addon_create_protocol_info_create(
const char *uri, TEN_PROTOCOL_ROLE role,
ten_env_addon_on_create_protocol_async_cb_t cb, void *user_data) {
TEN_ASSERT(uri && role > TEN_PROTOCOL_ROLE_INVALID, "Should not happen.");
TEN_ASSERT(role > TEN_PROTOCOL_ROLE_INVALID, "Should not happen.");

ten_addon_create_protocol_info_t *info =
(ten_addon_create_protocol_info_t *)TEN_MALLOC(
sizeof(ten_addon_create_protocol_info_t));
TEN_ASSERT(info, "Failed to allocate memory.");

ten_string_init_formatted(&info->uri, "%s", uri);
if (!uri || strlen(uri) == 0) {
ten_string_init(&info->uri);
} else {
ten_string_init_formatted(&info->uri, "%s", uri);
}

info->role = role;
info->cb = cb;
info->user_data = user_data;
Expand Down Expand Up @@ -150,8 +155,11 @@ static void proxy_on_addon_protocol_created(ten_env_t *ten_env, void *instance,
if (protocol) {
ten_protocol_determine_default_property_value(protocol);

ten_string_set_formatted(&protocol->uri, "%s",
ten_string_get_raw_str(&info->uri));
if (!ten_string_is_empty(&info->uri)) {
ten_string_set_formatted(&protocol->uri, "%s",
ten_string_get_raw_str(&info->uri));
}

protocol->role = info->role;
}

Expand All @@ -162,7 +170,7 @@ static void proxy_on_addon_protocol_created(ten_env_t *ten_env, void *instance,
ten_addon_create_protocol_info_destroy(info);
}

bool ten_addon_create_protocol_async(
bool ten_addon_create_protocol_with_uri(
ten_env_t *ten_env, const char *uri, TEN_PROTOCOL_ROLE role,
ten_env_addon_on_create_protocol_async_cb_t cb, void *user_data,
ten_error_t *err) {
Expand Down Expand Up @@ -222,3 +230,47 @@ bool ten_addon_create_protocol_async(

return true;
}

bool ten_addon_create_protocol(ten_env_t *ten_env, const char *addon_name,
const char *instance_name,
TEN_PROTOCOL_ROLE role,
ten_env_addon_on_create_protocol_async_cb_t cb,
void *user_data, ten_error_t *err) {
TEN_ASSERT(addon_name && instance_name, "Should not happen.");
TEN_ASSERT(role > TEN_PROTOCOL_ROLE_INVALID, "Should not happen.");
TEN_ASSERT(ten_env && ten_env_check_integrity(ten_env, true),
"Should not happen.");

TEN_ENV_ATTACH_TO attach_to = ten_env_get_attach_to(ten_env);
if (attach_to != TEN_ENV_ATTACH_TO_APP &&
attach_to != TEN_ENV_ATTACH_TO_ENGINE) {
TEN_LOGE("Invalid ten_env attach_to: %d", attach_to);
if (err) {
ten_error_set(err, TEN_ERRNO_INVALID_ARGUMENT, "Invalid ten_env.");
}
return false;
}

TEN_LOGD("Loading protocol addon: %s", addon_name);

ten_addon_create_protocol_info_t *info =
ten_addon_create_protocol_info_create(NULL, role, cb, user_data);
TEN_ASSERT(info, "Failed to allocate memory.");

bool rc = ten_addon_create_instance_async(
ten_env, addon_name, instance_name, TEN_ADDON_TYPE_PROTOCOL,
proxy_on_addon_protocol_created, info);

if (!rc) {
TEN_LOGE("Failed to create protocol for %s", addon_name);
ten_addon_create_protocol_info_destroy(info);

if (err) {
ten_error_set(err, TEN_ERRNO_GENERIC,
"Failed to create protocol for addon: %s.", addon_name);
}
return false;
}

return true;
}
2 changes: 1 addition & 1 deletion core/src/ten_runtime/app/ten_env/on_xxx.c
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ void ten_app_on_configure_done(ten_env_t *ten_env) {

if (!ten_string_is_equal_c_str(&self->uri, TEN_STR_LOCALHOST)) {
// Create the app listening endpoint protocol if specifying one.
rc = ten_addon_create_protocol_async(
rc = ten_addon_create_protocol_with_uri(
self->ten_env, ten_string_get_raw_str(&self->uri),
TEN_PROTOCOL_ROLE_LISTEN, ten_app_on_endpoint_protocol_created, NULL,
&err);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ static void ten_env_proxy_notify_addon_create_extension(ten_env_t *ten_env,
ten_error_t err;
ten_error_init(&err);

bool rc = ten_addon_create_extension_async(
bool rc = ten_addon_create_extension(
ten_env, ten_string_get_raw_str(&info->addon_name),
ten_string_get_raw_str(&info->instance_name),
proxy_addon_create_extension_done, info->callback_info, &err);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,9 @@ static void ten_env_proxy_notify_addon_destroy_extension(ten_env_t *ten_env,
ten_error_t err;
ten_error_init(&err);

bool rc = ten_addon_destroy_extension_async(
ten_env, info->c_extension, proxy_addon_destroy_extension_done, info,
&err);
bool rc = ten_addon_destroy_extension(ten_env, info->c_extension,
proxy_addon_destroy_extension_done,
info, &err);
TEN_ASSERT(rc, "Should not happen.");

ten_error_deinit(&err);
Expand Down
2 changes: 1 addition & 1 deletion core/src/ten_runtime/engine/internal/remote_interface.c
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ static bool ten_engine_create_remote_async(
ten_engine_on_protocol_created_info_create(on_remote_created_cb, cb_data);
TEN_ASSERT(info, "Failed to allocate memory.");

bool rc = ten_addon_create_protocol_async(
bool rc = ten_addon_create_protocol_with_uri(
self->ten_env, uri, TEN_PROTOCOL_ROLE_OUT_DEFAULT,
ten_engine_on_remote_protocol_created, info, &err);
if (!rc) {
Expand Down
4 changes: 2 additions & 2 deletions core/src/ten_runtime/extension_context/extension_context.c
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ static void destroy_extension_group_by_addon(
TEN_ASSERT(ten_env && ten_env_check_integrity(ten_env, true),
"Should not happen.");

ten_addon_extension_group_destroy(
ten_addon_destroy_extension_group(
ten_env, extension_group,
ten_extension_context_on_extension_group_destroyed, NULL);
}
Expand Down Expand Up @@ -566,7 +566,7 @@ bool ten_extension_context_start_extension_group(
// `app`.
if (ten_string_is_equal(&extension_group_info->loc.app_uri,
&self->engine->app->uri)) {
bool res = ten_addon_extension_group_create(
bool res = ten_addon_create_extension_group(
ten_env,
ten_string_get_raw_str(
&extension_group_info->extension_group_addon_name),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ static void ten_builtin_extension_group_on_create_extensions(
ten_string_t *extension_addon_name = &extension_name_info->addon_name;
ten_string_t *extension_instance_name = &extension_name_info->instance_name;

bool res = ten_addon_create_extension_async(
bool res = ten_addon_create_extension(
ten_env, ten_string_get_raw_str(extension_addon_name),
ten_string_get_raw_str(extension_instance_name),
(ten_env_addon_on_create_instance_async_cb_t)
Expand Down Expand Up @@ -156,8 +156,8 @@ static void ten_builtin_extension_group_on_destroy_extensions(
TEN_ASSERT(extension && ten_extension_check_integrity(extension, true),
"Invalid argument.");

ten_addon_destroy_extension_async(
ten_env, extension, on_addon_destroy_instance_done, NULL, NULL);
ten_addon_destroy_extension(ten_env, extension,
on_addon_destroy_instance_done, NULL, NULL);
}
}

Expand Down
30 changes: 17 additions & 13 deletions core/src/ten_runtime/protocol/asynced/protocol_asynced.c
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ void ten_protocol_asynced_on_connected_async(ten_protocol_asynced_t *self,
}

static void ten_app_thread_on_client_protocol_created(ten_env_t *ten_env,
void *instance,
ten_protocol_t *instance,
void *cb_data) {
TEN_ASSERT(ten_env && ten_env_check_integrity(ten_env, true),
"Should not happen.");
Expand All @@ -415,7 +415,7 @@ static void ten_app_thread_on_client_protocol_created(ten_env_t *ten_env,
ten_protocol_check_integrity(&listening_protocol->base, true),
"Invalid argument.");

ten_protocol_asynced_t *protocol = instance;
ten_protocol_asynced_t *protocol = (ten_protocol_asynced_t *)instance;
if (!protocol) {
TEN_LOGE("Failed to create the protocol instance.");
info->on_created(NULL, info);
Expand All @@ -427,13 +427,6 @@ static void ten_app_thread_on_client_protocol_created(ten_env_t *ten_env,
// object) might need to be migrated, so set the value to 'INIT' as the
// default value is 'DONE'. Refer to 'ten_protocol_asynced_init()'.
protocol->migration_state = TEN_CONNECTION_MIGRATION_STATE_INIT;

// We can _not_ know whether the protocol role is
// 'TEN_PROTOCOL_ROLE_IN_INTERNAL' or 'TEN_PROTOCOL_ROLE_IN_EXTERNAL' until
// the message received from the protocol is processed. Refer to
// 'ten_connection_on_msgs()' and
// 'ten_connection_handle_command_from_external_client()'.
protocol->base.role = TEN_PROTOCOL_ROLE_IN_DEFAULT;
protocol->base.on_accepted = listening_protocol->base.on_accepted;

ten_protocol_attach_to_app_and_thread(&protocol->base, app);
Expand Down Expand Up @@ -473,11 +466,22 @@ static void ten_protocol_asynced_on_client_accepted(void *self, void *info_) {
ten_app_t *app = listening_base_protocol->attached_target.app;
TEN_ASSERT(app && ten_app_check_integrity(app, true), "Should not happen.");

bool rc = ten_addon_create_instance_async(
ten_error_t err;
ten_error_init(&err);

// We can _not_ know whether the protocol role is
// 'TEN_PROTOCOL_ROLE_IN_INTERNAL' or 'TEN_PROTOCOL_ROLE_IN_EXTERNAL'
// until the message received from the protocol is processed. Refer to
// 'ten_connection_on_msgs()' and
// 'ten_connection_handle_command_from_external_client()'.
bool rc = ten_addon_create_protocol(
app->ten_env, ten_string_get_raw_str(&addon_host->name),
ten_string_get_raw_str(&addon_host->name), TEN_ADDON_TYPE_PROTOCOL,
ten_app_thread_on_client_protocol_created, info);
TEN_ASSERT(rc, "Should not happen.");
ten_string_get_raw_str(&addon_host->name), TEN_PROTOCOL_ROLE_IN_DEFAULT,
ten_app_thread_on_client_protocol_created, info, NULL);
TEN_ASSERT(rc, "Failed to create protocol, err: %s",
ten_error_errmsg(&err));

ten_error_deinit(&err);
}

// The task is completed, so delete a reference to the 'protocol' to reflect
Expand Down
Loading
Loading