예제 #1
0
void Container::modeChanged()
{
    if (isReceived() && CorePlugin::m_plugin->getContainerMode())
        QTimer::singleShot(0, this, SLOT(close()));
    if (CorePlugin::m_plugin->getContainerMode() == 0){
        list<UserWnd*> wnds = m_tabBar->windows();
        for (list<UserWnd*>::iterator it = wnds.begin(); it != wnds.end(); ++it){
            if ((*it) != m_tabBar->currentWnd())
                delete (*it);
        }
    }
}
예제 #2
0
int main(int argc, char* argv[]) {

    // Checks the program parameters
    int opt;
    map_options(argc, argv, &opt);
    argc -= optind;
    argv += optind;
    if(argc != 2) {
        usage();
        exit(EXIT_FAILURE);
    }

    // Destination file opening
    if(fd == -1) {
        if((fd = open(filename, O_RDWR|O_CREAT, S_IRWXU|S_IRWXG)) < 0) {
            die("Error opening destination file");
        }
    }

    // Port and adresses string init
    char *addr_str = argv[0];
    char *port_str = argv[1];

    if(verbose) {
        printf("\nListenning address \'%s\' on port \'%s\'\n", addr_str,
        port_str);
    }

    // Resolve the address passed to the program
    int result;
    if((result = getaddrinfo(addr_str, port_str, &hints, &address)) < 0) {
        printf("Error resolving address %s - code %i", addr_str, result);
        freeaddrinfo(address);
        close(fd);
        exit(EXIT_FAILURE);
    }

    // initialize the socket used by the receiver
    sock_id = init_host(address, receiver);
    if(sock_id == -1) {
        die("Error creating socket");
    }
    
    packetstruct tmp_packet;           // stores the just received packet
                                       // last in-sequence acknowledge packet
    int lastack = SEQSPAN-1;					// last in-sequence acknowledge packet
    int bufferPos = 0;                 // Position following lastack in the buffer

                                       // of paylod has been received.

    int bufferFill = 0;     //nb of unwritten valid packets received
    int idx;                //position in the buffer for the received packet
    ssize_t size = PAYLOADSIZE;     // Size of the received payload
    int is_valid;
	memset(window,0,BUFFSIZE*sizeof(window_slot));
    while( !isReceived(lastack) ) {
        /* blocking receive - we are waiting for a frame */
        src_len = sizeof(src_host);
        if(recvfrom(sock_id, (void *) &tmp_packet, sizeof(tmp_packet), 0,
            (struct sockaddr*) &src_host, &(src_len)) != sizeof(packetstruct)) {
                die("Error while receiving packet");
        }

        is_valid = packet_valid(&tmp_packet);
        
        /* only if the packet is valid */
        if(is_valid == 1) {

            size = tmp_packet.length;

            if(verbose) {
                printf("Received a %zd-byte type %u packet (seq %u)\n", size,
                tmp_packet.type, tmp_packet.seqnum);
            }

            // Is the sequence number in the receive window ?
            if((idx = idx_in_window(tmp_packet.seqnum,lastack,bufferPos)) != -1) {
                if(bufferFill<BUFFSIZE && !window[idx].received){
                	recv_buffer[idx] = tmp_packet;  // copy packet to rcv_buffer
                	window[idx].received = true;    // mark the frame as received
                	bufferFill++;                   // add 1 more received packet
                	/* If this is the last packet from the original file
                	 * (ie, payload with a size smaller than 512 Bytes */
                 	if(size < PAYLOADSIZE) {
                    	 if(verbose){
                    	 	printf("Packet with smaller PAYLOADSIZE received, end of file detected\n");
                    	 }
                    	 lastseq = tmp_packet.seqnum;
                 	}

                	// Try to empty the in-sequence received packets
                	if(flush_frames(fd, &lastack, &bufferPos, &bufferFill)) {
                	    die("Error writing packets to file");
                	}
				}
				else{printf("BufferFill : %d The receiving buffer is full\n",bufferFill);}
                // Send an acknowledgement
                acknowledge(lastack);
            }
            // Else, we do nothing and discard it...
            else if(verbose) {
                printf("Received an out-of-window packet (seq:%u)(lastack:%d)\n ", tmp_packet.seqnum,lastack);
            }
        }
        else{
        	if(verbose){
        		printf("Received a corrupted packet !\n");
        	}
        	acknowledge(lastack);
        }
        

    }
    // End of transmission : resend acks if the sender keeps sending data
    int received;
    int timeDiff = EOTDELAY-1;
    int EOTstart = clock();
	while(timeDiff<EOTDELAY){
		received = 0;
		fcntl(sock_id, F_SETFL, O_NONBLOCK);    // make receive non-blocking
        received = recvfrom(sock_id,(void*)(&tmp_packet),sizeof(tmp_packet),0,NULL,NULL);
        if(received != sizeof(packetstruct) && errno != EAGAIN) {
            die("Error receiving");
        }
        if(received==sizeof(packetstruct)){
        	acknowledge(lastack);
        }
        timeDiff = (clock()-EOTstart)*1000/CLOCKS_PER_SEC;
    }
    if(verbose) {
        printf("File successfully received\n");
    }

    freeaddrinfo(address);
    close(sock_id);
    exit(EXIT_SUCCESS);
}
예제 #3
0
파일: Peer.c 프로젝트: leogomes/peer2peer
void trataRecebimento(int peerfd)
{
	//int ret;
	Header h;
	IdPeer vizinho;
	char dadosPacote[TAM_MAX_SEG];
	
//	printf("Le Pacote\n");
	lePacote(peerfd, h, dadosPacote);
	
//	printf("Tipo de msg: %d\n\n", h.tipoMsg);
	
	#ifdef DEBUG
		char tmp[TAM_STR_IP + 1];
		IdPeer2StrIp(tmp, h.remetente);
		printf("****** MENSAGEM LIDA PELO SOCKET ******\n");
		printf("Origem de mensagem: %s\n", tmp);
		printf("Tipo de mensagem: %d\n", h.tipoMsg);
		printf("Tamanho dos dados: %d\n", h.tamDados);
		printf("Numero de sequencia: %d\n", h.numSeq);
		printf("***************************************\n");
	#endif
	
	if(!MensagemEhValida(h.tipoMsg, h.numSeq) )
	{
		printf("\n\n*********************************************************************\n");					
		printf("****************Mensagem descartada pois o timer expirou!*************\n");					
		printf("*********************************************************************\n");					
		return;
	} 
	
	// Verifico se a mensagem já foi recebida.
	if(isReceived(h)){
//		#ifdef DEBUG1
			printf("\nMsg já foi recebida (numSeq = %d\tTipo: %d)\n",h.numSeq, h.tipoMsg);
//		#endif
		return;
		
	}
	
	atualizaIdPeerTabelaSocket(peerfd, h.remetente);
	
	// Verifico se a mensagem deve ser roteada.
	char r[TAM_STR_IP+1];
	char d[TAM_STR_IP+1];
	
	IdPeer2StrIp(r, h.remetente);
	IdPeer2StrIp(d, h.destino);
	
//	if(h.tipoMsg == GET || h.tipoMsg == GETR)
//		printf("\nRem: %s\tDest.: %s\tNumSeq: %d\tTipo: %d\n\n",r, d, h.numSeq, h.tipoMsg);
	
	
	if(!IdPeerIguais(h.destino, DadosAplicacao.meuIp)){
		
		if(h.tipoMsg == GET){
			if(!buscaIdPeerVizinho(peerfd, vizinho)){
				vizinho = h.remetente;
			}
			InsereTabRoteamento(vizinho, h.remetente, h.tipoMsg, h.numSeq);
			distribuiGet(h, dadosPacote);
		}
		else
		{
			encaminha(peerfd, h, dadosPacote);
		}
		return;
	}

	// Insere a mensagem na tabela de roteamento.
	if(ehMensagemRoteada(h.tipoMsg))
	{
		if(!buscaIdPeerVizinho(peerfd, vizinho)){
			vizinho = h.remetente;
		}
		
		#ifdef DEBUG_ROT
			char tmp[TAM_STR_IP + 1];
			IdPeer2StrIp(tmp, h.remetente);
			printf("\n\nVIZINHO ENCONTRADO: %s\n\n", tmp);
		#endif
		
		InsereTabRoteamento(vizinho, h.remetente, h.tipoMsg, h.numSeq);
	}
	
//	printf("\nDEBUG\n");
//	printf("switch\n");
	
	// Tratamento da mensagem.
	switch( h.tipoMsg ){

		case HELLO:
			
			#ifdef DEBUG_HELLO
				puts("CHAMOU HELLO!");
			#endif
			trataHello(peerfd, h, dadosPacote);
			break;

		case JOIN:
			trataJoin(peerfd, h, dadosPacote);
			break;

		case JOINR:
			trataJoinResponse(peerfd, h, dadosPacote);
			break;

		case SEARCH:
			trataSearch(peerfd, h, dadosPacote);
			break;

		case SEARCHR:
			trataSearchResponse(peerfd, h, dadosPacote);
			break;

		case GET:
			trataGet(peerfd, h, dadosPacote);
			break;

		case GETR:
			trataGetResponse(peerfd, h, dadosPacote);
			break;

		default:
			puts("\nERRO: Ocorreu algum erro no cabeçalho da mensagem.\n");
			exit(1);
	}
}