-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpipe_callback.py
101 lines (76 loc) · 2.89 KB
/
pipe_callback.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
__author__ = 'nick'
import simpy
SIM_DURATION = 100
class Pipe(object):
"""This class represents the propagation through a cable."""
def __init__(self, env, delay):
self.env = env
self.delay = delay
self.store = simpy.Store(env)
def latency(self, value):
yield self.env.timeout(self.delay)
self.store.put(value)
def put(self, value):
self.env.process(self.latency(value))
def get(self):
return self.store.get()
class Receiver:
def __init__(self, env, cable):
self.env = env
self.cable = cable
self.action = env.process(self.run())
self.msg_event = self.cable.get()
self.msg_event.callbacks.append(self.msg_callback)
self.msg_rcv = False
def msg_callback(self, event):
self.msg_event = self.cable.get()
self.msg_event.callbacks.append(self.msg_callback)
print("Got message at {0} saying {1}".format(self.env.now, event.value))
# self.action.interrupt()
self.msg_rcv = True
def run(self):
while True:
self.msg_event = self.cable.get()
self.msg_event.callbacks.append(self.msg_callback)
self.msg_rcv = False
try:
print("Doing normal work at {0}".format(self.env.now))
yield self.env.timeout(20)
except simpy.Interrupt:
print("Interrupted!!!")
def main():
def __sender(env, cable):
"""A process which randomly generates messages."""
while True:
# wait for next transmission
yield env.timeout(5)
print("Put message to pipe at {0}".format(env.now))
cable.put('Sender sent this at %d' % env.now)
def __msg_callback(event):
print("Received message at {0} saying {1}".format(env.now, event.value))
# def __receiver(env, cable):
# """A process which consumes messages."""
# while True:
# # Get event for message pipe
# msg_event = cable.get()
# msg_event.callbacks.append(__msg_callback)
# msg_timeout = env.timeout(1)
# yield msg_timeout
# # if msg_event.triggered:
# # # print('Received this at %d while %s' % (env.now, msg))
# # print("Received message at {0} saying {1}".format(env.now, msg_event.value))
# # msg_timeout.cancel()
# # else:
# # msg_event.cancel()
# # print("Still waiting for message at {0}".format(env.now))
# print("Doing stuff at {0}".format(env.now))
# Setup and start the simulation
print('Event Latency')
env = simpy.Environment()
cable = Pipe(env, 2)
env.process(__sender(env, cable))
# env.process(__receiver(env, cable))
receiver = Receiver(env, cable)
env.run(until=SIM_DURATION)
if __name__=="__main__":
main()