-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathdisruptions.py
88 lines (71 loc) · 2.56 KB
/
disruptions.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
import random
from peer import BaseService, Connection
from peermanager import Hello
class BaseDisruption(BaseService):
mtbf = 24. * 60 * 60 # secs (mean time between failures)
availability = 0.97
interval = 1.
is_disrupted = False
def __init__(self, env, peer):
self.env = env
self.peer = peer
self.env.process(self.run())
def __repr__(self):
return '<%s %s>' % (self.__class__.__name__, self.peer.name)
def disruption_start(self):
pass
def disruption_end(self):
pass
def probe_status_change(self):
if not self.is_disrupted:
if random.random() < self.interval / self.mtbf:
# print self, 'started'
self.is_disrupted = True
self.disruption_start()
else:
avg_disruption_duration = self.mtbf * (1 - self.availability)
if random.random() < self.interval / avg_disruption_duration:
# print self, 'ended'
self.is_disrupted = False
self.disruption_end()
def run(self):
while True:
self.probe_status_change()
yield self.env.timeout(self.interval)
class Downtime(BaseDisruption):
"""
temporarily deactivates the peer
"""
mtbf = 4. * 60 # secs (mean time between failures)
availability = 0.4
interval = 1.
def __init__(self, env, peer):
self.last_peers = set()
super(Downtime, self).__init__(env, peer)
def disruption_start(self):
self.last_peers = self.peer.connections.keys()
self.peer.active = False
def disruption_end(self):
self.peer.active = True
for other in self.last_peers:
# create adhoc connection and send Hello
cnx = Connection(self.env, self.peer, other)
cnx.send(Hello(self.peer), connect=True)
class Slowdown(BaseDisruption):
"""
temporarily reduces bandwidth
"""
mtbf = 15 * 6 # secs (mean time between failures)
availability = 0.7 # full bandwidth
interval = 1.
bandwitdh_reduction = 0.2
def __init__(self, env, peer):
self.original_dl_bandwidth = peer.bandwidth_dl
self.original_ul_bandwidth = peer.bandwidth_ul
super(Slowdown, self).__init__(env, peer)
def disruption_start(self):
self.peer.bandwidth_ul *= self.bandwitdh_reduction
self.peer.bandwidth_dl *= self.bandwitdh_reduction
def disruption_end(self):
self.peer.bandwidth_dl = self.original_dl_bandwidth
self.peer.bandwidth_ul = self.original_ul_bandwidth