forked from hundeboll/riddler
/
riddler_node.py
262 lines (213 loc) · 8.19 KB
/
riddler_node.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
import threading
import socket
import pickle
import time
import copy
import riddler_interface as interface
nodes = []
class node(threading.Thread):
def __init__(self, name, host, port=8899):
super(node, self).__init__(None)
self.name = name
self.host = host
self.port = port
self.mesh_host = ""
self.mesh_port = 8877
self.dests = []
self.enable_ratio = False
self.sources = []
self.samples = []
self.store_samples = False
self.run_result = None
self.run_error = False
nodes.append(self)
self.end = threading.Event()
self.reply = threading.Event()
# Tell the main loop to stop
def stop(self):
# Stop any loops
self.end.set()
# Free waiters
self.reply.set()
# Close socket for faster quit
if self.socket:
self.socket.close()
# Free waiters
def pause(self):
# Tell the controller that this run should be discarded
self.run_error = True
# Free the controller from waiting
self.reply.set()
# Thread main function
def run(self):
# Thread main loop
while not self.end.is_set():
try:
# Try to connect this node
self.connect()
# Read data from node
self.recv()
except KeyboardInterrupt:
# Someone pressed Ctrl-C
self.stop()
return
except socket.timeout:
# Timed out during connect. Try again
continue
except socket.error as e:
# Something happened to the socket
if e.errno != 0:
# Error number 0 is self made, so don't print it
print("{0}: {1}".format(self.name.title(), e))
if e.errno in (113, 111):
# Connection refused. Wait a bit
time.sleep(5)
# Tell controller that something went wrong
self.run_error = True
self.socket = None
self.reply.set()
# Connect to configured node and start main loop
def connect(self):
self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Set timeout to 5 seconds to avoid too many reconnects.
# (See exception handler in self.run()
self.socket.settimeout(5)
# Do the connect
self.socket.connect((self.host, self.port))
# Reduce timeout to avoid hanging in socket.read() while quitting
self.socket.settimeout(.1)
# Reconnect to node
def reconnect(self):
# Free waiting processes
self.reply.set()
if self.socket:
# Closing the socket causes exception and reconnect in self.recv()
try:
self.socket.shutdown(socket.SHUT_RDWR)
except socket.error as e:
if e.errno != 107:
# We tried to close a non-connected socket
# Don't be too noisy
raise e
# Keep receiving objects from the node until connection is closed
def recv(self):
# Loop while we are not told otherwise
while not self.end.is_set():
try:
# Wait for data from the node
obj = interface.recv(self.socket)
if obj:
# Pass received object to the handler
self.handle(obj)
else:
# Connection was closed.
# Raise an exception to cause reconnect in self.run()
raise socket.error(0, "Connection closed")
except socket.timeout:
# This happens all the time. Just try again
continue
# Handle objects received from the node
def handle(self, obj):
if obj.cmd is interface.NODE_INFO:
self.handle_node_info(obj)
elif obj.cmd is interface.PREPARE_DONE:
self.handle_prepare_done(obj)
elif obj.cmd is interface.PREPARE_ERROR:
self.handle_prepare_error(obj)
elif obj.cmd is interface.RUN_RESULT:
self.handle_run_result(obj)
elif obj.cmd is interface.RUN_ERROR:
self.handle_run_error(obj)
elif obj.cmd is interface.FINISH_DONE:
self.handle_finish_done(obj)
elif obj.cmd is interface.SAMPLE:
self.handle_sample(obj)
elif obj.cmd is interface.SAMPLE_ERROR:
self.handle_sample_error(obj)
else:
print("Received unknown command from {0}: {1}".format(self.name, obj.cmd))
# Configure a destination node for tests
def add_dest(self, node):
self.dests.append(node)
# Return nicely formatted dictionary with destinations of this node
def get_dests(self):
return map(lambda n: {'name': n.name, 'host': n.mesh_host, 'port': n.mesh_port}, self.dests)
# Set if this node should apply the ratio given by a run_info
def set_enable_ratio(self, b):
self.enable_ratio = b
# Return result of current run
def get_result(self):
return self.run_result
# Return received samples from current run
def get_samples(self):
return self.samples
# Wait for node to answer last command
def wait(self):
while not self.end.is_set():
if self.reply.wait(.1):
break
return self.run_error
# Save information received from node
def handle_node_info(self, obj):
self.mesh_host = obj.mesh_host
self.mesh_port = obj.mesh_port
self.mesh_mac = obj.mesh_mac
self.reply.set()
# Tell the node to prepare a new run
def prepare_run(self, run_info):
# Adjust rate according to ratio, if enabled
run_info = copy.deepcopy(run_info)
if self.enable_ratio and run_info['ratio']:
run_info['rate'] = run_info['rate'] * run_info['ratio']/100
self.samples = []
self.run_info = run_info
self.run_error = False
self.run_result = None
self.reply.clear()
interface.send_node(self.socket, interface.PREPARE_RUN, dests=self.get_dests(), run_info=run_info)
# Set event to inform waiting callers
def handle_prepare_done(self, obj):
self.reply.set()
# Be verbose about errors occurring during while configuring the node
def handle_prepare_error(self, obj):
self.run_error = True
print("Setup failed at {0}: {1}".format(self.name, obj.error))
# Tell node to start a test run
def start_run(self):
self.reply.clear()
interface.send_node(self.socket, interface.START_RUN)
self.store_samples = True
# Save received result and set event to inform waiting callers
def handle_run_result(self, obj):
if obj.result:
self.store_samples = False
self.run_result = obj.result
# Send result to connected clients
self.client.export_result(self.name, self.run_info, obj.result)
self.reply.set()
# Save received error and set event to inform waiting callers
def handle_run_error(self, obj):
self.run_error = True
print("Run failed on {0}: {1}".format(self.name, obj.error))
self.reply.set()
# Tell node to clean up after test run
def finish_run(self):
self.store_samples = False
self.reply.clear()
interface.send_node(self.socket, interface.FINISH_RUN)
# Set event to inform waiting callers
def handle_finish_done(self, obj):
self.reply.set()
# Save received measurement sample for later extraction
def handle_sample(self, obj):
# Add name to sample
obj.sample['node'] = self.name
# Only store sample if a test is running
if self.store_samples:
self.samples.append(obj.sample)
# Send sample to connected clients
self.client.export_sample(self.name, obj.sample)
# Be verbose about sampling errors that not necessarily ruins the run result
def handle_sample_error(self, obj):
self.run_error = True
print("Sampling failed at {0}: {1}".format(self.name, obj.error))