/* Takes a table of node traffic in/out pairs and disburses the costs to the other nodes proportionally. disburse.c (aka bb.c) $RCSfile: disburse.c,v $ $Source: /home1/horton/IBCM/IPConnect/src/backbone_cost/RCS/disburse.c,v $ $Author: horton $ $Header: /home1/horton/IBCM/IPConnect/src/backbone_cost/RCS/disburse.c,v 1.2 1999/10/02 23:46:22 horton Exp $ */ #include #include #include #include typedef unsigned char boole; #define infinity 1.0e10 #define REALLOC(ptr,size) (ptr) ? realloc((ptr),(size)) : malloc((size)) #define FREE(x) if(!(x)) {;} else { free(x); x = 0; } typedef struct _router { char * name; double into; double outof; double x; double y; } Router; Router *routers = 0; int num_routers = 0; double intos = 0; double outofs = 0; void usage(char *f) { fprintf(stderr, "Usage: %s specification_file\n", f); /* specification file */ fprintf(stderr,"name _into_ _out_of_\n"); } int curr_line; char *curr_file = 0; int parse_router(FILE *f) { char buf[1000]; char keyword[100], arg[100], rest[100]; int keys; int errors = 0; Router router; while(fgets(buf, sizeof(buf), f)) { curr_line += 1; keys = strlen(buf); if (keys <= 1) continue; keyword[0] = 0; arg[0] = 0; rest[0] = 0; router.x = router.y = router.into = router.outof = 0; keys = sscanf(buf, "%s%lf%lf%lf%lf\n", keyword, &router.into, &router.outof, &router.x, &router.y); intos += router.into; outofs += router.outof; router.name = strdup(keyword); if (!errors) { routers = (Router *) (REALLOC( (void *)routers, (num_routers+1) * sizeof(Router))); routers[num_routers++] = router; } } return errors; } int parse_spec(char *fname) { FILE *f = 0; curr_line = 0; f = fopen(fname, "r"); if (f) { curr_file = fname; parse_router(f); fclose(f); } else { perror(fname); } return 0; } void main (int argc, char * argv[]) { int a; int from_r, to_r; char *matrix = 0; FILE *nodes = 0; FILE *contracts = 0; if (argc < 2) { usage(argv[0]); exit(1); } for (a = 1; a < argc; a++) { parse_spec(argv[a]); } nodes = fopen("nodes.bb", "w"); if (!nodes) { perror("nodes.bb"); exit (1); } contracts = fopen("contract.bb", "w"); if (!contracts) { perror("contract.bb"); exit (1); } fprintf(nodes, "# into[total] %f outof[total] %f\n\n", intos, outofs); for (from_r = 0; from_r < num_routers; from_r ++) { fprintf(nodes, "router\n\tname %s\n\tlocation %f %f\nend\n", routers[from_r].name, routers[from_r].x, routers[from_r].y); } for (from_r = 0; from_r < num_routers; from_r ++) { for (to_r = 0; to_r < num_routers; to_r ++) { if (to_r >= from_r) continue; /* only map 1/2 matrix */ fprintf(contracts, "contract\n\tfrom %s to %s \n\tcapacity %f %f\nend\n", routers[from_r].name, routers[to_r].name, routers[from_r].outof * routers[to_r].into / (intos - routers[from_r].into), routers[to_r].outof * routers[from_r].into / (intos - routers[to_r].into)); } } exit(0); } /* * $Log: disburse.c,v $ * Revision 1.2 1999/10/02 23:46:22 horton * make extra white space for trivial parser to interpret correctly * * Revision 1.1 1999/08/09 02:56:21 horton * Initial revision * * Revision 1.1 1999/08/06 08:17:23 horton * Initial revision * */