-
Notifications
You must be signed in to change notification settings - Fork 0
/
messages.py
290 lines (216 loc) · 7.45 KB
/
messages.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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
import struct
import logging
from collections import OrderedDict
log = logging.getLogger(__name__)
log.addHandler(logging.NullHandler())
class Message(object):
"""Abstract message base class."""
byte_format = ""
@classmethod
def unpack_string(cls, s):
(length,) = struct.unpack("!L", s[:4])
string = s[4:length + 4 - 1]
s = s[length + 4:]
return string, s
class LoginMessage(Message):
"""
Sent Monitor -> Scheduler to register for notifications.
Format is <host_id><msg_type>
"""
msg_type = 0x52
def pack(self):
return b''
@classmethod
def unpack(cls, string):
return LoginMessage()
def __str__(self):
return "[LOGIN]"
class StatsMessage(Message):
"""
Contains detailed information about one CS.
Format is <host_id>[body]
"""
msg_type = 0x57
def __init__(self, host_id, body):
self.host_id = host_id
self.parse_body(body)
def parse_body(self, body):
self.data = OrderedDict()
for l in body.split(b'\n'):
vals = l.split(b':')
if len(vals) == 2:
self.data[vals[0].decode()] = vals[1].decode()
def pack(self):
body = b'\n'.join(b':'.join(map(lambda s: s.encode(), i)) for i in self.data.items())
return (
struct.pack("!LL",
self.host_id,
len(body)) +
body +
b'\x00'
)
@classmethod
def unpack(cls, string):
(host_id,) = struct.unpack("!L", string[:4])
body, remainder = cls.unpack_string(string[4:])
assert(not remainder)
return StatsMessage(host_id, body)
def __str__(self):
body = "\n".join(": ".join(i) for i in self.data.items())
return "[STATS] host = {0}, \n{1}".format(self.host_id, body)
class LocalJobBeginMessage(Message):
"""
Indicates a local job (e.g. linking) is starting.
Format is <client_id><job_id><time>[filename]
"""
msg_type = 0x56
def __init__(self, job_id, client_id, time, filename):
self.job_id = job_id
self.client_id = client_id
self.time = time
self.filename = filename
def pack(self):
return (
struct.pack("!LLLL",
self.client_id,
self.job_id,
self.time,
len(self.filename)) +
self.filename +
b'\x00'
)
@classmethod
def unpack(cls, string):
hdr_len = struct.calcsize("!LLL")
(client_id,
job_id,
time) = struct.unpack("!LLL",
string[:hdr_len])
filename, remainder = cls.unpack_string(string[hdr_len:])
assert(not remainder)
return LocalJobBeginMessage(job_id, client_id, time, filename)
def __str__(self):
return (
"[LOCAL JOB BEGIN] id = {0}, client = {1}, time = {2}\n{3}"
).format(self.job_id, self.client_id, self.time, self.filename)
class LocalJobDoneMessage(Message):
"""
Indicates that a local job has been completed.
Format is <job_id>
"""
msg_type = 0x4f
def __init__(self, job_id):
self.job_id = job_id
def pack(self):
return struct.pack("!L", self.job_id)
@classmethod
def unpack(cls, string):
(job_id,) = struct.unpack("!L", string)
return LocalJobDoneMessage(job_id)
def __str__(self):
return "[LOCAL JOB DONE] id = {0}".format(self.id)
class GetCSMessage(Message):
"""
Indicates that a client has requested a CS to build the mentioned file.
Marks the creation of a new job.
Format is [filename]<lang><job_id><client_id>
"""
msg_type = 0x53
def __init__(self, filename, lang, job_id, client_id):
self.filename = filename
self.lang = lang
self.job_id = job_id
self.client_id = client_id
def pack(self):
return (
self.filename + "\x00" +
struct.pack("!LLL", self.lang, self.job_id, self.client_id)
)
@classmethod
def unpack(cls, string):
filename, string = cls.unpack_string(string)
(lang,
job_id,
client_id) = struct.unpack("!LLL", string)
return GetCSMessage(filename, lang, job_id, client_id)
def __str__(self):
return "[GetCS] lang = {0}, job_id = {1}, client_id = {2}\n{3}".format(
self.lang, self.job_id, self.client_id, self.filename)
class JobBeginMessage(Message):
"""
Indicates that a compile job is starting on a host.
Format is <job_id><time><host_id>
"""
msg_type = 0x54
def __init__(self, job_id, time, host_id):
self.job_id = job_id
self.time = time
self.host_id = host_id
def pack(self):
return struct.pack("!LLL", self.job_id, self.time, self.host_id)
@classmethod
def unpack(cls, string):
(job_id,
time,
host_id) = struct.unpack("!LLL", string)
return JobBeginMessage(job_id, time, host_id)
def __str__(self):
return "[JobBegin] job_id = {0}, host_id = {1}, time = {2}".format(
self.job_id, self.host_id, self.time)
class JobDoneMessage(Message):
"""
Indicates that a compile job is finished.
Format is <job_id><exit_code><real_ms><user_ms><sys_ms><pfaults>
<in_compressed><in_uncompressed><out_compressed><out_uncompressed>
<flags>
"""
msg_type = 0x55
byte_format = "!LLLLLLLLLLL"
def __init__(self, job_id, rc, real_ms, user_ms, sys_ms,
pfaults, in_comp, in_uncomp, out_comp, out_uncomp, flags):
self.job_id = job_id
self.rc = rc
self.real_ms = real_ms
self.user_ms = user_ms
self.sys_ms = sys_ms
self.pfaults = pfaults
self.in_comp = in_comp
self.in_uncomp = in_uncomp
self.out_comp = out_comp
self.out_uncomp = out_uncomp
self.flags = flags
def pack(self):
return struct.pack(self.byte_format, self.job_id, self.rc,
self.real_ms, self.user_ms, self.sys_ms,
self.pfaults, self.in_comp, self.in_uncomp,
self.out_comp, self.out_uncomp, self.flags)
@classmethod
def unpack(cls, string):
(job_id, rc,
real_ms, user_ms, sys_ms,
pfaults, in_comp, in_uncomp,
out_comp, out_uncomp, flags) = struct.unpack(cls.byte_format, string)
return JobDoneMessage(job_id, rc, real_ms, user_ms, sys_ms,
pfaults, in_comp, in_uncomp,
out_comp, out_uncomp, flags)
def __str__(self):
return (
"[JobDone] job_id = {m.job_id}, rc = {m.rc}\n"
"Time: real={m.real_ms}, user={m.user_ms}, sys={m.sys_ms}\n"
).format(m=self)
msg_types = {
LoginMessage.msg_type: LoginMessage,
StatsMessage.msg_type: StatsMessage,
LocalJobBeginMessage.msg_type: LocalJobBeginMessage,
LocalJobDoneMessage.msg_type: LocalJobDoneMessage,
GetCSMessage.msg_type: GetCSMessage,
JobBeginMessage.msg_type: JobBeginMessage,
JobDoneMessage.msg_type: JobDoneMessage,
}
def unpack(s):
(msg_type,) = struct.unpack("!L", s[:4])
if msg_type not in msg_types:
log.warn("Unknown message type {0}. Discarding.".format(msg_type))
return None
msg_cls = msg_types[msg_type]
return msg_cls.unpack(s[4:])