|
| 1 | +import java.io.IOException; |
| 2 | +import java.nio.file.Files; |
| 3 | +import java.nio.file.Paths; |
| 4 | +import java.util.HashMap; |
| 5 | +import java.util.Map; |
| 6 | + |
| 7 | +public class concoredocker { |
| 8 | + private static Map<String, Object> iport = new HashMap<>(); |
| 9 | + private static Map<String, Object> oport = new HashMap<>(); |
| 10 | + private static String s = ""; |
| 11 | + private static String olds = ""; |
| 12 | + private static int delay = 1; |
| 13 | + private static int retrycount = 0; |
| 14 | + private static String inpath = "/in"; |
| 15 | + private static String outpath = "/out"; |
| 16 | + private static Map<String, Object> params = new HashMap<>(); |
| 17 | + private static int maxtime; |
| 18 | + |
| 19 | + public static void main(String[] args) { |
| 20 | + try { |
| 21 | + iport = parseFile("concore.iport"); |
| 22 | + } catch (IOException e) { |
| 23 | + e.printStackTrace(); |
| 24 | + } |
| 25 | + try { |
| 26 | + oport = parseFile("concore.oport"); |
| 27 | + } catch (IOException e) { |
| 28 | + e.printStackTrace(); |
| 29 | + } |
| 30 | + |
| 31 | + try { |
| 32 | + String sparams = new String(Files.readAllBytes(Paths.get(inpath + "1/concore.params"))); |
| 33 | + if (sparams.charAt(0) == '"') { // windows keeps "" need to remove |
| 34 | + sparams = sparams.substring(1); |
| 35 | + sparams = sparams.substring(0, sparams.indexOf('"')); |
| 36 | + } |
| 37 | + if (!sparams.equals("{")) { |
| 38 | + System.out.println("converting sparams: " + sparams); |
| 39 | + sparams = "{'" + sparams.replaceAll(",", ",'").replaceAll("=", "':").replaceAll(" ", "") + "}"; |
| 40 | + System.out.println("converted sparams: " + sparams); |
| 41 | + } |
| 42 | + try { |
| 43 | + params = literalEval(sparams); |
| 44 | + } catch (Exception e) { |
| 45 | + System.out.println("bad params: " + sparams); |
| 46 | + } |
| 47 | + } catch (IOException e) { |
| 48 | + params = new HashMap<>(); |
| 49 | + } |
| 50 | + |
| 51 | + defaultMaxTime(100); |
| 52 | + } |
| 53 | + |
| 54 | + private static Map<String, Object> parseFile(String filename) throws IOException { |
| 55 | + String content = new String(Files.readAllBytes(Paths.get(filename))); |
| 56 | + return literalEval(content); |
| 57 | + } |
| 58 | + |
| 59 | + private static void defaultMaxTime(int defaultValue) { |
| 60 | + try { |
| 61 | + String content = new String(Files.readAllBytes(Paths.get(inpath + "1/concore.maxtime"))); |
| 62 | + maxtime = literalEval(content).size(); |
| 63 | + } catch (IOException e) { |
| 64 | + maxtime = defaultValue; |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + private static void unchanged() { |
| 69 | + if (olds.equals(s)) { |
| 70 | + s = ""; |
| 71 | + } else { |
| 72 | + olds = s; |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + private static Object tryParam(String n, Object i) { |
| 77 | + if (params.containsKey(n)) { |
| 78 | + return params.get(n); |
| 79 | + } else { |
| 80 | + return i; |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + private static Object read(int port, String name, String initstr) { |
| 85 | + try { |
| 86 | + String ins = new String(Files.readAllBytes(Paths.get(inpath + port + "/" + name))); |
| 87 | + while (ins.length() == 0) { |
| 88 | + Thread.sleep(delay); |
| 89 | + ins = new String(Files.readAllBytes(Paths.get(inpath + port + "/" + name))); |
| 90 | + retrycount++; |
| 91 | + } |
| 92 | + s += ins; |
| 93 | + Object[] inval = new Map[] { literalEval(ins) }; |
| 94 | + int simtime = Math.max((int) inval[0], 0); // assuming simtime is an integer |
| 95 | + return inval[1]; |
| 96 | + } catch (IOException | InterruptedException e) { |
| 97 | + return initstr; |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + private static void write(int port, String name, Object val, int delta) { |
| 102 | + try { |
| 103 | + String path = outpath + port + "/" + name; |
| 104 | + StringBuilder content = new StringBuilder(); |
| 105 | + if (val instanceof String) { |
| 106 | + Thread.sleep(2 * delay); |
| 107 | + } else if (!(val instanceof Object[])) { |
| 108 | + System.out.println("mywrite must have list or str"); |
| 109 | + System.exit(1); |
| 110 | + } |
| 111 | + if (val instanceof Object[]) { |
| 112 | + Object[] arrayVal = (Object[]) val; |
| 113 | + content.append("[") |
| 114 | + .append(maxtime + delta) |
| 115 | + .append(",") |
| 116 | + .append(arrayVal[0]); |
| 117 | + for (int i = 1; i < arrayVal.length; i++) { |
| 118 | + content.append(",") |
| 119 | + .append(arrayVal[i]); |
| 120 | + } |
| 121 | + content.append("]"); |
| 122 | + } else { |
| 123 | + content.append(val); |
| 124 | + } |
| 125 | + Files.write(Paths.get(path), content.toString().getBytes()); |
| 126 | + } catch (IOException | InterruptedException e) { |
| 127 | + System.out.println("skipping" + outpath + port + "/" + name); |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + private static Object[] initVal(String simtimeVal) { |
| 132 | + int simtime = 0; |
| 133 | + Object[] val = new Object[] {}; |
| 134 | + try { |
| 135 | + Object[] arrayVal = new Map[] { literalEval(simtimeVal) }; |
| 136 | + simtime = (int) arrayVal[0]; // assuming simtime is an integer |
| 137 | + val = new Object[arrayVal.length - 1]; |
| 138 | + System.arraycopy(arrayVal, 1, val, 0, val.length); |
| 139 | + } catch (Exception e) { |
| 140 | + e.printStackTrace(); |
| 141 | + } |
| 142 | + return val; |
| 143 | + } |
| 144 | + |
| 145 | + private static Map<String, Object> literalEval(String s) { |
| 146 | + |
| 147 | + return new HashMap<>(); |
| 148 | + } |
| 149 | +} |
0 commit comments