Skip to content

Commit

Permalink
Merge pull request #3 from tomasanchez/CFG-4-kernel-config
Browse files Browse the repository at this point in the history
feat: kernel config
  • Loading branch information
tomasanchez authored Apr 13, 2022
2 parents a696dd4 + ca53bd0 commit 4892485
Show file tree
Hide file tree
Showing 17 changed files with 632 additions and 10 deletions.
3 changes: 2 additions & 1 deletion config/kernel.cfg
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
IP=127.0.0.1
PUERTO_ESCUCHA=8000
IP_MEMORIA=127.0.0.1
PUERTO_MEMORIA=8002
IP_CPU=127.0.0.1
PUERTO_CPU_DISPATCH=8001
PUERTO_CPU_INTERRUPT=8005
PUERTO_ESCUCHA=8000
ALGORITMO_PLANIFICACION=FIFO
ESTIMACION_INICIAL=10000
ALFA=0.5
Expand Down
109 changes: 109 additions & 0 deletions kernel/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
# C Makefile using gcc, gdb and valgrind.
# Modified version of Makefile using g++ & gdb by Roberto Nicolas Savinelli <rsavinelli@est.frba.utn.edu.ar>
# Tomas Agustin Sanchez <tosanchez@est.frba.utn.edu.ar>

# ? Most defined will not required further modifycation -----------------------------------------------------------------------------------

# C Compiler
CC = gcc
# Compiler Flags
CFLAGS = -Wall -Wextra -g3
# Test Compiler flags
TCFLAGS = -Wall -Wextra -Wshadow -Wno-unused-variable -Wno-unused-function -Wno-unused-result -Wno-unused-variable -Wno-pragmas -O3 -g3
# Used libraries
# * Add libraries when needed
LIBS = -lcommons -pthread
#Log Folder
LOG_FOLDER=../log
# Build Folder
BUILD_FOLDER=../build
# Include directory
INCLUDE_DIRECTORY=./inc/ ../lib/inc/
# Source directory
SOURCE_DIRECTORY=./src
# Test Directory
TEST_DIRECTORY=./test
# The main file path
MAIN_FILE= ./src/main.c
# The shared library source directory
LIB_DIRECTORY= ../lib/src
# Inlcude folder
# ? Loops [includeDirectory].forEach(includeDirectory => concat("-I ", "includeDirectory"))
INCLUDES = $(foreach dir, $(shell find $(INCLUDE_DIRECTORY) -type d -print), $(addprefix -I , $(dir)))
# The shared library source files
LIB_SOURCES = ../shared/*.o
# Source files
# ? Obtains all source files except MAIN
SOURCES = $(filter-out $(MAIN_FILE), $(shell find $(SOURCE_DIRECTORY) -name '*.c')) $(LIB_SOURCES)
# Test cases files
# ? Obtains all test source files
TESTS = $(shell find $(TEST_DIRECTORY) -name '*.c')
# Module name
# ! auto-generated module name (name of directory)
# ? eg. /user/home/dev/so-starter/memory will be memory
MODULE_NAME = $(notdir $(shell pwd))
# Output file name
# ? eg. memory.out
OUTPUT = $(BUILD_FOLDER)/$(MODULE_NAME).out
# Test Output file
# ? eg. memory_test.out
TEST_OUTPUT = $(BUILD_FOLDER)/$(MODULE_NAME).test.out
# Leaks log file
LEAKS = $(LOG_FOLDER)/$(MODULE_NAME)_leaks.log
# Thread chek log file
HELGRIND = $(LOG_FOLDER)/$(MODULE_NAME)_threads.log


all : compile run

.PHONY: all

# ! Avoid modifying this section - (Unless you know what you are doing) --------------------------------------------------------------

# Compilation rule
# ? Generates .out executable
compile:
@mkdir -p $(BUILD_FOLDER)
@echo Building module: [$(MODULE_NAME)]...
$(CC) $(CFLAGS) $(MAIN_FILE) $(SOURCES) $(INCLUDES) $(LIBS) -o $(OUTPUT)
@echo Build completed.

# Compiles & Runs executable
run: compile
./$(OUTPUT)

# Test compilation
# ? Generates *_test.out executable
test-build:
@mkdir -p $(BUILD_FOLDER)
@echo Preparing tests...
$(CC) $(TCFLAGS) $(TESTS) $(SOURCES) $(INCLUDES) $(LIBS) -o $(TEST_OUTPUT)
@echo Test builds!

# Runs tests cases
test: test-build
@echo Running test cases...
./$(TEST_OUTPUT)
@echo Tests completed.

# ! Uses Valgrind MemCheck tool
leaks: compile
@mkdir -p $(LOG_FOLDER)
valgrind --leak-check=yes --log-file="$(LOG_FOLDER)/$(LEAKS)" --track-origins=yes ./$(OUTPUT)

# ! Uses Valgrind Helgrind tool
threads: compile
@mkdir -p $(LOG_FOLDER)
valgrind --tool=helgrind --log-file="$(LOG_FOLDER)/$(HELGRIND)" ./$(OUTPUT)

# Removes Output files
clean:
$(RM) ./$(OUTPUT)
$(RM) ./$(TEST_OUTPUT)

# Removes log files
cleanLogs:
$(RM) -r $(LOG_FOLDER) || true

# Removes both logs and executables
remove: clean cleanLogs
11 changes: 11 additions & 0 deletions kernel/inc/dispatcher.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#pragma once

#include <pthread.h>
#include <sys/syscall.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdlib.h>

#include "thread_manager.h"

void *dispatch_imprimir_mensaje(void *args);
14 changes: 14 additions & 0 deletions kernel/inc/main.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* main.h
*
* @file Core definitions for the server module
* @author Tomás A. Sánchez and R. Nicolás Savinelli
* @since 03.17.2022
*/

#pragma once

#define MODULE_NAME "kernel"

#define LOG_FILE "kernel.log"
#define CONF_FILE "kernel.cfg"
3 changes: 3 additions & 0 deletions kernel/inc/routines.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#pragma once

void *routine(void *fd);
25 changes: 25 additions & 0 deletions kernel/inc/runtime.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* runtime.h
*
* @file runtime interface for the server module
* @author Tomás A. Sánchez and R. Nicolás Savinelli
* @since 03.17.2022
*/

#pragma once

#include "server.h"
#include "thread_manager.h"
#include "cfg.h"
#include "log.h"

typedef struct context
{
servidor_t server;
} context_t;

int init(context_t *context);

int run(context_t *context);

void stop(context_t *context, int exit_code);
3 changes: 3 additions & 0 deletions kernel/inc/signals.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#pragma once

void signals_init();
110 changes: 110 additions & 0 deletions kernel/src/dispatcher.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
#include <commons/string.h>
#include <commons/collections/list.h>

#include "dispatcher.h"
#include "thread_manager.h"
#include "server.h"
#include "smartlist.h"
#include "accion.h"
#include "log.h"
#include "lib.h"

// ============================================================================================================
// ***** Dispatcher - Definiciones *****
// ============================================================================================================

// Los PIDs
bool pids[PIDS];

// ============================================================================================================
// ***** Funciones Privadas - Declaraciones *****
// ============================================================================================================

// ------------------------------------------------------------
// Getters
// ------------------------------------------------------------

/**
* @brief Get the primer pid libre
*
* @return el ID libre o UNDEFINED
*/
static uint32_t get_pid_libre(void);

/**
* @brief Lee un un entero de 32 bits de una porción de memoria
*
* @param stream la porción de memoria de la cual leer
* @param offset el desplazamiento dentro de esa memoria
* @return el valor leido
*/
static uint32_t _get_uint32(void *stream, size_t *offset);

/**
* @brief Lee un entero de tipo INT de una porción de memoria
*
* @param args la porción de memoria de la cual leer
* @param offset el desplazamiento dentro de esa memoria
* @return el valor leido
*/
static int _get_cliente(void *args, size_t *offset);

// ============================================================================================================
// ***** Funciones Privadas - Definiciones *****
// ============================================================================================================

void *dispatch_imprimir_mensaje(void *args)
{
char *msg = ((char *)args);

THREAD_SAFE(LOG_INFO("Mensaje: %s", msg));

free(msg);

return NULL;
}

// ------------------------------------------------------------
// Getters
// ------------------------------------------------------------

static uint32_t get_pid_libre(void)
{
for (uint32_t i = 0; i < PIDS; i++)
{
if (!pids[i])
{
pids[i] = true;
return i;
}
}

return UNDEFINED;
}

static int _get_cliente(void *args, size_t *offset)
{
int cliente = 0;

memcpy((void *)&cliente, args + *offset, sizeof(int));
*offset += sizeof(int);

return cliente;
}

static uint32_t _get_uint32(void *stream, size_t *offset)
{
uint32_t value = 0;

memcpy((void *)&value, stream + *offset, sizeof(uint32_t));
*offset += sizeof(uint32_t);

return value;
}

// ----------------------
// Wrappers
// ----------------------

// Obtiene la PATOTA ID
static uint32_t (*_get_pid)(void *, size_t *) = _get_uint32;
21 changes: 21 additions & 0 deletions kernel/src/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* main.c
*
* @file Módulo
* @author Tomás A. Sánchez and R. Nicolás Savinelli
* @since 03.17.2022
*/

#include "runtime.h"

context_t g_context;

int main(void)
{
int exit_code = init(&g_context);

if (exit_code == EXIT_SUCCESS)
exit_code = run(&g_context);

stop(&g_context, exit_code);
}
83 changes: 83 additions & 0 deletions kernel/src/routines.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#include "runtime.h"
#include "server.h"
#include "dispatcher.h"
#include "log.h"
#include "cfg.h"

// ============================================================================================================
// ***** Definiciones y Estructuras *****
// ============================================================================================================

// ============================================================================================================
// ***** Funciones Privadas *****
// ============================================================================================================

/**
* @brief Realiza los tratamientos del mensaje recibido.
*
* @param cliente la posicion del cliente en la lista.
* @return El mensaje leído.
*/
static char *
recibir_mensaje(int cliente)
{
// Bytes recibidos
ssize_t size = ERROR;
// El mensaje (MSG) recibido
return servidor_recibir_mensaje(cliente, &size);
}

// ============================================================================================================
// ***** Funciones Publicas *****
// ============================================================================================================
// TAKES SENDER FD AS INPUT
void *routine(void *fd)
{
int sender_fd = 0;

memcpy((void *)&sender_fd, fd, sizeof(int));
free(fd);

for (;;)
{
int opcode = servidor_recibir_operacion(sender_fd);

if (opcode <= 0)
{
if (opcode == DC)
{
// Connection closed
LOG_WARNING("El cliente en el socket %d terminó la conexión.", sender_fd);
}
else
{
LOG_ERROR("Error detectado al utilizar recv() con el cliente %d.", sender_fd);
LOG_DEBUG("Terminando conexión con el cliente %d.", sender_fd);
}

servidor_desconectar_cliente(sender_fd); // Bye!

thread_manager_terminar_thread();

return NULL;
}
else
{
// We got some good server from a client
LOG_TRACE("Recibí el código de operación <%s> del cliente %d.", opcode_to_string(opcode), sender_fd);

switch (opcode)
{
case MSG:
dispatch_imprimir_mensaje((void *)recibir_mensaje(sender_fd));
break;

default:
LOG_ERROR("Código de operación recibido del cliente %d es inválido: %d.", sender_fd, opcode);
break;
}
}
}

return NULL;
}
Loading

0 comments on commit 4892485

Please sign in to comment.