Changeset 111 for proiecte


Ignore:
Timestamp:
Jan 13, 2010, 6:45:16 PM (14 years ago)
Author:
(none)
Message:
 
File:
1 edited

Legend:

Unmodified
Added
Removed
  • proiecte/ptvs/src/vnsim/core/Engine.java

    r87 r111  
    1616import java.util.ListIterator;
    1717import java.util.Random;
     18import java.util.Vector;
    1819import java.util.concurrent.ExecutorService;
    1920import java.util.concurrent.Executors;
     
    8788public class Engine {
    8889       
     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       
    89103        public static int numThreads;
     104       
     105       
    90106       
    91107        /* EZCab - file informations about the client */
     
    106122        DataInputStream dis = null;
    107123
    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;
    111128       
    112129        //Petroaca - the DSRC emergency channel
     
    115132        public boolean pauseFlag = true;
    116133
    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;
    124140
    125141        public long t0 = 0L;
     
    129145        boolean runtimeMobility;
    130146
    131         public static boolean disableComm = false;
     147        public boolean disableComm = false;
    132148
    133149        File fText = null, fBin = null;
     
    140156        double lastSimSecond = 0.0;
    141157       
    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
    152163
    153164        public double maxControlDelay = 0;
     
    215226               
    216227                       
     228               
     229               
     230               
     231                /**
     232                 * PTVS project
     233                 */
    217234                ArrayList<Intersection> intersectii = map.allIntersections;
    218                
    219235                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               
    220244               
    221245                for(int q=0;q<intersectii.size();q++){
     
    274298
    275299                // init cars and events
    276                 eventQueue = new ArrayList<Event>();
     300                eventQueue = new Vector<Event>();
    277301               
    278302               
     
    306330                                        System.out.println("nu ai cum sa intri pe aici!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! cars este vida");
    307331                                        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));
    310335                                }
    311336                               
     
    314339                               
    315340                                /* 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                        }                       
    325349                                /* Szekeres A. STOP_MODIFY */
    326350                }
     
    373397                System.out.println();
    374398               
    375                 Road r = Globals.map.roads.get(1);
     399                /*Road r = Globals.map.roads.get(1);
    376400                System.out.print("road:"+r.getName());
    377401                System.out.println("  r.points.size():"+r.points.size());
     
    383407               
    384408                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));*/
    386410                System.out.println("__________________end init____________________________\n");
    387411                               
     
    394418        public void reset()
    395419        {
    396                 eventQueue = new ArrayList<Event>();
     420                eventQueue = new Vector<Event>();
    397421                cars = new ArrayList<SimulatedCarInfo>();
    398422
     
    452476                pauseFlag = false;
    453477                while (!pauseFlag && !doneFlag) {
    454                         step();
     478                        step();                 
    455479                }
    456480        }
     
    488512       
    489513       
    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
    500515       
    501516        ////////////////////////////////////// step ///////////////////////////////////////////////////////////
     
    505520         */
    506521       
    507         static int cnt = 0;
    508522       
    509523        /**
    510524         * PTVS
    511525         */
    512         int dimens = 0;
    513         int eventTime=0;
     526       
    514527       
    515528        public void step() {
    516529               
     530                int cnt = 0;
    517531               
    518532                if(Globals.SOCIALNETWORK == 1){
     
    568582                        {
    569583                                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));
    571585                               
    572586                               
     
    602616                        if (lastTime == -1)
    603617                                lastTime = t0;
     618                        long realTime = t1 - lastTime;
    604619                        lastTime = t1;
    605620
     
    644659                        Globals.demo.st.setSimulationTime((float) ((crtTime * 1000) / fps));
    645660                        //Globals.demo.st.setSemTime(mycnt);
    646                         Globals.demo.st.setNrEvents(eventQueue.size());
     661                        Globals.demo.st.setNrEvents(eventQueue.size() + nrEvThreads);
    647662                        Globals.demo.st.setCarsNo(cars.size());                 
    648663                }
     
    668683                        }
    669684                }
     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
    670700               
    671701                /**
     
    682712                //System.out.println("numThreads:"+numThreads);
    683713               
    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               
    689715                 
    690716               
     
    693719                        if (eventQueue.size() == 0) {
    694720                                doneFlag = true;
     721                                System.out.println("\ndoneFlag = true ");
    695722                                return;
    696723                        }
     
    698725                        // extrag un eveniment din coada !!!!! acum doar il extrag.. cand intru in playEvent il si scot cu remove(0)
    699726                        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                        
    706727                        if (e == null) continue;
     728                       
     729                        if (e.getTime() == crtTime) {
     730                                //System.out.println("------------->> e.getTime() == crtTime== "+crtTime);
     731                               
     732                        }
    707733                       
    708734                        if(dimens!=eventQueue.size()){
     
    724750                               
    725751                                synchronized (eventQueue) {
    726                                         e = (Event)eventQueue.remove(0);
     752                                        e = eventQueue.remove(0);
     753                                        nrEvThreads++;
     754                                        //System.out.println("remove from eventQueue");
    727755                                }
    728756                               
     
    764792                                               
    765793                                }
     794                               
    766795                                /**
    767796                                 * PTVS Project
     
    775804                                               
    776805                                                        System.out.print("SendEvent: ");
    777                                                                                                                
    778806                                                        //processingThreads[indexClosestIntersection] = new SendEventProcessingThread(e,this);
    779807                                                }
    780808                                        else {                                 
    781809                                                        System.out.print("ReceiveEvent: ");
    782                                                                                                                
    783810                                                        //processingThreads[indexClosestIntersection] = new ReceiveEventProcessingThread(e,this);
    784811                                                }
     
    788815                                        try {
    789816                                                start(e,indexClosestIntersection);
     817                                                cnt++;
    790818                                               
    791819                                        } catch (IOException e1) {
    792                                                
     820                                                System.out.println("aici exceptie");
    793821                                                e1.printStackTrace();
    794822                                        }
    795823                                       
    796824                                        //new Thread(processingThreads[indexClosestIntersection]).start();
    797                                         cnt++;
     825                                       
    798826                                }
    799827                                else
     
    802830                       
    803831                        else if (e.getTime() == crtTime) {
     832                                System.out.println("OtherEvent ");
    804833                                playEvent(null);
    805834                                cnt++;
     835                               
    806836                        } else
    807837                                break;
     
    813843                //increment simulation time
    814844                crtTime++;
     845                //System.out.println("Increment crtTime!  crtTime:"+crtTime);
    815846               
    816847                long t3 = System.currentTimeMillis();
     
    10411072////////////////////////////////////////// playEvent ////////////////////////////////////////////////////////////////   
    10421073
    1043         static long messagesSize = 0;
    1044         static long carInfosSize = 0;
     1074        long messagesSize = 0;
     1075        long carInfosSize = 0;
    10451076        /**
    10461077         * Execute the following event in the eventQueue
     
    10481079         */
    10491080        public void playEvent(Event ev) {
     1081        //public void playEvent() {
    10501082               
    10511083                //System.out.println(">> Thread:"+Thread.currentThread().getName());
     
    10581090                Event e = null;
    10591091                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                }
    10641100                //System.out.print(e.toString()+", ");
    10651101                //System.out.flush();
     
    10821118                        while (it.hasNext()) {
    10831119                                SimulatedCarInfo r = it.next();
    1084 
     1120synchronized (r.trafficDB) {
    10851121                                Iterator<CarInfo> j = r.trafficDB.iterator();
    10861122                                while (j.hasNext()) {
     
    10911127                                        }
    10921128                                }
     1129}
    10931130                                carInfosSize += r.trafficDB.size();
    10941131                        }
     
    11241161               
    11251162                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 step
    1129                          */
    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                        
    11391163                       
    11401164                        SendEvent se = (SendEvent) e;
     
    14101434         * @param e Event to schedule
    14111435         */
    1412         public static void schedEvent(Event e) {
     1436        public void schedEvent(Event e) {
    14131437
    14141438                long t1 = System.currentTimeMillis();
     
    14361460                                        idx++;
    14371461                                eventQueue.add(idx, e);
     1462                                //System.out.println("add in eventQueue..");
    14381463                        } else {
    14391464                                eventQueue.add(-(idx + 1), e);
     1465                                //System.out.println("add in eventQueue....");
    14401466                        }
    14411467                }
     
    14801506         *
    14811507         */
    1482         public static void simulateCommunication(SendEvent e) {
     1508        public void simulateCommunication(SendEvent e) {
    14831509               
    14841510                Road r1 = null;
Note: See TracChangeset for help on using the changeset viewer.