/* * sgb2newns2.c -- converter from sgb format to ns format. * * Alternative format: * * GRAPH (#nodes #edges id uu vv ww xx yy zz): * <#nodes> <#edges> [all integer utility fields] * * VERTICES (index name u v w x y z): * 0 [all integer utility fields for node0] * 1 [all integer utility fields for node1] * .... * * EDGES (from-node to-node length a b): * [all integer utility fields] * .... * */ #include #include #include "gb_graph.h" #include "gb_save.h" #include "geog.h" #define TRUE 1 #define FALSE 0 #define HUGE 655536 main(argc,argv) int argc; char *argv[]; { int i, j, nlink, k, jj, domain = 0, cluster = 0, p=0, /* q=0, */ q_b=0, r=0, n=0; Vertex *v; Arc *a; Graph *g; FILE *fopen(), *fout; char m[420], tempstr[10], node_per_cluster[HUGE], name[40]; char *temp, *index; char *address[HUGE]; int q[HUGE]; if (argc != 3) { printf("sgb2newns2 \n\n"); return; } fout = fopen(argv[2],"w"); g = restore_graph(argv[1]); if (g == NULL) { printf("%s does not contain a correct SGB graph\n",argv[1]); return; } fprintf(fout,"# Generated by sgb2hier-ns,sgb2hier-ns generated from sgb2ns\n"); fprintf(fout,"# sgb2ns adapted from sgb2alt by Polly Huang\n"); fprintf(fout,"# GRAPH (#nodes #edges id uu vv ww xx yy zz):\n"); fprintf(fout,"# %d %d %s ",g->n, g->m, g->id); if (g->util_types[8] == 'I') fprintf(fout,"%ld ",g->uu.I); if (g->util_types[9] == 'I') fprintf(fout,"%ld ",g->vv.I); if (g->util_types[10] == 'I') fprintf(fout,"%ld ",g->ww.I); if (g->util_types[11] == 'I') fprintf(fout,"%ld ",g->xx.I); if (g->util_types[12] == 'I') fprintf(fout,"%ld ",g->yy.I); if (g->util_types[13] == 'I') fprintf(fout,"%ld ",g->zz.I); fprintf(fout,"\n\n"); fprintf(fout, "#Creating hierarchical topology from transit-stub graph:\n\n"); q[p] = 0; /* generating hierarchical topology from transit-stub*/ for (v = g->vertices,i=0; i < g->n; i++,v++) { strcpy(name, v->name); temp = name; temp += 2; index = strstr(temp, "."); temp[index - temp] = '\0'; if ( p == atoi(temp)) { /* in same domain as before */ if (name[0] == 'T') { /* for transits -> single node domains */ r = 0; address[i] = (char *)malloc(strlen(v->name)); sprintf(address[i],"%d.%d.%d",p,q[p],r); strcat(node_per_cluster, "1 "); q[p] = q[p] + 1; } else if (name[0] == 'S') { /* for stubs */ strcpy(name, v->name); temp = name; while(temp){ index = strstr(temp, "."); if (index != NULL) temp = index + 1; else{ if (atoi(temp) == 0) { if ( q_b != 0) { /* printf("#num of nodes= %d\n",r); */ sprintf(tempstr, "%d ", r + 1); strcat(node_per_cluster, tempstr); } r = 0; address[i] = (char *)malloc(strlen(v->name)); sprintf(address[i],"%d.%d.%d", p, q[p], r); q_b = q[p]; q[p] = q[p] + 1; } else { r++; address[i] = (char *)malloc(strlen(v->name)); sprintf(address[i],"%d.%d.%d", p, q_b, r); } break; } } } } else { sprintf(tempstr, "%d ", r + 1); strcat(node_per_cluster, tempstr); p = atoi(temp); q[p] = 0; q_b = 0; r = 0; address[i] = (char *)malloc(strlen(v->name)); sprintf(address[i],"%d.%d.%d",p,q[p],r); strcat(node_per_cluster, "1 "); q[p] = q[p] + 1; } } domain = p + 1; /* cluster = q; */ /* assuming all domains have equal # of clusters */ /* for domains having diff no of clusters , use q[domain num]*/ sprintf(tempstr, "%d ", r + 1); strcat(node_per_cluster, tempstr); fprintf(fout, "proc create-hier-topology {linkBW} {\n"); /* fprintf(fout, "\tupvar $node n\n"); */ /* fprintf(fout, "\tupvar $nsns ns\n\n"); */ fprintf(fout, "\tglobal ns n\n\n"); fprintf(fout,"\tset verbose 1\n\n"); /* nodes */ fprintf(fout, "\tif {$verbose} { \n"); fprintf(fout, "\t\tputs \"Creating hierarchical nodes..\" \n"); fprintf(fout, "}\n"); for (i = 0; i < g->n; i++) { fprintf(fout, "set n(%d) [$ns hier-node %s]\n", i, address[i]); if ((i % 100) == 0) { fprintf(fout, "\tif {$verbose} { puts -nonewline \"%d...\"; flush stdout }\n", i); } } fprintf(fout,"\n\n"); fprintf(fout,"# Topology information :\n"); fprintf(fout, "lappend domain %d\n", domain); fprintf(fout, "AddrParams set domain_num_ $domain\n"); fprintf(fout, "lappend cluster "); for (i = 0; i < domain; i++) { /* printf("q[%d] = %d\n",i,q[i]); */ fprintf(fout, "%d ",q[i]); } fprintf(fout, "\n"); fprintf(fout, "AddrParams set cluster_num_ $cluster\n"); fprintf(fout, "lappend eilastlevel %s\n",node_per_cluster); fprintf(fout, "AddrParams set nodes_num_ $eilastlevel\n"); fprintf(fout,"\n\n"); /* edges */ fprintf(fout, "\t# EDGES (from-node to-node length a b):\n"); nlink = 0; fprintf(fout, "\tif {$verbose} { \n"); fprintf(fout, "\t\tputs \"Creating links 0...\"\n"); fprintf(fout, "\t\tflush stdout \n"); fprintf(fout, "\t}\n"); for (v = g->vertices,i=0; i < g->n; i++,v++) for (a = v->arcs; a != NULL; a = a->next) { j = a->tip - g->vertices; if (j > i) { fprintf(fout, "\t$ns duplex-link-of-interfaces $n(%d) $n(%d) $linkBW %dms DropTail\n", i, j, 10 * a->len); nlink++; if ((nlink % 10) == 0) { fprintf(fout, "\tif {$verbose} { puts -nonewline \"%d...\"; flush stdout }\n", nlink); } } } fprintf(fout, "\n\tif {$verbose} { \n"); fprintf(fout, "\t\tputs -nonewline \"%d...\"\n", nlink); fprintf(fout, "\t\tflush stdout\n"); fprintf(fout, "\t\tputs \"starting\"\n"); fprintf(fout, "\t}\n"); /* srm members. join-group will be performed by Agent/SRM::start */ /* return the number of nodes in this topology */ fprintf(fout, "\treturn %d", g->n); fprintf(fout, "}\n"); for (i=0; i < g->n; i++) if (address[i] != NULL) free(address[i]); fprintf(fout,"# end of hier topology generation\n"); }