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,200 +224,194 @@ 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 ||
event == Radio.RadioEvent.RECEPTION_FINISHED ||
event == Radio.RadioEvent.UNKNOWN) {
/* Ignored */
return;
}
if (event == Radio.RadioEvent.HW_ON) { switch (event) {
case RECEPTION_STARTED:
/* Update signal strengths */ case RECEPTION_INTERFERED:
updateSignalStrengths(); case RECEPTION_FINISHED:
case UNKNOWN:
} else if (event == Radio.RadioEvent.HW_OFF) { return;
case HW_ON: {
/* Remove any radio connections from this radio */ /* Update signal strengths */
removeFromActiveConnections(radio); updateSignalStrengths();
}
/* Update signal strengths */ break;
updateSignalStrengths(); case HW_OFF: {
/* Remove any radio connections from this radio */
} else if (event == Radio.RadioEvent.TRANSMISSION_STARTED) { removeFromActiveConnections(radio);
/* Create new radio connection */ /* Update signal strengths */
updateSignalStrengths();
if (radio.isReceiving()) { }
/* Radio starts transmitting when it should be receiving! break;
* Ok, but it won't receive the packet */ case TRANSMISSION_STARTED: {
for (RadioConnection conn : activeConnections) { /* Create new radio connection */
if (conn.isDestination(radio)) { if (radio.isReceiving()) {
conn.addInterfered(radio); /*
* Radio starts transmitting when it should be
* receiving! Ok, but it won't receive the packet
*/
radio.interfereAnyReception();
for (RadioConnection conn : activeConnections) {
if (conn.isDestination(radio)) {
conn.addInterfered(radio);
}
} }
} }
radio.interfereAnyReception();
}
RadioConnection newConnection = createConnections(radio);
activeConnections.add(newConnection);
for (Radio r: newConnection.getAllDestinations()) {
if (newConnection.getDestinationDelay(r) == 0) {
r.signalReceptionStart();
} else {
/* EXPERIMENTAL: Simulating propagation delay */
final Radio delayedRadio = r;
TimeEvent delayedEvent = new TimeEvent(0) {
public void execute(long t) {
delayedRadio.signalReceptionStart();
}
};
simulation.scheduleEvent(
delayedEvent,
simulation.getSimulationTime() + newConnection.getDestinationDelay(r));
}
}
/* Update signal strengths */
updateSignalStrengths();
/* Notify observers */
lastConnection = null;
radioMediumObservable.setRadioMediumChangedAndNotify();
} else if (event == Radio.RadioEvent.TRANSMISSION_FINISHED) {
/* Remove radio connection */
/* Connection */
RadioConnection connection = getActiveConnectionFrom(radio);
if (connection == null) {
logger.fatal("No radio connection found");
return;
}
activeConnections.remove(connection);
lastConnection = connection;
COUNTER_TX++;
for (Radio dstRadio : connection.getAllDestinations()) {
if (connection.getDestinationDelay(dstRadio) == 0) {
dstRadio.signalReceptionEnd();
} else {
/* EXPERIMENTAL: Simulating propagation delay */
final Radio delayedRadio = dstRadio;
TimeEvent delayedEvent = new TimeEvent(0) {
public void execute(long t) {
delayedRadio.signalReceptionEnd();
}
};
simulation.scheduleEvent(
delayedEvent,
simulation.getSimulationTime() + connection.getDestinationDelay(dstRadio));
}
}
COUNTER_RX += connection.getDestinations().length;
COUNTER_INTERFERED += connection.getInterfered().length;
for (Radio intRadio : connection.getInterferedNonDestinations()) {
intRadio.signalReceptionEnd();
}
/* Update signal strengths */
updateSignalStrengths();
/* Notify observers */
radioMediumObservable.setRadioMediumChangedAndNotify();
} else if (event == Radio.RadioEvent.CUSTOM_DATA_TRANSMITTED) {
/* Connection */
RadioConnection connection = getActiveConnectionFrom(radio);
if (connection == null) {
logger.fatal("No radio connection found");
return;
}
/* Custom data object */
Object data = ((CustomDataRadio) radio).getLastCustomDataTransmitted();
if (data == null) {
logger.fatal("No custom data object to forward");
return;
}
for (Radio dstRadio : connection.getAllDestinations()) {
if (!radio.getClass().equals(dstRadio.getClass()) || RadioConnection newConnection = createConnections(radio);
!(radio instanceof CustomDataRadio)) { activeConnections.add(newConnection);
/* Radios communicate via radio packets */
continue; for (Radio r : newConnection.getAllDestinations()) {
if (newConnection.getDestinationDelay(r) == 0) {
r.signalReceptionStart();
} else {
/* EXPERIMENTAL: Simulating propagation delay */
final Radio delayedRadio = r;
TimeEvent delayedEvent = new TimeEvent(0) {
public void execute(long t) {
delayedRadio.signalReceptionStart();
}
};
simulation.scheduleEvent(delayedEvent, simulation.getSimulationTime() + newConnection.getDestinationDelay(r));
}
} /* Update signal strengths */
updateSignalStrengths();
/* Notify observers */
lastConnection = null;
radioMediumObservable.setRadioMediumChangedAndNotify();
}
break;
case TRANSMISSION_FINISHED: {
/* Remove radio connection */
/* Connection */
RadioConnection connection = getActiveConnectionFrom(radio);
if (connection == null) {
logger.fatal("No radio connection found");
return;
} }
if (connection.getDestinationDelay(dstRadio) == 0) { activeConnections.remove(connection);
((CustomDataRadio) dstRadio).receiveCustomData(data); lastConnection = connection;
} else { COUNTER_TX++;
for (Radio dstRadio : connection.getAllDestinations()) {
/* EXPERIMENTAL: Simulating propagation delay */ if (connection.getDestinationDelay(dstRadio) == 0) {
final CustomDataRadio delayedRadio = (CustomDataRadio) dstRadio; dstRadio.signalReceptionEnd();
final Object delayedData = data; } else {
TimeEvent delayedEvent = new TimeEvent(0) {
public void execute(long t) { /* EXPERIMENTAL: Simulating propagation delay */
delayedRadio.receiveCustomData(delayedData); final Radio delayedRadio = dstRadio;
} TimeEvent delayedEvent = new TimeEvent(0) {
}; public void execute(long t) {
simulation.scheduleEvent( delayedRadio.signalReceptionEnd();
delayedEvent, }
simulation.getSimulationTime() + connection.getDestinationDelay(dstRadio)); };
simulation.scheduleEvent(delayedEvent,
simulation.getSimulationTime() + connection.getDestinationDelay(dstRadio));
}
} }
} COUNTER_RX += connection.getDestinations().length;
COUNTER_INTERFERED += connection.getInterfered().length;
} else if (event == Radio.RadioEvent.PACKET_TRANSMITTED) { for (Radio intRadio : connection.getInterferedNonDestinations()) {
intRadio.signalReceptionEnd();
/* Connection */
RadioConnection connection = getActiveConnectionFrom(radio);
if (connection == null) {
logger.fatal("No radio connection found");
return;
}
/* Radio packet */
RadioPacket packet = radio.getLastPacketTransmitted();
if (packet == null) {
logger.fatal("No radio packet to forward");
return;
}
for (Radio dstRadio : connection.getAllDestinations()) {
if (radio.getClass().equals(dstRadio.getClass()) &&
radio instanceof CustomDataRadio) {
/* Radios instead communicate via custom data objects */
continue;
} }
/* Forward radio packet */ /* Update signal strengths */
if (connection.getDestinationDelay(dstRadio) == 0) { updateSignalStrengths();
dstRadio.setReceivedPacket(packet);
} else { /* Notify observers */
radioMediumObservable.setRadioMediumChangedAndNotify();
}
break;
case CUSTOM_DATA_TRANSMITTED: {
/* Connection */
RadioConnection connection = getActiveConnectionFrom(radio);
if (connection == null) {
logger.fatal("No radio connection found");
return;
}
/* Custom data object */
Object data = ((CustomDataRadio) radio).getLastCustomDataTransmitted();
if (data == null) {
logger.fatal("No custom data object to forward");
return;
}
for (Radio dstRadio : connection.getAllDestinations()) {
/* EXPERIMENTAL: Simulating propagation delay */ if (!radio.getClass().equals(dstRadio.getClass()) || !(radio instanceof CustomDataRadio)) {
final Radio delayedRadio = dstRadio; /* Radios communicate via radio packets */
final RadioPacket delayedPacket = packet; continue;
TimeEvent delayedEvent = new TimeEvent(0) { }
public void execute(long t) {
delayedRadio.setReceivedPacket(delayedPacket); if (connection.getDestinationDelay(dstRadio) == 0) {
} ((CustomDataRadio) dstRadio).receiveCustomData(data);
}; } else {
simulation.scheduleEvent(
delayedEvent, /* EXPERIMENTAL: Simulating propagation delay */
simulation.getSimulationTime() + connection.getDestinationDelay(dstRadio)); final CustomDataRadio delayedRadio = (CustomDataRadio) dstRadio;
final Object delayedData = data;
TimeEvent delayedEvent = new TimeEvent(0) {
public void execute(long t) {
delayedRadio.receiveCustomData(delayedData);
}
};
simulation.scheduleEvent(delayedEvent,
simulation.getSimulationTime() + connection.getDestinationDelay(dstRadio));
}
} }
} }
break;
} else { case PACKET_TRANSMITTED: {
logger.fatal("Unsupported radio event: " + event); /* Connection */
RadioConnection connection = getActiveConnectionFrom(radio);
if (connection == null) {
logger.fatal("No radio connection found");
return;
}
/* Radio packet */
RadioPacket packet = radio.getLastPacketTransmitted();
if (packet == null) {
logger.fatal("No radio packet to forward");
return;
}
for (Radio dstRadio : connection.getAllDestinations()) {
if (radio.getClass().equals(dstRadio.getClass()) && radio instanceof CustomDataRadio) {
/* Radios instead communicate via custom data objects */
continue;
}
/* Forward radio packet */
if (connection.getDestinationDelay(dstRadio) == 0) {
dstRadio.setReceivedPacket(packet);
} else {
/* EXPERIMENTAL: Simulating propagation delay */
final Radio delayedRadio = dstRadio;
final RadioPacket delayedPacket = packet;
TimeEvent delayedEvent = new TimeEvent(0) {
public void execute(long t) {
delayedRadio.setReceivedPacket(delayedPacket);
}
};
simulation.scheduleEvent(delayedEvent,
simulation.getSimulationTime() + connection.getDestinationDelay(dstRadio));
}
}
}
break;
default:
logger.fatal("Unsupported radio event: " + event);
} }
} }
}; };
@ -473,5 +467,5 @@ public abstract class AbstractRadioMedium extends RadioMedium {
public RadioConnection getLastConnection() { public RadioConnection getLastConnection() {
return lastConnection; return lastConnection;
} }
} }