Ejemplo n.º 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");
      }
    }
  }
}
Ejemplo n.º 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);
  }
}
Ejemplo n.º 3
0
int process_user_input(int fd, char user_input[MAX_ARGUMENT_CNT][MAX_ARGUMENT_LEN], const struct command cmd_list[], int current_index)
{
	char save_buffer[CMD_SIZE] = "";
	const struct command *cmd;
	for (cmd = cmd_list; cmd->name; ++cmd)
	{
		if (strcmp(user_input[current_index], cmd->name) == 0)
		{
			return cmd->sub_cmd ? process_user_input(fd, user_input, cmd->sub_cmd, current_index+1)
								: cmd->func(fd, user_input, sizeof(user_input), save_buffer, sizeof(save_buffer), current_index+1);
		}
	}
	printf("Invalid command! Type \"help\" for a list of commands\n");
	return -1;
}
Ejemplo n.º 4
0
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");
      }
    }
  }
}
Ejemplo n.º 5
0
//-----------------------------------------------------------------------------------------------
//example for detach
void edit_document(std::string const& filename)
{
    open_document_and_display_gui(filename);
    while(!done_editing())
    {
        user_command cmd= get_user_input();
        if(cmd.type == open_new_document)
        {
            std::string const new_name = get_filename_from_user();
            std::thread t(edit_docment,new_name);
            t.detach();
        }
        else
        {
            process_user_input(cmd);
        }
    }
}
Ejemplo n.º 6
0
void child_handler(void)
{
    pid_t pid = getpid();
    int fifo_fd;

    int result;
    char user_input[MAX_DATA_LENGTH];

    struct message_struct tx_data;

    printf("[CHILD] Started with PID=%d\n", pid);

    // Check for existance of fifo by attempting to access it
    if (access(FIFO_2_NAME, F_OK) == -1)
    {
        // Create the fifo if it does not exist
        result = mkfifo(FIFO_2_NAME, 0777);
        if (result != 0)
        {
            fprintf(stderr, "[CHILD] Could not create fifo %s\n", FIFO_2_NAME);
            exit(EXIT_FAILURE);
        }
    }

    // Opens the writing end of the fifo
    fifo_fd = open(FIFO_2_NAME, O_WRONLY);
    if (fifo_fd == -1)
    {
        fprintf(stderr, "[CHILD] open failed with error: %d\n", errno);
        exit(EXIT_FAILURE);
    }

    printf("[CHILD] Connected to Controller via FIFO\n");

    while (g_running_flag)
    {
        char *result = fgets(user_input, MAX_DATA_LENGTH, stdin);
        int len = strlen(user_input);
        if (result != NULL && user_input[len-1] == '\n')
        {
            user_input[len-1] = '\0';
        }

        // Break out (end program) if SIGINT is raised
        if (!g_running_flag)
        {
            break;
        }

        memset((void *)&tx_data, 0, sizeof(tx_data));

        // Process query
        if (process_user_input(&tx_data, user_input) == -1)
        {
            printf("[CHILD] Malformed query. Please try again...\n");
            continue;
        }

        // Send query to controller
        printf("[CHILD] Sending query to Controller\n");
        if (write(fifo_fd, (void *)&tx_data, sizeof(tx_data)) == -1)
        {
            fprintf(stderr, "[CHILD] write failed with error: %d\n", errno);
            exit(EXIT_FAILURE);
        }
    }

    close(fifo_fd);
}
Ejemplo n.º 7
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;
                }
            }
        }
    }
}
Ejemplo n.º 8
0
static void tool_send_message(char *optarg)
{
    printf("tool_send_message(%s)\n",optarg);
    process_user_input(optarg);
}
Ejemplo n.º 9
0
Archivo: peer.c Proyecto: SunnyQ/cmu
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();
    }
}
Ejemplo n.º 11
0
Archivo: peer.c Proyecto: aglqcs/P2P_CC
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);
      }
    }
  }
}