Merge pull request #1224 from cmorty/pull/refactor

Pull/refactor
This commit is contained in:
Fredrik Österlind 2015-09-01 10:58:27 +02:00
commit 562a451191
12 changed files with 107 additions and 94 deletions

View file

@ -229,8 +229,8 @@ public class AreaViewer extends VisPlugin {
// We want to listen to changes both in the channel model as well as in the radio medium // We want to listen to changes both in the channel model as well as in the radio medium
currentChannelModel.addSettingsObserver(channelModelSettingsObserver); currentChannelModel.addSettingsObserver(channelModelSettingsObserver);
currentRadioMedium.addSettingsObserver(radioMediumSettingsObserver); currentRadioMedium.addRadioMediumObserver(radioMediumSettingsObserver);
currentRadioMedium.addRadioMediumObserver(radioMediumActivityObserver); currentRadioMedium.addRadioTransmissionObserver(radioMediumActivityObserver);
// Set initial size etc. // Set initial size etc.
setSize(500, 500); setSize(500, 500);
@ -2338,13 +2338,13 @@ public class AreaViewer extends VisPlugin {
} }
if (currentRadioMedium != null && radioMediumSettingsObserver != null) { if (currentRadioMedium != null && radioMediumSettingsObserver != null) {
currentRadioMedium.deleteSettingsObserver(radioMediumSettingsObserver); currentRadioMedium.deleteRadioMediumObserver(radioMediumSettingsObserver);
} else { } else {
logger.fatal("Could not remove observer: " + radioMediumSettingsObserver); logger.fatal("Could not remove observer: " + radioMediumSettingsObserver);
} }
if (currentRadioMedium != null && radioMediumActivityObserver != null) { if (currentRadioMedium != null && radioMediumActivityObserver != null) {
currentRadioMedium.deleteRadioMediumObserver(radioMediumActivityObserver); currentRadioMedium.deleteRadioTransmissionObserver(radioMediumActivityObserver);
} else { } else {
logger.fatal("Could not remove observer: " + radioMediumActivityObserver); logger.fatal("Could not remove observer: " + radioMediumActivityObserver);
} }

View file

@ -53,6 +53,8 @@ import org.contikios.cooja.Simulation;
import org.contikios.cooja.interfaces.DirectionalAntennaRadio; import org.contikios.cooja.interfaces.DirectionalAntennaRadio;
import org.contikios.cooja.interfaces.Radio; import org.contikios.cooja.interfaces.Radio;
import org.contikios.cooja.radiomediums.AbstractRadioMedium; import org.contikios.cooja.radiomediums.AbstractRadioMedium;
import org.contikios.cooja.util.ScnObservable;
import statistics.GaussianWrapper; import statistics.GaussianWrapper;
/** /**
@ -103,13 +105,7 @@ public class ChannelModel {
/** /**
* Notifies observers when this channel model has changed settings. * Notifies observers when this channel model has changed settings.
*/ */
private class SettingsObservable extends Observable { private ScnObservable settingsObservable = new ScnObservable();
private void notifySettingsChanged() {
setChanged();
notifyObservers();
}
}
private SettingsObservable settingsObservable = new SettingsObservable();
public enum Parameter { public enum Parameter {
apply_random, apply_random,
snr_threshold, snr_threshold,
@ -330,7 +326,7 @@ public class ChannelModel {
*/ */
public void removeAllObstacles() { public void removeAllObstacles() {
myObstacleWorld.removeAll(); myObstacleWorld.removeAll();
settingsObservable.notifySettingsChanged(); settingsObservable.setChangedAndNotify();
} }
/** /**
@ -360,7 +356,7 @@ public class ChannelModel {
myObstacleWorld.addObstacle(startX, startY, width, height); myObstacleWorld.addObstacle(startX, startY, width, height);
if (notify) { if (notify) {
settingsObservable.notifySettingsChanged(); settingsObservable.setChangedAndNotify();
} }
} }
@ -442,7 +438,7 @@ public class ChannelModel {
needToPrecalculateFSPL = true; needToPrecalculateFSPL = true;
needToPrecalculateOutputPower = true; needToPrecalculateOutputPower = true;
settingsObservable.notifySettingsChanged(); settingsObservable.setChangedAndNotify();
} }
/** /**
@ -450,7 +446,7 @@ public class ChannelModel {
* will be notified. * will be notified.
*/ */
public void notifySettingsChanged() { public void notifySettingsChanged() {
settingsObservable.notifySettingsChanged(); settingsObservable.setChangedAndNotify();
} }
/** /**
@ -1884,7 +1880,7 @@ public class ChannelModel {
} }
needToPrecalculateFSPL = true; needToPrecalculateFSPL = true;
needToPrecalculateOutputPower = true; needToPrecalculateOutputPower = true;
settingsObservable.notifySettingsChanged(); settingsObservable.setChangedAndNotify();
return true; return true;
} }

View file

@ -49,6 +49,7 @@ import org.contikios.cooja.interfaces.Position;
import org.contikios.cooja.interfaces.Radio; import org.contikios.cooja.interfaces.Radio;
import org.contikios.cooja.plugins.Visualizer; import org.contikios.cooja.plugins.Visualizer;
import org.contikios.cooja.radiomediums.AbstractRadioMedium; import org.contikios.cooja.radiomediums.AbstractRadioMedium;
import org.contikios.cooja.util.ScnObservable;
import org.contikios.mrm.ChannelModel.Parameter; import org.contikios.mrm.ChannelModel.Parameter;
import org.contikios.mrm.ChannelModel.RadioPair; import org.contikios.mrm.ChannelModel.RadioPair;
import org.contikios.mrm.ChannelModel.TxPair; import org.contikios.mrm.ChannelModel.TxPair;
@ -90,11 +91,6 @@ public class MRM extends AbstractRadioMedium {
private Random random = null; private Random random = null;
private ChannelModel currentChannelModel = null; private ChannelModel currentChannelModel = null;
/**
* Notifies observers when this radio medium has changed settings.
*/
private SettingsObservable settingsObservable = new SettingsObservable();
/** /**
* Creates a new Multi-path Ray-tracing Medium (MRM). * Creates a new Multi-path Ray-tracing Medium (MRM).
*/ */
@ -114,6 +110,9 @@ public class MRM extends AbstractRadioMedium {
WITH_CAPTURE_EFFECT = currentChannelModel.getParameterBooleanValue(ChannelModel.Parameter.captureEffect); WITH_CAPTURE_EFFECT = currentChannelModel.getParameterBooleanValue(ChannelModel.Parameter.captureEffect);
CAPTURE_EFFECT_THRESHOLD = currentChannelModel.getParameterDoubleValue(ChannelModel.Parameter.captureEffectSignalTreshold); CAPTURE_EFFECT_THRESHOLD = currentChannelModel.getParameterDoubleValue(ChannelModel.Parameter.captureEffectSignalTreshold);
CAPTURE_EFFECT_PREAMBLE_DURATION = currentChannelModel.getParameterDoubleValue(ChannelModel.Parameter.captureEffectPreambleDuration); CAPTURE_EFFECT_PREAMBLE_DURATION = currentChannelModel.getParameterDoubleValue(ChannelModel.Parameter.captureEffectPreambleDuration);
/* Radio Medium changed here, so notify */
radioMediumObservable.setChangedAndNotify();
} }
}); });
@ -142,6 +141,9 @@ public class MRM extends AbstractRadioMedium {
public void registerRadioInterface(Radio radio, Simulation sim) { public void registerRadioInterface(Radio radio, Simulation sim) {
super.registerRadioInterface(radio, sim); super.registerRadioInterface(radio, sim);
/* Radio Medium changed here so notify Observers */
radioMediumObservable.setChangedAndNotify();
if (WITH_NOISE && radio instanceof NoiseSourceRadio) { if (WITH_NOISE && radio instanceof NoiseSourceRadio) {
((NoiseSourceRadio)radio).addNoiseLevelListener(noiseListener); ((NoiseSourceRadio)radio).addNoiseLevelListener(noiseListener);
} }
@ -149,6 +151,9 @@ public class MRM extends AbstractRadioMedium {
public void unregisterRadioInterface(Radio radio, Simulation sim) { public void unregisterRadioInterface(Radio radio, Simulation sim) {
super.unregisterRadioInterface(radio, sim); super.unregisterRadioInterface(radio, sim);
/* Radio Medium changed here so notify Observers */
radioMediumObservable.setChangedAndNotify();
if (WITH_NOISE && radio instanceof NoiseSourceRadio) { if (WITH_NOISE && radio instanceof NoiseSourceRadio) {
((NoiseSourceRadio)radio).removeNoiseLevelListener(noiseListener); ((NoiseSourceRadio)radio).removeNoiseLevelListener(noiseListener);
} }
@ -402,25 +407,6 @@ public class MRM extends AbstractRadioMedium {
// -- MRM specific methods -- // -- MRM specific methods --
/**
* Adds an observer which is notified when this radio medium has
* changed settings, such as added or removed radios.
*
* @param obs New observer
*/
public void addSettingsObserver(Observer obs) {
settingsObservable.addObserver(obs);
}
/**
* Deletes an earlier registered setting observer.
*
* @param obs Earlier registered observer
*/
public void deleteSettingsObserver(Observer obs) {
settingsObservable.deleteObserver(obs);
}
/** /**
* @return Number of registered radios. * @return Number of registered radios.
*/ */
@ -449,13 +435,6 @@ public class MRM extends AbstractRadioMedium {
return currentChannelModel; return currentChannelModel;
} }
class SettingsObservable extends Observable {
private void notifySettingsChanged() {
setChanged();
notifyObservers();
}
}
class MRMRadioConnection extends RadioConnection { class MRMRadioConnection extends RadioConnection {
private Hashtable<Radio, Double> signalStrengths = new Hashtable<Radio, Double>(); private Hashtable<Radio, Double> signalStrengths = new Hashtable<Radio, Double>();

View file

@ -139,6 +139,7 @@ import org.contikios.cooja.plugins.ScriptRunner;
import org.contikios.cooja.plugins.SimControl; import org.contikios.cooja.plugins.SimControl;
import org.contikios.cooja.plugins.SimInformation; import org.contikios.cooja.plugins.SimInformation;
import org.contikios.cooja.util.ExecuteJAR; import org.contikios.cooja.util.ExecuteJAR;
import org.contikios.cooja.util.ScnObservable;
/** /**
* Main file of COOJA Simulator. Typically contains a visualizer for the * Main file of COOJA Simulator. Typically contains a visualizer for the
@ -325,21 +326,10 @@ public class Cooja extends Observable {
private Vector<Class<? extends Positioner>> positionerClasses = new Vector<Class<? extends Positioner>>(); private Vector<Class<? extends Positioner>> positionerClasses = new Vector<Class<? extends Positioner>>();
private class HighlightObservable extends Observable {
private void setChangedAndNotify(Mote mote) {
setChanged();
notifyObservers(mote);
}
}
private HighlightObservable moteHighlightObservable = new HighlightObservable();
private class MoteRelationsObservable extends Observable { private ScnObservable moteHighlightObservable = new ScnObservable();
private void setChangedAndNotify() {
setChanged(); private ScnObservable moteRelationObservable = new ScnObservable();
notifyObservers();
}
}
private MoteRelationsObservable moteRelationObservable = new MoteRelationsObservable();
private JTextPane quickHelpTextPane; private JTextPane quickHelpTextPane;
private JScrollPane quickHelpScroll; private JScrollPane quickHelpScroll;

View file

@ -100,24 +100,24 @@ public abstract class RadioMedium {
* Adds an observer which is notified each time a radio connection has finished. * Adds an observer which is notified each time a radio connection has finished.
* *
* @see #getLastConnection() * @see #getLastConnection()
* @see #deleteRadioMediumObserver(Observer) * @see #deleteRadioTransmissionObserver(Observer)
* @param observer New observer * @param observer New observer
*/ */
public abstract void addRadioMediumObserver(Observer observer); public abstract void addRadioTransmissionObserver(Observer observer);
/** /**
* @return Radio medium observable * @return Radio medium observable
*/ */
public abstract Observable getRadioMediumObservable(); public abstract Observable getRadioTransmissionObservable();
/** /**
* Deletes an radio medium observer. * Deletes an radio medium observer.
* *
* @see #addRadioMediumObserver(Observer) * @see #addRadioTransmissionObserver(Observer)
* @param observer * @param observer
* Observer to delete * Observer to delete
*/ */
public abstract void deleteRadioMediumObserver(Observer observer); public abstract void deleteRadioTransmissionObserver(Observer observer);
/** /**
* @return Last radio connection finished in the radio medium * @return Last radio connection finished in the radio medium

View file

@ -111,7 +111,7 @@ public class DGRMConfigurator extends VisPlugin {
radioMedium = (DirectedGraphMedium) sim.getRadioMedium(); radioMedium = (DirectedGraphMedium) sim.getRadioMedium();
/* Listen for graph updates */ /* Listen for graph updates */
radioMedium.addRadioMediumObserver(radioMediumObserver = new Observer() { radioMedium.addRadioTransmissionObserver(radioMediumObserver = new Observer() {
public void update(Observable obs, Object obj) { public void update(Observable obs, Object obj) {
model.fireTableDataChanged(); model.fireTableDataChanged();
} }
@ -500,7 +500,7 @@ public class DGRMConfigurator extends VisPlugin {
}; };
public void closePlugin() { public void closePlugin() {
radioMedium.deleteRadioMediumObserver(radioMediumObserver); radioMedium.deleteRadioTransmissionObserver(radioMediumObserver);
} }
} }

View file

@ -223,7 +223,7 @@ public class EventListener extends VisPlugin {
JCheckBox radioMediumCheckBox = new JCheckBox("Radio medium event", false); JCheckBox radioMediumCheckBox = new JCheckBox("Radio medium event", false);
radioMediumCheckBox.putClientProperty("observable", mySimulation radioMediumCheckBox.putClientProperty("observable", mySimulation
.getRadioMedium().getRadioMediumObservable()); .getRadioMedium().getRadioTransmissionObservable());
radioMediumCheckBox.addActionListener(generalCheckBoxListener); radioMediumCheckBox.addActionListener(generalCheckBoxListener);
generalPanel.add(radioMediumCheckBox); generalPanel.add(radioMediumCheckBox);

View file

@ -509,7 +509,7 @@ public class RadioLogger extends VisPlugin {
adjuster.setDynamicAdjustment(true); adjuster.setDynamicAdjustment(true);
adjuster.packColumns(); adjuster.packColumns();
radioMedium.addRadioMediumObserver(radioMediumObserver = new Observer() { radioMedium.addRadioTransmissionObserver(radioMediumObserver = new Observer() {
@Override @Override
public void update(Observable obs, Object obj) { public void update(Observable obs, Object obj) {
RadioConnection conn = radioMedium.getLastConnection(); RadioConnection conn = radioMedium.getLastConnection();
@ -784,7 +784,7 @@ public class RadioLogger extends VisPlugin {
@Override @Override
public void closePlugin() { public void closePlugin() {
if (radioMediumObserver != null) { if (radioMediumObserver != null) {
radioMedium.deleteRadioMediumObserver(radioMediumObserver); radioMedium.deleteRadioTransmissionObserver(radioMediumObserver);
} }
} }

View file

@ -130,7 +130,7 @@ public class TrafficVisualizerSkin implements VisualizerSkin {
historyList.clear(); historyList.clear();
/* Start observing radio medium for transmissions */ /* Start observing radio medium for transmissions */
radioMedium.addRadioMediumObserver(radioMediumObserver); radioMedium.addRadioTransmissionObserver(radioMediumObserver);
/* Fade away arrows */ /* Fade away arrows */
simulation.scheduleEvent(ageArrowsTimeEvent, simulation.getSimulationTime() + 100*Simulation.MILLISECOND); simulation.scheduleEvent(ageArrowsTimeEvent, simulation.getSimulationTime() + 100*Simulation.MILLISECOND);
@ -147,7 +147,7 @@ public class TrafficVisualizerSkin implements VisualizerSkin {
} }
/* Stop observing radio medium */ /* Stop observing radio medium */
radioMedium.deleteRadioMediumObserver(radioMediumObserver); radioMedium.deleteRadioTransmissionObserver(radioMediumObserver);
} }
@Override @Override

View file

@ -49,8 +49,10 @@ import org.contikios.cooja.Simulation;
import org.contikios.cooja.TimeEvent; import org.contikios.cooja.TimeEvent;
import org.contikios.cooja.interfaces.CustomDataRadio; import org.contikios.cooja.interfaces.CustomDataRadio;
import org.contikios.cooja.interfaces.Radio; import org.contikios.cooja.interfaces.Radio;
import org.contikios.cooja.util.ScnObservable;
import org.jdom.Element; import org.jdom.Element;
/** /**
* Abstract radio medium provides basic functionality for implementing radio * Abstract radio medium provides basic functionality for implementing radio
* mediums. * mediums.
@ -90,17 +92,13 @@ public abstract class AbstractRadioMedium extends RadioMedium {
public int COUNTER_RX = 0; public int COUNTER_RX = 0;
public int COUNTER_INTERFERED = 0; public int COUNTER_INTERFERED = 0;
public class RadioMediumObservable extends Observable { /**
public void setRadioMediumChanged() { * Two Observables to observe the radioMedium and radioTransmissions
setChanged(); * @see addRadioTransmissionObserver
} * @see addRadioMediumObserver
public void setRadioMediumChangedAndNotify() { */
setChanged(); protected ScnObservable radioMediumObservable = new ScnObservable();
notifyObservers(); protected ScnObservable radioTransmissionObservable = new ScnObservable();
}
}
private RadioMediumObservable radioMediumObservable = new RadioMediumObservable();
/** /**
* This constructor should always be called from implemented radio mediums. * This constructor should always be called from implemented radio mediums.
@ -288,7 +286,7 @@ public abstract class AbstractRadioMedium extends RadioMedium {
/* Notify observers */ /* Notify observers */
lastConnection = null; lastConnection = null;
radioMediumObservable.setRadioMediumChangedAndNotify(); radioTransmissionObservable.setChangedAndNotify();
} }
break; break;
case TRANSMISSION_FINISHED: { case TRANSMISSION_FINISHED: {
@ -333,7 +331,7 @@ public abstract class AbstractRadioMedium extends RadioMedium {
updateSignalStrengths(); updateSignalStrengths();
/* Notify observers */ /* Notify observers */
radioMediumObservable.setRadioMediumChangedAndNotify(); radioTransmissionObservable.setChangedAndNotify();
} }
break; break;
case CUSTOM_DATA_TRANSMITTED: { case CUSTOM_DATA_TRANSMITTED: {
@ -348,7 +346,7 @@ public abstract class AbstractRadioMedium extends RadioMedium {
/* Custom data object */ /* Custom data object */
Object data = ((CustomDataRadio) radio).getLastCustomDataTransmitted(); Object data = ((CustomDataRadio) radio).getLastCustomDataTransmitted();
if (data == null) { if (data == null) {
logger.fatal("No custom data object to forward"); logger.fatal("No custom data objecTransmissiont to forward");
return; return;
} }
@ -446,6 +444,7 @@ public abstract class AbstractRadioMedium extends RadioMedium {
registeredRadios.add(radio); registeredRadios.add(radio);
radio.addObserver(radioEventsObserver); radio.addObserver(radioEventsObserver);
radioMediumObservable.setChangedAndNotify();
/* Update signal strengths */ /* Update signal strengths */
updateSignalStrengths(); updateSignalStrengths();
@ -462,6 +461,8 @@ public abstract class AbstractRadioMedium extends RadioMedium {
removeFromActiveConnections(radio); removeFromActiveConnections(radio);
radioMediumObservable.setChangedAndNotify();
/* Update signal strengths */ /* Update signal strengths */
updateSignalStrengths(); updateSignalStrengths();
} }
@ -528,10 +529,40 @@ public abstract class AbstractRadioMedium extends RadioMedium {
sendRssi.put(radio, rssi); sendRssi.put(radio, rssi);
} }
/**
* Register an observer that gets notified when the radiotransmissions changed.
* E.g. creating new connections.
* This does not include changes in the settings and (de-)registration of radios.
* @see addRadioMediumObserver
* @param observer the Observer to register
*/
public void addRadioTransmissionObserver(Observer observer) {
radioTransmissionObservable.addObserver(observer);
}
public Observable getRadioTransmissionObservable() {
return radioTransmissionObservable;
}
public void deleteRadioTransmissionObserver(Observer observer) {
radioTransmissionObservable.deleteObserver(observer);
}
/**
* Register an observer that gets notified when the radio medium changed.
* This includes changes in the settings and (de-)registration of radios.
* This does not include transmissions, etc as these are part of the radio
* and not the radio medium itself.
* @see addRadioTransmissionObserver
* @param observer the Observer to register
*/
public void addRadioMediumObserver(Observer observer) { public void addRadioMediumObserver(Observer observer) {
radioMediumObservable.addObserver(observer); radioMediumObservable.addObserver(observer);
} }
/**
* @return the radioMediumObservable
*/
public Observable getRadioMediumObservable() { public Observable getRadioMediumObservable() {
return radioMediumObservable; return radioMediumObservable;
} }

View file

@ -93,8 +93,7 @@ public class DirectedGraphMedium extends AbstractRadioMedium {
edges.add(e); edges.add(e);
requestEdgeAnalysis(); requestEdgeAnalysis();
((AbstractRadioMedium.RadioMediumObservable) radioTransmissionObservable.setChangedAndNotify();
this.getRadioMediumObservable()).setRadioMediumChangedAndNotify();
} }
public void removeEdge(Edge edge) { public void removeEdge(Edge edge) {
@ -105,16 +104,15 @@ public class DirectedGraphMedium extends AbstractRadioMedium {
edges.remove(edge); edges.remove(edge);
requestEdgeAnalysis(); requestEdgeAnalysis();
((AbstractRadioMedium.RadioMediumObservable)
this.getRadioMediumObservable()).setRadioMediumChangedAndNotify(); radioTransmissionObservable.setChangedAndNotify();
} }
public void clearEdges() { public void clearEdges() {
edges.clear(); edges.clear();
requestEdgeAnalysis(); requestEdgeAnalysis();
((AbstractRadioMedium.RadioMediumObservable) radioTransmissionObservable.setChangedAndNotify();
this.getRadioMediumObservable()).setRadioMediumChangedAndNotify();
} }
public Edge[] getEdges() { public Edge[] getEdges() {
@ -224,6 +222,9 @@ public class DirectedGraphMedium extends AbstractRadioMedium {
this.edgesTable = arrTable; this.edgesTable = arrTable;
edgesDirty = false; edgesDirty = false;
/* Radio Medium changed here so notify Observers */
radioMediumObservable.setChangedAndNotify();
} }
/** /**

View file

@ -0,0 +1,16 @@
package org.contikios.cooja.util;
import java.util.Observable;
public class ScnObservable extends Observable {
public void setChangedAndNotify() {
setChanged();
notifyObservers();
}
public void setChangedAndNotify(Object obj) {
setChanged();
notifyObservers(obj);
}
}