Skip to content

Commit

Permalink
refactor(kernel): renamed to match
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasanchez committed Apr 29, 2022
1 parent 8be26fc commit 2c22d8b
Show file tree
Hide file tree
Showing 10 changed files with 83 additions and 90 deletions.
19 changes: 10 additions & 9 deletions kernel/inc/module/kernel.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,43 +13,44 @@
#include "cfg.h"
#include "log.h"


/**
* @brief Kernel module context.
* @brief Kernel module.
*
*/
typedef struct Context
typedef struct Kernel
{
// The Kernel Multi-thread Server dependency.
servidor_t server;
// Kernel-Memory Client dependency
// Kernel-Memory Client dependency.
conexion_t conexion_memory;
// TODO: Add Kernel-CPU Client dependency
// Kernel-CPU (Dispatch) connection dependency.
conexion_t conexion_dispatch;
// Kernel-CPU (Interrupt) connection dependency.
conexion_t conexion_interrupt;
// Thread Tracker dependency.
thread_manager_t tm;
} context_t;
} kernel_t;

/**
* @brief Initializes the Kernel Module with the required structures.
*
* @param context the server context structure.
* @return an status code.
*/
int on_init(context_t *context);
int on_init(kernel_t *context);

/**
* @brief Responsible of Kernel main processing action.
*
* @param context the server context structure.
* @return an status code.
*/
int on_run(context_t *context);
int on_run(kernel_t *context);

/**
* @brief Method called when Kernel does close. Destroys created structures.
*
* @param context the current processing context
* @param exit_code an status code
*/
void on_before_exit(context_t *context, int exit_code);
void on_before_exit(kernel_t *context, int exit_code);
22 changes: 10 additions & 12 deletions kernel/src/client/conexion_dispatch.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
// ***** Definiciones y Estructuras *****
// ============================================================================================================

extern context_t g_context;
extern kernel_t g_kernel;

// ============================================================================================================
// ***** Private Functions *****
Expand Down Expand Up @@ -38,17 +38,16 @@ int on_connect_dispatch(void *conexion, bool offline_mode)
return SUCCESS;
}


static int conexion_init(context_t *context)
static int conexion_init(kernel_t *kernel)
{
char *port = puerto_cpu_dispatch();
char *ip = ip_cpu();

LOG_DEBUG("Connecting <Cpu> at %s:%s", ip, port);
// Test connection with cpu
context->conexion_dispatch = conexion_cliente_create(ip, port);
kernel->conexion_dispatch = conexion_cliente_create(ip, port);

if (on_connect_dispatch(&context->conexion_dispatch, false) EQ SUCCESS)
if (on_connect_dispatch(&kernel->conexion_dispatch, false) EQ SUCCESS)
{
// Test connection with cpu
LOG_DEBUG("Connected as CLIENT at %s:%s", ip, port);
Expand All @@ -57,21 +56,20 @@ static int conexion_init(context_t *context)
return SUCCESS;
}


// ============================================================================================================
// ***** Public Functions *****
// ============================================================================================================


void *routine_conexion_dispatch(void *data)
void *routine_conexion_dispatch(void *data)
{
context_t *context = data;
kernel_t *kernel = data;

conexion_init(context);
conexion_init(kernel);

conexion_enviar_mensaje(context->conexion_dispatch, "Mando un msj");
conexion_enviar_mensaje(kernel->conexion_dispatch, "Mando un msj");

for(;;){
for (;;)
{
LOG_WARNING("Hola Thread DISPATCH");
sleep(TIEMPO_ESPERA);
}
Expand Down
22 changes: 10 additions & 12 deletions kernel/src/client/conexion_interrupt.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
// ***** Definiciones y Estructuras *****
// ============================================================================================================

extern context_t g_context;
extern kernel_t g_kernel;

// ============================================================================================================
// ***** Private Functions *****
Expand Down Expand Up @@ -38,17 +38,17 @@ int on_connect_interrupt(void *conexion, bool offline_mode)
return SUCCESS;
}

static int conexion_init(context_t *context)
static int conexion_init(kernel_t *kernel)
{
char *port = puerto_cpu_interrupt();
char *ip = ip_cpu();

LOG_DEBUG("Connecting <Cpu> at %s:%s", ip, port);

// Test connection with cpu
context->conexion_interrupt = conexion_cliente_create(ip, port);
kernel->conexion_interrupt = conexion_cliente_create(ip, port);

if (on_connect_interrupt(&context->conexion_interrupt, false) EQ SUCCESS)
if (on_connect_interrupt(&kernel->conexion_interrupt, false) EQ SUCCESS)
{

// Test connection with cpu
Expand All @@ -58,22 +58,20 @@ static int conexion_init(context_t *context)
return SUCCESS;
}


// ============================================================================================================
// ***** Public Functions *****
// ============================================================================================================



void *routine_conexion_interrupt(void *data)
void *routine_conexion_interrupt(void *data)
{
context_t *context = data;
kernel_t *kernel = data;

conexion_init(context);
conexion_init(kernel);

conexion_enviar_mensaje(context->conexion_interrupt, "Mando un msj");
conexion_enviar_mensaje(kernel->conexion_interrupt, "Mando un msj");

for(;;){
for (;;)
{
LOG_WARNING("Hola Thread INTERRUPT");
sleep(TIEMPO_ESPERA);
}
Expand Down
14 changes: 7 additions & 7 deletions kernel/src/client/conexion_memory.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
// ***** Definiciones y Estructuras *****
// ============================================================================================================

extern context_t g_context;
extern kernel_t g_kernel;

// ============================================================================================================
// ***** Private Functions *****
Expand Down Expand Up @@ -38,16 +38,16 @@ static int on_connect(void *conexion, bool offline_mode)
return SUCCESS;
}

static int conexion_init(context_t *context)
static int conexion_init(kernel_t *kernel)
{
char *port = puerto_memoria();
char *ip = ip_memoria();

LOG_DEBUG("[MEMORY-THREAD] - Connecting <Kernel> at %s:%s", ip, port);

context->conexion_memory = conexion_cliente_create(ip, port);
kernel->conexion_memory = conexion_cliente_create(ip, port);

if (on_connect(&context->conexion_memory, false) EQ SUCCESS)
if (on_connect(&kernel->conexion_memory, false) EQ SUCCESS)
{
LOG_DEBUG("Connected as CLIENT at %s:%s", ip, port);
}
Expand All @@ -62,11 +62,11 @@ static int conexion_init(context_t *context)
// Rutina (HILO) de Kernel como cliente.
void *routine_conexion_memoria(void *data)
{
context_t *context = data;
kernel_t *kernel = data;

conexion_init(context);
conexion_init(kernel);

conexion_enviar_mensaje(context->conexion_memory, "Send message");
conexion_enviar_mensaje(kernel->conexion_memory, "Send message");

for (;;)
{
Expand Down
8 changes: 4 additions & 4 deletions kernel/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@

#include "kernel.h"

context_t g_context;
kernel_t g_kernel;

int main(void)
{
int status_code = on_init(&g_context);
int status_code = on_init(&g_kernel);

if (status_code == EXIT_SUCCESS)
status_code = on_run(&g_context);
status_code = on_run(&g_kernel);

on_before_exit(&g_context, status_code);
on_before_exit(&g_kernel, status_code);
}
76 changes: 36 additions & 40 deletions kernel/src/module/kernel.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,43 +30,40 @@
// ***** Private Functions *****
// ============================================================================================================
/**
* @brief Initializes the Kernel Context
* @brief Initializes the Kernel
*
* @param context the kernel context
* @param kernel the module itself
* @return success or failure
*/
static int on_init_context(context_t *context);
static int on_init_context(kernel_t *kernel);

/**
* @brief Destroy the context elements
* @brief Destroy the kernel elements
*
* @param context the Kernel Context
* @param kernel the Kernel
*/
static void on_delete_context(context_t *context);
static void on_delete_context(kernel_t *kernel);

static int on_init_context(context_t *context)
static int on_init_context(kernel_t *kernel)
{
context->server = servidor_create(ip(), puerto_escucha());
context->tm = new_thread_manager();

kernel->server = servidor_create(ip(), puerto_escucha());
kernel->tm = new_thread_manager();
return EXIT_SUCCESS;
}

static void
on_delete_context(context_t *context)
on_delete_context(kernel_t *kernel)
{

servidor_destroy(&(context->server));

thread_manager_destroy(&(context->tm));

// Destroy CPU Connections
conexion_destroy(&(context->conexion_dispatch));
conexion_destroy(&(context->conexion_interrupt));


servidor_destroy(&(kernel->server));
thread_manager_destroy(&(kernel->tm));

// Destroy CPU Connections
conexion_destroy(&(kernel->conexion_dispatch));
conexion_destroy(&(kernel->conexion_interrupt));

// Destroy Memory Connection
conexion_destroy(&(context->conexion_memory));

conexion_destroy(&(kernel->conexion_memory));
}

int on_connect(void *conexion, bool offline_mode)
Expand Down Expand Up @@ -95,7 +92,7 @@ int on_connect(void *conexion, bool offline_mode)
// ***** Public Functions *****
// ============================================================================================================

int on_init(context_t *context)
int on_init(kernel_t *kernel)
{
if (log_init(MODULE_NAME, true) EQ ERROR)
return LOG_INITIALIZATION_ERROR;
Expand All @@ -111,9 +108,9 @@ int on_init(context_t *context)

LOG_DEBUG("Configurations loaded.");

if (on_init_context(context) EQ EXIT_SUCCESS)
if (on_init_context(kernel) EQ EXIT_SUCCESS)
{
LOG_DEBUG("Context initializated");
LOG_DEBUG("kernel initializated");
}

/* BO initialization routines */
Expand All @@ -129,19 +126,19 @@ int on_init(context_t *context)
return EXIT_SUCCESS;
}

int on_run(context_t *context)
int on_run(kernel_t *kernel)
{
// Dispatch Different threads for each connection
// CPU Connections:
thread_manager_launch(&context->tm, routine_conexion_dispatch, context);
thread_manager_launch(&context->tm, routine_conexion_interrupt, context);
// Memory Connection:
thread_manager_launch(&(context->tm), routine_conexion_memoria, context);
// Console Connection:
if (servidor_escuchar(&(context->server)) == -1)

// CPU Connections:
thread_manager_launch(&kernel->tm, routine_conexion_dispatch, kernel);
thread_manager_launch(&kernel->tm, routine_conexion_interrupt, kernel);

// Memory Connection:
thread_manager_launch(&(kernel->tm), routine_conexion_memoria, kernel);

// Console Connection:
if (servidor_escuchar(&(kernel->server)) == -1)
{
LOG_ERROR("Server could not listen.");
return SERVER_RUNTIME_ERROR;
Expand All @@ -150,16 +147,16 @@ int on_run(context_t *context)
LOG_DEBUG("[SERVER-THREAD] - Server listening. Awaiting for connections.");

for (;;)
servidor_run(&(context->server), routine);
servidor_run(&(kernel->server), routine);

return EXIT_SUCCESS;
}

void on_before_exit(context_t *context, int exit_code)
void on_before_exit(kernel_t *kernel, int exit_code)
{
LOG_WARNING("Closing Kernel...");

on_delete_context(context);
on_delete_context(kernel);

LOG_WARNING("Server has stopped.");

Expand All @@ -175,4 +172,3 @@ void on_before_exit(context_t *context, int exit_code)

exit(exit_code);
}

4 changes: 2 additions & 2 deletions kernel/src/module/signals.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@
#include "log.h"
#include "kernel.h"

extern context_t g_context;
extern kernel_t g_kernel;

static void
_sigint()
{
LOG_WARNING("Se capturó la señal SIGINT");
on_before_exit(&(g_context), SIGINT);
on_before_exit(&(g_kernel), SIGINT);
}

static void
Expand Down
Loading

0 comments on commit 2c22d8b

Please sign in to comment.