source: admin/Sources/Services/ogAdmRepo/sources/ogAdmRepo.cpp @ 9553071

918-git-images-111dconfigfileconfigure-oglivegit-imageslgromero-new-oglivemainmaint-cronmount-efivarfsmultivmmultivm-ogboot-installerogClonningEngineogboot-installer-jenkinsoglive-ipv6test-python-scriptsticket-301ticket-50ticket-50-oldticket-577ticket-585ticket-611ticket-612ticket-693ticket-700ubu24tplunification2use-local-agent-oglivevarios-instalacionwebconsole3
Last change on this file since 9553071 was fc480f2, checked in by ramon <ramongomez@…>, 13 years ago

#559: Liberar memoria reservada en todos los demonios.

git-svn-id: https://opengnsys.es/svn/branches/version1.0@3345 a21b9725-9963-47de-94b9-378ad31fedc9

  • Property mode set to 100644
File size: 6.6 KB
Line 
1// ********************************************************************************************************
2// Servicio: ogAdmRepo
3// Autor: José Manuel Alonso (E.T.S.I.I.) Universidad de Sevilla
4// Fecha Creación: Marzo-2010
5// Fecha Última modificación: Marzo-2010
6// Nombre del fichero: ogAdmRepo.cpp
7// Descripción :Este fichero implementa el servicio de administración general del sistema
8// ********************************************************************************************************
9#include "ogAdmRepo.h"
10#include "ogAdmLib.c"
11//________________________________________________________________________________________________________
12//      Función: tomaConfiguracion
13//
14//      Descripción:
15//              Lee el fichero de configuración del servicio
16//      Parámetros:
17//              filecfg : Ruta completa al fichero de configuración
18//      Devuelve:
19//              TRUE: Si el proceso es correcto
20//              FALSE: En caso de ocurrir algún error
21//________________________________________________________________________________________________________
22BOOLEAN tomaConfiguracion(char* filecfg) {
23        char modulo[] = "tomaConfiguracion()";
24
25        if (filecfg == NULL || strlen(filecfg) == 0) {
26                errorLog(modulo, 1, FALSE); // Fichero de configuración del servicio vacío
27                return (FALSE);
28        }
29        FILE *fcfg;
30        long lSize;
31        char * buffer, *lineas[MAXPRM], *dualparametro[2];
32        int i, numlin, resul;
33
34        fcfg = fopen(filecfg, "rt");
35        if (fcfg == NULL) {
36                errorLog(modulo, 2, FALSE); // No existe fichero de configuración del servicio
37                return (FALSE);
38        }
39
40        fseek(fcfg, 0, SEEK_END);
41        lSize = ftell(fcfg); // Obtiene tamaño del fichero.
42        rewind(fcfg);
43        buffer = (char*) reservaMemoria(lSize+1); // Toma memoria para el buffer de lectura.
44        if (buffer == NULL) { // No hay memoria suficiente para el buffer
45                errorLog(modulo, 3, FALSE);
46                return (FALSE);
47        }
48        fread(buffer, 1, lSize, fcfg); // Lee contenido del fichero
49        buffer[lSize]=(char) NULL;
50        fclose(fcfg);
51
52        iplocal[0] = (char) NULL; //inicializar variables globales
53        puerto[0] = (char) NULL;
54
55        numlin = splitCadena(lineas, buffer, '\n');
56        liberaMemoria(buffer);
57        for (i = 0; i < numlin; i++) {
58                splitCadena(dualparametro, lineas[i], '=');
59                resul = strcmp(StrToUpper(dualparametro[0]), "IPLOCAL");
60                if (resul == 0)
61                        strcpy(iplocal, dualparametro[1]);
62                resul = strcmp(StrToUpper(dualparametro[0]), "PUERTO");
63                if (resul == 0)
64                        strcpy(puerto, dualparametro[1]);
65        }
66        if (iplocal[0] == (char) NULL) {
67                errorLog(modulo, 4, FALSE); // Falta parámetro IPLOCAL
68                return (FALSE);
69        }
70        if (puerto[0] == (char) NULL) {
71                errorLog(modulo, 5, FALSE); // Falta parámetro PUERTO
72                return (FALSE);
73        }
74        return (TRUE);
75}
76// ________________________________________________________________________________________________________
77// Función: gestionaTrama
78//
79//              Descripción:
80//                      Procesa las tramas recibidas .
81//              Parametros:
82//                      - s : Socket usado para comunicaciones
83//      Devuelve:
84//              TRUE: Si el proceso es correcto
85//              FALSE: En caso de ocurrir algún error
86// ________________________________________________________________________________________________________
87BOOLEAN gestionaTrama(SOCKET *socket_c)
88{
89        TRAMA* ptrTrama;
90        int i, res;
91        char *nfn;
92        char modulo[] = "gestionaTrama()";
93
94        ptrTrama=recibeTrama(socket_c);
95        if (ptrTrama){
96                INTROaFINCAD(ptrTrama);
97                nfn = copiaParametro("nfn",ptrTrama); // Toma dirección/es IP
98                for (i = 0; i < MAXIMAS_FUNCIONES; i++) { // Recorre funciones que procesan las tramas
99                        res = strcmp(tbfuncionesRepo[i].nf, nfn);
100                        if (res == 0) { // Encontrada la función que procesa el mensaje
101                                return (tbfuncionesRepo[i].fptr(socket_c, ptrTrama)); // Invoca la función
102                        }
103                }
104        }
105        else
106                errorLog(modulo, 17, FALSE); // Error en la recepción
107        return (TRUE);
108}
109// ********************************************************************************************************
110// PROGRAMA PRINCIPAL (SERVICIO)
111// ********************************************************************************************************
112int main(int argc, char *argv[])
113{
114        SOCKET socket_r; // Socket donde escucha el servidor
115        SOCKET socket_c; // Socket de los clientes que se conectan
116        socklen_t iAddrSize;
117        struct sockaddr_in local, cliente;
118        char modulo[] = "main()";
119
120        /*--------------------------------------------------------------------------------------------------------
121                Validación de parámetros de ejecución y lectura del fichero de configuración del servicio
122         ---------------------------------------------------------------------------------------------------------*/
123        if (!validacionParametros(argc, argv,1)) // Valida parámetros de ejecución
124                exit(EXIT_FAILURE);
125
126        if (!tomaConfiguracion(szPathFileCfg)) { // Toma parametros de configuracion
127                exit(EXIT_FAILURE);
128        }
129        /*--------------------------------------------------------------------------------------------------------
130                Carga del catálogo de funciones que procesan las tramas (referencia directa por puntero a función)
131         ---------------------------------------------------------------------------------------------------------*/
132        int cf = 0;
133
134        cf++;
135
136
137        /*--------------------------------------------------------------------------------------------------------
138                Creación y configuración del socket del servicio
139         ---------------------------------------------------------------------------------------------------------*/
140        socket_r = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); // Crea socket del servicio
141        if (socket_r == SOCKET_ERROR) { // Error al crear el socket del servicio
142                errorLog(modulo, 13, TRUE);
143                exit(EXIT_FAILURE);
144        }
145
146        local.sin_addr.s_addr = htonl(INADDR_ANY); // Configura el socket del servicio
147        local.sin_family = AF_INET;
148        local.sin_port = htons(atoi(puerto));
149
150        if (bind(socket_r, (struct sockaddr *) &local, sizeof(local))== SOCKET_ERROR) { // Enlaza socket
151                errorLog(modulo, 14, TRUE);
152                exit(EXIT_FAILURE);
153        }
154
155        listen(socket_r, 250); // Pone a escuchar al socket
156        iAddrSize = sizeof(cliente);
157        /*--------------------------------------------------------------------------------------------------------
158                Bucle para acceptar conexiones
159         ---------------------------------------------------------------------------------------------------------*/
160        infoLog(1); // Inicio de sesión
161        while(TRUE) {
162                socket_c = accept(socket_r, (struct sockaddr *) &cliente, &iAddrSize);
163                if (socket_c == INVALID_SOCKET) {
164                        errorLog(modulo, 15, TRUE);
165                        exit(EXIT_FAILURE);
166                }
167                if(!gestionaTrama(&socket_c)){
168                        errorLog(modulo, 39, TRUE);
169                        break;
170                }
171                close(socket_c);
172        }
173        /*--------------------------------------------------------------------------------------------------------
174                Fin del servicio
175         ---------------------------------------------------------------------------------------------------------*/
176        close(socket_r);
177        exit(EXIT_SUCCESS);
178}
Note: See TracBrowser for help on using the repository browser.