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