void handle_timedout_frames(Sender * sender, LLnode ** outgoing_frames_head_ptr) { //TODO: Suggested steps for handling timed out datagrams // 1) Iterate through the sliding window protocol information you maintain for each receiver // 2) Locate frames that are timed out and add them to the outgoing frames // 3) Update the next timeout field on the outgoing frames int i = 0; struct timeval *time = malloc(sizeof(struct timeval)); gettimeofday(time, NULL); for (;i < SWS; i++) { if (sender->sendQ[i].inuse == 1) { long timediff = timeval_usecdiff(time, sender->sendQ[i].timeout); if (timediff < 0) { Frame * outgoing_frame = (Frame *) sender->sendQ[i].msg; char * outgoing_msg = convert_frame_to_char(outgoing_frame); //fprintf(stderr, "%d frame timing out\n", outgoing_frame->seqNum); append_crc(outgoing_msg,MAX_FRAME_SIZE); ll_append_node(outgoing_frames_head_ptr, outgoing_msg); setTimeOutTime(sender->sendQ[i].timeout); //free(outgoing_frame); } } } }
void handle_timedout_frames(Sender * sender, LLnode ** outgoing_frames_head_ptr) { //Iterate through the send queue int i; for(i=0; i < glb_receivers_array_length; i++) { send_Q ** curr_node = sender->send_q_head[i]; int lar = sender->LAR[i]; int lfs = sender->LFS[i]; while(lar != lfs) { lar = (lar + 1) % (MAX_SEQ_NUM + 1); send_Q * curr = curr_node[lar%WS]; Frame * sent_frame = curr->frame; struct timeval * tv = curr->frame_timeout; struct timeval now; gettimeofday(&now, NULL); //A frame with set timeout is found if(tv != NULL) { //Update timeout field for old, timed out frames if(timeval_usecdiff(tv, &now) > 0) { fprintf(stderr, "TIMEDOUT DATA %s being resent\n", sent_frame->data); char * outframe_char_buf = convert_frame_to_char(sent_frame); ll_append_node(outgoing_frames_head_ptr, outframe_char_buf); calculate_timeout(curr->frame_timeout); } } //Update timeout field for fresh outgoing frames if(tv == NULL && sent_frame != NULL) { fprintf(stderr, "FRESH DATA %s being sent\n", sent_frame->data); curr->frame_timeout = (struct timeval *) malloc(sizeof(struct timeval)); calculate_timeout(curr->frame_timeout); } } } }
void handle_input_cmds(Sender * sender, LLnode ** outgoing_frames_head_ptr) { int input_cmd_length = ll_get_length(sender->input_cmdlist_head); //Split the message if it's too long ll_split_head(&sender->input_cmdlist_head, FRAME_PAYLOAD_SIZE - 1); //Recheck the command queue length to see if stdin_thread dumped a command on us input_cmd_length = ll_get_length(sender->input_cmdlist_head); while (input_cmd_length > 0) { //Pop a node off and update the input_cmd_length LLnode * ll_input_cmd_node = ll_pop_node(&sender->input_cmdlist_head); input_cmd_length = ll_get_length(sender->input_cmdlist_head); //Cast to Cmd type and free up the memory for the node Cmd * outgoing_cmd = (Cmd *) ll_input_cmd_node->value; free(ll_input_cmd_node); //Convert uint16_t to string char* src = malloc(MAC_ADDR_SIZE); char* dst = malloc(MAC_ADDR_SIZE); sprintf(src, "%u", outgoing_cmd->src_id); sprintf(dst, "%u", outgoing_cmd->dst_id); int id = atoi(dst); if(send_q_size(sender, id) < WS) { Frame * outgoing_frame = (Frame *) malloc (sizeof(Frame)); //Populate the outgoing frame's fields strncpy(outgoing_frame->receiver_addr, dst, MAC_ADDR_SIZE); strncpy(outgoing_frame->sender_addr, src, MAC_ADDR_SIZE); strncpy(outgoing_frame->data, outgoing_cmd->message, FRAME_PAYLOAD_SIZE); //Assign seqnum to the outgoing frame outgoing_frame->seqnum = sender->seqnum[id]; char* raw_char_buf = convert_frame_to_char(outgoing_frame); //Assign crc to the outgoing frame outgoing_frame->crc = crc8(raw_char_buf, MAX_FRAME_SIZE); free(raw_char_buf); //At this point, we don't need the outgoing_cmd free(outgoing_cmd->message); free(outgoing_cmd); //Update LFS sender->LFS[id] = sender->seqnum[id]; //Convert the message to the outgoing_charbuf and send it char * outgoing_charbuf = convert_frame_to_char(outgoing_frame); //fprintf(stderr, "MESSAGE %s SENT\n", outgoing_frame->data); ll_append_node(outgoing_frames_head_ptr, outgoing_charbuf); sender->send_q_head[id][sender->seqnum[id] % WS]->frame = malloc(sizeof(Frame)); //Store the sent frame in the sent queue memcpy(sender->send_q_head[id][sender->seqnum[id] % WS]->frame, outgoing_frame, sizeof(Frame)); sender->send_q_head[id][sender->seqnum[id] % WS]->frame_timeout = NULL; free(outgoing_frame); sender->seqnum[id] = (sender->seqnum[id] == 255) ? 0 : sender->seqnum[id] + 1; } else { ll_append_node_at_front(&sender->input_cmdlist_head, outgoing_cmd); input_cmd_length = ll_get_length(sender->input_cmdlist_head); break; } } }
void handle_input_cmds(Sender * sender, LLnode ** outgoing_frames_head_ptr) { //TODO: Suggested steps for handling input cmd // 1) Dequeue the Cmd from sender->input_cmdlist_head // 2) Convert to Frame // 3) Set up the frame according to the sliding window protocol // 4) Compute CRC and add CRC to Frame int input_cmd_length = ll_get_length(sender->input_cmdlist_head); //Recheck the command queue length to see if stdin_thread dumped a command on us input_cmd_length = ll_get_length(sender->input_cmdlist_head); ll_split_head(&sender->input_cmdlist_head, FRAME_PAYLOAD_SIZE - 1); while (input_cmd_length > 0 && sender->sendQSize <= SWS) { // fprintf(stderr, "%d %d\n", input_cmd_length, ll_get_length(sender->input_cmdlist_head)); unsigned char upper = sender->LAR + SWS - 1; //printf("%d %d %d ", sender->LAR, upper, sender->seqNum); if (!((sender->LAR <= upper && sender->seqNum >= sender->LAR && sender->seqNum <= upper) || (sender->LAR > upper && (sender->seqNum >= sender->LAR || sender->seqNum <= upper)))) break; //Pop a node off and update the input_cmd_length LLnode * ll_input_cmd_node = ll_pop_node(&sender->input_cmdlist_head); input_cmd_length = ll_get_length(sender->input_cmdlist_head); //Cast to Cmd type and free up the memory for the node Cmd * outgoing_cmd = (Cmd *) ll_input_cmd_node->value; free(ll_input_cmd_node); //DUMMY CODE: Add the raw char buf to the outgoing_frames list //NOTE: You should not blindly send this message out! // Ask yourself: Is this message actually going to the right receiver (recall that default behavior of send is to broadcast to all receivers)? // Does the receiver have enough space in in it's input queue to handle this message? // Were the previous messages sent to this receiver ACTUALLY delivered to the receiver? int msg_length = strlen(outgoing_cmd->message); if (msg_length > MAX_FRAME_SIZE) { //Do something about messages that exceed the frame size printf("<SEND_%d>: sending messages of length greater than %d is not implemented\n", sender->send_id, MAX_FRAME_SIZE); } else { //This is probably ONLY one step you want Frame * outgoing_frame = (Frame *) malloc (sizeof(Frame)); strcpy(outgoing_frame->data, outgoing_cmd->message); outgoing_frame->src_id = outgoing_cmd->src_id; outgoing_frame->dst_id = outgoing_cmd->dst_id; outgoing_frame->seqNum = sender->seqNum; sender->seqNum++; //if (sender->seqNum >= SWS) // sender->seqNum = 0; //if (sender->seqNum == 5) // sender->seqNum++; //At this point, we don't need the outgoing_cmd free(outgoing_cmd->message); free(outgoing_cmd); //Convert the message to the outgoing_charbuf char * outgoing_charbuf = convert_frame_to_char(outgoing_frame); append_crc(outgoing_charbuf, MAX_FRAME_SIZE); //fprintf(stderr, "Sending %d with data %s\n", outgoing_frame->seqNum,outgoing_frame->data); ll_append_node(outgoing_frames_head_ptr, outgoing_charbuf); int i = 0; for (; i < SWS; i++) { if (sender->sendQ[i].inuse == 0) { sender->sendQSize++; struct timeval * timeout = malloc(sizeof(struct timeval)); setTimeOutTime(timeout); sender->sendQ[i].inuse = 1; sender->sendQ[i].timeout = timeout; sender->sendQ[i].msg =(Frame *)malloc(MAX_FRAME_SIZE); memcpy(sender->sendQ[i].msg,outgoing_frame,MAX_FRAME_SIZE); break; } } free(outgoing_frame); } } }
void handle_input_cmds(Sender * sender, LLnode ** outgoing_frames_head_ptr) { //TODO: Suggested steps for handling input cmd // 1) Dequeue the Cmd from sender->input_cmdlist_head // 2) Convert to Frame // 3) Set up the frame according to the sliding window protocol // 4) Compute CRC and add CRC to Frame int input_cmd_length = ll_get_length(sender->input_cmdlist_head); //Recheck the command queue length to see if stdin_thread dumped a command on us input_cmd_length = ll_get_length(sender->input_cmdlist_head); while (input_cmd_length > 0) { //Pop a node off and update the input_cmd_length LLnode * ll_input_cmd_node = ll_pop_node(&sender->input_cmdlist_head); input_cmd_length = ll_get_length(sender->input_cmdlist_head); //Cast to Cmd type and free up the memory for the node Cmd * outgoing_cmd = (Cmd *) ll_input_cmd_node->value; free(ll_input_cmd_node); //DUMMY CODE: Add the raw char buf to the outgoing_frames list //NOTE: You should not blindly send this message out! // Ask yourself: Is this message actually going to the right receiver (recall that default behavior of send is to broadcast to all receivers)? // Does the receiver have enough space in in it's input queue to handle this message? // Were the previous messages sent to this receiver ACTUALLY delivered to the receiver? int msg_length = strlen(outgoing_cmd->message); if (msg_length > 0) { //init id sender->recv_id = outgoing_cmd->dst_id; //init sender sender->LFS = -1; sender->LAR = -1; sender->SWS = 8; sender->FSS = (msg_length + TEMP_SIZE - 1) / TEMP_SIZE; //sender->FSS = (msg_length + TEMP_SIZE) / TEMP_SIZE; sender->fin = 0;// not fin free(sender->message); sender->message = malloc(msg_length + 1); strcpy(sender->message, outgoing_cmd->message); sender->message_length = msg_length; //init buffer; sender->buffer = (struct Frame**) malloc(8 * sizeof(Frame*)); sender->timestamp = malloc(8 * sizeof(struct timeval)); int i; struct timeval init_time; gettimeofday(&init_time, NULL); init_time.tv_sec += 999999; for (i = 0; i < 8; i++) { sender->buffer[i] = malloc(1); sender->timestamp[i] = init_time; } //print_sender(sender); } else { //This is probably ONLY one step you want Frame * outgoing_frame = (Frame *) malloc (sizeof(Frame)); strcpy(outgoing_frame->data, outgoing_cmd->message); //At this point, we don't need the outgoing_cmd free(outgoing_cmd->message); free(outgoing_cmd); //Convert the message to the outgoing_charbuf char * outgoing_charbuf = convert_frame_to_char(outgoing_frame); //char * outgoing_charbuf = add_chksum(outgoing_frame); ll_append_node(outgoing_frames_head_ptr, outgoing_charbuf); free(outgoing_frame); } } }