Beispiel #1
0
void ViWindowFunction<T>::create(const int size)
{
	if(size != mWindow.size())
	{
		mWindow.clear();
		mWindow.resize(size);
		fillWindow(size);
	}
}
Beispiel #2
0
void process_inbound_udp(int sock) {
  #define BUFLEN 1500
  struct sockaddr_in from;
  socklen_t fromlen;
  char buf[BUFLEN];

  fromlen = sizeof(from);
  spiffy_recvfrom(sock, buf, BUFLEN, 0, (struct sockaddr *) &from, &fromlen);
	//recvfrom(sock, buf, BUFLEN, 0, (struct sockaddr *) &from, &fromlen);
  packetHead* pHead = (packetHead* )buf;
  peerEle* peer = resolvePeer(from, peerList);

  
  switch(pHead->type){
	case WHOHAS:{
		printf("Recieve WHOHAS request\n");
		void* ihavep = ihaveCons(buf, &haschunkList);
		if( ihavep!= NULL){
			unsigned int bufSize = ((packetHead *)ihavep)->packLen;
			spiffy_sendto(sock, ihavep, bufSize, 0, (struct sockaddr *) &from, fromlen);
			//sendto(sock, ihavep, bufSize, 0, (struct sockaddr *) &from, fromlen);
			time(&start);
		}
		break;
	}
	case IHAVE:{
		printf("Receieve IHAVE request\n");
		printf("packet length is %u\n", pHead->packLen);
		char tmp[sizeofHash];
		memcpy(tmp, (buf+20), sizeofHash);
		printf("chunk hash: %s\n", tmp);
		peerEle* thisPeer = resolvePeer(from, peerList);
		time_t finish;
		time(&finish);
		if( thisPeer == NULL ){
			printf("RESOLVE FAILED\n");
		}
		thisPeer->rtt = difftime(finish, start);
		AddResponses(thisPeer, buf, &chunkList, sock);
		printChunkList(chunkList);
		break;
	}
	case GET:{
		printf("Receive GET Request\n");
		printf("packet length is %u\n", pHead->packLen);
		
		//check for free connections
		printf("maxcon:%d id: %d numout: %d\n",maxCon,mePeer->id,mePeer->numOut);
		if(mePeer->numOut == maxCon){
			chunkEle* dcp = lookupChunkHash((buf+20),&haschunkList);
			void* deniedp = deniedCons(dcp->chunkId);
			spiffy_sendto(sock, deniedp, headerSize, 0, (struct sockaddr *) &peer->cli_addr, sizeof(peer->cli_addr));
			//sendto(sock, deniedp, headerSize, 0, (struct sockaddr *) &peer->cli_addr, sizeof(peer->cli_addr));
			break;
		}
		

		chunkEle* thisWindow = buildNewWindow(&windowSets, &haschunkList, peer, masterDataFilePath, buf);

		// send first element, no need to clean or fill
		sendWindow(thisWindow, sock);
		mePeer->numOut++;//increase num
		break;
	}
	case DATA:{
		unsigned int bufSize = ((packetHead *)buf)->packLen;
		void* newBuf = malloc(bufSize);
		memcpy(newBuf,buf,bufSize);
		chunkEle* cep = resolveChunk(peer, chunkList);
		
		getRetryTimer = 0;

		// reject packets from finished chunk
		if(cep && (cep->chunkId != ((packetHead*)buf)->ackNum)){
			printf("Wrong chunk!\n");
			break;
		}

		orderedAdd(cep,newBuf);

		printf("Receive data packet %d, with size %d \n", ((packetHead *)buf)->seqNum, ((packetHead *)buf)->packLen - headerSize);

		findMex(cep);
		printPacketList(cep->packetList);
		void* packet = ackCons(cep->nextExpectedSeq - 1);

		printf("Send Ack %d\n", cep->nextExpectedSeq - 1);
		spiffy_sendto(sock, packet, headerSize, 0, (struct sockaddr *) &peer->cli_addr, sizeof(peer->cli_addr));
		//sendto(sock, packet, headerSize, 0, (struct sockaddr *) &peer->cli_addr, sizeof(peer->cli_addr));
		printf("bytes read for hash %s : %d\n",cep->chunkHash, cep->bytesRead);
		//check if finish receiving the whole chunk
		if(cep->bytesRead == chunkSize ){
			if ( cep->fromThisPeer->inUse == 1){
			printf("Sucessfully receive the chunk %s\n", cep->chunkHash);
			chunkList.finished ++;
			cep->fromThisPeer->inUse = 0;
			cep->inProgress = 0;
			mePeer->numOut--;
			}
			printf("FINISHED %d\n", chunkList.finished);
			if(chunkList.finished == chunkList.length){
				printf("Finish receiving the whole file\n");
				// merge the chunks and write to the corresponding output file
				buildOuputFile(outputFile, &chunkList);
				fclose(outputFile);
				printf("GOT %s\n",outputFilePath);
			}else{
				// try to send rest of pending get requests 
				// they are deferred to make sure no concurrent downloading from the same peer
				sendPendingGetRequest(&chunkList, sock);
			}
		}
		
		break;
	}
	case ACK:{
		chunkEle* cep = resolveChunk(peer, windowSets);

		if ( cep->inProgress == 0){
			break;
		}
		//check which wether SLOWSTART or CONGAVOID
		if(cep->mode == SLOWSTART){
			cep->windowSize++;
			printf("####in slow start\n");
		}
		else
			printf("@@@@in cong avoid rtt: %f\n",cep->fromThisPeer->rtt);
		//check if enough to enter cong avoid
		if(cep->windowSize == cep->ssthresh)
			cep->mode = CONGAVOID;

		//check for no ack in rtt
		if((cep->mode == CONGAVOID) && (cep->haveACK == 0)){
			cep->windowSize++;
			printf("IIIIincreasing in cong avoid\n");
		}

		//set ack
		cep->haveACK = 1;

		printf("Receive Ack %d\n", *(int*)(buf+12));
		if( (cep->lastAcked) && ((packetHead* )(cep->lastAcked->data))->seqNum == ((packetHead* )(buf))->ackNum ){
			cep->lastAckedCount ++;
			printf("Incrementing last ack counter for %d to %d\n", ((packetHead* )(buf))->ackNum,cep->lastAckedCount);
			if( cep->lastAckedCount  == 3 ){
				//cut ssthresh in half and set window 1
				cep->ssthresh = halve(cep->windowSize);
				cep->windowSize = 1;
				cep->mode = SLOWSTART;

				//reset this ones time
				time(&cep->afterLastAckedTime);

				//try retrasmit
				cep->lastAckedCount = 0;
				node* retry = cep->lastAcked->prevp;
				printf("Retramsmit packet %d\n", ((packetHead* )retry->data)->seqNum);
				spiffy_sendto(sock, retry->data, ((packetHead* )retry->data)->packLen, 0, (struct sockaddr *) &peer->cli_addr, sizeof(peer->cli_addr));
				break;
			}

		}else{
			cep->lastAcked = resolveLastPacketAcked( ((packetHead*)buf)->ackNum, cep);
			cleanList(cep);
			time(&cep->afterLastAckedTime);
		}
		
		//check if finish sending the whole chunk
		if( cep->bytesRead == chunkSize ){
			printf("Successfully send the chunk %s\n", cep->chunkHash);
			if(cep->lastAcked == cep->lastSent){
				// some clear below
				printf("All sent packets have been acked\n");
				cep->inProgress = 0;
				mePeer->numOut--;
			}else{
				printf("lastAcked:%d, lastSent:%d\n",((packetHead*)cep->lastAcked->data)->seqNum, ((packetHead*)cep->lastSent->data)->seqNum );
			}
			
		}else{
			cleanList(cep);
			fillWindow(cep);
			sendWindow(cep, sock);
		}

		printPacketList(cep->packetList);
		printf("window: %d, ssthresh: %d, mode: %d\n",cep->windowSize, cep->ssthresh,
			cep->mode);
		break;
	}
	case DENIED:{
		printf("Received a denied ack!\n");
		chunkEle* cep = resolveChunk(peer, chunkList);
		//fclose(outputFile);
		time(&getRetryTimer);
		cep->fromThisPeer->inUse = 0;
		break;
	}

	}

  printf("PROCESS_INBOUND_UDP SKELETON -- replace!\n"
	 "Incoming message from %s:%d\n%s\n\n", 
	 inet_ntoa(from.sin_addr),
	 ntohs(from.sin_port),
	 buf);
}
Beispiel #3
0
// function for showing the AI menu and editing the felevent settings
int AIsettingMenu(windowRef w, GameRef game)
{

	int quit = 0;
	int retVal = 0;


	buttonRef continueBtn = getBtn(continueImgFile, (WSw / 2) - BtnRectWidth / 2, 4.8 * (WSh / 6));
	if (continueBtn == NULL){
		perror("ERROR: failed getBtn");
		return ERROR;
	}

	buttonRef minimaxDepthBtn = getBtn(minimaxDepthImgFile, (3 * WSw / 4) - BtnRectWidth / 2, 1.15 * (WSh / 6));
	if (minimaxDepthBtn == NULL){
		perror("ERROR: failed getBtn");
		return ERROR;
	}

	buttonRef minimax1Btn = getBtn(minimax1ImgFile, (3 * WSw / 4) - BtnRectWidth / 2, 1.6 * (WSh / 6));
	if (minimax1Btn == NULL){
		perror("ERROR: failed getBtn");
		return ERROR;
	}

	buttonRef minimax2Btn = getBtn(minimax2ImgFile, (3 * WSw / 4) - BtnRectWidth / 2, 2.2 * (WSh / 6));
	if (minimax2Btn == NULL){
		perror("ERROR: failed getBtn");
		return ERROR;
	}

	buttonRef minimax3Btn = getBtn(minimax3ImgFile, (3 * WSw / 4) - BtnRectWidth / 2, 2.8 * (WSh / 6));
	if (minimax3Btn == NULL){
		perror("ERROR: failed getBtn");
		return ERROR;
	}

	buttonRef minimax4Btn = getBtn(minimax4ImgFile, (3 * WSw / 4) - BtnRectWidth / 2, 3.4 * (WSh / 6));
	if (minimax4Btn == NULL){
		perror("ERROR: failed getBtn");
		return ERROR;
	}

	buttonRef bestDepthBtn = getBtn(bestDepthImgFile, (3 * WSw / 4) - BtnRectWidth / 2, 4 * (WSh / 6));
	if (bestDepthBtn == NULL){
		perror("ERROR: failed getBtn");
		return ERROR;
	}

	buttonRef userColorBtn = getBtn(userColorImgFile, (WSw / 4) - BtnRectWidth / 2, 1.15 * (WSh / 6));
	if (userColorBtn == NULL){
		perror("ERROR: failed getBtn");
		return ERROR;
	}

	buttonRef whiteBtn = getBtn(whiteImgFile, (WSw / 4) - BtnRectWidth / 2, 1.6 * (WSh / 6));
	if (whiteBtn == NULL){
		perror("ERROR: failed getBtn");
		return ERROR;
	}

	buttonRef blackBtn = getBtn(blackImgFile, (WSw / 4) - BtnRectWidth / 2, 2.2 * (WSh / 6));
	if (blackBtn == NULL){
		perror("ERROR: failed getBtn");
		return ERROR;
	}


	panelRef topPanel = getPanel(0, 0, WSw, WSh);
	if (topPanel == NULL){
		perror("ERROR: failed getPanel");
		return ERROR;
	}

	lableRef topPanelLbl = getLbl(backImgFile, 0, 0);
	if (topPanelLbl == NULL){
		perror("ERROR: failed getLabel");
		return ERROR;
	}
	setLblHightWigth(topPanelLbl, WSw, WSh);



	while (!quit)
	{
		// Clear window to WHITE
		if (fillWindow(w, 255, 255, 255) == 1)
		{
			quit = QUIT;
			retVal = ERROR;
			perror("ERROR: failed fillWindow");
			break;
		}
		fillPanel(topPanel, 255, 255, 255);

		if (drowImgOnLblPnl(topPanelLbl, topPanel) == 1)
		{

			quit = QUIT;
			retVal = ERROR;
			perror("ERROR: failed drowImg");
			break;
		}

		// Draw images


		if (drowImgOnBtn(continueBtn, w) == 1)
		{
			quit = QUIT;
			retVal = ERROR;
			perror("ERROR: failed drowImg");
			break;
		}
		if (drowImgOnBtn(minimaxDepthBtn, w) == 1)
		{
			quit = QUIT;
			retVal = ERROR;
			perror("ERROR: failed drowImg");
			break;
		}

		if (drowImgOnBtn(minimax1Btn, w) == 1)
		{
			quit = QUIT;
			retVal = ERROR;
			perror("ERROR: failed drowImg");
			break;
		}

		if (drowImgOnBtn(minimax2Btn, w) == 1)
		{
			quit = QUIT;
			retVal = ERROR;
			perror("ERROR: failed drowImg");
			break;
		}

		if (drowImgOnBtn(minimax3Btn, w) == 1)
		{
			quit = QUIT;
			retVal = ERROR;
			perror("ERROR: failed drowImg");
			break;
		}

		if (drowImgOnBtn(minimax4Btn, w) == 1)
		{
			quit = QUIT;
			retVal = ERROR;
			perror("ERROR: failed drowImg");
			break;
		}

		if (drowImgOnBtn(bestDepthBtn, w) == 1)
		{
			quit = QUIT;
			retVal = ERROR;
			perror("ERROR: failed drowImg");
			break;
		}

		if (drowImgOnBtn(userColorBtn, w) == 1)
		{
			quit = QUIT;
			retVal = ERROR;
			perror("ERROR: failed drowImg");
			break;
		}

		if (drowImgOnBtn(whiteBtn, w) == 1)
		{
			quit = QUIT;
			retVal = ERROR;
			perror("ERROR: failed drowImg");
			break;
		}

		if (drowImgOnBtn(blackBtn, w) == 1)
		{
			quit = QUIT;
			retVal = ERROR;
			perror("ERROR: failed drowImg");
			break;
		}


		if (SDL_Flip(w->window) != 0)
		{
			perror("ERROR: failed perform SDL_Flip in MainMenu");
			return ERROR;
		}

		//Poll for keyboard & mouse events
		SDL_Event e = { 0 };
		while (SDL_PollEvent(&e) != 0 && quit == 0 && retVal == 0)
		{
			switch (e.type)
			{
			case (SDL_QUIT) :
				quit = QUIT;
				retVal = QUIT;
				break;
			case (SDL_KEYUP) :
				if (e.key.keysym.sym == SDLK_ESCAPE){
					quit = QUIT;
					retVal = QUIT;
				}
			case (SDL_MOUSEBUTTONUP) :
				if ((e.button.button == SDL_BUTTON_LEFT) && BtnArea(continueBtn, e)){
					quit = QUIT;
					retVal = setOrGame(w, game, 2);
				}
									 if ((e.button.button == SDL_BUTTON_LEFT) && BtnArea(minimax1Btn, e)){
										 unmarkAll(minimax1Btn, minimax2Btn, minimax3Btn, minimax4Btn, bestDepthBtn);
										 reloadImgToBtn(minimax1Btn, minimax1PressImgFile);
										 game->minimaxDepth = 1;
									 }
									 if ((e.button.button == SDL_BUTTON_LEFT) && BtnArea(minimax2Btn, e)){
										 unmarkAll(minimax1Btn, minimax2Btn, minimax3Btn, minimax4Btn, bestDepthBtn);
										 reloadImgToBtn(minimax2Btn, minimax2PressImgFile);
										 game->minimaxDepth = 2;
									 }
									 if ((e.button.button == SDL_BUTTON_LEFT) && BtnArea(minimax3Btn, e)){
										 unmarkAll(minimax1Btn, minimax2Btn, minimax3Btn, minimax4Btn, bestDepthBtn);
										 reloadImgToBtn(minimax3Btn, minimax3PressImgFile);
										 game->minimaxDepth = 3;
									 }
									 if ((e.button.button == SDL_BUTTON_LEFT) && BtnArea(minimax4Btn, e)){
										 unmarkAll(minimax1Btn, minimax2Btn, minimax3Btn, minimax4Btn, bestDepthBtn);
										 reloadImgToBtn(minimax4Btn, minimax4PressImgFile);
										 game->minimaxDepth = 4;
									 }
									 if ((e.button.button == SDL_BUTTON_LEFT) && BtnArea(bestDepthBtn, e)){
										 unmarkAll(minimax1Btn, minimax2Btn, minimax3Btn, minimax4Btn, bestDepthBtn);
										 reloadImgToBtn(bestDepthBtn, bestDepthPressImgFile);
										 game->minimaxDepth = 5;
									 }
									 if ((e.button.button == SDL_BUTTON_LEFT) && BtnArea(whiteBtn, e)){
										 reloadImgToBtn(whiteBtn, whitePressImgFile);
										 reloadImgToBtn(blackBtn, blackImgFile);
										 game->userColor = WHITE;
										 game->computerColor = BLACK;
									 }
									 if ((e.button.button == SDL_BUTTON_LEFT) && BtnArea(blackBtn, e)){
										 reloadImgToBtn(blackBtn, blackPressImgFile);
										 reloadImgToBtn(whiteBtn, whiteImgFile);
										 game->userColor = BLACK;
										 game->computerColor = WHITE;
									 }




			}

		}
		SDL_Delay(0);
	}



	// free allocated mem
	freePanel(topPanel);
	freeBtn(continueBtn);
	freeBtn(minimaxDepthBtn);
	freeBtn(minimax1Btn);
	freeBtn(minimax2Btn);
	freeBtn(minimax3Btn);
	freeBtn(minimax4Btn);
	freeBtn(bestDepthBtn);
	freeBtn(whiteBtn);
	freeBtn(blackBtn);
	freeLbl(topPanelLbl);
	freeBtn(userColorBtn);

	return retVal;
}