Fixed minor stuff in code-a5
This commit is contained in:
parent
1f26e9fde1
commit
ea9f19e66c
@ -1,16 +1,15 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <queue>
|
#include <queue>
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
class SlidingWindow {
|
class SlidingWindow {
|
||||||
private:
|
private:
|
||||||
int window_size;
|
int window_size;
|
||||||
int total_frames;
|
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(int window_size, int total_frames) {
|
SlidingWindow(int window_size, int total_frames) {
|
||||||
@ -20,39 +19,40 @@ public:
|
|||||||
this->next_frame_to_receive = 0;
|
this->next_frame_to_receive = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
// send a frame
|
// send a frame
|
||||||
void send_frame() {
|
void send_frame() {
|
||||||
if (sent_frames.size() < window_size && next_frame_to_send < total_frames) {
|
if (sent_frames.size() < window_size && next_frame_to_send < total_frames) {
|
||||||
sent_frames.push_back(next_frame_to_send);
|
sent_frames.push_back(next_frame_to_send);
|
||||||
next_frame_to_send++;
|
next_frame_to_send++;
|
||||||
cout << "Sent frame " << sent_frames.back() << endl;
|
cout<<"Sent frame "<<sent_frames.back()<<endl;
|
||||||
} else {
|
}
|
||||||
cout << "Window is full. Waiting before sending next frames." << endl;
|
else {
|
||||||
}
|
cout<<"Window is full. Waiting before sending next frames."<<endl;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// receive a frame
|
// receive a frame
|
||||||
void receive_frame(int frame_number) {
|
void receive_frame(int frame_number) {
|
||||||
if (frame_number == next_frame_to_receive) {
|
if (frame_number == next_frame_to_receive) {
|
||||||
next_frame_to_receive++;
|
next_frame_to_receive++;
|
||||||
cout << "Received frame " << frame_number << endl;
|
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());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
else {
|
||||||
bool all_frames_sent() {
|
cout<<"Received frame "<<frame_number<<", but expected frame "<<next_frame_to_receive<<endl;
|
||||||
return next_frame_to_send == total_frames;
|
|
||||||
}
|
}
|
||||||
|
// remove acknowledged frames from the sent_frames queue
|
||||||
bool all_frames_received() {
|
while (!sent_frames.empty() && sent_frames.front()<=next_frame_to_receive-1) {
|
||||||
return next_frame_to_receive == total_frames;
|
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() {
|
||||||
@ -66,14 +66,13 @@ int main() {
|
|||||||
|
|
||||||
while (!window.all_frames_sent() || !window.all_frames_received()) {
|
while (!window.all_frames_sent() || !window.all_frames_received()) {
|
||||||
window.send_frame();
|
window.send_frame();
|
||||||
|
|
||||||
// simulate receiving frames
|
// simulate receiving frames
|
||||||
for (int i = 0; i < rand() % 3; i++) {
|
for (int i=0; i<rand()%3; i++) {
|
||||||
int frame_number = rand() % total_frames;
|
int frame_number = rand() % total_frames;
|
||||||
window.receive_frame(frame_number);
|
window.receive_frame(frame_number);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
cout << "All frames sent and received successfully!" << endl;
|
cout<<"All frames sent and received successfully!"<<endl;
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user