simulating received frames and added user inputs
This commit is contained in:
parent
14d91d3406
commit
fc0dba3552
@ -4,25 +4,30 @@
|
|||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
const int WINDOW_SIZE = 5; // size of the sliding window
|
|
||||||
|
|
||||||
class SlidingWindow {
|
class SlidingWindow {
|
||||||
private:
|
private:
|
||||||
|
int window_size;
|
||||||
|
int total_frames;
|
||||||
vector<int> sent_frames; // queue of sent frames
|
vector<int> sent_frames; // queue of sent frames
|
||||||
int next_frame_to_send; // index of the next frame to send
|
int next_frame_to_send; // index of the next frame to send
|
||||||
int next_frame_to_receive; // index of the next frame to receive
|
int next_frame_to_receive; // index of the next frame to receive
|
||||||
|
|
||||||
public:
|
public:
|
||||||
SlidingWindow() : next_frame_to_send(0), next_frame_to_receive(0) {}
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
// send a frame
|
// send a frame
|
||||||
void send_frame(int frame_number) {
|
void send_frame() {
|
||||||
if (sent_frames.size() < WINDOW_SIZE) {
|
if (sent_frames.size() < window_size && next_frame_to_send < total_frames) {
|
||||||
sent_frames.push_back(frame_number);
|
sent_frames.push_back(next_frame_to_send);
|
||||||
next_frame_to_send++;
|
next_frame_to_send++;
|
||||||
cout << "Sent frame " << frame_number << endl;
|
cout << "Sent frame " << sent_frames.back() << endl;
|
||||||
} else {
|
} else {
|
||||||
cout << "Window is full, cannot send frame " << frame_number << endl;
|
cout << "Window is full. Waiting before sending next frames." << endl;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -40,25 +45,35 @@ public:
|
|||||||
sent_frames.erase(sent_frames.begin());
|
sent_frames.erase(sent_frames.begin());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool all_frames_sent() {
|
||||||
|
return next_frame_to_send == total_frames;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool all_frames_received() {
|
||||||
|
return next_frame_to_receive == total_frames;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
SlidingWindow window;
|
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;
|
||||||
|
|
||||||
// send some frames
|
SlidingWindow window(window_size, total_frames);
|
||||||
window.send_frame(0);
|
|
||||||
window.send_frame(1);
|
|
||||||
window.send_frame(2);
|
|
||||||
window.send_frame(3);
|
|
||||||
window.send_frame(4);
|
|
||||||
window.send_frame(5);
|
|
||||||
|
|
||||||
// receive some frames
|
while (!window.all_frames_sent() || !window.all_frames_received()) {
|
||||||
window.receive_frame(0);
|
window.send_frame();
|
||||||
window.receive_frame(1);
|
|
||||||
window.receive_frame(2);
|
// simulate receiving frames
|
||||||
window.receive_frame(3);
|
for (int i = 0; i < rand() % 3; i++) {
|
||||||
window.receive_frame(4);
|
int frame_number = rand() % total_frames;
|
||||||
|
window.receive_frame(frame_number);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
cout << "All frames sent and received successfully!" << endl;
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user