Changeset 111 for proiecte/ptvs/src/vnsim/core/Engine.java
- Timestamp:
- Jan 13, 2010, 6:45:16 PM (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
proiecte/ptvs/src/vnsim/core/Engine.java
r87 r111 16 16 import java.util.ListIterator; 17 17 import java.util.Random; 18 import java.util.Vector; 18 19 import java.util.concurrent.ExecutorService; 19 20 import java.util.concurrent.Executors; … … 87 88 public class Engine { 88 89 90 91 /** 92 * PTVS Project 93 */ 94 public ExecutorService executor[];/////////////////////////////////////// thread pool with Executors //////////////////// 95 96 public void start(Event e, int i) throws IOException { 97 98 executor[i].submit(new EventProcessingThread(this,e)); 99 } 100 101 int dimens; 102 89 103 public static int numThreads; 104 105 90 106 91 107 /* EZCab - file informations about the client */ … … 106 122 DataInputStream dis = null; 107 123 108 public static int fps; 109 110 static ArrayList<Event> eventQueue = null; 124 public int fps; 125 126 static Vector<Event> eventQueue = null; 127 static int nrEvThreads = 0; 111 128 112 129 //Petroaca - the DSRC emergency channel … … 115 132 public boolean pauseFlag = true; 116 133 117 static boolean doneFlag = false; 118 119 boolean doneFlagEmergency=false; 120 121 public static ArrayList<SimulatedCarInfo> cars = null; 122 123 public static int crtTime = 0; 134 135 boolean doneFlag = false,doneFlagEmergency=false; 136 137 public ArrayList<SimulatedCarInfo> cars = null; 138 139 public int crtTime = 0; 124 140 125 141 public long t0 = 0L; … … 129 145 boolean runtimeMobility; 130 146 131 public staticboolean disableComm = false;147 public boolean disableComm = false; 132 148 133 149 File fText = null, fBin = null; … … 140 156 double lastSimSecond = 0.0; 141 157 142 int eventsTime = 0, mobilityTime = 0;//, netTime = 0, codeTime = 0; 143 144 static int cleanupTime = 0; 145 static int netTime = 0; 146 static int codeTime = 0; 147 static int schedTime = 0; 148 149 int moveTime = 0; 150 151 static int semTime = 0; 158 int eventsTime = 0, mobilityTime = 0, cleanupTime = 0, netTime = 0, codeTime = 0; 159 160 161 int schedTime = 0, moveTime = 0, semTime = 0; 162 152 163 153 164 public double maxControlDelay = 0; … … 215 226 216 227 228 229 230 231 /** 232 * PTVS project 233 */ 217 234 ArrayList<Intersection> intersectii = map.allIntersections; 218 219 235 numThreads=intersectii.size(); 236 237 executor=new ExecutorService[numThreads]; 238 239 for(int i=0;i<numThreads;i++){ 240 executor[i] = Executors.newFixedThreadPool(1); // un singur thread e mereu refolosit pt fiecare intersectie.. ar putea fi mai multe 241 } 242 //////////////////////////////////////////////////////////////////////////// 243 220 244 221 245 for(int q=0;q<intersectii.size();q++){ … … 274 298 275 299 // init cars and events 276 eventQueue = new ArrayList<Event>();300 eventQueue = new Vector<Event>(); 277 301 278 302 … … 306 330 System.out.println("nu ai cum sa intri pe aici!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! cars este vida"); 307 331 SimulatedCarInfo r = it.next(); 308 schedEvent(new SendEvent(4, r, SimulatedCarInfo.STANDARD_MESSAGE_ID)); 309 System.out.println("add in the queue.."); 332 333 schedEvent(new SendEvent(4, r, 334 SimulatedCarInfo.STANDARD_MESSAGE_ID)); 310 335 } 311 336 … … 314 339 315 340 /* Szekeres A. START_MODIFY - add broadcast event for each traffic light*/ 316 /*for(int i=0;i<Globals.map.allIntersections.size();i++) { 317 Intersection sender = Globals.map.allIntersections.get(i); 318 if (sender instanceof EmissionsTrafficLight) 319 { 320 ((EmissionsTrafficLight)sender).init(); 321 System.out.println("add in the queue.."); 322 schedEvent(new SendEvent(4, (EmissionsTrafficLight)sender, (int)Globals.PROT_TL_FEEDBACK)); 323 } 324 } */ 341 for(int i=0;i<Globals.map.allIntersections.size();i++) { 342 Intersection sender = Globals.map.allIntersections.get(i); 343 if (sender instanceof EmissionsTrafficLight) 344 { 345 ((EmissionsTrafficLight)sender).init(); 346 schedEvent(new SendEvent(4, (EmissionsTrafficLight)sender, (int)Globals.PROT_TL_FEEDBACK)); 347 } 348 } 325 349 /* Szekeres A. STOP_MODIFY */ 326 350 } … … 373 397 System.out.println(); 374 398 375 Road r = Globals.map.roads.get(1);399 /*Road r = Globals.map.roads.get(1); 376 400 System.out.print("road:"+r.getName()); 377 401 System.out.println(" r.points.size():"+r.points.size()); … … 383 407 384 408 System.out.println("distanta: " +(p1.getDistance() - p2.getDistance())); 385 System.out.println("distanta: " +GPSutil.distance(p1, p2)); 409 System.out.println("distanta: " +GPSutil.distance(p1, p2));*/ 386 410 System.out.println("__________________end init____________________________\n"); 387 411 … … 394 418 public void reset() 395 419 { 396 eventQueue = new ArrayList<Event>();420 eventQueue = new Vector<Event>(); 397 421 cars = new ArrayList<SimulatedCarInfo>(); 398 422 … … 452 476 pauseFlag = false; 453 477 while (!pauseFlag && !doneFlag) { 454 step(); 478 step(); 455 479 } 456 480 } … … 488 512 489 513 490 /////////////////////////////////////// thread pool with Executors //////////////////// 491 /** 492 * PTVS Project 493 */ 494 public ExecutorService executor[]; 495 496 public void start(Event e, int i) throws IOException { 497 498 executor[i].submit(new EventProcessingThread(e, this)); 499 } 514 500 515 501 516 ////////////////////////////////////// step /////////////////////////////////////////////////////////// … … 505 520 */ 506 521 507 static int cnt = 0;508 522 509 523 /** 510 524 * PTVS 511 525 */ 512 int dimens = 0; 513 int eventTime=0; 526 514 527 515 528 public void step() { 516 529 530 int cnt = 0; 517 531 518 532 if(Globals.SOCIALNETWORK == 1){ … … 568 582 { 569 583 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 570 ,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, Engine.cars.size(),Globals.EMERGENCY_RECEIVED,Globals.EMERGENCY_SENT));584 ,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)); 571 585 572 586 … … 602 616 if (lastTime == -1) 603 617 lastTime = t0; 618 long realTime = t1 - lastTime; 604 619 lastTime = t1; 605 620 … … 644 659 Globals.demo.st.setSimulationTime((float) ((crtTime * 1000) / fps)); 645 660 //Globals.demo.st.setSemTime(mycnt); 646 Globals.demo.st.setNrEvents(eventQueue.size() );661 Globals.demo.st.setNrEvents(eventQueue.size() + nrEvThreads); 647 662 Globals.demo.st.setCarsNo(cars.size()); 648 663 } … … 668 683 } 669 684 } 685 686 /*while (true) { 687 if (eventQueue.size() == 0) { 688 doneFlag = true; 689 return; 690 } 691 692 Event e = (Event) eventQueue.get(0); 693 if (e.getTime() == crtTime) { 694 playEvent(null); 695 cnt++; 696 } else 697 break; 698 }*/ 699 670 700 671 701 /** … … 682 712 //System.out.println("numThreads:"+numThreads); 683 713 684 executor=new ExecutorService[numThreads]; 685 686 for(int i=0;i<numThreads;i++){ 687 executor[i] = Executors.newFixedThreadPool(1); // un singur thread e mereu refolosit pt fiecare intersectie.. ar putea fi mai multe 688 } 714 689 715 690 716 … … 693 719 if (eventQueue.size() == 0) { 694 720 doneFlag = true; 721 System.out.println("\ndoneFlag = true "); 695 722 return; 696 723 } … … 698 725 // extrag un eveniment din coada !!!!! acum doar il extrag.. cand intru in playEvent il si scot cu remove(0) 699 726 Event e = (Event) eventQueue.get(0); 700 701 /*if (e.getTime() == crtTime) {702 System.out.println("----||||--------->> e.getTime() == crtTime== "+crtTime);703 eventTime = crtTime;704 }*/705 706 727 if (e == null) continue; 728 729 if (e.getTime() == crtTime) { 730 //System.out.println("------------->> e.getTime() == crtTime== "+crtTime); 731 732 } 707 733 708 734 if(dimens!=eventQueue.size()){ … … 724 750 725 751 synchronized (eventQueue) { 726 e = (Event)eventQueue.remove(0); 752 e = eventQueue.remove(0); 753 nrEvThreads++; 754 //System.out.println("remove from eventQueue"); 727 755 } 728 756 … … 764 792 765 793 } 794 766 795 /** 767 796 * PTVS Project … … 775 804 776 805 System.out.print("SendEvent: "); 777 778 806 //processingThreads[indexClosestIntersection] = new SendEventProcessingThread(e,this); 779 807 } 780 808 else { 781 809 System.out.print("ReceiveEvent: "); 782 783 810 //processingThreads[indexClosestIntersection] = new ReceiveEventProcessingThread(e,this); 784 811 } … … 788 815 try { 789 816 start(e,indexClosestIntersection); 817 cnt++; 790 818 791 819 } catch (IOException e1) { 792 820 System.out.println("aici exceptie"); 793 821 e1.printStackTrace(); 794 822 } 795 823 796 824 //new Thread(processingThreads[indexClosestIntersection]).start(); 797 cnt++;825 798 826 } 799 827 else … … 802 830 803 831 else if (e.getTime() == crtTime) { 832 System.out.println("OtherEvent "); 804 833 playEvent(null); 805 834 cnt++; 835 806 836 } else 807 837 break; … … 813 843 //increment simulation time 814 844 crtTime++; 845 //System.out.println("Increment crtTime! crtTime:"+crtTime); 815 846 816 847 long t3 = System.currentTimeMillis(); … … 1041 1072 ////////////////////////////////////////// playEvent //////////////////////////////////////////////////////////////// 1042 1073 1043 staticlong messagesSize = 0;1044 staticlong carInfosSize = 0;1074 long messagesSize = 0; 1075 long carInfosSize = 0; 1045 1076 /** 1046 1077 * Execute the following event in the eventQueue … … 1048 1079 */ 1049 1080 public void playEvent(Event ev) { 1081 //public void playEvent() { 1050 1082 1051 1083 //System.out.println(">> Thread:"+Thread.currentThread().getName()); … … 1058 1090 Event e = null; 1059 1091 if (ev == null) { 1060 synchronized (eventQueue) { 1061 e = eventQueue.remove(0); 1062 } 1063 } else e = ev; 1092 synchronized (eventQueue) { 1093 e = eventQueue.remove(0); 1094 //System.out.println("remove from eventQueue"); 1095 } 1096 } else { 1097 e = ev; 1098 synchronized (eventQueue) { nrEvThreads--; } 1099 } 1064 1100 //System.out.print(e.toString()+", "); 1065 1101 //System.out.flush(); … … 1082 1118 while (it.hasNext()) { 1083 1119 SimulatedCarInfo r = it.next(); 1084 1120 synchronized (r.trafficDB) { 1085 1121 Iterator<CarInfo> j = r.trafficDB.iterator(); 1086 1122 while (j.hasNext()) { … … 1091 1127 } 1092 1128 } 1129 } 1093 1130 carInfosSize += r.trafficDB.size(); 1094 1131 } … … 1124 1161 1125 1162 if (e instanceof SendEvent) { /////////////////////////////// eveniment SEND 1126 1127 /**1128 * cod mutat in metoda run a lui SendEventProcessingThread - intre timp m-am razgandit.. am pornit threaduri in metoda step1129 */1130 1131 /*SendEventProcessingThread thread=new SendEventProcessingThread(e, crtTime, disableComm, messagesSize);1132 synchronized (eventQueue) {1133 if(!thread.isAlive()) {1134 thread.start();1135 System.out.println(".........................thread started");1136 }1137 }*/1138 1139 1163 1140 1164 SendEvent se = (SendEvent) e; … … 1410 1434 * @param e Event to schedule 1411 1435 */ 1412 public staticvoid schedEvent(Event e) {1436 public void schedEvent(Event e) { 1413 1437 1414 1438 long t1 = System.currentTimeMillis(); … … 1436 1460 idx++; 1437 1461 eventQueue.add(idx, e); 1462 //System.out.println("add in eventQueue.."); 1438 1463 } else { 1439 1464 eventQueue.add(-(idx + 1), e); 1465 //System.out.println("add in eventQueue...."); 1440 1466 } 1441 1467 } … … 1480 1506 * 1481 1507 */ 1482 public staticvoid simulateCommunication(SendEvent e) {1508 public void simulateCommunication(SendEvent e) { 1483 1509 1484 1510 Road r1 = null;
Note: See TracChangeset
for help on using the changeset viewer.