-
Notifications
You must be signed in to change notification settings - Fork 1
/
station.py
75 lines (59 loc) · 2.26 KB
/
station.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
'''
ALOHA PROTOCOL SIMULATION
authors: George Evangelou, Nikos Roussos
date: July 2019
'''
import random
import ini
class Station:
"""This class determines the elements and actions of a station in the network"""
def __init__(self, id):
self.probability = random.uniform(ini.min_probability, ini.max_probability)
self.id = str(id)
self.throughput = 0
self.maxdelay = 0
self.avdelay = 0
self.queuesize = 0
self.packetssent = 0
self.totaldelays = []
self.currentdelays = []
self.pending = False
self.collisions = 0
self.backoffdelay = 0
def __str__(self):
a = "This node's statistics are the following: \n"
b = "probability: {0}, queuesize: {1}, packetssent: {2} \n".format(self.probability, self.queuesize, self.packetssent)
c = "totaldelays: {0}, pending: {1} \n".format(self.totaldelays, self.pending)
d = " throughput: {0}, maxdelay: {1}, avdelay: {2} \n".format(self.throughput, self.maxdelay, self.avdelay)
return a+b+c+d
def successfulsend(self):
self.queuesize -= 1
self.packetssent += 1
self. backoffdelay = 0
self.collisions = 0
self.totaldelays.append(self.currentdelays[0]+1) #+1 packet time to the packet that was successfully sent
del self.currentdelays[0]
if (self.queuesize == 0): self.pending = False
return
def _newpacket(self):
if (self.probability > random.random()) :
self.queuesize += 1
self.pending = True
self.currentdelays.append(0)
return
def wantstosend(self):
self._newpacket()
if (self.backoffdelay > 0): self.backoffdelay -= 1
if (self.backoffdelay == 0): return self.queuesize > 0
return False
def _incrementdelays(self):
if self.currentdelays == []: return
for i in range(self.queuesize): self.currentdelays[i] += 1
return
def update(self):
self._incrementdelays()
if (self.totaldelays == []): return
self.maxdelay = max(self.totaldelays)
self.avdelay = sum(self.totaldelays)/len(self.totaldelays)
self.throughput = self.packetssent / ini.time_elapsed
return