/* James Williams CS 622 - Distributed Networks */ /* This file contains functions to read a network file into the Network_type struct. */ #include #include #include "net.h" Network_type* load_net( char * net_file_name ) { FILE *fp; int i, j; Network_type *net = (Network_type *) malloc( sizeof(Network_type) ); if ( net == NULL ) { printf("Error allocating memory for Network.\n"); exit(1); } strcpy( net->netname, net_file_name ); fp = fopen( net_file_name, "r" ); if ( fp == NULL ) { printf("Error opening file '%s'.\n", net_file_name ); exit(1); } fscanf(fp, "%d%*[^\n]", &(net->netsize)); net->node_list = (Node_type *) malloc( sizeof(Node_type) * (net->netsize +1) ); /*+1 waste 0*/ if ( net->node_list == NULL ) { printf("Error allocating memory for Network.\n"); free( net ); exit(1); } /* read in each node of the network */ for ( i = 1; i <= net->netsize; i++ ) { Node_type this_node; fscanf(fp,"%s%d%f%d%f%f%d", &(this_node.name), &(this_node.link_count), &(this_node.msg_process_delay), &(this_node.state), &(this_node.latitude), &(this_node.longitude), &(this_node.node_num) ); if ( this_node.node_num != i ) { printf("Datafile corrupted. Node numbers\n"); } /* create link nodes */ this_node.link_list = (Link_type *) malloc( sizeof(Link_type) * (this_node.link_count +1) ); /*+1 waste 0*/ for ( j = 1; j <= this_node.link_count; j++ ) { Link_type this_link; fscanf(fp,"%d%d%d%d%*[^\n]", &(this_link.dest_node), &(this_link.active), &(this_link.work), &(this_link.spare) ); /* struct copy */ this_node.link_list[j] = this_link; } /* for j */ /* struct copy */ net->node_list[i] = this_node; } /* for i */ fclose( fp ); return net; } /* load_net */ /* free memory allocated for network */ void delete_net( Network_type *net ) { int i; for ( i = 1; i <= net->netsize; i++ ) { free( net->node_list[i].link_list ); } free( net->node_list ); free( net ); } /* delete_net */ void show_net( Network_type *net ) { int i, j; printf("\nShow network\n"); /* DEBUG */ printf("netsize = %d\n", net->netsize ); /* read in each node of the network */ for ( i = 1; i <= net->netsize; i++ ) { Node_type this_node = net->node_list[i]; /* DEBUG */ printf("%s %d %f %d %f %f %d\n", this_node.name, this_node.link_count, this_node.msg_process_delay, this_node.state, this_node.latitude, this_node.longitude, this_node.node_num ); for ( j = 1; j <= this_node.link_count; j++ ) { Link_type this_link = this_node.link_list[j]; /* DEBUG */ printf("%d %d %d %d\n", this_link.dest_node, this_link.active, this_link.work, this_link.spare ); } /* for j */ } /* for i */ } /* show_net */ /* DEBUG void main( void ) { Network_type *net; net = load_net("njx.net"); show_net( net ); delete_net( net ); } */