Refactored RadioMediumObservable and added new Observable

Refactored RadioMediumObservable to RadioTransmissionObservable because of its function
Added correct RadioMediumObservable and updating in AbstractRadioMedium, DirectedGraphMedium and MRM
Added some documentation
This commit is contained in:
Sebastian Schinabeck 2013-11-21 18:08:41 +01:00 committed by Moritz 'Morty' Strübe
parent 42077adbb1
commit c1a275f0b2
9 changed files with 69 additions and 20 deletions

View file

@ -230,7 +230,7 @@ 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.addSettingsObserver(radioMediumSettingsObserver);
currentRadioMedium.addRadioMediumObserver(radioMediumActivityObserver); currentRadioMedium.addRadioTransmissionObserver(radioMediumActivityObserver);
// Set initial size etc. // Set initial size etc.
setSize(500, 500); setSize(500, 500);
@ -2344,7 +2344,7 @@ public class AreaViewer extends VisPlugin {
} }
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

@ -143,6 +143,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);
} }
@ -150,6 +153,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);
} }

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

@ -92,7 +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;
/**
* Two Observables to observe the radioMedium and radioTransmissions
* @see addRadioTransmissionObserver
* @see addRadioMediumObserver
*/
protected ScnObservable radioMediumObservable = new ScnObservable(); protected ScnObservable radioMediumObservable = new ScnObservable();
protected ScnObservable radioTransmissionObservable = new ScnObservable();
/** /**
* This constructor should always be called from implemented radio mediums. * This constructor should always be called from implemented radio mediums.
@ -280,7 +286,7 @@ public abstract class AbstractRadioMedium extends RadioMedium {
/* Notify observers */ /* Notify observers */
lastConnection = null; lastConnection = null;
radioMediumObservable.setChangedAndNotify(); radioTransmissionObservable.setChangedAndNotify();
} }
break; break;
case TRANSMISSION_FINISHED: { case TRANSMISSION_FINISHED: {
@ -325,7 +331,7 @@ public abstract class AbstractRadioMedium extends RadioMedium {
updateSignalStrengths(); updateSignalStrengths();
/* Notify observers */ /* Notify observers */
radioMediumObservable.setChangedAndNotify(); radioTransmissionObservable.setChangedAndNotify();
} }
break; break;
case CUSTOM_DATA_TRANSMITTED: { case CUSTOM_DATA_TRANSMITTED: {
@ -340,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;
} }
@ -438,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();
@ -454,6 +461,8 @@ public abstract class AbstractRadioMedium extends RadioMedium {
removeFromActiveConnections(radio); removeFromActiveConnections(radio);
radioMediumObservable.setChangedAndNotify();
/* Update signal strengths */ /* Update signal strengths */
updateSignalStrengths(); updateSignalStrengths();
} }
@ -520,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,7 +93,7 @@ public class DirectedGraphMedium extends AbstractRadioMedium {
edges.add(e); edges.add(e);
requestEdgeAnalysis(); requestEdgeAnalysis();
radioMediumObservable.setChangedAndNotify(); radioTransmissionObservable.setChangedAndNotify();
} }
public void removeEdge(Edge edge) { public void removeEdge(Edge edge) {
@ -104,14 +104,15 @@ public class DirectedGraphMedium extends AbstractRadioMedium {
edges.remove(edge); edges.remove(edge);
requestEdgeAnalysis(); requestEdgeAnalysis();
radioMediumObservable.setChangedAndNotify();
radioTransmissionObservable.setChangedAndNotify();
} }
public void clearEdges() { public void clearEdges() {
edges.clear(); edges.clear();
requestEdgeAnalysis(); requestEdgeAnalysis();
radioMediumObservable.setChangedAndNotify(); radioTransmissionObservable.setChangedAndNotify();
} }
public Edge[] getEdges() { public Edge[] getEdges() {
@ -221,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();
} }
/** /**