-
Notifications
You must be signed in to change notification settings - Fork 0
/
HsMessage.py
209 lines (172 loc) · 6.82 KB
/
HsMessage.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
#!/usr/bin/env python
"""
Message-related functions and constants
"""
import binascii
import getpass
import numbers
import struct
import sys
import threading
import time
import HsConstants
from HsException import HsException
from HsPrefix import HsPrefix
from HsUtil import dict_to_object
# version number for current message format
CURRENT_VERSION = 2
# message types
INITIAL = "REQUEST"
STARTED = "STARTED" # hub has received a request
WORKING = "WORKING" # hub is sending data (can be sent many times)
DONE = "DONE" # hub has finished sending data
FAILED = "FAILED" # hub failed to fill request
IGNORED = "IGNORED" # hub is not part of the request
DELETE = "DELETE" # delete a request
# mandatory message fields
__MANDATORY_FIELDS = ("username", "prefix", "destination_dir", "host",
"version")
class ID(object):
"Unique ID generator"
__seed = 0
__seed_lock = threading.Lock()
@classmethod
def generate(cls):
with cls.__seed_lock:
val = cls.__seed
cls.__seed = (cls.__seed + 1) % 0xFFFFFF
packed = struct.pack('>i', int(time.time()))
packed += struct.pack('>i', val)[1:4]
new_id = binascii.hexlify(packed)
if isinstance(new_id, bytes):
new_id = new_id.decode()
return new_id
def dict_to_message(mdict, allow_old_format=False):
fixed = fix_message_dict(mdict, allow_old_format=allow_old_format)
return dict_to_object(fixed, __MANDATORY_FIELDS, "Message")
def fix_message_dict(mdict, allow_old_format=False):
if mdict is None:
return None
if not isinstance(mdict, dict):
raise HsException("Message is not a dictionary: \"%s\"<%s>" %
(mdict, type(mdict)))
for prefix in ("start", "stop"):
found = False
for suffix in ("ticks", "time"):
if prefix + "_" + suffix in mdict:
found = True
break
if not found:
raise HsException("Dictionary is missing %s_time or %s_ticks" %
(prefix, prefix))
# check for mandatory fields
if "msgtype" not in mdict:
mdict["msgtype"] = INITIAL
if "request_id" not in mdict:
if mdict["msgtype"] != INITIAL:
raise HsException("No request ID found in %s" % str(mdict))
mdict["request_id"] = ID.generate()
if "copy_dir" not in mdict:
mdict["copy_dir"] = None
if "extract" not in mdict:
mdict["extract"] = False
if "hubs" not in mdict:
mdict["hubs"] = ""
return mdict
def from_dict(mdict, allow_old_format=False):
"""
Convert a dictionary to an HsMessage object
"""
return dict_to_message(mdict, allow_old_format=allow_old_format)
def send(sock, msgtype, req_id, user, start_ticks, stop_ticks, destdir,
prefix=None, copydir=None, extract=None, host=None, hubs=None,
version=None):
# check for required fields
if req_id is None:
raise HsException("Request ID is not set")
elif isinstance(req_id, bytes):
req_id = req_id.decode()
if msgtype is None:
raise HsException("Message type is not set for Req#" + str(req_id))
if user is None:
raise HsException("Username is not set for Req#" + str(req_id))
if start_ticks is None:
raise HsException("Start time is not set for Req#" + str(req_id))
elif not isinstance(start_ticks, numbers.Number):
raise TypeError("Start time %s<%s> should be number for Req#%s" %
(start_ticks, type(start_ticks).__name__, req_id))
if stop_ticks is None:
raise HsException("Stop time is not set for Req#" + str(req_id))
elif not isinstance(stop_ticks, numbers.Number):
raise TypeError("Stop time %s<%s> should be number for Req#%s" %
(stop_ticks, type(stop_ticks).__name__, req_id))
if destdir is None:
raise HsException("Destination directory is not set for Req#" +
str(req_id))
# fill in optional or deriveable fields
if prefix is None:
prefix = HsPrefix.guess_from_dir(destdir)
extract = extract is not None and extract
if version is None:
version = CURRENT_VERSION
# I3Live should only specify one of their known dropboxes
if prefix == HsPrefix.LIVE:
if not destdir.endswith("/"):
chkdir = destdir
else:
chkdir = destdir[:-1]
# testbed/test_requests.py writes to /tmp/TESTCLUSTER
if destdir not in HsConstants.I3LIVE_DROPBOXES \
and chkdir not in HsConstants.I3LIVE_DROPBOXES \
and "TESTCLUSTER" not in destdir:
raise HsException("Destination \"%s\" is not valid for %s"
" in Req#%s" % (destdir, prefix, str(req_id)))
msg = {
"msgtype": msgtype,
"version": version,
"request_id": req_id,
"username": user,
"start_ticks": start_ticks,
"stop_ticks": stop_ticks,
"copy_dir": copydir,
"destination_dir": destdir,
"prefix": prefix,
"extract": extract,
"hubs": hubs,
"host": host,
}
return sock.send_json(msg) is None
def send_for_request(sock, req, host, copydir, destdir, msgtype):
return send(sock, msgtype, req.request_id, req.username, req.start_ticks,
req.stop_ticks, destdir, prefix=req.prefix, copydir=copydir,
extract=req.extract, host=host, version=None)
def send_initial(sock, req_id, start_ticks, stop_ticks, destdir, prefix=None,
extract_hits=False, hubs=None, host=None, username=None):
"Send initial request"
if req_id is None:
req_id = ID.generate()
if username is None:
username = getpass.getuser()
return send(sock, INITIAL, req_id, username, start_ticks,
stop_ticks, destdir, prefix=prefix, copydir=None,
extract=extract_hits, hubs=hubs, host=host,
version=CURRENT_VERSION)
def send_worker_status(sock, req, host, copydir, destdir, msgtype):
"Send worker status to HsSender based on the request"
if hasattr(req, "start_ticks"):
start_ticks = req.start_ticks
elif hasattr(req, "start_time"):
start_ticks = req.start_time.ticks
else:
raise ValueError("Request is missing 'start_ticks' or 'start_time':"
" %s" % (req, ))
if hasattr(req, "stop_ticks"):
stop_ticks = req.stop_ticks
elif hasattr(req, "stop_time"):
stop_ticks = req.stop_time.ticks
else:
raise ValueError("Request is missing 'stop_ticks' or 'stop_time':"
" %s" % (req, ))
return send(sock, msgtype, req.request_id, req.username, start_ticks,
stop_ticks, destdir, prefix=req.prefix, copydir=copydir,
extract=req.extract, host=host, version=None)