Example #1
0
void handle_incoming_acks(Sender * sender,
                          LLnode ** outgoing_frames_head_ptr)
{
    //TODO: Suggested steps for handling incoming ACKs
    //    1) Dequeue the ACK from the sender->input_framelist_head
    //    2) Convert the char * buffer to a Frame data type
    //    3) Check whether the frame is corrupted
    //    4) Check whether the frame is for this sender
    //    5) Do sliding window protocol for sender/receiver pair 
    int incoming_ack_length = ll_get_length(sender->input_framelist_head);
    while (incoming_ack_length > 0) {
        LLnode * ll_ack_node = ll_pop_node(&sender->input_framelist_head);
        incoming_ack_length = ll_get_length(sender->input_framelist_head);
        char * raw_char_buf = (char *) ll_ack_node->value;
        int flag = is_corrupted(raw_char_buf, MAX_FRAME_SIZE);
        //fprintf(stderr, "SFLAG = %d\n", flag);
        if (flag == 0) {
           // fprintf(stderr, "CORRUPTED\n");
            break;
        }
        Frame * ackFrame = convert_char_to_frame(raw_char_buf);
        free (raw_char_buf);

        if (sender->send_id == ackFrame->src_id) {
            if (!((sender->LAR <= sender->seqNum &&
                   ackFrame->seqNum > sender->LAR &&
                   ackFrame->seqNum <= sender->seqNum) ||
                  (sender->LAR > sender->seqNum &&
                  (ackFrame->seqNum > sender->LAR ||
                   ackFrame->seqNum <= sender->seqNum))))
                break;
      //      fprintf(stderr, "Receiver[%d] Ack #%d\n",ackFrame->dst_id, 
        //                                           ackFrame->seqNum);
           // fprintf(stderr, "OLD LAR %d and OLD sendQSize %d\n", sender->LAR, sender->sendQSize);
            do {
                int i = 0;
                for (;i < SWS; i++) {
                    if (sender->sendQ[i].inuse == 1) {
                        Frame * qFrame = (Frame *)sender->sendQ[i].msg;
                        if (qFrame->seqNum == sender->LAR){
                            sender->sendQ[i].inuse = 0;
                            sender->sendQSize--;
                   //         fprintf(stderr, "REMOVING FRAME %d\n", qFrame->seqNum);
                        }
                    }
                }
                sender->LAR++;
            } while (sender->LAR != ackFrame->seqNum);
            //fprintf(stderr, "LAR = %d and SENDQSIZE %d\n", sender->LAR, sender->sendQSize);
        } 
        free (ackFrame);
        free (ll_ack_node);
    }
}
Example #2
0
void handle_incoming_msgs(Receiver * receiver,
                          LLnode ** outgoing_frames_head_ptr)
{
    //TODO: Suggested steps for handling incoming frames
    //    1) Dequeue the Frame from the sender->input_framelist_head
    //    2) Convert the char * buffer to a Frame data type
    //    3) Check whether the frame is corrupted
    //    4) Check whether the frame is for this receiver
    //    5) Do sliding window protocol for sender/receiver pair

    int incoming_msgs_length = ll_get_length(receiver->input_framelist_head);
    while (incoming_msgs_length > 0)
    {
        //Pop a node off the front of the link list and update the count
        LLnode * ll_inmsg_node = ll_pop_node(&receiver->input_framelist_head);
        incoming_msgs_length = ll_get_length(receiver->input_framelist_head);

        //DUMMY CODE: Print the raw_char_buf
        //NOTE: You should not blindly print messages!
        //      Ask yourself: Is this message really for me?
        //                    Is this message corrupted?
        //                    Is this an old, retransmitted message?           
        char * raw_char_buf = (char *) ll_inmsg_node->value;
        
	if (chksum_all(raw_char_buf))
	{
	    fprintf(stderr, "chksum error\n");
	    free(raw_char_buf);
            free(ll_inmsg_node);
	    continue;
	}
        Frame * inframe = convert_char_to_frame(raw_char_buf);
	if (inframe->src == receiver->send_id)
	{
	    if (inframe->dst == receiver->recv_id)
	    {
		Frame * outframe;
		char* buf;

		outframe = build_ack(receiver, inframe);

		buf = add_chksum(outframe);
		print_receiver(receiver);
		ll_append_node(outgoing_frames_head_ptr, buf);
		free(outframe);
	    }
	}

	//free(inframe);
	free(raw_char_buf);
        free(ll_inmsg_node);
    }
}
Example #3
0
File: sender.c Project: s4byun/SWP
void handle_incoming_acks(Sender * sender,
        LLnode ** outgoing_frames_head_ptr)
{
    int incoming_msgs_length = ll_get_length(sender->input_framelist_head);
    while(incoming_msgs_length > 0)
    {
        //Receive the ACK and update the list length
        LLnode * ll_inmsg_node = ll_pop_node(&sender->input_framelist_head);
        incoming_msgs_length = ll_get_length(sender->input_framelist_head);

        //Convert the ACK node to an actual frame
        char * raw_char_buf = (char *) ll_inmsg_node->value;
        Frame * inframe = convert_char_to_frame(raw_char_buf);

        //Don't need the node anymore
        ll_destroy_node(ll_inmsg_node);

        //Checks if the frame is corrupted
        if(crc8(raw_char_buf, MAX_FRAME_SIZE))
        {
            //Checks if this ACK is for this sender
            if(atoi(inframe->sender_addr) == sender->send_id)
            {
                int id = atoi(inframe->receiver_addr);
                int seqnum = (int) inframe->seqnum;

                if(is_valid_sender(sender, seqnum, id))
                {
                    fprintf(stderr, "ACK %d RECEIVED\n", seqnum);
                    send_Q** buf = sender->send_q_head[id];
                    int lar = sender->LAR[id];
                    while(lar != seqnum)
                    {
                        lar = (lar == MAX_SEQ_NUM) ? 0 : lar + 1;
                        free(buf[lar % WS]->frame);
                        free(buf[lar % WS]->frame_timeout);
                        buf[lar % WS]->frame = NULL;
                        buf[lar % WS]->frame_timeout = NULL;
                    }
                    sender->LAR[id] = lar;
                }
            }
        }

        free(inframe);
    }
}
Example #4
0
File: sender.c Project: popacai/SWP
void handle_incoming_acks(Sender * sender,
                          LLnode ** outgoing_frames_head_ptr)
{
    //TODO: Suggested steps for handling incoming ACKs
    //    1) Dequeue the ACK from the sender->input_framelist_head
    //    2) Convert the char * buffer to a Frame data type
    //    3) Check whether the frame is corrupted
    //    4) Check whether the frame is for this sender
    //    5) Do sliding window protocol for sender/receiver pair   
    int incoming_msgs_length = ll_get_length(sender->input_framelist_head);
    while (incoming_msgs_length > 0)
    {
        LLnode * ll_inmsg_node = ll_pop_node(&sender->input_framelist_head);
        incoming_msgs_length = ll_get_length(sender->input_framelist_head);
        char * raw_char_buf = (char *) ll_inmsg_node->value;
        Frame * inframe = convert_char_to_frame(raw_char_buf);
	short checksum;

	checksum = chksum((unsigned short*) raw_char_buf, 
			    MAX_FRAME_SIZE / 2);
	if (checksum)
	{
	    fprintf(stderr, "send checksum error\n");
	    continue;
	}
        free(raw_char_buf);

	if (sender->send_id == inframe->dst)
	{
	    if (inframe->flag == ACK)
	    {
		recv_ack(sender, inframe);
	    }
	}

	free(inframe);
    }
}
Example #5
0
void handle_incoming_msgs(Receiver *r, LLnode **output_framelist_head) {
  while( ll_get_length(r->input_framelist_head) > 0 ) {
    LLnode *msgNode = ll_pop_node(&r->input_framelist_head);
    char *raw_buf = (char *)msgNode->value;
    Frame *inframe = convert_char_to_frame(raw_buf);

    // If corrupted, drop this frame 
    if( !(crc32(inframe, 4+FRAME_PAYLOAD_SIZE) ^ inframe->crc) ) {
      // if not the right recipient, drop this frame
      if( r->recv_id == inframe->dst ) {
        printf("<RECV_%d>:[%s]\n", r->recv_id, inframe->data);
        char *ack_buf = (char *)malloc(MAX_FRAME_SIZE);
        memcpy(ack_buf, raw_buf, MAX_FRAME_SIZE);
        ack_buf[3] = ACK_FLAG;
        ll_append_node(output_framelist_head, ack_buf);
      }
    }

    free(msgNode);
    free(raw_buf);
    free(inframe);
  }
}