|
13 | 13 | MESSAGE_FILE_OTHER = "messages_other.json" |
14 | 14 | LOCK_FILE_JAVA = "lock_java.txt" |
15 | 15 | LOCK_FILE_OTHER = "lock_other.txt" |
| 16 | +STARTED_FILE_JAVA = "started_java.txt" |
| 17 | +STARTED_FILE_OTHER = "started_other.txt" |
16 | 18 |
|
17 | 19 | class CrossPlayException(Exception): |
18 | 20 | def __init__(self, message): |
@@ -200,60 +202,130 @@ def from_json(cls, json_data): |
200 | 202 |
|
201 | 203 | # 0.1 ms timestep, 10 min timeout |
202 | 204 | def wait(message: CrossPlayMessage, timeout=600, timestep=0.0001, message_dir=MESSAGE_DIR): |
203 | | - read_file = os.path.join(message_dir, MESSAGE_FILE_JAVA) |
204 | | - write_file = os.path.join(message_dir, MESSAGE_FILE_OTHER) |
205 | | - java_lock_file = os.path.join(message_dir, LOCK_FILE_JAVA) |
206 | | - other_lock_file = os.path.join(message_dir, LOCK_FILE_OTHER) |
| 205 | + try: |
| 206 | + read_file = os.path.join(message_dir, MESSAGE_FILE_JAVA) |
| 207 | + write_file = os.path.join(message_dir, MESSAGE_FILE_OTHER) |
| 208 | + java_lock_file = os.path.join(message_dir, LOCK_FILE_JAVA) |
| 209 | + other_lock_file = os.path.join(message_dir, LOCK_FILE_OTHER) |
207 | 210 |
|
208 | | - json_message = message.to_json() |
209 | | - time_limit = time.time() + timeout |
| 211 | + # if directory does not exist, create it |
| 212 | + if not os.path.exists(message_dir): |
| 213 | + os.makedirs(message_dir) |
210 | 214 |
|
211 | | - # print(f"Waiting to send message Python -> Java: {json_message}") |
| 215 | + json_message = message.to_json() |
| 216 | + time_limit = time.time() + timeout |
212 | 217 |
|
213 | | - while os.path.exists(read_file) or os.path.exists(write_file) or os.path.exists(java_lock_file): |
214 | | - time.sleep(timestep) |
| 218 | + # print(f"Waiting to send message Python -> Java: {json_message}") |
215 | 219 |
|
216 | | - if time.time() > time_limit: |
217 | | - raise CrossPlayException("Cross-play message passing timed out (Python waiting, Java busy).") |
| 220 | + while os.path.exists(read_file) or os.path.exists(write_file) or os.path.exists(java_lock_file): |
| 221 | + time.sleep(timestep) |
218 | 222 |
|
219 | | - if not os.path.exists(other_lock_file): |
220 | | - with open(other_lock_file, 'x') as f: |
221 | | - f.write('') |
| 223 | + if time.time() > time_limit: |
| 224 | + raise CrossPlayException("Cross-play message passing timed out (Python waiting, Java busy).") |
222 | 225 |
|
223 | | - # print("Created other lock file") |
| 226 | + if not os.path.exists(other_lock_file): |
| 227 | + with open(other_lock_file, 'x') as f: |
| 228 | + f.write('') |
224 | 229 |
|
225 | | - with open(write_file, 'w') as f: |
226 | | - json.dump(json_message, f) |
| 230 | + # print("Created other lock file") |
227 | 231 |
|
228 | | - if os.path.exists(other_lock_file): |
229 | | - os.remove(other_lock_file) |
| 232 | + with open(write_file, 'w') as f: |
| 233 | + json.dump(json_message, f) |
230 | 234 |
|
231 | | - # print(f"Sent message Python -> Java: {json_message}") |
232 | | - # print("Waiting for response Java -> Python...") |
233 | | - time_limit = time.time() + timeout |
234 | | - |
235 | | - while not os.path.exists(read_file) or os.path.exists(write_file) or os.path.exists(java_lock_file): |
236 | | - time.sleep(timestep) |
| 235 | + if os.path.exists(other_lock_file): |
| 236 | + os.remove(other_lock_file) |
| 237 | + |
| 238 | + # print(f"Sent message Python -> Java: {json_message}") |
| 239 | + # print("Waiting for response Java -> Python...") |
| 240 | + time_limit = time.time() + timeout |
| 241 | + |
| 242 | + while not os.path.exists(read_file) or os.path.exists(write_file) or os.path.exists(java_lock_file): |
| 243 | + time.sleep(timestep) |
237 | 244 |
|
238 | | - if time.time() > time_limit: |
239 | | - raise CrossPlayException("Cross-play message passing timed out (Python waiting, Java not responding).") |
| 245 | + if time.time() > time_limit: |
| 246 | + raise CrossPlayException("Cross-play message passing timed out (Python waiting, Java not responding).") |
240 | 247 |
|
241 | | - if not os.path.exists(other_lock_file): |
242 | | - with open(other_lock_file, 'x') as f: |
243 | | - f.write('') |
| 248 | + if not os.path.exists(other_lock_file): |
| 249 | + with open(other_lock_file, 'x') as f: |
| 250 | + f.write('') |
| 251 | + |
| 252 | + with open(read_file, 'r') as f: |
| 253 | + json_data = json.load(f) |
| 254 | + result = CrossPlayObject.from_json(json_data) |
| 255 | + |
| 256 | + os.remove(read_file) |
| 257 | + |
| 258 | + if os.path.exists(other_lock_file): |
| 259 | + os.remove(other_lock_file) |
244 | 260 |
|
245 | | - with open(read_file, 'r') as f: |
246 | | - json_data = json.load(f) |
247 | | - result = CrossPlayObject.from_json(json_data) |
| 261 | + # print(f"Received message Java -> Python: {result}") |
| 262 | + |
| 263 | + if isinstance(result, CrossPlayLiteral): |
| 264 | + return result.reduce_literal() |
| 265 | + else: |
| 266 | + return result |
| 267 | + except IOError as e: |
| 268 | + raise CrossPlayException("Cross-play message passing failed due to file I/O error: " + str(e)) |
| 269 | + |
| 270 | +def reset_files(message_dir=MESSAGE_DIR): |
| 271 | + read_file = os.path.join(message_dir, MESSAGE_FILE_JAVA) |
| 272 | + write_file = os.path.join(message_dir, MESSAGE_FILE_OTHER) |
| 273 | + java_lock_file = os.path.join(message_dir, LOCK_FILE_JAVA) |
| 274 | + other_lock_file = os.path.join(message_dir, LOCK_FILE_OTHER) |
| 275 | + java_started_file = os.path.join(message_dir, STARTED_FILE_JAVA) |
| 276 | + other_started_file = os.path.join(message_dir, STARTED_FILE_OTHER) |
| 277 | + |
| 278 | + if not os.path.exists(message_dir) or not os.path.isdir(message_dir): |
| 279 | + os.makedirs(message_dir) |
| 280 | + elif os.path.exists(other_started_file): |
| 281 | + print("DEBUGGING: Detected existing crossplay_temp/started_other.txt file. " \ |
| 282 | + "This indicates that a previous cross-play match did not terminate cleanly. " \ |
| 283 | + "Deleting the old crossplay_temp files.") |
| 284 | + elif os.path.exists(java_started_file): |
| 285 | + print("DEBUGGING: Java cross-play runner already started. Using existing cross-play temp directory.") |
| 286 | + return |
| 287 | + |
| 288 | + if os.path.exists(read_file): |
| 289 | + os.remove(read_file) |
248 | 290 |
|
249 | | - os.remove(read_file) |
| 291 | + if os.path.exists(write_file): |
| 292 | + os.remove(write_file) |
| 293 | + |
| 294 | + if os.path.exists(java_lock_file): |
| 295 | + os.remove(java_lock_file) |
250 | 296 |
|
251 | 297 | if os.path.exists(other_lock_file): |
252 | 298 | os.remove(other_lock_file) |
| 299 | + |
| 300 | + if not os.path.exists(os.path.join(message_dir, STARTED_FILE_OTHER)): |
| 301 | + with open(other_started_file, 'x') as f: |
| 302 | + f.write('') |
| 303 | + |
| 304 | +def clear_temp_files(message_dir=MESSAGE_DIR): |
| 305 | + if not os.path.exists(message_dir) or not os.path.isdir(message_dir): |
| 306 | + return |
253 | 307 |
|
254 | | - # print(f"Received message Java -> Python: {result}") |
| 308 | + read_file = os.path.join(message_dir, MESSAGE_FILE_JAVA) |
| 309 | + write_file = os.path.join(message_dir, MESSAGE_FILE_OTHER) |
| 310 | + java_lock_file = os.path.join(message_dir, LOCK_FILE_JAVA) |
| 311 | + other_lock_file = os.path.join(message_dir, LOCK_FILE_OTHER) |
| 312 | + java_started_file = os.path.join(message_dir, STARTED_FILE_JAVA) |
| 313 | + other_started_file = os.path.join(message_dir, STARTED_FILE_OTHER) |
255 | 314 |
|
256 | | - if isinstance(result, CrossPlayLiteral): |
257 | | - return result.reduce_literal() |
258 | | - else: |
259 | | - return result |
| 315 | + if os.path.exists(read_file): |
| 316 | + os.remove(read_file) |
| 317 | + |
| 318 | + if os.path.exists(write_file): |
| 319 | + os.remove(write_file) |
| 320 | + |
| 321 | + if os.path.exists(java_lock_file): |
| 322 | + os.remove(java_lock_file) |
| 323 | + |
| 324 | + if os.path.exists(other_lock_file): |
| 325 | + os.remove(other_lock_file) |
| 326 | + |
| 327 | + if os.path.exists(java_started_file): |
| 328 | + os.remove(java_started_file) |
| 329 | + |
| 330 | + if os.path.exists(other_started_file): |
| 331 | + os.remove(other_started_file) |
0 commit comments