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