2006-08-21 14:11:16 +02:00
|
|
|
/*
|
2009-05-26 16:15:41 +02:00
|
|
|
* Copyright (c) 2009, Swedish Institute of Computer Science. All rights
|
2007-01-10 15:57:42 +01:00
|
|
|
* reserved.
|
2007-10-03 16:20:57 +02:00
|
|
|
*
|
2006-08-21 14:11:16 +02:00
|
|
|
* Redistribution and use in source and binary forms, with or without
|
2007-01-10 15:57:42 +01:00
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer. 2. Redistributions in
|
|
|
|
* binary form must reproduce the above copyright notice, this list of
|
|
|
|
* conditions and the following disclaimer in the documentation and/or other
|
|
|
|
* materials provided with the distribution. 3. Neither the name of the
|
|
|
|
* Institute nor the names of its contributors may be used to endorse or promote
|
|
|
|
* products derived from this software without specific prior written
|
|
|
|
* permission.
|
2007-10-03 16:20:57 +02:00
|
|
|
*
|
2007-01-10 15:57:42 +01:00
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND ANY
|
|
|
|
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
|
|
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE FOR ANY
|
|
|
|
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
|
|
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
|
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
|
|
|
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
|
|
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
2007-10-03 16:20:57 +02:00
|
|
|
*
|
2009-05-26 16:15:41 +02:00
|
|
|
* $Id: Simulation.java,v 1.47 2009/05/26 14:15:41 fros4943 Exp $
|
2006-08-21 14:11:16 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
package se.sics.cooja;
|
|
|
|
|
|
|
|
import java.util.*;
|
|
|
|
import org.apache.log4j.Logger;
|
|
|
|
import org.jdom.*;
|
|
|
|
|
2009-05-26 16:15:41 +02:00
|
|
|
import se.sics.cooja.contikimote.ContikiMote;
|
2006-08-21 14:11:16 +02:00
|
|
|
import se.sics.cooja.dialogs.*;
|
|
|
|
|
|
|
|
/**
|
2007-01-10 15:57:42 +01:00
|
|
|
* A simulation consists of a number of motes and mote types.
|
2007-10-03 16:20:57 +02:00
|
|
|
*
|
2008-10-28 14:35:59 +01:00
|
|
|
* A simulation is observable:
|
|
|
|
* changed simulation state, added or deleted motes etc are observed.
|
|
|
|
* To track mote changes, observe the mote (interfaces) itself.
|
2007-10-03 16:20:57 +02:00
|
|
|
*
|
2009-03-02 10:46:19 +01:00
|
|
|
* @author Fredrik Osterlind
|
2006-08-21 14:11:16 +02:00
|
|
|
*/
|
|
|
|
public class Simulation extends Observable implements Runnable {
|
2009-05-26 16:15:41 +02:00
|
|
|
public static final long MILLISECOND = 1000L;
|
2006-08-21 14:11:16 +02:00
|
|
|
|
2009-05-26 16:15:41 +02:00
|
|
|
/*private static long EVENT_COUNTER = 0;*/
|
|
|
|
|
2006-08-21 14:11:16 +02:00
|
|
|
private Vector<Mote> motes = new Vector<Mote>();
|
2007-01-10 15:57:42 +01:00
|
|
|
|
2006-08-21 14:11:16 +02:00
|
|
|
private Vector<MoteType> moteTypes = new Vector<MoteType>();
|
|
|
|
|
2009-03-02 10:46:19 +01:00
|
|
|
private int delayTime = 0;
|
2007-01-10 15:57:42 +01:00
|
|
|
|
2008-12-04 15:03:41 +01:00
|
|
|
private long currentSimulationTime = 0;
|
2007-01-10 15:57:42 +01:00
|
|
|
|
2006-08-21 14:11:16 +02:00
|
|
|
private String title = null;
|
|
|
|
|
|
|
|
private RadioMedium currentRadioMedium = null;
|
|
|
|
|
|
|
|
private static Logger logger = Logger.getLogger(Simulation.class);
|
|
|
|
|
|
|
|
private boolean isRunning = false;
|
2007-01-10 15:57:42 +01:00
|
|
|
|
2006-08-21 14:11:16 +02:00
|
|
|
private boolean stopSimulation = false;
|
2007-01-10 15:57:42 +01:00
|
|
|
|
2008-12-08 14:07:06 +01:00
|
|
|
private Thread simulationThread = null;
|
2006-08-21 14:11:16 +02:00
|
|
|
|
2007-01-09 11:21:08 +01:00
|
|
|
private GUI myGUI = null;
|
2007-01-10 15:57:42 +01:00
|
|
|
|
2007-06-19 11:58:43 +02:00
|
|
|
private long randomSeed = 123456;
|
2007-05-29 09:27:10 +02:00
|
|
|
|
2009-02-18 11:09:32 +01:00
|
|
|
private boolean randomSeedGenerated = false;
|
|
|
|
|
2009-05-26 16:15:41 +02:00
|
|
|
private long maxMoteStartupDelay = 1000*MILLISECOND;
|
2007-05-29 09:27:10 +02:00
|
|
|
|
2009-02-18 11:09:32 +01:00
|
|
|
private Random randomGenerator = new Random();
|
2007-05-29 09:27:10 +02:00
|
|
|
|
2009-05-26 16:15:41 +02:00
|
|
|
private boolean hasMillisecondObservers = false;
|
|
|
|
private MillisecondObservable millisecondObservable = new MillisecondObservable();
|
|
|
|
private class MillisecondObservable extends Observable {
|
|
|
|
private void newMillisecond(long time) {
|
2006-08-21 14:11:16 +02:00
|
|
|
setChanged();
|
2009-05-26 16:15:41 +02:00
|
|
|
notifyObservers(time);
|
2006-08-21 14:11:16 +02:00
|
|
|
}
|
|
|
|
}
|
2007-01-10 15:57:42 +01:00
|
|
|
|
2009-05-26 16:15:41 +02:00
|
|
|
private EventQueue eventQueue = new EventQueue();
|
2006-08-21 14:11:16 +02:00
|
|
|
|
|
|
|
/**
|
2009-05-26 16:15:41 +02:00
|
|
|
* Add millisecond observer.
|
|
|
|
* This observer is notified once every simulated millisecond.
|
2007-10-03 16:20:57 +02:00
|
|
|
*
|
2009-05-26 16:15:41 +02:00
|
|
|
* @see #deleteMillisecondObserver(Observer)
|
|
|
|
* @param newObserver Observer
|
2006-08-21 14:11:16 +02:00
|
|
|
*/
|
2009-05-26 16:15:41 +02:00
|
|
|
public void addMillisecondObserver(Observer newObserver) {
|
|
|
|
millisecondObservable.addObserver(newObserver);
|
|
|
|
hasMillisecondObservers = true;
|
|
|
|
rescheduleEvents = true;
|
2006-08-21 14:11:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2009-05-26 16:15:41 +02:00
|
|
|
* Delete millisecond observer.
|
2007-10-03 16:20:57 +02:00
|
|
|
*
|
2009-05-26 16:15:41 +02:00
|
|
|
* @see #addMillisecondObserver(Observer)
|
|
|
|
* @param observer Observer to delete
|
2006-08-21 14:11:16 +02:00
|
|
|
*/
|
2009-05-26 16:15:41 +02:00
|
|
|
public void deleteMillisecondObserver(Observer observer) {
|
|
|
|
millisecondObservable.deleteObserver(observer);
|
|
|
|
hasMillisecondObservers = millisecondObservable.countObservers() > 0;
|
|
|
|
rescheduleEvents = true;
|
2006-08-21 14:11:16 +02:00
|
|
|
}
|
|
|
|
|
2008-12-08 14:07:06 +01:00
|
|
|
|
2009-05-26 16:15:41 +02:00
|
|
|
/**
|
|
|
|
* Schedule events.
|
|
|
|
* This method is not thread-safe, and should only be invoked when the
|
|
|
|
* simulation is paused, or from inside the simulation loop.
|
|
|
|
*
|
|
|
|
* @see #scheduleEvent(TimeEvent, long)
|
|
|
|
* @param e Event
|
|
|
|
* @param time Execution time
|
|
|
|
*/
|
|
|
|
public void scheduleEventUnsafe(TimeEvent e, long time) {
|
2008-12-08 14:07:06 +01:00
|
|
|
eventQueue.addEvent(e, time);
|
|
|
|
}
|
|
|
|
|
2008-10-28 14:35:59 +01:00
|
|
|
/**
|
|
|
|
* Schedule event to be handled by event loop.
|
|
|
|
*
|
|
|
|
* @param e Event
|
2009-05-26 16:15:41 +02:00
|
|
|
* @param time Execution time
|
2008-10-28 14:35:59 +01:00
|
|
|
*/
|
2008-12-04 15:03:41 +01:00
|
|
|
public void scheduleEvent(TimeEvent e, long time) {
|
2008-12-08 14:07:06 +01:00
|
|
|
if (Thread.currentThread() == simulationThread) {
|
|
|
|
eventQueue.addEvent(e, time);
|
|
|
|
} else {
|
2009-01-08 16:42:25 +01:00
|
|
|
eventQueue.addEventUnsorted(e, time);
|
2008-12-08 14:07:06 +01:00
|
|
|
}
|
2008-10-28 14:35:59 +01:00
|
|
|
}
|
|
|
|
|
2009-02-23 09:33:23 +01:00
|
|
|
private Mote[] emulatedMoteArray;
|
2009-05-26 16:15:41 +02:00
|
|
|
private TimeEvent tickEmulatedMotesEvent = new TimeEvent(0) {
|
2008-12-04 15:03:41 +01:00
|
|
|
public void execute(long t) {
|
2008-10-28 14:35:59 +01:00
|
|
|
/*logger.info("MSP motes tick at: " + t);*/
|
2009-02-23 09:33:23 +01:00
|
|
|
if (emulatedMoteArray.length == 0) {
|
2008-12-04 17:52:03 +01:00
|
|
|
return;
|
|
|
|
}
|
2008-10-28 14:35:59 +01:00
|
|
|
|
|
|
|
/* Tick MSP motes */
|
|
|
|
boolean wantMoreTicks = true;
|
|
|
|
while (wantMoreTicks) {
|
|
|
|
/* Tick all MSP motes until none need more ticks */
|
|
|
|
wantMoreTicks = false;
|
2009-02-23 09:33:23 +01:00
|
|
|
for (Mote element : emulatedMoteArray) {
|
2008-10-28 14:35:59 +01:00
|
|
|
if (element.tick(currentSimulationTime)) {
|
|
|
|
wantMoreTicks = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-26 16:15:41 +02:00
|
|
|
/* XXX Reschedule MSP motes (millisecond resolution) */
|
|
|
|
scheduleEventUnsafe(this, t+1000);
|
|
|
|
}
|
|
|
|
public String toString() {
|
|
|
|
return "MSPSIM ALL";
|
2008-10-28 14:35:59 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2009-05-26 16:15:41 +02:00
|
|
|
private TimeEvent delayEvent = new TimeEvent(0) {
|
2008-12-04 15:03:41 +01:00
|
|
|
public void execute(long t) {
|
2009-05-26 16:15:41 +02:00
|
|
|
/*logger.info("Delay at: " + t);*/
|
|
|
|
if (delayTime == 0) {
|
2008-12-04 17:52:03 +01:00
|
|
|
return;
|
|
|
|
}
|
2008-10-28 14:35:59 +01:00
|
|
|
|
2009-05-26 16:15:41 +02:00
|
|
|
try { Thread.sleep(delayTime); } catch (InterruptedException e) { }
|
|
|
|
scheduleEventUnsafe(this, t+MILLISECOND);
|
|
|
|
}
|
|
|
|
public String toString() {
|
|
|
|
return "DELAY";
|
2008-10-28 14:35:59 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2009-05-26 16:15:41 +02:00
|
|
|
private TimeEvent millisecondEvent = new TimeEvent(0) {
|
2008-12-04 15:03:41 +01:00
|
|
|
public void execute(long t) {
|
2009-05-26 16:15:41 +02:00
|
|
|
if (!hasMillisecondObservers) {
|
2008-12-04 17:52:03 +01:00
|
|
|
return;
|
2008-12-08 14:07:06 +01:00
|
|
|
}
|
2008-10-28 14:35:59 +01:00
|
|
|
|
2009-05-26 16:15:41 +02:00
|
|
|
millisecondObservable.newMillisecond(getSimulationTime());
|
|
|
|
scheduleEventUnsafe(this, t+MILLISECOND);
|
|
|
|
}
|
|
|
|
public String toString() {
|
|
|
|
return "MILLISECOND: " + millisecondObservable.countObservers();
|
2008-10-28 14:35:59 +01:00
|
|
|
}
|
|
|
|
};
|
2009-05-26 16:15:41 +02:00
|
|
|
|
|
|
|
private void recreateMoteLists() {
|
2009-02-18 16:57:47 +01:00
|
|
|
/* Tick MSP motes separately */
|
2009-02-23 09:33:23 +01:00
|
|
|
ArrayList<Mote> emulatedMotes = new ArrayList<Mote>();
|
2009-02-18 16:57:47 +01:00
|
|
|
for (Mote mote: motes) {
|
2009-02-23 09:33:23 +01:00
|
|
|
/* TODO: fixe an emulatedMote generic class */
|
2009-02-18 16:57:47 +01:00
|
|
|
if (mote.getType().getClass().toString().contains(".mspmote.")) {
|
2009-02-23 09:33:23 +01:00
|
|
|
emulatedMotes.add(mote);
|
|
|
|
} else if (mote.getType().getClass().toString().contains(".avrmote.")) {
|
|
|
|
emulatedMotes.add(mote);
|
2009-02-18 16:57:47 +01:00
|
|
|
}
|
|
|
|
}
|
2009-02-23 09:33:23 +01:00
|
|
|
emulatedMoteArray = emulatedMotes.toArray(new Mote[emulatedMotes.size()]);
|
2009-02-18 16:57:47 +01:00
|
|
|
}
|
|
|
|
|
2008-12-04 17:52:03 +01:00
|
|
|
private boolean rescheduleEvents = false;
|
2006-08-21 14:11:16 +02:00
|
|
|
public void run() {
|
|
|
|
long lastStartTime = System.currentTimeMillis();
|
|
|
|
logger.info("Simulation main loop started, system time: " + lastStartTime);
|
|
|
|
isRunning = true;
|
2009-05-26 16:15:41 +02:00
|
|
|
|
2008-11-03 19:09:43 +01:00
|
|
|
/* Schedule tick events */
|
2009-05-26 16:15:41 +02:00
|
|
|
scheduleEventUnsafe(tickEmulatedMotesEvent, currentSimulationTime);
|
|
|
|
scheduleEventUnsafe(delayEvent, currentSimulationTime - (currentSimulationTime % 1000) + 1000);
|
|
|
|
scheduleEventUnsafe(millisecondEvent, currentSimulationTime - (currentSimulationTime % 1000) + 1000);
|
2008-11-03 19:09:43 +01:00
|
|
|
|
2008-10-28 14:35:59 +01:00
|
|
|
/* Simulation starting */
|
2006-08-21 14:11:16 +02:00
|
|
|
this.setChanged();
|
|
|
|
this.notifyObservers(this);
|
|
|
|
|
2009-05-26 16:15:41 +02:00
|
|
|
recreateMoteLists();
|
2008-04-01 10:12:16 +02:00
|
|
|
|
2008-09-22 18:18:22 +02:00
|
|
|
try {
|
2008-12-04 17:52:03 +01:00
|
|
|
TimeEvent nextEvent;
|
2008-10-28 14:35:59 +01:00
|
|
|
while (isRunning) {
|
2007-10-03 16:20:57 +02:00
|
|
|
|
2008-12-04 17:52:03 +01:00
|
|
|
if (rescheduleEvents) {
|
|
|
|
rescheduleEvents = false;
|
2009-05-26 16:15:41 +02:00
|
|
|
scheduleEventUnsafe(tickEmulatedMotesEvent, currentSimulationTime);
|
|
|
|
scheduleEventUnsafe(delayEvent, currentSimulationTime - (currentSimulationTime % 1000) + 1000);
|
|
|
|
scheduleEventUnsafe(millisecondEvent, currentSimulationTime - (currentSimulationTime % 1000) + 1000);
|
2008-12-04 17:52:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
nextEvent = eventQueue.popFirst();
|
2008-10-28 14:35:59 +01:00
|
|
|
if (nextEvent == null) {
|
|
|
|
throw new RuntimeException("No more events");
|
2007-10-03 16:20:57 +02:00
|
|
|
}
|
2006-08-21 14:11:16 +02:00
|
|
|
|
2008-10-28 14:35:59 +01:00
|
|
|
currentSimulationTime = nextEvent.time;
|
2009-05-26 16:15:41 +02:00
|
|
|
/*logger.info("Executing event #" + EVENT_COUNTER++ + " @ " + currentSimulationTime + ": " + nextEvent);*/
|
2008-10-28 14:35:59 +01:00
|
|
|
nextEvent.execute(currentSimulationTime);
|
2006-08-21 14:11:16 +02:00
|
|
|
|
|
|
|
if (stopSimulation) {
|
|
|
|
isRunning = false;
|
|
|
|
}
|
2008-10-28 14:35:59 +01:00
|
|
|
}
|
2008-09-30 01:04:27 +02:00
|
|
|
} catch (RuntimeException e) {
|
2009-03-12 12:01:26 +01:00
|
|
|
logger.fatal("Simulation stopped due to error", e);
|
2009-03-11 23:17:04 +01:00
|
|
|
|
2009-03-12 12:01:26 +01:00
|
|
|
if (!GUI.isVisualized()) {
|
|
|
|
/* Quit simulator if in test mode */
|
|
|
|
System.exit(1);
|
2008-12-03 16:38:01 +01:00
|
|
|
}
|
2008-09-30 01:04:27 +02:00
|
|
|
}
|
2006-08-21 14:11:16 +02:00
|
|
|
isRunning = false;
|
2008-12-08 14:07:06 +01:00
|
|
|
simulationThread = null;
|
2006-08-21 14:11:16 +02:00
|
|
|
stopSimulation = false;
|
|
|
|
|
|
|
|
// Notify observers simulation has stopped
|
|
|
|
this.setChanged();
|
|
|
|
this.notifyObservers(this);
|
|
|
|
|
|
|
|
logger.info("Simulation main loop stopped, system time: "
|
|
|
|
+ System.currentTimeMillis() + "\tDuration: "
|
|
|
|
+ (System.currentTimeMillis() - lastStartTime) + " ms");
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2007-01-10 15:57:42 +01:00
|
|
|
* Creates a new simulation with a delay time of 100 ms.
|
2006-08-21 14:11:16 +02:00
|
|
|
*/
|
2007-01-09 11:21:08 +01:00
|
|
|
public Simulation(GUI gui) {
|
|
|
|
myGUI = gui;
|
2006-08-21 14:11:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Starts this simulation (notifies observers).
|
|
|
|
*/
|
|
|
|
public void startSimulation() {
|
|
|
|
if (!isRunning()) {
|
2006-12-13 12:57:04 +01:00
|
|
|
isRunning = true;
|
2008-12-08 14:07:06 +01:00
|
|
|
simulationThread = new Thread(this);
|
|
|
|
simulationThread.start();
|
2006-08-21 14:11:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Stops this simulation (notifies observers).
|
|
|
|
*/
|
|
|
|
public void stopSimulation() {
|
|
|
|
if (isRunning()) {
|
|
|
|
stopSimulation = true;
|
|
|
|
|
2008-12-04 13:44:02 +01:00
|
|
|
/* Wait until simulation stops */
|
2008-12-08 14:07:06 +01:00
|
|
|
if (Thread.currentThread() != simulationThread) {
|
2008-12-04 13:44:02 +01:00
|
|
|
try {
|
2009-05-26 16:15:41 +02:00
|
|
|
Thread simThread = simulationThread;
|
|
|
|
if (simThread != null) {
|
|
|
|
simThread.join();
|
2008-12-04 14:04:26 +01:00
|
|
|
}
|
2008-12-04 13:44:02 +01:00
|
|
|
} catch (InterruptedException e) {
|
2006-08-21 14:11:16 +02:00
|
|
|
}
|
2007-10-03 16:20:57 +02:00
|
|
|
}
|
|
|
|
}
|
2006-08-21 14:11:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2007-01-10 15:57:42 +01:00
|
|
|
* Starts simulation if stopped, ticks all motes once, and finally stops
|
2006-08-21 14:11:16 +02:00
|
|
|
* simulation again.
|
|
|
|
*/
|
2009-05-26 16:15:41 +02:00
|
|
|
public void stepMillisecondSimulation() {
|
|
|
|
TimeEvent stopEvent = new TimeEvent(0) {
|
|
|
|
public void execute(long t) {
|
|
|
|
/* Stop simulation */
|
2008-12-04 13:44:02 +01:00
|
|
|
stopSimulation();
|
2006-08-21 14:11:16 +02:00
|
|
|
}
|
2009-05-26 16:15:41 +02:00
|
|
|
};
|
|
|
|
scheduleEvent(stopEvent, getSimulationTime()+Simulation.MILLISECOND);
|
|
|
|
|
|
|
|
/* Start simulation if not running */
|
|
|
|
if (!isRunning()) {
|
|
|
|
startSimulation();
|
|
|
|
}
|
2006-08-21 14:11:16 +02:00
|
|
|
}
|
|
|
|
|
2007-01-10 15:57:42 +01:00
|
|
|
/**
|
|
|
|
* @return GUI holding this simulation
|
|
|
|
*/
|
|
|
|
public GUI getGUI() {
|
|
|
|
return myGUI;
|
|
|
|
}
|
|
|
|
|
2007-07-10 14:43:23 +02:00
|
|
|
/**
|
2007-08-21 10:51:33 +02:00
|
|
|
* @return Random seed
|
2007-07-10 14:43:23 +02:00
|
|
|
*/
|
|
|
|
public long getRandomSeed() {
|
|
|
|
return randomSeed;
|
|
|
|
}
|
2007-08-21 10:51:33 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param randomSeed Random seed
|
|
|
|
*/
|
|
|
|
public void setRandomSeed(long randomSeed) {
|
|
|
|
this.randomSeed = randomSeed;
|
2009-02-18 11:09:32 +01:00
|
|
|
randomGenerator.setSeed(randomSeed);
|
|
|
|
logger.info("Simulation random seed: " + randomSeed);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param generated Autogenerated random seed at simulation load
|
|
|
|
*/
|
|
|
|
public void setRandomSeedGenerated(boolean generated) {
|
|
|
|
this.randomSeedGenerated = generated;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return Autogenerated random seed at simulation load
|
|
|
|
*/
|
|
|
|
public boolean getRandomSeedGenerated() {
|
|
|
|
return randomSeedGenerated;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Random getRandomGenerator() {
|
|
|
|
return randomGenerator;
|
2007-08-21 10:51:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return Maximum mote startup delay
|
|
|
|
*/
|
2009-05-26 16:15:41 +02:00
|
|
|
public long getDelayedMoteStartupTime() {
|
2007-08-21 10:51:33 +02:00
|
|
|
return maxMoteStartupDelay;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2008-02-12 16:31:22 +01:00
|
|
|
* @param maxMoteStartupDelay Maximum mote startup delay
|
2007-08-21 10:51:33 +02:00
|
|
|
*/
|
2009-05-26 16:15:41 +02:00
|
|
|
public void setDelayedMoteStartupTime(long maxMoteStartupDelay) {
|
2007-08-21 10:51:33 +02:00
|
|
|
this.maxMoteStartupDelay = Math.max(0, maxMoteStartupDelay);
|
|
|
|
}
|
|
|
|
|
2006-08-21 14:11:16 +02:00
|
|
|
/**
|
|
|
|
* Returns the current simulation config represented by XML elements. This
|
|
|
|
* config also includes the current radio medium, all mote types and motes.
|
2007-10-03 16:20:57 +02:00
|
|
|
*
|
2006-08-21 14:11:16 +02:00
|
|
|
* @return Current simulation config
|
|
|
|
*/
|
|
|
|
public Collection<Element> getConfigXML() {
|
|
|
|
Vector<Element> config = new Vector<Element>();
|
|
|
|
|
|
|
|
Element element;
|
|
|
|
|
|
|
|
// Title
|
|
|
|
element = new Element("title");
|
|
|
|
element.setText(title);
|
|
|
|
config.add(element);
|
|
|
|
|
|
|
|
// Delay time
|
|
|
|
element = new Element("delaytime");
|
|
|
|
element.setText(Integer.toString(delayTime));
|
|
|
|
config.add(element);
|
|
|
|
|
2007-07-04 18:13:17 +02:00
|
|
|
// Random seed
|
|
|
|
element = new Element("randomseed");
|
2009-02-18 11:09:32 +01:00
|
|
|
if (randomSeedGenerated) {
|
|
|
|
element.setText("generated");
|
|
|
|
} else {
|
|
|
|
element.setText(Long.toString(getRandomSeed()));
|
|
|
|
}
|
2007-07-04 18:13:17 +02:00
|
|
|
config.add(element);
|
|
|
|
|
2007-08-21 10:51:33 +02:00
|
|
|
// Max mote startup delay
|
2009-05-26 16:15:41 +02:00
|
|
|
element = new Element("motedelay_us");
|
|
|
|
element.setText(Long.toString(maxMoteStartupDelay));
|
2007-08-21 10:51:33 +02:00
|
|
|
config.add(element);
|
|
|
|
|
2006-08-21 14:11:16 +02:00
|
|
|
// Radio Medium
|
|
|
|
element = new Element("radiomedium");
|
|
|
|
element.setText(currentRadioMedium.getClass().getName());
|
|
|
|
|
|
|
|
Collection radioMediumXML = currentRadioMedium.getConfigXML();
|
2007-10-03 16:20:57 +02:00
|
|
|
if (radioMediumXML != null) {
|
2006-08-21 14:11:16 +02:00
|
|
|
element.addContent(radioMediumXML);
|
2007-10-03 16:20:57 +02:00
|
|
|
}
|
2006-08-21 14:11:16 +02:00
|
|
|
config.add(element);
|
|
|
|
|
|
|
|
// Mote types
|
|
|
|
for (MoteType moteType : getMoteTypes()) {
|
|
|
|
element = new Element("motetype");
|
|
|
|
element.setText(moteType.getClass().getName());
|
|
|
|
|
|
|
|
Collection moteTypeXML = moteType.getConfigXML();
|
2007-10-03 16:20:57 +02:00
|
|
|
if (moteTypeXML != null) {
|
2006-08-21 14:11:16 +02:00
|
|
|
element.addContent(moteTypeXML);
|
2007-10-03 16:20:57 +02:00
|
|
|
}
|
2006-08-21 14:11:16 +02:00
|
|
|
config.add(element);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Motes
|
|
|
|
for (Mote mote : motes) {
|
|
|
|
element = new Element("mote");
|
|
|
|
element.setText(mote.getClass().getName());
|
|
|
|
|
|
|
|
Collection moteXML = mote.getConfigXML();
|
2007-10-03 16:20:57 +02:00
|
|
|
if (moteXML != null) {
|
2006-08-21 14:11:16 +02:00
|
|
|
element.addContent(moteXML);
|
2007-10-03 16:20:57 +02:00
|
|
|
}
|
2006-08-21 14:11:16 +02:00
|
|
|
config.add(element);
|
|
|
|
}
|
|
|
|
|
|
|
|
return config;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the current simulation config depending on the given XML elements.
|
2007-10-03 16:20:57 +02:00
|
|
|
*
|
2006-08-21 14:11:16 +02:00
|
|
|
* @see #getConfigXML()
|
|
|
|
* @param configXML
|
|
|
|
* Config XML elements
|
2007-01-10 15:57:42 +01:00
|
|
|
* @param visAvailable
|
|
|
|
* True if simulation is allowed to show visualizers while loading
|
|
|
|
* the given config
|
|
|
|
* @return True if simulation config set successfully
|
|
|
|
* @throws Exception
|
|
|
|
* If configuration could not be loaded
|
2006-08-21 14:11:16 +02:00
|
|
|
*/
|
2007-01-10 15:57:42 +01:00
|
|
|
public boolean setConfigXML(Collection<Element> configXML,
|
|
|
|
boolean visAvailable) throws Exception {
|
2006-08-21 14:11:16 +02:00
|
|
|
|
|
|
|
// Parse elements
|
|
|
|
for (Element element : configXML) {
|
|
|
|
|
|
|
|
// Title
|
|
|
|
if (element.getName().equals("title")) {
|
|
|
|
title = element.getText();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delay time
|
|
|
|
if (element.getName().equals("delaytime")) {
|
|
|
|
delayTime = Integer.parseInt(element.getText());
|
|
|
|
}
|
|
|
|
|
2007-07-04 18:13:17 +02:00
|
|
|
// Random seed
|
|
|
|
if (element.getName().equals("randomseed")) {
|
2009-02-18 11:09:32 +01:00
|
|
|
if (element.getText().equals("generated")) {
|
|
|
|
randomSeedGenerated = true;
|
|
|
|
setRandomSeed(new Random().nextLong());
|
|
|
|
} else {
|
|
|
|
setRandomSeed(Long.parseLong(element.getText()));
|
|
|
|
}
|
2007-07-04 18:13:17 +02:00
|
|
|
}
|
|
|
|
|
2007-08-21 10:51:33 +02:00
|
|
|
// Max mote startup delay
|
|
|
|
if (element.getName().equals("motedelay")) {
|
2009-05-26 16:15:41 +02:00
|
|
|
maxMoteStartupDelay = Integer.parseInt(element.getText())*MILLISECOND;
|
|
|
|
}
|
|
|
|
if (element.getName().equals("motedelay_us")) {
|
2007-08-21 10:51:33 +02:00
|
|
|
maxMoteStartupDelay = Integer.parseInt(element.getText());
|
|
|
|
}
|
|
|
|
|
2006-08-21 14:11:16 +02:00
|
|
|
// Radio medium
|
|
|
|
if (element.getName().equals("radiomedium")) {
|
|
|
|
String radioMediumClassName = element.getText().trim();
|
2007-01-10 15:57:42 +01:00
|
|
|
Class<? extends RadioMedium> radioMediumClass = myGUI.tryLoadClass(
|
|
|
|
this, RadioMedium.class, radioMediumClassName);
|
2006-08-21 14:11:16 +02:00
|
|
|
|
2007-01-09 11:21:08 +01:00
|
|
|
if (radioMediumClass != null) {
|
2006-08-21 14:11:16 +02:00
|
|
|
// Create radio medium specified in config
|
2007-01-09 11:21:08 +01:00
|
|
|
try {
|
2009-02-24 16:08:20 +01:00
|
|
|
currentRadioMedium = RadioMedium.generateRadioMedium(radioMediumClass, this);
|
2007-01-09 11:21:08 +01:00
|
|
|
} catch (Exception e) {
|
|
|
|
currentRadioMedium = null;
|
2009-02-24 16:08:20 +01:00
|
|
|
logger.warn("Could not load radio medium class: " + radioMediumClassName);
|
2007-01-09 11:21:08 +01:00
|
|
|
}
|
|
|
|
}
|
2007-01-10 15:57:42 +01:00
|
|
|
|
2006-08-21 14:11:16 +02:00
|
|
|
// Show configure simulation dialog
|
2007-01-09 11:21:08 +01:00
|
|
|
boolean createdOK = false;
|
|
|
|
if (visAvailable) {
|
2008-02-12 16:03:02 +01:00
|
|
|
createdOK = CreateSimDialog.showDialog(GUI.getTopParentContainer(), this);
|
2007-01-09 11:21:08 +01:00
|
|
|
} else {
|
|
|
|
createdOK = true;
|
|
|
|
}
|
2006-08-21 14:11:16 +02:00
|
|
|
|
|
|
|
if (!createdOK) {
|
|
|
|
logger.debug("Simulation not created, aborting");
|
|
|
|
throw new Exception("Load aborted by user");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if radio medium specific config should be applied
|
2009-02-24 16:08:20 +01:00
|
|
|
if (radioMediumClassName.equals(currentRadioMedium.getClass().getName())) {
|
2007-01-09 11:21:08 +01:00
|
|
|
currentRadioMedium.setConfigXML(element.getChildren(), visAvailable);
|
2006-08-21 14:11:16 +02:00
|
|
|
} else {
|
2009-02-24 16:08:20 +01:00
|
|
|
logger.info("Radio Medium changed - ignoring radio medium specific config");
|
2006-08-21 14:11:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mote type
|
|
|
|
if (element.getName().equals("motetype")) {
|
|
|
|
String moteTypeClassName = element.getText().trim();
|
|
|
|
|
2007-01-10 15:57:42 +01:00
|
|
|
Class<? extends MoteType> moteTypeClass = myGUI.tryLoadClass(this,
|
|
|
|
MoteType.class, moteTypeClassName);
|
2006-08-21 14:11:16 +02:00
|
|
|
|
|
|
|
if (moteTypeClass == null) {
|
|
|
|
logger.fatal("Could not load mote type class: " + moteTypeClassName);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-03-10 22:05:29 +01:00
|
|
|
MoteType moteType = moteTypeClass.getConstructor((Class[]) null).newInstance();
|
2006-08-21 14:11:16 +02:00
|
|
|
|
2007-01-10 15:57:42 +01:00
|
|
|
boolean createdOK = moteType.setConfigXML(this, element.getChildren(),
|
|
|
|
visAvailable);
|
2006-08-21 14:11:16 +02:00
|
|
|
if (createdOK) {
|
|
|
|
addMoteType(moteType);
|
|
|
|
} else {
|
|
|
|
logger
|
|
|
|
.fatal("Mote type was not created: " + element.getText().trim());
|
|
|
|
throw new Exception("All mote types were not recreated");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mote
|
|
|
|
if (element.getName().equals("mote")) {
|
2007-01-10 15:57:42 +01:00
|
|
|
Class<? extends Mote> moteClass = myGUI.tryLoadClass(this, Mote.class,
|
|
|
|
element.getText().trim());
|
2006-08-21 14:11:16 +02:00
|
|
|
|
2007-07-04 09:44:13 +02:00
|
|
|
Mote mote = moteClass.getConstructor((Class[]) null).newInstance((Object[]) null);
|
2007-01-09 11:21:08 +01:00
|
|
|
if (mote.setConfigXML(this, element.getChildren(), visAvailable)) {
|
2006-08-21 14:11:16 +02:00
|
|
|
addMote(mote);
|
|
|
|
} else {
|
|
|
|
logger.fatal("Mote was not created: " + element.getText().trim());
|
|
|
|
throw new Exception("All motes were not recreated");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-01-09 11:21:08 +01:00
|
|
|
setChanged();
|
|
|
|
notifyObservers(this);
|
2006-08-21 14:11:16 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes a mote from this simulation
|
2007-10-03 16:20:57 +02:00
|
|
|
*
|
2006-08-21 14:11:16 +02:00
|
|
|
* @param mote
|
|
|
|
* Mote to remove
|
|
|
|
*/
|
2009-02-18 17:01:31 +01:00
|
|
|
public void removeMote(final Mote mote) {
|
2006-08-21 14:11:16 +02:00
|
|
|
|
2009-02-18 17:01:31 +01:00
|
|
|
/* Simulation is running, remove mote in simulation loop */
|
2009-05-26 16:15:41 +02:00
|
|
|
TimeEvent removeMoteEvent = new TimeEvent(0) {
|
2009-02-18 17:01:31 +01:00
|
|
|
public void execute(long t) {
|
|
|
|
motes.remove(mote);
|
|
|
|
currentRadioMedium.unregisterMote(mote, Simulation.this);
|
2009-05-26 16:15:41 +02:00
|
|
|
|
|
|
|
/* Loop through all scheduled events.
|
|
|
|
* Delete all events associated with deleted mote. */
|
|
|
|
TimeEvent ev = eventQueue.peekFirst();
|
|
|
|
while (ev != null) {
|
|
|
|
if (ev instanceof MoteTimeEvent) {
|
|
|
|
if (((MoteTimeEvent)ev).getMote() == mote) {
|
|
|
|
ev.remove();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ev = ev.nextEvent;
|
|
|
|
}
|
|
|
|
|
|
|
|
recreateMoteLists();
|
2009-02-18 17:01:31 +01:00
|
|
|
Simulation.this.setChanged();
|
|
|
|
Simulation.this.notifyObservers(this);
|
|
|
|
}
|
|
|
|
};
|
2008-03-17 09:35:10 +01:00
|
|
|
|
2009-05-26 16:15:41 +02:00
|
|
|
if (!isRunning()) {
|
|
|
|
/* Simulation is stopped, remove mote immediately */
|
|
|
|
removeMoteEvent.execute(0);
|
|
|
|
} else {
|
|
|
|
/* Schedule event */
|
|
|
|
scheduleEvent(removeMoteEvent, Simulation.this.getSimulationTime());
|
|
|
|
}
|
2006-08-21 14:11:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds a mote to this simulation
|
2007-10-03 16:20:57 +02:00
|
|
|
*
|
2006-08-21 14:11:16 +02:00
|
|
|
* @param mote
|
|
|
|
* Mote to add
|
|
|
|
*/
|
2009-02-18 16:57:47 +01:00
|
|
|
public void addMote(final Mote mote) {
|
2007-10-03 16:20:57 +02:00
|
|
|
if (maxMoteStartupDelay > 0 && mote.getInterfaces().getClock() != null) {
|
2009-02-18 11:09:32 +01:00
|
|
|
mote.getInterfaces().getClock().setDrift(
|
2009-05-26 16:15:41 +02:00
|
|
|
- getSimulationTime()
|
|
|
|
- randomGenerator.nextInt((int)maxMoteStartupDelay)
|
2009-02-18 11:09:32 +01:00
|
|
|
);
|
2007-06-19 11:58:43 +02:00
|
|
|
}
|
2007-10-03 16:20:57 +02:00
|
|
|
|
2009-02-18 16:57:47 +01:00
|
|
|
if (!isRunning()) {
|
|
|
|
/* Simulation is stopped, add mote immediately */
|
|
|
|
motes.add(mote);
|
|
|
|
currentRadioMedium.registerMote(mote, this);
|
|
|
|
this.setChanged();
|
|
|
|
this.notifyObservers(this);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Simulation is running, add mote in simulation loop */
|
|
|
|
TimeEvent addNewMoteEvent = new TimeEvent(0) {
|
|
|
|
public void execute(long t) {
|
|
|
|
motes.add(mote);
|
|
|
|
currentRadioMedium.registerMote(mote, Simulation.this);
|
2009-05-26 16:15:41 +02:00
|
|
|
recreateMoteLists();
|
2009-02-18 16:57:47 +01:00
|
|
|
Simulation.this.setChanged();
|
|
|
|
Simulation.this.notifyObservers(this);
|
|
|
|
}
|
2009-05-26 16:15:41 +02:00
|
|
|
public String toString() {
|
|
|
|
return "ADD MOTE";
|
|
|
|
}
|
2009-02-18 16:57:47 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
scheduleEvent(addNewMoteEvent, Simulation.this.getSimulationTime());
|
2006-08-21 14:11:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a mote from this simulation.
|
2007-10-03 16:20:57 +02:00
|
|
|
*
|
2006-08-21 14:11:16 +02:00
|
|
|
* @param pos
|
2007-01-10 15:57:42 +01:00
|
|
|
* Internal list position of mote
|
2006-08-21 14:11:16 +02:00
|
|
|
* @return Mote
|
|
|
|
*/
|
|
|
|
public Mote getMote(int pos) {
|
|
|
|
return motes.get(pos);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns number of motes in this simulation.
|
2007-10-03 16:20:57 +02:00
|
|
|
*
|
2006-08-21 14:11:16 +02:00
|
|
|
* @return Number of motes
|
|
|
|
*/
|
|
|
|
public int getMotesCount() {
|
|
|
|
return motes.size();
|
|
|
|
}
|
|
|
|
|
2009-02-24 16:08:20 +01:00
|
|
|
/**
|
|
|
|
* Returns all motes in this simulation.
|
|
|
|
*
|
|
|
|
* @return Motes
|
|
|
|
*/
|
|
|
|
public Mote[] getMotes() {
|
|
|
|
Mote[] arr = new Mote[motes.size()];
|
|
|
|
motes.toArray(arr);
|
|
|
|
return arr;
|
|
|
|
}
|
|
|
|
|
2006-08-21 14:11:16 +02:00
|
|
|
/**
|
|
|
|
* Returns all mote types in simulation.
|
2007-10-03 16:20:57 +02:00
|
|
|
*
|
2006-08-21 14:11:16 +02:00
|
|
|
* @return All mote types
|
|
|
|
*/
|
2009-03-10 22:05:29 +01:00
|
|
|
public MoteType[] getMoteTypes() {
|
|
|
|
MoteType[] types = new MoteType[moteTypes.size()];
|
|
|
|
moteTypes.toArray(types);
|
|
|
|
return types;
|
2006-08-21 14:11:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns mote type with given identifier.
|
2007-10-03 16:20:57 +02:00
|
|
|
*
|
2006-08-21 14:11:16 +02:00
|
|
|
* @param identifier
|
|
|
|
* Mote type identifier
|
|
|
|
* @return Mote type or null if not found
|
|
|
|
*/
|
|
|
|
public MoteType getMoteType(String identifier) {
|
|
|
|
for (MoteType moteType : getMoteTypes()) {
|
2007-10-03 16:20:57 +02:00
|
|
|
if (moteType.getIdentifier().equals(identifier)) {
|
2006-08-21 14:11:16 +02:00
|
|
|
return moteType;
|
2007-10-03 16:20:57 +02:00
|
|
|
}
|
2006-08-21 14:11:16 +02:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds given mote type to simulation.
|
2007-10-03 16:20:57 +02:00
|
|
|
*
|
2007-01-10 15:57:42 +01:00
|
|
|
* @param newMoteType Mote type
|
2006-08-21 14:11:16 +02:00
|
|
|
*/
|
|
|
|
public void addMoteType(MoteType newMoteType) {
|
|
|
|
moteTypes.add(newMoteType);
|
|
|
|
|
|
|
|
this.setChanged();
|
|
|
|
this.notifyObservers(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2009-05-26 16:15:41 +02:00
|
|
|
* Set delay time in milliseconds.
|
|
|
|
* The simulation loop sleeps this value every simulated millisecond.
|
|
|
|
*
|
|
|
|
* @param delayTime New delay time (ms)
|
2006-08-21 14:11:16 +02:00
|
|
|
*/
|
|
|
|
public void setDelayTime(int delayTime) {
|
|
|
|
this.delayTime = delayTime;
|
|
|
|
|
2008-12-04 17:52:03 +01:00
|
|
|
rescheduleEvents = true;
|
2008-10-28 14:35:59 +01:00
|
|
|
|
2006-08-21 14:11:16 +02:00
|
|
|
this.setChanged();
|
|
|
|
this.notifyObservers(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns current delay time.
|
2007-10-03 16:20:57 +02:00
|
|
|
*
|
2006-08-21 14:11:16 +02:00
|
|
|
* @return Delay time (ms)
|
|
|
|
*/
|
|
|
|
public int getDelayTime() {
|
|
|
|
return delayTime;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set simulation time to simulationTime.
|
2007-10-03 16:20:57 +02:00
|
|
|
*
|
2006-08-21 14:11:16 +02:00
|
|
|
* @param simulationTime
|
|
|
|
* New simulation time (ms)
|
|
|
|
*/
|
|
|
|
public void setSimulationTime(int simulationTime) {
|
|
|
|
currentSimulationTime = simulationTime;
|
|
|
|
|
|
|
|
this.setChanged();
|
|
|
|
this.notifyObservers(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns current simulation time.
|
2007-10-03 16:20:57 +02:00
|
|
|
*
|
2009-05-26 16:15:41 +02:00
|
|
|
* @return Simulation time (microseconds)
|
2006-08-21 14:11:16 +02:00
|
|
|
*/
|
2008-12-04 15:03:41 +01:00
|
|
|
public long getSimulationTime() {
|
2006-08-21 14:11:16 +02:00
|
|
|
return currentSimulationTime;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2009-05-26 16:15:41 +02:00
|
|
|
* Returns current simulation time rounded to milliseconds.
|
|
|
|
*
|
|
|
|
* @see #getSimulationTime()
|
|
|
|
* @return
|
2006-08-21 14:11:16 +02:00
|
|
|
*/
|
2009-05-26 16:15:41 +02:00
|
|
|
public long getSimulationTimeMillis() {
|
|
|
|
return currentSimulationTime / MILLISECOND;
|
2006-08-21 14:11:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Changes radio medium of this simulation to the given.
|
2007-10-03 16:20:57 +02:00
|
|
|
*
|
2006-08-21 14:11:16 +02:00
|
|
|
* @param radioMedium
|
|
|
|
* New radio medium
|
|
|
|
*/
|
|
|
|
public void setRadioMedium(RadioMedium radioMedium) {
|
|
|
|
// Remove current radio medium from observing motes
|
2007-10-03 16:20:57 +02:00
|
|
|
if (currentRadioMedium != null) {
|
|
|
|
for (int i = 0; i < motes.size(); i++) {
|
2006-08-21 14:11:16 +02:00
|
|
|
currentRadioMedium.unregisterMote(motes.get(i), this);
|
2007-10-03 16:20:57 +02:00
|
|
|
}
|
|
|
|
}
|
2006-08-21 14:11:16 +02:00
|
|
|
|
|
|
|
// Change current radio medium to new one
|
|
|
|
if (radioMedium == null) {
|
|
|
|
logger.fatal("Radio medium could not be created!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.currentRadioMedium = radioMedium;
|
|
|
|
|
2007-01-10 15:57:42 +01:00
|
|
|
// Add all current motes to the new radio medium
|
2007-10-03 16:20:57 +02:00
|
|
|
for (int i = 0; i < motes.size(); i++) {
|
2006-08-21 14:11:16 +02:00
|
|
|
currentRadioMedium.registerMote(motes.get(i), this);
|
2007-10-03 16:20:57 +02:00
|
|
|
}
|
2006-08-21 14:11:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get currently used radio medium.
|
2007-10-03 16:20:57 +02:00
|
|
|
*
|
2006-08-21 14:11:16 +02:00
|
|
|
* @return Currently used radio medium
|
|
|
|
*/
|
|
|
|
public RadioMedium getRadioMedium() {
|
|
|
|
return currentRadioMedium;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return true is simulation is running.
|
2007-10-03 16:20:57 +02:00
|
|
|
*
|
2006-08-21 14:11:16 +02:00
|
|
|
* @return True if simulation is running
|
|
|
|
*/
|
|
|
|
public boolean isRunning() {
|
2008-12-08 14:07:06 +01:00
|
|
|
return isRunning && simulationThread != null;
|
2006-08-21 14:11:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get current simulation title (short description).
|
2007-10-03 16:20:57 +02:00
|
|
|
*
|
2006-08-21 14:11:16 +02:00
|
|
|
* @return Title
|
|
|
|
*/
|
|
|
|
public String getTitle() {
|
|
|
|
return title;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set simulation title.
|
2007-10-03 16:20:57 +02:00
|
|
|
*
|
2006-08-21 14:11:16 +02:00
|
|
|
* @param title
|
|
|
|
* New title
|
|
|
|
*/
|
|
|
|
public void setTitle(String title) {
|
|
|
|
this.title = title;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|