1 | /************************************************************************************ |
---|
2 | * Copyright (C) 2008 by Politehnica University of Bucharest and Rutgers University |
---|
3 | * All rights reserved. |
---|
4 | * Refer to LICENSE for terms and conditions of use. |
---|
5 | ***********************************************************************************/ |
---|
6 | package vnsim.vehicular.routePlan.infrastructureRouted; |
---|
7 | |
---|
8 | |
---|
9 | import java.io.ObjectInputStream; |
---|
10 | import java.io.ByteArrayInputStream; |
---|
11 | import java.io.Serializable; |
---|
12 | import java.util.StringTokenizer; |
---|
13 | |
---|
14 | import java.util.ArrayList; |
---|
15 | import java.util.Vector; |
---|
16 | |
---|
17 | import vnsim.applications.trafficview.SimulatedCarInfo; |
---|
18 | import vnsim.applications.vitp.CarRunningVITP; |
---|
19 | import vnsim.applications.vitp.routing.RoutingPacket; |
---|
20 | import vnsim.core.Communicator; |
---|
21 | import vnsim.core.events.SendEvent; |
---|
22 | import vnsim.map.object.Cross; |
---|
23 | import vnsim.map.object.Globals; |
---|
24 | import vnsim.map.object.Road; |
---|
25 | import vnsim.vehicular.generator.Mobility; |
---|
26 | import vnsim.vehicular.routePlan.RouteComputingUtils; |
---|
27 | import vnsim.vehicular.simulator.CarInstance; |
---|
28 | import vnsim.vehicular.simulator.Location; |
---|
29 | import vnsim.vehicular.simulator.NotMovingPersonality; |
---|
30 | import vnsim.vehicular.simulator.RouteSegment; |
---|
31 | import vnsim.vehicular.simulator.RoutingUtils; |
---|
32 | import vnsim.vehicular.simulator.intersections.DirectedRoadSegment; |
---|
33 | import vnsim.vehicular.simulator.intersections.Intersection; |
---|
34 | |
---|
35 | |
---|
36 | |
---|
37 | |
---|
38 | |
---|
39 | public class InfrastructureNode extends CarRunningVITP { |
---|
40 | Intersection correspondingIntersection = null; |
---|
41 | |
---|
42 | Vector<DelayRecord> correspondingDelayRecords = new Vector<DelayRecord>(); |
---|
43 | |
---|
44 | public InfrastructureNode(int vehicleId, short roadIdx, short pointIdx, |
---|
45 | Intersection p) { |
---|
46 | super(vehicleId, (byte) 0, 0.0, roadIdx, pointIdx, (byte) 0, 0.0); |
---|
47 | this.correspondingIntersection = p; |
---|
48 | |
---|
49 | for (int i = 0; i < correspondingIntersection.segments.size(); i++) { |
---|
50 | for (int j = 0; j < Globals.routePlanConstants.delays.size(); j++) { |
---|
51 | if (Globals.routePlanConstants.delays.get(j).equals(correspondingIntersection.segments.get(i))) { |
---|
52 | this.correspondingDelayRecords.add(Globals.routePlanConstants.delays.get(j)); |
---|
53 | break; |
---|
54 | } |
---|
55 | } |
---|
56 | } |
---|
57 | if (this.correspondingIntersection.segments.size() != this.correspondingDelayRecords.size()) { |
---|
58 | System.out.println("Err: delayrecords are not properly initialized"); |
---|
59 | System.exit(0); |
---|
60 | |
---|
61 | } |
---|
62 | |
---|
63 | } |
---|
64 | |
---|
65 | public void onReceive(byte[] bytesReceived, Serializable m, Communicator sender) { |
---|
66 | // super.onReceive(bytesReceived); |
---|
67 | // System.out.println("SEMAFOR on receive!"); |
---|
68 | if (!isEquipped || bytesReceived == null) |
---|
69 | return; |
---|
70 | // is it a VITP packet? |
---|
71 | byte header = bytesReceived[0]; |
---|
72 | |
---|
73 | byte[] message = null; |
---|
74 | |
---|
75 | switch (header) { |
---|
76 | case Globals.PROT_NEIGHBOR_DISCOVERY: |
---|
77 | // ByteBuffer bb = ByteBuffer.wrap(bytesReceived, 1, |
---|
78 | // bytesReceived.length-1); |
---|
79 | |
---|
80 | super.onReceive(bytesReceived, m, sender); |
---|
81 | // SimulatedCarInfo sc = new SimulatedCarInfo(); |
---|
82 | // message = new byte[bytesReceived.length - 1]; |
---|
83 | // for (int i = 0; i < message.length; i++) { |
---|
84 | // message[i] = bytesReceived[i + 1]; |
---|
85 | // } |
---|
86 | // sc.wrap(message); |
---|
87 | // boolean added = false; |
---|
88 | // for (int i = 0; i < trafficDB.size(); i++) |
---|
89 | // if ((trafficDB.get(i)).getVehicleId() == sc.getVehicleId()) { |
---|
90 | // synchronized (trafficDB) { |
---|
91 | // trafficDB.set(i, sc); |
---|
92 | // // System.out.println("Insert "+dc.getVehicleId() +" at |
---|
93 | // // "+i); |
---|
94 | // added = true; |
---|
95 | // } |
---|
96 | // break; |
---|
97 | // } |
---|
98 | // if (!added) |
---|
99 | // synchronized (trafficDB) { |
---|
100 | // trafficDB.add(sc); |
---|
101 | // // System.out.println("Insert "+dc.getVehicleId() +" at |
---|
102 | // // "+(Globals.neighbors.size()-1)); |
---|
103 | // } |
---|
104 | break; |
---|
105 | |
---|
106 | case Globals.VITP_PROT: |
---|
107 | // // System.out.println("I AM :"+vehicleId+"; RECEIVED VITP!"); |
---|
108 | super.onReceive(bytesReceived, m, sender); |
---|
109 | // message = new byte[bytesReceived.length - 1]; |
---|
110 | // for (int i = 0; i < message.length; i++) { |
---|
111 | // message[i] = bytesReceived[i + 1]; |
---|
112 | // } |
---|
113 | // try { |
---|
114 | // ObjectInputStream ois = new ObjectInputStream( |
---|
115 | // new ByteArrayInputStream(message)); |
---|
116 | // RoutingPacket rp = (RoutingPacket) ois.readObject(); |
---|
117 | // |
---|
118 | // r.postMessageFromBelow(rp); |
---|
119 | // |
---|
120 | // } catch (Exception ex) { |
---|
121 | // System.out.println("EXCEPTION CarRunningVITP:" + ex.toString()); |
---|
122 | // ex.printStackTrace(); |
---|
123 | // return; |
---|
124 | // } |
---|
125 | break; |
---|
126 | case Globals.ROAD_PLANNING_PROT: |
---|
127 | System.out.println("I AM INFRASTRUCTURE RECEIVED REQUEST!"); |
---|
128 | message = new byte[bytesReceived.length - 1]; |
---|
129 | for (int i = 0; i < message.length; i++) { |
---|
130 | message[i] = bytesReceived[i + 1]; |
---|
131 | } |
---|
132 | String reply = parseRoutingRequest(message); |
---|
133 | if (reply != null) { |
---|
134 | this.broadcastRoadPacket(reply); |
---|
135 | } |
---|
136 | break; |
---|
137 | } |
---|
138 | } |
---|
139 | |
---|
140 | public String parseRoutingRequest(byte[] rec) { |
---|
141 | String replyBody = null; |
---|
142 | String body = new String(rec); |
---|
143 | |
---|
144 | System.out.println("Struct cu id=" + this.vehicleId |
---|
145 | + " primesc un mesaj:" + body+ "r="+this.roadIdx+ |
---|
146 | |
---|
147 | "p="+this.pointIdx); |
---|
148 | StringTokenizer st = null; |
---|
149 | boolean cont = false; |
---|
150 | int cr, cp2, cp1; |
---|
151 | try { |
---|
152 | st = new StringTokenizer(body); |
---|
153 | String type = null; |
---|
154 | type = st.nextToken(); |
---|
155 | if (!type.equals("DR")) { |
---|
156 | return null; |
---|
157 | } |
---|
158 | st.nextToken(); |
---|
159 | |
---|
160 | Cross c; |
---|
161 | cr = (new Integer(st.nextToken())).intValue(); |
---|
162 | cp1 = (new Integer(st.nextToken())).intValue(); |
---|
163 | cp2 = (new Integer(st.nextToken())).intValue(); |
---|
164 | // System.out.println("cere sem de pe sourceRoadIdx "+cr+"p1="+cp1+" p2="+cp2); |
---|
165 | if ((cr == this.getRealPos().getRoadIdx()) |
---|
166 | && (cp2 == this.getRealPos().getPointIdx())) { |
---|
167 | cont = true; |
---|
168 | } else { |
---|
169 | |
---|
170 | for (int i = 0; i < Globals.map.roads.get(this.getRealPos() |
---|
171 | .getRoadIdx()).crosses.size(); i++) { |
---|
172 | c = Globals.map.roads.get(this.getRealPos().getRoadIdx()).crosses |
---|
173 | .get(i); |
---|
174 | if (c.getCrossRoadIndex() == cr |
---|
175 | && c.getCrossPointIndex() == cp2 |
---|
176 | && (c.getPointIndex() == this.getRealPos() |
---|
177 | .getPointIdx())) { |
---|
178 | cont = true; |
---|
179 | break; |
---|
180 | } |
---|
181 | } |
---|
182 | } |
---|
183 | |
---|
184 | } catch (Exception e) { |
---|
185 | |
---|
186 | return null; |
---|
187 | } |
---|
188 | |
---|
189 | if (!cont) { |
---|
190 | System.out.println("Struct " + this.vehicleId |
---|
191 | + " got messg not for me but for" + cr + " " + cp2); |
---|
192 | return null; |
---|
193 | } |
---|
194 | try { |
---|
195 | |
---|
196 | // replyBody = new String(body); |
---|
197 | // replyBody = replyBody.substring(replyBody.indexOf(' ')); |
---|
198 | // replyBody="RDR "+replyBody; |
---|
199 | replyBody = "R" + body; |
---|
200 | int rd, pt; |
---|
201 | |
---|
202 | RouteComputingUtils rc = new RouteComputingUtils(); |
---|
203 | try { |
---|
204 | |
---|
205 | rd = (new Integer(st.nextToken())).intValue(); |
---|
206 | pt = (new Integer(st.nextToken())).intValue(); |
---|
207 | boolean nextToMe = false; |
---|
208 | int p1, p2; |
---|
209 | Road r = Globals.map.roads.get(this.getRealPos().getRoadIdx()); |
---|
210 | if (this.getRealPos().getRoadIdx() == rd) { |
---|
211 | // System.out.println("destination e pe str cu mine"); |
---|
212 | if (pt < this.getRealPos().getPointIdx()) { |
---|
213 | p1 = pt; |
---|
214 | p2 = this.getRealPos().getPointIdx(); |
---|
215 | } else { |
---|
216 | p2 = pt; |
---|
217 | p1 = this.getRealPos().getPointIdx(); |
---|
218 | } |
---|
219 | boolean foundCross = false; |
---|
220 | |
---|
221 | for (int i = 0; i < r.crosses.size(); i++) { |
---|
222 | if ((r.crosses.get(i).getPointIndex() > p1) |
---|
223 | && ((r.crosses.get(i).getPointIndex()) < p2)) { |
---|
224 | // System.out |
---|
225 | // .println("Gasesc un cros pana in destination in pct " |
---|
226 | // + r.crosses.get(i).getPointIndex()); |
---|
227 | foundCross = true; |
---|
228 | break; |
---|
229 | } |
---|
230 | } |
---|
231 | if (!foundCross) { |
---|
232 | nextToMe = true; |
---|
233 | replyBody = replyBody.concat(" " + rd + " " |
---|
234 | + this.getRealPos().getPointIdx() + " " + pt); |
---|
235 | // System.out.println(" Nu am gasit cross intre noi"); |
---|
236 | |
---|
237 | } |
---|
238 | |
---|
239 | } |
---|
240 | if (!nextToMe) { |
---|
241 | // System.out.println("caut in crossuri"); |
---|
242 | for (int i = 0; i < r.crosses.size(); i++) { |
---|
243 | if ((r.crosses.get(i).getCrossRoadIndex() == rd) |
---|
244 | && (r.crosses.get(i).getPointIndex() == this.pointIdx)) { |
---|
245 | // System.out.println("Am cross cu strada aia "); |
---|
246 | if (pt < r.crosses.get(i).getCrossPointIndex()) { |
---|
247 | p1 = pt; |
---|
248 | p2 = r.crosses.get(i).getCrossPointIndex(); |
---|
249 | } else { |
---|
250 | p2 = pt; |
---|
251 | p1 = r.crosses.get(i).getCrossPointIndex(); |
---|
252 | } |
---|
253 | boolean foundCross = false; |
---|
254 | Road tpR = Globals.map.roads.get(rd); |
---|
255 | for (int j = 0; j < tpR.crosses.size(); j++) { |
---|
256 | if ((tpR.crosses.get(j).getPointIndex() > p1) |
---|
257 | && ((tpR.crosses.get(j).getPointIndex()) < p2)) { |
---|
258 | // System.out |
---|
259 | // .println("Mai am un cross pana la punctul |
---|
260 | // ala pe cross road"); |
---|
261 | foundCross = true; |
---|
262 | break; |
---|
263 | } |
---|
264 | } |
---|
265 | if (!foundCross) { |
---|
266 | nextToMe = true; |
---|
267 | replyBody = replyBody.concat(" " + rd + " " |
---|
268 | + r.crosses.get(i).getCrossPointIndex() |
---|
269 | + " " + pt); |
---|
270 | // System.out |
---|
271 | // .println("Nu mai e cross pe noua strada intre |
---|
272 | // mine si destination"); |
---|
273 | break; |
---|
274 | } |
---|
275 | |
---|
276 | } |
---|
277 | } |
---|
278 | |
---|
279 | } |
---|
280 | |
---|
281 | if (!nextToMe) |
---|
282 | |
---|
283 | { |
---|
284 | // System.out.println("NU E LANGA MINE"); |
---|
285 | ArrayList<Location> points = rc |
---|
286 | .dijkstraPathWithDelay(new Location(this |
---|
287 | .getRoadIdx(), this.getPointIdx()), |
---|
288 | new Location(rd, pt), |
---|
289 | new Location(cr, cp2), |
---|
290 | new Location(cr, cp1)); |
---|
291 | if (points != null) { |
---|
292 | // System.out.println(" am points:" +points); |
---|
293 | ArrayList<RouteSegment> ret = RoutingUtils |
---|
294 | .mergeRoute(points); |
---|
295 | // System.out.println(" am merged points:" + ret); |
---|
296 | ret = RoutingUtils.splitRoute(ret); |
---|
297 | // System.out.println(" am ruta:" + ret); |
---|
298 | int ii = 0; |
---|
299 | while (ii < ret.size()) { |
---|
300 | if (ret.get(ii).pt1 == ret.get(ii).pt2) { |
---|
301 | // System.out.println(" scot din ruta:" + ret+" |
---|
302 | // poz "+ii); |
---|
303 | ret.remove(ii); |
---|
304 | } else { |
---|
305 | ii++; |
---|
306 | } |
---|
307 | } |
---|
308 | |
---|
309 | replyBody = replyBody.concat(" " + ret.get(0).roadIndex |
---|
310 | + " " + ret.get(0).pt1 + " " + ret.get(0).pt2); |
---|
311 | // System.out.println(" ret "+ret.get(0)+"repl |
---|
312 | // "+replyBody); |
---|
313 | // System.out.println("Gasesc ruta"); |
---|
314 | } else { |
---|
315 | System.out.println("Nu gasesc ruta"); |
---|
316 | } |
---|
317 | |
---|
318 | // System.out.println("Struct cu id=" + this.getVehicleId() |
---|
319 | // + " raspund cu " + replyBody); |
---|
320 | |
---|
321 | } |
---|
322 | } catch (Exception e) { |
---|
323 | System.out.println("Incorect format for the request"); |
---|
324 | e.printStackTrace(); |
---|
325 | return null; |
---|
326 | } |
---|
327 | |
---|
328 | } catch (Exception e) { |
---|
329 | e.printStackTrace(); |
---|
330 | return null; |
---|
331 | } |
---|
332 | // System.out.println("returnez: " + replyBody); |
---|
333 | return replyBody; |
---|
334 | |
---|
335 | } |
---|
336 | |
---|
337 | public Intersection getCurrespondingIntersection() { |
---|
338 | return correspondingIntersection; |
---|
339 | } |
---|
340 | |
---|
341 | public void updateDelays() { |
---|
342 | for (int i = 0; i < this.correspondingDelayRecords.size(); i++) { |
---|
343 | |
---|
344 | int p1, p2, r, nr = 0; |
---|
345 | r = this.correspondingDelayRecords.get(i).roadSegment.roadIndex; |
---|
346 | p2 = this.correspondingDelayRecords.get(i).roadSegment.pointIndex; |
---|
347 | p1 = this.correspondingDelayRecords.get(i).previosCrossPoint; |
---|
348 | CarInstance car; |
---|
349 | if (p1 < p2) { |
---|
350 | for (int j = 0; j < Globals.map.roads.get(r).carsOnThisRoad |
---|
351 | .size(); j++) { |
---|
352 | car = Globals.map.roads.get(r).carsOnThisRoad.get(j); |
---|
353 | if ((car.getPointIdx() >= p1) && (car.getPointIdx() <= p2)) { |
---|
354 | if (car.route[car.routeIndex].pt1 < car.route[car.routeIndex].pt2) { |
---|
355 | nr++; |
---|
356 | } |
---|
357 | } |
---|
358 | } |
---|
359 | } else { |
---|
360 | for (int j = 0; j < Globals.map.roads.get(r).carsOnThisRoad |
---|
361 | .size(); j++) { |
---|
362 | car = Globals.map.roads.get(r).carsOnThisRoad.get(j); |
---|
363 | if ((car.getPointIdx() <= p1) && (car.getPointIdx() >= p2)) { |
---|
364 | if (car.route[car.routeIndex].pt1 > car.route[car.routeIndex].pt2) { |
---|
365 | nr++; |
---|
366 | } |
---|
367 | } |
---|
368 | } |
---|
369 | |
---|
370 | } |
---|
371 | // System.out.println("infrastruct" + this.getVehicleId() + "nr=" + nr |
---|
372 | // + " road r=" + r + " p1=" + p1 + " p2=" + p2 + " dr=" |
---|
373 | // + this.mySegments.get(i).location); |
---|
374 | |
---|
375 | long d = 0L; |
---|
376 | |
---|
377 | d = (long) (nr * 60 * 60); |
---|
378 | d = (d / (this.correspondingDelayRecords.get(i).getCapacity())); |
---|
379 | this.correspondingDelayRecords.get(i).setDelay(d); |
---|
380 | } |
---|
381 | |
---|
382 | } |
---|
383 | |
---|
384 | public static void createInfrastructureNodes(){ |
---|
385 | for (int i = 0; i < Globals.map.allIntersections.size(); i++) { |
---|
386 | Intersection it = Globals.map.allIntersections.get(i); |
---|
387 | DirectedRoadSegment dr = it.segments.get(0); |
---|
388 | CarInstance car = null; |
---|
389 | |
---|
390 | car = Mobility.createNewInvisibleCar(dr.roadIndex, |
---|
391 | dr.pointIndex, 0, 0, 0.0, new NotMovingPersonality(), |
---|
392 | 0.0, Globals.engine.lastCarID, dr.roadIndex, dr.pointIndex + 1, |
---|
393 | new RouteSegment((short) dr.roadIndex, |
---|
394 | (short) dr.pointIndex, |
---|
395 | (short) (dr.pointIndex + 1)), 5); |
---|
396 | if (car != null) { |
---|
397 | Globals.engine.lastCarID++; |
---|
398 | System.out.println("Created invisible carReceivingMsg id=" |
---|
399 | + (Globals.engine.lastCarID - 1) + " at road " + dr.roadIndex |
---|
400 | + " point " + dr.pointIndex); |
---|
401 | addInfrastructureNode(car, it); |
---|
402 | } else { |
---|
403 | System.out.println("nu am putut creea o struct"); |
---|
404 | } |
---|
405 | |
---|
406 | } |
---|
407 | |
---|
408 | } |
---|
409 | public static void addInfrastructureNode(CarInstance car, Intersection pos) { |
---|
410 | if (car == null) |
---|
411 | return; |
---|
412 | |
---|
413 | synchronized (Globals.engine.cars) { |
---|
414 | |
---|
415 | InfrastructureNode ic; |
---|
416 | |
---|
417 | if (car.routingType == 5) { |
---|
418 | if (Globals.engine.crtTime == 0) { |
---|
419 | |
---|
420 | ic = new InfrastructureNode(car.ID, car.getRoadIdx(), car.getPointIdx(), |
---|
421 | pos); |
---|
422 | ic.setTimestamp(Globals.engine.crtTime); |
---|
423 | ic.setRealPos(car); |
---|
424 | Globals.engine.cars.add(ic); |
---|
425 | Globals.engine.infrastructure.add(ic); |
---|
426 | ic.init(); |
---|
427 | |
---|
428 | } else { |
---|
429 | int poz = Globals.engine.cars.indexOf(new SimulatedCarInfo(car.ID)); |
---|
430 | if (poz == -1) { |
---|
431 | ic = new InfrastructureNode(car.ID, car.getRoadIdx(), |
---|
432 | car.getPointIdx(), pos); |
---|
433 | ic.setTimestamp(Globals.engine.crtTime); |
---|
434 | ic.setRealPos(car); |
---|
435 | Globals.engine.schedEvent(new SendEvent(Globals.engine.crtTime + 1, ic, |
---|
436 | SimulatedCarInfo.STANDARD_MESSAGE_ID)); |
---|
437 | Globals.engine.cars.add(ic); |
---|
438 | Globals.engine.infrastructure.add(ic); |
---|
439 | ic.init(); |
---|
440 | |
---|
441 | } else { |
---|
442 | ((SimulatedCarInfo) Globals.engine.cars.get(poz)).setRealPos(car); |
---|
443 | |
---|
444 | } |
---|
445 | } |
---|
446 | |
---|
447 | } |
---|
448 | } |
---|
449 | } |
---|
450 | |
---|
451 | |
---|
452 | |
---|
453 | } |
---|