Start a thread to listen for console input
- Listen for 'commands' on stdin
    the thread is running alongside
    the simulation.
			
			
This commit is contained in:
		
							parent
							
								
									5fde1eee7e
								
							
						
					
					
						commit
						bd68322a75
					
				
							
								
								
									
										35
									
								
								src/InputListener.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										35
									
								
								src/InputListener.java
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,35 @@ | ||||
| import java.util.Scanner; | ||||
| 
 | ||||
| public class InputListener implements Runnable { | ||||
| 
 | ||||
|     private volatile String input = ""; | ||||
|     private volatile boolean hasNew; | ||||
|     private final Scanner in; | ||||
|     private boolean stop; | ||||
| 
 | ||||
|     public InputListener() { | ||||
|         in = new Scanner(System.in); | ||||
|     } | ||||
| 
 | ||||
|     @Override | ||||
|     public void run() { | ||||
|         while(!stop) { | ||||
|             input = in.next(); | ||||
|             hasNew = true; | ||||
|         } | ||||
|         System.out.println("Stopped input listener"); | ||||
|     } | ||||
| 
 | ||||
|     public void stop() { | ||||
|         stop = true; | ||||
|     } | ||||
| 
 | ||||
|     public String getInput() { | ||||
|         hasNew = false; | ||||
|         return input; | ||||
|     } | ||||
| 
 | ||||
|     public boolean hasNew() { | ||||
|         return hasNew; | ||||
|     } | ||||
| } | ||||
| @ -20,8 +20,12 @@ public class MakkahCity { | ||||
| 
 | ||||
| 	private static PDate currenttimeManager = firstDayTimeMan; | ||||
| 
 | ||||
| 	private static final InputListener inputListener = new InputListener(); | ||||
| 	private static final Thread t = new Thread(inputListener,"InputThread-Makkah"); | ||||
| 
 | ||||
| 	public static void main(String[] args) { | ||||
| 
 | ||||
| 		t.start(); | ||||
| 		//Gen Camp | ||||
| 		campPerDistrict[District.ALMANSOOR.ordinal()] = new ArrayList<>(); | ||||
| 		campPerDistrict[District.ALAZIZIYA.ordinal()] = new ArrayList<>(); | ||||
| @ -42,6 +46,7 @@ public class MakkahCity { | ||||
| 		setRoutesForCampaigns(Mashier.ARAFAT); | ||||
| 		System.out.println(preSimulationReport()); | ||||
| 		while(!firstDayTimeMan.isEnded()) { | ||||
| 			checkInput(); | ||||
| 			//Start of Every hour | ||||
| 			if (firstDayTimeMan.getCurrentCalendar().get(Calendar.MINUTE) == 0){ | ||||
| 				System.out.println("\n\n" + getStreetsReport()); | ||||
| @ -98,6 +103,7 @@ public class MakkahCity { | ||||
| 		} | ||||
| 		System.out.println("***************STARTING LAST DAY***************"); | ||||
| 		while(!lastDayTimeMan.isEnded()) { | ||||
| 			checkInput(); | ||||
| 			//Start of Every hour | ||||
| 			if (lastDayTimeMan.getCurrentCalendar().get(Calendar.MINUTE) == 0){ | ||||
| 				//TODO: removed break here. now should schedule. | ||||
| @ -146,9 +152,30 @@ public class MakkahCity { | ||||
| 			lastDayTimeMan.step(Calendar.MINUTE, 1); | ||||
| 			//for (int i = 0; i < 46; i++) System.out.print("\b"); | ||||
| 		} | ||||
| 		inputListener.stop(); | ||||
| 		//TODO: print final report  | ||||
| 	} | ||||
| 
 | ||||
| 	private static void checkInput() { | ||||
| 		String input = ""; | ||||
| 		if (inputListener.hasNew()){ | ||||
| 			input = inputListener.getInput(); | ||||
| 			if (input.equals("p")){ | ||||
| 				System.out.println("PAUSED"); | ||||
| 				try { | ||||
| 					Thread.sleep(10000); | ||||
| 				} catch (InterruptedException e) { | ||||
| 					e.printStackTrace(); | ||||
| 				} | ||||
| 			} | ||||
| 			else if (input.equals("s")) { | ||||
| 				inputListener.stop(); | ||||
| 				t.interrupt(); | ||||
| 				System.exit(0); | ||||
| 			} | ||||
| 		} | ||||
| 	} | ||||
| 
 | ||||
| 	private static void clearDoneCivilVehicles() { | ||||
| 		//Clear civil cars from list | ||||
| 		for (int i = 0; i < listOfVehicles.size();){ | ||||
|  | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user