Skip to content

Commit

Permalink
refactor: check duplicated addon name in store (#50)
Browse files Browse the repository at this point in the history
  • Loading branch information
halajohn authored Sep 26, 2024
1 parent ad6cebe commit 8c8ce3f
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 22 deletions.
2 changes: 1 addition & 1 deletion core/include_internal/ten_runtime/addon/common/store.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ typedef struct ten_addon_store_t {

TEN_RUNTIME_PRIVATE_API void ten_addon_store_init(ten_addon_store_t *store);

TEN_RUNTIME_PRIVATE_API void ten_addon_store_add(ten_addon_store_t *store,
TEN_RUNTIME_PRIVATE_API bool ten_addon_store_add(ten_addon_store_t *store,
ten_addon_host_t *addon_host);

TEN_RUNTIME_PRIVATE_API ten_addon_t *ten_addon_store_del(
Expand Down
5 changes: 2 additions & 3 deletions core/src/ten_runtime/addon/addon.c
Original file line number Diff line number Diff line change
Expand Up @@ -166,9 +166,8 @@ static void ten_addon_load_metadata(ten_addon_host_t *self, ten_env_t *ten_env,
*
* register -> on_init --> on_init_done --> add to the addon store
*
* The default behavior in the 'on_init' stage is to loading the metadata of the
* 'addon'. However, the developer could override the 'on_init' function to
* perform user-defined operations the addon needs.
* Developers could override the 'on_init' function to perform user-defined
* operations the addon needs.
*/
void ten_addon_register(ten_addon_store_t *addon_store,
ten_addon_host_t *addon_host, const char *name,
Expand Down
43 changes: 31 additions & 12 deletions core/src/ten_runtime/addon/common/store.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,46 @@ static void ten_addon_remove_from_store(ten_addon_host_t *addon) {
ten_ref_dec_ref(&addon->ref);
}

void ten_addon_store_add(ten_addon_store_t *store, ten_addon_host_t *addon) {
static ten_addon_host_t *ten_addon_store_find_internal(ten_addon_store_t *store,
const char *name) {
TEN_ASSERT(store, "Invalid argument.");
TEN_ASSERT(name, "Invalid argument.");

ten_addon_host_t *result = NULL;

ten_list_foreach (&store->store, iter) {
ten_addon_host_t *addon = ten_ptr_listnode_get(iter.node);
TEN_ASSERT(addon, "Should not happen.");

if (ten_string_is_equal_c_str(&addon->name, name)) {
result = addon;
break;
}
}

return result;
}

bool ten_addon_store_add(ten_addon_store_t *store, ten_addon_host_t *addon) {
TEN_ASSERT(store, "Invalid argument.");
TEN_ASSERT(addon, "Invalid argument.");

ten_mutex_lock(store->lock);

// Check if there is an addon with the same name in the addon store. If there
// is, it is considered an error.
if (ten_addon_store_find_internal(store,
ten_string_get_raw_str(&addon->name))) {
return false;
}

ten_list_push_ptr_back(
&store->store, addon,
(ten_ptr_listnode_destroy_func_t)ten_addon_remove_from_store);

ten_mutex_unlock(store->lock);

return true;
}

ten_addon_t *ten_addon_store_del(ten_addon_store_t *store, const char *name) {
Expand Down Expand Up @@ -80,17 +109,7 @@ ten_addon_host_t *ten_addon_store_find(ten_addon_store_t *store,
ten_addon_host_t *result = NULL;

ten_mutex_lock(store->lock);

ten_list_foreach (&store->store, iter) {
ten_addon_host_t *addon = ten_ptr_listnode_get(iter.node);
TEN_ASSERT(addon, "Should not happen.");

if (ten_string_is_equal_c_str(&addon->name, name)) {
result = addon;
break;
}
}

result = ten_addon_store_find_internal(store, name);
ten_mutex_unlock(store->lock);

return result;
Expand Down
16 changes: 10 additions & 6 deletions core/src/ten_runtime/addon/ten_env/on_xxx.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
//
#include "include_internal/ten_runtime/addon/ten_env/on_xxx.h"

#include <stdlib.h>

#include "include_internal/ten_runtime/addon/addon.h"
#include "include_internal/ten_runtime/addon/common/store.h"
#include "include_internal/ten_runtime/common/constant_str.h"
Expand Down Expand Up @@ -71,14 +73,12 @@ void ten_addon_on_init_done(ten_env_t *self) {
"the manifest.",
ten_string_get_raw_str(&addon_host->name), manifest_name);

// TODO(Wei): Enable the following checking. Get 'name' from manifest, and
// check the consistency between the name specified in the argument, and
// the name specified in the manifest.
// Get 'name' from manifest, and check the consistency between the name
// specified in the argument, and the name specified in the manifest.
//
// The name in the manifest could be checked by the TEN store to ensure
// the uniqueness of the name.
//
// TEN_ASSERT(0 && "Should not happen.")
TEN_ASSERT(0, "Should not happen.");
}

// If an extension defines an extension name in its manifest file, TEN
Expand All @@ -90,7 +90,11 @@ void ten_addon_on_init_done(ten_env_t *self) {
}
}

ten_addon_store_add(addon_host->store, addon_host);
rc = ten_addon_store_add(addon_host->store, addon_host);
if (!rc) {
TEN_ASSERT(0, "Should not happen.");
exit(EXIT_FAILURE);
}
}

void ten_addon_on_deinit_done(ten_env_t *self) {
Expand Down

0 comments on commit 8c8ce3f

Please sign in to comment.