Cooja: Refactored AbstractRadioMedium:update(): if -> switch
This commit is contained in:
parent
f34826f3f1
commit
3c9e3e1b95
|
@ -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();
|
||||||
}
|
}
|
||||||
|
break;
|
||||||
|
case TRANSMISSION_FINISHED: {
|
||||||
|
/* Remove radio connection */
|
||||||
|
|
||||||
RadioConnection newConnection = createConnections(radio);
|
/* Connection */
|
||||||
activeConnections.add(newConnection);
|
RadioConnection connection = getActiveConnectionFrom(radio);
|
||||||
for (Radio r: newConnection.getAllDestinations()) {
|
if (connection == null) {
|
||||||
if (newConnection.getDestinationDelay(r) == 0) {
|
logger.fatal("No radio connection found");
|
||||||
r.signalReceptionStart();
|
return;
|
||||||
} 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()) ||
|
|
||||||
!(radio instanceof CustomDataRadio)) {
|
|
||||||
/* Radios communicate via radio packets */
|
|
||||||
continue;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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) {
|
|
||||||
delayedRadio.receiveCustomData(delayedData);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
simulation.scheduleEvent(
|
|
||||||
delayedEvent,
|
|
||||||
simulation.getSimulationTime() + connection.getDestinationDelay(dstRadio));
|
|
||||||
|
|
||||||
|
/* 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;
|
||||||
} 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 {
|
|
||||||
|
|
||||||
/* EXPERIMENTAL: Simulating propagation delay */
|
/* Notify observers */
|
||||||
final Radio delayedRadio = dstRadio;
|
radioMediumObservable.setRadioMediumChangedAndNotify();
|
||||||
final RadioPacket delayedPacket = packet;
|
}
|
||||||
TimeEvent delayedEvent = new TimeEvent(0) {
|
break;
|
||||||
public void execute(long t) {
|
case CUSTOM_DATA_TRANSMITTED: {
|
||||||
delayedRadio.setReceivedPacket(delayedPacket);
|
|
||||||
}
|
/* Connection */
|
||||||
};
|
RadioConnection connection = getActiveConnectionFrom(radio);
|
||||||
simulation.scheduleEvent(
|
if (connection == null) {
|
||||||
delayedEvent,
|
logger.fatal("No radio connection found");
|
||||||
simulation.getSimulationTime() + connection.getDestinationDelay(dstRadio));
|
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()) || !(radio instanceof CustomDataRadio)) {
|
||||||
|
/* Radios communicate via radio packets */
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (connection.getDestinationDelay(dstRadio) == 0) {
|
||||||
|
((CustomDataRadio) dstRadio).receiveCustomData(data);
|
||||||
|
} else {
|
||||||
|
|
||||||
|
/* EXPERIMENTAL: Simulating propagation delay */
|
||||||
|
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;
|
||||||
|
case PACKET_TRANSMITTED: {
|
||||||
|
/* Connection */
|
||||||
|
RadioConnection connection = getActiveConnectionFrom(radio);
|
||||||
|
if (connection == null) {
|
||||||
|
logger.fatal("No radio connection found");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
} else {
|
/* Radio packet */
|
||||||
logger.fatal("Unsupported radio event: " + event);
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in a new issue