1 | package vnsim.vehicular.routePlan.cityRouting; |
---|
2 | |
---|
3 | import java.io.BufferedReader; |
---|
4 | import java.io.BufferedWriter; |
---|
5 | import java.io.File; |
---|
6 | import java.io.FileReader; |
---|
7 | import java.io.FileWriter; |
---|
8 | import java.io.IOException; |
---|
9 | import java.util.ArrayList; |
---|
10 | import java.util.StringTokenizer; |
---|
11 | |
---|
12 | import vnsim.map.object.Globals; |
---|
13 | |
---|
14 | public class Server { |
---|
15 | // server is processing data x time faster |
---|
16 | public static final int kTimeCompression = 5; |
---|
17 | // threshold |
---|
18 | private static final double kCongestionThresh = 5.0; |
---|
19 | // congestion for every cross roads |
---|
20 | private ArrayList<CrossRoadCongestion> crCongestion; |
---|
21 | // list of segments with their actual congestion; |
---|
22 | private ArrayList<RoadSegmentCongestion> updatedCongestion; |
---|
23 | // true if lastWeek was generated or loaded |
---|
24 | private boolean isLastWeekSet; |
---|
25 | |
---|
26 | public Server() { |
---|
27 | crCongestion = new ArrayList<CrossRoadCongestion>(); |
---|
28 | updatedCongestion = new ArrayList<RoadSegmentCongestion>(); |
---|
29 | isLastWeekSet = false; |
---|
30 | } |
---|
31 | |
---|
32 | // mediate real time data and store it into lastWeek db |
---|
33 | private void updateLastWeekCongestions(int day, int hour) { |
---|
34 | if (hour == 0) { // store to the previews hour entry; |
---|
35 | if (day > 0) { |
---|
36 | day--; |
---|
37 | hour = 23; |
---|
38 | } else { |
---|
39 | return; |
---|
40 | } |
---|
41 | } else { |
---|
42 | hour--; |
---|
43 | } |
---|
44 | for (int i = 0; i < crCongestion.size(); i++) { |
---|
45 | crCongestion.get(i).updateLastWeekCongestion(day, hour); |
---|
46 | } |
---|
47 | } |
---|
48 | |
---|
49 | // make last week public by storing it in Globals.lastWeekCongestions |
---|
50 | private void publishLastWeekCongestions() { |
---|
51 | ArrayList<ArrayList<WeekCongestion>> lastWeekCongestions = new ArrayList<ArrayList<WeekCongestion>>(); |
---|
52 | for (int i = 0; i < crCongestion.size(); i++) { |
---|
53 | lastWeekCongestions.add(crCongestion.get(i).cloneLastWeek()); |
---|
54 | } |
---|
55 | Globals.lastWeekCongestions = lastWeekCongestions; |
---|
56 | isLastWeekSet = true; |
---|
57 | } |
---|
58 | |
---|
59 | // set real time data |
---|
60 | public void setCongestion(int crossIndex, int segmentIndex, int crtTime, |
---|
61 | Double congestion) { |
---|
62 | crCongestion.get(crossIndex).setCongestion(segmentIndex, congestion); |
---|
63 | if (isLastWeekSet) { |
---|
64 | int time = crtTime / kTimeCompression; // secs |
---|
65 | time = time / 3600; // hours |
---|
66 | int hour = time % 24; |
---|
67 | time = time / 24; // days |
---|
68 | int day = time % 7; |
---|
69 | Double lastCongestion = crCongestion.get(crossIndex) |
---|
70 | .getLastWeekCongestion(segmentIndex, day, hour); |
---|
71 | RoadSegmentCongestion rsc = new RoadSegmentCongestion(crossIndex, |
---|
72 | segmentIndex, congestion); |
---|
73 | if (isDifferent(congestion, lastCongestion)) { |
---|
74 | RoadSegmentCongestion existingRSC = null; |
---|
75 | for (int i = 0; i < updatedCongestion.size(); i++) { |
---|
76 | if (updatedCongestion.get(i).equals(rsc)) { |
---|
77 | existingRSC = updatedCongestion.get(i); |
---|
78 | break; |
---|
79 | } |
---|
80 | } |
---|
81 | if (existingRSC == null) { // entry does not exist -> add |
---|
82 | // congestion |
---|
83 | updatedCongestion.add(rsc); |
---|
84 | broadcastUpdatedCongestions(); |
---|
85 | } else { |
---|
86 | if (isDifferent(congestion, existingRSC.getCongestion())) { |
---|
87 | existingRSC.setCongestion(congestion); |
---|
88 | broadcastUpdatedCongestions(); |
---|
89 | } |
---|
90 | } |
---|
91 | } else { |
---|
92 | for (int i = 0; i < updatedCongestion.size(); i++) { |
---|
93 | if (updatedCongestion.get(i).equals(rsc)) { |
---|
94 | updatedCongestion.remove(i); |
---|
95 | broadcastUpdatedCongestions(); |
---|
96 | break; |
---|
97 | } |
---|
98 | } |
---|
99 | } |
---|
100 | } |
---|
101 | } |
---|
102 | |
---|
103 | private boolean isDifferent(Double current, Double previous) { |
---|
104 | return (Math.abs(current - previous) > kCongestionThresh); |
---|
105 | } |
---|
106 | |
---|
107 | private void broadcastUpdatedCongestions() { |
---|
108 | ArrayList<RoadSegmentCongestion> toSend = new ArrayList<RoadSegmentCongestion>(); |
---|
109 | for (int i = 0; i < updatedCongestion.size(); i++) { |
---|
110 | toSend.add(updatedCongestion.get(i).clone()); |
---|
111 | } |
---|
112 | IntersectionNode in; |
---|
113 | for (int i = 0; i < crCongestion.size(); i++) { |
---|
114 | in = crCongestion.get(i).getIntersectionNode(); |
---|
115 | in.updateCongestions(toSend); |
---|
116 | } |
---|
117 | } |
---|
118 | |
---|
119 | // IntersectionNode subscribes to the server for server replies |
---|
120 | public int addIntersectionNode(IntersectionNode inode) { |
---|
121 | crCongestion.add(new CrossRoadCongestion(inode)); |
---|
122 | return crCongestion.size() - 1; |
---|
123 | } |
---|
124 | |
---|
125 | // to be called after the Intersection nodes were registered to the server |
---|
126 | public void init() { |
---|
127 | if (!isLastWeekSet) { |
---|
128 | isLastWeekSet = loadCongestions(null); |
---|
129 | publishLastWeekCongestions(); |
---|
130 | } |
---|
131 | } |
---|
132 | |
---|
133 | public void step(int crtTime) { |
---|
134 | if (crtTime % kTimeCompression != 0) { |
---|
135 | return; |
---|
136 | } |
---|
137 | int time = crtTime / kTimeCompression; // seconds |
---|
138 | time = time / 3600; // hours |
---|
139 | int hour = time % 24; |
---|
140 | time = time / 24; // days |
---|
141 | int day = time % 7; |
---|
142 | time = crtTime / kTimeCompression; |
---|
143 | |
---|
144 | // for each hour |
---|
145 | if (time % 3600 == 0) { |
---|
146 | updateLastWeekCongestions(day, hour); |
---|
147 | updatedCongestion.clear(); |
---|
148 | broadcastUpdatedCongestions(); |
---|
149 | } |
---|
150 | |
---|
151 | // for each week |
---|
152 | //if (time % 604800 == 0) { |
---|
153 | if (time % 86400 == 0) { |
---|
154 | saveCongestions(null); |
---|
155 | publishLastWeekCongestions(); |
---|
156 | } |
---|
157 | } |
---|
158 | |
---|
159 | // save lastWeek congestion into filename file |
---|
160 | private boolean saveCongestions(String filename) { |
---|
161 | if (filename == null) { |
---|
162 | filename = System.getProperty("user.home") + File.separatorChar + "maps" + |
---|
163 | File.separatorChar + "rt" + File.separatorChar + Globals.scenarioName + ".cng.txt"; |
---|
164 | } |
---|
165 | BufferedWriter out = null; |
---|
166 | try { |
---|
167 | out = new BufferedWriter(new FileWriter(filename)); |
---|
168 | String str; |
---|
169 | StringTokenizer st; |
---|
170 | |
---|
171 | out.write("crosses_nb " + crCongestion.size()); |
---|
172 | out.newLine(); |
---|
173 | for (int i = 0; i < crCongestion.size(); i++) { |
---|
174 | out.write("cross " + i); |
---|
175 | out.newLine(); |
---|
176 | str = crCongestion.get(i).toString(); |
---|
177 | st = new StringTokenizer(str, "\n"); |
---|
178 | while (st.hasMoreTokens()) { |
---|
179 | out.write(st.nextToken()); |
---|
180 | out.newLine(); |
---|
181 | } |
---|
182 | } |
---|
183 | out.close(); |
---|
184 | } catch (IOException e) { |
---|
185 | System.out.println("Could not save congestion file " + filename); |
---|
186 | return false; |
---|
187 | } |
---|
188 | return true; |
---|
189 | } |
---|
190 | |
---|
191 | // load lastWeek congestion from filename |
---|
192 | private boolean loadCongestions(String filename) { |
---|
193 | if (filename == null) { |
---|
194 | filename = System.getProperty("user.home") + File.separatorChar + "maps" + |
---|
195 | File.separatorChar + "rt" + File.separatorChar + Globals.scenarioName + ".cng.txt"; |
---|
196 | } |
---|
197 | BufferedReader in = null; |
---|
198 | try { |
---|
199 | in = new BufferedReader(new FileReader(filename)); |
---|
200 | String line = ""; |
---|
201 | StringTokenizer st; |
---|
202 | |
---|
203 | int crossIndex, segmentIndex, day, hour; |
---|
204 | int crosses_nb, segments_nb; |
---|
205 | Double congestion; |
---|
206 | |
---|
207 | line = in.readLine(); |
---|
208 | while (line != null) { |
---|
209 | st = new StringTokenizer(line, " "); |
---|
210 | st.nextToken(); // crosses_nb |
---|
211 | crosses_nb = Integer.parseInt(st.nextToken()); |
---|
212 | for (crossIndex = 0; crossIndex < crosses_nb; crossIndex++) { |
---|
213 | line = in.readLine(); // cross i |
---|
214 | line = in.readLine(); |
---|
215 | st = new StringTokenizer(line, " "); |
---|
216 | st.nextToken(); // segments_nb; |
---|
217 | segments_nb = Integer.parseInt(st.nextToken()); |
---|
218 | for (segmentIndex = 0; segmentIndex < segments_nb; segmentIndex++) { |
---|
219 | line = in.readLine(); // segment i |
---|
220 | for (day = 0; day < 7; day++) { |
---|
221 | line = in.readLine(); |
---|
222 | st = new StringTokenizer(line, " "); |
---|
223 | for (hour = 0; hour < 24; hour++) { |
---|
224 | congestion = Double.parseDouble(st.nextToken()); |
---|
225 | crCongestion.get(crossIndex) |
---|
226 | .setLastWeekCongestion(segmentIndex, |
---|
227 | day, hour, congestion); |
---|
228 | } |
---|
229 | } |
---|
230 | } |
---|
231 | } |
---|
232 | line = in.readLine(); |
---|
233 | } |
---|
234 | in.close(); |
---|
235 | } catch (IOException e) { |
---|
236 | System.out.println("Could not load congestion file " + filename); |
---|
237 | return false; |
---|
238 | } |
---|
239 | return true; |
---|
240 | } |
---|
241 | } |
---|