// Path: src/input/ScheduledUpdatesQueue.java
// public class ScheduledUpdatesQueue implements EventQueue {
// 	/** Time of the event (simulated seconds) */
// 	private ExternalEvent nextEvent;
// 	private List<ExternalEvent> updates;
// 
// 	/**
// 	 * Constructor. Creates an empty update queue.
// 	 */
// 	public ScheduledUpdatesQueue(){
// 		this.nextEvent = new ExternalEvent(Double.MAX_VALUE);
// 		this.updates = new ArrayList<ExternalEvent>();
// 	}
// 
// 	/**
// 	 * Returns the next scheduled event or event with time Double.MAX_VALUE
// 	 * if there aren't any.
// 	 * @return the next scheduled event
// 	 */
// 	public ExternalEvent nextEvent() {
// 		ExternalEvent event = this.nextEvent;
// 
// 		if (this.updates.size() == 0) {
// 			this.nextEvent = new ExternalEvent(Double.MAX_VALUE);
// 		}
// 		else {
// 			this.nextEvent = this.updates.remove(0);
// 		}
// 
// 		return event;
// 	}
// 
// 	/**
// 	 * Returns the next scheduled event's time or Double.MAX_VALUE if there
// 	 * aren't any events left
// 	 * @return the next scheduled event's time
// 	 */
// 	public double nextEventsTime() {
// 		return this.nextEvent.getTime();
// 	}
// 
// 	/**
// 	 * Add a new update request for the given time
// 	 * @param simTime The time when the update should happen
// 	 */
// 	public void addUpdate(double simTime) {
// 		ExternalEvent ee = new ExternalEvent(simTime);
// 
// 		if (ee.compareTo(nextEvent) == 0) { // this event is already next
// 			return;
// 		}
// 		else if (this.nextEvent.getTime() > simTime) { // new nextEvent
// 			putToQueue(this.nextEvent); // put the old nextEvent back to q
// 			this.nextEvent = ee;
// 		}
// 		else { // given event happens later..
// 			putToQueue(ee);
// 		}
// 	}
// 
// 	/**
// 	 * Puts a event to the queue in the right place
// 	 * @param ee The event to put to the queue
// 	 */
// 	private void putToQueue(ExternalEvent ee) {
// 		double eeTime = ee.getTime();
// 
// 		for (int i=0, n=this.updates.size(); i<n; i++) {
// 			double time = updates.get(i).getTime();
// 			if (time == eeTime) {
// 				return; // update with the given time exists -> no need for new
// 			}
// 			else if (eeTime < time) {
// 				this.updates.add(i, ee);
// 				return;
// 			}
// 		}
// 
// 		/* all existing updates are earlier -> add to the end of the list */
// 		this.updates.add(ee);
// 	}
// 
// 	public String toString() {
// 		String times = "updates @ " + this.nextEvent.getTime();
// 
// 		for (ExternalEvent ee : this.updates) {
// 			times += ", " + ee.getTime();
// 		}
// 
// 		return times;
// 	}
// }


import input.EventQueue;
import input.ExternalEvent;
import input.ScheduledUpdatesQueue;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
/*
 * Copyright 2010 Aalto University, ComNet
 * Released under GPLv3. See LICENSE.txt for details.
 */
package core;



/**
 * World contains all the nodes and is responsible for updating their
 * location and connections.
 */
public class World {
	/** name space of optimization settings ({@value})*/
	public static final String OPTIMIZATION_SETTINGS_NS = "Optimization";

	/**
	 * Should the order of node updates be different (random) within every
	 * update step -setting id ({@value}). Boolean (true/false) variable.
	 * Default is @link {@link #DEF_RANDOMIZE_UPDATES}.
	 */
	public static final String RANDOMIZE_UPDATES_S = "randomizeUpdateOrder";
	/** should the update order of nodes be randomized -setting's default value
	 * ({@value}) */
	public static final boolean DEF_RANDOMIZE_UPDATES = true;

	
	/**
	 * Real-time simulation enabled -setting id ({@value}). 
	 * If set to true and simulation time moves faster than real time,
	 * the simulation will pause after each update round to wait until real
	 * time catches up. Default = false.
	 */
	public static final String REALTIME_SIM_S = "realtime";
	/** should the update order of nodes be randomized -setting's default value
	 * ({@value}) */
	
	/**
	 * Should the connectivity simulation be stopped after one round
	 * -setting id ({@value}). Boolean (true/false) variable. Default = false.
	 */
	public static final String SIMULATE_CON_ONCE_S = "simulateConnectionsOnce";

	private int sizeX;
	private int sizeY;
	private List<EventQueue> eventQueues;
	private double updateInterval;
	private SimClock simClock;
	private double nextQueueEventTime;
	private EventQueue nextEventQueue;
	/** list of nodes; nodes are indexed by their network address */
	private List<DTNHost> hosts;
	private boolean simulateConnections;
	/** nodes in the order they should be updated (if the order should be
	 * randomized; null value means that the order should not be randomized) */
	private ArrayList<DTNHost> updateOrder;
	/** is cancellation of simulation requested from UI */
	private boolean isCancelled;
	private List<UpdateListener> updateListeners;
	/** Queue of scheduled update requests */
// Path: src/input/ScheduledUpdatesQueue.java
// public class ScheduledUpdatesQueue implements EventQueue {
// 	/** Time of the event (simulated seconds) */
// 	private ExternalEvent nextEvent;
// 	private List<ExternalEvent> updates;
// 
// 	/**
// 	 * Constructor. Creates an empty update queue.
// 	 */
// 	public ScheduledUpdatesQueue(){
// 		this.nextEvent = new ExternalEvent(Double.MAX_VALUE);
// 		this.updates = new ArrayList<ExternalEvent>();
// 	}
// 
// 	/**
// 	 * Returns the next scheduled event or event with time Double.MAX_VALUE
// 	 * if there aren't any.
// 	 * @return the next scheduled event
// 	 */
// 	public ExternalEvent nextEvent() {
// 		ExternalEvent event = this.nextEvent;
// 
// 		if (this.updates.size() == 0) {
// 			this.nextEvent = new ExternalEvent(Double.MAX_VALUE);
// 		}
// 		else {
// 			this.nextEvent = this.updates.remove(0);
// 		}
// 
// 		return event;
// 	}
// 
// 	/**
// 	 * Returns the next scheduled event's time or Double.MAX_VALUE if there
// 	 * aren't any events left
// 	 * @return the next scheduled event's time
// 	 */
// 	public double nextEventsTime() {
// 		return this.nextEvent.getTime();
// 	}
// 
// 	/**
// 	 * Add a new update request for the given time
// 	 * @param simTime The time when the update should happen
// 	 */
// 	public void addUpdate(double simTime) {
// 		ExternalEvent ee = new ExternalEvent(simTime);
// 
// 		if (ee.compareTo(nextEvent) == 0) { // this event is already next
// 			return;
// 		}
// 		else if (this.nextEvent.getTime() > simTime) { // new nextEvent
// 			putToQueue(this.nextEvent); // put the old nextEvent back to q
// 			this.nextEvent = ee;
// 		}
// 		else { // given event happens later..
// 			putToQueue(ee);
// 		}
// 	}
// 
// 	/**
// 	 * Puts a event to the queue in the right place
// 	 * @param ee The event to put to the queue
// 	 */
// 	private void putToQueue(ExternalEvent ee) {
// 		double eeTime = ee.getTime();
// 
// 		for (int i=0, n=this.updates.size(); i<n; i++) {
// 			double time = updates.get(i).getTime();
// 			if (time == eeTime) {
// 				return; // update with the given time exists -> no need for new
// 			}
// 			else if (eeTime < time) {
// 				this.updates.add(i, ee);
// 				return;
// 			}
// 		}
// 
// 		/* all existing updates are earlier -> add to the end of the list */
// 		this.updates.add(ee);
// 	}
// 
// 	public String toString() {
// 		String times = "updates @ " + this.nextEvent.getTime();
// 
// 		for (ExternalEvent ee : this.updates) {
// 			times += ", " + ee.getTime();
// 		}
// 
// 		return times;
// 	}
// }


// Path: src/core/World.java
import input.EventQueue;
import input.ExternalEvent;
import input.ScheduledUpdatesQueue;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;

/*
 * Copyright 2010 Aalto University, ComNet
 * Released under GPLv3. See LICENSE.txt for details.
 */
package core;



/**
 * World contains all the nodes and is responsible for updating their
 * location and connections.
 */
public class World {
	/** name space of optimization settings ({@value})*/
	public static final String OPTIMIZATION_SETTINGS_NS = "Optimization";

	/**
	 * Should the order of node updates be different (random) within every
	 * update step -setting id ({@value}). Boolean (true/false) variable.
	 * Default is @link {@link #DEF_RANDOMIZE_UPDATES}.
	 */
	public static final String RANDOMIZE_UPDATES_S = "randomizeUpdateOrder";
	/** should the update order of nodes be randomized -setting's default value
	 * ({@value}) */
	public static final boolean DEF_RANDOMIZE_UPDATES = true;

	
	/**
	 * Real-time simulation enabled -setting id ({@value}). 
	 * If set to true and simulation time moves faster than real time,
	 * the simulation will pause after each update round to wait until real
	 * time catches up. Default = false.
	 */
	public static final String REALTIME_SIM_S = "realtime";
	/** should the update order of nodes be randomized -setting's default value
	 * ({@value}) */
	
	/**
	 * Should the connectivity simulation be stopped after one round
	 * -setting id ({@value}). Boolean (true/false) variable. Default = false.
	 */
	public static final String SIMULATE_CON_ONCE_S = "simulateConnectionsOnce";

	private int sizeX;
	private int sizeY;
	private List<EventQueue> eventQueues;
	private double updateInterval;
	private SimClock simClock;
	private double nextQueueEventTime;
	private EventQueue nextEventQueue;
	/** list of nodes; nodes are indexed by their network address */
	private List<DTNHost> hosts;
	private boolean simulateConnections;
	/** nodes in the order they should be updated (if the order should be
	 * randomized; null value means that the order should not be randomized) */
	private ArrayList<DTNHost> updateOrder;
	/** is cancellation of simulation requested from UI */
	private boolean isCancelled;
	private List<UpdateListener> updateListeners;
	/** Queue of scheduled update requests */
	private ScheduledUpdatesQueue scheduledUpdates;