-
Notifications
You must be signed in to change notification settings - Fork 0
/
stopnwait.cpp
88 lines (83 loc) · 1.72 KB
/
stopnwait.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include<bits/stdc++.h>
using namespace std;
class timer {
private:
unsigned long begTime;
public:
void start() {
begTime = clock();
}
unsigned long elapsedTime() {
return ((unsigned long) clock() - begTime) / CLOCKS_PER_SEC;
}
bool isTimeout(unsigned long seconds) {
return seconds >= elapsedTime();
}
};
int main()
{
int n;
cout<<"Enter number of Frames : ";
cin>>n;
vector<int> frames(n);
cout<<"Enter frame sequence numbers : ";
for(int i = 0;i<n;i++)
{
cin>>frames[i];
}
unsigned long seconds = 4;
timer t;
cout<<"Sender has to send frames : ";
for(int i=0;i<n;i++)
{
cout<<frames[i]<<" ";
}
cout<<endl;
int count = 0;
bool delay = false;
cout<<endl<<"Sender\t\t\t\t\tReceiver"<<endl;
while(count<n)
{
bool timeout = false;
cout<<"Sending Frame : "<<frames[count]<<"\t";
t.start();
this_thread::sleep_for (chrono::seconds(rand()%6));
if(t.elapsedTime() <= seconds)
{
cout<<"Received Frame : "<<frames[count]<<" ";
if(delay)
{
cout<<"Duplicate";
}
count++;
cout<<endl;
}
else
{
cout<<"---"<<endl;
cout<<"\t\tTimeout"<<endl;
timeout = true;
}
if(timeout)
{
continue;
}
else
{
t.start();
this_thread::sleep_for (chrono::seconds(rand()%6));
if(t.elapsedTime() > seconds )
{
cout<<"\t\tDelayed Ack"<<endl;
delay = true;
count--;
}
else
{
cout<<"\t\tAcknowledgement : "<<frames[count-1]<<endl;
delay = false;
}
}
}
return 0;
}