-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDriverFile.py
330 lines (272 loc) · 11.9 KB
/
DriverFile.py
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
import Node
from tkinter import *
# all the functions go here
# function to be called on click of 'Simulate Button'
def simulate():
# get values from UI
print("let the execution begin")
global simulation_time
simulation_time = int(number_of_time_slots.get())
buffer_size = int(size_of_buffer.get())
frame_size = int(size_of_frame.get())
packet_gen_prob = float(probability_of_generating_packet.get())
num_of_nodes = int(number_of_nodes.get())
randomization_ceiling = int(maximum_randomisation_ceiling.get())
max_retrans_count_until_drop = int(maximum_retransmission_count.get())
# generate an array with node objects
nodes_list = []
for number in range(0, num_of_nodes):
identity = 'Node '
identity += str(number+1)
nodes_list.append(Node.Node(identity, buffer_size, packet_gen_prob, num_of_nodes, randomization_ceiling,
max_retrans_count_until_drop))
# variables here
channel_state = "idle"
wasted_timeslots = 0
utilized_timeslots = 0
occupied_timeslot = 0
for timeslot_instance in range(0, simulation_time):
# processes that can happen concurrently in one time slot will be part of this loop
# give a chance to every node to generate packets
for node_instance in nodes_list:
node_instance.gen_packet_with_prob(timeslot_instance)
# reduce backoff time of each node
for node_instance in nodes_list:
if node_instance.back_off_time > 0:
node_instance.back_off_time -= 1
# decide channel state for this timeslot
if channel_state == "transmission" and occupied_timeslot != 0:
occupied_timeslot -= 1
utilized_timeslots += 1
channel_state = "transmission"
else:
transmitting_nodes_list = get_nodes_wanting_to_transmit(nodes_list)
if len(transmitting_nodes_list) == 0:
channel_state = "idle"
wasted_timeslots += 1
elif len(transmitting_nodes_list) == 1:
channel_state = "transmission"
utilized_timeslots += 1
transmiting_node = transmitting_nodes_list.pop()
transmiting_node.book_keeping_after_suc_trans(timeslot_instance)
occupied_timeslot = frame_size - 1
elif len(transmitting_nodes_list) > 1:
channel_state = "contention"
wasted_timeslots += 1
for node_instance in transmitting_nodes_list:
node_instance.book_keeping_after_collision()
# results to be printed
print("Number of frames generated by each station:")
for node_instance in nodes_list:
print("Number of frame generated by %s is %d" % (node_instance.identity_ip, node_instance.num_of_gen_packets))
global total_frames
total_frames = get_total_num_of_frames_generated(nodes_list)
for node_instance in nodes_list:
if simulation_time != 0:
station_average = (node_instance.num_of_gen_packets / simulation_time)
else:
station_average = 0
print("Average Number of frame generated by %s is %f" % (node_instance.identity_ip, station_average))
global average_frames
if simulation_time != 0:
average_frames = (total_frames/simulation_time)
else:
average_frames = 0
global lost_frame_count
lost_frame_count = get_num_of_frames_lost(nodes_list)
global awaiting_frame_count
awaiting_frame_count = get_num_of_awaiting_frames(nodes_list)
global succ_frame_count
succ_frame_count = get_num_of_succ_trans(nodes_list)
global total_num_of_collisions
total_num_of_collisions = get_num_of_coll_trans(nodes_list)
# Determines the throughput of the system
global throughput
if simulation_time != 0:
throughput = (succ_frame_count / simulation_time)
else:
throughput = 0
# Determines the channel utilization
global channel_util
if simulation_time != 0:
channel_util = (utilized_timeslots / simulation_time)
else:
channel_util = 0
# Determines the channel waste
global channel_waste
if simulation_time != 0:
channel_waste = (wasted_timeslots / simulation_time)
else:
channel_waste = 0
# Determines retransmission overhead
global retrans_overhead
num_of_retrans = get_retrans_overhead(nodes_list)
if succ_frame_count != 0:
retrans_overhead = round(num_of_retrans/succ_frame_count,2)
else:
retrans_overhead = 0
# Determines average delay
global avg_delay
num_of_delayed_slots = get_avg_total_delay(nodes_list)
if succ_frame_count != 0:
avg_delay = round(num_of_delayed_slots / succ_frame_count,2)
else:
avg_delay = 0
display_output()
def get_nodes_wanting_to_transmit(nodes_list):
# count how many nodes have packets in their buffer
transmitting_nodes = []
for node_instance in nodes_list:
if len(node_instance.buffer) > 0 and node_instance.back_off_time == 0:
transmitting_nodes.append(node_instance)
return transmitting_nodes
def get_num_of_awaiting_frames(nodes_list):
count = 0
for node_instance in nodes_list:
count += len(node_instance.buffer)
return count
def get_total_num_of_frames_generated(nodes_list):
count = 0
for node_instance in nodes_list:
count += node_instance.num_of_gen_packets
return count
def get_num_of_frames_lost(nodes_list):
count = 0
for node_instance in nodes_list:
count += node_instance.lost_packet_count
return count
def get_num_of_succ_trans(nodes_list):
count = 0
for node_instance in nodes_list:
count += node_instance.success_trans_packet_count
return count
def get_num_of_coll_trans(nodes_list):
count = 0
for node_instance in nodes_list:
count += node_instance.num_of_collisions
return count
def get_retrans_overhead(nodes_list):
# addition of retransmission attempts
count = 0
for node_instance in nodes_list:
count += node_instance.num_of_retrans_attempts
return count
def get_avg_total_delay(nodes_list):
# addition of number_of_delayed_slots / total_frames_generated across each station
count = 0
for node_instance in nodes_list:
count += node_instance.num_of_delayed_slots
return count
def formulate_window():
global window
window = Tk()
window.title("Transmission Simulation")
window.geometry('1200x600')
filename = PhotoImage(file="networking.png")
background_label = Label(window, image=filename)
background_label.place(x=0, y=0, relwidth=1, relheight=1)
# Insert the labels in the window
text = Label(window, text="Input Values", font=('Helvetica', 20, 'bold'))
text.grid(column=0, row=0, columnspan=2)
text = Label(window, text="\t", font=('Helvetica', 20, 'bold'))
text.grid(column=2, row=0,)
text = Label(window, text="Results", font=('Helvetica', 20, 'bold'))
text.grid(column=3, row=0, columnspan=2)
# To display Number of Node label,spin box
lbl = Label(window, text="Enter number of Nodes: ", font=('Helvetica', 10, 'bold'))
lbl.grid(column=0, row=1)
global number_of_nodes
number_of_nodes = Spinbox(window, from_=0,to=1000, width=20)
number_of_nodes.grid(column=1, row=1)
# To display total simulation time,spin box
lbl = Label(window, text="Enter number of Simulation Time Slots: ", font=('Helvetica', 10, 'bold'))
lbl.grid(column=0, row=2)
global number_of_time_slots
number_of_time_slots = Spinbox(window, from_=0, to=1000, width=20)
number_of_time_slots.grid(column=1, row=2)
# # To display Buffer Size label,spin box
lbl = Label(window, text="Enter Buffer size: ", font=('Helvetica', 10, 'bold'))
lbl.grid(column=0, row=3)
global size_of_buffer
size_of_buffer = Spinbox(window, from_=0, to=1000, width=20)
size_of_buffer.grid(column=1, row=3)
# # To display Buffer Size label,spin box
lbl = Label(window, text="Enter Frame size: ", font=('Helvetica', 10, 'bold'))
lbl.grid(column=0, row=4)
global size_of_frame
size_of_frame = Spinbox(window, from_=0, to=1000, width=20)
size_of_frame.grid(column=1, row=4)
#
# # To display Probability of Generating packet label,spin box, double data type was used for decimal
lbl = Label(window, text="Enter the Probability of Packet generation (0.1-1)", font=('Helvetica', 10, 'bold'))
lbl.grid(column=0, row=5)
global probability_of_generating_packet
probability_of_generating_packet = Spinbox(window, from_=0, to=1, increment=0.1, width=20)
probability_of_generating_packet.grid(column=1, row=5)
#
# # To display Re-transmission count
lbl = Label(window, text="Enter the re-transmission count: ", font=('Helvetica', 10, 'bold'))
lbl.grid(column=0, row=6)
global maximum_retransmission_count
maximum_retransmission_count = Spinbox(window, from_=0, to=1000, width=20)
maximum_retransmission_count.grid(column=1, row=6)
#
# # To display Maximum randomisation ceiling
lbl = Label(window, text="Enter maximum randomisation ceiling: ", font=('Helvetica', 10, 'bold'))
lbl.grid(column=0, row=7)
global maximum_randomisation_ceiling
maximum_randomisation_ceiling = Spinbox(window, from_=0, to=1000, width=20)
maximum_randomisation_ceiling.grid(column=1, row=7)
#
#
# # To display a button
global button
button = Button(window, text='Simulate', command=simulate, height=3, width=17, font=('Helvetica', 10, 'bold'))
button.grid(column=0, row=8, columnspan=2)
button.configure(background='Steelblue1')
# OutputDisplay
window.mainloop()
def display_output():
lb2 = Label(window, text="Simulation period: " + str(simulation_time), font=('Helvetica', 10, 'bold'))
lb2.grid(column=3, row=1)
lb2 = Label(window, text="Cumulative total of all the frames generated: " + str(total_frames),
font=('Helvetica', 10, 'bold'))
lb2.grid(column=3, row=2)
lb2 = Label(window, text="Average number of frames generated by each station: " + str(average_frames),
font=('Helvetica', 10, 'bold'))
lb2.grid(column=3, row=3)
lb2 = Label(window, text="Total frames lost due to buffer overflows and maximum retransmission count reached: "
+ str(lost_frame_count), font=('Helvetica', 10, 'bold'))
lb2.grid(column=3, row=4)
lb2 = Label(window, text="Total frames awaiting at the end of simulation time: " + str(awaiting_frame_count),
font=('Helvetica', 10, 'bold'))
lb2.grid(column=3, row=5)
lb2 = Label(window, text="Total successful frames at the end of simulation time: " + str(succ_frame_count),
font=('Helvetica', 10, 'bold'))
lb2.grid(column=3, row=6)
lb2 = Label(window, text="Total collisions at the end of simulation time: " + str(total_num_of_collisions),
font=('Helvetica', 10, 'bold'))
lb2.grid(column=3, row=7)
lb2 = Label(window, text="Throughput of the system: " + str(throughput), font=('Helvetica', 10, 'bold'))
lb2.grid(column=3, row=8)
lb2 = Label(window,
text="Channel utilization: " + str(channel_util),
font=('Helvetica', 10, 'bold'))
lb2.grid(column=3, row=9)
lb2 = Label(window,
text="Channel waste " + str(channel_waste),
font=('Helvetica', 10, 'bold'))
lb2.grid(column=3, row=10)
lb2 = Label(window,
text="Retransmission overhead " + str(retrans_overhead),
font=('Helvetica', 10, 'bold'))
lb2.grid(column=3, row=11)
lb2 = Label(window,
text="Average Delay" + str(avg_delay),
font=('Helvetica', 10, 'bold'))
lb2.grid(column=3, row=12)
# this is the main driver code for the project
# declare buttons here
# on click of 'Simulate' button
# formulate window
formulate_window()