void ShortestPath::CalculateDiameter() {
	
	double Distance = 0.0;
		
	//Keeping the old state.
	unsigned int Old_Source = m_Source;
	unsigned int Old_Destination = m_Destination;
	double Old_Cost = m_Cost;
	vector<unsigned int> Old_Path = m_Path;

	vector<Vertex*> GraphVertices = m_Graph->getVertices();
	for(vector<Vertex*>::const_iterator V = GraphVertices.begin(); V != GraphVertices.end(); ++V) {
		for(vector<Vertex*>::const_iterator U = V+1; U != GraphVertices.end(); ++U) {
			m_Source = (*V)->second;
			m_Destination = (*U)->second;
			try{
				CalculateShortestPath();
				if( m_Cost > Distance)
					Distance = m_Cost;
			}

			//Handle the case in which no path exists between the 2 vertices.
			catch (GraphException e) {
				continue;
			}
		}
	}
	m_Diameter = Distance;

	//Retrieving the original state.
	m_Source = Old_Source;
	m_Destination = Old_Destination;
	m_Cost = Old_Cost;
	m_Path = Old_Path;
}
예제 #2
0
파일: router1.c 프로젝트: zinjiggle/router
void dealWithUDPMessage(char* buf, int length, struct NetworkTopoStruct* p, int tcpsockfd)
{
    if(length>MAXMESSAGESIZE) {
    	printf("dealWithUDPMessage: Message Size too Big! Can not deal with it!\n");
    	return;
    }
    MSG message,*pMSG;
    pMSG = initMSG(&message);
    char newMessage[MAXMESSAGESIZE];
    //char buf[MAXMESSAGESIZE];
    //memset(buf,'\0',sizeof(char)*MAXMESSAGESIZE);
    //memcpy(buf,receivebuf,sizeof(char)*(length+2));
    int newMSGLen = 0;
    int neiID=0;
    int i;
    char sendbuf[MAXDATASIZE];
    SPATH s_path;
    SPATH *shortestPath = initPathList(&s_path,1);
    if (fetchReceivedMessage(pMSG, buf)==0) {
    	perror("ERR in dealWithUDPMessage: fetch received message error !\n");
    	return;
    }
    
    switch(pMSG->msgType){
    case 1:
    	if(CalculateShortestPath(shortestPath, pMSG->dest,p) != 1)
    	{
    		printf("dealWithUDPMessage: no route exist!\n");
    		sprintf(sendbuf,"DROP %s",pMSG->message);
    		sendmessage(tcpsockfd,sendbuf);
    		receive(tcpsockfd,buf);
    		break;
    	}
    	generateForWardingMessage(newMessage,&newMSGLen,shortestPath,pMSG);
    	// send log information
    	sprintf(sendbuf,"LOG FWD %d %s",shortestPath->nexthop,pMSG->message);
    	sendmessage(tcpsockfd,sendbuf);
    	receive(tcpsockfd,buf); // This is LOG OK
    	// send newMessage to the neighbour
    	if((neiID = fetchNeiIdByAddr(p,shortestPath->nexthop))<0){
    		perror("ERR dealWithUDPMessage case 1: neigh id not found\n");
    		exit(1);
    	}
    	printf("dealWithUDPMessage: sending %d bytes with %d messages to %d through %d\n",newMSGLen,strlen(pMSG->message),pMSG->dest,p->Neighs[neiID].addr);
    	if(udpTalkTo(p->Neighs[neiID].host,p->Neighs[neiID].udpport,newMessage,newMSGLen) != 0)
    	{
    		perror("ERR dealWithUDPMessage case 1: udp talking error.\n");
    		exit(1);
    	}
    	break;
    case 2:
    	// find myself in the path list, 
    	for(i=0;i<pMSG->hopNum;i++){
    		if(pMSG->path[i] == p->myaddr) break;
    	}
    	//if not in the list, err
    	if(i==pMSG->hopNum){
    		perror("ERR dealWithUDPMessage: src not in path list\n");
    		break;
    	}
    	// if in the list but not the last one, forward and send the log information
    	if( i < pMSG->hopNum-1 ){
    		// send log information
    		sprintf(sendbuf,"LOG FWD %d %s",pMSG->path[i+1],pMSG->message);
    		sendmessage(tcpsockfd,sendbuf);
    		receive(tcpsockfd,buf); // This is LOG OK received
    		// generate forward message
		 	generateForWardingMessage(newMessage,&newMSGLen,NULL,pMSG);
    		if((neiID = fetchNeiIdByAddr(p,pMSG->path[i+1])) < 0){
    			perror("ERR dealWithUDPMessage case 2: neigh id not found\n");
    			exit(1);
    		}
    		// forward the information
    		printf("dealWithUDPMessage: sending %d bytes with %d messages to %d through %d\n",newMSGLen,strlen(pMSG->message),pMSG->dest,p->Neighs[neiID].addr);
    		if(udpTalkTo(p->Neighs[neiID].host,p->Neighs[neiID].udpport,newMessage,newMSGLen) != 0)
    		{
    			perror("ERR dealWithUDPMessage case 2: udp talking error.\n");
    			exit(1);
    		}
    	}
    	else{     // else, report received the list
    		sprintf(sendbuf,"RECEIVED %s",pMSG->message);
    		sendmessage(tcpsockfd,sendbuf);
    	}
    	break;
    case 3:
    case 4:
    	// check whether the link cost is already stored
    	if(!isLinkStored(pMSG->linkinfo,p)){  //if it is not stored.
    		// stores it and broadcast it
    		updateLinkChange(pMSG->linkinfo,p);
    		if(!broadCastLinkInfo(pMSG->msgType, pMSG->linkinfo, p)){
    			printf("ERR dealWithUDPMessage: broadcastlinkinfo\n");
    		}
    	}
    	break;
    default:
    	break;
    }
}