[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;
|
---|
[231f87d] | 141 | anno=anno-2009; // 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
|
---|
[231f87d] | 152 | ordiasem_1=DiadelaSemana(1,mes,anno+2009);
|
---|
[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{
|
---|
[231f87d] | 157 | if (bisiesto(anno+2009))
|
---|
[5ea3638] | 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 | }
|
---|
[231f87d] | 172 | sprintf(sqlstr,"SELECT DISTINCT tipoaccion,identificador FROM programaciones WHERE "\
|
---|
| 173 | " suspendida=0 "\
|
---|
| 174 | " AND (annos & %d <> 0) "\
|
---|
| 175 | " AND (meses & %d<>0) "\
|
---|
| 176 | " AND ((diario & %d<>0) OR (dias & %d<>0) OR (semanas & %d<>0))"\
|
---|
| 177 | " AND (horas & %d<>0) AND ampm=%d AND minutos=%d",\
|
---|
| 178 | HEX_annos[anno],\
|
---|
| 179 | HEX_meses[mes],\
|
---|
| 180 | HEX_dias[dia],\
|
---|
| 181 | HEX_diasemana[diasemana],
|
---|
| 182 | bitsemana,\
|
---|
| 183 | HEX_horas[hora],\
|
---|
| 184 | swampm,minutos);
|
---|
| 185 |
|
---|
[5ea3638] | 186 | if(!db.Execute(sqlstr,tbl)){ // Error al leer
|
---|
| 187 | db.GetErrorErrStr(ErrStr);
|
---|
| 188 | return(false);
|
---|
| 189 | }
|
---|
| 190 | if(tbl.ISEOF()){
|
---|
| 191 | return(true); // No hay acciones programadas
|
---|
| 192 | }
|
---|
[9182fd9] | 193 | if (!wdb.Open(usuario, pasguor, datasource, catalog)) { // error de conexion
|
---|
| 194 | db.GetErrorErrStr(ErrStr);
|
---|
| 195 | return (false);
|
---|
[5ea3638] | 196 | }
|
---|
| 197 | while(!tbl.ISEOF()){ // Busca entre todas las programaciones
|
---|
| 198 | if(!tbl.Get("tipoaccion",tipoaccion)){ // Toma dato
|
---|
| 199 | tbl.GetErrorErrStr(ErrStr); // error al acceder al registro
|
---|
| 200 | return(false);
|
---|
| 201 | }
|
---|
| 202 | if(!tbl.Get("identificador",identificador)){ // Toma dato
|
---|
| 203 | tbl.GetErrorErrStr(ErrStr); // error al acceder al registro
|
---|
| 204 | return(false);
|
---|
| 205 | }
|
---|
[9182fd9] | 206 | if(tipoaccion==EJECUCION_TAREA){ // Es una programación de una tarea
|
---|
[5ea3638] | 207 | EjecutarTarea(identificador,0,0,0,wdb,parametros);
|
---|
| 208 | }
|
---|
| 209 | else{
|
---|
| 210 | if(tipoaccion==EJECUCION_TRABAJO){
|
---|
[9182fd9] | 211 | EjecutarTrabajo(identificador,wdb,parametros); // Es una programación de un trabajo
|
---|
[5ea3638] | 212 | }
|
---|
| 213 | else{
|
---|
| 214 | if(tipoaccion==EJECUCION_RESERVA){
|
---|
[9182fd9] | 215 | EjecutarReserva(identificador,wdb,parametros); // Es una programación de un trabajo
|
---|
[5ea3638] | 216 | }
|
---|
| 217 | }
|
---|
| 218 | }
|
---|
| 219 | tbl.MoveNext();
|
---|
| 220 | }
|
---|
| 221 | return(true);
|
---|
| 222 | }
|
---|
| 223 | // _____________________________________________________________________________________________________________
|
---|
[9182fd9] | 224 | // Función: bisiesto
|
---|
[5ea3638] | 225 | //
|
---|
[9182fd9] | 226 | // Descripción:
|
---|
| 227 | // Esta Función devuelve true si el año pasado como parámetro es bisiesto y false si no lo es
|
---|
[5ea3638] | 228 | // Parametros:
|
---|
[9182fd9] | 229 | // - anob : un año en formato aaaa
|
---|
[5ea3638] | 230 | // _____________________________________________________________________________________________________________
|
---|
| 231 | bool bisiesto(WORD anob){
|
---|
| 232 | return(anob%4==0);
|
---|
| 233 | }
|
---|
| 234 | // _____________________________________________________________________________________________________________
|
---|
[9182fd9] | 235 | // Función: DiadelaSemana
|
---|
[5ea3638] | 236 | //
|
---|
[9182fd9] | 237 | // Descripción:
|
---|
| 238 | // 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] | 239 | // Parametros:
|
---|
[9182fd9] | 240 | // - dia : Un día
|
---|
[5ea3638] | 241 | // - mes : Un mes
|
---|
[9182fd9] | 242 | // - anno : Un año
|
---|
[5ea3638] | 243 | // _____________________________________________________________________________________________________________
|
---|
| 244 | int DiadelaSemana(WORD dia,WORD mes,WORD anno)
|
---|
| 245 | {
|
---|
| 246 | int i,cont,dias_anuales;
|
---|
| 247 | int desplazamiento_dias=6;
|
---|
| 248 | int orddiasem;
|
---|
| 249 |
|
---|
| 250 | cont =0;
|
---|
| 251 | for (i=1900;i<anno;i++){
|
---|
| 252 | if (bisiesto(i)) dias_anuales=366; else dias_anuales=365;
|
---|
| 253 | cont+=dias_anuales;
|
---|
| 254 | }
|
---|
| 255 | for (i=1;i<mes;i++){
|
---|
| 256 | if (i!=2)
|
---|
| 257 | cont+=dias_meses[i];
|
---|
| 258 | else{
|
---|
| 259 | if (bisiesto(anno))
|
---|
| 260 | cont+=29;
|
---|
| 261 | else
|
---|
| 262 | cont+=28;
|
---|
| 263 | }
|
---|
| 264 | }
|
---|
| 265 | cont+=dia+desplazamiento_dias;
|
---|
| 266 | orddiasem=(cont%7);
|
---|
| 267 | if(orddiasem==0) orddiasem=7;
|
---|
| 268 | return(orddiasem);
|
---|
| 269 | }
|
---|
| 270 | // _____________________________________________________________________________________________________________
|
---|
[9182fd9] | 271 | // Función: SemanadelMes
|
---|
[5ea3638] | 272 | //
|
---|
[9182fd9] | 273 | // Descripción:
|
---|
| 274 | // Esta Función devuelve el número de semana perteneciente a un día de ese mes
|
---|
| 275 | // Parámetros:
|
---|
| 276 | // - ordiasem_1 : Orden semenal (1,2...) del dia del primer dia del mes que se pasa como parámetro
|
---|
[5ea3638] | 277 | // - diames : El mes concreto
|
---|
| 278 | // _____________________________________________________________________________________________________________
|
---|
| 279 | int SemanadelMes(int ordiasem_1,int diames)
|
---|
| 280 | {
|
---|
| 281 | int nwdia,resto,cociente;
|
---|
| 282 |
|
---|
| 283 | nwdia=diames+ordiasem_1-1;
|
---|
| 284 | cociente=nwdia/7;
|
---|
| 285 | resto=nwdia%7;
|
---|
| 286 | if(resto>0) cociente++;
|
---|
| 287 | return(cociente);
|
---|
| 288 | }
|
---|
| 289 | // _____________________________________________________________________________________________________________
|
---|
[9182fd9] | 290 | // Función: Pausa
|
---|
[5ea3638] | 291 | //
|
---|
[9182fd9] | 292 | // Descripción:
|
---|
[5ea3638] | 293 | // Hace una pausa en segundos
|
---|
| 294 | // Parametros:
|
---|
| 295 | // - s : Segundos de pausa
|
---|
| 296 | // _____________________________________________________________________________________________________________
|
---|
| 297 | void Pausa(int s)
|
---|
| 298 | {
|
---|
| 299 | int seg=0;
|
---|
| 300 | clock_t comienzo;
|
---|
| 301 |
|
---|
| 302 | comienzo = clock();
|
---|
| 303 | do{
|
---|
| 304 | seg=(clock()-comienzo)/CLOCKS_PER_SEC;
|
---|
| 305 | }while(seg<s);
|
---|
| 306 | }
|
---|
| 307 | // _____________________________________________________________________________________________________________
|
---|
[9182fd9] | 308 | // Función: EjecutarTrabajo
|
---|
[5ea3638] | 309 | //
|
---|
[9182fd9] | 310 | // Descripción:
|
---|
| 311 | // Registra una acción (Trabajo y la envía para su ejecución
|
---|
| 312 | // Parámetros:
|
---|
[5ea3638] | 313 | // - idtrabajo : Identificador del trabajo
|
---|
[9182fd9] | 314 | // - db: una conexion ADO operativa
|
---|
| 315 | // - parametros: Parámetros de la acción
|
---|
[5ea3638] | 316 | // _____________________________________________________________________________________________________________
|
---|
| 317 | int EjecutarTrabajo(int idtrabajo,Database db,char*parametros )
|
---|
| 318 | {
|
---|
| 319 | char sqlstr[1000],ErrStr[200];
|
---|
| 320 | Table tbl;
|
---|
| 321 | int cont_tareas=0,lon;
|
---|
| 322 | int idtarea,idtrabajotarea,idcentro;
|
---|
| 323 | char wambitrabajo[500],ambitrabajo[4000];
|
---|
| 324 | char wparamtrabajo[20],paramtrabajo[1000];
|
---|
| 325 | int tbTareasidtarea[100],tbTareasidnotificador[100];
|
---|
| 326 | char *tbTareasparametros[100],*tbTareasambitoambitskwrk[100];
|
---|
| 327 | char ambitskwrk[500];
|
---|
| 328 |
|
---|
[9182fd9] | 329 | ambitrabajo[0]=(char)NULL; // Inicialización
|
---|
| 330 | strcpy(paramtrabajo,"tsk="); // Inicialización
|
---|
[5ea3638] | 331 |
|
---|
| 332 | // recupera el identificador del Centro propietario de la tarea
|
---|
| 333 | sprintf(sqlstr,"SELECT idcentro FROM trabajos WHERE idtrabajo=%d",idtrabajo);
|
---|
| 334 | if(!db.Execute(sqlstr,tbl)){ // Error al leer
|
---|
| 335 | db.GetErrorErrStr(ErrStr);
|
---|
| 336 | return(false);
|
---|
| 337 | }
|
---|
| 338 | if(tbl.ISEOF()) return(true);
|
---|
| 339 | if(!tbl.Get("idcentro",idcentro)){ // Toma dato
|
---|
| 340 | tbl.GetErrorErrStr(ErrStr); // error al acceder al registro
|
---|
| 341 | return(false);
|
---|
| 342 | }
|
---|
| 343 | // Recupera las tareas que forman parte del trabajo
|
---|
| 344 | sprintf(sqlstr,"SELECT * FROM trabajos_tareas WHERE idtrabajo=%d ORDER by orden",idtrabajo);
|
---|
| 345 | if(!db.Execute(sqlstr,tbl)){ // Error al leer
|
---|
| 346 | db.GetErrorErrStr(ErrStr);
|
---|
| 347 | return(false);
|
---|
| 348 | }
|
---|
| 349 | if(tbl.ISEOF()) return(true);
|
---|
| 350 | // Recorre trabajos-tareas
|
---|
| 351 | while(!tbl.ISEOF()){
|
---|
| 352 | if(!tbl.Get("idtrabajotarea",idtrabajotarea)){ // Toma dato
|
---|
| 353 | tbl.GetErrorErrStr(ErrStr); // error al acceder al registro
|
---|
| 354 | return(false);
|
---|
| 355 | }
|
---|
| 356 | tbTareasidnotificador[cont_tareas]=idtrabajotarea;
|
---|
| 357 |
|
---|
| 358 | if(!tbl.Get("idtarea",idtarea)){ // Toma dato
|
---|
| 359 | tbl.GetErrorErrStr(ErrStr); // error al acceder al registro
|
---|
| 360 | return(false);
|
---|
| 361 | }
|
---|
| 362 | tbTareasidtarea[cont_tareas]=idtarea;
|
---|
| 363 |
|
---|
| 364 | if(!tbl.Get("parametros",parametros)){ // Toma dato
|
---|
| 365 | tbl.GetErrorErrStr(ErrStr); // error al acceder al registro
|
---|
| 366 | return(false);
|
---|
| 367 | }
|
---|
| 368 | lon=strlen(parametros);
|
---|
| 369 | tbTareasparametros[cont_tareas]=(char*)malloc(lon);
|
---|
| 370 | if(tbTareasparametros[cont_tareas]==NULL)
|
---|
| 371 | return(false); // No hay memoria bastante
|
---|
| 372 | strcpy(tbTareasparametros[cont_tareas],parametros);
|
---|
| 373 |
|
---|
| 374 | if(!tbl.Get("ambitskwrk",ambitskwrk)){ // Toma dato
|
---|
| 375 | tbl.GetErrorErrStr(ErrStr); // error al acceder al registro
|
---|
| 376 | return(false);
|
---|
| 377 | }
|
---|
| 378 | lon=strlen(ambitskwrk);
|
---|
| 379 | tbTareasambitoambitskwrk[cont_tareas]=(char*)malloc(lon);
|
---|
| 380 | strcpy(tbTareasambitoambitskwrk[cont_tareas],ambitskwrk);
|
---|
| 381 |
|
---|
| 382 | sprintf(wambitrabajo,"%s;",ambitskwrk);
|
---|
| 383 | strcat(ambitrabajo,wambitrabajo);
|
---|
| 384 |
|
---|
| 385 | sprintf(wparamtrabajo,"%d;",idtrabajotarea);
|
---|
| 386 | strcat(paramtrabajo,wparamtrabajo);
|
---|
| 387 |
|
---|
| 388 | cont_tareas++;
|
---|
| 389 | tbl.MoveNext();
|
---|
| 390 | }
|
---|
| 391 | lon=strlen(ambitrabajo);
|
---|
| 392 | ambitrabajo[lon-1]=(char)NULL; // Quita la coma final
|
---|
| 393 |
|
---|
| 394 | lon=strlen(paramtrabajo);
|
---|
| 395 | paramtrabajo[lon-1]=(char)NULL; // Quita la coma final
|
---|
| 396 |
|
---|
[9182fd9] | 397 | char fechareg[100];
|
---|
| 398 |
|
---|
| 399 |
|
---|
| 400 | struct tm* st;
|
---|
| 401 | st = TomaHora();
|
---|
| 402 | sprintf(fechareg, "%d/%d/%d %d:%d:%d", st->tm_year + 1900, st->tm_mon + 1,
|
---|
| 403 | st->tm_mday, st->tm_hour, st->tm_min, st->tm_sec);
|
---|
[5ea3638] | 404 |
|
---|
[9182fd9] | 405 | 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] | 406 | if(!db.Execute(sqlstr)){ // Error al insertar
|
---|
| 407 | db.GetErrorErrStr(ErrStr);
|
---|
| 408 | return(false);
|
---|
| 409 | }
|
---|
| 410 | int accionid=0;
|
---|
[9182fd9] | 411 | // Toma identificador de la acción
|
---|
[5ea3638] | 412 | sprintf(sqlstr,"SELECT @@identity as identificador");
|
---|
| 413 | if(!db.Execute(sqlstr,tbl)){ // Error al leer
|
---|
| 414 | db.GetErrorErrStr(ErrStr);
|
---|
| 415 | return(false);
|
---|
| 416 | }
|
---|
| 417 | if(!tbl.ISEOF()){ // Si existe registro
|
---|
| 418 | if(!tbl.Get("identificador",accionid)){
|
---|
| 419 | tbl.GetErrorErrStr(ErrStr); // error al acceder al registro
|
---|
| 420 | return(false);
|
---|
| 421 | }
|
---|
| 422 | }
|
---|
| 423 | int i;
|
---|
| 424 | // Insertar acciones:tareas
|
---|
| 425 | for (i=0;i<cont_tareas;i++){
|
---|
| 426 | if(!EjecutarTarea(tbTareasidtarea[i],accionid,tbTareasidnotificador[i],idcentro,db,parametros)){
|
---|
| 427 | free(tbTareasparametros[i]);
|
---|
| 428 | free(tbTareasambitoambitskwrk[i]);
|
---|
| 429 | return(false);
|
---|
| 430 | }
|
---|
| 431 | free(tbTareasparametros[i]);
|
---|
| 432 | free(tbTareasambitoambitskwrk[i]);
|
---|
| 433 | }
|
---|
| 434 | return(true);
|
---|
| 435 | }
|
---|
| 436 | // _____________________________________________________________________________________________________________
|
---|
[9182fd9] | 437 | // Función: EjecutarTarea
|
---|
[5ea3638] | 438 | //
|
---|
[9182fd9] | 439 | // Descripción:
|
---|
| 440 | // Registra una acción (Tarea) y la envía para su ejecución
|
---|
| 441 | // Parámetros:
|
---|
[5ea3638] | 442 | // - idtarea : Identificador de la tarea
|
---|
| 443 | // - accionid: identificador del trabajo padre (si existe)
|
---|
| 444 | // - idnotificador: identificador del trabajo_tarea incluido en trabajo padre (si existe)
|
---|
| 445 | // - idcentro: Centro propietario del trabjo padre (si existe este trabajo)
|
---|
[9182fd9] | 446 | // - db: una conexion ADO operativa
|
---|
| 447 | // - parametros: Parámetros de la acción
|
---|
[5ea3638] | 448 | // _____________________________________________________________________________________________________________
|
---|
| 449 | int EjecutarTarea(int idtarea,int accionid,int idnotificador,int idcentro,Database db,char *parametros )
|
---|
| 450 | {
|
---|
| 451 | char sqlstr[1000],ErrStr[200],ambito;
|
---|
| 452 | Table tbl;
|
---|
| 453 | int cont_comandos=0,lon;
|
---|
| 454 | int idcomando,idambito,idtareacomando,accionidcmd;
|
---|
| 455 | char wambitarea[20],ambitarea[4000];
|
---|
| 456 | char wparamtarea[20],paramtarea[1000],pids[20];
|
---|
| 457 | int tbComandosidcomando[100],tbComandosambito[100],tbComandosidnotificador[100],tbComandosidambito[100];
|
---|
| 458 | char *tbComandosparametros[100];
|
---|
| 459 |
|
---|
[9182fd9] | 460 | ambitarea[0]=(char)NULL; // Inicialización
|
---|
| 461 | strcpy(paramtarea,"cmd="); // Inicialización
|
---|
[5ea3638] | 462 | if(idcentro==0){
|
---|
| 463 | // recupera el identificador del Centro propietario de la tarea
|
---|
| 464 | sprintf(sqlstr,"SELECT idcentro FROM tareas WHERE idtarea=%d",idtarea);
|
---|
| 465 | if(!db.Execute(sqlstr,tbl)){ // Error al leer
|
---|
| 466 | db.GetErrorErrStr(ErrStr);
|
---|
| 467 | return(false);
|
---|
| 468 | }
|
---|
| 469 | if(tbl.ISEOF()) return(true);
|
---|
| 470 | if(!tbl.Get("idcentro",idcentro)){ // Toma dato
|
---|
| 471 | tbl.GetErrorErrStr(ErrStr); // error al acceder al registro
|
---|
| 472 | return(false);
|
---|
| 473 | }
|
---|
| 474 | }
|
---|
| 475 | // Recupera los comandos que forman parte de la tarea
|
---|
| 476 | sprintf(sqlstr,"SELECT * FROM tareas_comandos WHERE idtarea=%d ORDER by orden",idtarea);
|
---|
| 477 | if(!db.Execute(sqlstr,tbl)){ // Error al leer
|
---|
| 478 | db.GetErrorErrStr(ErrStr);
|
---|
| 479 | return(false);
|
---|
| 480 | }
|
---|
| 481 | if(tbl.ISEOF()) return(true);
|
---|
| 482 |
|
---|
| 483 | // Recorre tareas-comandos
|
---|
| 484 | while(!tbl.ISEOF()){
|
---|
| 485 | if(!tbl.Get("idcomando",idcomando)){ // Toma dato
|
---|
| 486 | tbl.GetErrorErrStr(ErrStr); // error al acceder al registro
|
---|
| 487 | return(false);
|
---|
| 488 | }
|
---|
| 489 | tbComandosidcomando[cont_comandos]=idcomando;
|
---|
| 490 |
|
---|
| 491 | if(!tbl.Get("ambito",ambito)){ // Toma dato
|
---|
| 492 | tbl.GetErrorErrStr(ErrStr); // error al acceder al registro
|
---|
| 493 | return(false);
|
---|
| 494 | }
|
---|
| 495 | tbComandosambito[cont_comandos]=ambito;
|
---|
| 496 |
|
---|
| 497 | if(!tbl.Get("idambito",idambito)){ // Toma dato
|
---|
| 498 | tbl.GetErrorErrStr(ErrStr); // error al acceder al registro
|
---|
| 499 | return(false);
|
---|
| 500 | }
|
---|
| 501 | tbComandosidambito[cont_comandos]=idambito;
|
---|
| 502 |
|
---|
| 503 |
|
---|
| 504 | if(!tbl.Get("parametros",parametros)){ // Toma dato
|
---|
| 505 | tbl.GetErrorErrStr(ErrStr); // error al acceder al registro
|
---|
| 506 | return(false);
|
---|
| 507 | }
|
---|
| 508 |
|
---|
| 509 | lon=strlen(parametros);
|
---|
| 510 | tbComandosparametros[cont_comandos]=(char*)malloc(lon+20);
|
---|
| 511 | if(tbComandosparametros[cont_comandos]==NULL)
|
---|
| 512 | return(false); // No hay memoria bastante
|
---|
| 513 |
|
---|
| 514 | strcpy(tbComandosparametros[cont_comandos],parametros);
|
---|
| 515 |
|
---|
| 516 | if(!tbl.Get("idtareacomando",idtareacomando)){ // Toma dato
|
---|
| 517 | tbl.GetErrorErrStr(ErrStr); // error al acceder al registro
|
---|
| 518 | return(false);
|
---|
| 519 | }
|
---|
| 520 | tbComandosidnotificador[cont_comandos]=idtareacomando;
|
---|
| 521 |
|
---|
| 522 | sprintf(wambitarea,"%d:%d;",ambito,idambito);
|
---|
| 523 | strcat(ambitarea,wambitarea);
|
---|
| 524 |
|
---|
| 525 | sprintf(wparamtarea,"%d;",idtareacomando);
|
---|
| 526 | strcat(paramtarea,wparamtarea);
|
---|
| 527 |
|
---|
| 528 | cont_comandos++;
|
---|
| 529 | tbl.MoveNext();
|
---|
| 530 | }
|
---|
| 531 | lon=strlen(ambitarea);
|
---|
| 532 | ambitarea[lon-1]=(char)NULL; // Quita la coma final
|
---|
| 533 |
|
---|
| 534 | lon=strlen(paramtarea);
|
---|
| 535 | paramtarea[lon-1]=(char)NULL; // Quita la coma final
|
---|
| 536 |
|
---|
[9182fd9] | 537 | char fechareg[100];
|
---|
[5ea3638] | 538 |
|
---|
[9182fd9] | 539 |
|
---|
| 540 | struct tm* st;
|
---|
| 541 | st = TomaHora();
|
---|
| 542 | sprintf(fechareg, "%d/%d/%d %d:%d:%d", st->tm_year + 1900, st->tm_mon + 1,
|
---|
| 543 | st->tm_mday, st->tm_hour, st->tm_min, st->tm_sec);
|
---|
| 544 |
|
---|
| 545 | 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] | 546 | if(!db.Execute(sqlstr)){ // Error al insertar
|
---|
| 547 | db.GetErrorErrStr(ErrStr);
|
---|
| 548 | return(false);
|
---|
| 549 | }
|
---|
| 550 | accionid=0;
|
---|
[9182fd9] | 551 | // Toma identificador de la acción
|
---|
[5ea3638] | 552 | sprintf(sqlstr,"SELECT @@identity as identificador");
|
---|
| 553 | if(!db.Execute(sqlstr,tbl)){ // Error al leer
|
---|
| 554 | db.GetErrorErrStr(ErrStr);
|
---|
| 555 | return(false);
|
---|
| 556 | }
|
---|
| 557 | if(!tbl.ISEOF()){ // Si existe registro
|
---|
| 558 | if(!tbl.Get("identificador",accionid)){
|
---|
| 559 | tbl.GetErrorErrStr(ErrStr); // error al acceder al registro
|
---|
| 560 | return(false);
|
---|
| 561 | }
|
---|
| 562 | }
|
---|
| 563 | int i;
|
---|
| 564 | // Insertar acciones:comandos
|
---|
| 565 | for (i=0;i<cont_comandos;i++){
|
---|
[9182fd9] | 566 | st = TomaHora();
|
---|
| 567 | sprintf(fechareg, "%d/%d/%d %d:%d:%d", st->tm_year + 1900, st->tm_mon
|
---|
| 568 | + 1, st->tm_mday, st->tm_hour, st->tm_min, st->tm_sec);
|
---|
| 569 |
|
---|
| 570 | 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] | 571 | if(!db.Execute(sqlstr)){ // Error al insertar
|
---|
| 572 | db.GetErrorErrStr(ErrStr);
|
---|
| 573 | free(tbComandosparametros[i]);
|
---|
| 574 | return(false);
|
---|
| 575 | }
|
---|
| 576 |
|
---|
[9182fd9] | 577 | // Toma identificador dela acción
|
---|
[5ea3638] | 578 | sprintf(sqlstr,"SELECT @@identity as identificador");
|
---|
| 579 | if(!db.Execute(sqlstr,tbl)){ // Error al leer
|
---|
| 580 | db.GetErrorErrStr(ErrStr);
|
---|
| 581 | return(false);
|
---|
| 582 | }
|
---|
| 583 |
|
---|
| 584 | if(!tbl.ISEOF()){ // Si existe registro
|
---|
| 585 | if(!tbl.Get("identificador",accionidcmd)){
|
---|
| 586 | tbl.GetErrorErrStr(ErrStr); // error al acceder al registro
|
---|
| 587 | return(false);
|
---|
| 588 | }
|
---|
| 589 | }
|
---|
| 590 | sprintf(pids,"ids=%d\r",accionidcmd);
|
---|
[9182fd9] | 591 | strcat(tbComandosparametros[i],pids); // Le añade el identificador de la acción
|
---|
[5ea3638] | 592 | envia_comando(tbComandosparametros[i]);
|
---|
| 593 | free(tbComandosparametros[i]);
|
---|
| 594 | }
|
---|
| 595 | return(true);
|
---|
| 596 | }
|
---|
| 597 | // _____________________________________________________________________________________________________________
|
---|
[9182fd9] | 598 | // Función: EjecutarReserva
|
---|
[5ea3638] | 599 | //
|
---|
[9182fd9] | 600 | // Descripción:
|
---|
| 601 | // Registra una acción (Tarea) y la envía para su ejecución
|
---|
| 602 | // Parámetros:
|
---|
[5ea3638] | 603 | // - idreserva : Identificador de la reserva
|
---|
[9182fd9] | 604 | // - db: una conexion ADO operativa
|
---|
| 605 | // - parametros: Parámetros de la acción
|
---|
[5ea3638] | 606 | // _____________________________________________________________________________________________________________
|
---|
| 607 | int EjecutarReserva(int idreserva,Database db,char*parametros )
|
---|
| 608 | {
|
---|
| 609 | char sqlstr[1000],ErrStr[200];
|
---|
| 610 | Table tbl;
|
---|
| 611 | int idaccion;
|
---|
| 612 |
|
---|
| 613 | sprintf(sqlstr,"SELECT idtarea,idtrabajo FROM reservas WHERE idreserva=%d",idreserva);
|
---|
| 614 | if(!db.Execute(sqlstr,tbl)){ // Error al leer
|
---|
| 615 | db.GetErrorErrStr(ErrStr);
|
---|
| 616 | return(false);
|
---|
| 617 | }
|
---|
| 618 | if(tbl.ISEOF()){
|
---|
| 619 | return(false); // No hay acciones previas en la reserva
|
---|
| 620 | }
|
---|
| 621 |
|
---|
| 622 | if(!tbl.Get("idtarea",idaccion)){ // Toma dato
|
---|
| 623 | tbl.GetErrorErrStr(ErrStr); // error al acceder al registro
|
---|
| 624 | return(false);
|
---|
| 625 | }
|
---|
| 626 | if(idaccion>0)
|
---|
| 627 | EjecutarTarea(idaccion,0,0,0,db,parametros); // Es una reserva con tarea previa
|
---|
| 628 |
|
---|
| 629 | if(!tbl.Get("idtrabajo",idaccion)){ // Toma dato
|
---|
| 630 | tbl.GetErrorErrStr(ErrStr); // error al acceder al registro
|
---|
| 631 | return(false);
|
---|
| 632 | }
|
---|
| 633 | if(idaccion>0)
|
---|
| 634 | EjecutarTrabajo(idaccion,db,parametros); // Es una reserva con trabajo previo
|
---|
| 635 |
|
---|
| 636 | return(true);
|
---|
| 637 | }
|
---|
| 638 | // _____________________________________________________________________________________________________________
|
---|
[9182fd9] | 639 | // Función: envia_comando
|
---|
[5ea3638] | 640 | //
|
---|
[9182fd9] | 641 | // Descripción:
|
---|
| 642 | // Envía un comando a la red. Para ello es necesario tener iniciado el servicio hidra.
|
---|
| 643 | // Parámetros:
|
---|
| 644 | // - parametros: Parámetros del comando
|
---|
[5ea3638] | 645 | // _____________________________________________________________________________________________________________
|
---|
| 646 | int envia_comando(char* parametros)
|
---|
| 647 | {
|
---|
| 648 | SOCKET sClient;
|
---|
| 649 | TRAMA trama;
|
---|
| 650 |
|
---|
| 651 | sClient = AbreConexion(servidorhidra,puerto);
|
---|
| 652 | if (sClient == (SOCKET)NULL)
|
---|
| 653 | return(FALSE);
|
---|
| 654 |
|
---|
| 655 | trama.arroba='@';
|
---|
| 656 | strncpy(trama.identificador,"JMMLCAMDJ",9);
|
---|
| 657 | trama.ejecutor=parametros[0];
|
---|
| 658 | strcpy(trama.parametros,(char*)¶metros[1]);
|
---|
| 659 | return(manda_trama(sClient,&trama));
|
---|
| 660 | }
|
---|
| 661 |
|
---|
| 662 | /// _____________________________________________________________________________________________________________
|
---|
[9182fd9] | 663 | // Función: manda_trama
|
---|
[5ea3638] | 664 | //
|
---|
[9182fd9] | 665 | // Descripción:
|
---|
| 666 | // Esta Función envía una trama por la red (TCP)
|
---|
| 667 | // Parámetros:
|
---|
[5ea3638] | 668 | // - sock : El socket del host al que se dirige la trama
|
---|
| 669 | // - trama: El contenido de la trama
|
---|
| 670 | /// _____________________________________________________________________________________________________________
|
---|
| 671 | int manda_trama(SOCKET sock,TRAMA* trama)
|
---|
| 672 | {
|
---|
| 673 | int nLeft,idx,ret;
|
---|
| 674 |
|
---|
| 675 | Encriptar((char*)trama);
|
---|
| 676 | nLeft = strlen((char*)trama);
|
---|
| 677 | idx = 0;
|
---|
| 678 | while(nLeft > 0){
|
---|
| 679 | ret = send(sock,(char*)&trama[idx], nLeft, 0);
|
---|
| 680 | if (ret == 0)
|
---|
| 681 | break;
|
---|
| 682 | else
|
---|
| 683 | if (ret == SOCKET_ERROR){
|
---|
| 684 | RegistraLog("***AGENT***send() fallo al enviar trama:",true);
|
---|
| 685 | return(FALSE);
|
---|
| 686 | }
|
---|
| 687 | nLeft -= ret;
|
---|
| 688 | idx += ret;
|
---|
| 689 | }
|
---|
| 690 | return(TRUE);
|
---|
| 691 | }
|
---|
| 692 | //******************************************************************************************************************************************
|
---|
[9182fd9] | 693 | // PROGRAMA PRINCIPAL
|
---|
[5ea3638] | 694 | //******************************************************************************************************************************************
|
---|
[9182fd9] | 695 | int main(int argc, char *argv[]) {
|
---|
| 696 |
|
---|
| 697 | struct tm* st;
|
---|
| 698 |
|
---|
| 699 | strcpy(szPathFileCfg, "ogAdmAgent.cfg");
|
---|
| 700 | strcpy(szPathFileLog, "ogAdmAgent.log");
|
---|
| 701 | int i;
|
---|
| 702 | for (i = 1; (i + 1) < argc; i += 2) {
|
---|
| 703 | if (argv[i][0] == '-') {
|
---|
| 704 | switch (tolower(argv[i][1])) {
|
---|
| 705 | case 'f':
|
---|
| 706 | if (argv[i + 1] != NULL)
|
---|
| 707 | strcpy(szPathFileCfg, argv[i + 1]);
|
---|
| 708 | else {
|
---|
| 709 | RegistraLog(
|
---|
| 710 | "Fallo en los parámetros: Debe especificar el fichero de configuración del servicio",
|
---|
| 711 | false);
|
---|
| 712 | exit(EXIT_FAILURE);
|
---|
| 713 | }
|
---|
| 714 | break;
|
---|
| 715 | case 'l':
|
---|
| 716 | if (argv[i + 1] != NULL)
|
---|
| 717 | strcpy(szPathFileLog, argv[i + 1]);
|
---|
| 718 | else {
|
---|
| 719 | RegistraLog(
|
---|
| 720 | "Fallo en los parámetros: Debe especificar el fichero de log para el servicio",
|
---|
| 721 | false);
|
---|
| 722 | exit(EXIT_FAILURE);
|
---|
| 723 | }
|
---|
| 724 | break;
|
---|
| 725 | default:
|
---|
| 726 | RegistraLog(
|
---|
| 727 | "Fallo de sintaxis en los parámetros: Debe especificar -f nombre_del_fichero_de_configuración_del_servicio",
|
---|
| 728 | false);
|
---|
| 729 | exit(EXIT_FAILURE);
|
---|
| 730 | break;
|
---|
| 731 | }
|
---|
| 732 | }
|
---|
| 733 | }
|
---|
| 734 | if (szPathFileCfg == NULL) {
|
---|
| 735 | printf("***Error. No se ha especificado fichero de configuración\n");
|
---|
| 736 | exit(EXIT_FAILURE);
|
---|
| 737 | }
|
---|
| 738 | if (!TomaConfiguracion(szPathFileCfg)) { // Toma parametros de configuración
|
---|
| 739 | RegistraLog(
|
---|
| 740 | "El fichero de configuración contiene un error de sintaxis",
|
---|
| 741 | false);
|
---|
| 742 | exit(EXIT_FAILURE);
|
---|
| 743 | }
|
---|
[5ea3638] | 744 |
|
---|
[9182fd9] | 745 | int pseg;
|
---|
[5ea3638] | 746 | while (TRUE){
|
---|
[9182fd9] | 747 | st = TomaHora();
|
---|
| 748 | //pseg=1000*(65-st->tm_sec); // Calcula milisegundos de inactividad de la hebra
|
---|
| 749 | pseg=65-st->tm_sec; // Calcula segundos de inactividad de la hebra
|
---|
| 750 | sleep(pseg);
|
---|
| 751 |
|
---|
[5ea3638] | 752 | // Toma la hora
|
---|
[9182fd9] | 753 | st = TomaHora();
|
---|
[231f87d] | 754 | busca_accion(st->tm_mday,st->tm_mon+1,st->tm_year+1900,st->tm_hour,st->tm_min,st->tm_wday );
|
---|
[5ea3638] | 755 | }
|
---|
[9182fd9] | 756 | }
|
---|
[5ea3638] | 757 |
|
---|