From b488bafc3c048f1e69c201414bd766f4025b3d93 Mon Sep 17 00:00:00 2001 From: Rodgger Date: Wed, 4 Jan 2023 15:55:28 -0500 Subject: [PATCH] =?UTF-8?q?coment=C3=A1rios=20no=20c=C3=B3digo,=20mas=20n?= =?UTF-8?q?=C3=A3o=20padronizado=20Doxygen?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/agent/agent.c | 84 +++++++++++++++++++++++++------------------- src/agent/agent.h | 64 +++++++++++++++++++++++---------- src/agent/agent_db.c | 2 +- 3 files changed, 95 insertions(+), 55 deletions(-) diff --git a/src/agent/agent.c b/src/agent/agent.c index 957e79e..5e58cb5 100644 --- a/src/agent/agent.c +++ b/src/agent/agent.c @@ -1,34 +1,34 @@ /*** - * _____ _ _ _____ - * / ____(_) | | |_ _| - * | (___ _ _ __ ___ _ __ | | ___ ___ | | _ __ - * \___ \| | '_ ` _ \| '_ \| |/ _ \/ __| | | | '_ \ - * ____) | | | | | | | |_) | | __/\__ \ _| |_| |_) | - * |_____/|_|_| |_| |_| .__/|_|\___||___/ |_____| .__/ - * | \/ | | | | | - * | \ / | __ _ _ __ |_|_ _ __ _ ___ _ __ |_| - * | |\/| |/ _` | '_ \ / _` |/ _` |/ _ \ '__| - * | | | | (_| | | | | (_| | (_| | __/ | - * |_| |_|\__,_|_| |_|\__,_|\__, |\___|_| - * __/ | - * |___/ - * + * .----------------. .----------------. .----------------. .----------------. + * | .--------------. || .--------------. || .--------------. || .--------------. | + * | | __ | || | ____ ____ | || | _____ | || | ________ | | + * | | / \ | || ||_ \ / _|| || | |_ _| | || | |_ ___ `. | | + * | | / /\ \ | || | | \/ | | || | | | | || | | | `. \ | | + * | | / ____ \ | || | | |\ /| | | || | | | | || | | | | | | | + * | | _/ / \ \_ | || | _| |_\/_| |_ | || | _| |_ | || | _| |___.' / | | + * | ||____| |____|| || ||_____||_____|| || | |_____| | || | |________.' | | + * | | | || | | || | | || | | | + * | '--------------' || '--------------' || '--------------' || '--------------' | + * '----------------' '----------------' '----------------' '----------------' + * * Copyright (C) 2022 Simples IP. - * Author: Ródgger Bruno + * Maintainers: Rodgger Bruno * */ + #include #include -#include +#include // camada de persistência #include -#include -#include #include #include - +/* + * lida com os eventos agents + * +*/ int parse_event_agents( EVENT *event, s_manager *smanager ){ /* @@ -89,39 +89,41 @@ int parse_event_agents( EVENT *event, s_manager *smanager ){ status = ami_get_value( smanager->ami, event->args, "Status" ); if( !status ) { goto fail; } - { + { //banco de dados struct s_table_agent *table_agent = get_agents_db( Select_agents, matricula ); if (table_agent) { if( strcmp_n( table_agent->name, name ) || strcmp_n( table_agent->status, status )){ + /* atualizar linha de dados da tabela agent */ if(update_agents_db( Update_agents, name, status, table_agent->id ) == -1){ goto fail; } } + // liberar resultado mysql free_table_agent(table_agent); } else { + // inseri uma linha na tabela if(insert_agents_db( Insert_agents, name, matricula, status) == -1){ goto fail; } } } - { + { // camada de persistência struct s_agents *agent = get_agents(matricula); if(agent){ - agent->action_update = 1; + agent->action_update = 1; // esse agente existe no asterisk } - else{ + else{ // criar um novo agente na persistência que existe no asteriks agent = create_agents(); newstrncpy(&agent->matricula, matricula); agent->action_update = 1; insert_agents( agent ); } - } return 1; @@ -130,30 +132,40 @@ fail: return -1; } + + +/* + * lida com o eventos agentscomplete. + * esse evento é recebido quando terminou a lista de eventos dos agentes, por isso o nome + * Esse evento é usado para sincronizar agentes com o asterisk + */ int parse_event_agentscomplete(EVENT *event, s_manager *smanager){ if( strcasecmp_n( ami_get_value(smanager->ami, event->args, "Eventlist"), "Complete" ) ) { return -1; } + // Excluí agentes na camada de persistêcnia que não estão no asterisk agent_update(); - struct s_table_agent *table_agent = get_agents_update_db(Select_agents_update); - struct s_table_agent *root_table_agent = table_agent; - while(table_agent){ - struct s_agents *agent = get_agents(table_agent->matricula); - if(agent){ - if(agent->action_update == 0){ + { // Excluí agentes no banco de dados que não estão na camada de persistência + struct s_table_agent *table_agent = get_agents_all_db(Select_agents_update); + struct s_table_agent *root_table_agent = table_agent; + while(table_agent){ + struct s_agents *agent = get_agents(table_agent->matricula); + if(agent){ + if(agent->action_update == 0){ + delete_agents_db(Delete_agents, table_agent->id); + } + + agent->action_update = 0; + } + else{ delete_agents_db(Delete_agents, table_agent->id); } - agent->action_update = 0; - } - else{ - delete_agents_db(Delete_agents, table_agent->id); + table_agent = table_agent->next; } - - table_agent = table_agent->next; } free_table_agent(root_table_agent); diff --git a/src/agent/agent.h b/src/agent/agent.h index e27c440..f33fe6a 100644 --- a/src/agent/agent.h +++ b/src/agent/agent.h @@ -1,20 +1,18 @@ /*** - * _____ _ _ _____ - * / ____(_) | | |_ _| - * | (___ _ _ __ ___ _ __ | | ___ ___ | | _ __ - * \___ \| | '_ ` _ \| '_ \| |/ _ \/ __| | | | '_ \ - * ____) | | | | | | | |_) | | __/\__ \ _| |_| |_) | - * |_____/|_|_| |_| |_| .__/|_|\___||___/ |_____| .__/ - * | \/ | | | | | - * | \ / | __ _ _ __ |_|_ _ __ _ ___ _ __ |_| - * | |\/| |/ _` | '_ \ / _` |/ _` |/ _ \ '__| - * | | | | (_| | | | | (_| | (_| | __/ | - * |_| |_|\__,_|_| |_|\__,_|\__, |\___|_| - * __/ | - * |___/ - * + * .----------------. .----------------. .----------------. .----------------. + * | .--------------. || .--------------. || .--------------. || .--------------. | + * | | __ | || | ____ ____ | || | _____ | || | ________ | | + * | | / \ | || ||_ \ / _|| || | |_ _| | || | |_ ___ `. | | + * | | / /\ \ | || | | \/ | | || | | | | || | | | `. \ | | + * | | / ____ \ | || | | |\ /| | | || | | | | || | | | | | | | + * | | _/ / \ \_ | || | _| |_\/_| |_ | || | _| |_ | || | _| |___.' / | | + * | ||____| |____|| || ||_____||_____|| || | |_____| | || | |________.' | | + * | | | || | | || | | || | | | + * | '--------------' || '--------------' || '--------------' || '--------------' | + * '----------------' '----------------' '----------------' '----------------' + * * Copyright (C) 2022 Simples IP. - * Author: Ródgger Bruno + * Maintainers: Rodgger Bruno * */ @@ -22,27 +20,57 @@ #define AGENT_H 1 #include -#include +#include // biblioteca ami_c #include + +/* struct para guardar valores da tabela agent */ struct s_table_agent { - unsigned long long int id; + unsigned long long int id; // id mariadb char *name; char *matricula; char *status; struct s_table_agent *next; }; + +/* Event https://wiki.asterisk.org/wiki/display/AST/Asterisk+13+ManagerEvent_Agents */ int parse_event_agents( EVENT *event, s_manager *smanager ); + + +/* Event https://wiki.asterisk.org/wiki/display/AST/Asterisk+13+ManagerEvent_AgentComplete */ int parse_event_agentscomplete(EVENT *event, s_manager *smanager); -struct s_table_agent *get_agents_update_db(stmt_t type_stmt); + +/* Recebe todas as agentes */ +struct s_table_agent *get_agents_all_db(stmt_t type_stmt); + + +/* adcionar a próxima linha do resultado sql */ int add_table_agent(struct s_table_agent **A, struct s_table_agent *next); + + +/* liberar ponteiro do resultado */ int free_table_agent(struct s_table_agent *A); + + +/* buscar um agente de acordo com a matrícula */ struct s_table_agent *get_agents_db( stmt_t type_stmt, const char *matricula ); + + +/* atualizar um linha da tabela */ int update_agents_db( stmt_t type_stmt, const char *name, const char *status, unsigned long long int id ); + + +/* inserir uma linha da tabela */ int insert_agents_db( stmt_t type_stmt, const char *name, const char *matricula, const char *status ); + + +/* deletar uma linha databela agents */ int delete_agents_db(stmt_t type_stmt, unsigned long long int id); + + #endif + diff --git a/src/agent/agent_db.c b/src/agent/agent_db.c index b2412dd..52dc40a 100644 --- a/src/agent/agent_db.c +++ b/src/agent/agent_db.c @@ -54,7 +54,7 @@ int free_table_agent(struct s_table_agent *A){ } -struct s_table_agent *get_agents_update_db(stmt_t type_stmt){ +struct s_table_agent *get_agents_all_db(stmt_t type_stmt){ MYSQL_STMT *stmt = NULL; SET_STMT( stmt, type_stmt, 0 );