274 |
|
return -1; |
275 |
|
} |
276 |
|
|
277 |
+ |
fflush(fm_fd_r); |
278 |
+ |
fflush(fm_fd_w); |
279 |
+ |
|
280 |
|
if(fclose(fm_fd_r) !=0){ |
281 |
|
errf("Failed to close read FD (%m)"); |
282 |
|
return -1; |
365 |
|
return -1; |
366 |
|
} |
367 |
|
|
368 |
+ |
close(sd); |
369 |
+ |
|
370 |
|
return 0; |
371 |
|
} |
372 |
|
|
374 |
|
ihost_state_t ihost_state; |
375 |
|
int heartbeat_exit; |
376 |
|
int counter=0; |
377 |
< |
long udp_time=0, tcp_time=0, cur_time=0; |
377 |
> |
long udp_time=0, tcp_time=0, stat_grab_time=0, cur_time=0; |
378 |
|
int sleep_delay=0; |
379 |
|
char *xml_stats; |
380 |
|
|
402 |
|
exit(1); |
403 |
|
} |
404 |
|
|
400 |
– |
/* get starting stats */ |
401 |
– |
if((heartbeat(&ihost_state)) == -1){ |
402 |
– |
errf("inital heartbeat failed (%m)"); |
403 |
– |
exit(1); |
404 |
– |
} |
405 |
– |
|
406 |
– |
|
407 |
– |
if((xml_stats=stat_grab(&ihost_state, counter)) == NULL){ |
408 |
– |
errf("Failed to get stats (%m)"); |
409 |
– |
exit(1); |
410 |
– |
} |
411 |
– |
|
412 |
– |
|
405 |
|
for(;;){ |
406 |
|
cur_time=time(NULL); |
407 |
|
if(cur_time>=tcp_time){ |
408 |
+ |
/*printf("sending TCP\n");*/ |
409 |
|
heartbeat_exit=heartbeat(&ihost_state); |
410 |
|
if(heartbeat_exit==RECONFIGURE_RETURN_CODE){ |
411 |
< |
errf("heartbeat needs to be reconfigured"); |
411 |
> |
/*errf("heartbeat needs to be reconfigured");*/ |
412 |
|
ihost_configure(&ihost_state); |
413 |
|
/* So udp doesn't wait til next sending before updating */ |
414 |
|
udp_time=0; |
419 |
|
} |
420 |
|
tcp_time=time(NULL)+ihost_state.tcp_update_time; |
421 |
|
} |
422 |
+ |
|
423 |
|
if(cur_time>=udp_time){ |
424 |
< |
send_stats(&ihost_state, xml_stats); |
425 |
< |
free(xml_stats); |
424 |
> |
/*printf("sending UDP\n");*/ |
425 |
> |
stat_grab_time=time(NULL); |
426 |
|
if((xml_stats=stat_grab(&ihost_state, counter)) == NULL){ |
427 |
|
errf("Failed to get stats (%m)"); |
428 |
|
exit(1); |
429 |
|
} |
430 |
< |
udp_time=time(NULL)+ihost_state.udp_update_time; |
430 |
> |
stat_grab_time=time(NULL)-stat_grab_time; |
431 |
> |
send_stats(&ihost_state, xml_stats); |
432 |
> |
free(xml_stats); |
433 |
> |
udp_time=time(NULL)+ihost_state.udp_update_time-stat_grab_time; |
434 |
|
} |
435 |
< |
if(tcp_time>udp_time){ |
436 |
< |
sleep_delay=time(NULL)-tcp_time; |
435 |
> |
|
436 |
> |
if(tcp_time<udp_time){ |
437 |
> |
sleep_delay=tcp_time-time(NULL); |
438 |
|
}else{ |
439 |
< |
sleep_delay=time(NULL)-udp_time; |
439 |
> |
sleep_delay=udp_time-time(NULL); |
440 |
|
} |
441 |
< |
sleep(sleep_delay); |
441 |
> |
|
442 |
> |
/*printf("tcp epoc: %ld \t udp epoc: %ld\ntime:%ld \tsleeping: %d\n", tcp_time, udp_time, time(NULL), sleep_delay);*/ |
443 |
> |
if(sleep_delay>0) sleep(sleep_delay); |
444 |
|
} |
445 |
|
return 0; |
446 |
|
} |