-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalgorithm.py
More file actions
80 lines (78 loc) · 2.96 KB
/
algorithm.py
File metadata and controls
80 lines (78 loc) · 2.96 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
# vim:ts=4:sts=4:sw=4:expandtab
import api
import logging
import random
rconst=10
class SimpleAlgorithm(api.Algorithm):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
def tick(self):
logging.debug("Algorithm tick : %s"%(repr([
self.tick_count,
(self.frame and str(self.frame.id)),
(self.frame and self.frame.first_tick),
(self.frame and self.frame.length),
self.part,
self.channel.transmit_status,
self.max_length,
]),))
#print(self.channel._transmit_status)
if self.frame is not None:
self.transmit()
class aloha2n(api.Algorithm):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.wait_const=rconst
self.wait=random.randrange(1,self.wait_const) #mozna zrobic testy jaka stala jest ok
self.wait_counter=self.wait
def tick(self):
if self.frame is not None:
if(self.wait-self.wait_counter==0):
self.transmit()
if(self.channel._transmit_status==False):
self.wait=self.wait*2+random.randrange(1,self.wait_const)
self.wait_const*=2
self.wait_counter=0
class slotted(api.Algorithm):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.started=0
self.wait_const=rconst
self.wait=random.randrange(1,self.wait_const) #mozna zrobic testy jaka stala jest ok
self.wait_counter=self.wait
def tick(self):
if self.frame is not None:
if(self.wait-self.wait_counter==0):
if self.started==1:
self.transmit()
else: #started = 0
if self.tick_count%self.max_length==0:
self.transmit()
self.started=1
if(self.channel._transmit_status==False):
self.wait=self.wait*2+random.randrange(1,self.wait_const)
self.wait_const*=2 #rosnie z czasem
self.wait_counter=0
self.started=0
class CSMA(api.Algorithm):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.started=0
self.wait_const=rconst
self.wait=random.randrange(1,self.wait_const)
self.wait_counter=self.wait
def tick(self):
if self.frame is not None:
if(self.wait-self.wait_counter==0):
if self.started==1:
self.transmit()
else: #started = 0
if self.channel._carrier_sense==None:
self.transmit()
self.started=1
self.wait_const=rconst
if(self.channel._transmit_status==False):
self.wait=random.randrange(1,self.wait_const)
self.wait_counter=0
self.wait_const*=2
self.started=0