-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstopandwaitGUI.py
More file actions
395 lines (326 loc) · 9.66 KB
/
stopandwaitGUI.py
File metadata and controls
395 lines (326 loc) · 9.66 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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
'''
Program : Stop and Wait Protocol
'''
# Importing necessary libraries
from tkinter import *
from random import *
import time
# -----------------------------------------------------------------------------
'''
Creating the canvas for the graphical user interface.
Module Used : Tkinter for GUI
'''
root = Tk()
root.title('Stop and Wait')
canvas = Canvas(root, width=900, height=500)
canvas.pack()
canvas.create_text(100, 50, text="Sender", font="Times 20 italic bold")
canvas.create_text(800, 50, text="Receiver", font="Times 20 italic bold")
# -----------------------------------------------------------------------------
# -----------------------------------------------------------------------------
# Functions Definitions
def sender_network():
'''
Sender at network layer.
Parameters
----------
None
Returns
-------
frame : dtype - list, data to be sent to receiver.
Description
-----------
sender_network function definition.
Approach
--------
Defines a list containing the data values to be sent to receiver.
'''
frame = [0x1,0x2,0x3,0x4,0x5]
return frame
def sender_physical(counter):
'''
Sender at physical layer.
Parameters
----------
counter : dtype - int, value of the counter.
Returns
-------
counter : dtype - int, value of the counter after moving the frame.
Description
-----------
sender_physical function description.
Approach
--------
A frame is made and a random number is being generated. According to the
value of random number, the cases are handled, if frame is going to be
lost or to be sent successfully to receiver side.
'''
frame = draw_frame()
random = randint(1, 4)
#print('Sender random', random)
if (random == 3):
time.sleep(0.5)
frame_lost(frame, counter)
print('Frame lost, sending same again...')
sender_physical(0)
else:
counter = move_frame(frame, counter)
receiver(frame)
return int(counter)
def frame_lost(frame, counter):
'''
Frame is being lost.
Parameters
----------
frame : dtype - canvas object, a rectangular frame.
counter : dtype - int, value of the counter.
Returns
-------
None
Description
-----------
frame_lost function description.
Approach
--------
Frame is being deleted using delete() method of canvas module.
'''
time.sleep(5)
for i in range(1, 25):
if (i % 2 == 0):
counter = counter + 1
canvas.itemconfig(timer, text=counter)
# print("counter: ", counter)
canvas.update()
time.sleep(0.25)
canvas.move(frame, 5, 0)
canvas.update()
for i in range(1, 25):
if (i % 2 == 0):
counter = counter + 1
canvas.itemconfig(timer, text=counter)
# print("counter: ", counter)
canvas.update()
time.sleep(0.25)
lost = canvas.create_text(450, 250, text="Frame is lost and timeout...", font="Times 15 italic bold")
canvas.update()
time.sleep(2)
canvas.delete(lost)
canvas.delete(frame)
print('Frame is lost...')
return
def receiver(frame):
'''
Receiver Side working.
Parameters
----------
frame : dtype - canvas object, a rectangular frame.
Returns
-------
None
Description
-----------
receiver function description.
Approach
--------
Notification about the frame being received at receiver side.
'''
framereceived = canvas.create_text(450, 250, text="Frame Received.", font="Times 15 italic bold")
canvas.update()
time.sleep(2)
canvas.delete(framereceived)
canvas.delete(frame)
print('Data received at physical layer of receiver...')
return
def acknowledgement_received(ack, counter):
'''
Acknowledgement being received at sender side and new frame is ready
to be sent.
Parameters
----------
ack : dtype - canvas object, acknowledgement.
counter : dtype - int, value of the counter.
Returns
-------
None
Description
-----------
acknowledgement_received function description.
Approach
--------
A notification about the acknowledgement being received at sender side
and telling the sender to send the next frame to the receiver.
'''
move_ack(ack, counter)
ackrec = canvas.create_text(450, 250, text="Ack Received. Ready for next frame.", font="Times 15 italic bold")
canvas.update()
time.sleep(2)
canvas.delete(ack)
canvas.delete(ackrec)
print('Acknowledgement received.')
return
def acknowledgement_lost(ack, counter):
'''
Acknowledgement being lost while moving from receiver to sender side.
Parameters
----------
ack : dtype - canvas object, acknowledgement.
counter : dtype - int, value of the counter.
Returns
-------
None
Description
-----------
acknowledgement_lost function description.
Approach
--------
A notification about the acknowledgement being lost while going from receiver
side to sender's side.
'''
for i in range(1, 25):
if (i % 2 == 0):
counter = counter + 1
canvas.itemconfig(timer, text=counter)
canvas.update()
time.sleep(0.25)
canvas.move(ack, -7, 0)
canvas.update()
acklost = canvas.create_text(450, 250, text="Time out...! Acknowledge Lost, send the same frame again", font="Times 15 italic bold")
canvas.update()
time.sleep(1)
canvas.delete(acklost)
canvas.delete(ack)
print('Acknowledgement lost.')
return
def wait_for_event(counter):
'''
main function whihc functions the stop and wait protocol.
Parameters
----------
counter : dtype - int, value of the counter.
Returns
-------
None
Description
-----------
wait_for_event function description.
Approach
--------
Data from sender_network helper function is assigned to a variable.
Usinga for loop, the frames are being sent one by one. A random function is
used to randomly deletes an acknowledgement.
'''
data = sender_network()
for i in data:
counter = int(sender_physical(0))
#print("Counter value in wait for event", counter)
random = randint(1, 4)
ack = draw_ack()
if (random == 3):
time.sleep(0.25)
acknowledgement_lost(ack, counter)
print('Sending same frame again...')
counter = int(sender_physical(0))
ack = draw_ack()
acknowledgement_received(ack, counter)
else:
acknowledgement_received(ack, counter)
def draw_frame():
'''
Drawing a rectangular frame to show the data packet.
Parameters
----------
None
Returns
-------
dtype - canvas object,a rectangle which is used to display the data packet.
Description
-----------
draw_frame function description.
Approach
--------
member function of canvas module is used to draw a rectangle.
'''
return canvas.create_rectangle(50, 100, 120, 150, fill='green')
def move_frame(frame, counter):
'''
Move a frame.
Parameters
----------
frame : dtype - canvas object, frame.
counter : dtype - int, value of the counter.
Returns
-------
counter : dtype - int, value of the counter.
Description
-----------
move_frame function description.
Approach
--------
canvas.move method is used to move the frame. The canvas is needed to be
updated so that we get to see the latest frame on new location. A timer
is also being maintained for the protocol timer.
'''
for i in range(1, 25):
if (i % 2 == 0):
counter = counter + 1
canvas.itemconfig(timer, text=counter)
#print("Counter: ", counter)
canvas.update()
time.sleep(0.25)
canvas.move(frame, 30, 0)
canvas.update()
return counter
def draw_ack():
'''
Draw acknowledgement frame.
Parameters
----------
None
Returns
-------
A frame, dtype - canvas object, for the acknowledgement purpose.
Description
-----------
draw_ack function description.
Approach
--------
canvas.crate_rectangle method is used to create an acknowledgement frame.
'''
return canvas.create_rectangle(700, 100, 780, 150, fill='red')
def move_ack(ack, counter):
'''
Move an acknowledgement frame.
Parameters
----------
ack : dtype - canvas object, acknowledgement.
counter : dtype - int, value of the counter.
Returns
-------
counter : dtype - int, value of the counter.
Description
-----------
move_ack function description.
Approach
--------
canvas.move method is used to move the acknowledgement frame. The canvas is
needed to be updated so that we get to see the latest frame on new location.
A timer is also being maintained for the protocol timer.
'''
for i in range(1, 25):
if (i % 2 == 0):
counter = counter + 1
canvas.itemconfig(timer, text=counter)
#print("Counter: ", counter)
canvas.update()
time.sleep(0.25)
canvas.move(ack, -28, 0)
canvas.update()
return counter
# -----------------------------------------------------------------------------
# -----------------------------------------------------------------------------
# Main handler block
counter = 0
timer = canvas.create_text(450, 450, text="Timer", font="Times 30 italic bold")
wait_for_event(counter)
root.mainloop()
# -----------------------------------------------------------------------------