2024-09-25 22:18:20 +05:30
|
|
|
#include <iostream>
|
|
|
|
#include <vector>
|
|
|
|
#include <queue>
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
class SlidingWindow {
|
|
|
|
private:
|
2024-09-25 22:28:10 +05:30
|
|
|
int window_size;
|
|
|
|
int total_frames;
|
2024-09-25 22:18:20 +05:30
|
|
|
vector<int> sent_frames; // queue of sent frames
|
|
|
|
int next_frame_to_send; // index of the next frame to send
|
|
|
|
int next_frame_to_receive; // index of the next frame to receive
|
|
|
|
|
|
|
|
public:
|
2024-09-25 22:28:10 +05:30
|
|
|
SlidingWindow(int window_size, int total_frames) {
|
|
|
|
this->window_size = window_size;
|
|
|
|
this->total_frames = total_frames;
|
|
|
|
this->next_frame_to_send = 0;
|
|
|
|
this->next_frame_to_receive = 0;
|
|
|
|
}
|
2024-09-25 22:18:20 +05:30
|
|
|
|
|
|
|
// send a frame
|
2024-09-25 22:28:10 +05:30
|
|
|
void send_frame() {
|
|
|
|
if (sent_frames.size() < window_size && next_frame_to_send < total_frames) {
|
|
|
|
sent_frames.push_back(next_frame_to_send);
|
2024-09-25 22:18:20 +05:30
|
|
|
next_frame_to_send++;
|
2024-09-25 22:28:10 +05:30
|
|
|
cout << "Sent frame " << sent_frames.back() << endl;
|
2024-09-25 22:18:20 +05:30
|
|
|
} else {
|
2024-09-25 22:28:10 +05:30
|
|
|
cout << "Window is full. Waiting before sending next frames." << endl;
|
2024-09-25 22:18:20 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// receive a frame
|
|
|
|
void receive_frame(int frame_number) {
|
|
|
|
if (frame_number == next_frame_to_receive) {
|
|
|
|
next_frame_to_receive++;
|
|
|
|
cout << "Received frame " << frame_number << endl;
|
|
|
|
} else {
|
|
|
|
cout << "Received frame " << frame_number << ", but expected frame " << next_frame_to_receive << endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
// remove acknowledged frames from the sent_frames queue
|
|
|
|
while (!sent_frames.empty() && sent_frames.front() <= next_frame_to_receive - 1) {
|
|
|
|
sent_frames.erase(sent_frames.begin());
|
|
|
|
}
|
|
|
|
}
|
2024-09-25 22:28:10 +05:30
|
|
|
|
|
|
|
bool all_frames_sent() {
|
|
|
|
return next_frame_to_send == total_frames;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool all_frames_received() {
|
|
|
|
return next_frame_to_receive == total_frames;
|
|
|
|
}
|
2024-09-25 22:18:20 +05:30
|
|
|
};
|
|
|
|
|
|
|
|
int main() {
|
2024-09-25 22:28:10 +05:30
|
|
|
int window_size, total_frames;
|
|
|
|
cout << "Enter the window size:\t";
|
|
|
|
cin >> window_size;
|
|
|
|
cout << "Enter the total number of frames:\t";
|
|
|
|
cin >> total_frames;
|
|
|
|
|
|
|
|
SlidingWindow window(window_size, total_frames);
|
2024-09-25 22:18:20 +05:30
|
|
|
|
2024-09-25 22:28:10 +05:30
|
|
|
while (!window.all_frames_sent() || !window.all_frames_received()) {
|
|
|
|
window.send_frame();
|
2024-09-25 22:18:20 +05:30
|
|
|
|
2024-09-25 22:28:10 +05:30
|
|
|
// simulate receiving frames
|
|
|
|
for (int i = 0; i < rand() % 3; i++) {
|
|
|
|
int frame_number = rand() % total_frames;
|
|
|
|
window.receive_frame(frame_number);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
cout << "All frames sent and received successfully!" << endl;
|
2024-09-25 22:18:20 +05:30
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|