void topology_analysis() { char host1[20], host2[20]; int cost, host1ID, host2ID; in_addr_t host1IP, host2IP; int node[20], nbr[20]; in_addr_t nip[20]; int myNodeID = topology_getMyNodeID(); FILE *pFile = fopen("../topology/topology.dat", "r"); if(pFile == NULL) perror("Error opening topology"); while(fscanf(pFile,"%s %s %d", host1, host2, &cost) > 0){ host1ID = topology_getNodeIDfromname(host1); host1IP = topology_getNodeIPfromname(host1); host2ID = topology_getNodeIDfromname(host2); host2IP = topology_getNodeIPfromname(host2); if(host1ID == myNodeID){ nbr[nbrNum] = host2ID; nip[nbrNum] = host2IP; nbrNum++; if(host2ID < myNodeID) lessNbr++; } else if(host2ID == myNodeID){ nbr[nbrNum] = host1ID; nip[nbrNum] = host1IP; nbrNum++; if(host1ID < myNodeID) lessNbr++; } if(searchInArray(node, host1ID, nodeNum) == -1){ node[nodeNum] = host1ID; nodeNum++; } if(searchInArray(node, host2ID, nodeNum) == -1){ node[nodeNum] = host2ID; nodeNum++; } } int i = 0; nbrIDArray = (int *)malloc(nbrNum * sizeof (int)); for(i = 0; i < nbrNum; i++) nbrIDArray[i] = nbr[i]; nbrIPArray = (in_addr_t *)malloc(nbrNum * sizeof (in_addr_t)); for(i = 0; i < nbrNum; i++) nbrIPArray[i] = nip[i]; nodeArray = (int *)malloc(nodeNum * sizeof (int)); for(i = 0; i < nodeNum; i++) nodeArray[i] = node[i]; fclose(pFile); }
//read topology.dat void getTopoData() { FILE *pFile; pFile = fopen("../topology/topology.dat","r"); if(pFile == NULL) { printf("open file error\n"); exit(-1); } char buf[128]; int lineCount=0; while (fgets(buf,sizeof(buf),pFile)!= NULL) { lineCount++; } if(lineCount<=0) { printf("no topology in file\n"); exit(-1); } //init topo data list allIdList=malloc(lineCount*2*sizeof(int)); nbIdList=malloc(lineCount*sizeof(int)); nbIpList=malloc(lineCount*sizeof(in_addr_t)); costList=malloc(lineCount*sizeof(int)); //get data from file char host1[32],host2[32]; int cost; int myId=topology_getMyNodeID(); nbCount=0; fseek(pFile,0,SEEK_SET); while(fscanf(pFile,"%s %s %d", host1, host2, &cost) > 0) { int id1=topology_getNodeIDfromname(host1); int id2=topology_getNodeIDfromname(host2); int i=0; for(;i<allCount;i++) { if(allIdList[i]==id1)break; } if(i==allCount)allIdList[allCount++]=id1; i=0; for(;i<allCount;i++) { if(allIdList[i]==id2)break; } if(i==allCount)allIdList[allCount++]=id2; //neighbor if(id1==myId) { nbIdList[nbCount]=id2; nbIpList[nbCount]=topology_getNodeIPfromname(host2); costList[nbCount]=cost; if(id2>id1)bigCount++; else if(id1>id2)smallCount++; nbCount++; } else if(id2==myId) { nbIdList[nbCount]=id1; nbIpList[nbCount]=topology_getNodeIPfromname(host1); costList[nbCount]=cost; if(id2>id1)smallCount++; else if(id1>id2)bigCount++; nbCount++; } } }