-
Notifications
You must be signed in to change notification settings - Fork 1
/
SelectiveRepeat.cpp
94 lines (76 loc) · 2.32 KB
/
SelectiveRepeat.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
89
90
91
92
93
94
/* This code is contributed by Sourajita Dewasi. In Selective Repeat Protocol, there is a window in sender's sideand a same sized receiver buffer.
So if out of order packet arrives it's not taken in, if it's required in the receiver's buffer.
There is independent acknowledgement. And for a lost data packet or acknowledgement slectively that data
packet is retransmitted.*/
/*We are assuming the window size =4. both at sender and receiver's side.
There might be different order of transmission but the no of retransmissions are same.
The order of transmission will depend upon the acknowledgement timer and Timeout timer*/
#include<iostream>
int tmp1, tmp2, tmp3, tmp4, tmp5, i, windowsize = 4, noofPacket, morePacket;
using namespace std;
int main()
{
char c;
int receiver(int);
int simulate(int);
int negack(int);
for(int i = 0;i < 10;i++)
rand();
noofPacket = rand()%10;
cout<<"Number of frames is: "<<noofPacket;
morePacket = noofPacket;
while(morePacket >= 0)
{
tmp1 = simulate(windowsize);
windowsize -= tmp1;
tmp4 += tmp1;
if(tmp4 > noofPacket)
tmp4 = noofPacket;
for(i = noofPacket - morePacket; i <= tmp4; i++)
cout<<"\nSending Frame "<<i;
tmp2 = receiver(tmp1);
tmp3 += tmp2;
if(tmp3 > noofPacket)
tmp3 = noofPacket;
tmp2 = negack(tmp1);
tmp5 += tmp2;
if(tmp5 != 0)
{
cout<<"\nNo acknowledgement for the frame "<<tmp5;
cout<<"\nRetransmitting frame "<<tmp5;
}
morePacket -= tmp1;
if(windowsize <= 0)
windowsize = 4;
}
cout<<"\n Selective Repeat Protocol Ends. All packets are successfully transmitted.";
}
int receiver(int tmp1)
{
int i;
for(i = 0;i < 5;i++)
rand();
i = rand() % tmp1;
return i;
}
int negack(int tmp1)
{
int i;
for(i = 0;i < 5;i++)
rand();
i = rand() % tmp1;
return i;
}
int simulate(int windowsize)
{
int tmp1, i;
for(i = 0;i < 5;i++)
tmp1 = rand();
if(tmp1 == 0)
tmp1 = simulate(windowsize);
i = tmp1 % windowsize;
if(i == 0)
return windowsize;
else
return tmp1 % windowsize;
}