Cooja: Refactored AbstractRadioMedium:update(): if -> switch

This commit is contained in:
Moritz 'Morty' Strübe 2012-10-30 11:42:03 +01:00
parent f34826f3f1
commit 3c9e3e1b95

View file

@ -224,48 +224,47 @@ public abstract class AbstractRadioMedium extends RadioMedium {
Radio radio = (Radio) obs; Radio radio = (Radio) obs;
final Radio.RadioEvent event = radio.getLastEvent(); final Radio.RadioEvent event = radio.getLastEvent();
if (event == Radio.RadioEvent.RECEPTION_STARTED ||
event == Radio.RadioEvent.RECEPTION_INTERFERED || switch (event) {
event == Radio.RadioEvent.RECEPTION_FINISHED || case RECEPTION_STARTED:
event == Radio.RadioEvent.UNKNOWN) { case RECEPTION_INTERFERED:
/* Ignored */ case RECEPTION_FINISHED:
case UNKNOWN:
return; return;
} case HW_ON: {
if (event == Radio.RadioEvent.HW_ON) {
/* Update signal strengths */ /* Update signal strengths */
updateSignalStrengths(); updateSignalStrengths();
}
} else if (event == Radio.RadioEvent.HW_OFF) { break;
case HW_OFF: {
/* Remove any radio connections from this radio */ /* Remove any radio connections from this radio */
removeFromActiveConnections(radio); removeFromActiveConnections(radio);
/* Update signal strengths */ /* Update signal strengths */
updateSignalStrengths(); updateSignalStrengths();
}
} else if (event == Radio.RadioEvent.TRANSMISSION_STARTED) { break;
case TRANSMISSION_STARTED: {
/* Create new radio connection */ /* Create new radio connection */
if (radio.isReceiving()) { if (radio.isReceiving()) {
/* Radio starts transmitting when it should be receiving! /*
* Ok, but it won't receive the packet */ * Radio starts transmitting when it should be
* receiving! Ok, but it won't receive the packet
*/
radio.interfereAnyReception();
for (RadioConnection conn : activeConnections) { for (RadioConnection conn : activeConnections) {
if (conn.isDestination(radio)) { if (conn.isDestination(radio)) {
conn.addInterfered(radio); conn.addInterfered(radio);
} }
} }
radio.interfereAnyReception();
} }
RadioConnection newConnection = createConnections(radio); RadioConnection newConnection = createConnections(radio);
activeConnections.add(newConnection); activeConnections.add(newConnection);
for (Radio r : newConnection.getAllDestinations()) { for (Radio r : newConnection.getAllDestinations()) {
if (newConnection.getDestinationDelay(r) == 0) { if (newConnection.getDestinationDelay(r) == 0) {
r.signalReceptionStart(); r.signalReceptionStart();
} else { } else {
/* EXPERIMENTAL: Simulating propagation delay */ /* EXPERIMENTAL: Simulating propagation delay */
final Radio delayedRadio = r; final Radio delayedRadio = r;
TimeEvent delayedEvent = new TimeEvent(0) { TimeEvent delayedEvent = new TimeEvent(0) {
@ -273,21 +272,18 @@ public abstract class AbstractRadioMedium extends RadioMedium {
delayedRadio.signalReceptionStart(); delayedRadio.signalReceptionStart();
} }
}; };
simulation.scheduleEvent( simulation.scheduleEvent(delayedEvent, simulation.getSimulationTime() + newConnection.getDestinationDelay(r));
delayedEvent,
simulation.getSimulationTime() + newConnection.getDestinationDelay(r));
} }
} } /* Update signal strengths */
/* Update signal strengths */
updateSignalStrengths(); updateSignalStrengths();
/* Notify observers */ /* Notify observers */
lastConnection = null; lastConnection = null;
radioMediumObservable.setRadioMediumChangedAndNotify(); radioMediumObservable.setRadioMediumChangedAndNotify();
}
} else if (event == Radio.RadioEvent.TRANSMISSION_FINISHED) { break;
case TRANSMISSION_FINISHED: {
/* Remove radio connection */ /* Remove radio connection */
/* Connection */ /* Connection */
@ -312,8 +308,7 @@ public abstract class AbstractRadioMedium extends RadioMedium {
delayedRadio.signalReceptionEnd(); delayedRadio.signalReceptionEnd();
} }
}; };
simulation.scheduleEvent( simulation.scheduleEvent(delayedEvent,
delayedEvent,
simulation.getSimulationTime() + connection.getDestinationDelay(dstRadio)); simulation.getSimulationTime() + connection.getDestinationDelay(dstRadio));
} }
} }
@ -328,8 +323,9 @@ public abstract class AbstractRadioMedium extends RadioMedium {
/* Notify observers */ /* Notify observers */
radioMediumObservable.setRadioMediumChangedAndNotify(); radioMediumObservable.setRadioMediumChangedAndNotify();
}
} else if (event == Radio.RadioEvent.CUSTOM_DATA_TRANSMITTED) { break;
case CUSTOM_DATA_TRANSMITTED: {
/* Connection */ /* Connection */
RadioConnection connection = getActiveConnectionFrom(radio); RadioConnection connection = getActiveConnectionFrom(radio);
@ -347,8 +343,7 @@ public abstract class AbstractRadioMedium extends RadioMedium {
for (Radio dstRadio : connection.getAllDestinations()) { for (Radio dstRadio : connection.getAllDestinations()) {
if (!radio.getClass().equals(dstRadio.getClass()) || if (!radio.getClass().equals(dstRadio.getClass()) || !(radio instanceof CustomDataRadio)) {
!(radio instanceof CustomDataRadio)) {
/* Radios communicate via radio packets */ /* Radios communicate via radio packets */
continue; continue;
} }
@ -365,15 +360,15 @@ public abstract class AbstractRadioMedium extends RadioMedium {
delayedRadio.receiveCustomData(delayedData); delayedRadio.receiveCustomData(delayedData);
} }
}; };
simulation.scheduleEvent( simulation.scheduleEvent(delayedEvent,
delayedEvent,
simulation.getSimulationTime() + connection.getDestinationDelay(dstRadio)); simulation.getSimulationTime() + connection.getDestinationDelay(dstRadio));
} }
} }
} else if (event == Radio.RadioEvent.PACKET_TRANSMITTED) { }
break;
case PACKET_TRANSMITTED: {
/* Connection */ /* Connection */
RadioConnection connection = getActiveConnectionFrom(radio); RadioConnection connection = getActiveConnectionFrom(radio);
if (connection == null) { if (connection == null) {
@ -390,8 +385,7 @@ public abstract class AbstractRadioMedium extends RadioMedium {
for (Radio dstRadio : connection.getAllDestinations()) { for (Radio dstRadio : connection.getAllDestinations()) {
if (radio.getClass().equals(dstRadio.getClass()) && if (radio.getClass().equals(dstRadio.getClass()) && radio instanceof CustomDataRadio) {
radio instanceof CustomDataRadio) {
/* Radios instead communicate via custom data objects */ /* Radios instead communicate via custom data objects */
continue; continue;
} }
@ -409,14 +403,14 @@ public abstract class AbstractRadioMedium extends RadioMedium {
delayedRadio.setReceivedPacket(delayedPacket); delayedRadio.setReceivedPacket(delayedPacket);
} }
}; };
simulation.scheduleEvent( simulation.scheduleEvent(delayedEvent,
delayedEvent,
simulation.getSimulationTime() + connection.getDestinationDelay(dstRadio)); simulation.getSimulationTime() + connection.getDestinationDelay(dstRadio));
} }
} }
}
} else { break;
default:
logger.fatal("Unsupported radio event: " + event); logger.fatal("Unsupported radio event: " + event);
} }
} }