Changeset 38
- Timestamp:
- Dec 27, 2009, 10:04:45 AM (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
proiecte/ptvs/src/vnsim/core/Engine.java
r31 r38 23 23 import vnsim.applications.ezcab.EzcabClient; 24 24 import vnsim.applications.trafficview.SimulatedCarInfo; 25 import vnsim.applications.vitp.CarRunningVITP;26 import vnsim.applications.vitp.predefinedqueries.PredefinedQueries;27 import vnsim.applications.vitp.predefinedqueries.QueryByLocation;28 import vnsim.applications.vitp.predefinedqueries.QueryByVehicleId;29 25 import vnsim.core.events.CleanupEvent; 30 26 import vnsim.core.events.Event; … … 106 102 DataInputStream dis = null; 107 103 108 public int fps; 109 110 ArrayList<Event> eventQueue = null; 104 public static int fps; 105 106 static ArrayList<Event> eventQueue = null; 107 108 /** 109 * Mihaela Teler 110 * de fiecare data cand vreau sa fac o adaugare in eventQueue, intai verific punctul de unde vine evenimentul si astfel asignez 111 * un thread pt procesarea ulterioara a acelui eveniment(cand va fi scos din coada..) 112 * folosesc un singur eventQueue dar modific clasa Event sa contina si o referinta la un Thread 113 */ 114 111 115 112 116 //Petroaca - the DSRC emergency channel … … 115 119 public boolean pauseFlag = true; 116 120 117 boolean doneFlag = false,doneFlagEmergency=false; 118 119 public ArrayList<SimulatedCarInfo> cars = null; 120 121 public int crtTime = 0; 121 static boolean doneFlag = false; 122 123 boolean doneFlagEmergency=false; 124 125 public static ArrayList<SimulatedCarInfo> cars = null; 126 127 public static int crtTime = 0; 122 128 123 129 public long t0 = 0L; … … 127 133 boolean runtimeMobility; 128 134 129 public boolean disableComm = false;135 public static boolean disableComm = false; 130 136 131 137 File fText = null, fBin = null; … … 138 144 double lastSimSecond = 0.0; 139 145 140 int eventsTime = 0, mobilityTime = 0, cleanupTime = 0, netTime = 0, codeTime = 0; 141 int schedTime = 0, moveTime = 0, semTime = 0; 146 int eventsTime = 0, mobilityTime = 0;//, netTime = 0, codeTime = 0; 147 148 static int cleanupTime = 0; 149 static int netTime = 0; 150 static int codeTime = 0; 151 static int schedTime = 0; 152 153 int moveTime = 0; 154 155 static int semTime = 0; 142 156 143 157 public double maxControlDelay = 0; … … 194 208 public void init() { 195 209 196 // try{197 // File f=new File(".//maps//map//apaca.map");198 // if(!f.exists()) {199 // System.out.println("NOT EXIST!");200 // Globals.map = new trafficview.object.Map(".//maps\\rt\\apaca.RT1",".\\maps\\rt\\apaca.RT2");201 //202 //// Globals.map = new trafficview.object.Map(".\\maps\\rt\\Intersectie.RT1","Intersectie.RT2");203 // // Globals.map = new trafficview.object.Map("Autostrada.RT1","Autostrada.RT2");204 //205 //// Globals.map = new trafficview.object.Map("TGR34023_2.RT1","TGR34023_2.RT2");206 // ObjectOutputStream oos=new ObjectOutputStream(new207 // FileOutputStream(".\\maps\\map\\apaca.map"));208 // oos.writeObject(Globals.map);209 // } else {210 // System.out.println("EXIST!");211 // ObjectInputStream ois=new ObjectInputStream(new212 // FileInputStream(".\\maps\\map\\apaca.map"));213 // Globals.map=(trafficview.object.Map)ois.readObject();214 // }215 // }catch(Exception e){216 // System.err.println("Error reading map;");217 // e.printStackTrace();218 // System.exit(0);219 // }220 // MapLoader.loadMap();221 222 223 210 Map map = Globals.map; 211 212 //System.out.println("roads.size: "+map.roads.size()+" : "+map.roads.get(0).getName()); 213 214 //Road road = (Road)map.roads.get(0); 215 216 //System.out.println("road.points: "+road.points); 217 218 //System.out.println("strada:"+road.getName()+":"+" road.crosses.size(): "+road.crosses.size()); 219 220 221 ArrayList<Intersection> intersectii = map.allIntersections; 222 for(int q=0;q<intersectii.size();q++){ 223 System.out.println("intersectia "+q+": "+" nume: "+intersectii.get(q).toString()+" "+intersectii.get(q).getMapPoint()); 224 } 225 226 System.out.println(".........................aici4........."); 227 228 //*************************************************************** 229 224 230 if (map.roads.get(0).getName().startsWith("Iuliu") || map.roads.get(0).getName().startsWith("Dr")) 225 231 { 226 232 227 233 Globals.monitorWTL = (WirelessTrafficLight)map.allIntersections.get(1); 228 234 } … … 271 277 // init cars and events 272 278 eventQueue = new ArrayList<Event>(); 273 279 280 274 281 cars = new ArrayList<SimulatedCarInfo>(); 275 282 … … 277 284 278 285 //Petroaca - if DSRC simulation then initialize the emergency event queue 279 if( this.simulationProtocol==Globals.PROTOCOL_DSRC)286 if(Engine.simulationProtocol==Globals.PROTOCOL_DSRC) 280 287 eventQueueEmergency = new ArrayList<Event>(); 281 288 … … 290 297 } 291 298 292 synchronized (eventQueue) 293 { 294 eventQueue.add(new GPSEvent(0)); 295 296 eventQueue.add(new CleanupEvent(1)); 297 Iterator<SimulatedCarInfo> it = cars.iterator(); 298 while (it.hasNext()) 299 { 300 SimulatedCarInfo r = it.next(); 301 int t = 4 + random.nextInt(Globals.executionFPS / 3); 302 schedEvent(new SendEvent(4, r, 303 SimulatedCarInfo.STANDARD_MESSAGE_ID)); 304 } 305 306 /* Szekeres A. START_MODIFY - add broadcast event for each traffic light*/ 307 for(int i=0;i<Globals.map.allIntersections.size();i++) { 308 Intersection sender = Globals.map.allIntersections.get(i); 309 if (sender instanceof EmissionsTrafficLight) 299 /** 300 * Mihaela Teler 301 * inainte sa adaug evenimentul aflu pozitia de unde vine.. 302 */ 303 synchronized (eventQueue) { 304 305 // exceptie pt evenimentele GPS si Cleanup.. pe acestea le proceseaza threadul main 306 eventQueue.add(new GPSEvent(0)); 307 308 eventQueue.add(new CleanupEvent(1)); 309 310 Iterator<SimulatedCarInfo> it = cars.iterator(); 311 while (it.hasNext()) 310 312 { 311 ((EmissionsTrafficLight)sender).init(); 312 schedEvent(new SendEvent(4, (EmissionsTrafficLight)sender, (int)Globals.PROT_TL_FEEDBACK)); 313 } 314 } 315 /* Szekeres A. STOP_MODIFY */ 316 317 } 318 313 System.out.println("nu ai cum sa intri pe aici!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! cars este vida"); 314 SimulatedCarInfo r = it.next(); 315 schedEvent(new SendEvent(4, r, SimulatedCarInfo.STANDARD_MESSAGE_ID)); 316 System.out.println("add in the queue.."); 317 } 318 319 /* Szekeres A. START_MODIFY - add broadcast event for each traffic light*/ 320 for(int i=0;i<Globals.map.allIntersections.size();i++) { 321 Intersection sender = Globals.map.allIntersections.get(i); 322 if (sender instanceof EmissionsTrafficLight) 323 { 324 ((EmissionsTrafficLight)sender).init(); 325 schedEvent(new SendEvent(4, (EmissionsTrafficLight)sender, (int)Globals.PROT_TL_FEEDBACK)); 326 } 327 } 328 /* Szekeres A. STOP_MODIFY */ 329 } 330 331 319 332 try 320 333 { … … 341 354 } 342 355 343 if ( this.simulationType == Globals.routePlanConstants.DYNAMIC_INFRASTRUCTURE_ROUTE) {356 if (Engine.simulationType == RoutingConstants.DYNAMIC_INFRASTRUCTURE_ROUTE) { 344 357 DelayRecord.initDelays(); 345 358 this.infrastructure=new ArrayList<SimulatedCarInfo>(); … … 347 360 InfrastructureNode.createInfrastructureNodes(); 348 361 } 349 /*if (Engine.simulationType == RoutingConstants.DYNAMIC_CITY_ROUTE) {350 RoadSegmentCost.initRoadSegmentsCost();351 this.infrastructure=new ArrayList<SimulatedCarInfo>();352 //create a fixed node for every intersection353 IntersectionNode.createIntersectionNodes();354 }*/355 362 356 363 //Petroaca - instantiate the ricean propagation module … … 358 365 { 359 366 if(Engine.propagationModel==Globals.RICEAN) 360 this.riceanModule=new Ricean(Globals.RICEAN_FILE_NAME,true);367 Engine.riceanModule=new Ricean(Globals.RICEAN_FILE_NAME,true); 361 368 } 362 369 catch(IOException ioe){System.out.println("Ricean file io error:"+ioe.getMessage());System.exit(1);} 363 // System.out.println("Engine initialized - "+eventQueue.size()+" 364 // events"); 365 // System.out.println("CARS:"); 366 // System.out.println(cars); 367 // System.out.println("Events:"); 368 // System.out.println(eventQueue); 369 370 // Road r = Globals.map.roads.get(5); 371 // Point p = r.points.get(10900); 372 // int i = 10899; 373 // while (true){ 374 // Point px = r.points.get(i); 375 // i--; 376 // if (p.getDistance() - px.getDistance() > 10) 377 // break; 378 // } 379 // System.out.println(i + " " +(p.getDistance() - r.points.get(i).getDistance())); 370 371 System.out.println(">>> Engine initialized - "+eventQueue.size()+" events in the queue"); 372 System.out.println("CARS:"+cars); 373 System.out.println("Events:"+eventQueue); 374 375 376 System.out.println(); 377 378 Road r = Globals.map.roads.get(1); 379 System.out.print("road:"+r.getName()); 380 System.out.println(" r.points.size():"+r.points.size()); 381 382 Point p1 = r.points.get(r.points.size()-1); 383 System.out.println(p1); 384 Point p2 = r.points.get(r.points.size()-2); 385 System.out.println(p2); 386 387 System.out.println("distanta: " +(p1.getDistance() - p2.getDistance())); 388 System.out.println("distanta: " +GPSutil.distance(p1, p2)); 389 System.out.println("__________________end init____________________________\n"); 390 380 391 381 392 } … … 390 401 391 402 //Petroaca - if DSRC simulation also reset the emergency channel 392 if( this.simulationProtocol==Globals.PROTOCOL_DSRC)403 if(Engine.simulationProtocol==Globals.PROTOCOL_DSRC) 393 404 eventQueueEmergency = new ArrayList<Event>(); 394 405 … … 413 424 while (it.hasNext()) { 414 425 SimulatedCarInfo r = it.next(); 415 schedEvent(new SendEvent(crtTime + 2, r, 416 SimulatedCarInfo.STANDARD_MESSAGE_ID)); 426 schedEvent(new SendEvent(crtTime + 2, r, SimulatedCarInfo.STANDARD_MESSAGE_ID)); 417 427 } 418 428 } … … 420 430 421 431 // Petroaca - if DSRC simulation also reset the emergency channel 422 if( this.simulationProtocol==Globals.PROTOCOL_DSRC)432 if(Engine.simulationProtocol==Globals.PROTOCOL_DSRC) 423 433 eventQueueEmergency = new ArrayList<Event>(); 424 434 } … … 428 438 * 429 439 */ 430 public void play() 431 { 440 public void play() { 432 441 // supposed to be in the init() 433 442 // between init and play() Globals.map.allIntersections is changing 434 443 // need to find a fix 444 435 445 if (Engine.simulationType == RoutingConstants.DYNAMIC_CITY_ROUTE) { 436 446 CityTrafficLight.upgradeToCityTL(); … … 441 451 server.init(); 442 452 } 443 //453 444 454 prevRealSec = t0 = System.currentTimeMillis(); 445 455 pauseFlag = false; … … 480 490 long lastTime = -1; 481 491 492 ////////////////////////////////////// step /////////////////////////////////////////////////////////// 482 493 /** 483 494 * Process the events for the current step of the simulation 484 495 * 485 496 */ 497 498 static int cnt = 0; 499 486 500 public void step() { 487 int cnt = 0;501 488 502 489 503 if(Globals.SOCIALNETWORK == 1){ 490 504 double sec = vnsim.socialNet.GlobalNetwork.nextReconfigurationTime*Globals.SECOND; 491 // if(crtTime == sec){ 492 // int ret=JOptionPane.showConfirmDialog(null, "au trecut "+sec+" secunde"); 493 // //vnsim.vehicular.generator.Mobility.events = new SortedEventsVector(); 494 // Scenario s = vnsim.socialNet.Main.recofigurare(); 495 // try { 496 // vnsim.socialNet.Main.addCarEvents(s,crtTime); 497 // } catch (Exception ex) { 498 // JOptionPane.showMessageDialog(null, "<html>ERROR! Map structure has been modified<br> Try rebuilding the map " + s.mapFileName +"!</html>"); 499 // } 500 // } 501 } 502 503 if (crtTime > 3.5 * Globals.HOUR){ 505 } 506 507 if (crtTime > 3.5 * Globals.HOUR){ //nu rulez mai mult de 3 ore juma 504 508 doneFlag = true; 505 509 System.exit(0); … … 533 537 534 538 if (crtTime % Globals.MINUTE == 0){ 535 System.out.println((crtTime / Globals.MINUTE) + " sim min;\t" + 536 "packets: "+ Globals.packets +"\t" + 537 " collided: "+Globals.collided + 538 " lost: " + Globals.lost+ 539 " interfered: " + Globals.interfered+ 540 " max range: " + Globals.maxRange); 541 542 Globals.pw.println((crtTime / Globals.MINUTE) + " sim min;\t" + 543 "packets: "+ Globals.packets +"\t" + 544 " collided: "+Globals.collided + 545 " lost: " + Globals.lost+ 546 " interfered: " + Globals.interfered+ 547 " max range: " + Globals.maxRange); 539 548 540 Globals.packets = 0; 549 541 Globals.collided = 0; 550 542 Globals.lost = 0; 551 543 552 // System.out.println((crtTime / Globals.MINUTE) + " sim min;\t" + 553 // "simulation time: "+ (System.currentTimeMillis() - t0) +"\t" + 554 // " cars: "+cars.size() + 555 // " created " + created); 556 Globals.pw.println((crtTime / Globals.MINUTE) + 557 " global volume: " + Globals.volume * 60+ 558 " global demand: " + Globals.demand * 60); 544 System.out.println((crtTime / Globals.MINUTE) + " sim min;\t" + 545 "simulation time: "+ (System.currentTimeMillis() - t0) +"\t" + 546 " cars: "+cars.size() + " created " + created); 547 559 548 Globals.volume = 0; 560 549 Globals.demand = 0; 561 550 562 551 //Petroaca 563 if( this.simulationProtocol==Globals.PROTOCOL_DSRC)552 if(Engine.simulationProtocol==Globals.PROTOCOL_DSRC) 564 553 { 565 /*System.out.print("\n");566 System.out.println("DSRC DATA------------------------------");567 System.out.println((crtTime / Globals.MINUTE)+ " min\t" + "weak :"+Globals.DSRC_PACKETS_LOST_WEAK+568 " collided :"+Globals.DSRC_PACKETS_LOST_COLLISION+" ok: "+Globals.DSRC_PACKETS_RECEIVED_OK+569 " lost Tx :"+Globals.DSRC_PACKETS_LOST_TX+" lost Rx :"+Globals.DSRC_PACKETS_LOST_RX+570 "lost per :"+Globals.DSRC_PACKETS_LOST_PER+" lost corrupted :"+Globals.DSRC_PACKETS_LOST_CORRUPTED571 +" total packets: "+Globals.DSRC_PACKETS_TOTAL);572 System.out.println("send faliure Rx:"+Globals.DSRC_PACKETS_SEND_FALIURE_RX+" send faliure noise:"+Globals.DSRC_PACKETS_SEND_FALIURE_NOISE573 );574 */575 554 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 576 555 ,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,this.cars.size(),Globals.EMERGENCY_RECEIVED,Globals.EMERGENCY_SENT)); … … 589 568 Globals.DSRC_SEND=0; 590 569 591 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);570 //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); 592 571 593 572 Globals.CUMMULATIVE_PACKETS_RAV=0; … … 608 587 if (lastTime == -1) 609 588 lastTime = t0; 610 long realTime = t1 - lastTime;611 589 lastTime = t1; 612 // Globals.pw.println("\n-----");613 // Globals.pw.println("5 virtual minutes = " + (double)realTime/1000);614 // Globals.pw.println("density [veh/km] = " + (double) cars.size()/10);615 // Globals.pw.println("events: " + (double) (100 * (eventsTime - codeTime - cleanupTime))616 // / realTime);617 // Globals.pw.println("mobility: " + (double) (100 * mobilityTime)618 // / realTime);619 // Globals.pw.println("code: " + (double) (100 * codeTime + cleanupTime) / realTime);620 // Globals.pw.println("-----");621 590 622 591 eventsTime = 0; … … 625 594 mobilityTime = 0; 626 595 627 Globals.pw.println( "\n" + (crtTime / Globals.MINUTE) + " " +628 " " + fincars +629 " "+ (totalControlDelay/fincars) +630 " "+ (maxControlDelay) +631 " "+ ( ( totalEM.fc * 100) / (1000 * totalkm)) +632 " "+ (totalEM.fc / fincars) +633 " "+ (totalEM.co2 / fincars) +634 " "+ (totalEM.co / fincars) +635 " "+ (totalEM.hc / fincars) +636 " "+ (totalEM.nox / fincars)637 );638 596 Globals.pw.flush(); 639 597 … … 647 605 Globals.demo.st.addInfo("NOx: " + dec1((totalEM.nox * 20 )/ 1000)+" kg/h"); 648 606 649 650 651 Globals.pwxc.println( (crtTime / Globals.MINUTE) + " " + 652 " " + fincars + 653 " "+ (totalControlDelay/fincars) + 654 " "+ (maxControlDelay) + 655 " "+ ( (totalEM.fc * 100) / ( 1000 * totalkm)) + 656 " "+ (totalEM.fc / crossedCars) + 657 " "+ (totalEM.co2 / crossedCars) + 658 " "+ (totalEM.co / crossedCars) + 659 " "+ (totalEM.hc / crossedCars) + 660 " "+ (totalEM.nox / crossedCars) + 661 " "+ crossedCars 662 ); 663 Globals.pwxc.flush(); 607 664 608 if (fincars>0) { 665 609 Globals.pwxc.println( "min " + (crtTime / Globals.MINUTE) + … … 677 621 Globals.pwxc.flush(); 678 622 } 679 //fincars = 0;680 //crossedCars = 0;681 //totalkm = 0;682 //totalEM = new EmissionsResults();683 //totalControlDelay = 0;684 //maxControlDelay = 0;685 623 } 686 624 687 625 688 // if (crtTime % Globals.SECOND == 0)689 //690 // if (crtTime % (Globals.executionFPS / Globals.FPS) == 0)691 626 if (startedWithGUI) { 692 627 Globals.demo.st.setCount(crtTime); … … 695 630 //Globals.demo.st.setSemTime(mycnt); 696 631 Globals.demo.st.setNrEvents(eventQueue.size()); 697 Globals.demo.st.setCarsNo(cars.size()); 632 Globals.demo.st.setCarsNo(cars.size()); 698 633 } 699 634 … … 702 637 // Petroaca - if DSRC simualtion play the events in emergency channel as well 703 638 704 if( this.simulationProtocol==Globals.PROTOCOL_DSRC && Globals.MULTI_CHANNEL)639 if(Engine.simulationProtocol==Globals.PROTOCOL_DSRC && Globals.MULTI_CHANNEL) 705 640 { 706 641 while (true) { … … 719 654 } 720 655 656 /** 657 * Mihaela Teler 658 * vreau sa aflu cate intersectii am 659 * voi avea tot atatea threaduri 660 */ 661 Map map = Globals.map; 662 ArrayList<Intersection> intersectii = map.allIntersections; 663 int numThreads=intersectii.size(); 664 665 EventProcessingThread processingThreads[] = new EventProcessingThread[numThreads]; 666 667 721 668 while (true) { 669 722 670 if (eventQueue.size() == 0) { 723 671 doneFlag = true; … … 725 673 } 726 674 675 // extrag un eveniment din coada !!!!! acum doar il extrag.. cand intru in playEvent il si scot cu remove(0) 727 676 Event e = (Event) eventQueue.get(0); 728 if (e.getTime() == crtTime) { 677 System.out.println("--- no of events in the queue:"+eventQueue.size() ); 678 679 /** 680 * Mihaela Teler 681 * vector de threaduri pt procesare evenimente in functie de locatie 682 * procesez folosind threaduri doar evenimentele send si receive 683 */ 684 685 686 if((e instanceof SendEvent) || (e instanceof ReceiveEvent)){ 687 688 /** 689 * Mihaela Teler 690 * aflu locatia evenimentului 691 */ 692 Road r = null; 693 Point p = null; 694 if(e instanceof SendEvent){ 695 SendEvent se=(SendEvent)e; 696 r = (Road) Globals.map.roads.get(se.sender.getRoadIdx()); //aflu strada pe care se afla senderul 697 p = (Point) r.points.get(se.sender.getPointIdx()); //aflu punctul unde se afla senderul 698 } 699 if(e instanceof ReceiveEvent){ 700 ReceiveEvent re=(ReceiveEvent)e; 701 r = (Road) Globals.map.roads.get(re.sender.getRoadIdx()); 702 p = (Point) r.points.get(re.sender.getPointIdx()); 703 } 704 705 //System.out.println("p:"+p); 706 707 /** 708 * Mihaela Teler 709 * parcurg toate intersectiile si o gasesc pe cea mai apropiata 710 */ 711 double MIN_DIST=Double.MAX_VALUE; 712 int indexClosestIntersection=0; 713 double distanta; 714 for(int i=0;i<intersectii.size();i++){ 715 Intersection intersectie = intersectii.get(i); 716 if(r.getPoints().contains(intersectie.getMapPoint())){ //iau in calcul doar intersectiile aflate pe strada pe care sunt 717 distanta=GPSutil.distance(p, intersectie.getMapPoint()); 718 if (distanta < MIN_DIST){ 719 MIN_DIST=distanta; 720 indexClosestIntersection=i; 721 } 722 } 723 724 } 725 /** 726 * Mihaela Teler 727 * acum trebuie sa asignez un thread sa proceseze 728 */ 729 730 if(e instanceof SendEvent){ 731 processingThreads[indexClosestIntersection] = new SendEventProcessingThread(e,crtTime); 732 } 733 else processingThreads[indexClosestIntersection] = new ReceiveEventProcessingThread(e,crtTime); 734 735 // nu e bine cu apelul lui run pt ca practic nu se porneste un thread nou ci 736 // se apeleaza metoda run ca orice alta metoda dintr-o clasa, executia este secventiala.. 737 738 if (e.getTime() == crtTime) { 739 System.out.println("Processing thread "+indexClosestIntersection+" from intersection "+intersectii.get(indexClosestIntersection)); 740 //processingThreads[indexClosestIntersection].start();// cu start nu merge ... why???? 741 processingThreads[indexClosestIntersection].run(); 742 cnt++; 743 } 744 else 745 break; 746 } 747 748 else if (e.getTime() == crtTime) { 729 749 playEvent(); 730 750 cnt++; 731 751 } else 732 752 break; 753 754 733 755 } 734 756 … … 743 765 else 744 766 generateScenarioPhase(); 767 745 768 checkCreateNewCars(); 746 769 … … 749 772 mobilityTime += t4 - t3; 750 773 751 if ( this.simulationType == Globals.routePlanConstants.DYNAMIC_INFRASTRUCTURE_ROUTE) {774 if (Engine.simulationType == RoutingConstants.DYNAMIC_INFRASTRUCTURE_ROUTE) { 752 775 if (crtTime % 100 == 0) { 753 776 for (int i = 0; i < this.infrastructure.size(); i++) { 754 777 755 ((InfrastructureNode) this.infrastructure.get(i)) 756 .updateDelays(); 778 ((InfrastructureNode) this.infrastructure.get(i)).updateDelays(); 757 779 } 758 780 } … … 764 786 server.step(crtTime); 765 787 } 766 767 // if (crtTime % Globals.MINUTE == 0){ 768 // System.out.println("[Engine - step()] "); 769 // System.out.println("\ntotal: " + (double)(t4 - t0)/1000); 770 // System.out.println("events: " + (double) (100 * eventsTime) 771 // / (t4 - t0)); 772 // System.out.println("mobility: " + (double) (100 * mobilityTime) 773 // / (t4 - t0)); 774 // System.out.println("code: " + (double) (100 * codeTime) 775 // / (t4 - t0)); 776 // 777 // } 778 // System.out.println(); 779 // System.out.println("crtTime "+crtTime); 780 // System.out.println("CARS:"); 781 // System.out.println(cars); 782 // System.out.println("Events:"); 783 // System.out.println(eventQueue); 784 785 //checkInjectQueries(); 786 787 // if (crtTime % Globals.SECOND == 0){ 788 // 789 // if (crtTime % (Globals.executionFPS / Globals.FPS) == 0) 790 // if (withGUI) { 791 // synchronized (Globals.mutex){ 792 // Globals.flag = 1; 793 // Globals.mutex.notify(); 794 // } 795 // } 796 // }else 797 // synchronized (Globals.mutex){ 798 // Globals.mutex.notify(); 799 // } 800 801 } 802 803 /** 804 * VITP. 805 * 806 */ 807 private void checkInjectQueries() { 808 // should inject some queries? 809 vnsim.applications.vitp.predefinedqueries.Query q=PredefinedQueries.queries.get(crtTime); 810 if(q!=null) 811 { 812 if(q instanceof QueryByVehicleId) { 813 //look for the vehicle with the specified id 814 Iterator<SimulatedCarInfo> it = cars.iterator(); 815 while (it.hasNext()) { 816 SimulatedCarInfo r = it.next(); 817 if(r.getVehicleId()==((QueryByVehicleId)q).vehicleId) 818 { 819 //inject the query 820 CarRunningVITP x = (CarRunningVITP)r; 821 if(!x.isEquipped) 822 continue; //look for an equipped vehicle 823 x.p.postMessageFromAbove(q.req); 824 System.out.println("QUERY INJECTED; CRTTIME="+crtTime); 825 break; 826 } 827 } 828 } else if(q instanceof QueryByLocation) { 829 //look for a vehicle in the specified location 830 int roadIdx=((QueryByLocation)q).roadIdx; 831 int pointIdx=((QueryByLocation)q).pointIdx; 832 833 Iterator<SimulatedCarInfo> it = cars.iterator(); 834 while (it.hasNext()) { 835 SimulatedCarInfo r = it.next(); 836 if(r.getRoadIdx()==roadIdx && r.getPointIdx()<pointIdx+5 && r.getPointIdx()>pointIdx-5) { 837 CarRunningVITP x = (CarRunningVITP)r; 838 if(!x.isEquipped) 839 continue; //look for an equipped vehicle 840 x.p.postMessageFromAbove(q.req); 841 System.out.println("QUERY INJECTED; CRTTIME="+crtTime); 842 843 // Road road=Globals.map.roads.get(r.getRoadIdx()); 844 // 845 // Point p1=road.points.get(r.getPointIdx()); 846 // Point p2=road.points.get(q.req.getDest().pt1); 847 // 848 // // System.out.println("P1="+r.getPointIdx()); 849 // // System.out.println("P2="+q.req.getDest().pt1); 850 // // 851 // // System.out.println("AWAY="+(p1.getDistance()-p2.getDistance())); 852 // 853 // Statistics.setQueryDistance(MessageIndexModule.index, (p1.getDistance()-p2.getDistance())); 854 // Statistics.setInitialTime(MessageIndexModule.index, crtTime); 855 break; 856 } 857 } 858 } 859 } 860 } 788 } 789 //////////////////////////////////////////////end metoda step//////////////////////////////////// 861 790 862 791 static Random random = new Random(); … … 868 797 * 869 798 */ 870 private void checkCreateNewCars() 871 { 799 private void checkCreateNewCars(){ 872 800 873 801 int percent; … … 929 857 //one vehicle on each lane 930 858 931 // double offset=4*random.nextDouble();932 // System.out.println("OFFSET="+offset);933 934 859 Personality personality; 935 860 rand=(random.nextDouble())*100; … … 950 875 roadIndexDst, pointIndexDst, r.route); 951 876 952 System.out.println("Finished creating new car"); 877 System.out.println("Finished creating new car "+car); 878 953 879 double d =0; 954 880 if(car!=null) … … 957 883 // closer than 5 meters --> do not create 958 884 // System.out.println("Skipping creation..."); 959 Globals.map.roads.get(car.getRoadIdx()).carsOnThisRoad 960 .remove(car.ID); 885 Globals.map.roads.get(car.getRoadIdx()).carsOnThisRoad.remove(car.ID); 961 886 continue; 962 887 } 963 888 } 964 889 965 if ( this.simulationType == Globals.routePlanConstants.DYNAMIC_SELF_ROUTE) {890 if (Engine.simulationType == RoutingConstants.DYNAMIC_SELF_ROUTE) { 966 891 System.out.println("Dynamic; we are here"); 967 892 percent = random.nextInt(100); … … 983 908 // closer than 10 meters --> do not create 984 909 985 Globals.map.roads.get(car.getRoadIdx()).carsOnThisRoad 986 .remove(car.ID); 910 Globals.map.roads.get(car.getRoadIdx()).carsOnThisRoad.remove(car.ID); 987 911 continue; 988 912 } 989 913 990 914 } 991 if ( this.simulationType == Globals.routePlanConstants.DYNAMIC_INFRASTRUCTURE_ROUTE) {915 if (Engine.simulationType == RoutingConstants.DYNAMIC_INFRASTRUCTURE_ROUTE) { 992 916 993 917 car = Mobility.createNewCarCommunicatingWithInfrastucture( … … 1060 984 } 1061 985 1062 1063 1064 long messagesSize = 0;1065 long carInfosSize = 0;986 ////////////////////////////////////////// playEvent //////////////////////////////////////////////////////////////// 987 988 static long messagesSize = 0; 989 static long carInfosSize = 0; 1066 990 /** 1067 991 * Execute the following event in the eventQueue 1068 992 * 1069 993 */ 1070 public void playEvent() { 994 public static void playEvent() { 995 996 //System.out.println(">> Thread:"+Thread.currentThread().getName()); 997 1071 998 if (eventQueue.size() == 0) { 1072 999 doneFlag = true; … … 1078 1005 e = eventQueue.remove(0); 1079 1006 } 1080 // System.out.print(e.toString()+", "); 1081 // System.out.flush(); 1082 // 1083 if (e instanceof GPSEvent) { 1007 //System.out.print(e.toString()+", "); 1008 //System.out.flush(); 1009 1010 if (e instanceof GPSEvent) { //////////// eveniment GPS 1011 1084 1012 Iterator<SimulatedCarInfo> it = cars.iterator(); 1085 1013 while (it.hasNext()) { … … 1090 1018 schedEvent(newEvent); 1091 1019 } 1092 if (e instanceof CleanupEvent) { 1020 if (e instanceof CleanupEvent) { //////////// eveniment CLEANUP 1093 1021 1094 1022 long t1 = System.currentTimeMillis(); … … 1097 1025 while (it.hasNext()) { 1098 1026 SimulatedCarInfo r = it.next(); 1099 // if (r.isPromiscuousMode() && r.pointIdx < 256 && r.pointIdx > 50){ 1100 // r.getDirection(); 1101 // } 1027 1102 1028 Iterator<CarInfo> j = r.trafficDB.iterator(); 1103 1029 while (j.hasNext()) { 1104 1030 CarInfo neighbor = j.next(); 1105 if ( (crtTime - neighbor.getTimestamp() > Globals.NEIGHBOR_EXPIRES 1106 && neighbor.state != 3) || 1031 if ( (crtTime - neighbor.getTimestamp() > Globals.NEIGHBOR_EXPIRES && neighbor.state != 3) || 1107 1032 (neighbor.state == 3 && crtTime - neighbor.getTimestamp() > Globals.PROMISCUOUS_NEIGHBOR_EXPIRES)){ 1108 1033 j.remove(); 1109 // deleted++;1110 1034 } 1111 1035 } … … 1139 1063 schedEvent(newCleanupEvent); 1140 1064 } 1141 // if (deleted != 0)1142 // System.out.println(crtTime+")\n\ttotal cars - "+carInfosSize+"\n\ttotal cars deleted- "+deleted+"\n\ttotal bytes transfered - "+messagesSize);1143 1065 messagesSize = 0; 1144 1066 } 1145 if (e instanceof SendEvent) { 1067 1068 if (e instanceof SendEvent) { /////////////////////////////// eveniment SEND 1069 1070 /** 1071 * cod mutat in metoda run a lui SendEventProcessingThread - intre timp m-am razgandit.. am pornit threaduri in metoda step 1072 */ 1073 1074 /*SendEventProcessingThread thread=new SendEventProcessingThread(e, crtTime, disableComm, messagesSize); 1075 synchronized (eventQueue) { 1076 if(!thread.isAlive()) { 1077 thread.start(); 1078 System.out.println(".........................thread started"); 1079 } 1080 }*/ 1081 1082 1146 1083 SendEvent se = (SendEvent) e; 1147 1084 Communicator sender = se.getSender(); 1148 1085 1149 1086 long t1 = System.currentTimeMillis(); 1150 1087 1088 // senderul e un SimulatedCarInfo 1151 1089 1152 1090 if (se.getMessage() == null){ … … 1154 1092 se.setMessage(message); 1155 1093 if (se.getMessage() == null){ 1156 // System.out.println("null message"+se.sender + " "+se.sender.getClass());1157 1094 if (sender.isPromiscuousMode() && sender.isPeriodicalMessage(se.getMessageType()) && !disableComm) { 1158 1095 int eventPeriod = sender.getPeriod(se.getMessageType()); 1159 1096 if (eventPeriod != -1) 1160 schedEvent(new SendEvent(crtTime + eventPeriod, sender, se 1161 .getMessageType())); 1097 schedEvent(new SendEvent(crtTime + eventPeriod, sender, se.getMessageType())); 1162 1098 } 1163 1099 return; … … 1170 1106 int eventPeriod = sender.getPeriod(se.getMessageType()); 1171 1107 if (eventPeriod != -1) 1172 schedEvent(new SendEvent(crtTime + eventPeriod, sender, se 1173 .getMessageType())); 1108 schedEvent(new SendEvent(crtTime + eventPeriod, sender, se.getMessageType())); 1174 1109 } 1175 1110 } … … 1178 1113 if(Engine.simulationProtocol==Globals.PROTOCOL_80211) 1179 1114 { 1180 // TODO To be replaced with a real MAC layer1115 1181 1116 if (!sender.mediumAvailable(crtTime)) { 1182 1117 e.setTime(crtTime + 1); … … 1227 1162 simulateCommunication(se); 1228 1163 } 1229 1230 1231 } 1232 1233 1234 1235 1236 1237 } 1238 if (e instanceof ReceiveEvent) { 1164 } 1165 } 1166 1167 if (e instanceof ReceiveEvent) { //////////////////////// eveniment RECEIVE 1239 1168 ReceiveEvent re = (ReceiveEvent) e; 1240 1241 // long tmp = 0; 1242 // long t3 = System.currentTimeMillis(); 1243 // long t4; 1244 // if (re.isUnicast()) { 1245 // long t1 = System.currentTimeMillis(); 1246 // re.receiver.onReceive(re.getMessage(), re.sender); 1247 // long t2 = System.currentTimeMillis(); 1248 // codeTime += t2 - t1; 1249 // } else { 1169 1250 1170 long t1 = System.currentTimeMillis(); 1171 1251 1172 //Petroaca - the DSRC impl 1252 if(Engine.simulationProtocol==Globals.PROTOCOL_80211) 1253 { 1173 if(Engine.simulationProtocol==Globals.PROTOCOL_80211){ 1254 1174 RadioDev rxradio = re.receiver.getRadio(); 1255 1175 ReceiveEvent goodSignal = rxradio.receive(re.signals); 1256 1176 re.receiver.removeReceiveEventForTime(re.getTime()); 1257 1177 1258 // for (ReceiveEvent goodSignal : re.signals){ 1259 // Road r1 = (Road) Globals.map.roads.get(re.sender.getRoadIdx()); 1260 // Point p1 = (Point) r1.points.get(re.sender.getPointIdx()); 1261 // Road r2 = (Road) Globals.map.roads.get(re.receiver.getRoadIdx()); 1262 // Point p2 = (Point) r2.points.get(re.receiver.getPointIdx()); 1263 // double distance = GPSutil.distance(p1, p2); 1264 // if (distance <= WIRELESS_RANGE){ 1265 if (goodSignal != null){ 1178 if (goodSignal != null){ 1266 1179 re.receiver.onReceive(goodSignal.getMessage(), goodSignal.getMessageObject(), goodSignal.sender); 1267 1180 } 1268 // }1269 // }1270 1271 1181 } 1272 1182 else … … 1298 1208 //appr.LaneChangeAssistance(best.getMessage(),((CarInfo)re.receiver)); 1299 1209 re.receiver.onReceive(best.getMessage(), best.getMessageObject(), best.sender); 1300 1301 1210 } 1302 1303 1304 1211 } 1305 1306 1307 1212 } 1308 1213 else … … 1312 1217 re.receiver.removeReceiveEventForTime(re.getTime()); 1313 1218 1314 1315 if (goodSignal != null){ 1219 if (goodSignal != null){ 1316 1220 re.receiver.onReceive(goodSignal.getMessage(), goodSignal.getMessageObject(), goodSignal.sender); 1317 1221 } 1318 1319 1320 } 1321 1322 1222 } 1323 1223 } 1324 1224 1325 1225 long t2 = System.currentTimeMillis(); 1326 1226 codeTime += t2 - t1; 1327 /* if (!re.sender.mediumAvailable(crtTime)) { 1328 re.setTime(crtTime + 1); 1329 schedEvent(re); 1330 return; 1331 } 1332 1333 Road r1 = null; 1334 Point p1 = null; 1335 if (re.sender instanceof SimulatedCarInfo){ 1336 SimulatedCarInfo sender = (SimulatedCarInfo)re.sender; 1337 r1 = (Road) Globals.map.roads.get(sender.getRealPos() 1338 .getRoadIdx()); 1339 p1 = (Point) r1.points.get(sender.getRealPos().getPointIdx()); 1340 }else{ 1341 WirelessTrafficLight wtl = (WirelessTrafficLight)re.sender; 1342 r1 = (Road) Globals.map.roads.get(wtl.segments.get(0).roadIndex); 1343 p1 = (Point) r1.points.get(wtl.segments.get(0).pointIndex); 1344 } 1345 // recvEventsCnt++; 1346 1347 long comp = 0; 1348 // peanoKeys 1349 int cnt2 = 0; 1350 int cnt3 = 0; 1351 int cnt4 = 0; 1352 int pkcnt = 0; 1353 PeanoKey pk = Globals.map.peanoKeys.get(p1.getPeanoKeyIdx()); 1354 1355 ListIterator<PeanoKey> pkit = Globals.map.peanoKeys 1356 .listIterator(p1.getPeanoKeyIdx()); 1357 PeanoKey maxpk = null; 1358 1359 double wifirange; 1360 if (re.sender instanceof WirelessTrafficLight){ 1361 maxpk = GPSutil.getMaxSearchBoundPK(p1,Engine.EXTENDED_WIRELESS_RANGE); 1362 wifirange = EXTENDED_WIRELESS_RANGE; 1363 }else{ 1364 wifirange = WIRELESS_RANGE; 1365 maxpk = pk.getMaxpk(); 1366 } 1367 PeanoKey pkx = null; 1368 for (pkx = pkit.next(); pkit.hasNext(); pkx = pkit.next()) { 1369 pkcnt++; 1370 // long tc1 = System.currentTimeMillis(); 1371 // PeanoKey pkx = Globals.map.peanoKeys.get(idx); 1372 if (pkx.compareTo(maxpk) > 0) 1373 break; 1374 // long tc2 = System.currentTimeMillis(); 1375 // comp += (tc2-tc1); 1376 1377 if (pkx.getCars() != null) { 1378 Road r2 = (Road) Globals.map.roads.get(pkx 1379 .getRoadIndex()); 1380 Point p2 = (Point) r2.points.get(pkx.getPointIndex()); 1381 if (GPSutil.distance(p1, p2) < wifirange) { 1382 cnt3 += pkx.getCars().size(); 1383 Iterator<SimulatedCarInfo> it = pkx.getCars() 1384 .iterator(); 1385 while (it.hasNext()) { 1386 SimulatedCarInfo r = it.next(); 1387 if (re.sender instanceof SimulatedCarInfo) 1388 if (r.getVehicleId() == ((SimulatedCarInfo)re.sender) 1389 .getVehicleId()) 1390 continue; 1391 r.setLastMediumTransmition(crtTime); 1392 t4 = System.currentTimeMillis(); 1393 tmp += t4 - t3; 1394 long t1 = System.currentTimeMillis(); 1395 r.onReceive(re.getMessage(), re.sender); 1396 long t2 = System.currentTimeMillis(); 1397 codeTime += t2 - t1; 1398 cnt2++; 1399 t3 = System.currentTimeMillis(); 1400 } 1401 } 1402 } 1403 } 1404 PeanoKey minpk = null; 1405 if (re.sender instanceof WirelessTrafficLight) 1406 minpk = GPSutil.getMaxSearchBoundPK(p1,-Engine.EXTENDED_WIRELESS_RANGE); 1407 else 1408 minpk = pk.getMinpk(); 1409 1410 pkit = Globals.map.peanoKeys.listIterator(p1.getPeanoKeyIdx()); 1411 1412 if (pkit.hasPrevious()){ 1413 pkx = pkit.previous(); 1414 } 1415 for (; pkit.hasPrevious(); pkx = pkit 1416 .previous()) { 1417 pkcnt++; 1418 // long tc1 = System.currentTimeMillis(); 1419 // PeanoKey pkx = Globals.map.peanoKeys.get(idx); 1420 if (pkx.compareTo(minpk) < 0) { 1421 break; 1422 } 1423 // long tc2 = System.currentTimeMillis(); 1424 // comp += (tc2-tc1); 1425 1426 if (pkx.getCars() != null) { 1427 Road r2 = (Road) Globals.map.roads.get(pkx 1428 .getRoadIndex()); 1429 Point p2 = (Point) r2.points.get(pkx.getPointIndex()); 1430 if (GPSutil.distance(p1, p2) < wifirange) { 1431 Iterator<SimulatedCarInfo> it = pkx.getCars() 1432 .iterator(); 1433 cnt3 += pkx.getCars().size(); 1434 while (it.hasNext()) { 1435 SimulatedCarInfo r = it.next(); 1436 r.setLastMediumTransmition(crtTime); 1437 1438 if (re.sender instanceof SimulatedCarInfo) 1439 if (r.getVehicleId() == ((SimulatedCarInfo)re.sender) 1440 .getVehicleId()) 1441 continue; 1442 1443 t4 = System.currentTimeMillis(); 1444 tmp += t4 - t3; 1445 long t1 = System.currentTimeMillis(); 1446 r.onReceive(re.getMessage(), re.sender); 1447 long t2 = System.currentTimeMillis(); 1448 codeTime += t2 - t1; 1449 cnt4++; 1450 t3 = System.currentTimeMillis(); 1451 } 1452 } 1453 } 1454 } 1455 1456 for (int j = 0; j < Globals.map.lightsIndices.size(); j++) { 1457 WirelessTrafficLight wtl = (WirelessTrafficLight) Globals.map.allIntersections 1458 .get(Globals.map.lightsIndices.get(j)); 1459 long t1sem = System.currentTimeMillis(); 1460 1461 if (GPSutil.distance(p1, wtl.getMapPoint()) < WIRELESS_RANGE) { 1462 wtl.setLastMediumTransmition(crtTime); 1463 wtl.onReceive(re.getMessage(), re.sender); 1464 } 1465 long t2sem = System.currentTimeMillis(); 1466 semTime += (t2sem - t1sem); 1467 } 1468 1469 t4 = System.currentTimeMillis(); 1470 tmp += t4 - t3; 1471 netTime += tmp; 1472 long t5 = System.currentTimeMillis(); 1473 */ 1474 // if (tmp > 0) 1475 // avgTime += (double) (100 * comp) / tmp; 1476 // if (pkcnt > pkmax) 1477 // pkmax = pkcnt; 1478 // avgi++; 1479 // pkavg += cnt2; 1480 1481 // long t5 = System.currentTimeMillis(); 1482 // 1483 // if (cnt1 != cnt2 + cnt4){ 1484 // System.out.println("==== "+crtTime+" car: "+re.sender+ " 1485 // ======\n"+ 1486 // "Count FAILED old:" + cnt1 +" - new: "+cnt2 +" + 1487 // "+cnt4+"/"+pkcnt 1488 // +" --- "+minpk+" - "+maxpk); 1489 // } 1490 // if (t5 - t4 > tmp){ 1491 // System.out.println("==== "+crtTime+" car: "+re.sender+ " 1492 // ======\n"+ 1493 // "Time FAILED old:" + tmp+"/"+cnt1 +" - new: "+ 1494 // (t5-t4)+"/"+cnt2 +"/"+ cnt3+" / "+pkcnt +" - "+comp); 1495 // } 1496 1497 } 1498 1499 // if (Globals.demo.mv.currentCar != null && 1500 // Globals.demo.mv.currentCar == re.getReceiver().car) 1501 // System.out.println(i+" cars nearby"); 1502 1503 // } // end if else isUnicast 1504 } 1505 1506 // static int recvEventsCnt = 0; 1507 // 1508 // static int pkmax; 1509 // 1510 // static long pkavg = 0; 1511 // 1512 // static double avgTime = 0; 1513 // 1514 // static int avgi = 0; 1515 // 1227 } 1228 } 1229 1230 1516 1231 public void playEventEmergency() 1517 1232 { … … 1529 1244 SendEvent se = (SendEvent) e; 1530 1245 Communicator sender = se.getSender(); 1531 1246 1532 1247 long t1 = System.currentTimeMillis(); 1533 1248 … … 1541 1256 int eventPeriod = sender.getPeriod(se.getMessageType()); 1542 1257 if (eventPeriod != -1) 1543 schedEventEmergency(new SendEvent(crtTime + eventPeriod, sender, se 1544 .getMessageType())); 1258 schedEventEmergency(new SendEvent(crtTime + eventPeriod, sender, se.getMessageType())); 1545 1259 } 1546 1260 return; … … 1553 1267 int eventPeriod = sender.getPeriod(se.getMessageType()); 1554 1268 if (eventPeriod != -1) 1555 schedEventEmergency(new SendEvent(crtTime + eventPeriod, sender, se 1556 .getMessageType())); 1269 schedEventEmergency(new SendEvent(crtTime + eventPeriod, sender, se.getMessageType())); 1557 1270 } 1558 1271 } … … 1591 1304 return; 1592 1305 } 1593 1594 1595 1596 1597 } 1598 1599 1600 1306 } 1307 1601 1308 if (e instanceof ReceiveEvent) { 1602 1309 ReceiveEvent re = (ReceiveEvent) e; 1603 1310 1604 // long tmp = 0; 1605 // long t3 = System.currentTimeMillis(); 1606 // long t4; 1607 // if (re.isUnicast()) { 1608 // long t1 = System.currentTimeMillis(); 1609 // re.receiver.onReceive(re.getMessage(), re.sender); 1610 // long t2 = System.currentTimeMillis(); 1611 // codeTime += t2 - t1; 1612 // } else { 1613 long t1 = System.currentTimeMillis(); 1614 //Petroaca - the DSRC impl 1311 long t1 = System.currentTimeMillis(); 1312 1313 //Petroaca - the DSRC impl 1615 1314 1616 1315 if(re.receiver instanceof CarRunningDSRC) … … 1640 1339 //appr.LaneChangeAssistance(best.getMessage(),((CarInfo)re.receiver)); 1641 1340 re.receiver.onReceive(best.getMessage(), best.getMessageObject(), best.sender); 1642 1643 1341 } 1644 1645 1646 1342 } 1647 1648 1649 1343 } 1650 1344 long t2 = System.currentTimeMillis(); 1651 1345 codeTime += t2 - t1; 1652 1653 1346 } 1654 1655 1656 1347 } 1657 1348 … … 1662 1353 * @param e Event to schedule 1663 1354 */ 1664 public void schedEvent(Event e) {1355 public static void schedEvent(Event e) { 1665 1356 1666 1357 long t1 = System.currentTimeMillis(); … … 1728 1419 * 1729 1420 */ 1730 public void simulateCommunication(SendEvent e) { 1421 public static void simulateCommunication(SendEvent e) { 1422 1423 Road r1 = null; 1424 Point p1 = null; 1425 r1 = (Road) Globals.map.roads.get(e.sender.getRoadIdx()); 1426 p1 = (Point) r1.points.get(e.sender.getPointIdx()); 1427 1428 if (e instanceof UnicastSendEvent) { 1429 long t1 = System.currentTimeMillis(); 1430 Iterator<SimulatedCarInfo> it = cars.iterator(); 1431 ReceiveEvent re = null; 1432 while (it.hasNext()) { 1433 SimulatedCarInfo r = it.next(); 1434 if (r.vehicleId == ((UnicastSendEvent) e).getReceiverId()) { 1435 Road r2 = (Road) Globals.map.roads.get(r.getRealPos().getRoadIdx()); 1436 Point p2 = (Point) r2.points.get(r.getRealPos().getPointIdx()); 1437 1438 // MODIFIED BY CRISTI!!! 1439 // ReceiveEvent er = new ReceiveEvent(e.getTime() + 1, r, e.getMessage()); 1440 1441 re = new ReceiveEvent(e.getTime() + 1, r, e.getMessage(), e.getMessageObject(), e.getMessageType()); 1442 re.setUnicast(true); 1443 1444 schedEvent(re); 1445 if (GPSutil.distance(p1, p2) < WIRELESS_RANGE) { 1446 } else { 1447 System.out.println("EROARE! DISTANCE="+ GPSutil.distance(p1, p2)); 1448 } 1449 // END MODIFIED!!! 1450 return; 1451 } 1452 } 1453 1454 1455 re.setSender(e.getSender()); 1456 1457 schedEvent(re); 1458 1459 long t2 = System.currentTimeMillis(); 1460 netTime += t2 - t1; 1461 1462 } else { 1463 1464 long tmp = 0; 1465 long t3 = System.currentTimeMillis(); 1466 long t4; 1467 1468 // re = new ReceiveEvent(e.getTime() + 1, e.getSender(), e.getMessage()); 1469 1470 1471 int cnt3 = 0; 1472 int pkcnt = 0; 1473 PeanoKey pk = Globals.map.peanoKeys.get(p1.getPeanoKeyIdx()); 1474 1475 ListIterator<PeanoKey> pkit = Globals.map.peanoKeys.listIterator(p1.getPeanoKeyIdx()); 1476 PeanoKey maxpk = null; 1477 1478 double wifirange; 1479 if (e.sender instanceof WirelessTrafficLight){ 1480 maxpk = GPSutil.getMaxSearchBoundPK(p1,Engine.EXTENDED_WIRELESS_RANGE); 1481 wifirange = EXTENDED_WIRELESS_RANGE; 1482 }else{ 1483 wifirange = WIRELESS_RANGE; 1484 maxpk = pk.getMaxpk(); 1485 } 1486 1487 PeanoKey pkx = null; 1488 for (pkx = pkit.next(); pkit.hasNext(); pkx = pkit.next()) { 1489 pkcnt++; 1490 // long tc1 = System.currentTimeMillis(); 1491 if (pkx.compareTo(maxpk) > 0) 1492 break; 1493 // long tc2 = System.currentTimeMillis(); 1494 // comp += (tc2-tc1); 1495 1496 if (pkx.getCars() != null) { 1497 Road r2 = (Road) Globals.map.roads.get(pkx.getRoadIndex()); 1498 Point p2 = (Point) r2.points.get(pkx.getPointIndex()); 1499 if (GPSutil.distance(p1, p2) < 5 * wifirange) { 1500 cnt3 += pkx.getCars().size(); 1501 Iterator<SimulatedCarInfo> it = pkx.getCars().iterator(); 1502 while (it.hasNext()) { 1503 SimulatedCarInfo r = it.next(); 1504 if (e.sender instanceof SimulatedCarInfo) 1505 if (r.getVehicleId() == ((SimulatedCarInfo)e.sender).getVehicleId()) 1506 continue; 1507 r.setLastMediumTransmition(crtTime); 1508 1509 ReceiveEvent re = new ReceiveEvent(e.getTime() + 1, r, e.getMessage(), e.getMessageObject(), e.getMessageType()); 1510 re.setSender(e.getSender()); 1511 schedEvent(re); 1512 } 1513 } 1514 } 1515 } 1516 PeanoKey minpk = null; 1517 if (e.sender instanceof WirelessTrafficLight) 1518 minpk = GPSutil.getMaxSearchBoundPK(p1,-Engine.EXTENDED_WIRELESS_RANGE); 1519 else 1520 minpk = pk.getMinpk(); 1521 1522 pkit = Globals.map.peanoKeys.listIterator(p1.getPeanoKeyIdx()); 1523 1524 if (pkit.hasPrevious()){ 1525 pkx = pkit.previous(); 1526 } 1527 for (; pkit.hasPrevious(); pkx = pkit.previous()) { 1528 pkcnt++; 1529 // long tc1 = System.currentTimeMillis(); 1530 if (pkx.compareTo(minpk) < 0) { 1531 break; 1532 } 1533 // long tc2 = System.currentTimeMillis(); 1534 // comp += (tc2-tc1); 1535 1536 if (pkx.getCars() != null) { 1537 Road r2 = (Road) Globals.map.roads.get(pkx.getRoadIndex()); 1538 Point p2 = (Point) r2.points.get(pkx.getPointIndex()); 1539 double distance = GPSutil.distance(p1, p2); 1540 if (distance < 5 * wifirange) { 1541 Iterator<SimulatedCarInfo> it = pkx.getCars().iterator(); 1542 cnt3 += pkx.getCars().size(); 1543 while (it.hasNext()) { 1544 SimulatedCarInfo r = it.next(); 1545 1546 r.setLastMediumTransmition(crtTime); 1547 1548 if (e.sender instanceof SimulatedCarInfo) 1549 if (r.getVehicleId() == ((SimulatedCarInfo)e.sender).getVehicleId()) 1550 continue; 1551 ReceiveEvent re = new ReceiveEvent(e.getTime() + 1, r, e.getMessage(), e.getMessageObject(), e.getMessageType()); 1552 re.setSender(e.getSender()); 1553 schedEvent(re); 1554 } 1555 } 1556 } 1557 } 1558 1559 for (int j = 0; j < Globals.map.lightsIndices.size(); j++) { 1560 WirelessTrafficLight wtl = (WirelessTrafficLight) Globals.map.allIntersections 1561 .get(Globals.map.lightsIndices.get(j)); 1562 long t1sem = System.currentTimeMillis(); 1563 1564 if (GPSutil.distance(p1, wtl.getMapPoint()) < 5 * WIRELESS_RANGE) { 1565 wtl.setLastMediumTransmition(crtTime); 1566 ReceiveEvent re = new ReceiveEvent(e.getTime() + 1, wtl, e.getMessage(), e.getMessageObject(), e.getMessageType()); 1567 re.setSender(e.getSender()); 1568 schedEvent(re); 1569 } 1570 long t2sem = System.currentTimeMillis(); 1571 semTime += (t2sem - t1sem); 1572 } 1573 1574 t4 = System.currentTimeMillis(); 1575 tmp += t4 - t3; 1576 netTime += tmp; 1577 } 1578 } 1579 1580 //Petroaca - the communication simulation for the emergency channel 1581 public void simulateCommunicationEmergency(SendEvent e) { 1731 1582 1732 1583 Road r1 = null; … … 1754 1605 re.setUnicast(true); 1755 1606 1756 schedEvent(re);1757 if (GPSutil.distance(p1, p2) < WIRELESS_RANGE) {1758 } else {1759 System.out.println("EROARE! DISTANCE="1760 + GPSutil.distance(p1, p2));1761 }1762 // END MODIFIED!!!1763 return;1764 }1765 }1766 1767 re.setSender(e.getSender());1768 schedEvent(re);1769 1770 long t2 = System.currentTimeMillis();1771 netTime += t2 - t1;1772 1773 } else {1774 1775 long tmp = 0;1776 long t3 = System.currentTimeMillis();1777 long t4;1778 1779 // re = new ReceiveEvent(e.getTime() + 1, e.getSender(), e1780 // .getMessage());1781 1782 1783 long comp = 0;1784 // peanoKeys1785 int cnt2 = 0;1786 int cnt3 = 0;1787 int cnt4 = 0;1788 int pkcnt = 0;1789 PeanoKey pk = Globals.map.peanoKeys.get(p1.getPeanoKeyIdx());1790 1791 ListIterator<PeanoKey> pkit = Globals.map.peanoKeys1792 .listIterator(p1.getPeanoKeyIdx());1793 PeanoKey maxpk = null;1794 1795 double wifirange;1796 if (e.sender instanceof WirelessTrafficLight){1797 maxpk = GPSutil.getMaxSearchBoundPK(p1,Engine.EXTENDED_WIRELESS_RANGE);1798 wifirange = EXTENDED_WIRELESS_RANGE;1799 }else{1800 wifirange = WIRELESS_RANGE;1801 maxpk = pk.getMaxpk();1802 }1803 PeanoKey pkx = null;1804 for (pkx = pkit.next(); pkit.hasNext(); pkx = pkit.next()) {1805 pkcnt++;1806 // long tc1 = System.currentTimeMillis();1807 if (pkx.compareTo(maxpk) > 0)1808 break;1809 // long tc2 = System.currentTimeMillis();1810 // comp += (tc2-tc1);1811 1812 if (pkx.getCars() != null) {1813 Road r2 = (Road) Globals.map.roads.get(pkx1814 .getRoadIndex());1815 Point p2 = (Point) r2.points.get(pkx.getPointIndex());1816 if (GPSutil.distance(p1, p2) < 5 * wifirange) {1817 cnt3 += pkx.getCars().size();1818 Iterator<SimulatedCarInfo> it = pkx.getCars()1819 .iterator();1820 while (it.hasNext()) {1821 SimulatedCarInfo r = it.next();1822 if (e.sender instanceof SimulatedCarInfo)1823 if (r.getVehicleId() == ((SimulatedCarInfo)e.sender)1824 .getVehicleId())1825 continue;1826 r.setLastMediumTransmition(crtTime);1827 1828 ReceiveEvent re = new ReceiveEvent(e.getTime() + 1, r, e.getMessage(), e.getMessageObject(), e.getMessageType());1829 re.setSender(e.getSender());1830 schedEvent(re);1831 }1832 }1833 }1834 }1835 PeanoKey minpk = null;1836 if (e.sender instanceof WirelessTrafficLight)1837 minpk = GPSutil.getMaxSearchBoundPK(p1,-Engine.EXTENDED_WIRELESS_RANGE);1838 else1839 minpk = pk.getMinpk();1840 1841 pkit = Globals.map.peanoKeys.listIterator(p1.getPeanoKeyIdx());1842 1843 if (pkit.hasPrevious()){1844 pkx = pkit.previous();1845 }1846 for (; pkit.hasPrevious(); pkx = pkit1847 .previous()) {1848 pkcnt++;1849 // long tc1 = System.currentTimeMillis();1850 if (pkx.compareTo(minpk) < 0) {1851 break;1852 }1853 // long tc2 = System.currentTimeMillis();1854 // comp += (tc2-tc1);1855 1856 if (pkx.getCars() != null) {1857 Road r2 = (Road) Globals.map.roads.get(pkx1858 .getRoadIndex());1859 Point p2 = (Point) r2.points.get(pkx.getPointIndex());1860 double distance = GPSutil.distance(p1, p2);1861 if (distance < 5 * wifirange) {1862 Iterator<SimulatedCarInfo> it = pkx.getCars()1863 .iterator();1864 cnt3 += pkx.getCars().size();1865 while (it.hasNext()) {1866 SimulatedCarInfo r = it.next();1867 r.setLastMediumTransmition(crtTime);1868 1869 if (e.sender instanceof SimulatedCarInfo)1870 if (r.getVehicleId() == ((SimulatedCarInfo)e.sender)1871 .getVehicleId())1872 continue;1873 ReceiveEvent re = new ReceiveEvent(e.getTime() + 1, r, e.getMessage(), e.getMessageObject(), e.getMessageType());1874 re.setSender(e.getSender());1875 schedEvent(re);1876 }1877 }1878 }1879 }1880 1881 for (int j = 0; j < Globals.map.lightsIndices.size(); j++) {1882 WirelessTrafficLight wtl = (WirelessTrafficLight) Globals.map.allIntersections1883 .get(Globals.map.lightsIndices.get(j));1884 long t1sem = System.currentTimeMillis();1885 1886 if (GPSutil.distance(p1, wtl.getMapPoint()) < 5 * WIRELESS_RANGE) {1887 wtl.setLastMediumTransmition(crtTime);1888 ReceiveEvent re = new ReceiveEvent(e.getTime() + 1, wtl, e.getMessage(), e.getMessageObject(), e.getMessageType());1889 re.setSender(e.getSender());1890 schedEvent(re);1891 }1892 long t2sem = System.currentTimeMillis();1893 semTime += (t2sem - t1sem);1894 }1895 1896 t4 = System.currentTimeMillis();1897 tmp += t4 - t3;1898 netTime += tmp;1899 }1900 }1901 1902 //Petroaca - the communication simulation for the emergency channel1903 public void simulateCommunicationEmergency(SendEvent e) {1904 1905 Road r1 = null;1906 Point p1 = null;1907 r1 = (Road) Globals.map.roads.get(e.sender.getRoadIdx());1908 p1 = (Point) r1.points.get(e.sender.getPointIdx());1909 1910 if (e instanceof UnicastSendEvent) {1911 long t1 = System.currentTimeMillis();1912 Iterator<SimulatedCarInfo> it = cars.iterator();1913 ReceiveEvent re = null;1914 while (it.hasNext()) {1915 SimulatedCarInfo r = it.next();1916 if (r.vehicleId == ((UnicastSendEvent) e).getReceiverId()) {1917 Road r2 = (Road) Globals.map.roads.get(r.getRealPos()1918 .getRoadIdx());1919 Point p2 = (Point) r2.points.get(r.getRealPos()1920 .getPointIdx());1921 1922 // MODIFIED BY CRISTI!!!1923 // ReceiveEvent er = new ReceiveEvent(e.getTime() + 1, r,1924 // e.getMessage());1925 1926 re = new ReceiveEvent(e.getTime() + 1, r, e.getMessage(), e.getMessageObject(), e.getMessageType());1927 re.setUnicast(true);1928 1929 1607 schedEventEmergency(re); 1930 1608 if (GPSutil.distance(p1, p2) < WIRELESS_RANGE) { … … 1954 1632 1955 1633 1956 long comp = 0;1957 // peanoKeys1958 int cnt2 = 0;1959 1634 int cnt3 = 0; 1960 int cnt4 = 0;1961 1635 int pkcnt = 0; 1962 1636 PeanoKey pk = Globals.map.peanoKeys.get(p1.getPeanoKeyIdx()); … … 2151 1825 dc.setRealPos(car); 2152 1826 int t = 1 + random.nextInt(fps / 2 - 5); 2153 schedEvent(new SendEvent(crtTime + t, dc, 2154 SimulatedCarInfo.STANDARD_MESSAGE_ID)); 1827 schedEvent(new SendEvent(crtTime + t, dc,SimulatedCarInfo.STANDARD_MESSAGE_ID)); 2155 1828 cars.add(dc); 2156 dc.init(); 1829 dc.init(); 2157 1830 } 2158 1831 else … … 2294 1967 Random r; 2295 1968 int randId=0; 2296 if( this.simulationProtocol==Globals.PROTOCOL_DSRC)1969 if(Engine.simulationProtocol==Globals.PROTOCOL_DSRC) 2297 1970 { 2298 1971 r=new Random(); … … 2333 2006 car.move(); 2334 2007 2335 if ( this.simulationType == Globals.routePlanConstants.DYNAMIC_SELF_ROUTE) {2008 if (Engine.simulationType == RoutingConstants.DYNAMIC_SELF_ROUTE) { 2336 2009 QueryGeneration.generateQueryForCar(car); 2337 2010 } 2338 2011 2339 if ( this.simulationType == Globals.routePlanConstants.DYNAMIC_INFRASTRUCTURE_ROUTE) {2012 if (Engine.simulationType == RoutingConstants.DYNAMIC_INFRASTRUCTURE_ROUTE) { 2340 2013 GuidanceRequest.generateRequestToInfrastructure(car); 2341 2014 } … … 2347 2020 // Petroaca - emergency messages schedule 2348 2021 2349 if( this.simulationProtocol==Globals.PROTOCOL_DSRC && car.ID==randId && crtTime%50==0)2022 if(Engine.simulationProtocol==Globals.PROTOCOL_DSRC && car.ID==randId && crtTime%50==0) 2350 2023 { 2351 2024 EmergencyRequest.generateEmergencyMessage(car); … … 2364 2037 car.startMonitor = false; 2365 2038 } 2366 // 2 hardcoded exits for manhatan 2367 // if (car.roadIdx == 2 && car.pointIdx < 7) 2368 // car.finished = true; 2369 // if (car.roadIdx == 13 && car.pointIdx < 95) 2370 // car.finished = true; 2371 // if (car.totalDistance > 10){ 2372 // System.out.println("Fuel: "+ car.em.fc / 1000); 2373 // car.finished = true; 2374 // } 2039 2375 2040 car.updateCarToPointMapping(); 2376 2041 … … 2392 2057 } else { 2393 2058 // it's still on the same road 2394 newVector 2395 .addCarInstance(Globals.map.roads.get(j).carsOnThisRoad 2396 .get(k)); 2059 newVector.addCarInstance(Globals.map.roads.get(j).carsOnThisRoad.get(k)); 2397 2060 } 2398 2061 NCARS++; … … 2405 2068 Globals.map.roads.get(j).carsOnThisRoad = newVector; 2406 2069 } 2407 2408 // Iterator it2=cars.iterator(); 2409 // int countt2=0; 2410 // 2411 // while(it2.hasNext()) { 2412 // SimulatedCarInfo sci=(SimulatedCarInfo)it2.next(); 2413 // 2414 // if(sci.pulseMessage) 2415 // countt2++; 2416 // } 2417 // System.out.println(" -- "+countt2+" / "+cars.size()); 2418 2070 2419 2071 2420 2072 if (!outputToFile) … … 2505 2157 Engine e = Globals.engine = new Engine(false, true); 2506 2158 e.init(); 2507 // Thread t = new Thread ( new Runnable(){2508 2159 // public void run(){ 2509 2160 // Globals.demo = new Display();
Note: See TracChangeset
for help on using the changeset viewer.