-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathconcoredocker.java
More file actions
206 lines (184 loc) · 6.84 KB
/
concoredocker.java
File metadata and controls
206 lines (184 loc) · 6.84 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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
// concoredocker.java -- this java file will be the equivalent of concoredocker.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 concoredocker {
static String s = "";
static String olds = "";
static String inpath = "/in"; // must be absolute 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<String, Integer> iport = new HashMap<>();
public static Map<String, Integer> oport = new HashMap<>();
public concoredocker(){
iport = mapParser("concore.iport");
oport = mapParser("concore.oport");
}
public static void main(String[] args) {
default_maxtime(100);
}
private static Map<String, Integer> mapParser(String filename) {
Map<String, Integer> 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<Double> parser(String f){
ArrayList<Double> 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<f.length();i++){
if(f.charAt(i) != ','){
value += f.charAt(i);
}
else{
if(value.length() != 0)
temp.add(Double.parseDouble(value));
// reset value
value = "";
}
}
return temp;
}
//accepts the file name as string and returns a string of file content
public static ArrayList<Double> 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<Double> 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<Double> 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<val.size()-1;i++){
writer.write(val.get(i).toString());
writer.write(',');
}
writer.write(val.size()-1);
writer.write(']');
} catch (IOException e) {
System.out.println("skipping +" + outpath + port + " /" + name);
e.printStackTrace();
}
}
//write method, accepts a string and writes it to the file
public static void write(int port, String name, String val, int delta) {
try {Thread.sleep(1000 * delay);}
catch(InterruptedException e){
e.printStackTrace();
}
try (BufferedWriter writer = new BufferedWriter (new FileWriter(outpath + String.valueOf(port) + "/" + name))){
writer.write(val);
} catch (IOException e) {
System.out.println("skipping +" + outpath + port + " /" + name);
e.printStackTrace();
}
}
public static void default_maxtime(int defaultValue) {
try(BufferedReader reader = new BufferedReader(new FileReader(inpath+"1/concore.maxtime"))){
String line = reader.readLine();
maxtime = Integer.parseInt(line);
} catch (IOException e) {
maxtime = defaultValue;
}
}
//Initializing
public static ArrayList<Double> initval(String simtimeVal) {
//parsing
ArrayList<Double> val = new ArrayList<>();
//determining simtime
simtime = val.get(0);
//returning the rest of the values(except simtime) in val
val.remove(0);
return val;
}
}