Newer
Older
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
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class Helper {
public static synchronized void scriviFile(JSONObject json, String path) throws IOException {
BufferedWriter b = new BufferedWriter(new FileWriter(path));
b.write(json.toString());
b.close();
}
public static synchronized String leggiFile(String path) throws IOException {
String line;
String answer = "";
FileReader fileReader = new FileReader(path);
BufferedReader bufferedReader = new BufferedReader(fileReader);
while((line = bufferedReader.readLine()) != null) {
answer += line;
}
bufferedReader.close();
return answer;
}
// se add = true, aggiungo il valore n alla codaVal;
// se add = false, rimuovo il valore n dalla codaVal
public static synchronized void modificaFileStato(MyQueue<Integer> coda, boolean add, int n, int valore, String path) {
try {
JSONObject statoJson = new JSONObject(Helper.leggiFile(path));
statoJson.put("valore",valore);
JSONArray arrayCodaVal = statoJson.getJSONArray("codaVal");
if(add)
arrayCodaVal.put(n);
else {
int index = -1;
for(int i=0; i<arrayCodaVal.length(); i++) {
if(arrayCodaVal.getInt(i)==n)
index = i;
}
arrayCodaVal.remove(index);
}
Helper.scriviFile(statoJson.put("codaVal",arrayCodaVal), path);
} catch (JSONException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static synchronized void modificaFileStato(boolean add, int n, String path) {
try {
JSONObject statoJson = new JSONObject(Helper.leggiFile(path));
JSONArray arrayCodaVal = statoJson.getJSONArray("codaVal");
if(add)
arrayCodaVal.put(n);
else {
int index = -1;
for(int i=0; i<arrayCodaVal.length(); i++) {
if(arrayCodaVal.getInt(i)==n)
index = i;
}
arrayCodaVal.remove(index);
}
Helper.scriviFile(statoJson.put("codaVal",arrayCodaVal), path);
} catch (JSONException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static synchronized void modificaFileStato(boolean add, Pair p, String path) {
try {
JSONObject statoJson = new JSONObject(Helper.leggiFile(path));
JSONArray arrayCodaMsg = statoJson.getJSONArray("codaMsg");
if(add)
arrayCodaMsg.put(p.toJsonObject());
else {
int index = -1;
for(int i=0; i<arrayCodaMsg.length(); i++) {
JSONObject msgJson = arrayCodaMsg.getJSONObject(i);
if(msgJson.getString("topic").equals(p.getTopic()) && msgJson.getJSONObject("msg").toString().equals(p.getMsg()) )
index = i;
}
arrayCodaMsg.remove(index);
}
Helper.scriviFile(statoJson.put("codaMsg",arrayCodaMsg), path);
} catch (JSONException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static synchronized <T> void getFromFileStato(MyQueue<T> coda, String key, String path) throws JSONException, IOException {
JSONObject statoJson = new JSONObject(Helper.leggiFile(path));
JSONArray arrayCoda = statoJson.getJSONArray(key);
if(coda.getClass().equals(Pair.class)) {
MyQueue<Pair> codaMsg = (MyQueue<Pair>) coda;
for(int i = 0; i<arrayCoda.length(); i++) {
JSONObject pairJson = arrayCoda.getJSONObject(i);
Pair p = new Pair(pairJson.getString("topic"), pairJson.getString("msg"));
codaMsg.send(p);
}
}
if(coda.getClass().equals(Integer.class)) {
MyQueue<Integer> codaVal = (MyQueue<Integer>) coda;
for(int i = 0; i<arrayCoda.length(); i++) {
codaVal.send(arrayCoda.getInt(i));
}
}
}
}