-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathconcore.java
More file actions
923 lines (870 loc) · 35.5 KB
/
concore.java
File metadata and controls
923 lines (870 loc) · 35.5 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
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.HashMap;
import java.util.Map;
import java.util.ArrayList;
import java.util.List;
import org.zeromq.ZMQ;
/**
* Java implementation of concore local communication.
*
* This class provides file-based inter-process communication for control systems,
* mirroring the functionality of concore.py.
*/
public class concore {
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<>();
private static Map<String, ZeroMQPort> zmqPorts = new HashMap<>();
private static ZMQ.Context zmqContext = null;
// simtime as double to preserve fractional values (e.g. "[0.0, ...]")
private static double simtime = 0;
private static double maxtime;
private static final Path BASE_DIR = Paths.get("").toAbsolutePath().normalize();
private static final Path PID_REGISTRY_FILE = BASE_DIR.resolve("concorekill_pids.txt");
private static final Path KILL_SCRIPT_FILE = BASE_DIR.resolve("concorekill.bat");
// initialize on class load, same as Python module-level init
static {
if (isWindows()) {
registerPid();
writeKillScript();
Runtime.getRuntime().addShutdownHook(new Thread(concore::cleanupPid));
}
try {
iport = parseFile("concore.iport");
} catch (IOException e) {
}
try {
oport = parseFile("concore.oport");
} catch (IOException e) {
}
try {
String paramsFile = Paths.get(portPath(inpath, 1), "concore.params").toString();
String sparams = new String(Files.readAllBytes(Paths.get(paramsFile)), 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('"'));
}
params = parseParams(sparams);
} catch (IOException e) {
params = new HashMap<>();
}
defaultMaxTime(100);
Runtime.getRuntime().addShutdownHook(new Thread(concore::terminateZmq));
}
private static boolean isWindows() {
String os = System.getProperty("os.name");
return os != null && os.toLowerCase().contains("win");
}
private static void registerPid() {
try {
String pid = String.valueOf(ProcessHandle.current().pid());
try (FileChannel channel = FileChannel.open(PID_REGISTRY_FILE,
StandardOpenOption.CREATE, StandardOpenOption.WRITE, StandardOpenOption.APPEND)) {
try (FileLock lock = channel.lock()) {
channel.write(ByteBuffer.wrap((pid + System.lineSeparator()).getBytes(java.nio.charset.StandardCharsets.UTF_8)));
}
}
} catch (IOException e) {
}
}
private static void cleanupPid() {
String pid = String.valueOf(ProcessHandle.current().pid());
if (!Files.exists(PID_REGISTRY_FILE)) return;
List<String> remaining = new ArrayList<>();
try (FileChannel channel = FileChannel.open(PID_REGISTRY_FILE,
StandardOpenOption.READ, StandardOpenOption.WRITE)) {
try (FileLock lock = channel.lock()) {
ByteBuffer buf = ByteBuffer.allocate((int) channel.size());
channel.read(buf);
buf.flip();
String content = java.nio.charset.StandardCharsets.UTF_8.decode(buf).toString();
for (String line : content.split("\\R")) {
String trimmed = line.trim();
if (!trimmed.isEmpty() && !trimmed.equals(pid)) {
remaining.add(trimmed);
}
}
channel.truncate(0);
channel.position(0);
if (!remaining.isEmpty()) {
StringBuilder sb = new StringBuilder();
for (String r : remaining) sb.append(r).append(System.lineSeparator());
channel.write(ByteBuffer.wrap(sb.toString().getBytes(java.nio.charset.StandardCharsets.UTF_8)));
}
}
} catch (IOException e) {
}
if (remaining.isEmpty()) {
try { Files.deleteIfExists(PID_REGISTRY_FILE); } catch (IOException e) {}
try { Files.deleteIfExists(KILL_SCRIPT_FILE); } catch (IOException e) {}
}
}
private static void writeKillScript() {
try {
String regName = PID_REGISTRY_FILE.getFileName().toString();
String batName = KILL_SCRIPT_FILE.getFileName().toString();
String script = "@echo off\r\n";
script += "if not exist \"%~dp0" + regName + "\" (\r\n";
script += " echo No PID registry found. Nothing to kill.\r\n";
script += " exit /b 0\r\n";
script += ")\r\n";
script += "for /f \"usebackq tokens=*\" %%p in (\"%~dp0" + regName + "\") do (\r\n";
script += " wmic process where \"ProcessId=%%p\" get CommandLine /value 2>nul | find /i \"concore\" >nul\r\n";
script += " if not errorlevel 1 (\r\n";
script += " echo Killing concore process %%p\r\n";
script += " taskkill /F /PID %%p >nul 2>&1\r\n";
script += " ) else (\r\n";
script += " echo Skipping PID %%p - not a concore process or not running\r\n";
script += " )\r\n";
script += ")\r\n";
script += "del /q \"%~dp0" + regName + "\" 2>nul\r\n";
script += "del /q \"%~dp0" + batName + "\" 2>nul\r\n";
Files.write(KILL_SCRIPT_FILE, script.getBytes(java.nio.charset.StandardCharsets.UTF_8));
} catch (IOException e) {
}
}
/**
* Parses a param string into a map, matching concore_base.parse_params.
* Tries dict literal first, then falls back to semicolon-separated key=value pairs.
*/
private static Map<String, Object> parseParams(String sparams) {
Map<String, Object> result = new HashMap<>();
if (sparams == null || sparams.isEmpty()) return result;
String trimmed = sparams.trim();
if (trimmed.startsWith("{") && trimmed.endsWith("}")) {
try {
Object val = literalEval(trimmed);
if (val instanceof Map) {
@SuppressWarnings("unchecked")
Map<String, Object> map = (Map<String, Object>) val;
return map;
}
} catch (Exception e) {
}
}
for (String item : trimmed.split(";")) {
if (item.contains("=")) {
String[] parts = item.split("=", 2); // split on first '=' only
String key = parts[0].trim();
String value = parts[1].trim();
try {
result.put(key, literalEval(value));
} catch (Exception e) {
result.put(key, value);
}
}
}
return result;
}
/**
* 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.
*/
public static void defaultMaxTime(double defaultValue) {
try {
String maxtimeFile = Paths.get(portPath(inpath, 1), "concore.maxtime").toString();
String content = new String(Files.readAllBytes(Paths.get(maxtimeFile)));
Object parsed = literalEval(content.trim());
if (parsed instanceof Number) {
maxtime = ((Number) parsed).doubleValue();
} else {
maxtime = defaultValue;
}
} catch (IOException | RuntimeException e) {
maxtime = defaultValue;
}
}
private static String portPath(String base, int portNum) {
return base + portNum;
}
// package-level helpers for testing with temp directories
static void setInPath(String path) { inpath = path; }
static void setOutPath(String path) { outpath = path; }
static void setDelay(int ms) { delay = ms; }
static double getSimtime() { return simtime; }
static void resetState() { s = ""; olds = ""; simtime = 0; }
public static boolean unchanged() {
if (olds.equals(s)) {
s = "";
return true;
}
olds = s;
return false;
}
public 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).
*/
public static ReadResult 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 = Paths.get(portPath(inpath, port), name).toString();
try {
Thread.sleep(delay);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
s += initstr;
return new ReadResult(ReadStatus.TIMEOUT, 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 new ReadResult(ReadStatus.FILE_NOT_FOUND, defaultVal);
}
int attempts = 0;
while (ins.length() == 0 && attempts < maxRetries) {
try {
Thread.sleep(delay);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
s += initstr;
return new ReadResult(ReadStatus.TIMEOUT, 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 new ReadResult(ReadStatus.RETRIES_EXCEEDED, 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 ReadResult(ReadStatus.SUCCESS, new ArrayList<>(inval.subList(1, inval.size())));
}
} catch (Exception e) {
System.out.println("Error parsing " + ins + ": " + e.getMessage());
}
return new ReadResult(ReadStatus.PARSE_ERROR, 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();
}
/**
* Escapes a Java string so it can be safely embedded in a JSON double-quoted string.
* Escapes backslash, double quote, newline, carriage return, and tab.
*/
private static String escapeJsonString(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 JSON string representation.
* true/false/null instead of True/False/None; strings double-quoted.
*/
private static String toJsonLiteral(Object obj) {
if (obj == null) return "null";
if (obj instanceof Boolean) return ((Boolean) obj) ? "true" : "false";
if (obj instanceof String) return "\"" + escapeJsonString((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(toJsonLiteral(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(toJsonLiteral(entry.getKey())).append(": ").append(toJsonLiteral(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).
*/
public static void write(int port, String name, Object val, int delta) {
try {
String path = Paths.get(portPath(outpath, port), name).toString();
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 must not be mutated here.
// Mutation breaks cross-language determinism.
} 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 must not be mutated here.
// Mutation breaks cross-language determinism.
} 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.
*/
public 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;
}
private static ZMQ.Context getZmqContext() {
if (zmqContext == null) {
zmqContext = ZMQ.context(1);
}
return zmqContext;
}
public static void initZmqPort(String portName, String portType, String address, String socketTypeStr) {
if (zmqPorts.containsKey(portName)) return;
int sockType = zmqSocketTypeFromString(socketTypeStr);
if (sockType == -1) {
System.err.println("initZmqPort: unknown socket type '" + socketTypeStr + "'");
return;
}
zmqPorts.put(portName, new ZeroMQPort(portType, address, sockType));
}
public static void terminateZmq() {
for (Map.Entry<String, ZeroMQPort> entry : zmqPorts.entrySet()) {
entry.getValue().socket.close();
}
zmqPorts.clear();
if (zmqContext != null) {
zmqContext.term();
zmqContext = null;
}
}
private static int zmqSocketTypeFromString(String s) {
switch (s.toUpperCase()) {
case "REQ": return ZMQ.REQ;
case "REP": return ZMQ.REP;
case "PUB": return ZMQ.PUB;
case "SUB": return ZMQ.SUB;
case "PUSH": return ZMQ.PUSH;
case "PULL": return ZMQ.PULL;
case "PAIR": return ZMQ.PAIR;
default: return -1;
}
}
/**
* Reads data from a ZMQ port. Same wire format as file-based read:
* expects [simtime, val1, val2, ...], strips simtime, returns the rest.
*/
public static ReadResult read(String portName, String name, String initstr) {
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) {
}
ZeroMQPort port = zmqPorts.get(portName);
if (port == null) {
System.err.println("read: ZMQ port '" + portName + "' not initialized");
return new ReadResult(ReadStatus.FILE_NOT_FOUND, defaultVal);
}
String msg = port.recvWithRetry();
if (msg == null) {
System.err.println("read: ZMQ recv timeout on port '" + portName + "'");
return new ReadResult(ReadStatus.TIMEOUT, defaultVal);
}
s += msg;
try {
List<?> inval = (List<?>) literalEval(msg);
if (!inval.isEmpty()) {
simtime = Math.max(simtime, ((Number) inval.get(0)).doubleValue());
return new ReadResult(ReadStatus.SUCCESS, new ArrayList<>(inval.subList(1, inval.size())));
}
} catch (Exception e) {
System.out.println("Error parsing ZMQ message '" + msg + "': " + e.getMessage());
}
return new ReadResult(ReadStatus.PARSE_ERROR, defaultVal);
}
/**
* Writes data to a ZMQ port. Prepends [simtime+delta] to match file-based write behavior.
*/
public static void write(String portName, String name, Object val, int delta) {
ZeroMQPort port = zmqPorts.get(portName);
if (port == null) {
System.err.println("write: ZMQ port '" + portName + "' not initialized");
return;
}
String payload;
if (val instanceof List) {
List<?> listVal = (List<?>) val;
StringBuilder sb = new StringBuilder("[");
sb.append(toJsonLiteral(simtime + delta));
for (Object o : listVal) {
sb.append(", ");
sb.append(toJsonLiteral(o));
}
sb.append("]");
payload = sb.toString();
// simtime must not be mutated here
} else if (val instanceof String) {
payload = (String) val;
} else {
System.out.println("write must have list or str");
return;
}
port.sendWithRetry(payload);
}
/**
* 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;
}
public enum ReadStatus {
SUCCESS, FILE_NOT_FOUND, TIMEOUT, PARSE_ERROR, RETRIES_EXCEEDED
}
public static class ReadResult {
public final ReadStatus status;
public final List<Object> data;
ReadResult(ReadStatus status, List<Object> data) {
this.status = status;
this.data = data;
}
}
/**
* ZMQ socket wrapper with bind/connect, timeouts, and retry.
*/
private static class ZeroMQPort {
final ZMQ.Socket socket;
final String address;
ZeroMQPort(String portType, String address, int socketType) {
this.address = address;
ZMQ.Context ctx = getZmqContext();
this.socket = ctx.socket(socketType);
this.socket.setReceiveTimeOut(2000);
this.socket.setSendTimeOut(2000);
this.socket.setLinger(0);
if (portType.equals("bind")) {
this.socket.bind(address);
} else {
this.socket.connect(address);
}
}
String recvWithRetry() {
for (int attempt = 0; attempt < 5; attempt++) {
String msg = socket.recvStr();
if (msg != null) return msg;
try { Thread.sleep(500); } catch (InterruptedException e) { Thread.currentThread().interrupt(); break; }
}
return null;
}
void sendWithRetry(String message) {
for (int attempt = 0; attempt < 5; attempt++) {
if (socket.send(message)) return;
try { Thread.sleep(500); } catch (InterruptedException e) { Thread.currentThread().interrupt(); break; }
}
}
}
/**
* 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": case "true": return Boolean.TRUE;
case "False": case "false": return Boolean.FALSE;
case "None": case "null": return null;
default: throw new IllegalArgumentException("Unknown keyword: '" + word + "' at position " + start);
}
}
}
}