-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathconcore.py
More file actions
338 lines (299 loc) · 12.3 KB
/
concore.py
File metadata and controls
338 lines (299 loc) · 12.3 KB
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
import time
import os
from ast import literal_eval
import sys
import re
import zmq # Added for ZeroMQ
# if windows, create script to kill this process
# because batch files don't provide easy way to know pid of last command
# ignored for posix != windows, because "concorepid" is handled by script
# ignored for docker (linux != windows), because handled by docker stop
if hasattr(sys, 'getwindowsversion'):
with open("concorekill.bat","w") as fpid:
fpid.write("taskkill /F /PID "+str(os.getpid())+"\n")
# ===================================================================
# ZeroMQ Communication Wrapper
# ===================================================================
class ZeroMQPort:
def __init__(self, port_type, address, zmq_socket_type):
"""
port_type: "bind" or "connect"
address: ZeroMQ address (e.g., "tcp://*:5555")
zmq_socket_type: zmq.REQ, zmq.REP, zmq.PUB, zmq.SUB etc.
"""
self.context = zmq.Context()
self.socket = self.context.socket(zmq_socket_type)
self.port_type = port_type # "bind" or "connect"
self.address = address
# Configure timeouts & immediate close on failure
self.socket.setsockopt(zmq.RCVTIMEO, 2000) # 2 sec receive timeout
self.socket.setsockopt(zmq.SNDTIMEO, 2000) # 2 sec send timeout
self.socket.setsockopt(zmq.LINGER, 0) # Drop pending messages on close
# Bind or connect
if self.port_type == "bind":
self.socket.bind(address)
print(f"ZMQ Port bound to {address}")
else:
self.socket.connect(address)
print(f"ZMQ Port connected to {address}")
def send_json_with_retry(self, message):
"""Send JSON message with retries if timeout occurs."""
for attempt in range(5):
try:
self.socket.send_json(message)
return
except zmq.Again:
print(f"Send timeout (attempt {attempt + 1}/5)")
time.sleep(0.5)
print("Failed to send after retries.")
return
def recv_json_with_retry(self):
"""Receive JSON message with retries if timeout occurs."""
for attempt in range(5):
try:
return self.socket.recv_json()
except zmq.Again:
print(f"Receive timeout (attempt {attempt + 1}/5)")
time.sleep(0.5)
print("Failed to receive after retries.")
return None
# Global ZeroMQ ports registry
zmq_ports = {}
def init_zmq_port(port_name, port_type, address, socket_type_str):
"""
Initializes and registers a ZeroMQ port.
port_name (str): A unique name for this ZMQ port.
port_type (str): "bind" or "connect".
address (str): The ZMQ address (e.g., "tcp://*:5555", "tcp://localhost:5555").
socket_type_str (str): String representation of ZMQ socket type (e.g., "REQ", "REP", "PUB", "SUB").
"""
if port_name in zmq_ports:
print(f"ZMQ Port {port_name} already initialized.")
return # Avoid reinitialization
try:
# Map socket type string to actual ZMQ constant (e.g., zmq.REQ, zmq.REP)
zmq_socket_type = getattr(zmq, socket_type_str.upper())
zmq_ports[port_name] = ZeroMQPort(port_type, address, zmq_socket_type)
print(f"Initialized ZMQ port: {port_name} ({socket_type_str}) on {address}")
except AttributeError:
print(f"Error: Invalid ZMQ socket type string '{socket_type_str}'.")
except zmq.error.ZMQError as e:
print(f"Error initializing ZMQ port {port_name} on {address}: {e}")
except Exception as e:
print(f"An unexpected error occurred during ZMQ port initialization for {port_name}: {e}")
def terminate_zmq():
for port in zmq_ports.values():
try:
port.socket.close()
port.context.term()
except Exception as e:
print(f"Error while terminating ZMQ port {port.address}: {e}")
# --- ZeroMQ Integration End ---
# ===================================================================
# File & Parameter Handling
# ===================================================================
def safe_literal_eval(filename, defaultValue):
try:
with open(filename, "r") as file:
return literal_eval(file.read())
except (FileNotFoundError, SyntaxError, ValueError, Exception) as e:
# Keep print for debugging, but can be made quieter
# print(f"Info: Error reading {filename} or file not found, using default: {e}")
return defaultValue
# Load input/output ports if present
iport = safe_literal_eval("concore.iport", {})
oport = safe_literal_eval("concore.oport", {})
# Global variables
s = ''
olds = ''
delay = 1
retrycount = 0
inpath = "./in" #must be rel path for local
outpath = "./out"
simtime = 0
#9/21/22
# ===================================================================
# Parameter Parsing
# ===================================================================
try:
sparams_path = os.path.join(inpath + "1", "concore.params")
if os.path.exists(sparams_path):
with open(sparams_path, "r") as f:
sparams = f.read()
if sparams: # Ensure sparams is not empty
# Windows sometimes keeps quotes
if sparams[0] == '"' and sparams[-1] == '"': #windows keeps "" need to remove
sparams = sparams[1:-1]
# Convert key=value;key2=value2 to Python dict format
if sparams != '{' and not (sparams.startswith('{') and sparams.endswith('}')): # Check if it needs conversion
print("converting sparams: "+sparams)
sparams = "{'"+re.sub(';',",'",re.sub('=',"':",re.sub(' ','',sparams)))+"}"
print("converted sparams: " + sparams)
try:
params = literal_eval(sparams)
except Exception as e:
print(f"bad params content: {sparams}, error: {e}")
params = dict()
else:
params = dict()
else:
params = dict()
except Exception as e:
# print(f"Info: concore.params not found or error reading, using empty dict: {e}")
params = dict()
#9/30/22
def tryparam(n, i):
"""Return parameter `n` from params dict, else default `i`."""
return params.get(n, i)
#9/12/21
# ===================================================================
# Simulation Time Handling
# ===================================================================
def default_maxtime(default):
"""Read maximum simulation time from file or use default."""
global maxtime
maxtime_path = os.path.join(inpath + "1", "concore.maxtime")
maxtime = safe_literal_eval(maxtime_path, default)
default_maxtime(100)
def unchanged():
"""Check if global string `s` is unchanged since last call."""
global olds, s
if olds == s:
s = ''
return True
olds = s
return False
# ===================================================================
# I/O Handling (File + ZMQ)
# ===================================================================
def read(port_identifier, name, initstr_val):
global s, simtime, retrycount
# Default return
default_return_val = initstr_val
if isinstance(initstr_val, str):
try:
default_return_val = literal_eval(initstr_val)
except (SyntaxError, ValueError):
pass
# Case 1: ZMQ port
if isinstance(port_identifier, str) and port_identifier in zmq_ports:
zmq_p = zmq_ports[port_identifier]
try:
message = zmq_p.recv_json_with_retry()
return message
except zmq.error.ZMQError as e:
print(f"ZMQ read error on port {port_identifier} (name: {name}): {e}. Returning default.")
return default_return_val
except Exception as e:
print(f"Unexpected error during ZMQ read on port {port_identifier} (name: {name}): {e}. Returning default.")
return default_return_val
# Case 2: File-based port
try:
file_port_num = int(port_identifier)
except ValueError:
print(f"Error: Invalid port identifier '{port_identifier}' for file operation. Must be integer or ZMQ name.")
return default_return_val
time.sleep(delay)
file_path = os.path.join(inpath+str(file_port_num), name)
ins = ""
try:
with open(file_path, "r") as infile:
ins = infile.read()
except FileNotFoundError:
ins = str(initstr_val)
except Exception as e:
print(f"Error reading {file_path}: {e}. Using default value.")
return default_return_val
# Retry logic if file is empty
attempts = 0
max_retries = 5
while len(ins) == 0 and attempts < max_retries:
time.sleep(delay)
try:
with open(file_path, "r") as infile:
ins = infile.read()
except Exception as e:
print(f"Retry {attempts + 1}: Error reading {file_path} - {e}")
attempts += 1
retrycount += 1
if len(ins) == 0:
print(f"Max retries reached for {file_path}, using default value.")
return default_return_val
s += ins
# Try parsing
try:
inval = literal_eval(ins)
if isinstance(inval, list) and len(inval) > 0:
current_simtime_from_file = inval[0]
if isinstance(current_simtime_from_file, (int, float)):
simtime = max(simtime, current_simtime_from_file)
return inval[1:]
else:
print(f"Warning: Unexpected data format in {file_path}: {ins}. Returning raw content or default.")
return inval
except Exception as e:
print(f"Error parsing content from {file_path} ('{ins}'): {e}. Returning default.")
return default_return_val
def write(port_identifier, name, val, delta=0):
"""
Write data either to ZMQ port or file.
`val` must be list (with simtime prefix) or string.
"""
global simtime
# Case 1: ZMQ port
if isinstance(port_identifier, str) and port_identifier in zmq_ports:
zmq_p = zmq_ports[port_identifier]
try:
zmq_p.send_json_with_retry(val)
except zmq.error.ZMQError as e:
print(f"ZMQ write error on port {port_identifier} (name: {name}): {e}")
except Exception as e:
print(f"Unexpected error during ZMQ write on port {port_identifier} (name: {name}): {e}")
# Case 2: File-based port
try:
if isinstance(port_identifier, str) and port_identifier in zmq_ports:
file_path = os.path.join("../"+port_identifier, name)
else:
file_port_num = int(port_identifier)
file_path = os.path.join(outpath+str(file_port_num), name)
except ValueError:
print(f"Error: Invalid port identifier '{port_identifier}' for file operation. Must be integer or ZMQ name.")
return
# File writing rules
if isinstance(val, str):
time.sleep(2 * delay) # string writes wait longer
elif not isinstance(val, list):
print(f"File write to {file_path} must have list or str value, got {type(val)}")
return
try:
with open(file_path, "w") as outfile:
if isinstance(val, list):
data_to_write = [simtime + delta] + val
outfile.write(str(data_to_write))
simtime += delta
else:
outfile.write(val)
except Exception as e:
print(f"Error writing to {file_path}: {e}")
def initval(simtime_val_str):
"""
Initialize simtime from string containing a list.
Example: "[10, 'foo', 'bar']" → simtime=10, returns ['foo','bar']
"""
global simtime
try:
val = literal_eval(simtime_val_str)
if isinstance(val, list) and len(val) > 0:
first_element = val[0]
if isinstance(first_element, (int, float)):
simtime = first_element
return val[1:]
else:
print(f"Error: First element in initval string '{simtime_val_str}' is not a number. Using data part as is or empty.")
return val[1:] if len(val) > 1 else []
else:
print(f"Error: initval string '{simtime_val_str}' is not a list or is empty. Returning empty list.")
return []
except Exception as e:
print(f"Error parsing simtime_val_str '{simtime_val_str}': {e}. Returning empty list.")
return []