set drift method as standard for all clock interfaces

This commit is contained in:
fros4943 2007-10-03 14:20:57 +00:00
parent fb9b800c53
commit dfc0199cbd
2 changed files with 75 additions and 58 deletions

View File

@ -1,7 +1,7 @@
/* /*
* Copyright (c) 2006, Swedish Institute of Computer Science. All rights * Copyright (c) 2006, Swedish Institute of Computer Science. All rights
* reserved. * reserved.
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met: * modification, are permitted provided that the following conditions are met:
* 1. Redistributions of source code must retain the above copyright notice, * 1. Redistributions of source code must retain the above copyright notice,
@ -12,7 +12,7 @@
* Institute nor the names of its contributors may be used to endorse or promote * Institute nor the names of its contributors may be used to endorse or promote
* products derived from this software without specific prior written * products derived from this software without specific prior written
* permission. * permission.
* *
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND ANY * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
@ -23,8 +23,8 @@
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * 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 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* *
* $Id: Simulation.java,v 1.17 2007/08/21 08:51:33 fros4943 Exp $ * $Id: Simulation.java,v 1.18 2007/10/03 14:20:57 fros4943 Exp $
*/ */
package se.sics.cooja; package se.sics.cooja;
@ -33,23 +33,21 @@ import java.util.*;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
import org.jdom.*; import org.jdom.*;
import se.sics.cooja.contikimote.ContikiMote;
import se.sics.cooja.contikimote.interfaces.ContikiClock;
import se.sics.cooja.dialogs.*; import se.sics.cooja.dialogs.*;
/** /**
* A simulation consists of a number of motes and mote types. * A simulation consists of a number of motes and mote types.
* *
* The motes in the simulation are ticked one by one in a simulation loop. When * The motes in the simulation are ticked one by one in a simulation loop. When
* all motes have been ticked once, the simulation time is updated and then the * all motes have been ticked once, the simulation time is updated and then the
* simulation sleeps for some specified delay time. Any tick observers are also * simulation sleeps for some specified delay time. Any tick observers are also
* notified at this time. * notified at this time.
* *
* When observing the simulation itself, the simulation state, added or deleted * When observing the simulation itself, the simulation state, added or deleted
* motes etc. are observed, as opposed to individual mote changes. Changes of * motes etc. are observed, as opposed to individual mote changes. Changes of
* individual motes should instead be observed via corresponding mote * individual motes should instead be observed via corresponding mote
* interfaces. * interfaces.
* *
* @author Fredrik Osterlind * @author Fredrik Osterlind
*/ */
public class Simulation extends Observable implements Runnable { public class Simulation extends Observable implements Runnable {
@ -87,7 +85,7 @@ public class Simulation extends Observable implements Runnable {
private int maxMoteStartupDelay = 0; private int maxMoteStartupDelay = 0;
private Random tickListRandom = new Random(); private Random tickListRandom = new Random();
private Random delayMotesRandom = new Random(); private Random delayMotesRandom = new Random();
// Tick observable // Tick observable
@ -103,7 +101,7 @@ public class Simulation extends Observable implements Runnable {
/** /**
* Add tick observer. This observer is notified once every tick loop, that is, * Add tick observer. This observer is notified once every tick loop, that is,
* when all motes have been ticked. * when all motes have been ticked.
* *
* @see #deleteTickObserver(Observer) * @see #deleteTickObserver(Observer)
* @param newObserver * @param newObserver
* New observer * New observer
@ -114,7 +112,7 @@ public class Simulation extends Observable implements Runnable {
/** /**
* Delete an existing tick observer. * Delete an existing tick observer.
* *
* @see #addTickObserver(Observer) * @see #addTickObserver(Observer)
* @param observer * @param observer
* Observer to delete * Observer to delete
@ -150,19 +148,20 @@ public class Simulation extends Observable implements Runnable {
allLists[i][j] = motesClone.remove(moteNr); allLists[i][j] = motesClone.remove(moteNr);
} }
} }
while (isRunning) { while (isRunning) {
try { try {
// Tick current mote subset // Tick current mote subset
for (Mote moteToTick : allLists[currentTickListIndex]) { for (Mote moteToTick : allLists[currentTickListIndex]) {
moteToTick.tick(currentSimulationTime); moteToTick.tick(currentSimulationTime);
} }
// Select next mote subset (persistent) // Select next mote subset (persistent)
currentTickListIndex++; currentTickListIndex++;
if (currentTickListIndex >= nrTickLists) if (currentTickListIndex >= nrTickLists) {
currentTickListIndex = 0; currentTickListIndex = 0;
}
// Increase simulation time // Increase simulation time
currentSimulationTime += tickTime; currentSimulationTime += tickTime;
@ -171,8 +170,9 @@ public class Simulation extends Observable implements Runnable {
tickObservable.allTicksPerformed(); tickObservable.allTicksPerformed();
// Sleep // Sleep
if (delayTime > 0) if (delayTime > 0) {
Thread.sleep(delayTime); Thread.sleep(delayTime);
}
if (stopSimulation) { if (stopSimulation) {
// We should only tick once (and we just did), so abort now // We should only tick once (and we just did), so abort now
@ -239,14 +239,15 @@ public class Simulation extends Observable implements Runnable {
thread.interrupt(); thread.interrupt();
// Wait until simulation stops // Wait until simulation stops
if (Thread.currentThread() != thread) if (Thread.currentThread() != thread) {
while (thread != null && thread.isAlive()) { while (thread != null && thread.isAlive()) {
try { try {
Thread.sleep(10); Thread.sleep(10);
} catch (InterruptedException e) { } catch (InterruptedException e) {
} }
} }
} }
}
} }
/** /**
@ -323,7 +324,7 @@ public class Simulation extends Observable implements Runnable {
/** /**
* Returns the current simulation config represented by XML elements. This * Returns the current simulation config represented by XML elements. This
* config also includes the current radio medium, all mote types and motes. * config also includes the current radio medium, all mote types and motes.
* *
* @return Current simulation config * @return Current simulation config
*/ */
public Collection<Element> getConfigXML() { public Collection<Element> getConfigXML() {
@ -366,8 +367,9 @@ public class Simulation extends Observable implements Runnable {
element.setText(currentRadioMedium.getClass().getName()); element.setText(currentRadioMedium.getClass().getName());
Collection radioMediumXML = currentRadioMedium.getConfigXML(); Collection radioMediumXML = currentRadioMedium.getConfigXML();
if (radioMediumXML != null) if (radioMediumXML != null) {
element.addContent(radioMediumXML); element.addContent(radioMediumXML);
}
config.add(element); config.add(element);
// Mote types // Mote types
@ -376,8 +378,9 @@ public class Simulation extends Observable implements Runnable {
element.setText(moteType.getClass().getName()); element.setText(moteType.getClass().getName());
Collection moteTypeXML = moteType.getConfigXML(); Collection moteTypeXML = moteType.getConfigXML();
if (moteTypeXML != null) if (moteTypeXML != null) {
element.addContent(moteTypeXML); element.addContent(moteTypeXML);
}
config.add(element); config.add(element);
} }
@ -387,8 +390,9 @@ public class Simulation extends Observable implements Runnable {
element.setText(mote.getClass().getName()); element.setText(mote.getClass().getName());
Collection moteXML = mote.getConfigXML(); Collection moteXML = mote.getConfigXML();
if (moteXML != null) if (moteXML != null) {
element.addContent(moteXML); element.addContent(moteXML);
}
config.add(element); config.add(element);
} }
@ -397,7 +401,7 @@ public class Simulation extends Observable implements Runnable {
/** /**
* Sets the current simulation config depending on the given XML elements. * Sets the current simulation config depending on the given XML elements.
* *
* @see #getConfigXML() * @see #getConfigXML()
* @param configXML * @param configXML
* Config XML elements * Config XML elements
@ -534,7 +538,7 @@ public class Simulation extends Observable implements Runnable {
/** /**
* Removes a mote from this simulation * Removes a mote from this simulation
* *
* @param mote * @param mote
* Mote to remove * Mote to remove
*/ */
@ -543,8 +547,9 @@ public class Simulation extends Observable implements Runnable {
stopSimulation(); stopSimulation();
motes.remove(mote); motes.remove(mote);
startSimulation(); startSimulation();
} else } else {
motes.remove(mote); motes.remove(mote);
}
currentRadioMedium.unregisterMote(mote, this); currentRadioMedium.unregisterMote(mote, this);
this.setChanged(); this.setChanged();
@ -553,7 +558,7 @@ public class Simulation extends Observable implements Runnable {
/** /**
* Adds a mote to this simulation * Adds a mote to this simulation
* *
* @param mote * @param mote
* Mote to add * Mote to add
*/ */
@ -562,13 +567,14 @@ public class Simulation extends Observable implements Runnable {
stopSimulation(); stopSimulation();
motes.add(mote); motes.add(mote);
startSimulation(); startSimulation();
} else } else {
motes.add(mote); motes.add(mote);
if (maxMoteStartupDelay > 0 && mote instanceof ContikiMote) {
((ContikiClock) mote.getInterfaces().getClock()).setDrift(-delayMotesRandom.nextInt(maxMoteStartupDelay));
} }
if (maxMoteStartupDelay > 0 && mote.getInterfaces().getClock() != null) {
mote.getInterfaces().getClock().setDrift(-delayMotesRandom.nextInt(maxMoteStartupDelay));
}
currentRadioMedium.registerMote(mote, this); currentRadioMedium.registerMote(mote, this);
this.setChanged(); this.setChanged();
this.notifyObservers(this); this.notifyObservers(this);
@ -576,7 +582,7 @@ public class Simulation extends Observable implements Runnable {
/** /**
* Get a mote from this simulation. * Get a mote from this simulation.
* *
* @param pos * @param pos
* Internal list position of mote * Internal list position of mote
* @return Mote * @return Mote
@ -587,7 +593,7 @@ public class Simulation extends Observable implements Runnable {
/** /**
* Returns number of motes in this simulation. * Returns number of motes in this simulation.
* *
* @return Number of motes * @return Number of motes
*/ */
public int getMotesCount() { public int getMotesCount() {
@ -596,7 +602,7 @@ public class Simulation extends Observable implements Runnable {
/** /**
* Returns all mote types in simulation. * Returns all mote types in simulation.
* *
* @return All mote types * @return All mote types
*/ */
public Vector<MoteType> getMoteTypes() { public Vector<MoteType> getMoteTypes() {
@ -605,22 +611,23 @@ public class Simulation extends Observable implements Runnable {
/** /**
* Returns mote type with given identifier. * Returns mote type with given identifier.
* *
* @param identifier * @param identifier
* Mote type identifier * Mote type identifier
* @return Mote type or null if not found * @return Mote type or null if not found
*/ */
public MoteType getMoteType(String identifier) { public MoteType getMoteType(String identifier) {
for (MoteType moteType : getMoteTypes()) { for (MoteType moteType : getMoteTypes()) {
if (moteType.getIdentifier().equals(identifier)) if (moteType.getIdentifier().equals(identifier)) {
return moteType; return moteType;
}
} }
return null; return null;
} }
/** /**
* Adds given mote type to simulation. * Adds given mote type to simulation.
* *
* @param newMoteType Mote type * @param newMoteType Mote type
*/ */
public void addMoteType(MoteType newMoteType) { public void addMoteType(MoteType newMoteType) {
@ -633,7 +640,7 @@ public class Simulation extends Observable implements Runnable {
/** /**
* Set delay time to delayTime. When all motes have been ticked, the * Set delay time to delayTime. When all motes have been ticked, the
* simulation waits for this time before ticking again. * simulation waits for this time before ticking again.
* *
* @param delayTime * @param delayTime
* New delay time (ms) * New delay time (ms)
*/ */
@ -646,7 +653,7 @@ public class Simulation extends Observable implements Runnable {
/** /**
* Returns current delay time. * Returns current delay time.
* *
* @return Delay time (ms) * @return Delay time (ms)
*/ */
public int getDelayTime() { public int getDelayTime() {
@ -655,7 +662,7 @@ public class Simulation extends Observable implements Runnable {
/** /**
* Set simulation time to simulationTime. * Set simulation time to simulationTime.
* *
* @param simulationTime * @param simulationTime
* New simulation time (ms) * New simulation time (ms)
*/ */
@ -668,7 +675,7 @@ public class Simulation extends Observable implements Runnable {
/** /**
* Returns current simulation time. * Returns current simulation time.
* *
* @return Simulation time (ms) * @return Simulation time (ms)
*/ */
public int getSimulationTime() { public int getSimulationTime() {
@ -679,7 +686,7 @@ public class Simulation extends Observable implements Runnable {
* Set tick time to tickTime. The tick time is the simulated time every tick * Set tick time to tickTime. The tick time is the simulated time every tick
* takes. When all motes have been ticked, current simulation time is * takes. When all motes have been ticked, current simulation time is
* increased with tickTime. Default tick time is 1 ms. * increased with tickTime. Default tick time is 1 ms.
* *
* @see #getTickTime() * @see #getTickTime()
* @see #getTickTimeInSeconds() * @see #getTickTimeInSeconds()
* @param tickTime * @param tickTime
@ -694,15 +701,17 @@ public class Simulation extends Observable implements Runnable {
/** /**
* Changes radio medium of this simulation to the given. * Changes radio medium of this simulation to the given.
* *
* @param radioMedium * @param radioMedium
* New radio medium * New radio medium
*/ */
public void setRadioMedium(RadioMedium radioMedium) { public void setRadioMedium(RadioMedium radioMedium) {
// Remove current radio medium from observing motes // Remove current radio medium from observing motes
if (currentRadioMedium != null) if (currentRadioMedium != null) {
for (int i = 0; i < motes.size(); i++) for (int i = 0; i < motes.size(); i++) {
currentRadioMedium.unregisterMote(motes.get(i), this); currentRadioMedium.unregisterMote(motes.get(i), this);
}
}
// Change current radio medium to new one // Change current radio medium to new one
if (radioMedium == null) { if (radioMedium == null) {
@ -712,13 +721,14 @@ public class Simulation extends Observable implements Runnable {
this.currentRadioMedium = radioMedium; this.currentRadioMedium = radioMedium;
// Add all current motes to the new radio medium // Add all current motes to the new radio medium
for (int i = 0; i < motes.size(); i++) for (int i = 0; i < motes.size(); i++) {
currentRadioMedium.registerMote(motes.get(i), this); currentRadioMedium.registerMote(motes.get(i), this);
}
} }
/** /**
* Get currently used radio medium. * Get currently used radio medium.
* *
* @return Currently used radio medium * @return Currently used radio medium
*/ */
public RadioMedium getRadioMedium() { public RadioMedium getRadioMedium() {
@ -727,7 +737,7 @@ public class Simulation extends Observable implements Runnable {
/** /**
* Get current tick time (ms). * Get current tick time (ms).
* *
* @see #setTickTime(int) * @see #setTickTime(int)
* @return Current tick time (ms) * @return Current tick time (ms)
*/ */
@ -737,17 +747,17 @@ public class Simulation extends Observable implements Runnable {
/** /**
* Get current tick time (seconds). * Get current tick time (seconds).
* *
* @see #setTickTime(int) * @see #setTickTime(int)
* @return Current tick time (seconds) * @return Current tick time (seconds)
*/ */
public double getTickTimeInSeconds() { public double getTickTimeInSeconds() {
return ((double) tickTime) / 1000.0; return (tickTime) / 1000.0;
} }
/** /**
* Return true is simulation is running. * Return true is simulation is running.
* *
* @return True if simulation is running * @return True if simulation is running
*/ */
public boolean isRunning() { public boolean isRunning() {
@ -756,7 +766,7 @@ public class Simulation extends Observable implements Runnable {
/** /**
* Get current simulation title (short description). * Get current simulation title (short description).
* *
* @return Title * @return Title
*/ */
public String getTitle() { public String getTitle() {
@ -765,7 +775,7 @@ public class Simulation extends Observable implements Runnable {
/** /**
* Set simulation title. * Set simulation title.
* *
* @param title * @param title
* New title * New title
*/ */

View File

@ -26,7 +26,7 @@
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE. * SUCH DAMAGE.
* *
* $Id: Clock.java,v 1.1 2006/08/21 12:12:59 fros4943 Exp $ * $Id: Clock.java,v 1.2 2007/10/03 14:20:57 fros4943 Exp $
*/ */
package se.sics.cooja.interfaces; package se.sics.cooja.interfaces;
@ -36,9 +36,9 @@ import se.sics.cooja.*;
/** /**
* A Clock represents a mote's internal clock. Notice that the overall * A Clock represents a mote's internal clock. Notice that the overall
* simulation time and the mote's own time may differ. * simulation time and the mote's own time may differ.
* *
* This observable never needs to update. * This observable never needs to update.
* *
* @author Fredrik Osterlind * @author Fredrik Osterlind
*/ */
@ClassDescription("Clock") @ClassDescription("Clock")
@ -46,7 +46,7 @@ public abstract class Clock extends MoteInterface {
/** /**
* Set mote's time to given time. * Set mote's time to given time.
* *
* @param newTime * @param newTime
* New time * New time
*/ */
@ -57,4 +57,11 @@ public abstract class Clock extends MoteInterface {
*/ */
public abstract int getTime(); public abstract int getTime();
/**
* Set time drift.
*
* @param timeDrift Time drift
*/
public abstract void setDrift(int timeDrift);
} }