Exemple #1
0
void peer_run(bt_config_t *config) {
  struct sockaddr_in myaddr;
  fd_set readfds;
  struct user_iobuf *userbuf;
  // get my id from config
  myId = config->identity;
  // parse peer list file
  parsePeerFile(config->peer_list_file);
  // parse has chunk file
  parseChunkFile(config->has_chunk_file, &haschunkList);
  // parse master chunk file to get the path of master data file
  parseMasterChunkFile(config->chunk_file);		
  printf("The master data file name: %s\n", masterDataFilePath);
	
  maxCon = config->max_conn;// rename max connections
  getRetryTimer = 0;

  if ((userbuf = create_userbuf()) == NULL) {
    perror("peer_run could not allocate userbuf");
    exit(-1);
  }
  
  if ((sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP)) == -1) {
    perror("peer_run could not create socket");
    exit(-1);
  }
  
  bzero(&myaddr, sizeof(myaddr));
  myaddr.sin_family = AF_INET;
  myaddr.sin_addr.s_addr = htonl(INADDR_ANY);
  myaddr.sin_port = htons(config->myport);
  
  if (bind(sock, (struct sockaddr *) &myaddr, sizeof(myaddr)) == -1) {
    perror("peer_run could not bind socket");
    exit(-1);
  }
  
  spiffy_init(config->identity, (struct sockaddr *)&myaddr, sizeof(myaddr));
  printf("I am peer %d, %s:%d\n",myId, inet_ntoa(myaddr.sin_addr) ,ntohs(myaddr.sin_port));
  while (1) {
    int nfds;
    FD_SET(STDIN_FILENO, &readfds);
    FD_SET(sock, &readfds);
    
    nfds = select(sock+1, &readfds, NULL, NULL, NULL);
    
    if (nfds > 0) {
      if (FD_ISSET(sock, &readfds)) {
		process_inbound_udp(sock);
      }
      
      if (FD_ISSET(STDIN_FILENO, &readfds)) {
		//printf("going to process_user_input\n");
		process_user_input(STDIN_FILENO, userbuf, handle_user_input,
			   "Currently unused");
      }
    }
  }
}
Exemple #2
0
void peer_run(bt_config_t *config)
{
  int sock;
  struct sockaddr_in myaddr;
  struct user_iobuf *userbuf;

  if ((userbuf = create_userbuf()) == NULL) {
    perror("User buffer could not be allocated!");
    exit(-1);
  }

  if ((sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP)) == -1) {
    perror("Socket could not be created!");
    exit(-1);
  }

  bzero(&myaddr, sizeof(myaddr));
  myaddr.sin_family = AF_INET;
  myaddr.sin_addr.s_addr = htonl(INADDR_ANY);
  myaddr.sin_port = htons(config->myport);

  if (bind(sock, (struct sockaddr *) &myaddr, sizeof(myaddr)) == -1) {
    perror("Socket binding failed!");
    exit(-1);
  }

  spiffy_init(config->identity, (struct sockaddr *)&myaddr, sizeof(myaddr));
  struct timeval timeout;
  while (1) {
    int nfds;
    fd_set readfds;
    FD_ZERO(&readfds);
    if(idle == 1)
      FD_SET(STDIN_FILENO, &readfds);
    FD_SET(sock, &readfds);

    timeout.tv_sec = 1;
    timeout.tv_usec = 0;

    nfds = select(sock + 1, &readfds, NULL, NULL, &timeout);
    
    if (nfds > 0) {
      if (FD_ISSET(sock, &readfds)) {
	process_inbound_udp(sock);
      }
      if (FD_ISSET(STDIN_FILENO, &readfds) && idle == 1) {
	process_user_input(STDIN_FILENO,
			   userbuf,
			   handle_user_input,
			   "Currently unused");
      }
    }
    flushQueue(sock, nonCongestQueue);
    flushUpload(sock);
    flushDownload(sock);
  }
}
void peer_run(bt_config_t *config) {

  struct sockaddr_in myaddr;
  fd_set readfds;
  struct user_iobuf *userbuf;
  
  if ((userbuf = create_userbuf()) == NULL) {
    perror("peer_run could not allocate userbuf");
    exit(-1);
  }
  
  if ((sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP)) == -1) {
    perror("peer_run could not create socket");
    exit(-1);
  }
  
  bzero(&myaddr, sizeof(myaddr));
  myaddr.sin_family = AF_INET;
  myaddr.sin_addr.s_addr = htonl(INADDR_ANY);
  myaddr.sin_port = htons(config->myport);
  
  if (bind(sock, (struct sockaddr *) &myaddr, sizeof(myaddr)) == -1) {
    perror("peer_run could not bind socket");
    exit(-1);
  }
  
  spiffy_init(config->identity, (struct sockaddr *)&myaddr, sizeof(myaddr));
  
  while (1) {
    int nfds;
    FD_SET(STDIN_FILENO, &readfds);
    FD_SET(sock, &readfds);
    
    nfds = select(sock+1, &readfds, NULL, NULL, NULL);
    
    if (nfds > 0) {
      if (FD_ISSET(sock, &readfds)) {
	process_inbound_udp(sock);
      }
      
      if (FD_ISSET(STDIN_FILENO, &readfds)) {
	process_user_input(STDIN_FILENO, userbuf, handle_user_input,
			   "Currently unused");
      }
    }
  }
}
Exemple #4
0
void peer_run() {
    init_my_timer();

    int sock;
    struct sockaddr_in myaddr;
    fd_set readfds;
    struct user_iobuf *userbuf;
    
    if ((userbuf = create_userbuf()) == NULL) {
        perror("peer_run could not allocate userbuf");
        exit(-1);
    }
    
    if ((sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP)) == -1) {
        perror("peer_run could not create socket");
        exit(-1);
    }
    
    bzero(&myaddr, sizeof(myaddr));
    myaddr.sin_family = AF_INET;
    myaddr.sin_addr.s_addr = htonl(INADDR_ANY);
    myaddr.sin_port = htons(config.myport);
    
    if (bind(sock, (struct sockaddr *) &myaddr, sizeof(myaddr)) == -1) {
        perror("peer_run could not bind socket");
        exit(-1);
    }
    
    struct timeval time_out;
    int stdin_closed = 0;
    config.sock_fd = sock;

    bt_peer_t *p = bt_peer_info(&config, config.identity);
    spiffy_init(config.identity, (struct sockaddr *)&(p->addr), sizeof(struct sockaddr_in));

    init_responser(&responser, config.has_chunk_file, config.chunk_file);
    int i;
    for(i=0; i<BT_MAX_UPLOAD; ++i){
        senders[i].is_idle = 1;
    }

    //open window size change log
    window_size_log = fopen("problem2-peer.txt", "w+");

    long last_time = my_get_time();

    while (1) {
        int nfds;
        FD_ZERO(&readfds);
        if(!stdin_closed) FD_SET(STDIN_FILENO, &readfds);
        FD_SET(sock, &readfds);
        time_out.tv_sec = 1;
        time_out.tv_usec = 0;
        
        nfds = select(sock+1, &readfds, NULL, NULL, &time_out);

        long cur_time = my_get_time();
        if(cur_time - last_time > TICKS_PER_MILISECOND * 1000){
            last_time = cur_time;
            requstor_timeout(&requestor);
            for(i=0; i<BT_MAX_UPLOAD; ++i){
                if(!senders[i].is_idle)
                    ctl_udp_time_out(&senders[i]);
            }
        }
        
        if (nfds > 0) {
            if (FD_ISSET(sock, &readfds)) {
                process_inbound_udp(sock);
            }
            
            if (FD_ISSET(STDIN_FILENO, &readfds)) {
                int ret = process_user_input(STDIN_FILENO, userbuf, handle_user_input, "Currently unused");
                if(ret == -1){
                    stdin_closed = 1;
                }
            }
        }
    }
}
Exemple #5
0
void peer_run(bt_config_t *config)
{
    struct sockaddr_in myaddr;
    fd_set readfds;
    struct user_iobuf *userbuf;
    struct timeval timeout;
    int i, j;

    if ((userbuf = create_userbuf()) == NULL )
    {
        perror("peer_run could not allocate userbuf");
        exit(-1);
    }

    if ((config->sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP)) == -1)
    {
        perror("peer_run could not create socket");
        exit(-1);
    }

    bzero(&myaddr, sizeof(myaddr));
    myaddr.sin_family = AF_INET;
    myaddr.sin_addr.s_addr = htonl(INADDR_ANY );
    myaddr.sin_port = htons(config->myport);

    if (bind(config->sock, (struct sockaddr *) &myaddr, sizeof(myaddr)) == -1)
    {
        perror("peer_run could not bind socket");
        exit(-1);
    }

    spiffy_init(config->identity, (struct sockaddr *) &myaddr, sizeof(myaddr));

    timeout.tv_sec = 1;
    while (1)
    {
        int nfds;
        FD_SET(STDIN_FILENO, &readfds);
        FD_SET(config->sock, &readfds);

        nfds = select(config->sock + 1, &readfds, NULL, NULL, &timeout);

        if (nfds > 0)
        {
            if (FD_ISSET(config->sock, &readfds))
                process_inbound_udp(config->sock, config);

            if (FD_ISSET(STDIN_FILENO, &readfds))
                process_user_input(STDIN_FILENO, userbuf, handle_user_input,
                        config);
        }
        // check timeout
        if (timeout.tv_sec == 0)
        {
            for (i = 0; i < BT_MAX_PEERS; i++)
            {
                for (j = 0; j < BT_CHUNK_SIZE; j++)
                {
                    if (TTL[i][j] != -1)
                        TTL[i][j]++;

                    // set the DATA timeout to 2
                    if (TTL[i][j] == 2)
                    {
                        dprintf(STDOUT_FILENO, "retransmitting on timeout %d\n",
                                j + 1);
                        lastSent[i] = j; // lastACKed should be the same as j
                        sendData(*getAddr(config, i), config);
                        dup_ack[i] = 0;
                        break;
                    }
                }
                if (GETTTL[i] != -1)
                    GETTTL[i]++;

                // set the GET timeout to 5
                if (GETTTL[i] == 5)
                {
                    if (++numGetMisses[i] > 3)
                    {
                        numGetMisses[i] = 0;
                        numDataMisses[i] = -1;
                        jobs[i] = -1;
                        dup_ack[i] = 0;
                        GETTTL[i] = -1;
                        windowSize[i] = 0;
                        congestionState[i] = -1;
                        if ((work_queue = process_get(chunkf, outf)) == NULL )
                        {
                            perror("I/O error");
                            exit(-1);
                        }
                        GETTTL[i] = -1;
                        broadcast_query(work_queue, config);
                    }
                    else
                    {
                        dprintf(STDOUT_FILENO, "resending GET\n");
                        if ((work_queue = process_get(chunkf, outf)) == NULL )
                        {
                            perror("I/O error");
                            exit(-1);
                        }
                        sendGetSW(config->sock, *getAddr(config, i));
                    }
                }

                if (numDataMisses[i] != -1)
                    numDataMisses[i]++;

                if (numDataMisses[i] > 15)
                {
                    numDataMisses[i] = -1;
                    jobs[i] = -1;
                    dup_ack[i] = 0;
                    GETTTL[i] = -1;
                    windowSize[i] = 0;
                    congestionState[i] = -1;
                    numGetMisses[i] = 0;
                    resetCCRTT();
                    if ((work_queue = process_get(chunkf, outf)) == NULL )
                    {
                        perror("I/O error");
                        exit(-1);
                    }
                    broadcast_query(work_queue, config);
                }
            }
            timeout.tv_sec = 1;
        }
    }
}
void peer_run(bt_config_t *config) {
    int sock;
    struct sockaddr_in myaddr;
    fd_set readfds;
    struct user_iobuf *userbuf;
    
    if ((userbuf = create_userbuf()) == NULL) {
        perror("peer_run could not allocate userbuf");
        exit(-1);
    }
    
    if ((sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP)) == -1) {
        perror("peer_run could not create socket");
        exit(-1);
    }
    
    bzero(&myaddr, sizeof(myaddr));
    myaddr.sin_family = AF_INET;
    //myaddr.sin_addr.s_addr = htonl(INADDR_ANY);
    inet_aton("127.0.0.1", (struct in_addr*)&myaddr.sin_addr.s_addr);
    myaddr.sin_port = htons(config->myport);
    
    if (bind(sock, (struct sockaddr *) &myaddr, sizeof(myaddr)) == -1) {
        perror("peer_run could not bind socket");
        exit(-1);
    }
    global_socket = sock;
    spiffy_init(config->identity, (struct sockaddr *)&myaddr, sizeof(myaddr));
    init_window_log();
    init_hasChunks(config->has_chunk_file);
    init_connections();

    struct timeval last_flood_whohas_time;
    gettimeofday(&last_flood_whohas_time, NULL);
    while (1) {
        int nfds;
        FD_SET(STDIN_FILENO, &readfds);
        FD_SET(sock, &readfds);

        struct timeval select_timeout;
        select_timeout.tv_sec = 0;
        select_timeout.tv_usec = 300000;

        nfds = select(sock+1, &readfds, NULL, NULL, &select_timeout);
        
        if (nfds > 0) {
            if (FD_ISSET(sock, &readfds)) {
    	       process_inbound_udp(sock);
            }
          
            if(FD_ISSET(STDIN_FILENO, &readfds)) {
    	        process_user_input(STDIN_FILENO, userbuf, handle_user_input,
    		           "Currently unused");
            }
        }
        providers_timeout();
        receivers_timeout();
        /* this is a regular send task, driven purely by time */
        all_provider_connection_send_data(provider_connection_head);

        /* every few seconds, if there are receiver connection available, flooding whohas*/
        if(get_time_diff(&last_flood_whohas_time) > WHOHAS_FLOOD_INTERVAL_MS){
            flood_whohas();
            gettimeofday(&last_flood_whohas_time, NULL);
        }
        /* if there is no more receiver connection and the job is not NULL
         * check to see if the job has finished
         */
        finish_job();
    }
}
Exemple #7
0
void peer_run(bt_config_t *config) {
  struct sockaddr_in myaddr;
  fd_set readfds;
  struct user_iobuf *userbuf;
  struct timeval tv;
  tv.tv_sec = 3;
  tv.tv_usec = 0;

  if ((userbuf = create_userbuf()) == NULL) {
    perror("peer_run could not allocate userbuf");
    exit(-1);
  }
  
  if ((sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP)) == -1) {
    perror("peer_run could not create socket");
    exit(-1);
  }
  
  bzero(&myaddr, sizeof(myaddr));
  myaddr.sin_family = AF_INET;
  myaddr.sin_addr.s_addr = htonl(INADDR_ANY);
  myaddr.sin_port = htons(config->myport);
  
  if (bind(sock, (struct sockaddr *) &myaddr, sizeof(myaddr)) == -1) {
    perror("peer_run could not bind socket");
    exit(-1);
  }
  
  spiffy_init(config->identity, (struct sockaddr *)&myaddr, sizeof(myaddr));
  
  while (1) {
    int nfds;
    FD_SET(STDIN_FILENO, &readfds);
    FD_SET(sock, &readfds);
    
    nfds = select(sock+1, &readfds, NULL, NULL, &tv);
    
    if (nfds > 0) {
      if (FD_ISSET(sock, &readfds)) {
	process_inbound_udp(sock);
      }
      
      if (FD_ISSET(STDIN_FILENO, &readfds)) {
	process_user_input(STDIN_FILENO, userbuf, handle_user_input,
			   "Currently unused");
      }
    }

    /* loop all the chunks to check if the node for this chunk fails */
    data_packet_list_t *potential_whohas = re_generateWhohas(config);
    if( NULL != potential_whohas){
      printf("=======================\n");
      printf("re_generateWhohas again\n");
      printf("=======================\n");

      potential_whohas = reverseList(potential_whohas);
      data_packet_list_t *p;
      for( p = potential_whohas; p != NULL; p = p->next){
        data_packet_t *who = p->packet;
        broadcast(who, config);
      }
    }

    /* loop all the data packet send and check if packet timeout*/
    packet_tracker_t *head;
    for( head = p_tracker ; head != NULL ;head = head->next ){
      if( -1 == timer_expired(head) ){
        /* retransmit*/
        data_packet_t *packet = head->packet;
        /* ignore dummy head */
        if( head->seq == -441) 
          continue; 
        
        printf("DEBUG timer out seq = %d \n", head->seq);
        packet->header.seq_num = htonl(head->seq);


        int p = spiffy_sendto(sock, packet,  ntohs(packet->header.packet_len), 0, (struct sockaddr *)head->from, sizeof(struct sockaddr));

        if( p < 0){
          printf("Timeout resend error = %d\n", p);
        }
        /* reduce the ssthresh */
        process_packet_loss( head->packet->header.ack_num );
        /* reset timer */
        head->send_time = time(NULL);
      }
    }
  }
}