Skip to content
Snippets Groups Projects

q3 358

  • Clone with SSH
  • Clone with HTTPS
  • Embed
  • Share
    The snippet can be accessed without any authentication.
    Authored by Sadman Kazi
    q3.cpp 2.17 KiB
       void runEventReader(int iterations) {
            // initialize by filling up the buffers
            for (int i = 0; i < N; i++) {
                m_buffer.set(i, i);
            }
    
            // send the first packet to start off the simulation
            sendPacket(0);
            int next_packet = 1;
    
            for (int i = 1; i < iterations; i++) {
                if (m_es.nextEventTime() <= m_cur_time || next_packet >= N) {
    
                    Event ev = m_es.dequeue();
                    m_cur_time = ev.timestamp;
    
                    //std::cout << "Prcessing: " << ev << std::endl;
                    if (ev.type == ACK) {
                        if (ev.error_flag == 0) {
                            if (ev.id != m_buffer.get(0)) {
                                int num_trans = (ev.id - m_buffer.get(0) + N + 1) % (N + 1);
                                m_buffer.shift(num_trans);
                                m_buffer.fill(num_trans);
                                for (int i = 0; i < num_trans; i++) {
                                    m_sent_times.pop();
                                }
                                m_bits_transmitted += num_trans * LEN;
                                next_packet = next_packet - num_trans;
                                if (next_packet < 0) {
                                    next_packet = 0;
                                }
                                sendPacket(next_packet);
                                if (next_packet != 0) {
                                    m_es.registerEvent(Event{TIMEOUT, m_sent_times.front() + m_timeout, -1, -1});
                                }
                                next_packet++;
                            }
                        }
                    } else if (ev.type == TIMEOUT) {
                        // empty the sent times
                        std::queue<double> empty;
                        std::swap( m_sent_times, empty );
                        sendPacket(0);
                        next_packet = 1;
                    }
                } else {
                    // send the next packet
                    //std::cout << "Sending another" << std::endl;
                    sendPacket(next_packet);
                    next_packet++;
                }
            }
    
            std::cout << (long double)m_bits_transmitted/m_cur_time << ",";
        }
    0% Loading or .
    You are about to add 0 people to the discussion. Proceed with caution.
    Finish editing this message first!
    Please register or to comment