示例#1
0
void cmdList(MSG *databuf, int connfd)
{
	printf("list cmd..............\n");
	//读取当前目录,如果打开成功,将返回值databuf->sig=SUCCESS
	//然后将读取的目录信息发送给client
	
	struct dirent *dirbuf;	//用于保存打开的目录结构体
	dirbuf = readdir("\.");	//先打开当前目录把
	if( dirbuf == NULL)
	{
		perror("opendir");
		initMSG(databuf);
		databuf->sig = FAIL;
		
		Send(connfd, databuf, sizeof(databuf), 0);
		printf("warning msg had sent to client...\n");
		return;
	}
	else
	{
		initMSG(databuf);
		databuf->sockfd = connfd;
		databuf->sig = SUCCESS;
		databuf->msg = &dirbuf;

		Send(connfd, databuf, sizeof(databuf), 0);
		printf("Msg had sent to client...\n");
		return;
	}
}
示例#2
0
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;
    }
}