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: ogAdmServer.php
|
---|
7 | // Descripción :
|
---|
8 | //
|
---|
9 | // ****************************************************************************
|
---|
10 | #include "ogAdmAgent.h"
|
---|
11 | #include "ogAdmLib.c"
|
---|
12 | // _____________________________________________________________________________________________________________
|
---|
13 | // Función: inicializa
|
---|
14 | //
|
---|
15 | // Descripción:
|
---|
16 | // Esta función nicializa variables
|
---|
17 | // _____________________________________________________________________________________________________________
|
---|
18 | void inicializa()
|
---|
19 | {
|
---|
20 | dias_meses[1]=31;
|
---|
21 | dias_meses[2]=28;
|
---|
22 | dias_meses[3]=31;
|
---|
23 | dias_meses[4]=30;
|
---|
24 | dias_meses[5]=31;
|
---|
25 | dias_meses[6]=30;
|
---|
26 | dias_meses[7]=31;
|
---|
27 | dias_meses[8]=31;
|
---|
28 | dias_meses[9]=30;
|
---|
29 | dias_meses[10]=31;
|
---|
30 | dias_meses[11]=30;
|
---|
31 | dias_meses[12]=31;
|
---|
32 |
|
---|
33 | // años tamaño 1 bytes
|
---|
34 | HEX_annos[1]=0x01;
|
---|
35 | HEX_annos[2]=0x02;
|
---|
36 | HEX_annos[3]=0x04;
|
---|
37 | HEX_annos[4]=0x08;
|
---|
38 | HEX_annos[5]=0x10;
|
---|
39 | HEX_annos[6]=0x20;
|
---|
40 | HEX_annos[7]=0x40;
|
---|
41 | HEX_annos[8]=0x80;
|
---|
42 |
|
---|
43 | // mese tamaño 2 bytes
|
---|
44 | HEX_meses[1]=0x0001;
|
---|
45 | HEX_meses[2]=0x0002;
|
---|
46 | HEX_meses[3]=0x0004;
|
---|
47 | HEX_meses[4]=0x0008;
|
---|
48 | HEX_meses[5]=0x0010;
|
---|
49 | HEX_meses[6]=0x0020;
|
---|
50 | HEX_meses[7]=0x0040;
|
---|
51 | HEX_meses[8]=0x0080;
|
---|
52 | HEX_meses[9]=0x0100;
|
---|
53 | HEX_meses[10]=0x0200;
|
---|
54 | HEX_meses[11]=0x0400;
|
---|
55 | HEX_meses[12]=0x0800;
|
---|
56 |
|
---|
57 | // dias tamaño 4 bytes
|
---|
58 | HEX_dias[1]=0x00000001;
|
---|
59 | HEX_dias[2]=0x00000002;
|
---|
60 | HEX_dias[3]=0x00000004;
|
---|
61 | HEX_dias[4]=0x00000008;
|
---|
62 | HEX_dias[5]=0x00000010;
|
---|
63 | HEX_dias[6]=0x00000020;
|
---|
64 | HEX_dias[7]=0x00000040;
|
---|
65 | HEX_dias[8]=0x00000080;
|
---|
66 | HEX_dias[9]=0x00000100;
|
---|
67 | HEX_dias[10]=0x00000200;
|
---|
68 | HEX_dias[11]=0x00000400;
|
---|
69 | HEX_dias[12]=0x00000800;
|
---|
70 | HEX_dias[13]=0x00001000;
|
---|
71 | HEX_dias[14]=0x00002000;
|
---|
72 | HEX_dias[15]=0x00004000;
|
---|
73 | HEX_dias[16]=0x00008000;
|
---|
74 | HEX_dias[17]=0x00010000;
|
---|
75 | HEX_dias[18]=0x00020000;
|
---|
76 | HEX_dias[19]=0x00040000;
|
---|
77 | HEX_dias[20]=0x00080000;
|
---|
78 | HEX_dias[21]=0x00100000;
|
---|
79 | HEX_dias[22]=0x00200000;
|
---|
80 | HEX_dias[23]=0x00400000;
|
---|
81 | HEX_dias[24]=0x00800000;
|
---|
82 | HEX_dias[25]=0x01000000;
|
---|
83 | HEX_dias[26]=0x02000000;
|
---|
84 | HEX_dias[27]=0x04000000;
|
---|
85 | HEX_dias[28]=0x08000000;
|
---|
86 | HEX_dias[29]=0x10000000;
|
---|
87 | HEX_dias[30]=0x20000000;
|
---|
88 | HEX_dias[31]=0x40000000;
|
---|
89 |
|
---|
90 | // horas tamaño 2 bytes
|
---|
91 | HEX_horas[0]=0x0001;
|
---|
92 | HEX_horas[1]=0x0002;
|
---|
93 | HEX_horas[2]=0x0004;
|
---|
94 | HEX_horas[3]=0x0008;
|
---|
95 |
|
---|
96 | HEX_horas[4]=0x0010;
|
---|
97 | HEX_horas[5]=0x0020;
|
---|
98 | HEX_horas[6]=0x0040;
|
---|
99 | HEX_horas[7]=0x0080;
|
---|
100 |
|
---|
101 | HEX_horas[8]=0x0100;
|
---|
102 | HEX_horas[9]=0x0200;
|
---|
103 | HEX_horas[10]=0x0400;
|
---|
104 | HEX_horas[11]=0x0800;
|
---|
105 |
|
---|
106 | // dia de la semana (L,M,X...) tamaño 1 bytes
|
---|
107 | HEX_diasemana[1]=0x01;
|
---|
108 | HEX_diasemana[2]=0x02;
|
---|
109 | HEX_diasemana[3]=0x04;
|
---|
110 | HEX_diasemana[4]=0x08;
|
---|
111 | HEX_diasemana[5]=0x10;
|
---|
112 | HEX_diasemana[6]=0x20;
|
---|
113 | HEX_diasemana[7]=0x40;
|
---|
114 |
|
---|
115 | // semana tamaño 1 bytes
|
---|
116 | HEX_semanas[1]=0x01;
|
---|
117 | HEX_semanas[2]=0x02;
|
---|
118 | HEX_semanas[3]=0x04;
|
---|
119 | HEX_semanas[4]=0x08;
|
---|
120 | HEX_semanas[5]=0x10;
|
---|
121 | HEX_semanas[6]=0x20;
|
---|
122 | }
|
---|
123 | // _____________________________________________________________________________________________________________
|
---|
124 | // Función: RegistraLog
|
---|
125 | //
|
---|
126 | // Descripción:
|
---|
127 | // Esta función registra los evento de errores en un fichero log
|
---|
128 | // Parametros:
|
---|
129 | // - msg : Mensage de error
|
---|
130 | // - swerrno: Switch que indica que recupere literal de error del sistema
|
---|
131 | // _____________________________________________________________________________________________________________
|
---|
132 | void RegistraLog(char *msg,int swerrno)
|
---|
133 | {
|
---|
134 | time_t rawtime;
|
---|
135 | struct tm * timeinfo;
|
---|
136 | char MsgHerror[1000];
|
---|
137 |
|
---|
138 | time ( &rawtime );
|
---|
139 | timeinfo = gmtime(&rawtime);
|
---|
140 |
|
---|
141 | FLog=fopen( "hidraagent.log","at");
|
---|
142 | if(swerrno){
|
---|
143 | // fprintf (FLog,"%02d/%02d/%d %02d:%02d ***%s:%s\n",timeinfo->tm_mday,timeinfo->tm_mon+1,timeinfo->tm_year,timeinfo->tm_hour,timeinfo->tm_min,msg,strerror(errno));
|
---|
144 | sprintf (MsgHerror,"%02d/%02d/%d %02d:%02d ***%s:%d\n",timeinfo->tm_mday,timeinfo->tm_mon+1,timeinfo->tm_year,timeinfo->tm_hour,timeinfo->tm_min,msg,WSAGetLastError());
|
---|
145 | }
|
---|
146 | else{
|
---|
147 | sprintf (MsgHerror,"%02d/%02d/%d %02d:%02d ***%s\n",timeinfo->tm_mday,timeinfo->tm_mon+1,timeinfo->tm_year+1900,timeinfo->tm_hour,timeinfo->tm_min,msg);
|
---|
148 | }
|
---|
149 | fprintf (FLog,MsgHerror);
|
---|
150 | AddToMessageLog((LPTSTR)MsgHerror);
|
---|
151 | fclose(FLog);
|
---|
152 | }
|
---|
153 | // _____________________________________________________________________________________________________________
|
---|
154 | // Función: TomaParametrosReg
|
---|
155 | //
|
---|
156 | // Descripción:
|
---|
157 | // Esta función toma los parámetros de conexión del registro
|
---|
158 | // _____________________________________________________________________________________________________________
|
---|
159 | int TomaParametrosReg()
|
---|
160 | {
|
---|
161 | if(!ReadRegistryString(HIVE,BASE,"servidorhidra",servidorhidra,20))
|
---|
162 | strcpy(servidorhidra,SERVIDORHIDRA);
|
---|
163 |
|
---|
164 | if(!ReadRegistryInteger(HIVE,BASE,"puerto",(DWORD *)&puerto))
|
---|
165 | puerto=PUERTO_DEFAULT;
|
---|
166 |
|
---|
167 | if(!ReadRegistryString(HIVE,BASE,"usuario",usuario,20))
|
---|
168 | strcpy(usuario,USUARIO);
|
---|
169 | Desencriptar(usuario);
|
---|
170 |
|
---|
171 | if(!ReadRegistryString(HIVE,BASE,"pasguor",pasguor,20))
|
---|
172 | strcpy(pasguor,PASGUOR);
|
---|
173 | Desencriptar(pasguor);
|
---|
174 |
|
---|
175 | if(!ReadRegistryString(HIVE,BASE,"datasource",datasource,20))
|
---|
176 | strcpy(datasource,DATASOURCE);
|
---|
177 |
|
---|
178 | if(!ReadRegistryString(HIVE,BASE,"catalog",catalog,50))
|
---|
179 | strcpy(catalog,CATALOG);
|
---|
180 |
|
---|
181 | sprintf(cadenaconexion,CADENACONEXION,catalog,datasource); // Crea cadena de conexión
|
---|
182 |
|
---|
183 | return(TRUE);
|
---|
184 | }
|
---|
185 | // _____________________________________________________________________________________________________________
|
---|
186 | //
|
---|
187 | // Función: GestionaProgramacion
|
---|
188 | //
|
---|
189 | // Descripción:
|
---|
190 | // Esta función es la encargada de leer la base de datos y comprobar si existe alguna acción o reserva programada
|
---|
191 | // Parametros:
|
---|
192 | // - pst : Estructura con la configuración de fecha y hora del sistema
|
---|
193 | // _____________________________________________________________________________________________________________
|
---|
194 | int GestionaProgramacion(SYSTEMTIME pst){
|
---|
195 | busca_accion(pst.wDay,pst.wMonth,pst.wYear,pst.wHour,pst.wMinute,pst.wDayOfWeek );
|
---|
196 | return(0);
|
---|
197 | }
|
---|
198 | // _____________________________________________________________________________________________________________
|
---|
199 | // Función: busca_accion
|
---|
200 | //
|
---|
201 | // Descripción:
|
---|
202 | // Esta función busca en la base de datos,acciones programadas
|
---|
203 | // Parametros:
|
---|
204 | // - dia : Dia actual del mes
|
---|
205 | // - mes : mes en curso
|
---|
206 | // - anno : Año en curso
|
---|
207 | // - hora : Hora actual
|
---|
208 | // - minutos : Minutos actuales
|
---|
209 | // - diasemana : Dia de la semana 1=lunes,2=martes ... ( 0 Domingo)
|
---|
210 | // _____________________________________________________________________________________________________________
|
---|
211 | int busca_accion(WORD dia,WORD mes,WORD anno,WORD hora,WORD minutos,WORD diasemana)
|
---|
212 | {
|
---|
213 | char sqlstr[1000],ErrStr[200];
|
---|
214 | Database db,wdb;
|
---|
215 | Table tbl;
|
---|
216 | char parametros[LONGITUD_PARAMETROS];
|
---|
217 | BYTE swampm,bitsemana;
|
---|
218 | int tipoaccion,identificador;
|
---|
219 | int ordsem,ordulsem,ordiasem_1,maxdias;
|
---|
220 | anno=anno-2003; // Año de comienzo es 2004
|
---|
221 | if(hora>11){
|
---|
222 | hora-=12;
|
---|
223 | swampm=1; // Es pm
|
---|
224 | }
|
---|
225 | else
|
---|
226 | swampm=0; // Es am
|
---|
227 |
|
---|
228 | if(diasemana==0) diasemana=7; // El domingo
|
---|
229 |
|
---|
230 | // Cuestion semanas
|
---|
231 | ordiasem_1=DiadelaSemana(1,mes,anno+2003);
|
---|
232 | ordsem=SemanadelMes(ordiasem_1,dia); // Calcula el numero de la semana
|
---|
233 | if (mes!=2) // Toma el ultimo dia de ese mes
|
---|
234 | maxdias=dias_meses[mes];
|
---|
235 | else{
|
---|
236 | if (bisiesto(anno+2003))
|
---|
237 | maxdias=29;
|
---|
238 | else
|
---|
239 | maxdias=28;
|
---|
240 | }
|
---|
241 | ordulsem=SemanadelMes(ordiasem_1,maxdias); // Calcula el numero de ultima semana
|
---|
242 |
|
---|
243 | bitsemana=HEX_semanas[ordsem];
|
---|
244 | if(ordsem==ordulsem) // Si es la ultima semana del mes
|
---|
245 | bitsemana|=HEX_semanas[6];
|
---|
246 |
|
---|
247 | if(!db.Open(usuario,pasguor,cadenaconexion)){ // error de conexion
|
---|
248 | db.GetErrorErrStr(ErrStr);
|
---|
249 | return(false);
|
---|
250 | }
|
---|
251 | sprintf(sqlstr,"SELECT DISTINCT tipoaccion,identificador FROM programaciones WHERE suspendida=0 AND (annos & %d <> 0) AND (meses & %d<>0) AND ((diario & %d<>0) OR (dias & %d<>0) OR (semanas & %d<>0)) AND (horas & %d<>0) AND ampm=%d AND minutos=%d",HEX_annos[anno],HEX_meses[mes],HEX_dias[dia],HEX_diasemana[diasemana],bitsemana,HEX_horas[hora],swampm,minutos);
|
---|
252 | if(!db.Execute(sqlstr,tbl)){ // Error al leer
|
---|
253 | db.GetErrorErrStr(ErrStr);
|
---|
254 | return(false);
|
---|
255 | }
|
---|
256 | if(tbl.ISEOF()){
|
---|
257 | return(true); // No hay acciones programadas
|
---|
258 | }
|
---|
259 | if(!wdb.Open(usuario,pasguor,cadenaconexion)){ // error de conexion
|
---|
260 | wdb.GetErrorErrStr(ErrStr);
|
---|
261 | return(false);
|
---|
262 | }
|
---|
263 | while(!tbl.ISEOF()){ // Busca entre todas las programaciones
|
---|
264 | if(!tbl.Get("tipoaccion",tipoaccion)){ // Toma dato
|
---|
265 | tbl.GetErrorErrStr(ErrStr); // error al acceder al registro
|
---|
266 | return(false);
|
---|
267 | }
|
---|
268 | if(!tbl.Get("identificador",identificador)){ // Toma dato
|
---|
269 | tbl.GetErrorErrStr(ErrStr); // error al acceder al registro
|
---|
270 | return(false);
|
---|
271 | }
|
---|
272 | if(tipoaccion==EJECUCION_TAREA){ // Es una programación de una tarea
|
---|
273 | EjecutarTarea(identificador,0,0,0,wdb,parametros);
|
---|
274 | }
|
---|
275 | else{
|
---|
276 | if(tipoaccion==EJECUCION_TRABAJO){
|
---|
277 | EjecutarTrabajo(identificador,wdb,parametros); // Es una programación de un trabajo
|
---|
278 | }
|
---|
279 | else{
|
---|
280 | if(tipoaccion==EJECUCION_RESERVA){
|
---|
281 | EjecutarReserva(identificador,wdb,parametros); // Es una programación de un trabajo
|
---|
282 | }
|
---|
283 | }
|
---|
284 | }
|
---|
285 | tbl.MoveNext();
|
---|
286 | }
|
---|
287 | return(true);
|
---|
288 | }
|
---|
289 | // _____________________________________________________________________________________________________________
|
---|
290 | // Función: bisiesto
|
---|
291 | //
|
---|
292 | // Descripción:
|
---|
293 | // Esta función devuelve true si el año pasado como parámetro es bisiesto y false si no lo es
|
---|
294 | // Parametros:
|
---|
295 | // - anob : un año en formato aaaa
|
---|
296 | // _____________________________________________________________________________________________________________
|
---|
297 | bool bisiesto(WORD anob){
|
---|
298 | return(anob%4==0);
|
---|
299 | }
|
---|
300 | // _____________________________________________________________________________________________________________
|
---|
301 | // Función: DiadelaSemana
|
---|
302 | //
|
---|
303 | // Descripción:
|
---|
304 | // Esta función devuelve el número del día de la semana: 1=Lunes, 2=mártes ... 6=sábado 7=domingo de una fecha determinada
|
---|
305 | // Parametros:
|
---|
306 | // - dia : Un dia
|
---|
307 | // - mes : Un mes
|
---|
308 | // - anno : Un año
|
---|
309 | // _____________________________________________________________________________________________________________
|
---|
310 | int DiadelaSemana(WORD dia,WORD mes,WORD anno)
|
---|
311 | {
|
---|
312 | int i,cont,dias_anuales;
|
---|
313 | int desplazamiento_dias=6;
|
---|
314 | int orddiasem;
|
---|
315 |
|
---|
316 | cont =0;
|
---|
317 | for (i=1900;i<anno;i++){
|
---|
318 | if (bisiesto(i)) dias_anuales=366; else dias_anuales=365;
|
---|
319 | cont+=dias_anuales;
|
---|
320 | }
|
---|
321 | for (i=1;i<mes;i++){
|
---|
322 | if (i!=2)
|
---|
323 | cont+=dias_meses[i];
|
---|
324 | else{
|
---|
325 | if (bisiesto(anno))
|
---|
326 | cont+=29;
|
---|
327 | else
|
---|
328 | cont+=28;
|
---|
329 | }
|
---|
330 | }
|
---|
331 | cont+=dia+desplazamiento_dias;
|
---|
332 | orddiasem=(cont%7);
|
---|
333 | if(orddiasem==0) orddiasem=7;
|
---|
334 | return(orddiasem);
|
---|
335 | }
|
---|
336 | // _____________________________________________________________________________________________________________
|
---|
337 | // Función: DiadelaSemana
|
---|
338 | //
|
---|
339 | // Descripción:
|
---|
340 | // Esta función devuelve el número de semana perteneciente a un día de ese mes
|
---|
341 | // Parametros:
|
---|
342 | // - ordiasem_1 : Orden semenal (1,2...) del dia del primer dia del mes que se pasa como parámetro
|
---|
343 | // - diames : El mes concreto
|
---|
344 | // _____________________________________________________________________________________________________________
|
---|
345 | int SemanadelMes(int ordiasem_1,int diames)
|
---|
346 | {
|
---|
347 | int nwdia,resto,cociente;
|
---|
348 |
|
---|
349 | nwdia=diames+ordiasem_1-1;
|
---|
350 | cociente=nwdia/7;
|
---|
351 | resto=nwdia%7;
|
---|
352 | if(resto>0) cociente++;
|
---|
353 | return(cociente);
|
---|
354 | }
|
---|
355 | // _____________________________________________________________________________________________________________
|
---|
356 | // Función: Pausa
|
---|
357 | //
|
---|
358 | // Descripción:
|
---|
359 | // Hace una pausa en segundos
|
---|
360 | // Parametros:
|
---|
361 | // - s : Segundos de pausa
|
---|
362 | // _____________________________________________________________________________________________________________
|
---|
363 | void Pausa(int s)
|
---|
364 | {
|
---|
365 | int seg=0;
|
---|
366 | clock_t comienzo;
|
---|
367 |
|
---|
368 | comienzo = clock();
|
---|
369 | do{
|
---|
370 | seg=(clock()-comienzo)/CLOCKS_PER_SEC;
|
---|
371 | }while(seg<s);
|
---|
372 | }
|
---|
373 | // _____________________________________________________________________________________________________________
|
---|
374 | // Función: EjecutarTrabajo
|
---|
375 | //
|
---|
376 | // Descripción:
|
---|
377 | // Registra una acción (Trabajo y la envía para su ejecución
|
---|
378 | // Parámetros:
|
---|
379 | // - idtrabajo : Identificador del trabajo
|
---|
380 | // - Database: una conexion ADO operativa
|
---|
381 | // - parametros: parámetros de la acción
|
---|
382 | // _____________________________________________________________________________________________________________
|
---|
383 | int EjecutarTrabajo(int idtrabajo,Database db,char*parametros )
|
---|
384 | {
|
---|
385 | char sqlstr[1000],ErrStr[200];
|
---|
386 | Table tbl;
|
---|
387 | int cont_tareas=0,lon;
|
---|
388 | int idtarea,idtrabajotarea,idcentro;
|
---|
389 | char wambitrabajo[500],ambitrabajo[4000];
|
---|
390 | char wparamtrabajo[20],paramtrabajo[1000];
|
---|
391 | int tbTareasidtarea[100],tbTareasidnotificador[100];
|
---|
392 | char *tbTareasparametros[100],*tbTareasambitoambitskwrk[100];
|
---|
393 | char ambitskwrk[500];
|
---|
394 |
|
---|
395 | ambitrabajo[0]=(char)NULL; // Inicialización
|
---|
396 | strcpy(paramtrabajo,"tsk="); // Inicialización
|
---|
397 |
|
---|
398 | // recupera el identificador del Centro propietario de la tarea
|
---|
399 | sprintf(sqlstr,"SELECT idcentro FROM trabajos WHERE idtrabajo=%d",idtrabajo);
|
---|
400 | if(!db.Execute(sqlstr,tbl)){ // Error al leer
|
---|
401 | db.GetErrorErrStr(ErrStr);
|
---|
402 | return(false);
|
---|
403 | }
|
---|
404 | if(tbl.ISEOF()) return(true);
|
---|
405 | if(!tbl.Get("idcentro",idcentro)){ // Toma dato
|
---|
406 | tbl.GetErrorErrStr(ErrStr); // error al acceder al registro
|
---|
407 | return(false);
|
---|
408 | }
|
---|
409 | // Recupera las tareas que forman parte del trabajo
|
---|
410 | sprintf(sqlstr,"SELECT * FROM trabajos_tareas WHERE idtrabajo=%d ORDER by orden",idtrabajo);
|
---|
411 | if(!db.Execute(sqlstr,tbl)){ // Error al leer
|
---|
412 | db.GetErrorErrStr(ErrStr);
|
---|
413 | return(false);
|
---|
414 | }
|
---|
415 | if(tbl.ISEOF()) return(true);
|
---|
416 | // Recorre trabajos-tareas
|
---|
417 | while(!tbl.ISEOF()){
|
---|
418 | if(!tbl.Get("idtrabajotarea",idtrabajotarea)){ // Toma dato
|
---|
419 | tbl.GetErrorErrStr(ErrStr); // error al acceder al registro
|
---|
420 | return(false);
|
---|
421 | }
|
---|
422 | tbTareasidnotificador[cont_tareas]=idtrabajotarea;
|
---|
423 |
|
---|
424 | if(!tbl.Get("idtarea",idtarea)){ // Toma dato
|
---|
425 | tbl.GetErrorErrStr(ErrStr); // error al acceder al registro
|
---|
426 | return(false);
|
---|
427 | }
|
---|
428 | tbTareasidtarea[cont_tareas]=idtarea;
|
---|
429 |
|
---|
430 | if(!tbl.Get("parametros",parametros)){ // Toma dato
|
---|
431 | tbl.GetErrorErrStr(ErrStr); // error al acceder al registro
|
---|
432 | return(false);
|
---|
433 | }
|
---|
434 | lon=strlen(parametros);
|
---|
435 | tbTareasparametros[cont_tareas]=(char*)malloc(lon);
|
---|
436 | if(tbTareasparametros[cont_tareas]==NULL)
|
---|
437 | return(false); // No hay memoria bastante
|
---|
438 | strcpy(tbTareasparametros[cont_tareas],parametros);
|
---|
439 |
|
---|
440 | if(!tbl.Get("ambitskwrk",ambitskwrk)){ // Toma dato
|
---|
441 | tbl.GetErrorErrStr(ErrStr); // error al acceder al registro
|
---|
442 | return(false);
|
---|
443 | }
|
---|
444 | lon=strlen(ambitskwrk);
|
---|
445 | tbTareasambitoambitskwrk[cont_tareas]=(char*)malloc(lon);
|
---|
446 | strcpy(tbTareasambitoambitskwrk[cont_tareas],ambitskwrk);
|
---|
447 |
|
---|
448 | sprintf(wambitrabajo,"%s;",ambitskwrk);
|
---|
449 | strcat(ambitrabajo,wambitrabajo);
|
---|
450 |
|
---|
451 | sprintf(wparamtrabajo,"%d;",idtrabajotarea);
|
---|
452 | strcat(paramtrabajo,wparamtrabajo);
|
---|
453 |
|
---|
454 | cont_tareas++;
|
---|
455 | tbl.MoveNext();
|
---|
456 | }
|
---|
457 | lon=strlen(ambitrabajo);
|
---|
458 | ambitrabajo[lon-1]=(char)NULL; // Quita la coma final
|
---|
459 |
|
---|
460 | lon=strlen(paramtrabajo);
|
---|
461 | paramtrabajo[lon-1]=(char)NULL; // Quita la coma final
|
---|
462 |
|
---|
463 | char _fechahorareg[100];
|
---|
464 | SYSTEMTIME st;
|
---|
465 | GetLocalTime(&st);
|
---|
466 | sprintf(_fechahorareg,"%d/%d/%d %d:%d:%d",st.wDay,st.wMonth,st.wYear,st.wHour,st.wMinute,st.wSecond);
|
---|
467 |
|
---|
468 | 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,_fechahorareg,ACCION_INICIADA,ACCION_SINERRORES,idcentro,paramtrabajo);
|
---|
469 | if(!db.Execute(sqlstr)){ // Error al insertar
|
---|
470 | db.GetErrorErrStr(ErrStr);
|
---|
471 | return(false);
|
---|
472 | }
|
---|
473 | int accionid=0;
|
---|
474 | // Toma identificador dela acción
|
---|
475 | sprintf(sqlstr,"SELECT @@identity as identificador");
|
---|
476 | if(!db.Execute(sqlstr,tbl)){ // Error al leer
|
---|
477 | db.GetErrorErrStr(ErrStr);
|
---|
478 | return(false);
|
---|
479 | }
|
---|
480 | if(!tbl.ISEOF()){ // Si existe registro
|
---|
481 | if(!tbl.Get("identificador",accionid)){
|
---|
482 | tbl.GetErrorErrStr(ErrStr); // error al acceder al registro
|
---|
483 | return(false);
|
---|
484 | }
|
---|
485 | }
|
---|
486 | int i;
|
---|
487 | // Insertar acciones:tareas
|
---|
488 | for (i=0;i<cont_tareas;i++){
|
---|
489 | if(!EjecutarTarea(tbTareasidtarea[i],accionid,tbTareasidnotificador[i],idcentro,db,parametros)){
|
---|
490 | free(tbTareasparametros[i]);
|
---|
491 | free(tbTareasambitoambitskwrk[i]);
|
---|
492 | return(false);
|
---|
493 | }
|
---|
494 | free(tbTareasparametros[i]);
|
---|
495 | free(tbTareasambitoambitskwrk[i]);
|
---|
496 | }
|
---|
497 | return(true);
|
---|
498 | }
|
---|
499 | // _____________________________________________________________________________________________________________
|
---|
500 | // Función: EjecutarTarea
|
---|
501 | //
|
---|
502 | // Descripción:
|
---|
503 | // Registra una acción (Tarea) y la envía para su ejecución
|
---|
504 | // Parámetros:
|
---|
505 | // - idtarea : Identificador de la tarea
|
---|
506 | // - accionid: identificador del trabajo padre (si existe)
|
---|
507 | // - idnotificador: identificador del trabajo_tarea incluido en trabajo padre (si existe)
|
---|
508 | // - idcentro: Centro propietario del trabjo padre (si existe este trabajo)
|
---|
509 | // - Database: una conexion ADO operativa
|
---|
510 | // - parametros: parámetros de la acción
|
---|
511 | // _____________________________________________________________________________________________________________
|
---|
512 | int EjecutarTarea(int idtarea,int accionid,int idnotificador,int idcentro,Database db,char *parametros )
|
---|
513 | {
|
---|
514 | char sqlstr[1000],ErrStr[200],ambito;
|
---|
515 | Table tbl;
|
---|
516 | int cont_comandos=0,lon;
|
---|
517 | int idcomando,idambito,idtareacomando,accionidcmd;
|
---|
518 | char wambitarea[20],ambitarea[4000];
|
---|
519 | char wparamtarea[20],paramtarea[1000],pids[20];
|
---|
520 | int tbComandosidcomando[100],tbComandosambito[100],tbComandosidnotificador[100],tbComandosidambito[100];
|
---|
521 | char *tbComandosparametros[100];
|
---|
522 |
|
---|
523 | ambitarea[0]=(char)NULL; // Inicialización
|
---|
524 | strcpy(paramtarea,"cmd="); // Inicialización
|
---|
525 | if(idcentro==0){
|
---|
526 | // recupera el identificador del Centro propietario de la tarea
|
---|
527 | sprintf(sqlstr,"SELECT idcentro FROM tareas WHERE idtarea=%d",idtarea);
|
---|
528 | if(!db.Execute(sqlstr,tbl)){ // Error al leer
|
---|
529 | db.GetErrorErrStr(ErrStr);
|
---|
530 | return(false);
|
---|
531 | }
|
---|
532 | if(tbl.ISEOF()) return(true);
|
---|
533 | if(!tbl.Get("idcentro",idcentro)){ // Toma dato
|
---|
534 | tbl.GetErrorErrStr(ErrStr); // error al acceder al registro
|
---|
535 | return(false);
|
---|
536 | }
|
---|
537 | }
|
---|
538 | // Recupera los comandos que forman parte de la tarea
|
---|
539 | sprintf(sqlstr,"SELECT * FROM tareas_comandos WHERE idtarea=%d ORDER by orden",idtarea);
|
---|
540 | if(!db.Execute(sqlstr,tbl)){ // Error al leer
|
---|
541 | db.GetErrorErrStr(ErrStr);
|
---|
542 | return(false);
|
---|
543 | }
|
---|
544 | if(tbl.ISEOF()) return(true);
|
---|
545 |
|
---|
546 | // Recorre tareas-comandos
|
---|
547 | while(!tbl.ISEOF()){
|
---|
548 | if(!tbl.Get("idcomando",idcomando)){ // Toma dato
|
---|
549 | tbl.GetErrorErrStr(ErrStr); // error al acceder al registro
|
---|
550 | return(false);
|
---|
551 | }
|
---|
552 | tbComandosidcomando[cont_comandos]=idcomando;
|
---|
553 |
|
---|
554 | if(!tbl.Get("ambito",ambito)){ // Toma dato
|
---|
555 | tbl.GetErrorErrStr(ErrStr); // error al acceder al registro
|
---|
556 | return(false);
|
---|
557 | }
|
---|
558 | tbComandosambito[cont_comandos]=ambito;
|
---|
559 |
|
---|
560 | if(!tbl.Get("idambito",idambito)){ // Toma dato
|
---|
561 | tbl.GetErrorErrStr(ErrStr); // error al acceder al registro
|
---|
562 | return(false);
|
---|
563 | }
|
---|
564 | tbComandosidambito[cont_comandos]=idambito;
|
---|
565 |
|
---|
566 |
|
---|
567 | if(!tbl.Get("parametros",parametros)){ // Toma dato
|
---|
568 | tbl.GetErrorErrStr(ErrStr); // error al acceder al registro
|
---|
569 | return(false);
|
---|
570 | }
|
---|
571 |
|
---|
572 | lon=strlen(parametros);
|
---|
573 | tbComandosparametros[cont_comandos]=(char*)malloc(lon+20);
|
---|
574 | if(tbComandosparametros[cont_comandos]==NULL)
|
---|
575 | return(false); // No hay memoria bastante
|
---|
576 |
|
---|
577 | strcpy(tbComandosparametros[cont_comandos],parametros);
|
---|
578 |
|
---|
579 | if(!tbl.Get("idtareacomando",idtareacomando)){ // Toma dato
|
---|
580 | tbl.GetErrorErrStr(ErrStr); // error al acceder al registro
|
---|
581 | return(false);
|
---|
582 | }
|
---|
583 | tbComandosidnotificador[cont_comandos]=idtareacomando;
|
---|
584 |
|
---|
585 | sprintf(wambitarea,"%d:%d;",ambito,idambito);
|
---|
586 | strcat(ambitarea,wambitarea);
|
---|
587 |
|
---|
588 | sprintf(wparamtarea,"%d;",idtareacomando);
|
---|
589 | strcat(paramtarea,wparamtarea);
|
---|
590 |
|
---|
591 | cont_comandos++;
|
---|
592 | tbl.MoveNext();
|
---|
593 | }
|
---|
594 | lon=strlen(ambitarea);
|
---|
595 | ambitarea[lon-1]=(char)NULL; // Quita la coma final
|
---|
596 |
|
---|
597 | lon=strlen(paramtarea);
|
---|
598 | paramtarea[lon-1]=(char)NULL; // Quita la coma final
|
---|
599 |
|
---|
600 | char _fechahorareg[100];
|
---|
601 | SYSTEMTIME st;
|
---|
602 | GetLocalTime(&st);
|
---|
603 | sprintf(_fechahorareg,"%d/%d/%d %d:%d:%d",st.wDay,st.wMonth,st.wYear,st.wHour,st.wMinute,st.wSecond);
|
---|
604 |
|
---|
605 | 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,_fechahorareg,ACCION_INICIADA,ACCION_SINERRORES,idcentro,paramtarea,accionid,idnotificador);
|
---|
606 | if(!db.Execute(sqlstr)){ // Error al insertar
|
---|
607 | db.GetErrorErrStr(ErrStr);
|
---|
608 | return(false);
|
---|
609 | }
|
---|
610 | accionid=0;
|
---|
611 | // Toma identificador dela acción
|
---|
612 | sprintf(sqlstr,"SELECT @@identity as identificador");
|
---|
613 | if(!db.Execute(sqlstr,tbl)){ // Error al leer
|
---|
614 | db.GetErrorErrStr(ErrStr);
|
---|
615 | return(false);
|
---|
616 | }
|
---|
617 | if(!tbl.ISEOF()){ // Si existe registro
|
---|
618 | if(!tbl.Get("identificador",accionid)){
|
---|
619 | tbl.GetErrorErrStr(ErrStr); // error al acceder al registro
|
---|
620 | return(false);
|
---|
621 | }
|
---|
622 | }
|
---|
623 | int i;
|
---|
624 | // Insertar acciones:comandos
|
---|
625 | for (i=0;i<cont_comandos;i++){
|
---|
626 | GetLocalTime(&st);
|
---|
627 | sprintf(_fechahorareg,"%d/%d/%d %d:%d:%d",st.wDay,st.wMonth,st.wYear,st.wHour,st.wMinute,st.wSecond);
|
---|
628 | 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],_fechahorareg,ACCION_EXITOSA,ACCION_SINERRORES,idcentro,tbComandosparametros[i],accionid,tbComandosidnotificador[i]);
|
---|
629 | if(!db.Execute(sqlstr)){ // Error al insertar
|
---|
630 | db.GetErrorErrStr(ErrStr);
|
---|
631 | free(tbComandosparametros[i]);
|
---|
632 | return(false);
|
---|
633 | }
|
---|
634 |
|
---|
635 | // Toma identificador dela acción
|
---|
636 | sprintf(sqlstr,"SELECT @@identity as identificador");
|
---|
637 | if(!db.Execute(sqlstr,tbl)){ // Error al leer
|
---|
638 | db.GetErrorErrStr(ErrStr);
|
---|
639 | return(false);
|
---|
640 | }
|
---|
641 |
|
---|
642 | if(!tbl.ISEOF()){ // Si existe registro
|
---|
643 | if(!tbl.Get("identificador",accionidcmd)){
|
---|
644 | tbl.GetErrorErrStr(ErrStr); // error al acceder al registro
|
---|
645 | return(false);
|
---|
646 | }
|
---|
647 | }
|
---|
648 | sprintf(pids,"ids=%d\r",accionidcmd);
|
---|
649 | strcat(tbComandosparametros[i],pids); // Le añade el identificador de la accion
|
---|
650 | envia_comando(tbComandosparametros[i]);
|
---|
651 | free(tbComandosparametros[i]);
|
---|
652 | }
|
---|
653 | return(true);
|
---|
654 | }
|
---|
655 | // _____________________________________________________________________________________________________________
|
---|
656 | // Función: EjecutarReserva
|
---|
657 | //
|
---|
658 | // Descripción:
|
---|
659 | // Registra una acción (Tarea) y la envía para su ejecución
|
---|
660 | // Parámetros:
|
---|
661 | // - idreserva : Identificador de la reserva
|
---|
662 | // - Database: una conexion ADO operativa
|
---|
663 | // - parametros: parámetros de la acción
|
---|
664 | // _____________________________________________________________________________________________________________
|
---|
665 | int EjecutarReserva(int idreserva,Database db,char*parametros )
|
---|
666 | {
|
---|
667 | char sqlstr[1000],ErrStr[200];
|
---|
668 | Table tbl;
|
---|
669 | int idaccion;
|
---|
670 |
|
---|
671 | sprintf(sqlstr,"SELECT idtarea,idtrabajo FROM reservas WHERE idreserva=%d",idreserva);
|
---|
672 | if(!db.Execute(sqlstr,tbl)){ // Error al leer
|
---|
673 | db.GetErrorErrStr(ErrStr);
|
---|
674 | return(false);
|
---|
675 | }
|
---|
676 | if(tbl.ISEOF()){
|
---|
677 | return(false); // No hay acciones previas en la reserva
|
---|
678 | }
|
---|
679 |
|
---|
680 | if(!tbl.Get("idtarea",idaccion)){ // Toma dato
|
---|
681 | tbl.GetErrorErrStr(ErrStr); // error al acceder al registro
|
---|
682 | return(false);
|
---|
683 | }
|
---|
684 | if(idaccion>0)
|
---|
685 | EjecutarTarea(idaccion,0,0,0,db,parametros); // Es una reserva con tarea previa
|
---|
686 |
|
---|
687 | if(!tbl.Get("idtrabajo",idaccion)){ // Toma dato
|
---|
688 | tbl.GetErrorErrStr(ErrStr); // error al acceder al registro
|
---|
689 | return(false);
|
---|
690 | }
|
---|
691 | if(idaccion>0)
|
---|
692 | EjecutarTrabajo(idaccion,db,parametros); // Es una reserva con trabajo previo
|
---|
693 |
|
---|
694 | return(true);
|
---|
695 | }
|
---|
696 | // _____________________________________________________________________________________________________________
|
---|
697 | // Función: envia_comando
|
---|
698 | //
|
---|
699 | // Descripción:
|
---|
700 | // Envia un comando a la red. Para ello es necesario teneriniciado el servicio hidra.
|
---|
701 | // Parámetros:
|
---|
702 | // - parametros: parámetros del comando
|
---|
703 | // _____________________________________________________________________________________________________________
|
---|
704 | int envia_comando(char* parametros)
|
---|
705 | {
|
---|
706 | SOCKET sClient;
|
---|
707 | TRAMA trama;
|
---|
708 |
|
---|
709 | sClient = AbreConexion(servidorhidra,puerto);
|
---|
710 | if (sClient == (SOCKET)NULL)
|
---|
711 | return(FALSE);
|
---|
712 |
|
---|
713 | trama.arroba='@';
|
---|
714 | strncpy(trama.identificador,"JMMLCAMDJ",9);
|
---|
715 | trama.ejecutor=parametros[0];
|
---|
716 | strcpy(trama.parametros,(char*)¶metros[1]);
|
---|
717 | return(manda_trama(sClient,&trama));
|
---|
718 | }
|
---|
719 | // _____________________________________________________________________________________________________________
|
---|
720 | // Función: AbreConexion
|
---|
721 | //
|
---|
722 | // Descripción:
|
---|
723 | // Crea un socket y lo conecta a una interface de red. Devuelve el socket
|
---|
724 | // Parámetros:
|
---|
725 | // - ips : La dirección IP con la que se comunicará el socket
|
---|
726 | // - port : Puerto para la comunicación
|
---|
727 | // _____________________________________________________________________________________________________________
|
---|
728 | SOCKET AbreConexion(char *ips,int port)
|
---|
729 | {
|
---|
730 | struct sockaddr_in server;
|
---|
731 | struct hostent *host = NULL;
|
---|
732 | SOCKET s;
|
---|
733 |
|
---|
734 | // Crea el socket y se intenta conectar
|
---|
735 | s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
|
---|
736 | if (s == SOCKET_ERROR){
|
---|
737 | return (SOCKET)NULL;
|
---|
738 | }
|
---|
739 | server.sin_family = AF_INET;
|
---|
740 | server.sin_port = htons((short)port);
|
---|
741 | server.sin_addr.s_addr = inet_addr(ips);
|
---|
742 |
|
---|
743 | if (connect(s, (struct sockaddr *)&server, sizeof(server)) == SOCKET_ERROR){
|
---|
744 | RegistraLog("***AGENT***connect() fallo:",true);
|
---|
745 | return (SOCKET)NULL;
|
---|
746 | }
|
---|
747 | return(s);
|
---|
748 | }
|
---|
749 | /// _____________________________________________________________________________________________________________
|
---|
750 | // Función: manda_trama
|
---|
751 | //
|
---|
752 | // Descripción:
|
---|
753 | // Esta función envia una trama por la red (TCP)
|
---|
754 | // Parametros:
|
---|
755 | // - sock : El socket del host al que se dirige la trama
|
---|
756 | // - trama: El contenido de la trama
|
---|
757 | /// _____________________________________________________________________________________________________________
|
---|
758 | int manda_trama(SOCKET sock,TRAMA* trama)
|
---|
759 | {
|
---|
760 | int nLeft,idx,ret;
|
---|
761 |
|
---|
762 | Encriptar((char*)trama);
|
---|
763 | nLeft = strlen((char*)trama);
|
---|
764 | idx = 0;
|
---|
765 | while(nLeft > 0){
|
---|
766 | ret = send(sock,(char*)&trama[idx], nLeft, 0);
|
---|
767 | if (ret == 0)
|
---|
768 | break;
|
---|
769 | else
|
---|
770 | if (ret == SOCKET_ERROR){
|
---|
771 | RegistraLog("***AGENT***send() fallo al enviar trama:",true);
|
---|
772 | return(FALSE);
|
---|
773 | }
|
---|
774 | nLeft -= ret;
|
---|
775 | idx += ret;
|
---|
776 | }
|
---|
777 | return(TRUE);
|
---|
778 | }
|
---|
779 | //******************************************************************************************************************************************
|
---|
780 | // PROGRAMA PRINCIPAL ( SERVICIO)
|
---|
781 | //******************************************************************************************************************************************
|
---|
782 |
|
---|
783 | main()
|
---|
784 |
|
---|
785 | while (TRUE){
|
---|
786 | GetLocalTime(&st);
|
---|
787 | pseg=1000*(65-st.wSecond); // Calcula milisegundos de inactividad de la hebra
|
---|
788 | Sleep(pseg);
|
---|
789 | // Toma la hora
|
---|
790 | GetLocalTime(&st);
|
---|
791 | GestionaProgramacion(st);
|
---|
792 | }
|
---|
793 |
|
---|