--- wmpiki.c.old 2005-03-26 12:46:27.000000000 +0100 +++ wmpiki.c 2005-03-29 22:10:04.000000000 +0200 @@ -70,29 +70,6 @@ int defined_hosts_number; /*----------------------------------------------------------------------*/ -int wmpiki_check_host(char *hostname) -{ -FILE *cmdpipe; -char linebuffer[MAX_BUFFER_SIZE], commandbuffer[MAX_BUFFER_SIZE]; -int ret; - - strcpy(commandbuffer, "ping "); - strcat(commandbuffer, hostname); - strcat(commandbuffer, " "); - strcat(commandbuffer, ping_options); - strcat(commandbuffer, " | grep '"); - strcat(commandbuffer, PING_GREP); - strcat(commandbuffer, "'"); - - cmdpipe = popen(commandbuffer, "r"); - linebuffer[0] = '\0'; - fscanf (cmdpipe, "%[^\n]", linebuffer); - ret = !pclose(cmdpipe); - return ret; -} - -/*----------------------------------------------------------------------*/ -/* FILE *wmpiki_start_check_host(char *hostname) { char commandbuffer[MAX_BUFFER_SIZE]; @@ -101,30 +78,36 @@ strcat(commandbuffer, hostname); strcat(commandbuffer, " "); strcat(commandbuffer, ping_options); - strcat(commandbuffer, " | grep '"); - strcat(commandbuffer, PING_GREP); - strcat(commandbuffer, "'"); - + if (ping_grep[0] != '\0') { + strcat(commandbuffer, " | grep '"); + strcat(commandbuffer, PING_GREP); + strcat(commandbuffer, "'"); + } return popen(commandbuffer, "r"); } -*/ + /*----------------------------------------------------------------------*/ void wmpiki_routine(int argc, char **argv) { XEvent Event; +long curtime, nexttime[MAX_HOSTS]; +FILE *ping_f[MAX_HOSTS]; +int i, launch; + + int xfd = 0; fd_set inputs; struct timeval timeout; -long start_time, curent_time, next_time[MAX_HOSTS]; +long start_time, next_time[MAX_HOSTS]; /* FILE *ping_f[MAX_HOSTS]; */ int yposition = 21; int yspacing = 5; +char linebuffer[MAX_BUFFER_SIZE]; int showed_hosts = 0; int yposition_table[8] = { 28, 25, 23, 20, 17, 9, 9, 9 }; int yspacing_table[8] = { 6, 6, 5, 4, 3, 3, 2, 1 }; -int i; yposition = yposition_table[defined_hosts_number-1]; yspacing += yspacing_table[defined_hosts_number-1]; @@ -134,12 +117,11 @@ dcl_open_x_window(argc, argv, wmpiki_master_xpm, wmpiki_mask_bits, MASK_WIDTH, MASK_HEIGHT); - if (defined_hosts_number>=6) + if (defined_hosts_number>=6) { /* hide wmpiki logo */ dcl_copy_xpm_area(0, 64, 56, 9, 4, 5); - else if (disable_logo) - { + } else if (disable_logo) { yposition -= 5; yspacing += 1; /* hide wmpiki logo */ @@ -147,133 +129,122 @@ } start_time = time(0); - for(i=0; i= next_time[i]) - { - if (wmp_hosts[i].update_period) - next_time[i] += wmp_hosts[i].update_period; - - else next_time[i] += update_period; - - dcl_draw_led(6, yposition + showed_hosts*yspacing, GRAY); - - printf("hostname[%d] = %s\n", i, wmp_hosts[i].host); - if (wmpiki_check_host(wmp_hosts[i].host)) - { - if (wmp_hosts[i].led) - dcl_draw_led(6, yposition + showed_hosts*yspacing, wmp_hosts[i].led); - else - dcl_draw_led(6, yposition + showed_hosts*yspacing, led_color); + FD_ZERO(&inputs); + FD_SET(xfd, &inputs); + + /* run pings */ + curtime = time(0); + for (i = 0; i < MAX_HOSTS; i++) { + if(defined_hosts[i] == true) { + if (ping_f[i] == NULL && launch == i && curtime >= nexttime[i]) { + nexttime[i] = curtime + wmp_hosts[i].update_period; + ping_f[i] = wmpiki_start_check_host(wmp_hosts[i].host); + } + if (ping_f[i] != NULL) { + FD_SET(fileno(ping_f[i]), &inputs); } } - showed_hosts++; } - - xfd = ConnectionNumber(display); - FD_ZERO(&inputs); - FD_SET(xfd, &inputs); + + /* read events */ switch (select(FD_SETSIZE, &inputs, NULL, NULL, &timeout)) { + /* timeout */ case 0: timeout.tv_sec = 0; - timeout.tv_usec = 150000; + timeout.tv_usec = 20000; dcl_redraw_window(); + launch = (launch + 1) % MAX_HOSTS; /* do not launch all pings at one time */ break; + + /* error */ case -1: break; + + /* X or ping */ default: - break; - } + /* check X */ + while (FD_ISSET(xfd, &inputs) && XPending(display)) { + XNextEvent(display, &Event); + switch (Event.type) { + case Expose: + dcl_redraw_window(); + break; + case DestroyNotify: + XCloseDisplay(display); + exit(0); + break; + } + } - while (FD_ISSET(xfd, &inputs) && XPending(display)) { - XNextEvent(display, &Event); - switch (Event.type) { - case Expose: - dcl_redraw_window(); - break; - case DestroyNotify: - XCloseDisplay(display); - exit(0); - break; - } + /* update hosts */ + for (i = showed_hosts = 0; i < MAX_HOSTS; i++) { + if(defined_hosts[i] == true) { + if(ping_f[i] != NULL && FD_ISSET(fileno(ping_f[i]), &inputs)) { + /* check ping */ + linebuffer[0] = '\0'; + while (fscanf (ping_f[i], "%[^\n]", linebuffer) == 1) + ; + + if (!pclose(ping_f[i])) { + dcl_draw_led(6, yposition + showed_hosts*yspacing, + wmp_hosts[i].led); + } else { + dcl_draw_led(6, yposition + showed_hosts*yspacing, + GRAY); + } + + ping_f[i] = NULL; + } + showed_hosts++; + } + } + break; } } }