Skip to content

Commit

Permalink
feat(console): semantica interpretation to instruction
Browse files Browse the repository at this point in the history
  • Loading branch information
R. Nicolás Savinelli committed Apr 23, 2022
1 parent 822459f commit 7da9251
Show file tree
Hide file tree
Showing 12 changed files with 149 additions and 112 deletions.
8 changes: 6 additions & 2 deletions console/include/module/console.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

#include "lib.h"
#include "conexion.h"
#include "instruction.h"
#include "accion.h"

#define CONF_FILE "console"

Expand Down Expand Up @@ -63,7 +65,7 @@ int on_before_exit(void);
*
* @return ERROR o SUCCESS
*/
int on_run(char *instructions_file_name, int process_size);
int on_run(char *instructions_file_name, uint32_t process_size);

/**
* @brief Event handler de establecer conexion
Expand All @@ -90,4 +92,6 @@ char *on_client_read(char *line, bool *status);
* @param line
* @return int
*/
int on_send_instruction(void *conexion, char *line);
ssize_t on_send_instruction(void *conexion, instruction_t *inst);

ssize_t on_send_action(conexion_t is_conexion, actioncode_t actioncode, uint32_t param);
38 changes: 11 additions & 27 deletions console/src/console/semantic.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,63 +27,47 @@
#include <smartlist.h>

#include "semantic.h"
#include "instruction.h"

extern t_list *input_file_commands;

static void
_request_instruction(const char *instruccion, int cantidad_de_argumentos, ...)
_request_instruction(instcode_t instcode, int param0, int param1)
{
va_list ap;

size_t instruction_size = sizeof(char) * (strlen(instruccion) + 1);
char *request = (char *)malloc(instruction_size);
strcpy(request, instruccion);

va_start(ap, cantidad_de_argumentos);
for (int i = 0; i < cantidad_de_argumentos; i++)
{
char *param = va_arg(ap, char *);
size_t param_size = sizeof(char) * (strlen(param) + 2);
request = (char *)realloc(request, instruction_size + param_size);
strcat(request, " ");
strcat(request, param);
instruction_size += param_size;
}

list_smart_add(input_file_commands, request);

va_end(ap);
instruction_t *instruction = instruction_create(instcode, param0, param1);

list_smart_add(input_file_commands, instruction);
}

void request_exit(void)
{
_request_instruction("EXIT", 0);
_request_instruction(C_REQUEST_EXIT, NO_INSTRUCTION_PARAMETER, NO_INSTRUCTION_PARAMETER);
}

void request_no_op(char *constant)
{
_request_instruction("NO_OP", 1, constant);
_request_instruction(C_REQUEST_NO_OP, atoi(constant), NO_INSTRUCTION_PARAMETER);

free(constant);
}

void request_io(char *constant)
{
_request_instruction("I/O", 1, constant);
_request_instruction(C_REQUEST_IO, atoi(constant), NO_INSTRUCTION_PARAMETER);

free(constant);
}

void request_read(char *constant)
{
_request_instruction("READ", 1, constant);
_request_instruction(C_REQUEST_READ, atoi(constant), NO_INSTRUCTION_PARAMETER);

free(constant);
}

void request_write(char *constant_l, char *constant_r)
{
_request_instruction("WRITE", 2, constant_l, constant_r);
_request_instruction(C_REQUEST_WRITE, atoi(constant_l), atoi(constant_r));

free(constant_l);

Expand All @@ -92,7 +76,7 @@ void request_write(char *constant_l, char *constant_r)

void request_copy(char *constant_l, char *constant_r)
{
_request_instruction("COPY", 2, constant_l, constant_r);
_request_instruction(C_REQUEST_COPY, atoi(constant_l), atoi(constant_r));

free(constant_l);

Expand Down
36 changes: 29 additions & 7 deletions console/src/module/console.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
#include "cfg.h"
#include "lib.h"
#include "context.h"
#include "instruction.h"
#include "conexion.h"
#include "accion.h"
#include "network.h"
#include "smartlist.h"
#include "parser.h"
Expand Down Expand Up @@ -191,7 +194,7 @@ int on_before_exit(void)
// Event Handlers
// ------------------------------------------------------------

int on_run(char *instructions_file_name, int process_size)
int on_run(char *instructions_file_name, uint32_t process_size)
{
LOG_TRACE("Running a Syntax Analysis...");
unsigned int analysis_result = parse(instructions_file_name);
Expand All @@ -205,9 +208,13 @@ int on_run(char *instructions_file_name, int process_size)

while (this.status EQ RUNNING)
{
on_send_action(this.conexion, NEW_PROCESS, process_size);

while (input_file_commands->elements_count > 0)
{
on_send_instruction(&this.conexion, (char *)list_remove(input_file_commands, 0));
on_send_instruction(
&this.conexion,
list_remove(input_file_commands, 0));
}

this.status = not RUNNING;
Expand Down Expand Up @@ -258,17 +265,32 @@ char *on_client_read(char *line, bool *status)
return NULL;
}

int on_send_instruction(void *conexion, char *line)
ssize_t on_send_action(conexion_t is_conexion, actioncode_t actioncode, uint32_t param)
{
if (line)
accion_t *accion = accion_create(actioncode, param);

void *stream = accion_serializar(accion);

ssize_t bytes_sent = conexion_enviar_stream(is_conexion, SYS, stream, sizeof(accion_t));

free(stream);

accion_destroy(accion);

return bytes_sent;
}

ssize_t on_send_instruction(void *conexion, instruction_t *inst)
{
if (inst)
{
// Si envio algo sera mayor a 0
int result = conexion_enviar_mensaje(*(conexion_t *)conexion, line) > 0 ? SUCCESS : ERROR;
ssize_t result = instruction_send(*(conexion_t *)conexion, inst) > 0 ? SUCCESS : ERROR;

free(line);
free(inst);

return result;
}

return ERROR;
return (ssize_t)ERROR;
}
55 changes: 31 additions & 24 deletions lib/inc/connection/accion.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/**
* accion.h
*
* @file API de Acciones
* @author Tomás Sánchez
* @since 05.04.2021
*/
* accion.h
*
* @file API de Acciones
* @author Tomás Sánchez
* @since 05.04.2021
*/

#pragma once

Expand All @@ -16,14 +16,25 @@
// ***** Tipos (y estructuras) *****
// ============================================================================================================

/**
* @brief Action ID Code
*
*/
typedef enum ActionCode
{
NEW_PROCESS,
} actioncode_t;

#define NO_ACTION_PARAMETER 0

/**
* @brief Acciones que se pueden tomar sobre un Tripulante
*
*
*/
typedef struct Accion
{
// El código de operacion
opcode_t opcode;
actioncode_t actioncode;
// El parámetro de la operación
uint32_t param;
} accion_t;
Expand All @@ -36,18 +47,20 @@ typedef struct Accion
// Constructor y Destructor
// ------------------------------------------------------------

void *accion_serializar(const accion_t *accion);

/**
* @brief Instancia una nueva accion
*
* @param opcode el codigo de accion
*
* @param actioncode el codigo de accion
* @param param el parametro de la accion
* @return Una referencia a una accion
*/
accion_t *accion_create(opcode_t opcode, uint32_t param);
accion_t *accion_create(actioncode_t actioncode, uint32_t param);

/**
* @brief Destruye la intancia
*
*
* @param accion la accion a destruirse.
*/
void accion_destroy(accion_t *accion);
Expand All @@ -58,25 +71,19 @@ void accion_destroy(accion_t *accion);

/**
* @brief Envía una accion lista para recibirse.
*
*
* @param accion la accion a enviar
* @param socket el file descriptor al cual enviar
* @return Los bytes enviados o ERROR.
* @return Los bytes enviados o ERROR.
*/
ssize_t accion_enviar(accion_t *accion, int socket);

/**
* @brief Recibe una accion
*
* @param socket el file descriptor del cual recibir.
* @return el parametro de la accion.
*/
uint32_t accion_recibir(int socket);

/**
* @brief Espera una accion.
*
*
* @param socket el file descriptor del cual recibir.
* @return la accion recibida
*/
accion_t *accion_recibir_full(int socket);
accion_t *accion_recibir(int socket);

accion_t *accion_from_stream(void *stream);
5 changes: 4 additions & 1 deletion lib/inc/connection/opcode.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ typedef enum Opcode
MSG,
// Package - paquete
PKG,
// TODO: Agregar los opcodes que vayan surgiendo
// System Call - syscall
SYS,
// Command - instruccion
CMD,
} opcode_t;

// ============================================================================================================
Expand Down
19 changes: 13 additions & 6 deletions lib/inc/instruction/instruction.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,26 @@

#include <inttypes.h>
#include "opcode.h"
#include "conexion.h"

/**
* @brief Instruction ID Code
*
*/
typedef enum InstructionCode
{
EXIT,
READ,
WRITE,
COPY,
IO,
NO_OP
// Console OPERATIONS tha can be sent to the Kernel
C_REQUEST_EXIT,
C_REQUEST_READ,
C_REQUEST_WRITE,
C_REQUEST_NO_OP,
C_REQUEST_IO,
C_REQUEST_COPY,
NO_INSTRUCTION
} instcode_t;

#define NO_INSTRUCTION_PARAMETER 0

/**
* @brief Pseudo-code's instruction.
*
Expand Down Expand Up @@ -115,3 +120,5 @@ void *instruction_reduce(void *buffer, void *next);
* @return a list.
*/
void *instruction_list_from(void *stream);

ssize_t instruction_send(conexion_t is_conexion, instruction_t *is_instruction);
Loading

0 comments on commit 7da9251

Please sign in to comment.