-
Notifications
You must be signed in to change notification settings - Fork 0
/
simulate.py
executable file
·58 lines (45 loc) · 1.61 KB
/
simulate.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
#!/usr/bin/env python3
import sys
import Algorithm, Adversary
if len(sys.argv) != 5 or not hasattr(Algorithm, sys.argv[1]) or not hasattr(Adversary, sys.argv[2]):
print("Usage: {} <algorithm> <adversary> <events file> <model file>".format(sys.argv[0]))
sys.exit(1)
from Model import Model
from Event import InjectEvent, SentEvent, ErrorEvent, ScheduleEvent, WaitEvent
from Events import Events
def log(event):
print(event)
def play(algorithm, adversary, events, speedup):
def schedule(algorithm):
packet = algorithm.schedule()
if packet:
if algorithm.algorithmType == "ALG":
events.schedule(SentEvent(time + packet / speedup, algorithm))
else:
events.schedule(SentEvent(time + packet, algorithm))
log(ScheduleEvent(time, algorithm, packet))
return packet
def scheduleError(time, error):
if error:
events.schedule(ErrorEvent(time + error) if error >= 0 else WaitEvent())
while events.hasNext():
event = events.next()
time = event.time
log(event)
algorithm.notify(event)
adversary.notify(event)
if events.hasNextNow(time): continue
if not algorithm.sending:
packet = schedule(algorithm)
packet = packet / speedup if packet else None
error = adversary.algorithmSchedules(packet)
scheduleError(time, error)
if not adversary.sending:
advpacket = schedule(adversary)
error = adversary.adversarySchedules(advpacket)
scheduleError(time, error)
model = Model.fromFile(sys.argv[4])
algorithm = getattr(Algorithm, sys.argv[1])(model)
adversary = getattr(Adversary, sys.argv[2])(model)
events = Events.fromFile(sys.argv[3])
play(algorithm, adversary, events, model.speedup)