forked from ControlCore-Project/concore
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconcoredocker.java
More file actions
599 lines (566 loc) · 22.4 KB
/
Copy pathconcoredocker.java
File metadata and controls
599 lines (566 loc) · 22.4 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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;
import java.util.ArrayList;
import java.util.List;
/**
* Java implementation of concore Docker communication.
*
* This class provides file-based inter-process communication for control systems,
* mirroring the functionality of concoredocker.py.
*/
public class concoredocker {
private static Map<String, Object> iport = new HashMap<>();
private static Map<String, Object> oport = new HashMap<>();
private static String s = "";
private static String olds = "";
// delay in milliseconds (Python uses time.sleep(1) = 1 second)
private static int delay = 1000;
private static int retrycount = 0;
private static int maxRetries = 5;
private static String inpath = "/in";
private static String outpath = "/out";
private static Map<String, Object> params = new HashMap<>();
// simtime as double to preserve fractional values (e.g. "[0.0, ...]")
private static double simtime = 0;
private static double maxtime;
public static void main(String[] args) {
try {
iport = parseFile("concore.iport");
} catch (IOException e) {
e.printStackTrace();
}
try {
oport = parseFile("concore.oport");
} catch (IOException e) {
e.printStackTrace();
}
try {
String sparams = new String(Files.readAllBytes(Paths.get(inpath + "1/concore.params")), java.nio.charset.StandardCharsets.UTF_8);
if (sparams.length() > 0 && sparams.charAt(0) == '"') { // windows keeps "" need to remove
sparams = sparams.substring(1);
sparams = sparams.substring(0, sparams.indexOf('"'));
}
// Try parsing as dict literal first (matches Python parse_params logic)
sparams = sparams.trim();
if (sparams.startsWith("{") && sparams.endsWith("}")) {
try {
Object parsed = literalEval(sparams);
if (parsed instanceof Map) {
@SuppressWarnings("unchecked")
Map<String, Object> parsedMap = (Map<String, Object>) parsed;
params = parsedMap;
}
} catch (Exception e) {
System.out.println("bad params: " + sparams);
}
} else if (!sparams.isEmpty()) {
// Fallback: convert key=value,key=value format to dict
System.out.println("converting sparams: " + sparams);
sparams = "{'" + sparams.replaceAll(";", ",'").replaceAll("=", "':").replaceAll(" ", "") + "}";
System.out.println("converted sparams: " + sparams);
try {
Object parsed = literalEval(sparams);
if (parsed instanceof Map) {
@SuppressWarnings("unchecked")
Map<String, Object> parsedMap = (Map<String, Object>) parsed;
params = parsedMap;
}
} catch (Exception e) {
System.out.println("bad params: " + sparams);
}
}
} catch (IOException e) {
params = new HashMap<>();
}
defaultMaxTime(100);
}
/**
* Parses a file containing a Python-style dictionary literal.
* Returns empty map if file is empty or malformed (matches Python safe_literal_eval).
*/
private static Map<String, Object> parseFile(String filename) throws IOException {
String content = new String(Files.readAllBytes(Paths.get(filename)), java.nio.charset.StandardCharsets.UTF_8);
content = content.trim();
if (content.isEmpty()) {
return new HashMap<>();
}
try {
Object result = literalEval(content);
if (result instanceof Map) {
@SuppressWarnings("unchecked")
Map<String, Object> map = (Map<String, Object>) result;
return map;
}
} catch (IllegalArgumentException e) {
System.err.println("Failed to parse file as map: " + filename + " (" + e.getMessage() + ")");
}
return new HashMap<>();
}
/**
* Sets maxtime from concore.maxtime file, or uses defaultValue if file not found.
* Catches both IOException and RuntimeException to match Python safe_literal_eval.
*/
private static void defaultMaxTime(double defaultValue) {
try {
String content = new String(Files.readAllBytes(Paths.get(inpath + "1/concore.maxtime")));
Object parsed = literalEval(content.trim());
if (parsed instanceof Number) {
maxtime = ((Number) parsed).doubleValue();
} else {
maxtime = defaultValue;
}
} catch (IOException | RuntimeException e) {
maxtime = defaultValue;
}
}
private static boolean unchanged() {
if (olds.equals(s)) {
s = "";
return true;
}
olds = s;
return false;
}
private static Object tryParam(String n, Object i) {
if (params.containsKey(n)) {
return params.get(n);
} else {
return i;
}
}
/**
* Reads data from a port file. Returns the values after extracting simtime.
* Input format: [simtime, val1, val2, ...]
* Returns: list of values after simtime
* Includes max retry limit to avoid infinite blocking (matches Python behavior).
*/
private static List<Object> read(int port, String name, String initstr) {
// Parse default value upfront for consistent return type
List<Object> defaultVal = new ArrayList<>();
try {
List<?> parsed = (List<?>) literalEval(initstr);
if (parsed.size() > 1) {
defaultVal = new ArrayList<>(parsed.subList(1, parsed.size()));
}
} catch (Exception e) {
// initstr not parseable as list; defaultVal stays empty
}
String filePath = inpath + port + "/" + name;
try {
Thread.sleep(delay);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
s += initstr;
return defaultVal;
}
String ins;
try {
ins = new String(Files.readAllBytes(Paths.get(filePath)));
} catch (IOException e) {
System.out.println("File " + filePath + " not found, using default value.");
s += initstr;
return defaultVal;
}
int attempts = 0;
while (ins.length() == 0 && attempts < maxRetries) {
try {
Thread.sleep(delay);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
s += initstr;
return defaultVal;
}
try {
ins = new String(Files.readAllBytes(Paths.get(filePath)));
} catch (IOException e) {
System.out.println("Retry " + (attempts + 1) + ": Error reading " + filePath);
}
attempts++;
retrycount++;
}
if (ins.length() == 0) {
System.out.println("Max retries reached for " + filePath + ", using default value.");
return defaultVal;
}
s += ins;
try {
List<?> inval = (List<?>) literalEval(ins);
if (!inval.isEmpty()) {
double firstSimtime = ((Number) inval.get(0)).doubleValue();
simtime = Math.max(simtime, firstSimtime);
return new ArrayList<>(inval.subList(1, inval.size()));
}
} catch (Exception e) {
System.out.println("Error parsing " + ins + ": " + e.getMessage());
}
return defaultVal;
}
/**
* Escapes a Java string so it can be safely used as a single-quoted Python string literal.
* At minimum, escapes backslash, single quote, newline, carriage return, and tab.
*/
private static String escapePythonString(String s) {
StringBuilder sb = new StringBuilder(s.length());
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
switch (c) {
case '\\': sb.append("\\\\"); break;
case '\'': sb.append("\\'"); break;
case '\n': sb.append("\\n"); break;
case '\r': sb.append("\\r"); break;
case '\t': sb.append("\\t"); break;
default: sb.append(c); break;
}
}
return sb.toString();
}
/**
* Converts a Java object to its Python-literal string representation.
* True/False/None instead of true/false/null; strings single-quoted.
*/
private static String toPythonLiteral(Object obj) {
if (obj == null) return "None";
if (obj instanceof Boolean) return ((Boolean) obj) ? "True" : "False";
if (obj instanceof String) return "'" + escapePythonString((String) obj) + "'";
if (obj instanceof Number) return obj.toString();
if (obj instanceof List) {
List<?> list = (List<?>) obj;
StringBuilder sb = new StringBuilder("[");
for (int i = 0; i < list.size(); i++) {
if (i > 0) sb.append(", ");
sb.append(toPythonLiteral(list.get(i)));
}
sb.append("]");
return sb.toString();
}
if (obj instanceof Map) {
Map<?, ?> map = (Map<?, ?>) obj;
StringBuilder sb = new StringBuilder("{");
boolean first = true;
for (Map.Entry<?, ?> entry : map.entrySet()) {
if (!first) sb.append(", ");
sb.append(toPythonLiteral(entry.getKey())).append(": ").append(toPythonLiteral(entry.getValue()));
first = false;
}
sb.append("}");
return sb.toString();
}
return obj.toString();
}
/**
* Writes data to a port file.
* Prepends simtime+delta to the value list, then serializes to Python-literal format.
* Accepts List or String values (matching Python implementation).
*/
private static void write(int port, String name, Object val, int delta) {
try {
String path = outpath + port + "/" + name;
StringBuilder content = new StringBuilder();
if (val instanceof String) {
Thread.sleep(2 * delay);
content.append(val);
} else if (val instanceof List) {
List<?> listVal = (List<?>) val;
content.append("[");
content.append(toPythonLiteral(simtime + delta));
for (int i = 0; i < listVal.size(); i++) {
content.append(", ");
content.append(toPythonLiteral(listVal.get(i)));
}
content.append("]");
simtime += delta;
} else if (val instanceof Object[]) {
// Legacy support for Object[] arguments
Object[] arrayVal = (Object[]) val;
content.append("[");
content.append(toPythonLiteral(simtime + delta));
for (Object o : arrayVal) {
content.append(", ");
content.append(toPythonLiteral(o));
}
content.append("]");
simtime += delta;
} else {
System.out.println("write must have list or str");
return;
}
Files.write(Paths.get(path), content.toString().getBytes());
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
System.out.println("skipping " + outpath + port + "/" + name);
} catch (IOException e) {
System.out.println("skipping " + outpath + port + "/" + name);
}
}
/**
* Parses an initial value string like "[0.0, 1.0, 2.0]".
* Extracts simtime from position 0 and returns the remaining values as a List.
*/
private static List<Object> initVal(String simtimeVal) {
List<Object> val = new ArrayList<>();
try {
List<?> inval = (List<?>) literalEval(simtimeVal);
if (!inval.isEmpty()) {
simtime = ((Number) inval.get(0)).doubleValue();
val = new ArrayList<>(inval.subList(1, inval.size()));
}
} catch (Exception e) {
System.out.println("Error parsing initVal: " + e.getMessage());
}
return val;
}
/**
* Parses a Python-literal string into Java objects using a recursive descent parser.
* Supports: dict, list, int, float, string (single/double quoted), bool, None, nested structures.
* This replaces the broken split-based parser that could not handle quoted commas or nesting.
*/
static Object literalEval(String s) {
if (s == null) throw new IllegalArgumentException("Input cannot be null");
s = s.trim();
if (s.isEmpty()) throw new IllegalArgumentException("Input cannot be empty");
Parser parser = new Parser(s);
Object result = parser.parseExpression();
parser.skipWhitespace();
if (parser.pos < parser.input.length()) {
throw new IllegalArgumentException("Unexpected trailing content at position " + parser.pos);
}
return result;
}
/**
* Recursive descent parser for Python literal expressions.
* Handles: dicts, lists, tuples, strings, numbers, booleans, None.
*/
private static class Parser {
final String input;
int pos;
Parser(String input) {
this.input = input;
this.pos = 0;
}
void skipWhitespace() {
while (pos < input.length() && Character.isWhitespace(input.charAt(pos))) {
pos++;
}
}
char peek() {
skipWhitespace();
if (pos >= input.length()) throw new IllegalArgumentException("Unexpected end of input");
return input.charAt(pos);
}
char advance() {
char c = input.charAt(pos);
pos++;
return c;
}
boolean hasMore() {
skipWhitespace();
return pos < input.length();
}
Object parseExpression() {
skipWhitespace();
if (pos >= input.length()) throw new IllegalArgumentException("Unexpected end of input");
char c = input.charAt(pos);
if (c == '{') return parseDict();
if (c == '[') return parseList();
if (c == '(') return parseTuple();
if (c == '\'' || c == '"') return parseString();
if (c == '-' || c == '+' || Character.isDigit(c)) return parseNumber();
return parseKeyword();
}
Map<String, Object> parseDict() {
Map<String, Object> map = new HashMap<>();
pos++; // skip '{'
skipWhitespace();
if (hasMore() && input.charAt(pos) == '}') {
pos++;
return map;
}
while (true) {
skipWhitespace();
Object key = parseExpression();
skipWhitespace();
if (pos >= input.length() || input.charAt(pos) != ':') {
throw new IllegalArgumentException("Expected ':' in dict at position " + pos);
}
pos++; // skip ':'
skipWhitespace();
Object value = parseExpression();
if (!(key instanceof String)) {
throw new IllegalArgumentException(
"Dict keys must be non-null strings, but got: "
+ (key == null ? "null" : key.getClass().getSimpleName()));
}
map.put((String) key, value);
skipWhitespace();
if (pos >= input.length()) {
throw new IllegalArgumentException("Unterminated dict: missing '}'");
}
if (input.charAt(pos) == '}') {
pos++;
break;
}
if (input.charAt(pos) == ',') {
pos++;
skipWhitespace();
// trailing comma before close
if (hasMore() && input.charAt(pos) == '}') {
pos++;
break;
}
} else {
throw new IllegalArgumentException("Expected ',' or '}' in dict at position " + pos);
}
}
return map;
}
List<Object> parseList() {
List<Object> list = new ArrayList<>();
pos++; // skip '['
skipWhitespace();
if (hasMore() && input.charAt(pos) == ']') {
pos++;
return list;
}
while (true) {
skipWhitespace();
list.add(parseExpression());
skipWhitespace();
if (pos >= input.length()) {
throw new IllegalArgumentException("Unterminated list: missing ']'");
}
if (input.charAt(pos) == ']') {
pos++;
break;
}
if (input.charAt(pos) == ',') {
pos++;
skipWhitespace();
// trailing comma before close
if (hasMore() && input.charAt(pos) == ']') {
pos++;
break;
}
} else {
throw new IllegalArgumentException("Expected ',' or ']' in list at position " + pos);
}
}
return list;
}
List<Object> parseTuple() {
List<Object> list = new ArrayList<>();
pos++; // skip '('
skipWhitespace();
if (hasMore() && input.charAt(pos) == ')') {
pos++;
return list;
}
while (true) {
skipWhitespace();
list.add(parseExpression());
skipWhitespace();
if (pos >= input.length()) {
throw new IllegalArgumentException("Unterminated tuple: missing ')'");
}
if (input.charAt(pos) == ')') {
pos++;
break;
}
if (input.charAt(pos) == ',') {
pos++;
skipWhitespace();
// trailing comma before close
if (hasMore() && input.charAt(pos) == ')') {
pos++;
break;
}
} else {
throw new IllegalArgumentException("Expected ',' or ')' in tuple at position " + pos);
}
}
return list;
}
String parseString() {
char quote = advance(); // opening quote
StringBuilder sb = new StringBuilder();
while (pos < input.length()) {
char c = input.charAt(pos);
if (c == '\\' && pos + 1 < input.length()) {
pos++;
char escaped = input.charAt(pos);
switch (escaped) {
case 'n': sb.append('\n'); break;
case 't': sb.append('\t'); break;
case 'r': sb.append('\r'); break;
case '\\': sb.append('\\'); break;
case '\'': sb.append('\''); break;
case '"': sb.append('"'); break;
default: sb.append('\\').append(escaped); break;
}
pos++;
} else if (c == quote) {
pos++;
return sb.toString();
} else {
sb.append(c);
pos++;
}
}
throw new IllegalArgumentException("Unterminated string starting at position " + (pos - sb.length() - 1));
}
Number parseNumber() {
int start = pos;
if (pos < input.length() && (input.charAt(pos) == '-' || input.charAt(pos) == '+')) {
pos++;
}
boolean hasDecimal = false;
boolean hasExponent = false;
while (pos < input.length()) {
char c = input.charAt(pos);
if (Character.isDigit(c)) {
pos++;
} else if (c == '.' && !hasDecimal && !hasExponent) {
hasDecimal = true;
pos++;
} else if ((c == 'e' || c == 'E') && !hasExponent) {
hasExponent = true;
pos++;
if (pos < input.length() && (input.charAt(pos) == '+' || input.charAt(pos) == '-')) {
pos++;
}
} else {
break;
}
}
String numStr = input.substring(start, pos);
try {
if (hasDecimal || hasExponent) {
return Double.parseDouble(numStr);
} else {
try {
return Integer.parseInt(numStr);
} catch (NumberFormatException e) {
return Long.parseLong(numStr);
}
}
} catch (NumberFormatException e) {
throw new IllegalArgumentException("Invalid number: '" + numStr + "' at position " + start);
}
}
Object parseKeyword() {
int start = pos;
while (pos < input.length() && Character.isLetterOrDigit(input.charAt(pos)) || (pos < input.length() && input.charAt(pos) == '_')) {
pos++;
}
String word = input.substring(start, pos);
switch (word) {
case "True": return Boolean.TRUE;
case "False": return Boolean.FALSE;
case "None": return null;
default: throw new IllegalArgumentException("Unknown keyword: '" + word + "' at position " + start);
}
}
}
}