add nf7core_init

This commit is contained in:
falsycat 2024-04-10 23:28:45 +09:00
parent 4af1bf878a
commit 5bef1e1f46
5 changed files with 226 additions and 0 deletions

View File

@ -9,6 +9,7 @@ set(MODS
sdl2
# ---- usecase layer
init
test
)

12
core/init/CMakeLists.txt Normal file
View File

@ -0,0 +1,12 @@
add_library(nf7core_init)
target_sources(nf7core_init
PRIVATE
mod.c
factory.priv.h
)
target_link_libraries(nf7core_init
PRIVATE
nf7if
nf7util
)

95
core/init/factory.priv.h Normal file
View File

@ -0,0 +1,95 @@
#pragma once
#include <assert.h>
#include <uv.h>
#include "nf7.h"
#include "core/exec/entity.h"
#include "core/exec/mod.h"
struct nf7core_init_factory {
struct nf7* nf7;
struct nf7util_malloc* malloc;
uv_loop_t* uv;
uv_idle_t idle;
const uint8_t* entity_name;
size_t entity_namelen;
void* data;
void (*on_created)(struct nf7core_init_factory*, struct nf7core_exec_entity*);
// after the callback is called
};
static struct nf7core_init_factory* factory_new_(struct nf7*, const uint8_t*, size_t);
static void factory_del_(struct nf7core_init_factory*);
static void factory_on_idle_(uv_idle_t*);
static void factory_on_close_(uv_handle_t*);
static inline struct nf7core_init_factory* factory_new_(
struct nf7* nf7, const uint8_t* entity_name, size_t entity_namelen) {
struct nf7core_init_factory* this = nf7util_malloc_alloc(nf7->malloc, sizeof(*this));
if (nullptr == this) {
nf7util_log_error("failed to allocate the first factory");
return nullptr;
}
*this = (struct nf7core_init_factory) {
.nf7 = nf7,
.malloc = nf7->malloc,
.uv = nf7->uv,
.entity_name = entity_name,
.entity_namelen = entity_namelen,
};
uv_idle_init(this->uv, &this->idle);
this->idle.data = this;
uv_idle_start(&this->idle, factory_on_idle_);
return this;
}
static inline void factory_del_(struct nf7core_init_factory* this) {
if (nullptr == this) {
return;
}
if (nullptr != this->idle.data) {
uv_idle_stop(&this->idle);
uv_close((uv_handle_t*) &this->idle, factory_on_close_);
return;
}
nf7util_malloc_free(this->malloc, this);
}
static inline void factory_on_idle_(uv_idle_t* idle) {
assert(nullptr != idle);
struct nf7core_init_factory* this = idle->data;
struct nf7core_exec* exec = (void*) nf7_get_mod_by_meta(this->nf7, &nf7core_exec);
if (nullptr == exec) {
nf7util_log_error("nf7core_exec module is not installed");
goto EXIT;
}
struct nf7core_exec_entity* entity = nf7core_exec_entity_new(exec, this->entity_name, this->entity_namelen);
if (nullptr == entity) {
nf7util_log_error("failed to create new entity");
goto EXIT;
}
assert(nullptr != this->on_created);
this->on_created(this, entity);
EXIT:
uv_idle_stop(&this->idle);
}
static inline void factory_on_close_(uv_handle_t* handle) {
assert(nullptr != handle);
struct nf7core_init_factory* this = handle->data;
handle->data = nullptr;
factory_del_(this);
}

96
core/init/mod.c Normal file
View File

@ -0,0 +1,96 @@
// No copyright
#include "core/init/mod.h"
#include <assert.h>
#include <stdint.h>
#include <uv.h>
#include "nf7.h"
#include "util/log.h"
#include "core/exec/mod.h"
#include "core/init/factory.priv.h"
#define IDEA_NAME "luajit"
static void del_(struct nf7_mod*);
static void start_(struct nf7core_init_factory*, struct nf7core_exec_entity*);
struct nf7_mod* nf7core_init_new(struct nf7* nf7) {
assert(nullptr != nf7);
struct nf7core_init* this = nf7util_malloc_alloc(nf7->malloc, sizeof(*this));
if (nullptr == this) {
nf7util_log_error("failed to allocate module context");
goto ABORT;
}
*this = (struct nf7core_init) {
.super = {
.meta = &nf7core_init,
.nf7 = nf7,
},
.malloc = nf7->malloc,
.uv = nf7->uv,
};
this->factory = factory_new_(nf7, (const uint8_t*) IDEA_NAME, sizeof(IDEA_NAME)-1);
if (nullptr == this->factory) {
nf7util_log_error("failed to start the first factory");
goto ABORT;
}
this->factory->data = this;
this->factory->on_created = start_;
return &this->super;
ABORT:
del_(&this->super);
return nullptr;
}
static void del_(struct nf7_mod* mod) {
struct nf7core_init* this = (void*) mod;
if (nullptr == this) {
return;
}
nf7util_log_info("delete factory");
factory_del_(this->factory);
this->factory = nullptr;
nf7core_exec_entity_del(this->entity);
nf7util_malloc_free(this->malloc, this);
}
static void start_(struct nf7core_init_factory* factory, struct nf7core_exec_entity* entity) {
assert(nullptr != factory);
struct nf7core_init* this = factory->data;
assert(nullptr != this);
this->entity = entity;
if (nullptr == this->entity) {
nf7util_log_warn("failed to create new entity");
goto EXIT;
}
struct nf7util_buffer* buf = nf7util_buffer_new(this->malloc, 0);
if (nullptr == buf) {
nf7util_log_error("failed to allocate an empty buffer to send as the first trigger");
goto EXIT;
}
nf7core_exec_entity_send(this->entity, buf);
EXIT:
this->factory = nullptr;
factory_del_(factory);
}
const struct nf7_mod_meta nf7core_init = {
.name = (const uint8_t*) "nf7core_init",
.desc = (const uint8_t*) "creates the first entity",
.ver = NF7_VERSION,
.del = del_,
};

22
core/init/mod.h Normal file
View File

@ -0,0 +1,22 @@
// No copyright
#pragma once
#include <uv.h>
#include "nf7.h"
#include "core/exec/entity.h"
struct nf7core_init_factory;
struct nf7core_init {
struct nf7_mod super;
struct nf7util_malloc* malloc;
uv_loop_t* uv;
struct nf7core_init_factory* factory;
struct nf7core_exec_entity* entity;
};
extern const struct nf7_mod_meta nf7core_init;