// Algorithm for Sender of Stop and Wait // This could be implemented as a thread or daemon // that is always running Inputs: RTT - The RTT of the link packet_queue - this a queue where network layer puts packets to send void Sender(int RTT, Queue packet_queue){ frame next_frame = new frame(), read_frame = new frame(); int next_seq_no = 0; Packet next_p; long wait_time = 0; int result = 0; bool do_send = true; // set timer to 1.5 RTT wait_time = 1.5 * RTT; for(;;) { if (do_send){ // get the next packet // this method will block untl somethin can be read next_p = packet_queue.get_next_packet(); frame.set_type(DATA_FRAME); // mark frame as data frame frame.set_seq(next_seq_no); // set seq_no for packet frame.set_packet(next_p); send_to_physical(frame); } // Read ACK from physical layer, but this function // will block for up to wait_time milliseconds result = read_from_physical(read_frame, wait_time); if (read_frame.get_type() != ACK_FRAME){ // something is very wrong since receiver is trying // to piggyback ACK with Data, which is not supported // in this algoritm exit(1); // panic!!! } if (result == TIME_OUT){ // Timeout occurred, so send the same frame again do_send = true; continue; // try to send again } else if (read_frame.get_seq() == next_frame.get_seq()){ // ACK for current frame has arrive // Move to send next frame on the queue packet_queue.dequeue(); // change seqence number next_seq_no = (next_seq_no + 1) % 2; do_send = true; } else { // If we here , we are in trouble // We got a repeated ACK, meaning that // receiver is out sync because our Timeout is too // short. // Discard this ACK, and wait for ACK for this Frame do_sent = false; continue; } } }