void bt_parse_command_line(bt_config_t *config) { int c, old_optind; bt_peer_t *p; int argc = config->argc; char **argv = config->argv; DPRINTF(DEBUG_INIT, "bt_parse_command_line starting\n"); old_optind = optind; while ((c = getopt(argc, argv, _bt_optstring)) != -1) { switch(c) { case 'h': bt_help(); exit(0); case 'd': if (set_debug(optarg) == -1) { fprintf(stderr, "%s: Invalid debug argument. Use -d list to see debugging options.\n", argv[0]); exit(-1); } break; case 'p': strcpy(config->peer_list_file, optarg); break; case 'c': strcpy(config->has_chunk_file, optarg); break; case 'f': strcpy(config->chunk_file, optarg); break; case 'm': config->max_conn = atoi(optarg); break; case 'i': config->identity = atoi(optarg); break; default: bt_usage(); exit(-1); } } bt_parse_peer_list(config); if (config->identity == 0) { fprintf(stderr, "bt_parse error: Node identity must not be zero!\n"); exit(-1); } if ((p = bt_peer_info(config, config->identity)) == NULL) { fprintf(stderr, "bt_parse error: No peer information for myself (id %d)!\n", config->identity); exit(-1); } config->myport = ntohs(p->addr.sin_port); assert(config->identity != 0); assert(strlen(config->chunk_file) != 0); assert(strlen(config->has_chunk_file) != 0); optind = old_optind; }
int send_packet(int peer, data_packet_t * packet){ static char buf[BT_MAX_PKT_SIZE]; memcpy(buf + sizeof(header_t), packet->data, packet->header.packet_len); packet->header.packet_len += sizeof(header_t); memcpy(buf, (const char *)&packet->header, sizeof(header_t)); bt_peer_t * pinfo = bt_peer_info(&config, peer); if(pinfo == NULL) return -1; spiffy_sendto(config.sock_fd, buf, packet->header.packet_len, 0, (struct sockaddr *) &pinfo->addr, sizeof(pinfo->addr)); return 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; } } } } }