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
191 lines (178 loc) · 6.93 KB
/
Copy pathconcoredocker.java
File metadata and controls
191 lines (178 loc) · 6.93 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
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;
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 = "";
private static int delay = 1;
private static int retrycount = 0;
private static String inpath = "/in";
private static String outpath = "/out";
private static Map<String, Object> params = new HashMap<>();
private static int simtime = 0;
private static int 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")));
if (sparams.charAt(0) == '"') { // windows keeps "" need to remove
sparams = sparams.substring(1);
sparams = sparams.substring(0, sparams.indexOf('"'));
}
if (!sparams.equals("{")) {
System.out.println("converting sparams: " + sparams);
sparams = "{'" + sparams.replaceAll(",", ",'").replaceAll("=", "':").replaceAll(" ", "") + "}";
System.out.println("converted sparams: " + sparams);
}
try {
// literalEval returns a proper Map for "{...}"
params = (Map<String, Object>) literalEval(sparams);
} catch (Exception e) {
System.out.println("bad params: " + sparams);
}
} catch (IOException e) {
params = new HashMap<>();
}
defaultMaxTime(100);
}
@SuppressWarnings("unchecked")
private static Map<String, Object> parseFile(String filename) throws IOException {
String content = new String(Files.readAllBytes(Paths.get(filename)));
return (Map<String, Object>) literalEval(content); // Casted to Map
}
private static void defaultMaxTime(int defaultValue) {
try {
String content = new String(Files.readAllBytes(Paths.get(inpath + "1/concore.maxtime")));
maxtime = ((Number) literalEval(content)).intValue();
} catch (IOException | ClassCastException | NumberFormatException 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;
}
}
private static Object read(int port, String name, String initstr) {
try {
String ins = new String(Files.readAllBytes(Paths.get(inpath + port + "/" + name)));
while (ins.length() == 0) {
Thread.sleep(delay);
ins = new String(Files.readAllBytes(Paths.get(inpath + port + "/" + name)));
retrycount++;
}
s += ins;
List<?> inval = (List<?>) literalEval(ins);
simtime = Math.max(simtime, ((Number) inval.get(0)).intValue());
Object[] val = new Object[inval.size() - 1];
for (int i = 1; i < inval.size(); i++) {
val[i - 1] = inval.get(i);
}
return val;
} catch (IOException | InterruptedException | ClassCastException e) {
return initstr;
}
}
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);
} else if (!(val instanceof Object[])) {
System.out.println("write must have list or str");
return;
}
if (val instanceof Object[]) {
Object[] arrayVal = (Object[]) val;
content.append("[")
.append(simtime + delta)
.append(",")
.append(arrayVal[0]);
for (int i = 1; i < arrayVal.length; i++) {
content.append(",")
.append(arrayVal[i]);
}
content.append("]");
simtime += delta;
} else {
content.append(val);
}
Files.write(Paths.get(path), content.toString().getBytes());
} catch (IOException | InterruptedException e) {
System.out.println("skipping" + outpath + port + "/" + name);
}
}
private static Object[] initVal(String simtimeVal) {
Object[] val = new Object[] {};
try {
List<?> inval = (List<?>) literalEval(simtimeVal);
simtime = ((Number) inval.get(0)).intValue();
val = new Object[inval.size() - 1];
for (int i = 1; i < inval.size(); i++) {
val[i - 1] = inval.get(i);
}
} catch (Exception e) {
e.printStackTrace();
}
return val;
}
// custom parser
private static Object literalEval(String s) {
s = s.trim();
if (s.startsWith("{") && s.endsWith("}")) {
Map<String, Object> map = new HashMap<>();
String content = s.substring(1, s.length() - 1);
if (content.isEmpty()) return map;
for (String pair : content.split(",")) {
String[] kv = pair.split(":");
if (kv.length == 2) map.put((String) parseVal(kv[0]), parseVal(kv[1]));
}
return map;
} else if (s.startsWith("[") && s.endsWith("]")) {
List<Object> list = new ArrayList<>();
String content = s.substring(1, s.length() - 1);
if (content.isEmpty()) return list;
for (String val : content.split(",")) {
list.add(parseVal(val));
}
return list;
}
return parseVal(s);
}
// helper: Converts Python types to Java primitives
private static Object parseVal(String s) {
s = s.trim().replace("'", "").replace("\"", "");
if (s.equalsIgnoreCase("True")) return true;
if (s.equalsIgnoreCase("False")) return false;
if (s.equalsIgnoreCase("None")) return null;
try { return Integer.parseInt(s); } catch (NumberFormatException e1) {
try { return Double.parseDouble(s); } catch (NumberFormatException e2) { return s; }
}
}
}