diff --git a/concore.java b/concore.java new file mode 100644 index 0000000..75347dd --- /dev/null +++ b/concore.java @@ -0,0 +1,221 @@ +// concore.java -- this java file will be the equivalent of concore.py +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Map; + +public class concore { + static String s = ""; + static String olds = ""; + static String inpath = "./in"; // must be relative path for local + static String outpath = "./out"; + static int maxtime; + + public static int delay = 1; // second + public static int retrycount = 0; + public static double simtime; + public static Map iport = new HashMap<>(); + public static Map oport = new HashMap<>(); + + public concore(){ + iport = mapParser("concore.iport"); + oport = mapParser("concore.oport"); + + } + public static void main(String[] args) { + // If Windows, create script to kill this process + // because batch files don't provide an easy way to know the PID of the last command + // Ignored for POSIX!=Windows, because "concorepid" is handled by script + // Ignored for Docker (Linux!=Windows), because handled by docker stop + if (System.getProperty("os.name").toLowerCase().contains("windows")) { + try { + FileWriter fpid = new FileWriter("concorekill.bat"); + fpid.write("taskkill /F /PID " + ProcessHandle.current().pid() + "\n"); + fpid.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + default_maxtime(100); + } + + private static Map mapParser(String filename) { + Map config = new HashMap<>(); + StringBuilder portstr = new StringBuilder(); // StringBuilder to store file data + try (BufferedReader reader = new BufferedReader(new FileReader(filename))) { + int ch; + while ((ch = reader.read()) != -1) { + portstr.append((char) ch); // Add character to string builder + } + } catch (IOException e) { + e.printStackTrace(); + } + + portstr.setCharAt(portstr.length() - 1, ','); // Replace last character with comma + portstr.append("}"); + int i = 0; + String portname = ""; + String portnum = ""; + + while(portstr.charAt(i) != '}'){ + if(portstr.charAt(i) == '\''){ + i++; + while(portstr.charAt(i) != '\''){ + portname += portstr.charAt(i); + i++; + } + config.put(portname,0); + } + if(portstr.charAt(i) == ':'){ + i++; + while(portstr.charAt(i) != ','){ + portnum += portstr.charAt(i); + i++; + } + config.put(portname,Integer.parseInt(portnum)); + portname = ""; + portnum = ""; + } + i++; + } + return config; + } + + //function to compare and determine whether file content has been changed + public static boolean unchanged() { + if (olds.equals(s)) { + s = ""; + return true; + } else { + olds = s; + return false; + } + } + + private static ArrayList parser(String f){ + ArrayList temp = new ArrayList<>(); + String value = ""; + // Changing last bracket to comma to use comma as a delimiter + f = f.substring(0, f.length() - 1) + ","; + for(int i=1;i read(int port, String name, String initstr) { + String ins = ""; + try {Thread.sleep(1000 * delay);} + catch(InterruptedException e){ + e.printStackTrace(); + } + try (BufferedReader reader = new BufferedReader(new FileReader(inpath + String.valueOf(port) + "/" + name))) { + int ch; + while ((ch = reader.read()) != -1) { + ins += ((char) ch); // Add character to string + } + }catch(IOException e){ + ins = initstr; + e.printStackTrace(); + } + + + while(ins.length() == 0){ + try {Thread.sleep(1000 * delay);} + catch(InterruptedException e){ + e.printStackTrace(); + } + try (BufferedReader reader = new BufferedReader(new FileReader(inpath + String.valueOf(port) + "/" + name))) { + int ch; + while ((ch = reader.read()) != -1) { + ins += ((char) ch); // Add character to string + } + retrycount++; + } catch (IOException e) { + //observed retry count in java from various tests is not calculated yet + retrycount++; + System.out.println("Read error"); + e.printStackTrace(); + } + } + s += ins; + + ArrayList inval = new ArrayList<>(); + inval = parser(ins); + simtime = simtime > inval.get(0) ? simtime : inval.get(0); + + // returning a string with data exluding simtime + inval.remove(0); + return inval; + } + + //write method, accepts a vector double and writes it to the file + public static void write(int port, String name, ArrayList val, int delta) { + try (BufferedWriter writer = new BufferedWriter (new FileWriter(outpath + String.valueOf(port) + "/" + name))){ + val.add(0,simtime+delta); + writer.write('['); + for(int i=0;i initval(String simtimeVal) { + //parsing + ArrayList val = parser(simtimeVal); + + + //determining simtime + simtime = val.get(0); + + //returning the rest of the values(except simtime) in val + val.remove(0); + return val; + } +} \ No newline at end of file