1 | //varianta cu noRoad (thread assignat per nr de drumuri), varianta cu 8 procesoare (adica 7 pentru toate strazile) |
---|
2 | package vnsim.core; |
---|
3 | |
---|
4 | |
---|
5 | import java.io.BufferedWriter; |
---|
6 | import java.io.DataInputStream; |
---|
7 | import java.io.DataOutputStream; |
---|
8 | import java.io.File; |
---|
9 | import java.io.FileInputStream; |
---|
10 | import java.io.FileOutputStream; |
---|
11 | import java.io.FileWriter; |
---|
12 | import java.io.IOException; |
---|
13 | import java.io.PrintWriter; |
---|
14 | import java.util.ArrayList; |
---|
15 | import java.util.Collections; |
---|
16 | import java.util.Iterator; |
---|
17 | import java.util.ListIterator; |
---|
18 | import java.util.Random; |
---|
19 | import java.util.Vector; |
---|
20 | import java.util.concurrent.ExecutorService; |
---|
21 | import java.util.concurrent.Executors; |
---|
22 | |
---|
23 | import vnsim.applications.adaptiveTL.IntersectionCarRecord; |
---|
24 | import vnsim.applications.adaptiveTL.WirelessTrafficLight; |
---|
25 | import vnsim.applications.emissions.EmissionsTrafficLight; |
---|
26 | import vnsim.applications.ezcab.EzcabCar; |
---|
27 | import vnsim.applications.ezcab.EzcabClient; |
---|
28 | import vnsim.applications.trafficview.SimulatedCarInfo; |
---|
29 | import vnsim.core.events.CleanupEvent; |
---|
30 | import vnsim.core.events.Event; |
---|
31 | import vnsim.core.events.GPSEvent; |
---|
32 | import vnsim.core.events.ReceiveEvent; |
---|
33 | import vnsim.core.events.SendEvent; |
---|
34 | import vnsim.core.events.UnicastSendEvent; |
---|
35 | import vnsim.map.object.Globals; |
---|
36 | import vnsim.map.object.Map; |
---|
37 | import vnsim.map.object.PeanoKey; |
---|
38 | import vnsim.map.object.Point; |
---|
39 | import vnsim.map.object.Road; |
---|
40 | import vnsim.map.utils.GPSutil; |
---|
41 | import vnsim.network.RadioDev; |
---|
42 | import vnsim.network.dsrc.Application; |
---|
43 | import vnsim.network.dsrc.CarRunningDSRC; |
---|
44 | import vnsim.network.dsrc.DSRCStatisticComponent; |
---|
45 | import vnsim.network.dsrc.DSRCStatistics; |
---|
46 | import vnsim.network.dsrc.EmergencyRequest; |
---|
47 | import vnsim.network.dsrc.MAC80211; |
---|
48 | import vnsim.network.dsrc.WirelessPhy; |
---|
49 | import vnsim.network.propagation.Ricean; |
---|
50 | import vnsim.network.scft.CarRunningSCFT; |
---|
51 | import vnsim.vehicular.emissions.EmissionsResults; |
---|
52 | import vnsim.vehicular.generator.EntryFlowVariation; |
---|
53 | import vnsim.vehicular.generator.Mobility; |
---|
54 | import vnsim.vehicular.generator.NewCarEvent; |
---|
55 | import vnsim.vehicular.routePlan.RoutingConstants; |
---|
56 | import vnsim.vehicular.routePlan.cityRouting.CarToIntersectionComm; |
---|
57 | import vnsim.vehicular.routePlan.cityRouting.CityTrafficLight; |
---|
58 | import vnsim.vehicular.routePlan.cityRouting.IntersectionNode; |
---|
59 | import vnsim.vehicular.routePlan.cityRouting.RoadSegmentCost; |
---|
60 | import vnsim.vehicular.routePlan.cityRouting.Server; |
---|
61 | import vnsim.vehicular.routePlan.infrastructureRouted.DelayRecord; |
---|
62 | import vnsim.vehicular.routePlan.infrastructureRouted.GuidanceRequest; |
---|
63 | import vnsim.vehicular.routePlan.infrastructureRouted.InfrastructureNode; |
---|
64 | import vnsim.vehicular.routePlan.selfRouted.QueryGeneration; |
---|
65 | import vnsim.vehicular.scenarios.EntryExitScenario; |
---|
66 | import vnsim.vehicular.scenarios.EntryScenario; |
---|
67 | import vnsim.vehicular.scenarios.Route; |
---|
68 | import vnsim.vehicular.simulator.AggresivePersonality; |
---|
69 | import vnsim.vehicular.simulator.CalmPersonality; |
---|
70 | import vnsim.vehicular.simulator.CarInstance; |
---|
71 | import vnsim.vehicular.simulator.Personality; |
---|
72 | import vnsim.vehicular.simulator.RegularPersonality; |
---|
73 | import vnsim.vehicular.simulator.SortedCarVector; |
---|
74 | import vnsim.vehicular.simulator.intersections.Intersection; |
---|
75 | import vnsim.vehicular.simulator.intersections.IntersectionWithTrafficLights; |
---|
76 | |
---|
77 | |
---|
78 | |
---|
79 | /** |
---|
80 | * |
---|
81 | * The Engine class is the main class that controlls the simulation. It manages |
---|
82 | * the list of entities (cars, trafficlights) and the list of events and passes the |
---|
83 | * control to the mobility layer or communication layer when needed. |
---|
84 | * |
---|
85 | * @author Victor Gradinescu |
---|
86 | * |
---|
87 | */ |
---|
88 | |
---|
89 | public class Engine { |
---|
90 | |
---|
91 | |
---|
92 | /** |
---|
93 | * PTVS Project |
---|
94 | */ |
---|
95 | public ExecutorService executor[];/////////////////////////////////////// thread pool with Executors //////////////////// |
---|
96 | |
---|
97 | public void start(Event e, int i) throws IOException { |
---|
98 | |
---|
99 | executor[i].submit(new EventProcessingThread(this,e)); |
---|
100 | } |
---|
101 | |
---|
102 | int dimens; |
---|
103 | |
---|
104 | public static int numThreads; |
---|
105 | ///////////////////////end PTVS Project///////////////////////////////////////////////////// |
---|
106 | |
---|
107 | |
---|
108 | /* EZCab - file informations about the client */ |
---|
109 | public static BufferedWriter bwClient, bwCab; |
---|
110 | |
---|
111 | public static double WIRELESS_RANGE = 0.1; |
---|
112 | public static double EXTENDED_WIRELESS_RANGE = 1.0; |
---|
113 | |
---|
114 | // Petroaca - simulation protocol type ( 802.11 or DSRC ) |
---|
115 | public static int simulationProtocol=Globals.PROTOCOL_80211; |
---|
116 | |
---|
117 | //Petroaca - propagation type ( tworay or shadowing ) |
---|
118 | public static int propagationModel=Globals.TWO_RAY_GROUND; |
---|
119 | |
---|
120 | //Petroaca - the Ricean propagation module |
---|
121 | public static Ricean riceanModule; |
---|
122 | |
---|
123 | DataInputStream dis = null; |
---|
124 | |
---|
125 | public int fps; |
---|
126 | |
---|
127 | static Vector<Event> eventQueue = null; |
---|
128 | static int nrEvThreads = 0; |
---|
129 | |
---|
130 | //Petroaca - the DSRC emergency channel |
---|
131 | ArrayList<Event> eventQueueEmergency = null; |
---|
132 | |
---|
133 | public boolean pauseFlag = true; |
---|
134 | |
---|
135 | |
---|
136 | boolean doneFlag = false,doneFlagEmergency=false; |
---|
137 | |
---|
138 | public ArrayList<SimulatedCarInfo> cars = null; |
---|
139 | |
---|
140 | public int crtTime = 0; |
---|
141 | |
---|
142 | public long t0 = 0L; |
---|
143 | |
---|
144 | public boolean withGUI, startedWithGUI; |
---|
145 | |
---|
146 | boolean runtimeMobility; |
---|
147 | |
---|
148 | public boolean disableComm = false; |
---|
149 | |
---|
150 | File fText = null, fBin = null; |
---|
151 | |
---|
152 | PrintWriter pwText = null; |
---|
153 | |
---|
154 | DataOutputStream outBin = null; |
---|
155 | |
---|
156 | long prevRealSec = 0; |
---|
157 | double lastSimSecond = 0.0; |
---|
158 | |
---|
159 | int eventsTime = 0, mobilityTime = 0, cleanupTime = 0, netTime = 0, codeTime = 0; |
---|
160 | |
---|
161 | |
---|
162 | int schedTime = 0, moveTime = 0, semTime = 0; |
---|
163 | |
---|
164 | |
---|
165 | public double maxControlDelay = 0; |
---|
166 | |
---|
167 | public int mycnt = 0; |
---|
168 | public long fincars; |
---|
169 | public long crossedCars; |
---|
170 | public double totalkm, totalControlDelay; |
---|
171 | public EmissionsResults totalEM = new EmissionsResults(); |
---|
172 | private long totalTime; |
---|
173 | |
---|
174 | public static int created = 0; |
---|
175 | |
---|
176 | // Routing |
---|
177 | public static int simulationType = 0; |
---|
178 | public static int applicationType = 0; |
---|
179 | int jammed; |
---|
180 | |
---|
181 | public ArrayList<SimulatedCarInfo> infrastructure = null; |
---|
182 | |
---|
183 | Object event; |
---|
184 | |
---|
185 | //City Routing |
---|
186 | private Server server= new Server(); |
---|
187 | |
---|
188 | /** |
---|
189 | * |
---|
190 | * |
---|
191 | * @param withGUI If the simulator is run at the same time with GUI, the code |
---|
192 | * will have to be synchronized |
---|
193 | * @param runtimeMobility |
---|
194 | */ |
---|
195 | public Engine(boolean withGUI, boolean runtimeMobility) |
---|
196 | { |
---|
197 | try { |
---|
198 | FileWriter fwClient = new FileWriter("client_request.txt"); |
---|
199 | bwClient = new BufferedWriter(fwClient); |
---|
200 | FileWriter fwCab = new FileWriter("client_answers.txt"); |
---|
201 | bwCab = new BufferedWriter(fwCab); |
---|
202 | } catch (IOException e1) { |
---|
203 | e1.printStackTrace(); |
---|
204 | } |
---|
205 | this.withGUI = withGUI; |
---|
206 | this.startedWithGUI = withGUI; |
---|
207 | this.runtimeMobility = runtimeMobility; |
---|
208 | // init(); |
---|
209 | prevRealSec = t0 = System.currentTimeMillis(); |
---|
210 | |
---|
211 | } |
---|
212 | |
---|
213 | /* |
---|
214 | * Load the map, initialize eventqueue, init |
---|
215 | */ |
---|
216 | public void init() { |
---|
217 | |
---|
218 | Map map = Globals.map; |
---|
219 | |
---|
220 | //System.out.println("roads.size: "+map.roads.size()+" : "+map.roads.get(0).getName()); |
---|
221 | |
---|
222 | //Road road = (Road)map.roads.get(0); |
---|
223 | |
---|
224 | //System.out.println("road.points: "+road.points); |
---|
225 | |
---|
226 | //System.out.println("strada:"+road.getName()+":"+" road.crosses.size(): "+road.crosses.size()); |
---|
227 | |
---|
228 | |
---|
229 | |
---|
230 | |
---|
231 | |
---|
232 | /** |
---|
233 | * PTVS project |
---|
234 | */ |
---|
235 | ArrayList<Intersection> intersectii = map.allIntersections; |
---|
236 | //numThreads=intersectii.size(); |
---|
237 | numThreads = Globals.NO_FEP_PROC - 1; |
---|
238 | |
---|
239 | executor=new ExecutorService[numThreads]; |
---|
240 | |
---|
241 | for(int i=0; i < numThreads; i++){ |
---|
242 | executor[i] = Executors.newFixedThreadPool(1); // un singur thread e mereu refolosit pt fiecare intersectie.. ar putea fi mai multe |
---|
243 | } |
---|
244 | ///////////////////////end PTVS Project///////////////////////////////////////////////////// |
---|
245 | |
---|
246 | |
---|
247 | for(int q=0;q<intersectii.size();q++){ |
---|
248 | System.out.println("intersectia "+q+": "+" nume: "+intersectii.get(q).toString()+" "+intersectii.get(q).getMapPoint()); |
---|
249 | } |
---|
250 | |
---|
251 | System.out.println(".........................aici4........."); |
---|
252 | |
---|
253 | //*************************************************************** |
---|
254 | |
---|
255 | if (map.roads.get(0).getName().startsWith("Iuliu") || map.roads.get(0).getName().startsWith("Dr")) |
---|
256 | { |
---|
257 | |
---|
258 | Globals.monitorWTL = (WirelessTrafficLight)map.allIntersections.get(1); |
---|
259 | } |
---|
260 | pauseFlag = false; |
---|
261 | doneFlag = false; |
---|
262 | |
---|
263 | // the positions of the cars may be updated by a mobility module by |
---|
264 | // recomputing positions at each step or can be updated by reading a |
---|
265 | // traces file |
---|
266 | if (!runtimeMobility) |
---|
267 | { |
---|
268 | try |
---|
269 | { |
---|
270 | if (dis != null) { |
---|
271 | dis.close(); |
---|
272 | } |
---|
273 | dis = new DataInputStream(new FileInputStream(Globals.inFile)); |
---|
274 | } |
---|
275 | catch (IOException e) |
---|
276 | { |
---|
277 | System.err.println("Error reading scenario; file error"+ Globals.inFile); |
---|
278 | e.printStackTrace(); |
---|
279 | System.exit(0); |
---|
280 | } |
---|
281 | try |
---|
282 | { |
---|
283 | fps = dis.readInt(); |
---|
284 | } |
---|
285 | catch (IOException e) |
---|
286 | { |
---|
287 | System.err.println("Error reading file " + Globals.inFile); |
---|
288 | e.printStackTrace(); |
---|
289 | } |
---|
290 | } |
---|
291 | else |
---|
292 | { |
---|
293 | fps = Globals.executionFPS; |
---|
294 | |
---|
295 | Globals.WIRELESS_TRANSMISSION_FRAMES = (int) Math |
---|
296 | .ceil((double) Globals.WIRELESS_TRANSMISSION_TIME |
---|
297 | / (1000 / Globals.executionFPS)); |
---|
298 | |
---|
299 | } |
---|
300 | |
---|
301 | // init cars and events |
---|
302 | eventQueue = new Vector<Event>(); |
---|
303 | |
---|
304 | |
---|
305 | cars = new ArrayList<SimulatedCarInfo>(); |
---|
306 | |
---|
307 | |
---|
308 | |
---|
309 | //Petroaca - if DSRC simulation then initialize the emergency event queue |
---|
310 | if(Engine.simulationProtocol==Globals.PROTOCOL_DSRC) |
---|
311 | eventQueueEmergency = new ArrayList<Event>(); |
---|
312 | |
---|
313 | crtTime = 0; |
---|
314 | |
---|
315 | if (!runtimeMobility) { |
---|
316 | readScenarioPhase(); |
---|
317 | } else { |
---|
318 | // Mobility.initCarsTest(this); |
---|
319 | // Mobility.initCarsAutostrada(this); |
---|
320 | // Mobility.initCars(this); |
---|
321 | } |
---|
322 | |
---|
323 | synchronized (eventQueue) { |
---|
324 | |
---|
325 | eventQueue.add(new GPSEvent(0)); |
---|
326 | |
---|
327 | eventQueue.add(new CleanupEvent(1)); |
---|
328 | |
---|
329 | Iterator<SimulatedCarInfo> it = cars.iterator(); |
---|
330 | while (it.hasNext()) |
---|
331 | { |
---|
332 | System.out.println("nu ai cum sa intri pe aici!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! cars este vida"); |
---|
333 | SimulatedCarInfo r = it.next(); |
---|
334 | |
---|
335 | schedEvent(new SendEvent(4, r, |
---|
336 | SimulatedCarInfo.STANDARD_MESSAGE_ID)); |
---|
337 | } |
---|
338 | |
---|
339 | // cod comentat deaorece vroiam sa vad daca se calculeaza cum trebuie intersectia cea mai apropiata |
---|
340 | // si partea asta face ca semafoarele din toate intersectiile sa genereze evenimente |
---|
341 | |
---|
342 | /* Szekeres A. START_MODIFY - add broadcast event for each traffic light*/ |
---|
343 | for(int i=0;i<Globals.map.allIntersections.size();i++) { |
---|
344 | Intersection sender = Globals.map.allIntersections.get(i); |
---|
345 | if (sender instanceof EmissionsTrafficLight) |
---|
346 | { |
---|
347 | ((EmissionsTrafficLight)sender).init(); |
---|
348 | schedEvent(new SendEvent(4, (EmissionsTrafficLight)sender, (int)Globals.PROT_TL_FEEDBACK)); |
---|
349 | } |
---|
350 | } |
---|
351 | /* Szekeres A. STOP_MODIFY */ |
---|
352 | } |
---|
353 | |
---|
354 | |
---|
355 | try |
---|
356 | { |
---|
357 | if (runtimeMobility && outputToFile) |
---|
358 | { |
---|
359 | |
---|
360 | fText = new File(Globals.inFile + ".txt"); |
---|
361 | fBin = new File(Globals.inFile); |
---|
362 | fText.createNewFile(); |
---|
363 | fBin.createNewFile(); |
---|
364 | pwText = new PrintWriter(new FileOutputStream(fText)); |
---|
365 | outBin = new DataOutputStream(new FileOutputStream(fBin)); |
---|
366 | |
---|
367 | pwText.println("" + Globals.FPS); |
---|
368 | outBin.writeInt(Globals.FPS); |
---|
369 | } |
---|
370 | } |
---|
371 | catch (IOException e) |
---|
372 | { |
---|
373 | System.err.println("Error reading scenario; file error" |
---|
374 | + Globals.inFile); |
---|
375 | e.printStackTrace(); |
---|
376 | System.exit(0); |
---|
377 | } |
---|
378 | |
---|
379 | if (Engine.simulationType == RoutingConstants.DYNAMIC_INFRASTRUCTURE_ROUTE) { |
---|
380 | DelayRecord.initDelays(); |
---|
381 | this.infrastructure=new ArrayList<SimulatedCarInfo>(); |
---|
382 | //create a fixed node for every intersection |
---|
383 | InfrastructureNode.createInfrastructureNodes(); |
---|
384 | } |
---|
385 | |
---|
386 | //Petroaca - instantiate the ricean propagation module |
---|
387 | try |
---|
388 | { |
---|
389 | if(Engine.propagationModel==Globals.RICEAN) |
---|
390 | Engine.riceanModule=new Ricean(Globals.RICEAN_FILE_NAME,true); |
---|
391 | } |
---|
392 | catch(IOException ioe){System.out.println("Ricean file io error:"+ioe.getMessage());System.exit(1);} |
---|
393 | |
---|
394 | System.out.println(">>> Engine initialized - "+eventQueue.size()+" events in the queue"); |
---|
395 | System.out.println("CARS:"+cars); |
---|
396 | System.out.println("Events:"+eventQueue); |
---|
397 | |
---|
398 | |
---|
399 | System.out.println(); |
---|
400 | |
---|
401 | /*Road r = Globals.map.roads.get(1); |
---|
402 | System.out.print("road:"+r.getName()); |
---|
403 | System.out.println(" r.points.size():"+r.points.size()); |
---|
404 | |
---|
405 | Point p1 = r.points.get(r.points.size()-1); |
---|
406 | System.out.println(p1); |
---|
407 | Point p2 = r.points.get(r.points.size()-2); |
---|
408 | System.out.println(p2); |
---|
409 | |
---|
410 | System.out.println("distanta: " +(p1.getDistance() - p2.getDistance())); |
---|
411 | System.out.println("distanta: " +GPSutil.distance(p1, p2));*/ |
---|
412 | System.out.println("__________________end init____________________________\n"); |
---|
413 | |
---|
414 | |
---|
415 | } |
---|
416 | |
---|
417 | /** |
---|
418 | * Reset simulation |
---|
419 | */ |
---|
420 | public void reset() |
---|
421 | { |
---|
422 | eventQueue = new Vector<Event>(); |
---|
423 | cars = new ArrayList<SimulatedCarInfo>(); |
---|
424 | |
---|
425 | //Petroaca - if DSRC simulation also reset the emergency channel |
---|
426 | if(Engine.simulationProtocol==Globals.PROTOCOL_DSRC) |
---|
427 | eventQueueEmergency = new ArrayList<Event>(); |
---|
428 | |
---|
429 | crtTime = 0; |
---|
430 | |
---|
431 | } |
---|
432 | |
---|
433 | /** |
---|
434 | * Turn on communication by scheduling transmission events for each car. |
---|
435 | * |
---|
436 | */ |
---|
437 | public void restartCommunication() { |
---|
438 | disableComm = false; |
---|
439 | synchronized (eventQueue) { |
---|
440 | // eventQueue.add(new CleanupEvent(crtTime+1)); |
---|
441 | |
---|
442 | schedEvent(new CleanupEvent(crtTime + 1)); |
---|
443 | |
---|
444 | synchronized (cars) |
---|
445 | { |
---|
446 | Iterator<SimulatedCarInfo> it = cars.iterator(); |
---|
447 | while (it.hasNext()) { |
---|
448 | SimulatedCarInfo r = it.next(); |
---|
449 | schedEvent(new SendEvent(crtTime + 2, r, SimulatedCarInfo.STANDARD_MESSAGE_ID)); |
---|
450 | } |
---|
451 | } |
---|
452 | } |
---|
453 | |
---|
454 | // Petroaca - if DSRC simulation also reset the emergency channel |
---|
455 | if(Engine.simulationProtocol==Globals.PROTOCOL_DSRC) |
---|
456 | eventQueueEmergency = new ArrayList<Event>(); |
---|
457 | } |
---|
458 | |
---|
459 | /** |
---|
460 | * Start simulation, execute events from queue continously |
---|
461 | * |
---|
462 | */ |
---|
463 | public void play() { |
---|
464 | // supposed to be in the init() |
---|
465 | // between init and play() Globals.map.allIntersections is changing |
---|
466 | // need to find a fix |
---|
467 | |
---|
468 | if (Engine.simulationType == RoutingConstants.DYNAMIC_CITY_ROUTE) { |
---|
469 | CityTrafficLight.upgradeToCityTL(); |
---|
470 | RoadSegmentCost.initRoadSegmentsCost(); |
---|
471 | this.infrastructure=new ArrayList<SimulatedCarInfo>(); |
---|
472 | //create a fixed node for every intersection |
---|
473 | IntersectionNode.createIntersectionNodes(server); |
---|
474 | server.init(); |
---|
475 | } |
---|
476 | |
---|
477 | prevRealSec = t0 = System.currentTimeMillis(); |
---|
478 | pauseFlag = false; |
---|
479 | while (!pauseFlag && !doneFlag) { |
---|
480 | step(); |
---|
481 | } |
---|
482 | } |
---|
483 | |
---|
484 | /** |
---|
485 | * Execute all the events for the following <seconds> s |
---|
486 | * |
---|
487 | */ |
---|
488 | public void play(int seconds) { |
---|
489 | int frames = seconds * fps; |
---|
490 | pauseFlag = false; |
---|
491 | for (int i = 0; i < frames && !pauseFlag && !doneFlag; i++) { |
---|
492 | step(); |
---|
493 | } |
---|
494 | } |
---|
495 | |
---|
496 | /** |
---|
497 | * Request the interruption of the continous execution of events |
---|
498 | * |
---|
499 | */ |
---|
500 | public void pause() { |
---|
501 | pauseFlag = true; |
---|
502 | } |
---|
503 | |
---|
504 | /** |
---|
505 | * Execute all the events for the current moment of time (virtual time) |
---|
506 | * |
---|
507 | */ |
---|
508 | double dec1(double x){ |
---|
509 | return (double)((int)(x * 10)) / 10; |
---|
510 | } |
---|
511 | |
---|
512 | |
---|
513 | long lastTime = -1; |
---|
514 | |
---|
515 | |
---|
516 | |
---|
517 | |
---|
518 | ////////////////////////////////////// step /////////////////////////////////////////////////////////// |
---|
519 | /** |
---|
520 | * Process the events for the current step of the simulation |
---|
521 | * |
---|
522 | */ |
---|
523 | |
---|
524 | |
---|
525 | /** |
---|
526 | * PTVS |
---|
527 | */ |
---|
528 | |
---|
529 | |
---|
530 | public void step() { |
---|
531 | |
---|
532 | int cnt = 0; |
---|
533 | |
---|
534 | if(Globals.SOCIALNETWORK == 1){ |
---|
535 | double sec = vnsim.socialNet.GlobalNetwork.nextReconfigurationTime*Globals.SECOND; |
---|
536 | } |
---|
537 | |
---|
538 | if (crtTime > 3.5 * Globals.HOUR){ //nu rulez mai mult de 3 ore juma |
---|
539 | doneFlag = true; |
---|
540 | System.exit(0); |
---|
541 | return; |
---|
542 | } |
---|
543 | if (withGUI) { |
---|
544 | synchronized (Globals.mutex) { |
---|
545 | if (Globals.flag == 1) { |
---|
546 | try { |
---|
547 | Globals.mutex.wait(); |
---|
548 | } catch (Exception e) { |
---|
549 | |
---|
550 | } |
---|
551 | } |
---|
552 | Globals.flag = 0; |
---|
553 | } |
---|
554 | } |
---|
555 | |
---|
556 | long t1 = System.currentTimeMillis(); |
---|
557 | if (crtTime % Globals.SECOND == 0) |
---|
558 | { |
---|
559 | lastSimSecond = (double)(t1-prevRealSec)/1000; |
---|
560 | prevRealSec = t1; |
---|
561 | if (lastSimSecond < 1) |
---|
562 | try{ |
---|
563 | Thread.sleep(100);//(int)((1.0 - lastSimSecond)*1000)); |
---|
564 | }catch(Exception e){ |
---|
565 | |
---|
566 | } |
---|
567 | } |
---|
568 | |
---|
569 | if (crtTime % Globals.MINUTE == 0){ |
---|
570 | |
---|
571 | Globals.packets = 0; |
---|
572 | Globals.collided = 0; |
---|
573 | Globals.lost = 0; |
---|
574 | |
---|
575 | System.out.println((crtTime / Globals.MINUTE) + " sim min;\t" + |
---|
576 | "simulation time: "+ (System.currentTimeMillis() - t0) +"\t" + |
---|
577 | " cars: "+cars.size() + " created " + created); |
---|
578 | |
---|
579 | Globals.volume = 0; |
---|
580 | Globals.demand = 0; |
---|
581 | |
---|
582 | //Petroaca |
---|
583 | if(Engine.simulationProtocol==Globals.PROTOCOL_DSRC) |
---|
584 | { |
---|
585 | DSRCStatistics.addStatistic(new DSRCStatisticComponent(0,Globals.DSRC_PACKETS_LOST_WEAK,Globals.DSRC_PACKETS_TOTAL,Globals.DSRC_PACKETS_LOST_CORRUPTED,Globals.DSRC_PACKETS_LOST_COLLISION,Globals.DSRC_PACKETS_LOST_TX,Globals.DSRC_PACKETS_LOST_RX,Globals.DSRC_PACKETS_LOST_PER,Globals.DSRC_PACKETS_RECEIVED_OK |
---|
586 | ,0,0,Globals.CUMMULATIVE_PACKETS_FV,Globals.CUMMULATIVE_PACKETS_NFV,Globals.CUMMULATIVE_PACKETS_LAV,Globals.CUMMULATIVE_PACKETS_RAV,Globals.IRT_FV,Globals.IRT_NFV,Globals.IRT_LAV,Globals.IRT_RAV,0,0,0,0,cars.size(),Globals.EMERGENCY_RECEIVED,Globals.EMERGENCY_SENT)); |
---|
587 | |
---|
588 | |
---|
589 | Globals.DSRC_PACKETS_LOST_WEAK=0; |
---|
590 | Globals.DSRC_PACKETS_LOST_COLLISION=0; |
---|
591 | Globals.DSRC_PACKETS_LOST_TX=0; |
---|
592 | Globals.DSRC_PACKETS_LOST_RX=0; |
---|
593 | Globals.DSRC_PACKETS_LOST_PER=0; |
---|
594 | Globals.DSRC_PACKETS_LOST_CORRUPTED=0; |
---|
595 | Globals.DSRC_PACKETS_RECEIVED_OK=0; |
---|
596 | Globals.DSRC_PACKETS_TOTAL=0; |
---|
597 | Globals.DSRC_PACKETS_SEND_FALIURE_RX=0; |
---|
598 | Globals.DSRC_PACKETS_SEND_FALIURE_NOISE=0; |
---|
599 | Globals.DSRC_SEND=0; |
---|
600 | |
---|
601 | //System.out.println((crtTime / Globals.MINUTE)+ " min\t"+"FV:"+Globals.CUMMULATIVE_PACKETS_FV+" NFV:"+Globals.CUMMULATIVE_PACKETS_NFV+" LAV:"+Globals.CUMMULATIVE_PACKETS_LAV+" RAV:"+Globals.CUMMULATIVE_PACKETS_RAV+" EMERGENCY_RECV:"+Globals.EMERGENCY_RECEIVED+" EMERGENCY_SENT:"+Globals.EMERGENCY_SENT); |
---|
602 | |
---|
603 | Globals.CUMMULATIVE_PACKETS_RAV=0; |
---|
604 | Globals.CUMMULATIVE_PACKETS_LAV=0; |
---|
605 | Globals.CUMMULATIVE_PACKETS_FV=0; |
---|
606 | Globals.CUMMULATIVE_PACKETS_NFV=0; |
---|
607 | Globals.IRT_FV=0; |
---|
608 | Globals.IRT_NFV=0; |
---|
609 | Globals.IRT_LAV=0; |
---|
610 | Globals.IRT_RAV=0; |
---|
611 | Globals.EMERGENCY_RECEIVED=0; |
---|
612 | Globals.EMERGENCY_SENT=0; |
---|
613 | |
---|
614 | /*System.out.println("END DSRC DATA------------------------------");*/ |
---|
615 | } |
---|
616 | } |
---|
617 | if (crtTime % (5 * Globals.MINUTE) == 0){ |
---|
618 | if (lastTime == -1) |
---|
619 | lastTime = t0; |
---|
620 | long realTime = t1 - lastTime; |
---|
621 | lastTime = t1; |
---|
622 | |
---|
623 | eventsTime = 0; |
---|
624 | codeTime = 0; |
---|
625 | cleanupTime = 0; |
---|
626 | mobilityTime = 0; |
---|
627 | |
---|
628 | Globals.pw.flush(); |
---|
629 | |
---|
630 | Globals.demo.st.clearInfo(); |
---|
631 | Globals.demo.st.addInfo("Avg Delay "+ dec1(totalControlDelay/(fincars*60))+" min"); |
---|
632 | Globals.demo.st.addInfo("Max Delay "+dec1(maxControlDelay / 60)+" min"); |
---|
633 | Globals.demo.st.addInfo("Fuel: " + dec1( ( totalEM.fc * 100) / (1000 * totalkm)) + " L/100 km"); |
---|
634 | Globals.demo.st.addInfo("CO2: " + dec1((totalEM.co2 * 20 )/ 1000)+" kg/h"); |
---|
635 | Globals.demo.st.addInfo("CO: " + dec1((totalEM.co * 20 )/ 1000)+" kg/h"); |
---|
636 | Globals.demo.st.addInfo("HC: " + dec1((totalEM.hc * 20 )/ 1000)+" kg/h"); |
---|
637 | Globals.demo.st.addInfo("NOx: " + dec1((totalEM.nox * 20 )/ 1000)+" kg/h"); |
---|
638 | |
---|
639 | |
---|
640 | if (fincars>0) { |
---|
641 | Globals.pwxc.println( "min " + (crtTime / Globals.MINUTE) + |
---|
642 | " cars: " + fincars + |
---|
643 | " total time: " + totalTime + |
---|
644 | " total km: " + totalkm + |
---|
645 | " time/car: " + totalTime / fincars + |
---|
646 | " L/100: "+ ( (totalEM.fc * 100) / ( 1000 * totalkm)) + |
---|
647 | " fc: " + (totalEM.fc / fincars) + |
---|
648 | " co2: " + (totalEM.co2 / fincars) + |
---|
649 | " co: " + (totalEM.co / fincars) + |
---|
650 | " hc: " + (totalEM.hc / fincars) + |
---|
651 | " nox: " + (totalEM.nox / fincars) |
---|
652 | ); |
---|
653 | Globals.pwxc.flush(); |
---|
654 | } |
---|
655 | } |
---|
656 | |
---|
657 | |
---|
658 | if (startedWithGUI) { |
---|
659 | Globals.demo.st.setCount(crtTime); |
---|
660 | Globals.demo.st.setCurrentTime(System.currentTimeMillis() - t0); |
---|
661 | Globals.demo.st.setSimulationTime((float) ((crtTime * 1000) / fps)); |
---|
662 | //Globals.demo.st.setSemTime(mycnt); |
---|
663 | Globals.demo.st.setNrEvents(eventQueue.size() + nrEvThreads); |
---|
664 | Globals.demo.st.setCarsNo(cars.size()); |
---|
665 | } |
---|
666 | |
---|
667 | long t2 = System.currentTimeMillis(); |
---|
668 | |
---|
669 | // Petroaca - if DSRC simualtion play the events in emergency channel as well |
---|
670 | |
---|
671 | if(Engine.simulationProtocol==Globals.PROTOCOL_DSRC && Globals.MULTI_CHANNEL) |
---|
672 | { |
---|
673 | while (true) { |
---|
674 | if (eventQueueEmergency.size() == 0) { |
---|
675 | doneFlagEmergency = true; |
---|
676 | break; |
---|
677 | } |
---|
678 | |
---|
679 | Event e = (Event) eventQueueEmergency.get(0); |
---|
680 | if (e.getTime() == crtTime) { |
---|
681 | playEventEmergency(); |
---|
682 | //cnt++; |
---|
683 | } else |
---|
684 | break; |
---|
685 | } |
---|
686 | } |
---|
687 | |
---|
688 | /*while (true) { |
---|
689 | if (eventQueue.size() == 0) { |
---|
690 | doneFlag = true; |
---|
691 | return; |
---|
692 | } |
---|
693 | |
---|
694 | Event e = (Event) eventQueue.get(0); |
---|
695 | if (e.getTime() == crtTime) { |
---|
696 | playEvent(null); |
---|
697 | cnt++; |
---|
698 | } else |
---|
699 | break; |
---|
700 | }*/ |
---|
701 | |
---|
702 | |
---|
703 | /** |
---|
704 | * PTVS Project |
---|
705 | * we know how many intersections i have, to start as many threads as intersections i have |
---|
706 | */ |
---|
707 | Map map = Globals.map; |
---|
708 | ArrayList<Intersection> intersectii = map.allIntersections; |
---|
709 | numThreads=intersectii.size(); |
---|
710 | numThreads = Globals.NO_FEP_PROC - 1; |
---|
711 | int noRoad = 0; |
---|
712 | |
---|
713 | |
---|
714 | while (true) { |
---|
715 | |
---|
716 | |
---|
717 | if (eventQueue.size() == 0) { |
---|
718 | doneFlag = true; |
---|
719 | System.out.println("\ndoneFlag = true "); |
---|
720 | return; |
---|
721 | } |
---|
722 | |
---|
723 | // extrag un eveniment din coada !!!!! acum doar il extrag.. cand intru in playEvent il si scot cu remove(0) |
---|
724 | Event e = (Event) eventQueue.get(0); |
---|
725 | if (e == null) continue; |
---|
726 | |
---|
727 | if (e.getTime() == crtTime) { |
---|
728 | //System.out.println("------------->> e.getTime() == crtTime== "+crtTime); |
---|
729 | |
---|
730 | } |
---|
731 | |
---|
732 | if(dimens!=eventQueue.size()){ |
---|
733 | |
---|
734 | System.out.println("\n----------- number of events in the queue:" +eventQueue.size()); |
---|
735 | dimens = eventQueue.size(); |
---|
736 | } |
---|
737 | |
---|
738 | |
---|
739 | /** |
---|
740 | * PTVS Project |
---|
741 | * |
---|
742 | * we have an array of threads for processing events based on location (intersection) |
---|
743 | * i process only receive and send messages |
---|
744 | */ |
---|
745 | |
---|
746 | |
---|
747 | if((e instanceof SendEvent) || (e instanceof ReceiveEvent)){ |
---|
748 | |
---|
749 | synchronized (eventQueue) { |
---|
750 | e = eventQueue.remove(0); |
---|
751 | nrEvThreads++; |
---|
752 | //System.out.println("remove from eventQueue"); |
---|
753 | } |
---|
754 | |
---|
755 | /** |
---|
756 | * PTVS Project |
---|
757 | * i should know the location of the event |
---|
758 | */ |
---|
759 | Road r = null; |
---|
760 | Point p = null; |
---|
761 | |
---|
762 | if(e instanceof SendEvent){ |
---|
763 | SendEvent se=(SendEvent)e; |
---|
764 | r = (Road) Globals.map.roads.get(se.sender.getRoadIdx()); //aflu strada pe care se afla senderul |
---|
765 | p = (Point) r.points.get(se.sender.getPointIdx()); //aflu punctul unde se afla senderul |
---|
766 | |
---|
767 | noRoad = se.sender.getRoadIdx(); |
---|
768 | } |
---|
769 | //!!!aici era o greseala, in loc de receiver era sender, am modificat |
---|
770 | if(e instanceof ReceiveEvent){ |
---|
771 | ReceiveEvent re=(ReceiveEvent)e; |
---|
772 | r = (Road) Globals.map.roads.get(re.receiver.getRoadIdx()); |
---|
773 | p = (Point) r.points.get(re.receiver.getPointIdx()); |
---|
774 | |
---|
775 | noRoad = re.receiver.getRoadIdx(); |
---|
776 | } |
---|
777 | |
---|
778 | //System.out.println("p:"+p); |
---|
779 | |
---|
780 | /** |
---|
781 | * PTVS Project |
---|
782 | * from intersections array, i find the closest one |
---|
783 | */ |
---|
784 | double MIN_DIST=Double.MAX_VALUE; |
---|
785 | int indexClosestIntersection=0; |
---|
786 | double distanta; |
---|
787 | for(int i=0;i<intersectii.size();i++){ |
---|
788 | Intersection intersectie = intersectii.get(i); |
---|
789 | if(r.getPoints().contains(intersectie.getMapPoint())){ //iau in calcul doar intersectiile aflate pe strada pe care sunt |
---|
790 | distanta=GPSutil.distance(p, intersectie.getMapPoint()); |
---|
791 | if (distanta < MIN_DIST){ |
---|
792 | MIN_DIST=distanta; |
---|
793 | indexClosestIntersection=i; |
---|
794 | } |
---|
795 | } |
---|
796 | |
---|
797 | } |
---|
798 | |
---|
799 | /** |
---|
800 | * PTVS Project |
---|
801 | * i will assign a thread for the event |
---|
802 | */ |
---|
803 | |
---|
804 | |
---|
805 | if (e.getTime() == crtTime) { |
---|
806 | |
---|
807 | if(e instanceof SendEvent){ |
---|
808 | |
---|
809 | System.out.print("SendEvent: "); |
---|
810 | //processingThreads[indexClosestIntersection] = new SendEventProcessingThread(e,this); |
---|
811 | } |
---|
812 | else { |
---|
813 | System.out.print("ReceiveEvent: "); |
---|
814 | //processingThreads[indexClosestIntersection] = new ReceiveEventProcessingThread(e,this); |
---|
815 | } |
---|
816 | |
---|
817 | System.out.println("Processing thread "+indexClosestIntersection+" from intersection "+intersectii.get(indexClosestIntersection)); |
---|
818 | |
---|
819 | try { |
---|
820 | //v3- with intersections |
---|
821 | //int indexThread = indexClosestIntersection % (Globals.NO_FEP_PROC - 1); |
---|
822 | |
---|
823 | //v2 - with roads |
---|
824 | int indexThread = noRoad % (Globals.NO_FEP_PROC - 1); |
---|
825 | start(e, indexThread); |
---|
826 | cnt++; |
---|
827 | |
---|
828 | } catch (IOException e1) { |
---|
829 | System.out.println("exception on assigning event for thread"); |
---|
830 | e1.printStackTrace(); |
---|
831 | } |
---|
832 | |
---|
833 | //new Thread(processingThreads[indexClosestIntersection]).start(); |
---|
834 | |
---|
835 | } |
---|
836 | else |
---|
837 | break; |
---|
838 | } |
---|
839 | |
---|
840 | else if (e.getTime() == crtTime) { |
---|
841 | System.out.println("OtherEvent "); |
---|
842 | playEvent(null); |
---|
843 | cnt++; |
---|
844 | |
---|
845 | } else |
---|
846 | break; |
---|
847 | |
---|
848 | |
---|
849 | } |
---|
850 | |
---|
851 | ///////////////////////end PTVS Project///////////////////////////////////////////////////// |
---|
852 | |
---|
853 | //increment simulation time |
---|
854 | crtTime++; |
---|
855 | //System.out.println("Increment crtTime! crtTime:"+crtTime); |
---|
856 | |
---|
857 | long t3 = System.currentTimeMillis(); |
---|
858 | if (crtTime % (Globals.executionFPS / Globals.FPS) == 0) |
---|
859 | if (!runtimeMobility) |
---|
860 | readScenarioPhase(); |
---|
861 | else |
---|
862 | generateScenarioPhase(); |
---|
863 | |
---|
864 | checkCreateNewCars(); |
---|
865 | |
---|
866 | long t4 = System.currentTimeMillis(); |
---|
867 | eventsTime += t3 - t2; |
---|
868 | mobilityTime += t4 - t3; |
---|
869 | |
---|
870 | if (Engine.simulationType == RoutingConstants.DYNAMIC_INFRASTRUCTURE_ROUTE) { |
---|
871 | if (crtTime % 100 == 0) { |
---|
872 | for (int i = 0; i < this.infrastructure.size(); i++) { |
---|
873 | |
---|
874 | ((InfrastructureNode) this.infrastructure.get(i)).updateDelays(); |
---|
875 | } |
---|
876 | } |
---|
877 | } |
---|
878 | if (Engine.simulationType == RoutingConstants.DYNAMIC_CITY_ROUTE) { |
---|
879 | for (int i = 0; i < this.infrastructure.size(); i++) { |
---|
880 | ((IntersectionNode) infrastructure.get(i)).step(crtTime); |
---|
881 | } |
---|
882 | server.step(crtTime); |
---|
883 | } |
---|
884 | } |
---|
885 | //////////////////////////////////////////////end metoda step//////////////////////////////////// |
---|
886 | |
---|
887 | static Random random = new Random(); |
---|
888 | |
---|
889 | public static int lastCarID = 0; |
---|
890 | |
---|
891 | /** |
---|
892 | * Insert new cars at the map entry points according to the scheduled flow. |
---|
893 | * |
---|
894 | */ |
---|
895 | private void checkCreateNewCars(){ |
---|
896 | |
---|
897 | int percent; |
---|
898 | // should create some new cars? |
---|
899 | NewCarEvent first=Mobility.events.first(); |
---|
900 | while(first!=null && first.timestamp==crtTime) { |
---|
901 | //create this one |
---|
902 | EntryScenario es=Mobility.entryScenarios.get(first.index); |
---|
903 | |
---|
904 | int roadIndexSrc=es.entry.roadIdx; |
---|
905 | int pointIndexSrc=es.entry.ptIdx; |
---|
906 | |
---|
907 | int ID=lastCarID; |
---|
908 | lastCarID++; |
---|
909 | |
---|
910 | //which exit is the car going towards? |
---|
911 | double rand; |
---|
912 | rand=(random.nextDouble())*100; |
---|
913 | int i=0; |
---|
914 | int sum=0; |
---|
915 | while(i<es.entryExits.size()) { |
---|
916 | sum+=es.entryExits.elementAt(i).percentOfFlow; |
---|
917 | if(rand<sum) { |
---|
918 | //this is the chosen exit |
---|
919 | break; |
---|
920 | } |
---|
921 | i++; |
---|
922 | } |
---|
923 | EntryExitScenario ees=null; |
---|
924 | if(i==es.entryExits.size()) { |
---|
925 | //last exit chosen |
---|
926 | ees=es.entryExits.lastElement(); |
---|
927 | } else |
---|
928 | ees=es.entryExits.elementAt(i); |
---|
929 | |
---|
930 | int roadIndexDst=ees.exit.roadIdx; |
---|
931 | int pointIndexDst=ees.exit.ptIdx; |
---|
932 | |
---|
933 | //which route is the car choosing? |
---|
934 | rand=(random.nextDouble())*100; |
---|
935 | i=0; |
---|
936 | sum=0; |
---|
937 | while(i<ees.percentsEachRoute.size()) { |
---|
938 | sum+=ees.percentsEachRoute.elementAt(i); |
---|
939 | if(rand<sum) { |
---|
940 | //this is the chosen route |
---|
941 | break; |
---|
942 | } |
---|
943 | i++; |
---|
944 | } |
---|
945 | Route r=null; |
---|
946 | if(i==ees.percentsEachRoute.size()) { |
---|
947 | //last route chosen |
---|
948 | r=ees.routes.lastElement(); |
---|
949 | } else |
---|
950 | r=ees.routes.elementAt(i); |
---|
951 | |
---|
952 | for(i=0;i<Globals.map.roads.get(roadIndexSrc).laneNo;i++) { |
---|
953 | //one vehicle on each lane |
---|
954 | |
---|
955 | Personality personality; |
---|
956 | rand=(random.nextDouble())*100; |
---|
957 | if(rand<es.percentsDriverTypes.elementAt(0)) |
---|
958 | { |
---|
959 | personality=new CalmPersonality(); |
---|
960 | } else if(rand<es.percentsDriverTypes.elementAt(0)+es.percentsDriverTypes.elementAt(1)) { |
---|
961 | personality=new RegularPersonality(); |
---|
962 | } else { |
---|
963 | personality=new AggresivePersonality(); |
---|
964 | } |
---|
965 | |
---|
966 | CarInstance car = null; |
---|
967 | if (simulationType == RoutingConstants.INITIALLY_PROVIDED_ROUTE || simulationType == RoutingConstants.ROUTING_SCFT) { |
---|
968 | |
---|
969 | car = Mobility.createNewCar(roadIndexSrc, |
---|
970 | pointIndexSrc, 0, i + 1, 0.0, personality, /*personality.getWantedSpeed(roadIndexSrc)*/ 0.0, ID, |
---|
971 | roadIndexDst, pointIndexDst, r.route); |
---|
972 | |
---|
973 | System.out.println("Finished creating new car "+car); |
---|
974 | |
---|
975 | double d =0; |
---|
976 | if(car!=null) |
---|
977 | d = car.getDistanceToCarInFront(); |
---|
978 | if (d < 10 && d!=0) { |
---|
979 | // closer than 5 meters --> do not create |
---|
980 | // System.out.println("Skipping creation..."); |
---|
981 | Globals.map.roads.get(car.getRoadIdx()).carsOnThisRoad.remove(car.ID); |
---|
982 | continue; |
---|
983 | } |
---|
984 | } |
---|
985 | |
---|
986 | if (Engine.simulationType == RoutingConstants.DYNAMIC_SELF_ROUTE) { |
---|
987 | System.out.println("Dynamic; we are here"); |
---|
988 | percent = random.nextInt(100); |
---|
989 | percent = (int) Math.abs((double) percent); |
---|
990 | if (percent <= Globals.routePlanConstants.percentBestRoute) { |
---|
991 | percent = 1; |
---|
992 | } else { |
---|
993 | percent = 0; |
---|
994 | } |
---|
995 | car = Mobility.createNewCar(roadIndexSrc, pointIndexSrc, 0, |
---|
996 | i + 1, 0.0, personality, 0.0, ID, roadIndexDst, |
---|
997 | pointIndexDst, r.route, percent); |
---|
998 | if (car == null) { |
---|
999 | |
---|
1000 | continue; |
---|
1001 | } |
---|
1002 | double d = car.getDistanceToCarInFront(); |
---|
1003 | if (d < 10) { |
---|
1004 | // closer than 10 meters --> do not create |
---|
1005 | |
---|
1006 | Globals.map.roads.get(car.getRoadIdx()).carsOnThisRoad.remove(car.ID); |
---|
1007 | continue; |
---|
1008 | } |
---|
1009 | |
---|
1010 | } |
---|
1011 | if (Engine.simulationType == RoutingConstants.DYNAMIC_INFRASTRUCTURE_ROUTE) { |
---|
1012 | |
---|
1013 | car = Mobility.createNewCarCommunicatingWithInfrastucture( |
---|
1014 | roadIndexSrc, pointIndexSrc, 0, i + 1, 0.0, |
---|
1015 | personality, 0.0, ID, roadIndexDst, pointIndexDst, |
---|
1016 | r.route); |
---|
1017 | if (car == null) { |
---|
1018 | |
---|
1019 | continue; |
---|
1020 | } |
---|
1021 | double d = car.getDistanceToCarInFront(); |
---|
1022 | if (d < 10) { |
---|
1023 | // closer than 10 meters --> do not create |
---|
1024 | Globals.map.roads.get(car.getRoadIdx()).carsOnThisRoad |
---|
1025 | .remove(car.ID); |
---|
1026 | |
---|
1027 | |
---|
1028 | continue; |
---|
1029 | } |
---|
1030 | |
---|
1031 | } |
---|
1032 | |
---|
1033 | //create new ezcab client (entity) |
---|
1034 | |
---|
1035 | if (Engine.simulationType == RoutingConstants.DYNAMIC_CITY_ROUTE) { |
---|
1036 | |
---|
1037 | car = Mobility.createNewCityCarCommunicatingWithInfrastucture( |
---|
1038 | roadIndexSrc, pointIndexSrc, 0, i + 1, 0.0, |
---|
1039 | personality, 0.0, ID, roadIndexDst, pointIndexDst, |
---|
1040 | r.route); |
---|
1041 | if (car == null) { |
---|
1042 | continue; |
---|
1043 | } |
---|
1044 | double d = car.getDistanceToCarInFront(); |
---|
1045 | if (d < 10) { |
---|
1046 | // closer than 10 meters --> do not create |
---|
1047 | Globals.map.roads.get(car.getRoadIdx()).carsOnThisRoad |
---|
1048 | .remove(car.ID); |
---|
1049 | |
---|
1050 | |
---|
1051 | continue; |
---|
1052 | } |
---|
1053 | |
---|
1054 | } |
---|
1055 | |
---|
1056 | ID = lastCarID; |
---|
1057 | lastCarID++; |
---|
1058 | this.addCar(car); |
---|
1059 | } |
---|
1060 | Mobility.events.removeFirst(); |
---|
1061 | |
---|
1062 | try { |
---|
1063 | EntryFlowVariation efv=Mobility.entryFlowVariations.get(first.index); |
---|
1064 | |
---|
1065 | if(efv.getFirstMom()<crtTime && efv.getFirstMom()!=-1) { |
---|
1066 | int newFlow=efv.getFirstValue(); |
---|
1067 | int old=first.period; |
---|
1068 | first.period=(int)((double)(3600 * Globals.SECOND) / newFlow); |
---|
1069 | System.out.print("TIME="+crtTime+"; CHANGING PERIOD FOR ENTRY="+first.index+" FROM: "+old+" TO: "+first.period); |
---|
1070 | efv.removeFirst(); |
---|
1071 | } |
---|
1072 | } catch (Exception ex) { |
---|
1073 | |
---|
1074 | } |
---|
1075 | if(Globals.SOCIALNETWORK == 0 ) |
---|
1076 | Mobility.events.addEvent(new NewCarEvent(first.timestamp |
---|
1077 | + first.period, first.index, first.period)); |
---|
1078 | first = Mobility.events.first(); |
---|
1079 | } |
---|
1080 | } |
---|
1081 | |
---|
1082 | ////////////////////////////////////////// playEvent //////////////////////////////////////////////////////////////// |
---|
1083 | |
---|
1084 | long messagesSize = 0; |
---|
1085 | long carInfosSize = 0; |
---|
1086 | /** |
---|
1087 | * Execute the following event in the eventQueue |
---|
1088 | * |
---|
1089 | */ |
---|
1090 | public void playEvent(Event ev) { |
---|
1091 | //public void playEvent() { |
---|
1092 | |
---|
1093 | //System.out.println(">> Thread:"+Thread.currentThread().getName()); |
---|
1094 | |
---|
1095 | if (eventQueue.size() == 0) { |
---|
1096 | doneFlag = true; |
---|
1097 | return; |
---|
1098 | } |
---|
1099 | |
---|
1100 | Event e = null; |
---|
1101 | if (ev == null) { |
---|
1102 | synchronized (eventQueue) { |
---|
1103 | e = eventQueue.remove(0); |
---|
1104 | //System.out.println("remove from eventQueue"); |
---|
1105 | } |
---|
1106 | } else { |
---|
1107 | e = ev; |
---|
1108 | synchronized (eventQueue) { nrEvThreads--; } |
---|
1109 | } |
---|
1110 | //System.out.print(e.toString()+", "); |
---|
1111 | //System.out.flush(); |
---|
1112 | |
---|
1113 | if (e instanceof GPSEvent) { //////////// eveniment GPS |
---|
1114 | |
---|
1115 | Iterator<SimulatedCarInfo> it = cars.iterator(); |
---|
1116 | while (it.hasNext()) { |
---|
1117 | SimulatedCarInfo r = it.next(); |
---|
1118 | r.update(); |
---|
1119 | } |
---|
1120 | GPSEvent newEvent = new GPSEvent(crtTime + fps); |
---|
1121 | schedEvent(newEvent); |
---|
1122 | } |
---|
1123 | if (e instanceof CleanupEvent) { //////////// eveniment CLEANUP |
---|
1124 | |
---|
1125 | long t1 = System.currentTimeMillis(); |
---|
1126 | long deleted = 0; |
---|
1127 | Iterator<SimulatedCarInfo> it = cars.iterator(); |
---|
1128 | while (it.hasNext()) { |
---|
1129 | SimulatedCarInfo r = it.next(); |
---|
1130 | synchronized (r.trafficDB) { |
---|
1131 | Iterator<CarInfo> j = r.trafficDB.iterator(); |
---|
1132 | while (j.hasNext()) { |
---|
1133 | CarInfo neighbor = j.next(); |
---|
1134 | if ( (crtTime - neighbor.getTimestamp() > Globals.NEIGHBOR_EXPIRES && neighbor.state != 3) || |
---|
1135 | (neighbor.state == 3 && crtTime - neighbor.getTimestamp() > Globals.PROMISCUOUS_NEIGHBOR_EXPIRES)){ |
---|
1136 | j.remove(); |
---|
1137 | } |
---|
1138 | } |
---|
1139 | } |
---|
1140 | carInfosSize += r.trafficDB.size(); |
---|
1141 | } |
---|
1142 | for (int j = 0; j < Globals.map.lightsIndices.size(); j++) { |
---|
1143 | WirelessTrafficLight wtl = (WirelessTrafficLight) Globals.map.allIntersections |
---|
1144 | .get(Globals.map.lightsIndices.get(j)); |
---|
1145 | |
---|
1146 | synchronized(wtl.trafficDB){ |
---|
1147 | Iterator<IntersectionCarRecord> wtlit = wtl.trafficDB.iterator(); |
---|
1148 | while (wtlit.hasNext()) { |
---|
1149 | IntersectionCarRecord icr = wtlit.next(); |
---|
1150 | if (!icr.isPassed() && crtTime - icr.getCar().getTimestamp() > Globals.HOUR |
---|
1151 | && icr.getCar().state != 3){ |
---|
1152 | icr.segment.cars.remove(icr); |
---|
1153 | wtlit.remove(); |
---|
1154 | deleted++; |
---|
1155 | } |
---|
1156 | if (icr.isPassed() && crtTime - icr.getCar().getTimestamp() > Globals.TRAFFICLIGHT_NEIGHBOR_EXPIRES){ |
---|
1157 | icr.segment.cars.remove(icr); |
---|
1158 | wtlit.remove(); |
---|
1159 | } |
---|
1160 | } |
---|
1161 | } |
---|
1162 | } |
---|
1163 | long t2 = System.currentTimeMillis(); |
---|
1164 | cleanupTime += t2 - t1; |
---|
1165 | if (!disableComm) { |
---|
1166 | CleanupEvent newCleanupEvent = new CleanupEvent(crtTime + fps); |
---|
1167 | schedEvent(newCleanupEvent); |
---|
1168 | } |
---|
1169 | messagesSize = 0; |
---|
1170 | } |
---|
1171 | |
---|
1172 | if (e instanceof SendEvent) { /////////////////////////////// eveniment SEND |
---|
1173 | |
---|
1174 | SendEvent se = (SendEvent) e; |
---|
1175 | Communicator sender = se.getSender(); |
---|
1176 | |
---|
1177 | long t1 = System.currentTimeMillis(); |
---|
1178 | |
---|
1179 | // senderul e un SimulatedCarInfo |
---|
1180 | |
---|
1181 | if (se.getMessage() == null){ |
---|
1182 | byte[] message = sender.prepareMessage(se.getMessageType()); |
---|
1183 | se.setMessage(message); |
---|
1184 | if (se.getMessage() == null){ |
---|
1185 | if (sender.isPromiscuousMode() && sender.isPeriodicalMessage(se.getMessageType()) && !disableComm) { |
---|
1186 | int eventPeriod = sender.getPeriod(se.getMessageType()); |
---|
1187 | if (eventPeriod != -1) |
---|
1188 | schedEvent(new SendEvent(crtTime + eventPeriod, sender, se.getMessageType())); |
---|
1189 | } |
---|
1190 | return; |
---|
1191 | } |
---|
1192 | messagesSize += message.length; |
---|
1193 | long t2 = System.currentTimeMillis(); |
---|
1194 | codeTime += t2 - t1; |
---|
1195 | |
---|
1196 | if (sender.isPeriodicalMessage(se.getMessageType()) && !disableComm) { |
---|
1197 | int eventPeriod = sender.getPeriod(se.getMessageType()); |
---|
1198 | if (eventPeriod != -1) |
---|
1199 | schedEvent(new SendEvent(crtTime + eventPeriod, sender, se.getMessageType())); |
---|
1200 | } |
---|
1201 | } |
---|
1202 | |
---|
1203 | //Petroaca - the DSRC impl |
---|
1204 | if(Engine.simulationProtocol==Globals.PROTOCOL_80211) |
---|
1205 | { |
---|
1206 | |
---|
1207 | if (!sender.mediumAvailable(crtTime)) { |
---|
1208 | e.setTime(crtTime + 1); |
---|
1209 | schedEvent(e); |
---|
1210 | |
---|
1211 | return; |
---|
1212 | } |
---|
1213 | |
---|
1214 | sender.getRadio().setRadioTxMode(); |
---|
1215 | simulateCommunication(se); |
---|
1216 | |
---|
1217 | } |
---|
1218 | else |
---|
1219 | { |
---|
1220 | if(sender instanceof CarRunningDSRC) |
---|
1221 | { |
---|
1222 | MAC80211 macs=((CarRunningDSRC)sender).getMac(); |
---|
1223 | WirelessPhy phys=((CarRunningDSRC)sender).getPhysical(); |
---|
1224 | |
---|
1225 | macs.refreshFrameTime(crtTime); |
---|
1226 | phys.refreshFrameTime(crtTime); |
---|
1227 | |
---|
1228 | if(macs.sendToPhysical(se.getMessage(),crtTime)!=null) |
---|
1229 | { |
---|
1230 | phys.sendToChannel(se.getMessage(),crtTime); |
---|
1231 | macs.setChannelState(Globals.CCA_BUSY); |
---|
1232 | simulateCommunication(se); |
---|
1233 | } |
---|
1234 | else |
---|
1235 | { |
---|
1236 | |
---|
1237 | e.setTime(crtTime+1); |
---|
1238 | schedEvent(e); |
---|
1239 | |
---|
1240 | } |
---|
1241 | return; |
---|
1242 | } |
---|
1243 | else |
---|
1244 | { |
---|
1245 | if (!sender.mediumAvailable(crtTime)) { |
---|
1246 | e.setTime(crtTime + 1); |
---|
1247 | schedEvent(e); |
---|
1248 | |
---|
1249 | return; |
---|
1250 | } |
---|
1251 | |
---|
1252 | sender.getRadio().setRadioTxMode(); |
---|
1253 | simulateCommunication(se); |
---|
1254 | } |
---|
1255 | } |
---|
1256 | } |
---|
1257 | |
---|
1258 | if (e instanceof ReceiveEvent) { //////////////////////// eveniment RECEIVE |
---|
1259 | ReceiveEvent re = (ReceiveEvent) e; |
---|
1260 | |
---|
1261 | long t1 = System.currentTimeMillis(); |
---|
1262 | |
---|
1263 | //Petroaca - the DSRC impl |
---|
1264 | if(Engine.simulationProtocol==Globals.PROTOCOL_80211){ |
---|
1265 | RadioDev rxradio = re.receiver.getRadio(); |
---|
1266 | ReceiveEvent goodSignal = rxradio.receive(re.signals); |
---|
1267 | re.receiver.removeReceiveEventForTime(re.getTime()); |
---|
1268 | |
---|
1269 | if (goodSignal != null){ |
---|
1270 | re.receiver.onReceive(goodSignal.getMessage(), goodSignal.getMessageObject(), goodSignal.sender); |
---|
1271 | } |
---|
1272 | } |
---|
1273 | else |
---|
1274 | { |
---|
1275 | if(re.receiver instanceof CarRunningDSRC) |
---|
1276 | { |
---|
1277 | MAC80211 macr=((CarRunningDSRC)re.receiver).getMac(); |
---|
1278 | WirelessPhy phyr=((CarRunningDSRC)re.receiver).getPhysical(); |
---|
1279 | Application appr=((CarRunningDSRC)re.receiver).getAppLayer(); |
---|
1280 | |
---|
1281 | if(Globals.DIFFERENTIAL_CW) |
---|
1282 | macr.recalculateCW(phyr.getTotalReceivedPackets(),crtTime); |
---|
1283 | |
---|
1284 | macr.refreshFrameTime(crtTime); |
---|
1285 | phyr.refreshFrameTime(crtTime); |
---|
1286 | appr.refreshApplication(crtTime); |
---|
1287 | |
---|
1288 | ReceiveEvent best=phyr.recvFromChannel(re.signals,crtTime); |
---|
1289 | re.receiver.removeReceiveEventForTime(re.getTime()); |
---|
1290 | |
---|
1291 | double sinr=phyr.getSINR(); |
---|
1292 | if(best!=null) |
---|
1293 | { |
---|
1294 | macr.setChannelState(Globals.CCA_BUSY); |
---|
1295 | |
---|
1296 | if(macr.recvFromPhysical(best,sinr,crtTime)!=null) |
---|
1297 | { |
---|
1298 | appr.ForwardCollisionWarning(best.getMessage(),((CarInfo)re.receiver)); |
---|
1299 | //appr.LaneChangeAssistance(best.getMessage(),((CarInfo)re.receiver)); |
---|
1300 | re.receiver.onReceive(best.getMessage(), best.getMessageObject(), best.sender); |
---|
1301 | } |
---|
1302 | } |
---|
1303 | } |
---|
1304 | else |
---|
1305 | { |
---|
1306 | RadioDev rxradio = re.receiver.getRadio(); |
---|
1307 | ReceiveEvent goodSignal = rxradio.receive(re.signals); |
---|
1308 | re.receiver.removeReceiveEventForTime(re.getTime()); |
---|
1309 | |
---|
1310 | if (goodSignal != null){ |
---|
1311 | re.receiver.onReceive(goodSignal.getMessage(), goodSignal.getMessageObject(), goodSignal.sender); |
---|
1312 | } |
---|
1313 | } |
---|
1314 | } |
---|
1315 | |
---|
1316 | long t2 = System.currentTimeMillis(); |
---|
1317 | codeTime += t2 - t1; |
---|
1318 | } |
---|
1319 | } |
---|
1320 | |
---|
1321 | |
---|
1322 | public void playEventEmergency() |
---|
1323 | { |
---|
1324 | if (eventQueueEmergency.size() == 0) { |
---|
1325 | doneFlagEmergency = true; |
---|
1326 | return; |
---|
1327 | } |
---|
1328 | |
---|
1329 | Event e = null; |
---|
1330 | synchronized (eventQueueEmergency) { |
---|
1331 | e = eventQueueEmergency.remove(0); |
---|
1332 | } |
---|
1333 | |
---|
1334 | if (e instanceof SendEvent) { |
---|
1335 | SendEvent se = (SendEvent) e; |
---|
1336 | Communicator sender = se.getSender(); |
---|
1337 | |
---|
1338 | long t1 = System.currentTimeMillis(); |
---|
1339 | |
---|
1340 | |
---|
1341 | if (se.getMessage() == null){ |
---|
1342 | byte[] message = sender.prepareMessage(se.getMessageType()); |
---|
1343 | se.setMessage(message); |
---|
1344 | if (se.getMessage() == null){ |
---|
1345 | // System.out.println("null message"+se.sender + " "+se.sender.getClass()); |
---|
1346 | if (sender.isPromiscuousMode() && sender.isPeriodicalMessage(se.getMessageType()) && !disableComm) { |
---|
1347 | int eventPeriod = sender.getPeriod(se.getMessageType()); |
---|
1348 | if (eventPeriod != -1) |
---|
1349 | schedEventEmergency(new SendEvent(crtTime + eventPeriod, sender, se.getMessageType())); |
---|
1350 | } |
---|
1351 | return; |
---|
1352 | } |
---|
1353 | messagesSize += message.length; |
---|
1354 | long t2 = System.currentTimeMillis(); |
---|
1355 | codeTime += t2 - t1; |
---|
1356 | |
---|
1357 | if (sender.isPeriodicalMessage(se.getMessageType()) && !disableComm) { |
---|
1358 | int eventPeriod = sender.getPeriod(se.getMessageType()); |
---|
1359 | if (eventPeriod != -1) |
---|
1360 | schedEventEmergency(new SendEvent(crtTime + eventPeriod, sender, se.getMessageType())); |
---|
1361 | } |
---|
1362 | } |
---|
1363 | |
---|
1364 | //Petroaca - the DSRC impl |
---|
1365 | if(sender instanceof CarRunningDSRC) |
---|
1366 | { |
---|
1367 | MAC80211 macs=((CarRunningDSRC)sender).getMac(); |
---|
1368 | WirelessPhy phys=((CarRunningDSRC)sender).getPhysical(); |
---|
1369 | |
---|
1370 | macs.refreshFrameTime(crtTime); |
---|
1371 | phys.refreshFrameTime(crtTime); |
---|
1372 | |
---|
1373 | if(macs.sendToPhysical(se.getMessage(),crtTime)!=null) |
---|
1374 | { |
---|
1375 | phys.sendToChannel(se.getMessage(),crtTime); |
---|
1376 | macs.setChannelState(Globals.CCA_BUSY); |
---|
1377 | simulateCommunicationEmergency(se); |
---|
1378 | } |
---|
1379 | else |
---|
1380 | { |
---|
1381 | |
---|
1382 | e.setTime(crtTime+1); |
---|
1383 | schedEventEmergency(e); |
---|
1384 | |
---|
1385 | } |
---|
1386 | return; |
---|
1387 | } |
---|
1388 | |
---|
1389 | if(sender instanceof CarRunningDSRC) |
---|
1390 | { |
---|
1391 | return; |
---|
1392 | } |
---|
1393 | else |
---|
1394 | { |
---|
1395 | return; |
---|
1396 | } |
---|
1397 | } |
---|
1398 | |
---|
1399 | if (e instanceof ReceiveEvent) { |
---|
1400 | ReceiveEvent re = (ReceiveEvent) e; |
---|
1401 | |
---|
1402 | long t1 = System.currentTimeMillis(); |
---|
1403 | |
---|
1404 | //Petroaca - the DSRC impl |
---|
1405 | |
---|
1406 | if(re.receiver instanceof CarRunningDSRC) |
---|
1407 | { |
---|
1408 | MAC80211 macr=((CarRunningDSRC)re.receiver).getMac(); |
---|
1409 | WirelessPhy phyr=((CarRunningDSRC)re.receiver).getPhysical(); |
---|
1410 | Application appr=((CarRunningDSRC)re.receiver).getAppLayer(); |
---|
1411 | appr.refreshApplication(crtTime); |
---|
1412 | |
---|
1413 | if(Globals.DIFFERENTIAL_CW) |
---|
1414 | macr.recalculateCW(phyr.getTotalReceivedPackets(),crtTime); |
---|
1415 | |
---|
1416 | macr.refreshFrameTime(crtTime); |
---|
1417 | phyr.refreshFrameTime(crtTime); |
---|
1418 | |
---|
1419 | ReceiveEvent best=phyr.recvFromChannel(re.signals,crtTime); |
---|
1420 | ((SimulatedCarInfo)re.receiver).removeReceiveEventEmergencyForTime(re.getTime()); |
---|
1421 | |
---|
1422 | double sinr=phyr.getSINR(); |
---|
1423 | if(best!=null) |
---|
1424 | { |
---|
1425 | macr.setChannelState(Globals.CCA_BUSY); |
---|
1426 | |
---|
1427 | if(macr.recvFromPhysical(best,sinr,crtTime)!=null) |
---|
1428 | { |
---|
1429 | //appr.ForwardCollisionWarning(best.getMessage(),((CarInfo)re.receiver)); |
---|
1430 | //appr.LaneChangeAssistance(best.getMessage(),((CarInfo)re.receiver)); |
---|
1431 | re.receiver.onReceive(best.getMessage(), best.getMessageObject(), best.sender); |
---|
1432 | } |
---|
1433 | } |
---|
1434 | } |
---|
1435 | long t2 = System.currentTimeMillis(); |
---|
1436 | codeTime += t2 - t1; |
---|
1437 | } |
---|
1438 | } |
---|
1439 | |
---|
1440 | /** |
---|
1441 | * Applications may schedule dynamically communication events using this method |
---|
1442 | * to insert new events in the queue. |
---|
1443 | * |
---|
1444 | * @param e Event to schedule |
---|
1445 | */ |
---|
1446 | public void schedEvent(Event e) { |
---|
1447 | |
---|
1448 | long t1 = System.currentTimeMillis(); |
---|
1449 | |
---|
1450 | if (e instanceof ReceiveEvent) { |
---|
1451 | ReceiveEvent re = (ReceiveEvent) e; |
---|
1452 | ReceiveEvent existing = re.receiver.getReceiveEventForTime(e.getTime()); |
---|
1453 | //getReceiveEventForTime returneaza evenimentele receive petrecute la momentul e.getTime() |
---|
1454 | |
---|
1455 | if (existing == null){ |
---|
1456 | re.receiver.addReceiveEventForTime(re); |
---|
1457 | }else{ |
---|
1458 | existing.signals.add(re); |
---|
1459 | return; |
---|
1460 | } |
---|
1461 | } |
---|
1462 | |
---|
1463 | // cauta evenimetul in coada, ii gaseste pozitia corecta in lista de evenimente si il adauga |
---|
1464 | // ce face de fapt? de ce cauta un eveniment si il readauga tot pe el in lista??? |
---|
1465 | int idx = Collections.binarySearch(eventQueue, e); |
---|
1466 | synchronized (eventQueue) { |
---|
1467 | if (idx >= 0) { |
---|
1468 | // advance till the end of the sequence of equal events |
---|
1469 | if (eventQueue.get(idx).equals(e)) |
---|
1470 | idx++; |
---|
1471 | eventQueue.add(idx, e); |
---|
1472 | //System.out.println("add in eventQueue.."); |
---|
1473 | } else { |
---|
1474 | eventQueue.add(-(idx + 1), e); |
---|
1475 | //System.out.println("add in eventQueue...."); |
---|
1476 | } |
---|
1477 | } |
---|
1478 | long t2 = System.currentTimeMillis(); |
---|
1479 | schedTime += t2 - t1; |
---|
1480 | } |
---|
1481 | |
---|
1482 | //Petroaca - the message scheduler for the emergency channel |
---|
1483 | public void schedEventEmergency(Event e) |
---|
1484 | { |
---|
1485 | long t1 = System.currentTimeMillis(); |
---|
1486 | |
---|
1487 | if (e instanceof ReceiveEvent) { |
---|
1488 | ReceiveEvent re = (ReceiveEvent) e; |
---|
1489 | ReceiveEvent existing = ((SimulatedCarInfo)re.receiver).getReceiveEventEmergencyForTime(e.getTime()); |
---|
1490 | if (existing == null){ |
---|
1491 | ((SimulatedCarInfo)re.receiver).addReceiveEventEmergencyForTime(re); |
---|
1492 | }else{ |
---|
1493 | existing.signals.add(re); |
---|
1494 | return; |
---|
1495 | } |
---|
1496 | } |
---|
1497 | |
---|
1498 | int idx = Collections.binarySearch(eventQueueEmergency, e); |
---|
1499 | synchronized (eventQueueEmergency) { |
---|
1500 | if (idx >= 0) { |
---|
1501 | // advance till the end of the sequence of equal events |
---|
1502 | if (eventQueueEmergency.get(idx).equals(e)) |
---|
1503 | idx++; |
---|
1504 | eventQueueEmergency.add(idx, e); |
---|
1505 | } else { |
---|
1506 | eventQueueEmergency.add(-(idx + 1), e); |
---|
1507 | } |
---|
1508 | } |
---|
1509 | long t2 = System.currentTimeMillis(); |
---|
1510 | schedTime += t2 - t1; |
---|
1511 | } |
---|
1512 | |
---|
1513 | /** |
---|
1514 | * Generate the receive events corresponding to one send event, for the cars |
---|
1515 | * in the wireless range of the sending car. |
---|
1516 | * |
---|
1517 | */ |
---|
1518 | public void simulateCommunication(SendEvent e) { |
---|
1519 | |
---|
1520 | Road r1 = null; |
---|
1521 | Point p1 = null; |
---|
1522 | r1 = (Road) Globals.map.roads.get(e.sender.getRoadIdx()); |
---|
1523 | p1 = (Point) r1.points.get(e.sender.getPointIdx()); |
---|
1524 | |
---|
1525 | if (e instanceof UnicastSendEvent) { |
---|
1526 | long t1 = System.currentTimeMillis(); |
---|
1527 | Iterator<SimulatedCarInfo> it = cars.iterator(); |
---|
1528 | ReceiveEvent re = null; |
---|
1529 | while (it.hasNext()) { |
---|
1530 | SimulatedCarInfo r = it.next(); |
---|
1531 | if (r.vehicleId == ((UnicastSendEvent) e).getReceiverId()) { |
---|
1532 | Road r2 = (Road) Globals.map.roads.get(r.getRealPos().getRoadIdx()); |
---|
1533 | Point p2 = (Point) r2.points.get(r.getRealPos().getPointIdx()); |
---|
1534 | |
---|
1535 | // MODIFIED BY CRISTI!!! |
---|
1536 | // ReceiveEvent er = new ReceiveEvent(e.getTime() + 1, r, e.getMessage()); |
---|
1537 | |
---|
1538 | re = new ReceiveEvent(e.getTime() + 1, r, e.getMessage(), e.getMessageObject(), e.getMessageType()); |
---|
1539 | re.setUnicast(true); |
---|
1540 | |
---|
1541 | schedEvent(re); |
---|
1542 | if (GPSutil.distance(p1, p2) < WIRELESS_RANGE) { |
---|
1543 | } else { |
---|
1544 | System.out.println("EROARE! DISTANCE="+ GPSutil.distance(p1, p2)); |
---|
1545 | } |
---|
1546 | // END MODIFIED!!! |
---|
1547 | return; |
---|
1548 | } |
---|
1549 | } |
---|
1550 | |
---|
1551 | |
---|
1552 | re.setSender(e.getSender()); |
---|
1553 | |
---|
1554 | schedEvent(re); |
---|
1555 | |
---|
1556 | long t2 = System.currentTimeMillis(); |
---|
1557 | netTime += t2 - t1; |
---|
1558 | |
---|
1559 | } else { |
---|
1560 | |
---|
1561 | long tmp = 0; |
---|
1562 | long t3 = System.currentTimeMillis(); |
---|
1563 | long t4; |
---|
1564 | |
---|
1565 | // re = new ReceiveEvent(e.getTime() + 1, e.getSender(), e.getMessage()); |
---|
1566 | |
---|
1567 | |
---|
1568 | int cnt3 = 0; |
---|
1569 | int pkcnt = 0; |
---|
1570 | PeanoKey pk = Globals.map.peanoKeys.get(p1.getPeanoKeyIdx()); |
---|
1571 | |
---|
1572 | ListIterator<PeanoKey> pkit = Globals.map.peanoKeys.listIterator(p1.getPeanoKeyIdx()); |
---|
1573 | PeanoKey maxpk = null; |
---|
1574 | |
---|
1575 | double wifirange; |
---|
1576 | if (e.sender instanceof WirelessTrafficLight){ |
---|
1577 | maxpk = GPSutil.getMaxSearchBoundPK(p1,Engine.EXTENDED_WIRELESS_RANGE); |
---|
1578 | wifirange = EXTENDED_WIRELESS_RANGE; |
---|
1579 | }else{ |
---|
1580 | wifirange = WIRELESS_RANGE; |
---|
1581 | maxpk = pk.getMaxpk(); |
---|
1582 | } |
---|
1583 | |
---|
1584 | PeanoKey pkx = null; |
---|
1585 | for (pkx = pkit.next(); pkit.hasNext(); pkx = pkit.next()) { |
---|
1586 | pkcnt++; |
---|
1587 | // long tc1 = System.currentTimeMillis(); |
---|
1588 | if (pkx.compareTo(maxpk) > 0) |
---|
1589 | break; |
---|
1590 | // long tc2 = System.currentTimeMillis(); |
---|
1591 | // comp += (tc2-tc1); |
---|
1592 | |
---|
1593 | if (pkx.getCars() != null) { |
---|
1594 | Road r2 = (Road) Globals.map.roads.get(pkx.getRoadIndex()); |
---|
1595 | Point p2 = (Point) r2.points.get(pkx.getPointIndex()); |
---|
1596 | if (GPSutil.distance(p1, p2) < 5 * wifirange) { |
---|
1597 | cnt3 += pkx.getCars().size(); |
---|
1598 | Iterator<SimulatedCarInfo> it = pkx.getCars().iterator(); |
---|
1599 | while (it.hasNext()) { |
---|
1600 | SimulatedCarInfo r = it.next(); |
---|
1601 | if (e.sender instanceof SimulatedCarInfo) |
---|
1602 | if (r.getVehicleId() == ((SimulatedCarInfo)e.sender).getVehicleId()) |
---|
1603 | continue; |
---|
1604 | r.setLastMediumTransmition(crtTime); |
---|
1605 | |
---|
1606 | ReceiveEvent re = new ReceiveEvent(e.getTime() + 1, r, e.getMessage(), e.getMessageObject(), e.getMessageType()); |
---|
1607 | re.setSender(e.getSender()); |
---|
1608 | schedEvent(re); |
---|
1609 | } |
---|
1610 | } |
---|
1611 | } |
---|
1612 | } |
---|
1613 | PeanoKey minpk = null; |
---|
1614 | if (e.sender instanceof WirelessTrafficLight) |
---|
1615 | minpk = GPSutil.getMaxSearchBoundPK(p1,-Engine.EXTENDED_WIRELESS_RANGE); |
---|
1616 | else |
---|
1617 | minpk = pk.getMinpk(); |
---|
1618 | |
---|
1619 | pkit = Globals.map.peanoKeys.listIterator(p1.getPeanoKeyIdx()); |
---|
1620 | |
---|
1621 | if (pkit.hasPrevious()){ |
---|
1622 | pkx = pkit.previous(); |
---|
1623 | } |
---|
1624 | for (; pkit.hasPrevious(); pkx = pkit.previous()) { |
---|
1625 | pkcnt++; |
---|
1626 | // long tc1 = System.currentTimeMillis(); |
---|
1627 | if (pkx.compareTo(minpk) < 0) { |
---|
1628 | break; |
---|
1629 | } |
---|
1630 | // long tc2 = System.currentTimeMillis(); |
---|
1631 | // comp += (tc2-tc1); |
---|
1632 | |
---|
1633 | if (pkx.getCars() != null) { |
---|
1634 | Road r2 = (Road) Globals.map.roads.get(pkx.getRoadIndex()); |
---|
1635 | Point p2 = (Point) r2.points.get(pkx.getPointIndex()); |
---|
1636 | double distance = GPSutil.distance(p1, p2); |
---|
1637 | if (distance < 5 * wifirange) { |
---|
1638 | Iterator<SimulatedCarInfo> it = pkx.getCars().iterator(); |
---|
1639 | cnt3 += pkx.getCars().size(); |
---|
1640 | while (it.hasNext()) { |
---|
1641 | SimulatedCarInfo r = it.next(); |
---|
1642 | |
---|
1643 | r.setLastMediumTransmition(crtTime); |
---|
1644 | |
---|
1645 | if (e.sender instanceof SimulatedCarInfo) |
---|
1646 | if (r.getVehicleId() == ((SimulatedCarInfo)e.sender).getVehicleId()) |
---|
1647 | continue; |
---|
1648 | ReceiveEvent re = new ReceiveEvent(e.getTime() + 1, r, e.getMessage(), e.getMessageObject(), e.getMessageType()); |
---|
1649 | re.setSender(e.getSender()); |
---|
1650 | schedEvent(re); |
---|
1651 | } |
---|
1652 | } |
---|
1653 | } |
---|
1654 | } |
---|
1655 | |
---|
1656 | for (int j = 0; j < Globals.map.lightsIndices.size(); j++) { |
---|
1657 | WirelessTrafficLight wtl = (WirelessTrafficLight) Globals.map.allIntersections |
---|
1658 | .get(Globals.map.lightsIndices.get(j)); |
---|
1659 | long t1sem = System.currentTimeMillis(); |
---|
1660 | |
---|
1661 | if (GPSutil.distance(p1, wtl.getMapPoint()) < 5 * WIRELESS_RANGE) { |
---|
1662 | wtl.setLastMediumTransmition(crtTime); |
---|
1663 | ReceiveEvent re = new ReceiveEvent(e.getTime() + 1, wtl, e.getMessage(), e.getMessageObject(), e.getMessageType()); |
---|
1664 | re.setSender(e.getSender()); |
---|
1665 | schedEvent(re); |
---|
1666 | } |
---|
1667 | long t2sem = System.currentTimeMillis(); |
---|
1668 | semTime += (t2sem - t1sem); |
---|
1669 | } |
---|
1670 | |
---|
1671 | t4 = System.currentTimeMillis(); |
---|
1672 | tmp += t4 - t3; |
---|
1673 | netTime += tmp; |
---|
1674 | } |
---|
1675 | } |
---|
1676 | |
---|
1677 | //Petroaca - the communication simulation for the emergency channel |
---|
1678 | public void simulateCommunicationEmergency(SendEvent e) { |
---|
1679 | |
---|
1680 | Road r1 = null; |
---|
1681 | Point p1 = null; |
---|
1682 | r1 = (Road) Globals.map.roads.get(e.sender.getRoadIdx()); |
---|
1683 | p1 = (Point) r1.points.get(e.sender.getPointIdx()); |
---|
1684 | |
---|
1685 | if (e instanceof UnicastSendEvent) { |
---|
1686 | long t1 = System.currentTimeMillis(); |
---|
1687 | Iterator<SimulatedCarInfo> it = cars.iterator(); |
---|
1688 | ReceiveEvent re = null; |
---|
1689 | while (it.hasNext()) { |
---|
1690 | SimulatedCarInfo r = it.next(); |
---|
1691 | if (r.vehicleId == ((UnicastSendEvent) e).getReceiverId()) { |
---|
1692 | Road r2 = (Road) Globals.map.roads.get(r.getRealPos() |
---|
1693 | .getRoadIdx()); |
---|
1694 | Point p2 = (Point) r2.points.get(r.getRealPos() |
---|
1695 | .getPointIdx()); |
---|
1696 | |
---|
1697 | // MODIFIED BY CRISTI!!! |
---|
1698 | // ReceiveEvent er = new ReceiveEvent(e.getTime() + 1, r, |
---|
1699 | // e.getMessage()); |
---|
1700 | |
---|
1701 | re = new ReceiveEvent(e.getTime() + 1, r, e.getMessage(), e.getMessageObject(), e.getMessageType()); |
---|
1702 | re.setUnicast(true); |
---|
1703 | |
---|
1704 | schedEventEmergency(re); |
---|
1705 | if (GPSutil.distance(p1, p2) < WIRELESS_RANGE) { |
---|
1706 | } else { |
---|
1707 | System.out.println("EROARE! DISTANCE=" |
---|
1708 | + GPSutil.distance(p1, p2)); |
---|
1709 | } |
---|
1710 | // END MODIFIED!!! |
---|
1711 | return; |
---|
1712 | } |
---|
1713 | } |
---|
1714 | |
---|
1715 | re.setSender(e.getSender()); |
---|
1716 | schedEventEmergency(re); |
---|
1717 | |
---|
1718 | long t2 = System.currentTimeMillis(); |
---|
1719 | netTime += t2 - t1; |
---|
1720 | |
---|
1721 | } else { |
---|
1722 | |
---|
1723 | long tmp = 0; |
---|
1724 | long t3 = System.currentTimeMillis(); |
---|
1725 | long t4; |
---|
1726 | |
---|
1727 | // re = new ReceiveEvent(e.getTime() + 1, e.getSender(), e |
---|
1728 | // .getMessage()); |
---|
1729 | |
---|
1730 | |
---|
1731 | int cnt3 = 0; |
---|
1732 | int pkcnt = 0; |
---|
1733 | PeanoKey pk = Globals.map.peanoKeys.get(p1.getPeanoKeyIdx()); |
---|
1734 | |
---|
1735 | ListIterator<PeanoKey> pkit = Globals.map.peanoKeys |
---|
1736 | .listIterator(p1.getPeanoKeyIdx()); |
---|
1737 | PeanoKey maxpk = null; |
---|
1738 | |
---|
1739 | double wifirange; |
---|
1740 | if (e.sender instanceof WirelessTrafficLight){ |
---|
1741 | maxpk = GPSutil.getMaxSearchBoundPK(p1,Engine.EXTENDED_WIRELESS_RANGE); |
---|
1742 | wifirange = EXTENDED_WIRELESS_RANGE; |
---|
1743 | }else{ |
---|
1744 | wifirange = WIRELESS_RANGE; |
---|
1745 | maxpk = pk.getMaxpk(); |
---|
1746 | } |
---|
1747 | PeanoKey pkx = null; |
---|
1748 | for (pkx = pkit.next(); pkit.hasNext(); pkx = pkit.next()) { |
---|
1749 | pkcnt++; |
---|
1750 | // long tc1 = System.currentTimeMillis(); |
---|
1751 | if (pkx.compareTo(maxpk) > 0) |
---|
1752 | break; |
---|
1753 | // long tc2 = System.currentTimeMillis(); |
---|
1754 | // comp += (tc2-tc1); |
---|
1755 | |
---|
1756 | if (pkx.getCars() != null) { |
---|
1757 | Road r2 = (Road) Globals.map.roads.get(pkx |
---|
1758 | .getRoadIndex()); |
---|
1759 | Point p2 = (Point) r2.points.get(pkx.getPointIndex()); |
---|
1760 | if (GPSutil.distance(p1, p2) < 5 * wifirange) { |
---|
1761 | cnt3 += pkx.getCars().size(); |
---|
1762 | Iterator<SimulatedCarInfo> it = pkx.getCars() |
---|
1763 | .iterator(); |
---|
1764 | while (it.hasNext()) { |
---|
1765 | SimulatedCarInfo r = it.next(); |
---|
1766 | if (e.sender instanceof SimulatedCarInfo) |
---|
1767 | if (r.getVehicleId() == ((SimulatedCarInfo)e.sender) |
---|
1768 | .getVehicleId()) |
---|
1769 | continue; |
---|
1770 | r.setLastMediumTransmition(crtTime); |
---|
1771 | |
---|
1772 | ReceiveEvent re = new ReceiveEvent(e.getTime() + 1, r, e.getMessage(), e.getMessageObject(), e.getMessageType()); |
---|
1773 | re.setSender(e.getSender()); |
---|
1774 | schedEventEmergency(re); |
---|
1775 | } |
---|
1776 | } |
---|
1777 | } |
---|
1778 | } |
---|
1779 | PeanoKey minpk = null; |
---|
1780 | if (e.sender instanceof WirelessTrafficLight) |
---|
1781 | minpk = GPSutil.getMaxSearchBoundPK(p1,-Engine.EXTENDED_WIRELESS_RANGE); |
---|
1782 | else |
---|
1783 | minpk = pk.getMinpk(); |
---|
1784 | |
---|
1785 | pkit = Globals.map.peanoKeys.listIterator(p1.getPeanoKeyIdx()); |
---|
1786 | |
---|
1787 | if (pkit.hasPrevious()){ |
---|
1788 | pkx = pkit.previous(); |
---|
1789 | } |
---|
1790 | for (; pkit.hasPrevious(); pkx = pkit |
---|
1791 | .previous()) { |
---|
1792 | pkcnt++; |
---|
1793 | // long tc1 = System.currentTimeMillis(); |
---|
1794 | if (pkx.compareTo(minpk) < 0) { |
---|
1795 | break; |
---|
1796 | } |
---|
1797 | // long tc2 = System.currentTimeMillis(); |
---|
1798 | // comp += (tc2-tc1); |
---|
1799 | |
---|
1800 | if (pkx.getCars() != null) { |
---|
1801 | Road r2 = (Road) Globals.map.roads.get(pkx |
---|
1802 | .getRoadIndex()); |
---|
1803 | Point p2 = (Point) r2.points.get(pkx.getPointIndex()); |
---|
1804 | double distance = GPSutil.distance(p1, p2); |
---|
1805 | if (distance < 5 * wifirange) { |
---|
1806 | Iterator<SimulatedCarInfo> it = pkx.getCars() |
---|
1807 | .iterator(); |
---|
1808 | cnt3 += pkx.getCars().size(); |
---|
1809 | while (it.hasNext()) { |
---|
1810 | SimulatedCarInfo r = it.next(); |
---|
1811 | r.setLastMediumTransmition(crtTime); |
---|
1812 | |
---|
1813 | if (e.sender instanceof SimulatedCarInfo) |
---|
1814 | if (r.getVehicleId() == ((SimulatedCarInfo)e.sender) |
---|
1815 | .getVehicleId()) |
---|
1816 | continue; |
---|
1817 | ReceiveEvent re = new ReceiveEvent(e.getTime() + 1, r, e.getMessage(), e.getMessageObject(), e.getMessageType()); |
---|
1818 | re.setSender(e.getSender()); |
---|
1819 | schedEventEmergency(re); |
---|
1820 | } |
---|
1821 | } |
---|
1822 | } |
---|
1823 | } |
---|
1824 | |
---|
1825 | t4 = System.currentTimeMillis(); |
---|
1826 | tmp += t4 - t3; |
---|
1827 | netTime += tmp; |
---|
1828 | } |
---|
1829 | } |
---|
1830 | |
---|
1831 | /** |
---|
1832 | * Adds a new car to the simulation. |
---|
1833 | * |
---|
1834 | * @param car The object that stores the physical behaviour of the vehicle to add |
---|
1835 | */ |
---|
1836 | public void addCar(CarInstance car) |
---|
1837 | { |
---|
1838 | if (car == null) |
---|
1839 | return; |
---|
1840 | |
---|
1841 | car.timestamp = crtTime; |
---|
1842 | created ++; |
---|
1843 | synchronized (cars) |
---|
1844 | { |
---|
1845 | SimulatedCarInfo dc = null; |
---|
1846 | if (crtTime == 0) { |
---|
1847 | // dc = new SimulatedCarInfo(car.ID, car.getLane(), |
---|
1848 | // car.getSpeed(), car.getRoadIdx(), car.getPointIdx(), |
---|
1849 | // car.getDirection(), car.getOffset()); |
---|
1850 | /* |
---|
1851 | dc = new CarRunningVITP(car.ID, car.getLane(), car.getSpeed(), |
---|
1852 | car.getRoadIdx(), car.getPointIdx(), |
---|
1853 | car.getDirection(), car.getOffset()); |
---|
1854 | */ |
---|
1855 | //EZcab car |
---|
1856 | if (applicationType == Globals.PROT_EZCAB){ |
---|
1857 | dc=new EzcabCar(car.ID, car.getLane(), car.getSpeed(), |
---|
1858 | car.getRoadIdx(), car.getPointIdx(), |
---|
1859 | car.getDirection(), car.getOffset()); |
---|
1860 | System.out.println("new ezcab car created"); |
---|
1861 | } |
---|
1862 | else if (simulationType == RoutingConstants.ROUTING_SCFT) |
---|
1863 | dc=new CarRunningSCFT(car.ID, car.getLane(), car.getSpeed(), |
---|
1864 | car.getRoadIdx(), car.getPointIdx(), |
---|
1865 | car.getDirection(), car.getOffset()); |
---|
1866 | else |
---|
1867 | dc=new CarRunningDSRC(car.ID, car.getLane(), car.getSpeed(), |
---|
1868 | car.getRoadIdx(), car.getPointIdx(), |
---|
1869 | car.getDirection(), car.getOffset()); |
---|
1870 | |
---|
1871 | dc.setTimestamp(crtTime); |
---|
1872 | dc.setRealPos(car); |
---|
1873 | cars.add(dc); |
---|
1874 | dc.init(); |
---|
1875 | } |
---|
1876 | else |
---|
1877 | { |
---|
1878 | int poz = cars.indexOf(new SimulatedCarInfo(car.ID)); |
---|
1879 | if (poz == -1) |
---|
1880 | { |
---|
1881 | /* |
---|
1882 | dc = new CarRunningVITP(car.ID, car.getLane(), car |
---|
1883 | .getSpeed(), car.getRoadIdx(), car.getPointIdx(), |
---|
1884 | car.getDirection(), car.getOffset()); |
---|
1885 | */ |
---|
1886 | //Ezcab |
---|
1887 | if (applicationType == Globals.PROT_EZCAB){ |
---|
1888 | if(System.currentTimeMillis() % 2 == 0){ |
---|
1889 | dc = new EzcabClient(car.ID, car.getLane(), 1.0, //a minimum speed |
---|
1890 | car.getRoadIdx(), car.getPointIdx(), |
---|
1891 | car.getDirection(), car.getOffset()); |
---|
1892 | |
---|
1893 | // dc=new EzcabCar(car.ID, car.getLane(), car.getSpeed(), |
---|
1894 | // car.getRoadIdx(), car.getPointIdx(), |
---|
1895 | // car.getDirection(), car.getOffset()); |
---|
1896 | //System.out.println("new client created"); |
---|
1897 | } |
---|
1898 | else { |
---|
1899 | dc=new EzcabCar(car.ID, car.getLane(), car.getSpeed(), |
---|
1900 | car.getRoadIdx(), car.getPointIdx(), |
---|
1901 | car.getDirection(), car.getOffset()); |
---|
1902 | |
---|
1903 | // dc = new EzcabClient(car.ID, car.getLane(), 1.0, //a minimum speed |
---|
1904 | // car.getRoadIdx(), car.getPointIdx(), |
---|
1905 | // car.getDirection(), car.getOffset()); |
---|
1906 | //System.out.println("new ezcab car created"); |
---|
1907 | } |
---|
1908 | } |
---|
1909 | else if (simulationType == RoutingConstants.ROUTING_SCFT) |
---|
1910 | dc=new CarRunningSCFT(car.ID, car.getLane(), car.getSpeed(), |
---|
1911 | car.getRoadIdx(), car.getPointIdx(), |
---|
1912 | car.getDirection(), car.getOffset()); |
---|
1913 | else |
---|
1914 | dc=new CarRunningDSRC(car.ID, car.getLane(), car.getSpeed(), |
---|
1915 | car.getRoadIdx(), car.getPointIdx(), |
---|
1916 | car.getDirection(), car.getOffset()); |
---|
1917 | // dc = new SimulatedCarInfo(car.ID, car.getLane(), car |
---|
1918 | // .getSpeed(), car.getRoadIdx(), car.getPointIdx(), |
---|
1919 | // car.getDirection(), car.getOffset()); |
---|
1920 | |
---|
1921 | dc.setTimestamp(crtTime); |
---|
1922 | dc.setRealPos(car); |
---|
1923 | int t = 1 + random.nextInt(fps / 2 - 5); |
---|
1924 | schedEvent(new SendEvent(crtTime + t, dc,SimulatedCarInfo.STANDARD_MESSAGE_ID)); |
---|
1925 | cars.add(dc); |
---|
1926 | dc.init(); |
---|
1927 | } |
---|
1928 | else |
---|
1929 | { |
---|
1930 | ((SimulatedCarInfo) cars.get(poz)).setRealPos(car); |
---|
1931 | } |
---|
1932 | } |
---|
1933 | } |
---|
1934 | } |
---|
1935 | |
---|
1936 | /* |
---|
1937 | * Read a scenario phase from a traces file. Update the vehicle's position |
---|
1938 | * for the current step. |
---|
1939 | * |
---|
1940 | */ |
---|
1941 | public void readScenarioPhase() { |
---|
1942 | int nrCars = 0; |
---|
1943 | try { |
---|
1944 | int vehId, rdIdx, ptIdx; |
---|
1945 | byte dir, lane, speed; |
---|
1946 | double offset; |
---|
1947 | nrCars = dis.readInt(); |
---|
1948 | // System.out.println(nrCars+" masini"); |
---|
1949 | |
---|
1950 | synchronized (cars) |
---|
1951 | { |
---|
1952 | for (int i = 0; i < nrCars; i++) |
---|
1953 | { |
---|
1954 | vehId = dis.readInt(); |
---|
1955 | rdIdx = dis.readInt(); |
---|
1956 | ptIdx = dis.readInt(); |
---|
1957 | offset = dis.readDouble(); |
---|
1958 | dir = (byte) dis.readInt(); |
---|
1959 | lane = (byte) dis.readInt(); |
---|
1960 | speed = (byte) dis.readDouble(); |
---|
1961 | |
---|
1962 | SimulatedCarInfo dc = null; |
---|
1963 | if (crtTime == 0) { |
---|
1964 | /* |
---|
1965 | dc = new CarRunningVITP(vehId, lane, speed, |
---|
1966 | (short) rdIdx, (short) ptIdx, dir, offset); |
---|
1967 | */ |
---|
1968 | //create ezcab car |
---|
1969 | if (applicationType == Globals.PROT_EZCAB){ |
---|
1970 | dc = new EzcabCar(vehId, lane, speed, |
---|
1971 | (short) rdIdx, (short) ptIdx, dir, offset); |
---|
1972 | System.out.println("new ezcab car"); |
---|
1973 | } |
---|
1974 | else if (simulationType == RoutingConstants.ROUTING_SCFT) |
---|
1975 | dc = new CarRunningSCFT(vehId, lane, speed, |
---|
1976 | (short) rdIdx, (short) ptIdx, dir, offset); |
---|
1977 | else |
---|
1978 | dc = new CarRunningDSRC(vehId, lane, speed, |
---|
1979 | (short) rdIdx, (short) ptIdx, dir, offset); |
---|
1980 | // dc = new SimulatedCarInfo(vehId, lane, speed, |
---|
1981 | // (short)rdIdx, (short)ptIdx, dir, offset); |
---|
1982 | dc.setTimestamp(crtTime); |
---|
1983 | dc.setRealPos(new CarInstance((CarInfo) dc)); |
---|
1984 | cars.add(dc); |
---|
1985 | dc.init(); |
---|
1986 | } else { |
---|
1987 | int poz = cars.indexOf(new SimulatedCarInfo(vehId)); |
---|
1988 | if (poz == -1) { |
---|
1989 | /* |
---|
1990 | dc = new CarRunningVITP(vehId, lane, speed, |
---|
1991 | (short) rdIdx, (short) ptIdx, dir, offset); |
---|
1992 | */ |
---|
1993 | //create new ezcab car or client |
---|
1994 | if (applicationType == Globals.PROT_EZCAB){ |
---|
1995 | if(System.currentTimeMillis() % 2 == 0){ |
---|
1996 | dc = new EzcabClient(vehId, lane, 1.0, //minimum allowed speed |
---|
1997 | (short) rdIdx, (short) ptIdx, dir, offset); |
---|
1998 | // dc = new EzcabCar(vehId, lane, speed, |
---|
1999 | // (short) rdIdx, (short) ptIdx, dir, offset); |
---|
2000 | //System.out.println("new ezcab client created"); |
---|
2001 | } |
---|
2002 | else{ |
---|
2003 | dc = new EzcabCar(vehId, lane, speed, |
---|
2004 | (short) rdIdx, (short) ptIdx, dir, offset); |
---|
2005 | // dc = new EzcabClient(vehId, lane, 1.0, //minimum allowed speed |
---|
2006 | // (short) rdIdx, (short) ptIdx, dir, offset); |
---|
2007 | //System.out.println("new ezcab car created"); |
---|
2008 | } |
---|
2009 | } |
---|
2010 | else if (simulationType == RoutingConstants.ROUTING_SCFT) |
---|
2011 | dc = new CarRunningSCFT(vehId, lane, speed, |
---|
2012 | (short) rdIdx, (short) ptIdx, dir, offset); |
---|
2013 | else |
---|
2014 | dc = new CarRunningDSRC(vehId, lane, speed, |
---|
2015 | (short) rdIdx, (short) ptIdx, dir, offset); |
---|
2016 | // dc = new SimulatedCarInfo(vehId, lane, speed, |
---|
2017 | // (short)rdIdx, (short)ptIdx, dir, offset); |
---|
2018 | dc.setTimestamp(crtTime); |
---|
2019 | dc.setRealPos(new CarInstance((CarInfo) dc)); |
---|
2020 | cars.add(dc); |
---|
2021 | dc.init(); |
---|
2022 | } else { |
---|
2023 | CarInstance ci = ((SimulatedCarInfo) cars.get(poz)) |
---|
2024 | .getRealPos(); |
---|
2025 | ci.deleteCarToPointMapping(); |
---|
2026 | ci.setSpeed((byte) speed); |
---|
2027 | ci.setRoadIdx((short) rdIdx); |
---|
2028 | ci.setPointIdx((short) ptIdx); |
---|
2029 | ci.setLane(lane); |
---|
2030 | ci.setDirection((byte) dir); |
---|
2031 | ci.setOffset(offset); |
---|
2032 | ci.setTimestamp(crtTime); |
---|
2033 | ci.updateCarToPointMapping(); |
---|
2034 | } |
---|
2035 | } |
---|
2036 | } |
---|
2037 | } |
---|
2038 | } catch (Exception e) { |
---|
2039 | System.err.println("Error reading scenario file"); |
---|
2040 | e.printStackTrace(); |
---|
2041 | } |
---|
2042 | } |
---|
2043 | |
---|
2044 | public static boolean outputToFile = false; |
---|
2045 | |
---|
2046 | public void generateScenarioPhase() { |
---|
2047 | |
---|
2048 | long t1sem = System.currentTimeMillis(); |
---|
2049 | if (crtTime % Globals.SECOND == 0){ |
---|
2050 | for (int j = 0; j < Globals.map.lightsIndices.size(); j++) { |
---|
2051 | IntersectionWithTrafficLights iwtl = (IntersectionWithTrafficLights) Globals.map.allIntersections |
---|
2052 | .get(Globals.map.lightsIndices.get(j)); |
---|
2053 | iwtl.step(crtTime); |
---|
2054 | } |
---|
2055 | } |
---|
2056 | long t2sem = System.currentTimeMillis(); |
---|
2057 | semTime += (t2sem - t1sem); |
---|
2058 | |
---|
2059 | int NCARS = 0; |
---|
2060 | |
---|
2061 | for (int j = 0; j < Globals.map.roads.size(); j++) { |
---|
2062 | |
---|
2063 | //Petroaca - random car on road to send emergency message |
---|
2064 | Random r; |
---|
2065 | int randId=0; |
---|
2066 | if(Engine.simulationProtocol==Globals.PROTOCOL_DSRC) |
---|
2067 | { |
---|
2068 | r=new Random(); |
---|
2069 | randId=r.nextInt(Globals.map.roads.get(j).carsOnThisRoad.size()+1); |
---|
2070 | |
---|
2071 | } |
---|
2072 | |
---|
2073 | SortedCarVector newVector = new SortedCarVector(); |
---|
2074 | for (int k = Globals.map.roads.get(j).carsOnThisRoad.size() - 1; k >= 0; k--) { |
---|
2075 | CarInstance car = Globals.map.roads.get(j).carsOnThisRoad |
---|
2076 | .get(k); |
---|
2077 | if (car.finished) { |
---|
2078 | |
---|
2079 | totalControlDelay += car.totalControlDelay; |
---|
2080 | fincars ++; |
---|
2081 | if (maxControlDelay < car.totalControlDelay) |
---|
2082 | maxControlDelay = car.totalControlDelay; |
---|
2083 | |
---|
2084 | synchronized (cars) { |
---|
2085 | if (car.startMonitor) { |
---|
2086 | crossedCars++; |
---|
2087 | totalEM.addEmissionsResults(car.em, 1); |
---|
2088 | totalkm += car.totalDistance - car.startMonitorDistance; |
---|
2089 | totalTime += (crtTime - car.startTime)/Globals.SECOND; |
---|
2090 | } |
---|
2091 | Globals.monitoredCars.remove(car.getSimulatedCarInfo()); |
---|
2092 | car.finish(); |
---|
2093 | car.deleteCarToPointMapping(); |
---|
2094 | cars.remove(car.getSimulatedCarInfo()); |
---|
2095 | } |
---|
2096 | continue; |
---|
2097 | } |
---|
2098 | try { |
---|
2099 | long t1 = System.currentTimeMillis(); |
---|
2100 | |
---|
2101 | synchronized (cars) { |
---|
2102 | car.deleteCarToPointMapping(); |
---|
2103 | car.move(); |
---|
2104 | |
---|
2105 | if (Engine.simulationType == RoutingConstants.DYNAMIC_SELF_ROUTE) { |
---|
2106 | QueryGeneration.generateQueryForCar(car); |
---|
2107 | } |
---|
2108 | |
---|
2109 | if (Engine.simulationType == RoutingConstants.DYNAMIC_INFRASTRUCTURE_ROUTE) { |
---|
2110 | GuidanceRequest.generateRequestToInfrastructure(car); |
---|
2111 | } |
---|
2112 | |
---|
2113 | if (Engine.simulationType == RoutingConstants.DYNAMIC_CITY_ROUTE) { |
---|
2114 | CarToIntersectionComm.updateMetrics(car); |
---|
2115 | } |
---|
2116 | |
---|
2117 | // Petroaca - emergency messages schedule |
---|
2118 | |
---|
2119 | if(Engine.simulationProtocol==Globals.PROTOCOL_DSRC && car.ID==randId && crtTime%50==0) |
---|
2120 | { |
---|
2121 | EmergencyRequest.generateEmergencyMessage(car); |
---|
2122 | } |
---|
2123 | |
---|
2124 | car.setTimestamp(crtTime); |
---|
2125 | |
---|
2126 | |
---|
2127 | |
---|
2128 | if (car.endMonitor){ |
---|
2129 | crossedCars ++; |
---|
2130 | totalEM.addEmissionsResults(car.em, 1); |
---|
2131 | totalkm += car.totalDistance - car.startMonitorDistance; |
---|
2132 | // System.out.println("Fuel: " + car.em.fc / 1000 +" "+ car.totalDistance + " "+car.em.fc * 100.0/ (10000 * car.totalDistance) + " "+(car.totalDistance * 3600.0) / (double)(car.travelTime / Globals.SECOND)); |
---|
2133 | car.endMonitor = false; |
---|
2134 | car.startMonitor = false; |
---|
2135 | } |
---|
2136 | |
---|
2137 | car.updateCarToPointMapping(); |
---|
2138 | |
---|
2139 | } |
---|
2140 | |
---|
2141 | long t2 = System.currentTimeMillis(); |
---|
2142 | moveTime += t2 - t1; |
---|
2143 | } catch (Exception ex) { |
---|
2144 | ex.printStackTrace(); |
---|
2145 | } |
---|
2146 | // if it has changed road, place it in the vector of its new |
---|
2147 | // road |
---|
2148 | if (car.getRoadIdx() != j) { |
---|
2149 | if (car.getRoadIdx() > j) |
---|
2150 | NCARS--; // it will be moved again when that road |
---|
2151 | // will be processed |
---|
2152 | Globals.map.roads.get(car.getRoadIdx()).carsOnThisRoad |
---|
2153 | .addCarInstance(car); |
---|
2154 | } else { |
---|
2155 | // it's still on the same road |
---|
2156 | newVector.addCarInstance(Globals.map.roads.get(j).carsOnThisRoad.get(k)); |
---|
2157 | } |
---|
2158 | NCARS++; |
---|
2159 | } |
---|
2160 | |
---|
2161 | // loop through the new vector and set the indices |
---|
2162 | for (int k = 0; k < newVector.size(); k++) { |
---|
2163 | newVector.get(k).index = k; |
---|
2164 | } |
---|
2165 | Globals.map.roads.get(j).carsOnThisRoad = newVector; |
---|
2166 | } |
---|
2167 | |
---|
2168 | |
---|
2169 | if (!outputToFile) |
---|
2170 | return; |
---|
2171 | // output text |
---|
2172 | try { |
---|
2173 | pwText.print(NCARS + " "); |
---|
2174 | outBin.writeInt(NCARS); |
---|
2175 | for (int j = 0; j < Globals.map.roads.size(); j++) { |
---|
2176 | for (int k = 0; k < Globals.map.roads.get(j).carsOnThisRoad |
---|
2177 | .size(); k++) { |
---|
2178 | CarInstance ci = Globals.map.roads.get(j).carsOnThisRoad |
---|
2179 | .get(k); |
---|
2180 | pwText.print(ci.ID + " " + ci.getRoadIdx() + " " |
---|
2181 | + ci.getPointIdx() + " " + ci.getOffset() + " " |
---|
2182 | + ci.getDirection() + " " + ci.getLane() + " "); |
---|
2183 | outBin.writeInt(ci.ID); |
---|
2184 | outBin.writeInt(ci.getRoadIdx()); |
---|
2185 | outBin.writeInt(ci.getPointIdx()); |
---|
2186 | outBin.writeDouble(ci.getOffset()); |
---|
2187 | outBin.writeInt(ci.getDirection()); |
---|
2188 | outBin.writeInt(ci.getLane()); |
---|
2189 | outBin.writeDouble(ci.getSpeed()); |
---|
2190 | } |
---|
2191 | } |
---|
2192 | pwText.println(); |
---|
2193 | } catch (IOException e) { |
---|
2194 | System.err.println("Error writing traces"); |
---|
2195 | e.printStackTrace(); |
---|
2196 | System.exit(0); |
---|
2197 | } |
---|
2198 | } |
---|
2199 | |
---|
2200 | public int getCrtTime(){ |
---|
2201 | return crtTime; |
---|
2202 | } |
---|
2203 | |
---|
2204 | |
---|
2205 | |
---|
2206 | |
---|
2207 | public SimulatedCarInfo getCarIdx(int rdIdx, int ptIdx) |
---|
2208 | { |
---|
2209 | synchronized (cars) |
---|
2210 | { |
---|
2211 | Iterator<SimulatedCarInfo> it = cars.iterator(); |
---|
2212 | while (it.hasNext()) { |
---|
2213 | SimulatedCarInfo r = it.next(); |
---|
2214 | if (r.getRoadIdx() == rdIdx && r.getPointIdx() == ptIdx) |
---|
2215 | return r; |
---|
2216 | } |
---|
2217 | } |
---|
2218 | return null; |
---|
2219 | } |
---|
2220 | |
---|
2221 | public SimulatedCarInfo getCarIdx(int idx) |
---|
2222 | { |
---|
2223 | synchronized (cars) |
---|
2224 | { |
---|
2225 | Iterator<SimulatedCarInfo> it = cars.iterator(); |
---|
2226 | while (it.hasNext()) { |
---|
2227 | SimulatedCarInfo r = it.next(); |
---|
2228 | if (r.vehicleId == idx) |
---|
2229 | return r; |
---|
2230 | } |
---|
2231 | } |
---|
2232 | return null; |
---|
2233 | } |
---|
2234 | |
---|
2235 | public static void main(String args[]) { |
---|
2236 | |
---|
2237 | // Globals.map = new Map("Intersectie.RT1","Intersectie.RT2"); |
---|
2238 | |
---|
2239 | int tests = 1; |
---|
2240 | for (int i = 0; i < tests; i++){ |
---|
2241 | try{ |
---|
2242 | switch (i){ |
---|
2243 | case 0: Globals.pw.println("Adaptive xc = 1, variations2"); |
---|
2244 | break; |
---|
2245 | case 1:Globals.pw.println("Adaptive xc = 0.95, variations2"); |
---|
2246 | break; |
---|
2247 | case 2:break; |
---|
2248 | } |
---|
2249 | |
---|
2250 | // ObjectInputStream ois=new ObjectInputStream(new FileInputStream(".\\maps\\fsc\\Apaca.fsc")); |
---|
2251 | // Scenario sc=(Scenario)ois.readObject(); |
---|
2252 | // Mobility.loadScenario(sc); |
---|
2253 | |
---|
2254 | Engine e = Globals.engine = new Engine(false, true); |
---|
2255 | e.init(); |
---|
2256 | // public void run(){ |
---|
2257 | // Globals.demo = new Display(); |
---|
2258 | // Globals.demo.setVisible(true); |
---|
2259 | // } |
---|
2260 | // }); |
---|
2261 | // t.start(); |
---|
2262 | // e.play(); |
---|
2263 | switch (i){ |
---|
2264 | case 0: |
---|
2265 | Globals.Xc = 0.95; |
---|
2266 | break; |
---|
2267 | case 1:break; |
---|
2268 | case 2:break; |
---|
2269 | } |
---|
2270 | |
---|
2271 | if (i != tests - 1){ |
---|
2272 | Globals.pw.close(); |
---|
2273 | String pwname = "rezultate"+System.currentTimeMillis()+".txt"; |
---|
2274 | Globals.pw = new PrintWriter(new FileWriter(pwname)); |
---|
2275 | } |
---|
2276 | } catch (Exception ex) { |
---|
2277 | ex.printStackTrace(); |
---|
2278 | System.exit(0); |
---|
2279 | } |
---|
2280 | |
---|
2281 | |
---|
2282 | } |
---|
2283 | |
---|
2284 | /* long t1 = System.currentTimeMillis(); |
---|
2285 | e.play(60); |
---|
2286 | long t2 = System.currentTimeMillis(); |
---|
2287 | |
---|
2288 | System.out.println("SimTime: " + (t2 - t1)); |
---|
2289 | System.out.println("events: " + (double) (100 * e.eventsTime) |
---|
2290 | / (t2 - t1)); |
---|
2291 | System.out.println("mobility: " + (double) (100 * e.mobilityTime) |
---|
2292 | / (t2 - t1)); |
---|
2293 | System.out.println("move: " + (double) (100 * e.moveTime) / (t2 - t1)); |
---|
2294 | System.out.println("code: " + (double) (100 * e.codeTime) / (t2 - t1)); |
---|
2295 | System.out.println("net: " + (double) (100 * e.netTime) / (t2 - t1)); |
---|
2296 | System.out.println("cleanup: " + (double) (100 * e.cleanupTime) |
---|
2297 | / (t2 - t1)); |
---|
2298 | System.out |
---|
2299 | .println("sched: " + (double) (100 * e.schedTime) / (t2 - t1)); |
---|
2300 | |
---|
2301 | // System.out.println("pkavg " + pkavg + "\npkmax" + pkmax |
---|
2302 | // + "\ncomp time " + avgTime); |
---|
2303 | // e.play(); |
---|
2304 | // e.step(); |
---|
2305 | // e.step(); |
---|
2306 | // e.step(); |
---|
2307 | // e.step(); |
---|
2308 | // e.step(); |
---|
2309 | System.exit(0);*/ |
---|
2310 | } |
---|
2311 | |
---|
2312 | |
---|
2313 | /* public void createDummyCars() { |
---|
2314 | Road r = Globals.map.roads.get(1); |
---|
2315 | |
---|
2316 | //Stopping a car on a road |
---|
2317 | int ID = lastCarID; |
---|
2318 | lastCarID++; |
---|
2319 | CarInstance car = Mobility.createNewCar(1, 46, 0, 1, 0.0, |
---|
2320 | new NotMovingPersonality(), 0.0, ID, 2, 64, null, 1); |
---|
2321 | if (car == null) { |
---|
2322 | lastCarID--; |
---|
2323 | System.out.println("Could not create the first stopped car"); |
---|
2324 | } else { |
---|
2325 | System.out.println("Jammed car id=" + ID); |
---|
2326 | jammed = ID; |
---|
2327 | } |
---|
2328 | |
---|
2329 | this.addCar(car); |
---|
2330 | } |
---|
2331 | */ |
---|
2332 | |
---|
2333 | |
---|
2334 | } |
---|