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
currentChannelModel.addSettingsObserver(channelModelSettingsObserver);
currentRadioMedium.addSettingsObserver(radioMediumSettingsObserver);
currentRadioMedium.addRadioMediumObserver(radioMediumActivityObserver);
currentRadioMedium.addRadioMediumObserver(radioMediumSettingsObserver);
currentRadioMedium.addRadioTransmissionObserver(radioMediumActivityObserver);
// Set initial size etc.
setSize(500, 500);
@ -2338,13 +2338,13 @@ public class AreaViewer extends VisPlugin {
}
if (currentRadioMedium != null && radioMediumSettingsObserver != null) {
currentRadioMedium.deleteSettingsObserver(radioMediumSettingsObserver);
currentRadioMedium.deleteRadioMediumObserver(radioMediumSettingsObserver);
} else {
logger.fatal("Could not remove observer: " + radioMediumSettingsObserver);
}
if (currentRadioMedium != null && radioMediumActivityObserver != null) {
currentRadioMedium.deleteRadioMediumObserver(radioMediumActivityObserver);
currentRadioMedium.deleteRadioTransmissionObserver(radioMediumActivityObserver);
} else {
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.Radio;
import org.contikios.cooja.radiomediums.AbstractRadioMedium;
import org.contikios.cooja.util.ScnObservable;
import statistics.GaussianWrapper;
/**
@ -103,13 +105,7 @@ public class ChannelModel {
/**
* Notifies observers when this channel model has changed settings.
*/
private class SettingsObservable extends Observable {
private void notifySettingsChanged() {
setChanged();
notifyObservers();
}
}
private SettingsObservable settingsObservable = new SettingsObservable();
private ScnObservable settingsObservable = new ScnObservable();
public enum Parameter {
apply_random,
snr_threshold,
@ -330,7 +326,7 @@ public class ChannelModel {
*/
public void removeAllObstacles() {
myObstacleWorld.removeAll();
settingsObservable.notifySettingsChanged();
settingsObservable.setChangedAndNotify();
}
/**
@ -360,7 +356,7 @@ public class ChannelModel {
myObstacleWorld.addObstacle(startX, startY, width, height);
if (notify) {
settingsObservable.notifySettingsChanged();
settingsObservable.setChangedAndNotify();
}
}
@ -442,7 +438,7 @@ public class ChannelModel {
needToPrecalculateFSPL = true;
needToPrecalculateOutputPower = true;
settingsObservable.notifySettingsChanged();
settingsObservable.setChangedAndNotify();
}
/**
@ -450,7 +446,7 @@ public class ChannelModel {
* will be notified.
*/
public void notifySettingsChanged() {
settingsObservable.notifySettingsChanged();
settingsObservable.setChangedAndNotify();
}
/**
@ -1884,7 +1880,7 @@ public class ChannelModel {
}
needToPrecalculateFSPL = true;
needToPrecalculateOutputPower = true;
settingsObservable.notifySettingsChanged();
settingsObservable.setChangedAndNotify();
return true;
}

View file

@ -49,6 +49,7 @@ import org.contikios.cooja.interfaces.Position;
import org.contikios.cooja.interfaces.Radio;
import org.contikios.cooja.plugins.Visualizer;
import org.contikios.cooja.radiomediums.AbstractRadioMedium;
import org.contikios.cooja.util.ScnObservable;
import org.contikios.mrm.ChannelModel.Parameter;
import org.contikios.mrm.ChannelModel.RadioPair;
import org.contikios.mrm.ChannelModel.TxPair;
@ -90,11 +91,6 @@ public class MRM extends AbstractRadioMedium {
private Random random = 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).
*/
@ -114,6 +110,9 @@ public class MRM extends AbstractRadioMedium {
WITH_CAPTURE_EFFECT = currentChannelModel.getParameterBooleanValue(ChannelModel.Parameter.captureEffect);
CAPTURE_EFFECT_THRESHOLD = currentChannelModel.getParameterDoubleValue(ChannelModel.Parameter.captureEffectSignalTreshold);
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) {
super.registerRadioInterface(radio, sim);
/* Radio Medium changed here so notify Observers */
radioMediumObservable.setChangedAndNotify();
if (WITH_NOISE && radio instanceof NoiseSourceRadio) {
((NoiseSourceRadio)radio).addNoiseLevelListener(noiseListener);
}
@ -149,6 +151,9 @@ public class MRM extends AbstractRadioMedium {
public void unregisterRadioInterface(Radio radio, Simulation sim) {
super.unregisterRadioInterface(radio, sim);
/* Radio Medium changed here so notify Observers */
radioMediumObservable.setChangedAndNotify();
if (WITH_NOISE && radio instanceof NoiseSourceRadio) {
((NoiseSourceRadio)radio).removeNoiseLevelListener(noiseListener);
}
@ -402,25 +407,6 @@ public class MRM extends AbstractRadioMedium {
// -- 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.
*/
@ -449,13 +435,6 @@ public class MRM extends AbstractRadioMedium {
return currentChannelModel;
}
class SettingsObservable extends Observable {
private void notifySettingsChanged() {
setChanged();
notifyObservers();
}
}
class MRMRadioConnection extends RadioConnection {
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.SimInformation;
import org.contikios.cooja.util.ExecuteJAR;
import org.contikios.cooja.util.ScnObservable;
/**
* 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 class HighlightObservable extends Observable {
private void setChangedAndNotify(Mote mote) {
setChanged();
notifyObservers(mote);
}
}
private HighlightObservable moteHighlightObservable = new HighlightObservable();
private class MoteRelationsObservable extends Observable {
private void setChangedAndNotify() {
setChanged();
notifyObservers();
}
}
private MoteRelationsObservable moteRelationObservable = new MoteRelationsObservable();
private ScnObservable moteHighlightObservable = new ScnObservable();
private ScnObservable moteRelationObservable = new ScnObservable();
private JTextPane quickHelpTextPane;
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.
*
* @see #getLastConnection()
* @see #deleteRadioMediumObserver(Observer)
* @see #deleteRadioTransmissionObserver(Observer)
* @param observer New observer
*/
public abstract void addRadioMediumObserver(Observer observer);
public abstract void addRadioTransmissionObserver(Observer observer);
/**
* @return Radio medium observable
*/
public abstract Observable getRadioMediumObservable();
public abstract Observable getRadioTransmissionObservable();
/**
* Deletes an radio medium observer.
*
* @see #addRadioMediumObserver(Observer)
* @see #addRadioTransmissionObserver(Observer)
* @param observer
* Observer to delete
*/
public abstract void deleteRadioMediumObserver(Observer observer);
public abstract void deleteRadioTransmissionObserver(Observer observer);
/**
* @return Last radio connection finished in the radio medium

View file

@ -111,7 +111,7 @@ public class DGRMConfigurator extends VisPlugin {
radioMedium = (DirectedGraphMedium) sim.getRadioMedium();
/* Listen for graph updates */
radioMedium.addRadioMediumObserver(radioMediumObserver = new Observer() {
radioMedium.addRadioTransmissionObserver(radioMediumObserver = new Observer() {
public void update(Observable obs, Object obj) {
model.fireTableDataChanged();
}
@ -500,7 +500,7 @@ public class DGRMConfigurator extends VisPlugin {
};
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);
radioMediumCheckBox.putClientProperty("observable", mySimulation
.getRadioMedium().getRadioMediumObservable());
.getRadioMedium().getRadioTransmissionObservable());
radioMediumCheckBox.addActionListener(generalCheckBoxListener);
generalPanel.add(radioMediumCheckBox);

View file

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

View file

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

View file

@ -49,8 +49,10 @@ import org.contikios.cooja.Simulation;
import org.contikios.cooja.TimeEvent;
import org.contikios.cooja.interfaces.CustomDataRadio;
import org.contikios.cooja.interfaces.Radio;
import org.contikios.cooja.util.ScnObservable;
import org.jdom.Element;
/**
* Abstract radio medium provides basic functionality for implementing radio
* mediums.
@ -90,17 +92,13 @@ public abstract class AbstractRadioMedium extends RadioMedium {
public int COUNTER_RX = 0;
public int COUNTER_INTERFERED = 0;
public class RadioMediumObservable extends Observable {
public void setRadioMediumChanged() {
setChanged();
}
public void setRadioMediumChangedAndNotify() {
setChanged();
notifyObservers();
}
}
private RadioMediumObservable radioMediumObservable = new RadioMediumObservable();
/**
* Two Observables to observe the radioMedium and radioTransmissions
* @see addRadioTransmissionObserver
* @see addRadioMediumObserver
*/
protected ScnObservable radioMediumObservable = new ScnObservable();
protected ScnObservable radioTransmissionObservable = new ScnObservable();
/**
* This constructor should always be called from implemented radio mediums.
@ -288,7 +286,7 @@ public abstract class AbstractRadioMedium extends RadioMedium {
/* Notify observers */
lastConnection = null;
radioMediumObservable.setRadioMediumChangedAndNotify();
radioTransmissionObservable.setChangedAndNotify();
}
break;
case TRANSMISSION_FINISHED: {
@ -333,7 +331,7 @@ public abstract class AbstractRadioMedium extends RadioMedium {
updateSignalStrengths();
/* Notify observers */
radioMediumObservable.setRadioMediumChangedAndNotify();
radioTransmissionObservable.setChangedAndNotify();
}
break;
case CUSTOM_DATA_TRANSMITTED: {
@ -348,7 +346,7 @@ public abstract class AbstractRadioMedium extends RadioMedium {
/* Custom data object */
Object data = ((CustomDataRadio) radio).getLastCustomDataTransmitted();
if (data == null) {
logger.fatal("No custom data object to forward");
logger.fatal("No custom data objecTransmissiont to forward");
return;
}
@ -446,6 +444,7 @@ public abstract class AbstractRadioMedium extends RadioMedium {
registeredRadios.add(radio);
radio.addObserver(radioEventsObserver);
radioMediumObservable.setChangedAndNotify();
/* Update signal strengths */
updateSignalStrengths();
@ -462,6 +461,8 @@ public abstract class AbstractRadioMedium extends RadioMedium {
removeFromActiveConnections(radio);
radioMediumObservable.setChangedAndNotify();
/* Update signal strengths */
updateSignalStrengths();
}
@ -528,10 +529,40 @@ public abstract class AbstractRadioMedium extends RadioMedium {
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) {
radioMediumObservable.addObserver(observer);
}
/**
* @return the radioMediumObservable
*/
public Observable getRadioMediumObservable() {
return radioMediumObservable;
}

View file

@ -93,8 +93,7 @@ public class DirectedGraphMedium extends AbstractRadioMedium {
edges.add(e);
requestEdgeAnalysis();
((AbstractRadioMedium.RadioMediumObservable)
this.getRadioMediumObservable()).setRadioMediumChangedAndNotify();
radioTransmissionObservable.setChangedAndNotify();
}
public void removeEdge(Edge edge) {
@ -105,16 +104,15 @@ public class DirectedGraphMedium extends AbstractRadioMedium {
edges.remove(edge);
requestEdgeAnalysis();
((AbstractRadioMedium.RadioMediumObservable)
this.getRadioMediumObservable()).setRadioMediumChangedAndNotify();
radioTransmissionObservable.setChangedAndNotify();
}
public void clearEdges() {
edges.clear();
requestEdgeAnalysis();
((AbstractRadioMedium.RadioMediumObservable)
this.getRadioMediumObservable()).setRadioMediumChangedAndNotify();
radioTransmissionObservable.setChangedAndNotify();
}
public Edge[] getEdges() {
@ -224,6 +222,9 @@ public class DirectedGraphMedium extends AbstractRadioMedium {
this.edgesTable = arrTable;
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);
}
}