Load the bitmap file only after connections has been
authorPekka Riikonen <priikone@silcnet.org>
Sat, 6 Mar 2004 15:44:12 +0000 (15:44 +0000)
committerPekka Riikonen <priikone@silcnet.org>
Sat, 6 Mar 2004 15:44:12 +0000 (15:44 +0000)
processed.

apps/silcmap/silcmap.h
apps/silcmap/silcmap_bitmap.c
apps/silcmap/silcmap_client.c
apps/silcmap/silcmap_command.c
apps/silcmap/silcmap_html.c

index b5fe8dd55c50590fd6d419c2223a243e8e0f8432..80b6140bc22c8d77f9da0c0f6e05449ff7e60e04 100644 (file)
@@ -4,7 +4,7 @@
 
   Author: Pekka Riikonen <priikone@silcnet.org>
 
-  Copyright (C) 2003 Pekka Riikonen
+  Copyright (C) 2003 - 2004 Pekka Riikonen
 
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
@@ -53,6 +53,7 @@ typedef struct {
   unsigned int lcolor_set     : 1;
 
   /* Command */
+  unsigned int loadmap        : 1;
   unsigned int writemap       : 1;
   unsigned int writehtml      : 1;
   unsigned int writemaphtml   : 1;
@@ -75,13 +76,14 @@ typedef struct {
 
   /* Bitmap data */
   unsigned char *bitmap;       /* Loaded bitmap image */
-  SilcUInt32 bitsilc_map_size; /* Size of bitmap */
+  SilcUInt32 bitmap_size;      /* Size of bitmap */
   SilcUInt32 width;            /* Bitmap width in pixels */
   SilcUInt32 height;           /* Bitmap height in pixels */
   SilcUInt32 maxcolor;         /* Max color value in bitmap */
   MapFonts font;               /* Current font */
 
   /* Output methods */
+  SilcMapCommandStruct loadmap;
   SilcMapCommandStruct writemap;
   SilcMapCommandStruct writehtml;
   SilcMapCommandStruct writerel;
index 5f67b388c25d9090e6fedd4852ed1d906123a441..b05082c2ed6a2c898b0f1ddee43aab506fe11f33 100644 (file)
@@ -4,7 +4,7 @@
 
   Author: Pekka Riikonen <priikone@silcnet.org>
 
-  Copyright (C) 2003 Pekka Riikonen
+  Copyright (C) 2003 - 2004 Pekka Riikonen
 
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
@@ -43,8 +43,10 @@ bool silc_map_load_ppm(SilcMap map, const char *filename)
   }
 
   /* Read width and height */
-  ret = fscanf(fp, "%s %ld %ld %ld ",
-              type, &map->width, &map->height, &map->maxcolor);
+  ret = fscanf(fp, "%s %ld %ld %ld ", type,
+              (unsigned long *)&map->width,
+              (unsigned long *)&map->height,
+              (unsigned long *)&map->maxcolor);
   if (ret < 4) {
     fprintf(stderr, "Invalid PPM file");
     retval = FALSE;
@@ -52,9 +54,9 @@ bool silc_map_load_ppm(SilcMap map, const char *filename)
   }
 
   /* Read the picture */
-  map->bitsilc_map_size = map->width * 3 * map->height;
-  map->bitmap = silc_malloc(map->bitsilc_map_size);
-  ret = fread(map->bitmap, map->bitsilc_map_size, 1, fp);
+  map->bitmap_size = map->width * 3 * map->height;
+  map->bitmap = silc_malloc(map->bitmap_size);
+  ret = fread(map->bitmap, map->bitmap_size, 1, fp);
   if (ret < 0) {
     fprintf(stderr, "fread: %s\n", strerror(errno));
     retval = FALSE;
@@ -72,7 +74,6 @@ bool silc_map_write_ppm(SilcMap map, const char *filename)
 {
   FILE *fp;
   int retval = TRUE;
-  int i, k;
 
   SILC_LOG_DEBUG(("Write PPM '%s'", filename));
 
@@ -83,10 +84,13 @@ bool silc_map_write_ppm(SilcMap map, const char *filename)
   }
 
   /* Write the header */
-  fprintf(fp, "P6 %ld %ld %ld\n", map->width, map->height, map->maxcolor);
+  fprintf(fp, "P6 %ld %ld %ld\n",
+         (unsigned long)map->width,
+         (unsigned long)map->height,
+         (unsigned long)map->maxcolor);
 
   /* Write the bitmap */
-  fwrite(map->bitmap, map->bitsilc_map_size, 1, fp);
+  fwrite(map->bitmap, map->bitmap_size, 1, fp);
   fclose(fp);
 
   return retval;
@@ -106,11 +110,13 @@ bool silc_map_cut(SilcMap map, SilcInt32 x, SilcInt32 y,
 
   /* Sanity checks */
   if (height > map->height - y) {
-    fprintf(stderr, "Requesting too much height: %ld\n", height);
+    fprintf(stderr, "Requesting too much height: %ld\n",
+           (unsigned long)height);
     return FALSE;
   }
   if (width > map->width - x) {
-    fprintf(stderr, "Requesting too much width: %ld\n", width);
+    fprintf(stderr, "Requesting too much width: %ld\n",
+           (unsigned long)width);
     return FALSE;
   }
 
@@ -123,8 +129,8 @@ bool silc_map_cut(SilcMap map, SilcInt32 x, SilcInt32 y,
   (*ret_map)->width = width;
   (*ret_map)->height = height;
   (*ret_map)->maxcolor = map->maxcolor;
-  (*ret_map)->bitsilc_map_size = (width * 3) * height;
-  (*ret_map)->bitmap = silc_malloc((*ret_map)->bitsilc_map_size);
+  (*ret_map)->bitmap_size = (width * 3) * height;
+  (*ret_map)->bitmap = silc_malloc((*ret_map)->bitmap_size);
 
   /* Copy the requested area */
   for (i = 0; i < height; i++) {
index 86974f93769aedd50584e1b3804b9076aa7d106f..c605763cd559b86b06f14f5526152a9652ca1bb4 100644 (file)
@@ -4,7 +4,7 @@
 
   Author: Pekka Riikonen <priikone@silcnet.org>
 
-  Copyright (C) 2003 Pekka Riikonen
+  Copyright (C) 2003 - 2004 Pekka Riikonen
 
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
@@ -41,125 +41,146 @@ void silc_map_process_data(SilcMap map, SilcMapConnection mapconn)
   SilcInt16 r, g, b, lr, lg, lb;
   int i;
 
-  map->conn_num++;
   SILC_LOG_DEBUG(("Processing the data from server (%d/%d)",
                  map->conn_num, map->conns_num));
 
-  /* Change colors according to server status */
-  silc_map_parse_color(mapconn->up_color, &r, &g, &b);
-  silc_map_parse_color(mapconn->up_text_color, &lr, &lg, &lb);
-  if (mapconn->down) {
-    silc_map_parse_color(mapconn->down_color, &r, &g, &b);
-    silc_map_parse_color(mapconn->down_text_color, &lr, &lg, &lb);
-  }
+  map->conn_num++;
+  if (map->conn_num != map->conns_num)
+    return;
 
-  /* Execute the map commands */
-  silc_dlist_start(mapconn->commands);
-  while ((cmd = silc_dlist_get(mapconn->commands)) != SILC_LIST_END) {
-    if (cmd->cut) {
-      if (silc_map_cut(map, cmd->x, cmd->y, cmd->width,
-                      cmd->height, &ret_map)) {
-       silc_map_write_ppm(ret_map, cmd->filename);
-       silc_map_free(ret_map);
-      }
-      continue;
+  /* Load the map image to be processed */
+  if (!map->bitmap) {
+    if (!map->loadmap.loadmap || !map->loadmap.filename) {
+      silc_schedule_task_add(map->client->schedule, 0,
+                            silc_map_process_done, map, 0, 1,
+                            SILC_TASK_TIMEOUT, SILC_TASK_PRI_NORMAL);
+      return;
     }
 
-    if (cmd->draw_line) {
-      if (cmd->color_set) {
-       r = cmd->r;
-       g = cmd->g;
-       b = cmd->b;
-      }
-      silc_map_draw_line(map, cmd->width, cmd->x, cmd->y, cmd->x2, cmd->y2,
-                        r, g, b);
-      continue;
+    if (!silc_map_load_ppm(map, map->loadmap.filename)) {
+      silc_schedule_task_add(map->client->schedule, 0,
+                            silc_map_process_done, map, 0, 1,
+                            SILC_TASK_TIMEOUT, SILC_TASK_PRI_NORMAL);
+      return;
     }
+  }
 
-    if (cmd->draw_text) {
-      if (cmd->color_set) {
-       lr = cmd->r;
-       lg = cmd->g;
-       lb = cmd->b;
-      }
-      silc_map_draw_text(map, cmd->text, cmd->x, cmd->y, lr, lg, lb);
+  /* Now process all received data one by one */
+  silc_dlist_start(map->conns);
+  while ((mapconn = silc_dlist_get(map->conns)) != SILC_LIST_END) {
 
-      continue;
+    /* Change colors according to server status */
+    silc_map_parse_color(mapconn->up_color, &r, &g, &b);
+    silc_map_parse_color(mapconn->up_text_color, &lr, &lg, &lb);
+    if (mapconn->down) {
+      silc_map_parse_color(mapconn->down_color, &r, &g, &b);
+      silc_map_parse_color(mapconn->down_text_color, &lr, &lg, &lb);
     }
 
-    if (cmd->draw_circle) {
-      if (cmd->color_set) {
-       r = cmd->r;
-       g = cmd->g;
-       b = cmd->b;
+    /* Execute the map commands */
+    silc_dlist_start(mapconn->commands);
+    while ((cmd = silc_dlist_get(mapconn->commands)) != SILC_LIST_END) {
+      if (cmd->cut) {
+       if (silc_map_cut(map, cmd->x, cmd->y, cmd->width,
+                        cmd->height, &ret_map)) {
+         silc_map_write_ppm(ret_map, cmd->filename);
+         silc_map_free(ret_map);
+       }
+       continue;
       }
-      if (cmd->lcolor_set) {
-       lr = cmd->lr;
-       lg = cmd->lg;
-       lb = cmd->lb;
+
+      if (cmd->draw_line) {
+       if (cmd->color_set) {
+         r = cmd->r;
+         g = cmd->g;
+         b = cmd->b;
+       }
+       silc_map_draw_line(map, cmd->width, cmd->x, cmd->y, cmd->x2, cmd->y2,
+                          r, g, b);
+       continue;
+      }
+
+      if (cmd->draw_text) {
+       if (cmd->color_set) {
+         lr = cmd->r;
+         lg = cmd->g;
+         lb = cmd->b;
+       }
+       silc_map_draw_text(map, cmd->text, cmd->x, cmd->y, lr, lg, lb);
+       continue;
       }
-      silc_map_draw_circle(map, cmd->x, cmd->y, r, g, b,
-                          cmd->text, cmd->lposx, cmd->lposy, lr, lg, lb);
-      continue;
-    }
 
-    if (cmd->draw_rectangle) {
-      if (cmd->color_set) {
-       r = cmd->r;
-       g = cmd->g;
-       b = cmd->b;
+      if (cmd->draw_circle) {
+       if (cmd->color_set) {
+         r = cmd->r;
+         g = cmd->g;
+         b = cmd->b;
+       }
+       if (cmd->lcolor_set) {
+         lr = cmd->lr;
+         lg = cmd->lg;
+         lb = cmd->lb;
+       }
+       silc_map_draw_circle(map, cmd->x, cmd->y, r, g, b,
+                            cmd->text, cmd->lposx, cmd->lposy, lr, lg, lb);
+       continue;
       }
-      if (cmd->lcolor_set) {
-       lr = cmd->lr;
-       lg = cmd->lg;
-       lb = cmd->lb;
+
+      if (cmd->draw_rectangle) {
+       if (cmd->color_set) {
+         r = cmd->r;
+         g = cmd->g;
+         b = cmd->b;
+       }
+       if (cmd->lcolor_set) {
+         lr = cmd->lr;
+         lg = cmd->lg;
+         lb = cmd->lb;
+       }
+       silc_map_draw_rectangle(map, cmd->x, cmd->y, r, g, b,
+                               cmd->text, cmd->lposx, cmd->lposy, lr, lg, lb);
+       continue;
       }
-      silc_map_draw_rectangle(map, cmd->x, cmd->y, r, g, b,
-                             cmd->text, cmd->lposx, cmd->lposy, lr, lg, lb);
-      continue;
     }
 
+    /* Write the html data file */
+    if (map->writehtml.writehtml)
+      silc_map_writehtml(map, mapconn);
+
+    /* Write uptime reliability data */
+    if (map->writerel.writerel)
+      silc_map_writerel(map, mapconn);
+
   }
 
-  /* Write the html data file */
-  if (map->writehtml.writehtml)
-    silc_map_writehtml(map, mapconn);
+  SILC_LOG_DEBUG(("All connections processed"));
 
-  /* Write uptime reliability data */
-  if (map->writerel.writerel)
-    silc_map_writerel(map, mapconn);
-
-  /* If this was last connection, we are done and ready to quit. */
-  if (map->conn_num == map->conns_num) {
-    SILC_LOG_DEBUG(("All connections processed"));
-
-    /* Produce output */
-    if (map->writemap.writemap)
-      silc_map_write_ppm(map, map->writemap.filename);
-    for (i = 0; i < map->cut_count; i++) {
-      if (silc_map_cut(map, map->cut[i].x, map->cut[i].y, map->cut[i].width,
-                      map->cut[i].height, &ret_map)) {
-       silc_map_write_ppm(ret_map, map->cut[i].filename);
-       silc_map_free(ret_map);
-      }
+  /* Produce output */
+  if (map->writemap.writemap)
+    silc_map_write_ppm(map, map->writemap.filename);
+  for (i = 0; i < map->cut_count; i++) {
+    if (silc_map_cut(map, map->cut[i].x, map->cut[i].y, map->cut[i].width,
+                    map->cut[i].height, &ret_map)) {
+      silc_map_write_ppm(ret_map, map->cut[i].filename);
+      silc_map_free(ret_map);
     }
+  }
 
-    /* Write the HTML index file */
-    if (map->writehtml.writehtml)
-      silc_map_writehtml_index(map);
+  /* Write the HTML index file */
+  if (map->writehtml.writehtml)
+    silc_map_writehtml_index(map);
 
-    /* Write the HTML map file(s) */
-    silc_map_writemaphtml(map);
+  /* Write the HTML map file(s) */
+  silc_map_writemaphtml(map);
 
-    /* Write uptime reliability graph */
-    if (map->writerel.writerel)
-      silc_map_writerelhtml(map);
+  /* Write uptime reliability graph */
+  if (map->writerel.writerel)
+    silc_map_writerelhtml(map);
 
-    /* Schedule to stop */
-    silc_schedule_task_add(map->client->schedule, 0,
-                          silc_map_process_done, map, 0, 1,
-                          SILC_TASK_TIMEOUT, SILC_TASK_PRI_NORMAL);
-  }
+  /* Schedule to stop */
+  silc_schedule_task_add(map->client->schedule, 0,
+                        silc_map_process_done, map, 0, 1,
+                        SILC_TASK_TIMEOUT, SILC_TASK_PRI_NORMAL);
 }
 
 /* Timeout callback to detect if server is down. */
index 2fcecaee35aece0b53efe60674bba4dc2ed6e8d3..ef548a4d800025cd04a33140c88851af7063eea7 100644 (file)
@@ -4,7 +4,7 @@
 
   Author: Pekka Riikonen <priikone@silcnet.org>
 
-  Copyright (C) 2003 Pekka Riikonen
+  Copyright (C) 2003 - 2004 Pekka Riikonen
 
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
@@ -368,10 +368,17 @@ SILC_CONFIG_CALLBACK(silc_map_cmd_loadmap)
 
     /* Destroy old bitmap if loadmaped */
     silc_free(map->bitmap);
+    map->bitmap = NULL;
 
-    /* Loadmap the bitmap image */
-    if (!silc_map_load_ppm(map, filename))
-      retval = SILC_CONFIG_ESILENT;
+    /* Execute directly if there are no connections */
+    if (map->conns_num == 0) {
+      /* Load the bitmap image */
+      if (!silc_map_load_ppm(map, filename))
+       retval = SILC_CONFIG_ESILENT;
+    } else {
+      map->loadmap.filename = strdup(filename);
+      map->loadmap.writemap = TRUE;
+    }
 
     /* Cleanup */
     silc_free(filename);
@@ -650,7 +657,7 @@ SILC_CONFIG_CALLBACK(silc_map_cmd_rectangle)
     }
 
     /* Execute directly if not for connection */
-    if (!curr_conn) {
+    if (!curr_conn && map->bitmap) {
       /* Draw the rectangle */
       ret = silc_map_draw_rectangle(map, silc_map_lon2x(map, lon),
                                    silc_map_lat2y(map, lat),
@@ -740,7 +747,7 @@ SILC_CONFIG_CALLBACK(silc_map_cmd_circle)
     }
 
     /* Execute directly if not for connection */
-    if (!curr_conn) {
+    if (!curr_conn && map->bitmap) {
       /* Draw the circle */
       ret = silc_map_draw_circle(map, silc_map_lon2x(map, lon),
                                 silc_map_lat2y(map, lat),
@@ -828,7 +835,7 @@ SILC_CONFIG_CALLBACK(silc_map_cmd_line)
       width = 1;
 
     /* Execute directly if not for connection */
-    if (!curr_conn) {
+    if (!curr_conn && map->bitmap) {
       /* Draw the line */
       ret = silc_map_draw_line(map, width,
                               silc_map_lon2x(map, lon),
@@ -907,7 +914,7 @@ SILC_CONFIG_CALLBACK(silc_map_cmd_text)
                    lat, lon, r, g, b, text));
 
     /* Execute directly if not for connection */
-    if (!curr_conn) {
+    if (!curr_conn && map->bitmap) {
       /* Print the text */
       ret = silc_map_draw_text(map, text,
                               silc_map_lon2x(map, lon),
@@ -985,7 +992,7 @@ bool silc_map_commands_parse(SilcMap map, const char *filename)
   SILC_LOG_DEBUG(("Parsing status: %s", silc_config_strerror(ret)));
 
   if (ret && ret != SILC_CONFIG_ESILENT) {
-    fprintf(stderr, "Error parsing commands: %s, line %ld\n",
+    fprintf(stderr, "Error parsing commands: %s, line %d\n",
            silc_config_strerror(ret), silc_config_get_line(file));
     retval = FALSE;
   }
index f6c56aef9139859c19e3da4a4e69d463e24c5789..f565fcaab9321cae0718a04e0eec59f410a98d12 100644 (file)
@@ -4,7 +4,7 @@
 
   Author: Pekka Riikonen <priikone@silcnet.org>
 
-  Copyright (C) 2003 Pekka Riikonen
+  Copyright (C) 2003 - 2004 Pekka Riikonen
 
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
@@ -215,67 +215,67 @@ bool silc_map_writehtml(SilcMap map, SilcMapConnection mapconn)
     if (mapconn->clients) {
       fprintf(fp, "<tr><td>&nbsp;&nbsp;");
       fprintf(fp, "Local clients</td><td>&nbsp;:</td><td>&nbsp;%ld</td></tr>\n",
-             mapconn->data.clients);
+             (unsigned long)mapconn->data.clients);
     }
     if (mapconn->channels) {
       fprintf(fp, "<tr><td>&nbsp;&nbsp;");
       fprintf(fp, "Local channels</td><td>&nbsp;:</td><td>&nbsp;%ld</td></tr>\n",
-             mapconn->data.channels);
+             (unsigned long)mapconn->data.channels);
     }
     if (mapconn->server_ops) {
       fprintf(fp, "<tr><td>&nbsp;&nbsp;");
       fprintf(fp, "Local server operators</td><td>&nbsp;:</td><td>&nbsp;%ld</td></tr>\n",
-             mapconn->data.server_ops);
+             (unsigned long)mapconn->data.server_ops);
     }
     if (mapconn->router_ops) {
       fprintf(fp, "<tr><td>&nbsp;&nbsp;");
       fprintf(fp, "Local router operators</td><td>&nbsp;:</td><td>&nbsp;%ld</td></tr>\n",
-             mapconn->data.router_ops);
+             (unsigned long)mapconn->data.router_ops);
     }
     if (mapconn->cell_clients) {
       fprintf(fp, "<tr><td>&nbsp;&nbsp;");
       fprintf(fp, "Cell clients</td><td>&nbsp;:</td><td>&nbsp;%ld</td></tr>\n",
-             mapconn->data.cell_clients);
+             (unsigned long)mapconn->data.cell_clients);
     }
     if (mapconn->cell_channels) {
       fprintf(fp, "<tr><td>&nbsp;&nbsp;");
       fprintf(fp, "Cell channels</td><td>&nbsp;:</td><td>&nbsp;%ld</td></tr>\n",
-             mapconn->data.cell_channels);
+             (unsigned long)mapconn->data.cell_channels);
     }
     if (mapconn->cell_servers) {
       fprintf(fp, "<tr><td>&nbsp;&nbsp;");
       fprintf(fp, "Cell servers</td><td>&nbsp;:</td><td>&nbsp;%ld</td></tr>\n",
-             mapconn->data.cell_servers);
+             (unsigned long)mapconn->data.cell_servers);
     }
     if (mapconn->all_clients) {
       fprintf(fp, "<tr><td>&nbsp;&nbsp;");
       fprintf(fp, "All SILC clients</td><td>&nbsp;:</td><td>&nbsp;%ld</td></tr>\n",
-             mapconn->data.all_clients);
+             (unsigned long)mapconn->data.all_clients);
     }
     if (mapconn->all_channels) {
       fprintf(fp, "<tr><td>&nbsp;&nbsp;");
       fprintf(fp, "All SILC channels</td><td>&nbsp;:</td><td>&nbsp;%ld</td></tr>\n",
-             mapconn->data.all_channels);
+             (unsigned long)mapconn->data.all_channels);
     }
     if (mapconn->all_servers) {
       fprintf(fp, "<tr><td>&nbsp;&nbsp;");
       fprintf(fp, "All SILC servers</td><td>&nbsp;:</td><td>&nbsp;%ld</td></tr>\n",
-             mapconn->data.all_servers);
+             (unsigned long)mapconn->data.all_servers);
     }
     if (mapconn->all_routers) {
       fprintf(fp, "<tr><td>&nbsp;&nbsp;");
       fprintf(fp, "All SILC routers</td><td>&nbsp;:</td><td>&nbsp;%ld</td></tr>\n",
-             mapconn->data.all_routers);
+             (unsigned long)mapconn->data.all_routers);
     }
     if (mapconn->all_server_ops) {
       fprintf(fp, "<tr><td>&nbsp;&nbsp;");
       fprintf(fp, "All SILC server operators</td><td>&nbsp;:</td><td>&nbsp;%ld</td></tr>\n",
-             mapconn->data.all_server_ops);
+             (unsigned long)mapconn->data.all_server_ops);
     }
     if (mapconn->all_router_ops) {
       fprintf(fp, "<tr><td>&nbsp;&nbsp;");
       fprintf(fp, "All SILC router operators</td><td>&nbsp;:</td><td>&nbsp;%ld</td></tr>\n",
-             mapconn->data.all_router_ops);
+             (unsigned long)mapconn->data.all_router_ops);
     }
     fprintf(fp, "</table>\n");
   }