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

@ -24,7 +24,7 @@
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* 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;
@ -33,8 +33,6 @@ import java.util.*;
import org.apache.log4j.Logger;
import org.jdom.*;
import se.sics.cooja.contikimote.ContikiMote;
import se.sics.cooja.contikimote.interfaces.ContikiClock;
import se.sics.cooja.dialogs.*;
/**
@ -161,8 +159,9 @@ public class Simulation extends Observable implements Runnable {
// Select next mote subset (persistent)
currentTickListIndex++;
if (currentTickListIndex >= nrTickLists)
if (currentTickListIndex >= nrTickLists) {
currentTickListIndex = 0;
}
// Increase simulation time
currentSimulationTime += tickTime;
@ -171,8 +170,9 @@ public class Simulation extends Observable implements Runnable {
tickObservable.allTicksPerformed();
// Sleep
if (delayTime > 0)
if (delayTime > 0) {
Thread.sleep(delayTime);
}
if (stopSimulation) {
// We should only tick once (and we just did), so abort now
@ -239,7 +239,7 @@ public class Simulation extends Observable implements Runnable {
thread.interrupt();
// Wait until simulation stops
if (Thread.currentThread() != thread)
if (Thread.currentThread() != thread) {
while (thread != null && thread.isAlive()) {
try {
Thread.sleep(10);
@ -248,6 +248,7 @@ public class Simulation extends Observable implements Runnable {
}
}
}
}
/**
* Starts simulation if stopped, ticks all motes once, and finally stops
@ -366,8 +367,9 @@ public class Simulation extends Observable implements Runnable {
element.setText(currentRadioMedium.getClass().getName());
Collection radioMediumXML = currentRadioMedium.getConfigXML();
if (radioMediumXML != null)
if (radioMediumXML != null) {
element.addContent(radioMediumXML);
}
config.add(element);
// Mote types
@ -376,8 +378,9 @@ public class Simulation extends Observable implements Runnable {
element.setText(moteType.getClass().getName());
Collection moteTypeXML = moteType.getConfigXML();
if (moteTypeXML != null)
if (moteTypeXML != null) {
element.addContent(moteTypeXML);
}
config.add(element);
}
@ -387,8 +390,9 @@ public class Simulation extends Observable implements Runnable {
element.setText(mote.getClass().getName());
Collection moteXML = mote.getConfigXML();
if (moteXML != null)
if (moteXML != null) {
element.addContent(moteXML);
}
config.add(element);
}
@ -543,8 +547,9 @@ public class Simulation extends Observable implements Runnable {
stopSimulation();
motes.remove(mote);
startSimulation();
} else
} else {
motes.remove(mote);
}
currentRadioMedium.unregisterMote(mote, this);
this.setChanged();
@ -562,11 +567,12 @@ public class Simulation extends Observable implements Runnable {
stopSimulation();
motes.add(mote);
startSimulation();
} else
} else {
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);
@ -612,9 +618,10 @@ public class Simulation extends Observable implements Runnable {
*/
public MoteType getMoteType(String identifier) {
for (MoteType moteType : getMoteTypes()) {
if (moteType.getIdentifier().equals(identifier))
if (moteType.getIdentifier().equals(identifier)) {
return moteType;
}
}
return null;
}
@ -700,9 +707,11 @@ public class Simulation extends Observable implements Runnable {
*/
public void setRadioMedium(RadioMedium radioMedium) {
// Remove current radio medium from observing motes
if (currentRadioMedium != null)
for (int i = 0; i < motes.size(); i++)
if (currentRadioMedium != null) {
for (int i = 0; i < motes.size(); i++) {
currentRadioMedium.unregisterMote(motes.get(i), this);
}
}
// Change current radio medium to new one
if (radioMedium == null) {
@ -712,9 +721,10 @@ public class Simulation extends Observable implements Runnable {
this.currentRadioMedium = radioMedium;
// 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);
}
}
/**
* Get currently used radio medium.
@ -742,7 +752,7 @@ public class Simulation extends Observable implements Runnable {
* @return Current tick time (seconds)
*/
public double getTickTimeInSeconds() {
return ((double) tickTime) / 1000.0;
return (tickTime) / 1000.0;
}
/**

View file

@ -26,7 +26,7 @@
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* 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;
@ -57,4 +57,11 @@ public abstract class Clock extends MoteInterface {
*/
public abstract int getTime();
/**
* Set time drift.
*
* @param timeDrift Time drift
*/
public abstract void setDrift(int timeDrift);
}