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); } } }