Skip to content

Commit 9bc45c2

Browse files
committed
concoredocker.py ported to concoredocker.java
1 parent e73dee9 commit 9bc45c2

1 file changed

Lines changed: 206 additions & 0 deletions

File tree

concoredocker.java

Lines changed: 206 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,206 @@
1+
// concoredocker.java -- this java file will be the equivalent of concoredocker.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 concoredocker {
12+
static String s = "";
13+
static String olds = "";
14+
static String inpath = "/in"; // must be absolute 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 concoredocker(){
25+
iport = mapParser("concore.iport");
26+
oport = mapParser("concore.oport");
27+
28+
}
29+
public static void main(String[] args) {
30+
default_maxtime(100);
31+
}
32+
33+
private static Map<String, Integer> mapParser(String filename) {
34+
Map<String, Integer> config = new HashMap<>();
35+
StringBuilder portstr = new StringBuilder(); // StringBuilder to store file data
36+
try (BufferedReader reader = new BufferedReader(new FileReader(filename))) {
37+
int ch;
38+
while ((ch = reader.read()) != -1) {
39+
portstr.append((char) ch); // Add character to string builder
40+
}
41+
} catch (IOException e) {
42+
e.printStackTrace();
43+
}
44+
45+
portstr.setCharAt(portstr.length() - 1, ','); // Replace last character with comma
46+
portstr.append("}");
47+
int i = 0;
48+
String portname = "";
49+
String portnum = "";
50+
51+
while(portstr.charAt(i) != '}'){
52+
if(portstr.charAt(i) == '\''){
53+
i++;
54+
while(portstr.charAt(i) != '\''){
55+
portname += portstr.charAt(i);
56+
i++;
57+
}
58+
config.put(portname,0);
59+
}
60+
if(portstr.charAt(i) == ':'){
61+
i++;
62+
while(portstr.charAt(i) != ','){
63+
portnum += portstr.charAt(i);
64+
i++;
65+
}
66+
config.put(portname,Integer.parseInt(portnum));
67+
portname = "";
68+
portnum = "";
69+
}
70+
i++;
71+
}
72+
return config;
73+
}
74+
75+
//function to compare and determine whether file content has been changed
76+
public static boolean unchanged() {
77+
if (olds.equals(s)) {
78+
s = "";
79+
return true;
80+
} else {
81+
olds = s;
82+
return false;
83+
}
84+
}
85+
86+
private static ArrayList<Double> parser(String f){
87+
ArrayList<Double> temp = new ArrayList<>();
88+
String value = "";
89+
// Changing last bracket to comma to use comma as a delimiter
90+
f = f.substring(0, f.length() - 1) + ",";
91+
for(int i=1;i<f.length();i++){
92+
if(f.charAt(i) != ','){
93+
value += f.charAt(i);
94+
}
95+
else{
96+
if(value.length() != 0)
97+
temp.add(Double.parseDouble(value));
98+
99+
// reset value
100+
value = "";
101+
}
102+
}
103+
return temp;
104+
}
105+
106+
//accepts the file name as string and returns a string of file content
107+
public static ArrayList<Double> read(int port, String name, String initstr) {
108+
String ins = "";
109+
try {Thread.sleep(1000 * delay);}
110+
catch(InterruptedException e){
111+
e.printStackTrace();
112+
}
113+
try (BufferedReader reader = new BufferedReader(new FileReader(inpath + String.valueOf(port) + "/" + name))) {
114+
int ch;
115+
while ((ch = reader.read()) != -1) {
116+
ins += ((char) ch); // Add character to string
117+
}
118+
}catch(IOException e){
119+
ins = initstr;
120+
e.printStackTrace();
121+
}
122+
123+
124+
while(ins.length() == 0){
125+
try {Thread.sleep(1000 * delay);}
126+
catch(InterruptedException e){
127+
e.printStackTrace();
128+
}
129+
try (BufferedReader reader = new BufferedReader(new FileReader(inpath + String.valueOf(port) + "/" + name))) {
130+
int ch;
131+
while ((ch = reader.read()) != -1) {
132+
ins += ((char) ch); // Add character to string
133+
}
134+
retrycount++;
135+
} catch (IOException e) {
136+
//observed retry count in java from various tests is not calculated yet
137+
retrycount++;
138+
System.out.println("Read error");
139+
e.printStackTrace();
140+
}
141+
}
142+
s += ins;
143+
144+
ArrayList<Double> inval = new ArrayList<>();
145+
inval = parser(ins);
146+
simtime = simtime > inval.get(0) ? simtime : inval.get(0);
147+
148+
// returning a string with data exluding simtime
149+
inval.remove(0);
150+
return inval;
151+
}
152+
153+
//write method, accepts a vector double and writes it to the file
154+
public static void write(int port, String name, ArrayList<Double> val, int delta) {
155+
try (BufferedWriter writer = new BufferedWriter (new FileWriter(outpath + String.valueOf(port) + "/" + name))){
156+
val.add(0,simtime+delta);
157+
writer.write('[');
158+
for(int i=0;i<val.size()-1;i++){
159+
writer.write(val.get(i).toString());
160+
writer.write(',');
161+
}
162+
writer.write(val.size()-1);
163+
writer.write(']');
164+
} catch (IOException e) {
165+
System.out.println("skipping +" + outpath + port + " /" + name);
166+
e.printStackTrace();
167+
}
168+
}
169+
170+
//write method, accepts a string and writes it to the file
171+
public static void write(int port, String name, String val, int delta) {
172+
try {Thread.sleep(1000 * delay);}
173+
catch(InterruptedException e){
174+
e.printStackTrace();
175+
}
176+
try (BufferedWriter writer = new BufferedWriter (new FileWriter(outpath + String.valueOf(port) + "/" + name))){
177+
writer.write(val);
178+
} catch (IOException e) {
179+
System.out.println("skipping +" + outpath + port + " /" + name);
180+
e.printStackTrace();
181+
}
182+
}
183+
184+
public static void default_maxtime(int defaultValue) {
185+
try(BufferedReader reader = new BufferedReader(new FileReader(inpath+"1/concore.maxtime"))){
186+
String line = reader.readLine();
187+
maxtime = Integer.parseInt(line);
188+
189+
} catch (IOException e) {
190+
maxtime = defaultValue;
191+
}
192+
}
193+
194+
//Initializing
195+
public static ArrayList<Double> initval(String simtimeVal) {
196+
//parsing
197+
ArrayList<Double> val = new ArrayList<>();
198+
199+
//determining simtime
200+
simtime = val.get(0);
201+
202+
//returning the rest of the values(except simtime) in val
203+
val.remove(0);
204+
return val;
205+
}
206+
}

0 commit comments

Comments
 (0)