Cooja: Change indention of AbstractRadioMedium to tabs

This commit is contained in:
Moritz 'Morty' Strübe 2012-10-30 11:42:02 +01:00
parent 9b1fb12a3f
commit f34826f3f1

View file

@ -62,416 +62,416 @@ import se.sics.cooja.interfaces.Radio;
* @author Fredrik Osterlind * @author Fredrik Osterlind
*/ */
public abstract class AbstractRadioMedium extends RadioMedium { public abstract class AbstractRadioMedium extends RadioMedium {
private static Logger logger = Logger.getLogger(AbstractRadioMedium.class); private static Logger logger = Logger.getLogger(AbstractRadioMedium.class);
/* Signal strengths in dBm. /* Signal strengths in dBm.
* Approx. values measured on TmoteSky */ * Approx. values measured on TmoteSky */
public static final double SS_NOTHING = -100; public static final double SS_NOTHING = -100;
public static final double SS_STRONG = -10; public static final double SS_STRONG = -10;
public static final double SS_WEAK = -95; public static final double SS_WEAK = -95;
private ArrayList<Radio> registeredRadios = new ArrayList<Radio>(); private ArrayList<Radio> registeredRadios = new ArrayList<Radio>();
private ArrayList<RadioConnection> activeConnections = new ArrayList<RadioConnection>(); private ArrayList<RadioConnection> activeConnections = new ArrayList<RadioConnection>();
private RadioConnection lastConnection = null; private RadioConnection lastConnection = null;
private Simulation simulation = null; private Simulation simulation = null;
/* Book-keeping */ /* Book-keeping */
public int COUNTER_TX = 0; public int COUNTER_TX = 0;
public int COUNTER_RX = 0; public int COUNTER_RX = 0;
public int COUNTER_INTERFERED = 0; public int COUNTER_INTERFERED = 0;
public class RadioMediumObservable extends Observable { public class RadioMediumObservable extends Observable {
public void setRadioMediumChanged() { public void setRadioMediumChanged() {
setChanged(); setChanged();
} }
public void setRadioMediumChangedAndNotify() { public void setRadioMediumChangedAndNotify() {
setChanged(); setChanged();
notifyObservers(); notifyObservers();
} }
} }
private RadioMediumObservable radioMediumObservable = new RadioMediumObservable(); private RadioMediumObservable radioMediumObservable = new RadioMediumObservable();
/** /**
* This constructor should always be called from implemented radio mediums. * This constructor should always be called from implemented radio mediums.
* *
* @param simulation Simulation * @param simulation Simulation
*/ */
public AbstractRadioMedium(Simulation simulation) { public AbstractRadioMedium(Simulation simulation) {
this.simulation = simulation; this.simulation = simulation;
} }
/** /**
* @return All registered radios * @return All registered radios
*/ */
public Radio[] getRegisteredRadios() { public Radio[] getRegisteredRadios() {
return registeredRadios.toArray(new Radio[0]); return registeredRadios.toArray(new Radio[0]);
} }
/** /**
* @return All active connections * @return All active connections
*/ */
public RadioConnection[] getActiveConnections() { public RadioConnection[] getActiveConnections() {
/* NOTE: toArray([0]) creates array and handles synchronization */ /* NOTE: toArray([0]) creates array and handles synchronization */
return activeConnections.toArray(new RadioConnection[0]); return activeConnections.toArray(new RadioConnection[0]);
} }
/** /**
* Creates a new connection from given radio. * Creates a new connection from given radio.
* *
* Determines which radios should receive or be interfered by the transmission. * Determines which radios should receive or be interfered by the transmission.
* *
* @param radio Source radio * @param radio Source radio
* @return New connection * @return New connection
*/ */
abstract public RadioConnection createConnections(Radio radio); abstract public RadioConnection createConnections(Radio radio);
/** /**
* Updates all radio interfaces' signal strengths according to * Updates all radio interfaces' signal strengths according to
* the current active connections. * the current active connections.
*/ */
public void updateSignalStrengths() { public void updateSignalStrengths() {
/* Reset signal strengths */ /* Reset signal strengths */
for (Radio radio : getRegisteredRadios()) { for (Radio radio : getRegisteredRadios()) {
radio.setCurrentSignalStrength(SS_NOTHING); radio.setCurrentSignalStrength(SS_NOTHING);
} }
/* Set signal strength to strong on destinations */ /* Set signal strength to strong on destinations */
RadioConnection[] conns = getActiveConnections(); RadioConnection[] conns = getActiveConnections();
for (RadioConnection conn : conns) { for (RadioConnection conn : conns) {
if (conn.getSource().getCurrentSignalStrength() < SS_STRONG) { if (conn.getSource().getCurrentSignalStrength() < SS_STRONG) {
conn.getSource().setCurrentSignalStrength(SS_STRONG); conn.getSource().setCurrentSignalStrength(SS_STRONG);
} }
for (Radio dstRadio : conn.getDestinations()) { for (Radio dstRadio : conn.getDestinations()) {
if (conn.getSource().getChannel() >= 0 && if (conn.getSource().getChannel() >= 0 &&
dstRadio.getChannel() >= 0 && dstRadio.getChannel() >= 0 &&
conn.getSource().getChannel() != dstRadio.getChannel()) { conn.getSource().getChannel() != dstRadio.getChannel()) {
continue; continue;
} }
if (dstRadio.getCurrentSignalStrength() < SS_STRONG) { if (dstRadio.getCurrentSignalStrength() < SS_STRONG) {
dstRadio.setCurrentSignalStrength(SS_STRONG); dstRadio.setCurrentSignalStrength(SS_STRONG);
} }
} }
} }
/* Set signal strength to weak on interfered */ /* Set signal strength to weak on interfered */
for (RadioConnection conn : conns) { for (RadioConnection conn : conns) {
for (Radio intfRadio : conn.getInterfered()) { for (Radio intfRadio : conn.getInterfered()) {
if (intfRadio.getCurrentSignalStrength() < SS_STRONG) { if (intfRadio.getCurrentSignalStrength() < SS_STRONG) {
intfRadio.setCurrentSignalStrength(SS_STRONG); intfRadio.setCurrentSignalStrength(SS_STRONG);
} }
if (conn.getSource().getChannel() >= 0 && if (conn.getSource().getChannel() >= 0 &&
intfRadio.getChannel() >= 0 && intfRadio.getChannel() >= 0 &&
conn.getSource().getChannel() != intfRadio.getChannel()) { conn.getSource().getChannel() != intfRadio.getChannel()) {
continue; continue;
} }
if (!intfRadio.isInterfered()) { if (!intfRadio.isInterfered()) {
/*logger.warn("Radio was not interfered");*/ /*logger.warn("Radio was not interfered");*/
intfRadio.interfereAnyReception(); intfRadio.interfereAnyReception();
} }
} }
} }
} }
/** /**
* Remove given radio from any active connections. * Remove given radio from any active connections.
* This method can be called if a radio node falls asleep or is removed. * This method can be called if a radio node falls asleep or is removed.
* *
* @param radio Radio * @param radio Radio
*/ */
private void removeFromActiveConnections(Radio radio) { private void removeFromActiveConnections(Radio radio) {
/* This radio must not be a connection source */ /* This radio must not be a connection source */
RadioConnection connection = getActiveConnectionFrom(radio); RadioConnection connection = getActiveConnectionFrom(radio);
if (connection != null) { if (connection != null) {
logger.fatal("Connection source turned off radio: " + radio); logger.fatal("Connection source turned off radio: " + radio);
} }
/* Set interfered if currently a connection destination */ /* Set interfered if currently a connection destination */
for (RadioConnection conn : activeConnections) { for (RadioConnection conn : activeConnections) {
if (conn.isDestination(radio)) { if (conn.isDestination(radio)) {
conn.addInterfered(radio); conn.addInterfered(radio);
if (!radio.isInterfered()) { if (!radio.isInterfered()) {
radio.interfereAnyReception(); radio.interfereAnyReception();
} }
} }
} }
} }
private RadioConnection getActiveConnectionFrom(Radio source) { private RadioConnection getActiveConnectionFrom(Radio source) {
for (RadioConnection conn : activeConnections) { for (RadioConnection conn : activeConnections) {
if (conn.getSource() == source) { if (conn.getSource() == source) {
return conn; return conn;
} }
} }
return null; return null;
} }
/** /**
* This observer is responsible for detecting radio interface events, for example * This observer is responsible for detecting radio interface events, for example
* new transmissions. * new transmissions.
*/ */
private Observer radioEventsObserver = new Observer() { private Observer radioEventsObserver = new Observer() {
public void update(Observable obs, Object obj) { public void update(Observable obs, Object obj) {
if (!(obs instanceof Radio)) { if (!(obs instanceof Radio)) {
logger.fatal("Radio event dispatched by non-radio object"); logger.fatal("Radio event dispatched by non-radio object");
return; return;
} }
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 || if (event == Radio.RadioEvent.RECEPTION_STARTED ||
event == Radio.RadioEvent.RECEPTION_INTERFERED || event == Radio.RadioEvent.RECEPTION_INTERFERED ||
event == Radio.RadioEvent.RECEPTION_FINISHED || event == Radio.RadioEvent.RECEPTION_FINISHED ||
event == Radio.RadioEvent.UNKNOWN) { event == Radio.RadioEvent.UNKNOWN) {
/* Ignored */ /* Ignored */
return; return;
} }
if (event == Radio.RadioEvent.HW_ON) { if (event == Radio.RadioEvent.HW_ON) {
/* Update signal strengths */ /* Update signal strengths */
updateSignalStrengths(); updateSignalStrengths();
} else if (event == Radio.RadioEvent.HW_OFF) { } else if (event == Radio.RadioEvent.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) { } else if (event == Radio.RadioEvent.TRANSMISSION_STARTED) {
/* Create new radio connection */ /* Create new radio connection */
if (radio.isReceiving()) { if (radio.isReceiving()) {
/* Radio starts transmitting when it should be receiving! /* Radio starts transmitting when it should be receiving!
* Ok, but it won't receive the packet */ * Ok, but it won't receive the packet */
for (RadioConnection conn : activeConnections) { for (RadioConnection conn : activeConnections) {
if (conn.isDestination(radio)) { if (conn.isDestination(radio)) {
conn.addInterfered(radio); conn.addInterfered(radio);
} }
} }
radio.interfereAnyReception(); 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) {
public void execute(long t) { public void execute(long t) {
delayedRadio.signalReceptionStart(); delayedRadio.signalReceptionStart();
} }
}; };
simulation.scheduleEvent( simulation.scheduleEvent(
delayedEvent, delayedEvent,
simulation.getSimulationTime() + newConnection.getDestinationDelay(r)); 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) { } else if (event == Radio.RadioEvent.TRANSMISSION_FINISHED) {
/* Remove radio connection */ /* Remove radio connection */
/* Connection */ /* Connection */
RadioConnection connection = getActiveConnectionFrom(radio); RadioConnection connection = getActiveConnectionFrom(radio);
if (connection == null) { if (connection == null) {
logger.fatal("No radio connection found"); logger.fatal("No radio connection found");
return; return;
} }
activeConnections.remove(connection); activeConnections.remove(connection);
lastConnection = connection; lastConnection = connection;
COUNTER_TX++; COUNTER_TX++;
for (Radio dstRadio : connection.getAllDestinations()) { for (Radio dstRadio : connection.getAllDestinations()) {
if (connection.getDestinationDelay(dstRadio) == 0) { if (connection.getDestinationDelay(dstRadio) == 0) {
dstRadio.signalReceptionEnd(); dstRadio.signalReceptionEnd();
} else { } else {
/* EXPERIMENTAL: Simulating propagation delay */ /* EXPERIMENTAL: Simulating propagation delay */
final Radio delayedRadio = dstRadio; final Radio delayedRadio = dstRadio;
TimeEvent delayedEvent = new TimeEvent(0) { TimeEvent delayedEvent = new TimeEvent(0) {
public void execute(long t) { public void execute(long t) {
delayedRadio.signalReceptionEnd(); delayedRadio.signalReceptionEnd();
} }
}; };
simulation.scheduleEvent( simulation.scheduleEvent(
delayedEvent, delayedEvent,
simulation.getSimulationTime() + connection.getDestinationDelay(dstRadio)); simulation.getSimulationTime() + connection.getDestinationDelay(dstRadio));
} }
} }
COUNTER_RX += connection.getDestinations().length; COUNTER_RX += connection.getDestinations().length;
COUNTER_INTERFERED += connection.getInterfered().length; COUNTER_INTERFERED += connection.getInterfered().length;
for (Radio intRadio : connection.getInterferedNonDestinations()) { for (Radio intRadio : connection.getInterferedNonDestinations()) {
intRadio.signalReceptionEnd(); intRadio.signalReceptionEnd();
} }
/* Update signal strengths */ /* Update signal strengths */
updateSignalStrengths(); updateSignalStrengths();
/* Notify observers */ /* Notify observers */
radioMediumObservable.setRadioMediumChangedAndNotify(); radioMediumObservable.setRadioMediumChangedAndNotify();
} else if (event == Radio.RadioEvent.CUSTOM_DATA_TRANSMITTED) { } else if (event == Radio.RadioEvent.CUSTOM_DATA_TRANSMITTED) {
/* Connection */ /* Connection */
RadioConnection connection = getActiveConnectionFrom(radio); RadioConnection connection = getActiveConnectionFrom(radio);
if (connection == null) { if (connection == null) {
logger.fatal("No radio connection found"); logger.fatal("No radio connection found");
return; return;
} }
/* 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 object to forward");
return; return;
} }
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;
} }
if (connection.getDestinationDelay(dstRadio) == 0) { if (connection.getDestinationDelay(dstRadio) == 0) {
((CustomDataRadio) dstRadio).receiveCustomData(data); ((CustomDataRadio) dstRadio).receiveCustomData(data);
} else { } else {
/* EXPERIMENTAL: Simulating propagation delay */ /* EXPERIMENTAL: Simulating propagation delay */
final CustomDataRadio delayedRadio = (CustomDataRadio) dstRadio; final CustomDataRadio delayedRadio = (CustomDataRadio) dstRadio;
final Object delayedData = data; final Object delayedData = data;
TimeEvent delayedEvent = new TimeEvent(0) { TimeEvent delayedEvent = new TimeEvent(0) {
public void execute(long t) { public void execute(long t) {
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) { } else if (event == Radio.RadioEvent.PACKET_TRANSMITTED) {
/* Connection */ /* Connection */
RadioConnection connection = getActiveConnectionFrom(radio); RadioConnection connection = getActiveConnectionFrom(radio);
if (connection == null) { if (connection == null) {
logger.fatal("No radio connection found"); logger.fatal("No radio connection found");
return; return;
} }
/* Radio packet */ /* Radio packet */
RadioPacket packet = radio.getLastPacketTransmitted(); RadioPacket packet = radio.getLastPacketTransmitted();
if (packet == null) { if (packet == null) {
logger.fatal("No radio packet to forward"); logger.fatal("No radio packet to forward");
return; return;
} }
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;
} }
/* Forward radio packet */ /* Forward radio packet */
if (connection.getDestinationDelay(dstRadio) == 0) { if (connection.getDestinationDelay(dstRadio) == 0) {
dstRadio.setReceivedPacket(packet); dstRadio.setReceivedPacket(packet);
} else { } else {
/* EXPERIMENTAL: Simulating propagation delay */ /* EXPERIMENTAL: Simulating propagation delay */
final Radio delayedRadio = dstRadio; final Radio delayedRadio = dstRadio;
final RadioPacket delayedPacket = packet; final RadioPacket delayedPacket = packet;
TimeEvent delayedEvent = new TimeEvent(0) { TimeEvent delayedEvent = new TimeEvent(0) {
public void execute(long t) { public void execute(long t) {
delayedRadio.setReceivedPacket(delayedPacket); delayedRadio.setReceivedPacket(delayedPacket);
} }
}; };
simulation.scheduleEvent( simulation.scheduleEvent(
delayedEvent, delayedEvent,
simulation.getSimulationTime() + connection.getDestinationDelay(dstRadio)); simulation.getSimulationTime() + connection.getDestinationDelay(dstRadio));
} }
} }
} else { } else {
logger.fatal("Unsupported radio event: " + event); logger.fatal("Unsupported radio event: " + event);
} }
} }
}; };
public void registerMote(Mote mote, Simulation sim) { public void registerMote(Mote mote, Simulation sim) {
registerRadioInterface(mote.getInterfaces().getRadio(), sim); registerRadioInterface(mote.getInterfaces().getRadio(), sim);
} }
public void unregisterMote(Mote mote, Simulation sim) { public void unregisterMote(Mote mote, Simulation sim) {
unregisterRadioInterface(mote.getInterfaces().getRadio(), sim); unregisterRadioInterface(mote.getInterfaces().getRadio(), sim);
} }
public void registerRadioInterface(Radio radio, Simulation sim) { public void registerRadioInterface(Radio radio, Simulation sim) {
if (radio == null) { if (radio == null) {
logger.warn("No radio to register"); logger.warn("No radio to register");
return; return;
} }
registeredRadios.add(radio); registeredRadios.add(radio);
radio.addObserver(radioEventsObserver); radio.addObserver(radioEventsObserver);
/* Update signal strengths */ /* Update signal strengths */
updateSignalStrengths(); updateSignalStrengths();
} }
public void unregisterRadioInterface(Radio radio, Simulation sim) { public void unregisterRadioInterface(Radio radio, Simulation sim) {
if (!registeredRadios.contains(radio)) { if (!registeredRadios.contains(radio)) {
logger.warn("No radio to unregister: " + radio); logger.warn("No radio to unregister: " + radio);
return; return;
} }
radio.deleteObserver(radioEventsObserver); radio.deleteObserver(radioEventsObserver);
registeredRadios.remove(radio); registeredRadios.remove(radio);
removeFromActiveConnections(radio); removeFromActiveConnections(radio);
/* Update signal strengths */ /* Update signal strengths */
updateSignalStrengths(); updateSignalStrengths();
} }
public void addRadioMediumObserver(Observer observer) { public void addRadioMediumObserver(Observer observer) {
radioMediumObservable.addObserver(observer); radioMediumObservable.addObserver(observer);
} }
public Observable getRadioMediumObservable() { public Observable getRadioMediumObservable() {
return radioMediumObservable; return radioMediumObservable;
} }
public void deleteRadioMediumObserver(Observer observer) { public void deleteRadioMediumObserver(Observer observer) {
radioMediumObservable.deleteObserver(observer); radioMediumObservable.deleteObserver(observer);
} }
public RadioConnection getLastConnection() { public RadioConnection getLastConnection() {
return lastConnection; return lastConnection;
} }
} }