1+ // concore.java -- this java file will be the equivalent of concore.py
2+ import java .io .BufferedReader ;
3+ import java .io .BufferedWriter ;
4+ import java .io .FileReader ;
5+ import java .io .FileWriter ;
6+ import java .io .IOException ;
7+ import java .util .ArrayList ;
8+ import java .util .HashMap ;
9+ import java .util .Map ;
10+
11+ public class concore {
12+ static String s = "" ;
13+ static String olds = "" ;
14+ static String inpath = "./in" ; // must be relative path for local
15+ static String outpath = "./out" ;
16+ static int maxtime ;
17+
18+ public static int delay = 1 ; // second
19+ public static int retrycount = 0 ;
20+ public static double simtime ;
21+ public static Map <String , Integer > iport = new HashMap <>();
22+ public static Map <String , Integer > oport = new HashMap <>();
23+
24+ public concore (){
25+ iport = mapParser ("concore.iport" );
26+ oport = mapParser ("concore.oport" );
27+
28+ }
29+ public static void main (String [] args ) {
30+ // If Windows, create script to kill this process
31+ // because batch files don't provide an easy way to know the PID of the last command
32+ // Ignored for POSIX!=Windows, because "concorepid" is handled by script
33+ // Ignored for Docker (Linux!=Windows), because handled by docker stop
34+ if (System .getProperty ("os.name" ).toLowerCase ().contains ("windows" )) {
35+ try {
36+ FileWriter fpid = new FileWriter ("concorekill.bat" );
37+ fpid .write ("taskkill /F /PID " + ProcessHandle .current ().pid () + "\n " );
38+ fpid .close ();
39+ } catch (IOException e ) {
40+ e .printStackTrace ();
41+ }
42+ }
43+
44+ default_maxtime (100 );
45+ }
46+
47+ private static Map <String , Integer > mapParser (String filename ) {
48+ Map <String , Integer > config = new HashMap <>();
49+ StringBuilder portstr = new StringBuilder (); // StringBuilder to store file data
50+ try (BufferedReader reader = new BufferedReader (new FileReader (filename ))) {
51+ int ch ;
52+ while ((ch = reader .read ()) != -1 ) {
53+ portstr .append ((char ) ch ); // Add character to string builder
54+ }
55+ } catch (IOException e ) {
56+ e .printStackTrace ();
57+ }
58+
59+ portstr .setCharAt (portstr .length () - 1 , ',' ); // Replace last character with comma
60+ portstr .append ("}" );
61+ int i = 0 ;
62+ String portname = "" ;
63+ String portnum = "" ;
64+
65+ while (portstr .charAt (i ) != '}' ){
66+ if (portstr .charAt (i ) == '\'' ){
67+ i ++;
68+ while (portstr .charAt (i ) != '\'' ){
69+ portname += portstr .charAt (i );
70+ i ++;
71+ }
72+ config .put (portname ,0 );
73+ }
74+ if (portstr .charAt (i ) == ':' ){
75+ i ++;
76+ while (portstr .charAt (i ) != ',' ){
77+ portnum += portstr .charAt (i );
78+ i ++;
79+ }
80+ config .put (portname ,Integer .parseInt (portnum ));
81+ portname = "" ;
82+ portnum = "" ;
83+ }
84+ i ++;
85+ }
86+ return config ;
87+ }
88+
89+ //function to compare and determine whether file content has been changed
90+ public static boolean unchanged () {
91+ if (olds .equals (s )) {
92+ s = "" ;
93+ return true ;
94+ } else {
95+ olds = s ;
96+ return false ;
97+ }
98+ }
99+
100+ private static ArrayList <Double > parser (String f ){
101+ ArrayList <Double > temp = new ArrayList <>();
102+ String value = "" ;
103+ // Changing last bracket to comma to use comma as a delimiter
104+ f = f .substring (0 , f .length () - 1 ) + "," ;
105+ for (int i =1 ;i <f .length ();i ++){
106+ if (f .charAt (i ) != ',' ){
107+ value += f .charAt (i );
108+ }
109+ else {
110+ if (value .length () != 0 )
111+ temp .add (Double .parseDouble (value ));
112+
113+ // reset value
114+ value = "" ;
115+ }
116+ }
117+ return temp ;
118+ }
119+
120+ //accepts the file name as string and returns a string of file content
121+ public static ArrayList <Double > read (int port , String name , String initstr ) {
122+ String ins = "" ;
123+ try {Thread .sleep (1000 * delay );}
124+ catch (InterruptedException e ){
125+ e .printStackTrace ();
126+ }
127+ try (BufferedReader reader = new BufferedReader (new FileReader (inpath + String .valueOf (port ) + "/" + name ))) {
128+ int ch ;
129+ while ((ch = reader .read ()) != -1 ) {
130+ ins += ((char ) ch ); // Add character to string
131+ }
132+ }catch (IOException e ){
133+ ins = initstr ;
134+ e .printStackTrace ();
135+ }
136+
137+
138+ while (ins .length () == 0 ){
139+ try {Thread .sleep (1000 * delay );}
140+ catch (InterruptedException e ){
141+ e .printStackTrace ();
142+ }
143+ try (BufferedReader reader = new BufferedReader (new FileReader (inpath + String .valueOf (port ) + "/" + name ))) {
144+ int ch ;
145+ while ((ch = reader .read ()) != -1 ) {
146+ ins += ((char ) ch ); // Add character to string
147+ }
148+ retrycount ++;
149+ } catch (IOException e ) {
150+ //observed retry count in java from various tests is not calculated yet
151+ retrycount ++;
152+ System .out .println ("Read error" );
153+ e .printStackTrace ();
154+ }
155+ }
156+ s += ins ;
157+
158+ ArrayList <Double > inval = new ArrayList <>();
159+ inval = parser (ins );
160+ simtime = simtime > inval .get (0 ) ? simtime : inval .get (0 );
161+
162+ // returning a string with data exluding simtime
163+ inval .remove (0 );
164+ return inval ;
165+ }
166+
167+ //write method, accepts a vector double and writes it to the file
168+ public static void write (int port , String name , ArrayList <Double > val , int delta ) {
169+ try (BufferedWriter writer = new BufferedWriter (new FileWriter (outpath + String .valueOf (port ) + "/" + name ))){
170+ val .add (0 ,simtime +delta );
171+ writer .write ('[' );
172+ for (int i =0 ;i <val .size ()-1 ;i ++){
173+ writer .write (val .get (i ).toString ());
174+ writer .write (',' );
175+ }
176+ writer .write (val .size ()-1 );
177+ writer .write (']' );
178+ } catch (IOException e ) {
179+ System .out .println ("skipping +" + outpath + port + " /" + name );
180+ e .printStackTrace ();
181+ }
182+ }
183+
184+ //write method, accepts a string and writes it to the file
185+ public static void write (int port , String name , String val , int delta ) {
186+ try {Thread .sleep (1000 * delay );}
187+ catch (InterruptedException e ){
188+ e .printStackTrace ();
189+ }
190+ try (BufferedWriter writer = new BufferedWriter (new FileWriter (outpath + String .valueOf (port ) + "/" + name ))){
191+ writer .write (val );
192+ } catch (IOException e ) {
193+ System .out .println ("skipping +" + outpath + port + " /" + name );
194+ e .printStackTrace ();
195+ }
196+ }
197+
198+ public static void default_maxtime (int defaultValue ) {
199+ try (BufferedReader reader = new BufferedReader (new FileReader (inpath +"1/concore.maxtime" ))){
200+ String line = reader .readLine ();
201+ maxtime = Integer .parseInt (line );
202+
203+ } catch (IOException e ) {
204+ maxtime = defaultValue ;
205+ }
206+ }
207+
208+ //Initializing
209+ public static ArrayList <Double > initval (String simtimeVal ) {
210+ //parsing
211+ ArrayList <Double > val = parser (simtimeVal );
212+
213+
214+ //determining simtime
215+ simtime = val .get (0 );
216+
217+ //returning the rest of the values(except simtime) in val
218+ val .remove (0 );
219+ return val ;
220+ }
221+ }
0 commit comments