[5ea3638] | 1 | // *************************************************************************
|
---|
[9182fd9] | 2 | // Aplicación: OPENGNSYS
|
---|
| 3 | // Autor: José Manuel Alonso (E.T.S.I.I.) Universidad de Sevilla
|
---|
| 4 | // Fecha Creación: Año 2003-2004
|
---|
| 5 | // Fecha Última modificación: Marzo-2006
|
---|
| 6 | // Nombre del fichero: ogAdmAgent.cpp
|
---|
| 7 | // Descripción :
|
---|
[5ea3638] | 8 | //
|
---|
| 9 | // ****************************************************************************
|
---|
| 10 | #include "ogAdmAgent.h"
|
---|
| 11 | #include "ogAdmLib.c"
|
---|
| 12 |
|
---|
[9182fd9] | 13 | //________________________________________________________________________________________________________
|
---|
[5ea3638] | 14 | //
|
---|
[9182fd9] | 15 | // Función: TomaConfiguracion
|
---|
[5ea3638] | 16 | //
|
---|
[9182fd9] | 17 | // Descripción:
|
---|
| 18 | // Esta función lee el fichero de configuración del programa hidralinuxcli y toma los parámetros
|
---|
| 19 | // Parámetros:
|
---|
| 20 | // - pathfilecfg : Ruta al fichero de configuración
|
---|
| 21 | //________________________________________________________________________________________________________
|
---|
| 22 | int TomaConfiguracion(char* pathfilecfg) {
|
---|
| 23 | long lSize;
|
---|
| 24 | char * buffer, *lineas[100], *dualparametro[2];
|
---|
| 25 | char ch[2];
|
---|
| 26 | int i, numlin, resul;
|
---|
| 27 |
|
---|
| 28 | if (pathfilecfg == NULL)
|
---|
| 29 | return (FALSE); // Nombre del fichero en blanco
|
---|
| 30 |
|
---|
| 31 | Fconfig = fopen(pathfilecfg, "rb");
|
---|
| 32 | if (Fconfig == NULL)
|
---|
| 33 | return (FALSE);
|
---|
| 34 | fseek(Fconfig, 0, SEEK_END); // Obtiene tamaño del fichero.
|
---|
| 35 | lSize = ftell(Fconfig);
|
---|
| 36 | rewind(Fconfig);
|
---|
| 37 | buffer = (char*) malloc(lSize); // Toma memoria para el buffer de lectura.
|
---|
| 38 | if (buffer == NULL)
|
---|
| 39 | return (FALSE);
|
---|
| 40 | fread(buffer, 1, lSize, Fconfig); // Lee contenido del fichero
|
---|
| 41 | fclose(Fconfig);
|
---|
| 42 |
|
---|
| 43 | //inicializar
|
---|
| 44 | IPlocal[0] = (char) NULL;
|
---|
| 45 | servidorhidra[0] = (char) NULL;
|
---|
| 46 | Puerto[0] = (char) NULL;
|
---|
| 47 |
|
---|
| 48 | usuario[0] = (char) NULL;
|
---|
| 49 | pasguor[0] = (char) NULL;
|
---|
| 50 | datasource[0] = (char) NULL;
|
---|
| 51 | catalog[0] = (char) NULL;
|
---|
| 52 |
|
---|
| 53 | strcpy(ch, "\n");// caracter delimitador (salto de linea)
|
---|
| 54 | numlin = split_parametros(lineas, buffer, ch);
|
---|
| 55 | for (i = 0; i < numlin; i++) {
|
---|
| 56 | strcpy(ch, "=");// caracter delimitador
|
---|
| 57 | split_parametros(dualparametro, lineas[i], ch); // Toma primer nombre del parametro
|
---|
| 58 |
|
---|
| 59 | resul = strcmp(dualparametro[0], "IPhidra");
|
---|
| 60 | if (resul == 0)
|
---|
| 61 | strcpy(IPlocal, dualparametro[1]);
|
---|
| 62 |
|
---|
| 63 | resul = strcmp(dualparametro[0], "IPhidra");
|
---|
| 64 | if (resul == 0)
|
---|
| 65 | strcpy(servidorhidra, dualparametro[1]);
|
---|
| 66 |
|
---|
| 67 | resul = strcmp(dualparametro[0], "Puerto");
|
---|
| 68 | if (resul == 0)
|
---|
| 69 | strcpy(Puerto, dualparametro[1]);
|
---|
| 70 |
|
---|
| 71 | resul = strcmp(dualparametro[0], "Usuario");
|
---|
| 72 | if (resul == 0)
|
---|
| 73 | strcpy(usuario, dualparametro[1]);
|
---|
| 74 |
|
---|
| 75 | resul = strcmp(dualparametro[0], "PassWord");
|
---|
| 76 | if (resul == 0)
|
---|
| 77 | strcpy(pasguor, dualparametro[1]);
|
---|
| 78 |
|
---|
| 79 | resul = strcmp(dualparametro[0], "DataSource");
|
---|
| 80 | if (resul == 0)
|
---|
| 81 | strcpy(datasource, dualparametro[1]);
|
---|
| 82 |
|
---|
| 83 | resul = strcmp(dualparametro[0], "Catalog");
|
---|
| 84 | if (resul == 0)
|
---|
| 85 | strcpy(catalog, dualparametro[1]);
|
---|
| 86 | }
|
---|
| 87 | if (IPlocal[0] == (char) NULL) {
|
---|
| 88 | RegistraLog("IPlocal, NO se ha definido este parámetro", false);
|
---|
| 89 | return (FALSE);
|
---|
| 90 | }
|
---|
| 91 | if (servidorhidra[0] == (char) NULL) {
|
---|
| 92 | RegistraLog("IPhidra, NO se ha definido este parámetro", false);
|
---|
| 93 | return (FALSE);
|
---|
| 94 | }
|
---|
| 95 | if (Puerto[0] == (char) NULL) {
|
---|
| 96 | RegistraLog("Puerto, NO se ha definido este parámetro", false);
|
---|
| 97 | return (FALSE);
|
---|
| 98 | }
|
---|
| 99 | puerto = atoi(Puerto);
|
---|
| 100 |
|
---|
| 101 | if (usuario[0] == (char) NULL) {
|
---|
| 102 | RegistraLog("Usuario, NO se ha definido este parámetro", false);
|
---|
| 103 | return (FALSE);
|
---|
| 104 | }
|
---|
| 105 | if (pasguor[0] == (char) NULL) {
|
---|
| 106 | RegistraLog("PassWord, NO se ha definido este parámetro", false);
|
---|
| 107 | return (FALSE);
|
---|
| 108 | }
|
---|
| 109 | if (datasource[0] == (char) NULL) {
|
---|
| 110 | RegistraLog("DataSource, NO se ha definido este parámetro", false);
|
---|
| 111 | return (FALSE);
|
---|
| 112 | }
|
---|
| 113 | if (catalog[0] == (char) NULL) {
|
---|
| 114 | RegistraLog("Catalog, NO se ha definido este parámetro", false);
|
---|
| 115 | return (FALSE);
|
---|
| 116 | }
|
---|
| 117 | return (TRUE);
|
---|
[5ea3638] | 118 | }
|
---|
| 119 | // _____________________________________________________________________________________________________________
|
---|
[9182fd9] | 120 | // Función: busca_accion
|
---|
[5ea3638] | 121 | //
|
---|
[9182fd9] | 122 | // Descripción:
|
---|
| 123 | // Esta Función busca en la base de datos, acciones programadas
|
---|
[5ea3638] | 124 | // Parametros:
|
---|
[9182fd9] | 125 | // - dia : Día actual del mes
|
---|
[5ea3638] | 126 | // - mes : mes en curso
|
---|
[9182fd9] | 127 | // - anno : Año en curso
|
---|
[5ea3638] | 128 | // - hora : Hora actual
|
---|
| 129 | // - minutos : Minutos actuales
|
---|
| 130 | // - diasemana : Dia de la semana 1=lunes,2=martes ... ( 0 Domingo)
|
---|
| 131 | // _____________________________________________________________________________________________________________
|
---|
| 132 | int busca_accion(WORD dia,WORD mes,WORD anno,WORD hora,WORD minutos,WORD diasemana)
|
---|
| 133 | {
|
---|
| 134 | char sqlstr[1000],ErrStr[200];
|
---|
| 135 | Database db,wdb;
|
---|
| 136 | Table tbl;
|
---|
| 137 | char parametros[LONGITUD_PARAMETROS];
|
---|
| 138 | BYTE swampm,bitsemana;
|
---|
| 139 | int tipoaccion,identificador;
|
---|
| 140 | int ordsem,ordulsem,ordiasem_1,maxdias;
|
---|
[9182fd9] | 141 | anno=anno-2003; // Año de comienzo es 2004
|
---|
[5ea3638] | 142 | if(hora>11){
|
---|
| 143 | hora-=12;
|
---|
| 144 | swampm=1; // Es pm
|
---|
| 145 | }
|
---|
| 146 | else
|
---|
| 147 | swampm=0; // Es am
|
---|
| 148 |
|
---|
| 149 | if(diasemana==0) diasemana=7; // El domingo
|
---|
| 150 |
|
---|
[9182fd9] | 151 | // Cuestión semanas
|
---|
[5ea3638] | 152 | ordiasem_1=DiadelaSemana(1,mes,anno+2003);
|
---|
[9182fd9] | 153 | ordsem=SemanadelMes(ordiasem_1,dia); // Calcula el número de la semana
|
---|
| 154 | if (mes!=2) // Toma el último día de ese mes
|
---|
[5ea3638] | 155 | maxdias=dias_meses[mes];
|
---|
| 156 | else{
|
---|
| 157 | if (bisiesto(anno+2003))
|
---|
| 158 | maxdias=29;
|
---|
| 159 | else
|
---|
| 160 | maxdias=28;
|
---|
| 161 | }
|
---|
[9182fd9] | 162 | ordulsem=SemanadelMes(ordiasem_1,maxdias); // Calcula el número de última semana
|
---|
[5ea3638] | 163 |
|
---|
| 164 | bitsemana=HEX_semanas[ordsem];
|
---|
[9182fd9] | 165 | if(ordsem==ordulsem) // Si es la última semana del mes
|
---|
[5ea3638] | 166 | bitsemana|=HEX_semanas[6];
|
---|
| 167 |
|
---|
[9182fd9] | 168 | if (!db.Open(usuario, pasguor, datasource, catalog)) { // error de conexion
|
---|
| 169 | db.GetErrorErrStr(ErrStr);
|
---|
| 170 | return (false);
|
---|
[5ea3638] | 171 | }
|
---|
| 172 | sprintf(sqlstr,"SELECT DISTINCT tipoaccion,identificador FROM programaciones WHERE suspendida=0 AND (annos & %d <> 0) AND (meses & %d<>0) AND ((diario & %d<>0) OR (dias & %d<>0) OR (semanas & %d<>0)) AND (horas & %d<>0) AND ampm=%d AND minutos=%d",HEX_annos[anno],HEX_meses[mes],HEX_dias[dia],HEX_diasemana[diasemana],bitsemana,HEX_horas[hora],swampm,minutos);
|
---|
| 173 | if(!db.Execute(sqlstr,tbl)){ // Error al leer
|
---|
| 174 | db.GetErrorErrStr(ErrStr);
|
---|
| 175 | return(false);
|
---|
| 176 | }
|
---|
| 177 | if(tbl.ISEOF()){
|
---|
| 178 | return(true); // No hay acciones programadas
|
---|
| 179 | }
|
---|
[9182fd9] | 180 | if (!wdb.Open(usuario, pasguor, datasource, catalog)) { // error de conexion
|
---|
| 181 | db.GetErrorErrStr(ErrStr);
|
---|
| 182 | return (false);
|
---|
[5ea3638] | 183 | }
|
---|
| 184 | while(!tbl.ISEOF()){ // Busca entre todas las programaciones
|
---|
| 185 | if(!tbl.Get("tipoaccion",tipoaccion)){ // Toma dato
|
---|
| 186 | tbl.GetErrorErrStr(ErrStr); // error al acceder al registro
|
---|
| 187 | return(false);
|
---|
| 188 | }
|
---|
| 189 | if(!tbl.Get("identificador",identificador)){ // Toma dato
|
---|
| 190 | tbl.GetErrorErrStr(ErrStr); // error al acceder al registro
|
---|
| 191 | return(false);
|
---|
| 192 | }
|
---|
[9182fd9] | 193 | if(tipoaccion==EJECUCION_TAREA){ // Es una programación de una tarea
|
---|
[5ea3638] | 194 | EjecutarTarea(identificador,0,0,0,wdb,parametros);
|
---|
| 195 | }
|
---|
| 196 | else{
|
---|
| 197 | if(tipoaccion==EJECUCION_TRABAJO){
|
---|
[9182fd9] | 198 | EjecutarTrabajo(identificador,wdb,parametros); // Es una programación de un trabajo
|
---|
[5ea3638] | 199 | }
|
---|
| 200 | else{
|
---|
| 201 | if(tipoaccion==EJECUCION_RESERVA){
|
---|
[9182fd9] | 202 | EjecutarReserva(identificador,wdb,parametros); // Es una programación de un trabajo
|
---|
[5ea3638] | 203 | }
|
---|
| 204 | }
|
---|
| 205 | }
|
---|
| 206 | tbl.MoveNext();
|
---|
| 207 | }
|
---|
| 208 | return(true);
|
---|
| 209 | }
|
---|
| 210 | // _____________________________________________________________________________________________________________
|
---|
[9182fd9] | 211 | // Función: bisiesto
|
---|
[5ea3638] | 212 | //
|
---|
[9182fd9] | 213 | // Descripción:
|
---|
| 214 | // Esta Función devuelve true si el año pasado como parámetro es bisiesto y false si no lo es
|
---|
[5ea3638] | 215 | // Parametros:
|
---|
[9182fd9] | 216 | // - anob : un año en formato aaaa
|
---|
[5ea3638] | 217 | // _____________________________________________________________________________________________________________
|
---|
| 218 | bool bisiesto(WORD anob){
|
---|
| 219 | return(anob%4==0);
|
---|
| 220 | }
|
---|
| 221 | // _____________________________________________________________________________________________________________
|
---|
[9182fd9] | 222 | // Función: DiadelaSemana
|
---|
[5ea3638] | 223 | //
|
---|
[9182fd9] | 224 | // Descripción:
|
---|
| 225 | // Esta Función devuelve el número del día de la semana: 1=Lunes, 2=martes ... 6=sábado 7=domingo de una fecha determinada
|
---|
[5ea3638] | 226 | // Parametros:
|
---|
[9182fd9] | 227 | // - dia : Un día
|
---|
[5ea3638] | 228 | // - mes : Un mes
|
---|
[9182fd9] | 229 | // - anno : Un año
|
---|
[5ea3638] | 230 | // _____________________________________________________________________________________________________________
|
---|
| 231 | int DiadelaSemana(WORD dia,WORD mes,WORD anno)
|
---|
| 232 | {
|
---|
| 233 | int i,cont,dias_anuales;
|
---|
| 234 | int desplazamiento_dias=6;
|
---|
| 235 | int orddiasem;
|
---|
| 236 |
|
---|
| 237 | cont =0;
|
---|
| 238 | for (i=1900;i<anno;i++){
|
---|
| 239 | if (bisiesto(i)) dias_anuales=366; else dias_anuales=365;
|
---|
| 240 | cont+=dias_anuales;
|
---|
| 241 | }
|
---|
| 242 | for (i=1;i<mes;i++){
|
---|
| 243 | if (i!=2)
|
---|
| 244 | cont+=dias_meses[i];
|
---|
| 245 | else{
|
---|
| 246 | if (bisiesto(anno))
|
---|
| 247 | cont+=29;
|
---|
| 248 | else
|
---|
| 249 | cont+=28;
|
---|
| 250 | }
|
---|
| 251 | }
|
---|
| 252 | cont+=dia+desplazamiento_dias;
|
---|
| 253 | orddiasem=(cont%7);
|
---|
| 254 | if(orddiasem==0) orddiasem=7;
|
---|
| 255 | return(orddiasem);
|
---|
| 256 | }
|
---|
| 257 | // _____________________________________________________________________________________________________________
|
---|
[9182fd9] | 258 | // Función: SemanadelMes
|
---|
[5ea3638] | 259 | //
|
---|
[9182fd9] | 260 | // Descripción:
|
---|
| 261 | // Esta Función devuelve el número de semana perteneciente a un día de ese mes
|
---|
| 262 | // Parámetros:
|
---|
| 263 | // - ordiasem_1 : Orden semenal (1,2...) del dia del primer dia del mes que se pasa como parámetro
|
---|
[5ea3638] | 264 | // - diames : El mes concreto
|
---|
| 265 | // _____________________________________________________________________________________________________________
|
---|
| 266 | int SemanadelMes(int ordiasem_1,int diames)
|
---|
| 267 | {
|
---|
| 268 | int nwdia,resto,cociente;
|
---|
| 269 |
|
---|
| 270 | nwdia=diames+ordiasem_1-1;
|
---|
| 271 | cociente=nwdia/7;
|
---|
| 272 | resto=nwdia%7;
|
---|
| 273 | if(resto>0) cociente++;
|
---|
| 274 | return(cociente);
|
---|
| 275 | }
|
---|
| 276 | // _____________________________________________________________________________________________________________
|
---|
[9182fd9] | 277 | // Función: Pausa
|
---|
[5ea3638] | 278 | //
|
---|
[9182fd9] | 279 | // Descripción:
|
---|
[5ea3638] | 280 | // Hace una pausa en segundos
|
---|
| 281 | // Parametros:
|
---|
| 282 | // - s : Segundos de pausa
|
---|
| 283 | // _____________________________________________________________________________________________________________
|
---|
| 284 | void Pausa(int s)
|
---|
| 285 | {
|
---|
| 286 | int seg=0;
|
---|
| 287 | clock_t comienzo;
|
---|
| 288 |
|
---|
| 289 | comienzo = clock();
|
---|
| 290 | do{
|
---|
| 291 | seg=(clock()-comienzo)/CLOCKS_PER_SEC;
|
---|
| 292 | }while(seg<s);
|
---|
| 293 | }
|
---|
| 294 | // _____________________________________________________________________________________________________________
|
---|
[9182fd9] | 295 | // Función: EjecutarTrabajo
|
---|
[5ea3638] | 296 | //
|
---|
[9182fd9] | 297 | // Descripción:
|
---|
| 298 | // Registra una acción (Trabajo y la envía para su ejecución
|
---|
| 299 | // Parámetros:
|
---|
[5ea3638] | 300 | // - idtrabajo : Identificador del trabajo
|
---|
[9182fd9] | 301 | // - db: una conexion ADO operativa
|
---|
| 302 | // - parametros: Parámetros de la acción
|
---|
[5ea3638] | 303 | // _____________________________________________________________________________________________________________
|
---|
| 304 | int EjecutarTrabajo(int idtrabajo,Database db,char*parametros )
|
---|
| 305 | {
|
---|
| 306 | char sqlstr[1000],ErrStr[200];
|
---|
| 307 | Table tbl;
|
---|
| 308 | int cont_tareas=0,lon;
|
---|
| 309 | int idtarea,idtrabajotarea,idcentro;
|
---|
| 310 | char wambitrabajo[500],ambitrabajo[4000];
|
---|
| 311 | char wparamtrabajo[20],paramtrabajo[1000];
|
---|
| 312 | int tbTareasidtarea[100],tbTareasidnotificador[100];
|
---|
| 313 | char *tbTareasparametros[100],*tbTareasambitoambitskwrk[100];
|
---|
| 314 | char ambitskwrk[500];
|
---|
| 315 |
|
---|
[9182fd9] | 316 | ambitrabajo[0]=(char)NULL; // Inicialización
|
---|
| 317 | strcpy(paramtrabajo,"tsk="); // Inicialización
|
---|
[5ea3638] | 318 |
|
---|
| 319 | // recupera el identificador del Centro propietario de la tarea
|
---|
| 320 | sprintf(sqlstr,"SELECT idcentro FROM trabajos WHERE idtrabajo=%d",idtrabajo);
|
---|
| 321 | if(!db.Execute(sqlstr,tbl)){ // Error al leer
|
---|
| 322 | db.GetErrorErrStr(ErrStr);
|
---|
| 323 | return(false);
|
---|
| 324 | }
|
---|
| 325 | if(tbl.ISEOF()) return(true);
|
---|
| 326 | if(!tbl.Get("idcentro",idcentro)){ // Toma dato
|
---|
| 327 | tbl.GetErrorErrStr(ErrStr); // error al acceder al registro
|
---|
| 328 | return(false);
|
---|
| 329 | }
|
---|
| 330 | // Recupera las tareas que forman parte del trabajo
|
---|
| 331 | sprintf(sqlstr,"SELECT * FROM trabajos_tareas WHERE idtrabajo=%d ORDER by orden",idtrabajo);
|
---|
| 332 | if(!db.Execute(sqlstr,tbl)){ // Error al leer
|
---|
| 333 | db.GetErrorErrStr(ErrStr);
|
---|
| 334 | return(false);
|
---|
| 335 | }
|
---|
| 336 | if(tbl.ISEOF()) return(true);
|
---|
| 337 | // Recorre trabajos-tareas
|
---|
| 338 | while(!tbl.ISEOF()){
|
---|
| 339 | if(!tbl.Get("idtrabajotarea",idtrabajotarea)){ // Toma dato
|
---|
| 340 | tbl.GetErrorErrStr(ErrStr); // error al acceder al registro
|
---|
| 341 | return(false);
|
---|
| 342 | }
|
---|
| 343 | tbTareasidnotificador[cont_tareas]=idtrabajotarea;
|
---|
| 344 |
|
---|
| 345 | if(!tbl.Get("idtarea",idtarea)){ // Toma dato
|
---|
| 346 | tbl.GetErrorErrStr(ErrStr); // error al acceder al registro
|
---|
| 347 | return(false);
|
---|
| 348 | }
|
---|
| 349 | tbTareasidtarea[cont_tareas]=idtarea;
|
---|
| 350 |
|
---|
| 351 | if(!tbl.Get("parametros",parametros)){ // Toma dato
|
---|
| 352 | tbl.GetErrorErrStr(ErrStr); // error al acceder al registro
|
---|
| 353 | return(false);
|
---|
| 354 | }
|
---|
| 355 | lon=strlen(parametros);
|
---|
| 356 | tbTareasparametros[cont_tareas]=(char*)malloc(lon);
|
---|
| 357 | if(tbTareasparametros[cont_tareas]==NULL)
|
---|
| 358 | return(false); // No hay memoria bastante
|
---|
| 359 | strcpy(tbTareasparametros[cont_tareas],parametros);
|
---|
| 360 |
|
---|
| 361 | if(!tbl.Get("ambitskwrk",ambitskwrk)){ // Toma dato
|
---|
| 362 | tbl.GetErrorErrStr(ErrStr); // error al acceder al registro
|
---|
| 363 | return(false);
|
---|
| 364 | }
|
---|
| 365 | lon=strlen(ambitskwrk);
|
---|
| 366 | tbTareasambitoambitskwrk[cont_tareas]=(char*)malloc(lon);
|
---|
| 367 | strcpy(tbTareasambitoambitskwrk[cont_tareas],ambitskwrk);
|
---|
| 368 |
|
---|
| 369 | sprintf(wambitrabajo,"%s;",ambitskwrk);
|
---|
| 370 | strcat(ambitrabajo,wambitrabajo);
|
---|
| 371 |
|
---|
| 372 | sprintf(wparamtrabajo,"%d;",idtrabajotarea);
|
---|
| 373 | strcat(paramtrabajo,wparamtrabajo);
|
---|
| 374 |
|
---|
| 375 | cont_tareas++;
|
---|
| 376 | tbl.MoveNext();
|
---|
| 377 | }
|
---|
| 378 | lon=strlen(ambitrabajo);
|
---|
| 379 | ambitrabajo[lon-1]=(char)NULL; // Quita la coma final
|
---|
| 380 |
|
---|
| 381 | lon=strlen(paramtrabajo);
|
---|
| 382 | paramtrabajo[lon-1]=(char)NULL; // Quita la coma final
|
---|
| 383 |
|
---|
[9182fd9] | 384 | char fechareg[100];
|
---|
| 385 |
|
---|
| 386 |
|
---|
| 387 | struct tm* st;
|
---|
| 388 | st = TomaHora();
|
---|
| 389 | sprintf(fechareg, "%d/%d/%d %d:%d:%d", st->tm_year + 1900, st->tm_mon + 1,
|
---|
| 390 | st->tm_mday, st->tm_hour, st->tm_min, st->tm_sec);
|
---|
[5ea3638] | 391 |
|
---|
[9182fd9] | 392 | sprintf(sqlstr,"INSERT INTO acciones (tipoaccion,idtipoaccion,cateaccion,ambito,idambito,ambitskwrk,fechahorareg,estado,resultado,idcentro,parametros,accionid,idnotificador) VALUES (%d,%d,%d,0,0,'%s','%s','%s','%s',%d,'%s',0,0)",EJECUCION_TRABAJO,idtrabajo,PROCESOS,ambitrabajo,fechareg,ACCION_INICIADA,ACCION_SINERRORES,idcentro,paramtrabajo);
|
---|
[5ea3638] | 393 | if(!db.Execute(sqlstr)){ // Error al insertar
|
---|
| 394 | db.GetErrorErrStr(ErrStr);
|
---|
| 395 | return(false);
|
---|
| 396 | }
|
---|
| 397 | int accionid=0;
|
---|
[9182fd9] | 398 | // Toma identificador de la acción
|
---|
[5ea3638] | 399 | sprintf(sqlstr,"SELECT @@identity as identificador");
|
---|
| 400 | if(!db.Execute(sqlstr,tbl)){ // Error al leer
|
---|
| 401 | db.GetErrorErrStr(ErrStr);
|
---|
| 402 | return(false);
|
---|
| 403 | }
|
---|
| 404 | if(!tbl.ISEOF()){ // Si existe registro
|
---|
| 405 | if(!tbl.Get("identificador",accionid)){
|
---|
| 406 | tbl.GetErrorErrStr(ErrStr); // error al acceder al registro
|
---|
| 407 | return(false);
|
---|
| 408 | }
|
---|
| 409 | }
|
---|
| 410 | int i;
|
---|
| 411 | // Insertar acciones:tareas
|
---|
| 412 | for (i=0;i<cont_tareas;i++){
|
---|
| 413 | if(!EjecutarTarea(tbTareasidtarea[i],accionid,tbTareasidnotificador[i],idcentro,db,parametros)){
|
---|
| 414 | free(tbTareasparametros[i]);
|
---|
| 415 | free(tbTareasambitoambitskwrk[i]);
|
---|
| 416 | return(false);
|
---|
| 417 | }
|
---|
| 418 | free(tbTareasparametros[i]);
|
---|
| 419 | free(tbTareasambitoambitskwrk[i]);
|
---|
| 420 | }
|
---|
| 421 | return(true);
|
---|
| 422 | }
|
---|
| 423 | // _____________________________________________________________________________________________________________
|
---|
[9182fd9] | 424 | // Función: EjecutarTarea
|
---|
[5ea3638] | 425 | //
|
---|
[9182fd9] | 426 | // Descripción:
|
---|
| 427 | // Registra una acción (Tarea) y la envía para su ejecución
|
---|
| 428 | // Parámetros:
|
---|
[5ea3638] | 429 | // - idtarea : Identificador de la tarea
|
---|
| 430 | // - accionid: identificador del trabajo padre (si existe)
|
---|
| 431 | // - idnotificador: identificador del trabajo_tarea incluido en trabajo padre (si existe)
|
---|
| 432 | // - idcentro: Centro propietario del trabjo padre (si existe este trabajo)
|
---|
[9182fd9] | 433 | // - db: una conexion ADO operativa
|
---|
| 434 | // - parametros: Parámetros de la acción
|
---|
[5ea3638] | 435 | // _____________________________________________________________________________________________________________
|
---|
| 436 | int EjecutarTarea(int idtarea,int accionid,int idnotificador,int idcentro,Database db,char *parametros )
|
---|
| 437 | {
|
---|
| 438 | char sqlstr[1000],ErrStr[200],ambito;
|
---|
| 439 | Table tbl;
|
---|
| 440 | int cont_comandos=0,lon;
|
---|
| 441 | int idcomando,idambito,idtareacomando,accionidcmd;
|
---|
| 442 | char wambitarea[20],ambitarea[4000];
|
---|
| 443 | char wparamtarea[20],paramtarea[1000],pids[20];
|
---|
| 444 | int tbComandosidcomando[100],tbComandosambito[100],tbComandosidnotificador[100],tbComandosidambito[100];
|
---|
| 445 | char *tbComandosparametros[100];
|
---|
| 446 |
|
---|
[9182fd9] | 447 | ambitarea[0]=(char)NULL; // Inicialización
|
---|
| 448 | strcpy(paramtarea,"cmd="); // Inicialización
|
---|
[5ea3638] | 449 | if(idcentro==0){
|
---|
| 450 | // recupera el identificador del Centro propietario de la tarea
|
---|
| 451 | sprintf(sqlstr,"SELECT idcentro FROM tareas WHERE idtarea=%d",idtarea);
|
---|
| 452 | if(!db.Execute(sqlstr,tbl)){ // Error al leer
|
---|
| 453 | db.GetErrorErrStr(ErrStr);
|
---|
| 454 | return(false);
|
---|
| 455 | }
|
---|
| 456 | if(tbl.ISEOF()) return(true);
|
---|
| 457 | if(!tbl.Get("idcentro",idcentro)){ // Toma dato
|
---|
| 458 | tbl.GetErrorErrStr(ErrStr); // error al acceder al registro
|
---|
| 459 | return(false);
|
---|
| 460 | }
|
---|
| 461 | }
|
---|
| 462 | // Recupera los comandos que forman parte de la tarea
|
---|
| 463 | sprintf(sqlstr,"SELECT * FROM tareas_comandos WHERE idtarea=%d ORDER by orden",idtarea);
|
---|
| 464 | if(!db.Execute(sqlstr,tbl)){ // Error al leer
|
---|
| 465 | db.GetErrorErrStr(ErrStr);
|
---|
| 466 | return(false);
|
---|
| 467 | }
|
---|
| 468 | if(tbl.ISEOF()) return(true);
|
---|
| 469 |
|
---|
| 470 | // Recorre tareas-comandos
|
---|
| 471 | while(!tbl.ISEOF()){
|
---|
| 472 | if(!tbl.Get("idcomando",idcomando)){ // Toma dato
|
---|
| 473 | tbl.GetErrorErrStr(ErrStr); // error al acceder al registro
|
---|
| 474 | return(false);
|
---|
| 475 | }
|
---|
| 476 | tbComandosidcomando[cont_comandos]=idcomando;
|
---|
| 477 |
|
---|
| 478 | if(!tbl.Get("ambito",ambito)){ // Toma dato
|
---|
| 479 | tbl.GetErrorErrStr(ErrStr); // error al acceder al registro
|
---|
| 480 | return(false);
|
---|
| 481 | }
|
---|
| 482 | tbComandosambito[cont_comandos]=ambito;
|
---|
| 483 |
|
---|
| 484 | if(!tbl.Get("idambito",idambito)){ // Toma dato
|
---|
| 485 | tbl.GetErrorErrStr(ErrStr); // error al acceder al registro
|
---|
| 486 | return(false);
|
---|
| 487 | }
|
---|
| 488 | tbComandosidambito[cont_comandos]=idambito;
|
---|
| 489 |
|
---|
| 490 |
|
---|
| 491 | if(!tbl.Get("parametros",parametros)){ // Toma dato
|
---|
| 492 | tbl.GetErrorErrStr(ErrStr); // error al acceder al registro
|
---|
| 493 | return(false);
|
---|
| 494 | }
|
---|
| 495 |
|
---|
| 496 | lon=strlen(parametros);
|
---|
| 497 | tbComandosparametros[cont_comandos]=(char*)malloc(lon+20);
|
---|
| 498 | if(tbComandosparametros[cont_comandos]==NULL)
|
---|
| 499 | return(false); // No hay memoria bastante
|
---|
| 500 |
|
---|
| 501 | strcpy(tbComandosparametros[cont_comandos],parametros);
|
---|
| 502 |
|
---|
| 503 | if(!tbl.Get("idtareacomando",idtareacomando)){ // Toma dato
|
---|
| 504 | tbl.GetErrorErrStr(ErrStr); // error al acceder al registro
|
---|
| 505 | return(false);
|
---|
| 506 | }
|
---|
| 507 | tbComandosidnotificador[cont_comandos]=idtareacomando;
|
---|
| 508 |
|
---|
| 509 | sprintf(wambitarea,"%d:%d;",ambito,idambito);
|
---|
| 510 | strcat(ambitarea,wambitarea);
|
---|
| 511 |
|
---|
| 512 | sprintf(wparamtarea,"%d;",idtareacomando);
|
---|
| 513 | strcat(paramtarea,wparamtarea);
|
---|
| 514 |
|
---|
| 515 | cont_comandos++;
|
---|
| 516 | tbl.MoveNext();
|
---|
| 517 | }
|
---|
| 518 | lon=strlen(ambitarea);
|
---|
| 519 | ambitarea[lon-1]=(char)NULL; // Quita la coma final
|
---|
| 520 |
|
---|
| 521 | lon=strlen(paramtarea);
|
---|
| 522 | paramtarea[lon-1]=(char)NULL; // Quita la coma final
|
---|
| 523 |
|
---|
[9182fd9] | 524 | char fechareg[100];
|
---|
[5ea3638] | 525 |
|
---|
[9182fd9] | 526 |
|
---|
| 527 | struct tm* st;
|
---|
| 528 | st = TomaHora();
|
---|
| 529 | sprintf(fechareg, "%d/%d/%d %d:%d:%d", st->tm_year + 1900, st->tm_mon + 1,
|
---|
| 530 | st->tm_mday, st->tm_hour, st->tm_min, st->tm_sec);
|
---|
| 531 |
|
---|
| 532 | sprintf(sqlstr,"INSERT INTO acciones (tipoaccion,idtipoaccion,cateaccion,ambito,idambito,ambitskwrk,fechahorareg,estado,resultado,idcentro,parametros,accionid,idnotificador) VALUES (%d,%d,%d,0,0,'%s','%s','%s','%s',%d,'%s',%d,%d)",EJECUCION_TAREA,idtarea,PROCESOS,ambitarea,fechareg,ACCION_INICIADA,ACCION_SINERRORES,idcentro,paramtarea,accionid,idnotificador);
|
---|
[5ea3638] | 533 | if(!db.Execute(sqlstr)){ // Error al insertar
|
---|
| 534 | db.GetErrorErrStr(ErrStr);
|
---|
| 535 | return(false);
|
---|
| 536 | }
|
---|
| 537 | accionid=0;
|
---|
[9182fd9] | 538 | // Toma identificador de la acción
|
---|
[5ea3638] | 539 | sprintf(sqlstr,"SELECT @@identity as identificador");
|
---|
| 540 | if(!db.Execute(sqlstr,tbl)){ // Error al leer
|
---|
| 541 | db.GetErrorErrStr(ErrStr);
|
---|
| 542 | return(false);
|
---|
| 543 | }
|
---|
| 544 | if(!tbl.ISEOF()){ // Si existe registro
|
---|
| 545 | if(!tbl.Get("identificador",accionid)){
|
---|
| 546 | tbl.GetErrorErrStr(ErrStr); // error al acceder al registro
|
---|
| 547 | return(false);
|
---|
| 548 | }
|
---|
| 549 | }
|
---|
| 550 | int i;
|
---|
| 551 | // Insertar acciones:comandos
|
---|
| 552 | for (i=0;i<cont_comandos;i++){
|
---|
[9182fd9] | 553 | st = TomaHora();
|
---|
| 554 | sprintf(fechareg, "%d/%d/%d %d:%d:%d", st->tm_year + 1900, st->tm_mon
|
---|
| 555 | + 1, st->tm_mday, st->tm_hour, st->tm_min, st->tm_sec);
|
---|
| 556 |
|
---|
| 557 | sprintf(sqlstr,"INSERT INTO acciones (tipoaccion,idtipoaccion,cateaccion,ambito,idambito,fechahorareg,estado,resultado,idcentro,parametros,accionid,idnotificador) VALUES (%d,%d,%d,%d,%d,'%s','%s','%s',%d,'%s',%d,%d)",EJECUCION_COMANDO,tbComandosidcomando[i],PROCESOS,tbComandosambito[i],tbComandosidambito[i],fechareg,ACCION_EXITOSA,ACCION_SINERRORES,idcentro,tbComandosparametros[i],accionid,tbComandosidnotificador[i]);
|
---|
[5ea3638] | 558 | if(!db.Execute(sqlstr)){ // Error al insertar
|
---|
| 559 | db.GetErrorErrStr(ErrStr);
|
---|
| 560 | free(tbComandosparametros[i]);
|
---|
| 561 | return(false);
|
---|
| 562 | }
|
---|
| 563 |
|
---|
[9182fd9] | 564 | // Toma identificador dela acción
|
---|
[5ea3638] | 565 | sprintf(sqlstr,"SELECT @@identity as identificador");
|
---|
| 566 | if(!db.Execute(sqlstr,tbl)){ // Error al leer
|
---|
| 567 | db.GetErrorErrStr(ErrStr);
|
---|
| 568 | return(false);
|
---|
| 569 | }
|
---|
| 570 |
|
---|
| 571 | if(!tbl.ISEOF()){ // Si existe registro
|
---|
| 572 | if(!tbl.Get("identificador",accionidcmd)){
|
---|
| 573 | tbl.GetErrorErrStr(ErrStr); // error al acceder al registro
|
---|
| 574 | return(false);
|
---|
| 575 | }
|
---|
| 576 | }
|
---|
| 577 | sprintf(pids,"ids=%d\r",accionidcmd);
|
---|
[9182fd9] | 578 | strcat(tbComandosparametros[i],pids); // Le añade el identificador de la acción
|
---|
[5ea3638] | 579 | envia_comando(tbComandosparametros[i]);
|
---|
| 580 | free(tbComandosparametros[i]);
|
---|
| 581 | }
|
---|
| 582 | return(true);
|
---|
| 583 | }
|
---|
| 584 | // _____________________________________________________________________________________________________________
|
---|
[9182fd9] | 585 | // Función: EjecutarReserva
|
---|
[5ea3638] | 586 | //
|
---|
[9182fd9] | 587 | // Descripción:
|
---|
| 588 | // Registra una acción (Tarea) y la envía para su ejecución
|
---|
| 589 | // Parámetros:
|
---|
[5ea3638] | 590 | // - idreserva : Identificador de la reserva
|
---|
[9182fd9] | 591 | // - db: una conexion ADO operativa
|
---|
| 592 | // - parametros: Parámetros de la acción
|
---|
[5ea3638] | 593 | // _____________________________________________________________________________________________________________
|
---|
| 594 | int EjecutarReserva(int idreserva,Database db,char*parametros )
|
---|
| 595 | {
|
---|
| 596 | char sqlstr[1000],ErrStr[200];
|
---|
| 597 | Table tbl;
|
---|
| 598 | int idaccion;
|
---|
| 599 |
|
---|
| 600 | sprintf(sqlstr,"SELECT idtarea,idtrabajo FROM reservas WHERE idreserva=%d",idreserva);
|
---|
| 601 | if(!db.Execute(sqlstr,tbl)){ // Error al leer
|
---|
| 602 | db.GetErrorErrStr(ErrStr);
|
---|
| 603 | return(false);
|
---|
| 604 | }
|
---|
| 605 | if(tbl.ISEOF()){
|
---|
| 606 | return(false); // No hay acciones previas en la reserva
|
---|
| 607 | }
|
---|
| 608 |
|
---|
| 609 | if(!tbl.Get("idtarea",idaccion)){ // Toma dato
|
---|
| 610 | tbl.GetErrorErrStr(ErrStr); // error al acceder al registro
|
---|
| 611 | return(false);
|
---|
| 612 | }
|
---|
| 613 | if(idaccion>0)
|
---|
| 614 | EjecutarTarea(idaccion,0,0,0,db,parametros); // Es una reserva con tarea previa
|
---|
| 615 |
|
---|
| 616 | if(!tbl.Get("idtrabajo",idaccion)){ // Toma dato
|
---|
| 617 | tbl.GetErrorErrStr(ErrStr); // error al acceder al registro
|
---|
| 618 | return(false);
|
---|
| 619 | }
|
---|
| 620 | if(idaccion>0)
|
---|
| 621 | EjecutarTrabajo(idaccion,db,parametros); // Es una reserva con trabajo previo
|
---|
| 622 |
|
---|
| 623 | return(true);
|
---|
| 624 | }
|
---|
| 625 | // _____________________________________________________________________________________________________________
|
---|
[9182fd9] | 626 | // Función: envia_comando
|
---|
[5ea3638] | 627 | //
|
---|
[9182fd9] | 628 | // Descripción:
|
---|
| 629 | // Envía un comando a la red. Para ello es necesario tener iniciado el servicio hidra.
|
---|
| 630 | // Parámetros:
|
---|
| 631 | // - parametros: Parámetros del comando
|
---|
[5ea3638] | 632 | // _____________________________________________________________________________________________________________
|
---|
| 633 | int envia_comando(char* parametros)
|
---|
| 634 | {
|
---|
| 635 | SOCKET sClient;
|
---|
| 636 | TRAMA trama;
|
---|
| 637 |
|
---|
| 638 | sClient = AbreConexion(servidorhidra,puerto);
|
---|
| 639 | if (sClient == (SOCKET)NULL)
|
---|
| 640 | return(FALSE);
|
---|
| 641 |
|
---|
| 642 | trama.arroba='@';
|
---|
| 643 | strncpy(trama.identificador,"JMMLCAMDJ",9);
|
---|
| 644 | trama.ejecutor=parametros[0];
|
---|
| 645 | strcpy(trama.parametros,(char*)¶metros[1]);
|
---|
| 646 | return(manda_trama(sClient,&trama));
|
---|
| 647 | }
|
---|
| 648 |
|
---|
| 649 | /// _____________________________________________________________________________________________________________
|
---|
[9182fd9] | 650 | // Función: manda_trama
|
---|
[5ea3638] | 651 | //
|
---|
[9182fd9] | 652 | // Descripción:
|
---|
| 653 | // Esta Función envía una trama por la red (TCP)
|
---|
| 654 | // Parámetros:
|
---|
[5ea3638] | 655 | // - sock : El socket del host al que se dirige la trama
|
---|
| 656 | // - trama: El contenido de la trama
|
---|
| 657 | /// _____________________________________________________________________________________________________________
|
---|
| 658 | int manda_trama(SOCKET sock,TRAMA* trama)
|
---|
| 659 | {
|
---|
| 660 | int nLeft,idx,ret;
|
---|
| 661 |
|
---|
| 662 | Encriptar((char*)trama);
|
---|
| 663 | nLeft = strlen((char*)trama);
|
---|
| 664 | idx = 0;
|
---|
| 665 | while(nLeft > 0){
|
---|
| 666 | ret = send(sock,(char*)&trama[idx], nLeft, 0);
|
---|
| 667 | if (ret == 0)
|
---|
| 668 | break;
|
---|
| 669 | else
|
---|
| 670 | if (ret == SOCKET_ERROR){
|
---|
| 671 | RegistraLog("***AGENT***send() fallo al enviar trama:",true);
|
---|
| 672 | return(FALSE);
|
---|
| 673 | }
|
---|
| 674 | nLeft -= ret;
|
---|
| 675 | idx += ret;
|
---|
| 676 | }
|
---|
| 677 | return(TRUE);
|
---|
| 678 | }
|
---|
| 679 | //******************************************************************************************************************************************
|
---|
[9182fd9] | 680 | // PROGRAMA PRINCIPAL
|
---|
[5ea3638] | 681 | //******************************************************************************************************************************************
|
---|
[9182fd9] | 682 | int main(int argc, char *argv[]) {
|
---|
| 683 |
|
---|
| 684 | struct tm* st;
|
---|
| 685 |
|
---|
| 686 | strcpy(szPathFileCfg, "ogAdmAgent.cfg");
|
---|
| 687 | strcpy(szPathFileLog, "ogAdmAgent.log");
|
---|
| 688 | int i;
|
---|
| 689 | for (i = 1; (i + 1) < argc; i += 2) {
|
---|
| 690 | if (argv[i][0] == '-') {
|
---|
| 691 | switch (tolower(argv[i][1])) {
|
---|
| 692 | case 'f':
|
---|
| 693 | if (argv[i + 1] != NULL)
|
---|
| 694 | strcpy(szPathFileCfg, argv[i + 1]);
|
---|
| 695 | else {
|
---|
| 696 | RegistraLog(
|
---|
| 697 | "Fallo en los parámetros: Debe especificar el fichero de configuración del servicio",
|
---|
| 698 | false);
|
---|
| 699 | exit(EXIT_FAILURE);
|
---|
| 700 | }
|
---|
| 701 | break;
|
---|
| 702 | case 'l':
|
---|
| 703 | if (argv[i + 1] != NULL)
|
---|
| 704 | strcpy(szPathFileLog, argv[i + 1]);
|
---|
| 705 | else {
|
---|
| 706 | RegistraLog(
|
---|
| 707 | "Fallo en los parámetros: Debe especificar el fichero de log para el servicio",
|
---|
| 708 | false);
|
---|
| 709 | exit(EXIT_FAILURE);
|
---|
| 710 | }
|
---|
| 711 | break;
|
---|
| 712 | default:
|
---|
| 713 | RegistraLog(
|
---|
| 714 | "Fallo de sintaxis en los parámetros: Debe especificar -f nombre_del_fichero_de_configuración_del_servicio",
|
---|
| 715 | false);
|
---|
| 716 | exit(EXIT_FAILURE);
|
---|
| 717 | break;
|
---|
| 718 | }
|
---|
| 719 | }
|
---|
| 720 | }
|
---|
| 721 | if (szPathFileCfg == NULL) {
|
---|
| 722 | printf("***Error. No se ha especificado fichero de configuración\n");
|
---|
| 723 | exit(EXIT_FAILURE);
|
---|
| 724 | }
|
---|
| 725 | if (!TomaConfiguracion(szPathFileCfg)) { // Toma parametros de configuración
|
---|
| 726 | RegistraLog(
|
---|
| 727 | "El fichero de configuración contiene un error de sintaxis",
|
---|
| 728 | false);
|
---|
| 729 | exit(EXIT_FAILURE);
|
---|
| 730 | }
|
---|
[5ea3638] | 731 |
|
---|
[9182fd9] | 732 | int pseg;
|
---|
[5ea3638] | 733 |
|
---|
| 734 | while (TRUE){
|
---|
[9182fd9] | 735 | st = TomaHora();
|
---|
| 736 | //pseg=1000*(65-st->tm_sec); // Calcula milisegundos de inactividad de la hebra
|
---|
| 737 | pseg=65-st->tm_sec; // Calcula segundos de inactividad de la hebra
|
---|
| 738 | sleep(pseg);
|
---|
| 739 |
|
---|
[5ea3638] | 740 | // Toma la hora
|
---|
[9182fd9] | 741 | st = TomaHora();
|
---|
| 742 | busca_accion(st->tm_mday,st->tm_mon,st->tm_year,st->tm_hour,st->tm_min,st->tm_wday );
|
---|
[5ea3638] | 743 | }
|
---|
[9182fd9] | 744 | }
|
---|
[5ea3638] | 745 |
|
---|