Index: proiecte/ptvs/src/vnsim/core/Engine.java
===================================================================
--- proiecte/ptvs/src/vnsim/core/Engine.java	(revision 87)
+++ proiecte/ptvs/src/vnsim/core/Engine.java	(revision 111)
@@ -16,4 +16,5 @@
 import java.util.ListIterator;
 import java.util.Random;
+import java.util.Vector;
 import java.util.concurrent.ExecutorService;
 import java.util.concurrent.Executors;
@@ -87,5 +88,20 @@
 public class Engine {
 	
+	
+	/**
+	 * PTVS Project
+	 */
+	public ExecutorService executor[];/////////////////////////////////////// thread pool with Executors ////////////////////
+	
+	public void start(Event e, int i) throws IOException {
+	    
+	      executor[i].submit(new EventProcessingThread(this,e));
+	  }
+	
+	int dimens;
+	
 	public static int numThreads;
+	
+	
 	
 	/* EZCab - file informations about the client */
@@ -106,7 +122,8 @@
 	DataInputStream dis = null;
 
-	public static int fps;
-
-	static ArrayList<Event> eventQueue = null;
+	public int fps;
+
+	static Vector<Event> eventQueue = null;
+	static int nrEvThreads = 0;
 	
 	//Petroaca - the DSRC emergency channel
@@ -115,11 +132,10 @@
 	public boolean pauseFlag = true;
 
-	static boolean doneFlag = false;
-
-	boolean doneFlagEmergency=false;
-
-	public static ArrayList<SimulatedCarInfo> cars = null;
-
-	public static int crtTime = 0;
+
+	boolean doneFlag = false,doneFlagEmergency=false;
+
+	public ArrayList<SimulatedCarInfo> cars = null;
+
+	public int crtTime = 0;
 
 	public long t0 = 0L;
@@ -129,5 +145,5 @@
 	boolean runtimeMobility;
 
-	public static boolean disableComm = false;
+	public boolean disableComm = false;
 
 	File fText = null, fBin = null;
@@ -140,14 +156,9 @@
 	double lastSimSecond = 0.0;
 	
-	int eventsTime = 0, mobilityTime = 0;//, netTime = 0, codeTime = 0;
-
-	static int cleanupTime = 0;
-	static int netTime = 0;
-	static int codeTime = 0;
-	static int schedTime = 0;
-
-	int moveTime = 0;
-
-	static int semTime = 0;
+	int eventsTime = 0, mobilityTime = 0, cleanupTime = 0, netTime = 0, codeTime = 0;
+
+
+	int schedTime = 0,  moveTime = 0, semTime = 0;
+
 
 	public double maxControlDelay = 0;
@@ -215,7 +226,20 @@
 		
 			
+		
+		
+		
+		/**
+		 * PTVS project
+		 */
 		ArrayList<Intersection> intersectii = map.allIntersections;
-		
 		numThreads=intersectii.size();
+				
+		executor=new ExecutorService[numThreads];
+		
+		for(int i=0;i<numThreads;i++){
+			executor[i] = Executors.newFixedThreadPool(1); // un singur thread e mereu refolosit pt fiecare intersectie.. ar putea fi mai multe
+		}
+		////////////////////////////////////////////////////////////////////////////
+		
 		
 		for(int q=0;q<intersectii.size();q++){
@@ -274,5 +298,5 @@
 
 		// init cars and events
-		eventQueue = new ArrayList<Event>();
+		eventQueue = new Vector<Event>();
 		
 		
@@ -306,6 +330,7 @@
 					System.out.println("nu ai cum sa intri pe aici!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! cars este vida");
 					SimulatedCarInfo r = it.next();
-					schedEvent(new SendEvent(4, r, SimulatedCarInfo.STANDARD_MESSAGE_ID));
-					System.out.println("add in the queue..");
+				
+				schedEvent(new SendEvent(4, r,
+						SimulatedCarInfo.STANDARD_MESSAGE_ID));
 				}
 				
@@ -314,13 +339,12 @@
 				
 				/* Szekeres A. START_MODIFY - add broadcast event for each traffic light*/
-				/*for(int i=0;i<Globals.map.allIntersections.size();i++) {
-					Intersection sender = Globals.map.allIntersections.get(i);
-					if (sender instanceof EmissionsTrafficLight)
-					{
-						((EmissionsTrafficLight)sender).init();
-						System.out.println("add in the queue..");
-						schedEvent(new SendEvent(4, (EmissionsTrafficLight)sender, (int)Globals.PROT_TL_FEEDBACK));
-					}
-				}	*/		
+			for(int i=0;i<Globals.map.allIntersections.size();i++) {
+				Intersection sender = Globals.map.allIntersections.get(i);
+				if (sender instanceof EmissionsTrafficLight)
+				{
+					((EmissionsTrafficLight)sender).init();
+					schedEvent(new SendEvent(4, (EmissionsTrafficLight)sender, (int)Globals.PROT_TL_FEEDBACK));
+				}
+			}			
 				/* Szekeres A. STOP_MODIFY */
 		}
@@ -373,5 +397,5 @@
 		System.out.println();
 		
-		Road r = Globals.map.roads.get(1);
+		/*Road r = Globals.map.roads.get(1);
 		System.out.print("road:"+r.getName());
 		System.out.println("  r.points.size():"+r.points.size());
@@ -383,5 +407,5 @@
 		
 		System.out.println("distanta: " +(p1.getDistance() - p2.getDistance()));
-		System.out.println("distanta: " +GPSutil.distance(p1, p2));
+		System.out.println("distanta: " +GPSutil.distance(p1, p2));*/
 		System.out.println("__________________end init____________________________\n");
 				
@@ -394,5 +418,5 @@
 	public void reset()
 	{
-		eventQueue = new ArrayList<Event>();
+		eventQueue = new Vector<Event>();
 		cars = new ArrayList<SimulatedCarInfo>();
 
@@ -452,5 +476,5 @@
 		pauseFlag = false;
 		while (!pauseFlag && !doneFlag) {
-			step();
+			step();			
 		}
 	}
@@ -488,14 +512,5 @@
 	
 	
-	/////////////////////////////////////// thread pool with Executors ////////////////////
-	/**
-	 * PTVS Project
-	 */
-	public ExecutorService executor[];
-	
-	public void start(Event e, int i) throws IOException {
-	    
-	      executor[i].submit(new EventProcessingThread(e, this));
-	  }
+
 	
 	////////////////////////////////////// step ///////////////////////////////////////////////////////////
@@ -505,14 +520,13 @@
 	 */
 	
-	static int cnt = 0;
 	
 	/**
 	 * PTVS
 	 */
-	int dimens = 0;
-	int eventTime=0;
+	
 	
 	public void step() {
 		
+		int cnt = 0;
 		
 		if(Globals.SOCIALNETWORK == 1){
@@ -568,5 +582,5 @@
 			{
 				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
-						,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));
+						,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));
 				
 				
@@ -602,4 +616,5 @@
 			if (lastTime == -1)
 				lastTime = t0;
+			long realTime = t1 - lastTime;
 			lastTime = t1;
 
@@ -644,5 +659,5 @@
 			Globals.demo.st.setSimulationTime((float) ((crtTime * 1000) / fps));
 			//Globals.demo.st.setSemTime(mycnt);
-			Globals.demo.st.setNrEvents(eventQueue.size());
+			Globals.demo.st.setNrEvents(eventQueue.size() + nrEvThreads);
 			Globals.demo.st.setCarsNo(cars.size());			
 		}
@@ -668,4 +683,19 @@
 			}
 		}
+		
+		/*while (true) {
+			if (eventQueue.size() == 0) {
+				doneFlag = true;
+				return;
+			}
+			
+			Event e = (Event) eventQueue.get(0);
+			if (e.getTime() == crtTime) {
+				playEvent(null);
+				cnt++;
+			} else
+				break;
+		}*/
+
 		
 		/**
@@ -682,9 +712,5 @@
 		//System.out.println("numThreads:"+numThreads);
 		
-		executor=new ExecutorService[numThreads];
-		
-		for(int i=0;i<numThreads;i++){
-			executor[i] = Executors.newFixedThreadPool(1); // un singur thread e mereu refolosit pt fiecare intersectie.. ar putea fi mai multe
-		}
+		
 		 
 		
@@ -693,4 +719,5 @@
 			if (eventQueue.size() == 0) {
 				doneFlag = true;
+				System.out.println("\ndoneFlag = true ");
 				return;
 			}
@@ -698,11 +725,10 @@
 			// extrag un eveniment din coada !!!!! acum doar il extrag.. cand intru in playEvent il si scot cu remove(0)
 			Event e = (Event) eventQueue.get(0);
-			
-			/*if (e.getTime() == crtTime) {
-				System.out.println("----||||--------->> e.getTime() == crtTime== "+crtTime);
-				eventTime = crtTime;
-			}*/
-			
 			if (e == null) continue;
+			
+			if (e.getTime() == crtTime) {
+				//System.out.println("------------->> e.getTime() == crtTime== "+crtTime);
+				
+			}
 			
 			if(dimens!=eventQueue.size()){
@@ -724,5 +750,7 @@
 				
 				synchronized (eventQueue) {
-					e = (Event)eventQueue.remove(0);
+					e = eventQueue.remove(0);
+					nrEvThreads++;
+					//System.out.println("remove from eventQueue");
 				}
 				
@@ -764,4 +792,5 @@
 						
 				}
+				
 				/**
 				 * PTVS Project
@@ -775,10 +804,8 @@
 						
 							System.out.print("SendEvent: ");
-														
 							//processingThreads[indexClosestIntersection] = new SendEventProcessingThread(e,this);
 						}
 					else {					
 							System.out.print("ReceiveEvent: ");
-														
 							//processingThreads[indexClosestIntersection] = new ReceiveEventProcessingThread(e,this);
 						}
@@ -788,12 +815,13 @@
 					try {
 						start(e,indexClosestIntersection);
+						cnt++;
 						
 					} catch (IOException e1) {
-						
+						System.out.println("aici exceptie");
 						e1.printStackTrace();
 					}
 					
 					//new Thread(processingThreads[indexClosestIntersection]).start();
-					cnt++;
+					
 				}
 				else
@@ -802,6 +830,8 @@
 			
 			else if (e.getTime() == crtTime) {
+				System.out.println("OtherEvent ");
 				playEvent(null);
 				cnt++;
+				
 			} else
 				break;
@@ -813,4 +843,5 @@
 		//increment simulation time
 		crtTime++;
+		//System.out.println("Increment crtTime!  crtTime:"+crtTime);
 		
 		long t3 = System.currentTimeMillis();
@@ -1041,6 +1072,6 @@
 ////////////////////////////////////////// playEvent ////////////////////////////////////////////////////////////////	
 
-	static long messagesSize = 0;
-	static long carInfosSize = 0;
+	long messagesSize = 0;
+	long carInfosSize = 0;
 	/**
 	 * Execute the following event in the eventQueue
@@ -1048,4 +1079,5 @@
 	 */
 	public void playEvent(Event ev) {
+	//public void playEvent() {
 		
 		//System.out.println(">> Thread:"+Thread.currentThread().getName());
@@ -1058,8 +1090,12 @@
 		Event e = null;
 		if (ev == null) {
-		synchronized (eventQueue) {
-			e = eventQueue.remove(0);
-		}
-		} else e = ev;
+			synchronized (eventQueue) {
+				e = eventQueue.remove(0);
+				//System.out.println("remove from eventQueue");
+			}
+		} else {
+			e = ev;
+			synchronized (eventQueue) { nrEvThreads--; }
+		}
 		//System.out.print(e.toString()+", ");
 		//System.out.flush();
@@ -1082,5 +1118,5 @@
 			while (it.hasNext()) {
 				SimulatedCarInfo r = it.next();
-
+synchronized (r.trafficDB) {
 				Iterator<CarInfo> j = r.trafficDB.iterator();
 				while (j.hasNext()) {
@@ -1091,4 +1127,5 @@
 					}
 				}
+}
 				carInfosSize += r.trafficDB.size();
 			}
@@ -1124,17 +1161,4 @@
 		
 		if (e instanceof SendEvent) {	/////////////////////////////// eveniment SEND
-			
-			/**
-			 * cod mutat in metoda run a lui SendEventProcessingThread - intre timp m-am razgandit.. am pornit threaduri in metoda step 
-			 */
-			
-			/*SendEventProcessingThread thread=new SendEventProcessingThread(e, crtTime, disableComm, messagesSize);
-			synchronized (eventQueue) {
-				if(!thread.isAlive()) {
-					thread.start();
-					System.out.println(".........................thread started");
-				}
-			}*/
-			
 			
 			SendEvent se = (SendEvent) e;
@@ -1410,5 +1434,5 @@
 	 * @param e Event to schedule
 	 */
-	public static void schedEvent(Event e) {
+	public void schedEvent(Event e) {
 
 		long t1 = System.currentTimeMillis();
@@ -1436,6 +1460,8 @@
 					idx++;
 				eventQueue.add(idx, e);
+				//System.out.println("add in eventQueue..");
 			} else {
 				eventQueue.add(-(idx + 1), e);
+				//System.out.println("add in eventQueue....");
 			}
 		}
@@ -1480,5 +1506,5 @@
 	 * 
 	 */
-	public static void simulateCommunication(SendEvent e) {
+	public void simulateCommunication(SendEvent e) {
 		
 		Road r1 = null;
