-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRDT 3.0.py
More file actions
289 lines (215 loc) · 7.22 KB
/
Copy pathRDT 3.0.py
File metadata and controls
289 lines (215 loc) · 7.22 KB
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
"""
Sender-side simulation of RDT 3.0;
Input packets are formatted
[type, seq_num, message]
0 message with seq_num to be sent;
1 ACK received, ACKing seq_num;
2 timeout event - resend last packet;
Output packets are formatted
[status, seq_num]
-1 unexpected packet, -1 as seq_num;
0 message sent successfully - seq_num is the seq # of the message;
1 ACK processed - seq_num is the ACk seq_num;
2 re-sending finished - seq_num is the seq_num of the re-sent message;
Four states as described in the FSM
0 - wait for data 0;
1 - wait for ack 0;
2 - wait for data 1;
3 - wait for ack 1;
"""
def rdt_sender(event, state=0):
#Defines variables to use.
event_type = None
seq_num = None
data = None
#Iterates through events and defines variables accordingly.
for item in event:
if event_type is None:
event_type = event[0]
elif seq_num is None:
seq_num = event[1]
elif data is None:
data = event[2]
#If we're at state 0
if state == 0:
if seq_num == 0:
if event_type == 0:
return (1, [0, seq_num])
elif event_type == 1:
return (0, [-1, -1])
elif event_type == 2:
return (0, [0, seq_num])
if seq_num == 1:
return (1, [1, 1])
else:
return (1, [2, seq_num])
#If we're at state 1
if state == 1:
if seq_num == 0:
if event_type == 0:
return (1, [-1, -1])
elif event_type == 1:
return (2, [1, 0])
elif event_type == 2:
return (1, [2, seq_num])
else:
return (state, [-1, -1])
#If we're at state 2:
if state == 2:
if seq_num == 1:
if event_type == 0:
return (3, [0, 1])
if event_type == 1:
return (2, [-1, -1])
if event_type == 2:
return (2, [2, seq_num])
else:
return (state, [-1, -1])
#If we're at state 3
if state == 3:
if event_type == 2:
return (0, [2, 1])
elif seq_num == 1:
if event_type == 0:
return (3, [-1, -1])
if event_type == 1:
return (0, [1, 1])
else:
return (0, [-1, -1])
#Do not modify the following lines
def sender_test(event_list):
state = 0
action_list = []
for event in event_list:
state, action = rdt_sender(event,state)
action_list.append(action)
print(f'{action_list}')
#sender_test([[0, 0, 1], [2, 0, 1]])
#sender_test([[0, 0, 1], [1, 0, 1], [0, 1, 3], [2],[1,1,3]])
#sender_test([[0, 0, 1], [2, 0, 1]])
#sender_test([[0, 0, 1], [0, 1, 2], [0, 0, 3], [0, 1, 4], [0, 0, 5]])
#sender_test([[0, 0, 1], [1, 0, 1], [0, 1, 3], [1, 1, 3]])
#sender_test([[0, 0, 1], [1, 0], [0, 1, 3], [1, 1]])
#sender_test([[0, 0, 1], [1, 1, 1], [0, 1, 3], [1, 1, 3]])
#sender_test([[0, 0, 1], [1, 0, 1], [0, 1, 3], [2], [1, 1, 3], [0, 1, 4]])
"""
Receiver-side simulation of RDT 3.0
"""
def rdt_receiver(packet):
if packet[0] not in [0, 1]:
return [-1, -1]
else:
return [0, packet[0]]
#Do NOT modify the following lines
def receiver_test(event_list):
action_list = []
for event in event_list:
action = rdt_receiver(event)
action_list.append(action)
print(f'{action_list}')
#receiver_test([[0, 1]])
"""
Sender-side simulation of GBN;
An event is formatted as
[type, seq_num, data]
0 data to send; no check on seq_num and data;
1 ACK received; acking seq_num;
2 timeout event; resend all outgoing unAck'ed events; no check on seq_num and data;
Output of function gbn_sender() is formatted as
[status, base, next_seq]
-1 unexpected event/window full
0 data sent successfully
1 ACK processed;
2 resending finished;
N - the window size
base - seq# of lower winder boundary (base)
"""
N = 4 # window size
def gbn_sender(event, base=0, next_seq=0):
"""Applies the Go Back N protocol to a list of variables"""
#Initialising variables
event_type = None
seq_num = None
data = None
for item in event:
if event_type is None:
event_type = item
elif seq_num is None:
seq_num = item
else:
data = item
#print(event)
#print(f"event type is {event_type}")
#print(f"next_seq is {next_seq}")
#print(range(base, next_seq - 1))
#print('')
#If event type is a date sending event
if event_type == 0:
#Data has been sent, ready for transmission
if (next_seq - base) < 4:
return [0, base, next_seq + 1]
else:
return [-1, base, next_seq]
#If event type is an acknowledgement event
if event_type == 1:
if seq_num in range(base, next_seq):
base = seq_num
return [1, base + 1, next_seq]
else:
return [-1, base, next_seq]
#If event type is a timeout
if event_type == 2:
return [2, base, next_seq]
#Do NOT modify the following code
def sender_test(event_list):
base = 0
next_seq = 0
action_list = []
for event in event_list:
action = gbn_sender(event, base, next_seq)
base = action[1]
next_seq = action[2]
action_list.append(action)
print(f'{action_list}')
#sender_test([[2, 0, 0]])
#sender_test([[0, 0, 1]])
#sender_test([[0, 0, 1], [0, 1, 2]])
#sender_test([[0, 0, 1], [0, 1, 2], [0, 2, 3]])
#sender_test([[0, 0, 1], [0, 1, 2], [0, 2, 3], [0, 3, 4], [0, 4, 5]])
#sender_test([[0, 0, 1], [0, 1, 1], [1, 1, 3], [0, 1, 4]])
#sender_test([[0, 0, 1], [0, 1, 2], [0, 2, 3],[2]])
#sender_test([[1, 0, 0]])
#sender_test([[0, 0, 1], [0, 1, 1], [1, 1, 3],[1,1,3]])
#sender_test([[0, 0, 1], [0, 1, 1], [1, 2, 3]])
#sender_test([[0, 0, 1], [0, 1, 2], [0, 2, 3], [0, 3, 4], [0, 4, 5], [1,2,3],[0,4,5],[0,5,6],[2,0,0],[1,4,5]])
"""
Receiver-side simulation of GBN
Input packets are formatted as
[seq_num, data]
Output packets are formatted as
[status, exp_num]
0 - an ACK is sent;
-1 - unexpected packet received;
"""
def gbn_receiver(packet, exp_num=1):
seq_num = packet[0]
data = packet[1]
if seq_num == exp_num:
status = 0
exp_num += 1
else:
status = -1
return [status, exp_num]
#Do NOT modify the following code
def receiver_test(packet_list):
action_list = []
exp_num = 1
for packet in packet_list:
action = gbn_receiver(packet, exp_num)
exp_num = action[1]
action_list.append(action)
print(f'{action_list}')
receiver_test([[1,1]])
receiver_test([[1,1],[2,2]])
receiver_test([[1,1],[2,2],[3,3]])
receiver_test([[0,1]])