setting receiving signal strengths when transmission starts instead of at end of tick + updating signal strengths each tick

This commit is contained in:
fros4943 2006-10-09 14:15:29 +00:00
parent 5c4f07dcf0
commit 788b6c75c0

View file

@ -26,7 +26,7 @@
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE. * SUCH DAMAGE.
* *
* $Id: StandardRadioMedium.java,v 1.6 2006/10/06 13:32:45 fros4943 Exp $ * $Id: StandardRadioMedium.java,v 1.7 2006/10/09 14:15:29 fros4943 Exp $
*/ */
package se.sics.cooja.radiomediums; package se.sics.cooja.radiomediums;
@ -67,14 +67,19 @@ public class StandardRadioMedium extends RadioMedium {
private static Logger logger = Logger.getLogger(StandardRadioMedium.class); private static Logger logger = Logger.getLogger(StandardRadioMedium.class);
private Vector<Position> registeredPositions = new Vector<Position>(); private Vector<Position> registeredPositions = new Vector<Position>();
private Vector<Radio> registeredRadios = new Vector<Radio>(); private Vector<Radio> registeredRadios = new Vector<Radio>();
private Vector<Position> transmissionStartedPositions = new Vector<Position>();
private Vector<Radio> transmissionStartedRadios = new Vector<Radio>();
private Vector<Radio> transmissionEndedRadios = new Vector<Radio>(); private Vector<Radio> transmissionEndedRadios = new Vector<Radio>();
private static RadioMedium myRadioMedium; private static RadioMedium myRadioMedium;
public static final double SS_NOTHING = -200;
public static final double SS_BAD = -110;
public static final double SS_OK = -100;
/** /**
* Visualizes radio traffic in the current radio medium. Allows a user to * Visualizes radio traffic in the current radio medium. Allows a user to
* change transmission ranges. * change transmission ranges.
@ -91,6 +96,7 @@ public class StandardRadioMedium extends RadioMedium {
private Mote selectedMote = null; private Mote selectedMote = null;
private JSpinner transmissionSpinner = null; private JSpinner transmissionSpinner = null;
private JSpinner interferenceSpinner = null; private JSpinner interferenceSpinner = null;
private Observer radioMediumObserver; private Observer radioMediumObserver;
@ -99,9 +105,11 @@ public class StandardRadioMedium extends RadioMedium {
public boolean isEnabled(Mote mote) { public boolean isEnabled(Mote mote) {
return true; return true;
} }
public String getDescription(Mote mote) { public String getDescription(Mote mote) {
return "Change transmission ranges"; return "Change transmission ranges";
} }
public void doAction(Mote mote) { public void doAction(Mote mote) {
transmissionSpinner.setVisible(true); transmissionSpinner.setVisible(true);
interferenceSpinner.setVisible(true); interferenceSpinner.setVisible(true);
@ -160,12 +168,15 @@ public class StandardRadioMedium extends RadioMedium {
public void mouseExited(MouseEvent e) { public void mouseExited(MouseEvent e) {
// Do nothing // Do nothing
} }
public void mouseEntered(MouseEvent e) { public void mouseEntered(MouseEvent e) {
// Do nothing // Do nothing
} }
public void mouseReleased(MouseEvent e) { public void mouseReleased(MouseEvent e) {
// Do nothing // Do nothing
} }
public void mousePressed(MouseEvent e) { public void mousePressed(MouseEvent e) {
Vector<Mote> clickedMotes = findMotesAtPosition(e.getX(), e.getY()); Vector<Mote> clickedMotes = findMotesAtPosition(e.getX(), e.getY());
if (clickedMotes == null || clickedMotes.size() == 0) { if (clickedMotes == null || clickedMotes.size() == 0) {
@ -188,6 +199,7 @@ public class StandardRadioMedium extends RadioMedium {
} }
repaint(); repaint();
} }
public void mouseClicked(MouseEvent e) { public void mouseClicked(MouseEvent e) {
} }
}); });
@ -314,6 +326,7 @@ public class StandardRadioMedium extends RadioMedium {
// Maximum ranges (SS indicator 100) // Maximum ranges (SS indicator 100)
private static double TRANSMITTING_RANGE = 50; private static double TRANSMITTING_RANGE = 50;
private static double INTERFERENCE_RANGE = 100; private static double INTERFERENCE_RANGE = 100;
private class RadioMediumObservable extends Observable { private class RadioMediumObservable extends Observable {
@ -321,34 +334,90 @@ public class StandardRadioMedium extends RadioMedium {
setChanged(); setChanged();
} }
} }
private RadioMediumObservable radioMediumObservable = new RadioMediumObservable(); private RadioMediumObservable radioMediumObservable = new RadioMediumObservable();
private RadioConnection[] lastTickConnections = null; private RadioConnection[] lastTickConnections = null;
private Vector<RadioConnection> pendingConnections = new Vector<RadioConnection>(); private Vector<RadioConnection> pendingConnections = new Vector<RadioConnection>();
private ConnectionLogger myLogger = null; private ConnectionLogger myLogger = null;
private Observer radioDataObserver = new Observer() { private Observer radioDataObserver = new Observer() {
public void update(Observable radio, Object obj) { public void update(Observable radio, Object obj) {
// Something, let tick loop notify observers // This radio changed, let tick loop notify observers
radioMediumObservable.setRadioMediumChanged(); radioMediumObservable.setRadioMediumChanged();
// Register any new transmission
if (((Radio) radio).getLastEvent() == Radio.RadioEvent.TRANSMISSION_STARTED) { if (((Radio) radio).getLastEvent() == Radio.RadioEvent.TRANSMISSION_STARTED) {
Radio sendingRadio = (Radio) radio;
// If obj is the position, use it directly (speeds up things) // If obj is the position, use it directly (speeds up things)
// Otherwise we must search for the position ourselves // Otherwise we must search for the position ourselves
Position startingPosition = (Position) obj; Position sendingPosition = (Position) obj;
if (startingPosition == null) { if (sendingPosition == null) {
if (!registeredRadios.contains(radio)) { if (!registeredRadios.contains(radio)) {
logger.fatal("Sending radio not registered, skipping packet"); logger.fatal("Sending radio not registered, skipping packet");
return; return;
} }
startingPosition = registeredPositions.get(registeredRadios.indexOf(radio)); sendingPosition = registeredPositions.get(registeredRadios
.indexOf(radio));
if (sendingPosition == null) {
logger.fatal("Sending radio not registered, skipping packet");
return;
}
} }
if (!transmissionStartedRadios.contains(radio)) { byte[] dataToSend = sendingRadio.getLastPacketTransmitted();
transmissionStartedPositions.add(startingPosition);
transmissionStartedRadios.add((Radio) radio); RadioConnection newConnection = new RadioConnection();
pendingConnections.add(newConnection);
newConnection.setSource(sendingRadio, sendingPosition, dataToSend);
// Loop through all radios that are listening
for (int listenNr = 0; listenNr < registeredPositions.size(); listenNr++) {
Radio listeningRadio = registeredRadios.get(listenNr);
// If not the sending radio..
if (sendingRadio != listeningRadio) {
double distance = sendingPosition.getDistanceTo(registeredPositions
.get(listenNr));
// Fetch current output power indicator (scale with as percent)
// TODO Probably not the best way to use indicator
double moteInterferenceRange = INTERFERENCE_RANGE
* (0.01 * (double) sendingRadio
.getCurrentOutputPowerIndicator());
double moteTransmissionRange = TRANSMITTING_RANGE
* (0.01 * (double) sendingRadio
.getCurrentOutputPowerIndicator());
if (distance <= moteTransmissionRange) {
newConnection.addDestination(registeredRadios.get(listenNr),
registeredPositions.get(listenNr), dataToSend);
// If close enough to transmit ok..
if (listeningRadio.isReceiving() || listeningRadio.isInterfered()) {
// .. but listening radio already received a packet
listeningRadio.interferReception(sendingRadio
.getTransmissionEndTime());
listeningRadio.setCurrentSignalStrength(SS_BAD);
} else {
// .. send packet
listeningRadio.receivePacket(dataToSend, sendingRadio
.getTransmissionEndTime());
listeningRadio.setCurrentSignalStrength(SS_OK);
}
} else if (distance <= moteInterferenceRange) {
// If close enough to sabotage other transmissions..
listeningRadio.interferReception(sendingRadio
.getTransmissionEndTime());
listeningRadio.setCurrentSignalStrength(SS_BAD);
}
// else too far away
}
} }
} }
@ -392,10 +461,11 @@ public class StandardRadioMedium extends RadioMedium {
} }
// Remove connection if old (don't keep) // Remove connection if old (don't keep)
if (pendingConnection.getSourceRadio().isTransmitting()) if (pendingConnection.getSourceRadio().isTransmitting()) {
updatedPendingConnections.add(pendingConnection); updatedPendingConnections.add(pendingConnection);
} }
} }
}
lastTickConnections = new RadioConnection[newTickConnections.size()]; lastTickConnections = new RadioConnection[newTickConnections.size()];
for (int i = 0; i < lastTickConnections.length; i++) for (int i = 0; i < lastTickConnections.length; i++)
@ -408,73 +478,21 @@ public class StandardRadioMedium extends RadioMedium {
radioMediumObservable.setRadioMediumChanged(); radioMediumObservable.setRadioMediumChanged();
} }
// Start new transmissions
if (transmissionStartedPositions.size() > 0) {
final int numberSending = transmissionStartedPositions.size();
// Loop through all new sending radios
for (int sendNr = 0; sendNr < numberSending; sendNr++) {
Radio sendingRadio = transmissionStartedRadios.get(sendNr);
byte[] dataToSend = sendingRadio.getLastPacketTransmitted();
RadioConnection newConnection = new RadioConnection();
pendingConnections.add(newConnection);
newConnection.setSource(transmissionStartedRadios.get(sendNr),
transmissionStartedPositions.get(sendNr), dataToSend);
// Loop through all radios that are listening
for (int listenNr = 0; listenNr < registeredPositions.size(); listenNr++) {
Radio listeningRadio = registeredRadios.get(listenNr);
// If not the sending radio..
if (sendingRadio != listeningRadio) {
double distance = transmissionStartedPositions.get(sendNr).getDistanceTo(
registeredPositions.get(listenNr));
// Fetch current output power indicator (scale with as percent)
// TODO Probably not the best way to use indicator
double moteInterferenceRange = INTERFERENCE_RANGE
* (0.01 * (double) sendingRadio
.getCurrentOutputPowerIndicator());
double moteTransmissionRange = TRANSMITTING_RANGE
* (0.01 * (double) sendingRadio
.getCurrentOutputPowerIndicator());
if (distance <= moteTransmissionRange) {
newConnection.addDestination(registeredRadios
.get(listenNr), registeredPositions.get(listenNr),
dataToSend);
// If close enough to transmit ok..
if (listeningRadio.isReceiving() || listeningRadio.isInterfered()) {
// .. but listening radio already received a packet
listeningRadio.interferReception(sendingRadio.getTransmissionEndTime());
} else {
// .. send packet
listeningRadio.receivePacket(dataToSend, sendingRadio.getTransmissionEndTime());
}
} else if (distance <= moteInterferenceRange) {
// If close enough to sabotage other transmissions..
listeningRadio.interferReception(sendingRadio.getTransmissionEndTime());
}
// else too far away
}
}
}
transmissionStartedPositions.clear();
transmissionStartedRadios.clear();
if (myLogger != null) { if (myLogger != null) {
for (RadioConnection conn : lastTickConnections) for (RadioConnection conn : lastTickConnections)
myLogger.logConnection(conn); myLogger.logConnection(conn);
} }
// Radio medium has changed, notifing below // Set signal strengths on all radios
radioMediumObservable.setRadioMediumChanged(); for (Radio radio : registeredRadios) {
if (radio.isTransmitting())
radio.setCurrentSignalStrength(SS_OK);
else if (radio.isReceiving())
radio.setCurrentSignalStrength(SS_OK);
else if (radio.isInterfered())
radio.setCurrentSignalStrength(SS_BAD);
else
radio.setCurrentSignalStrength(SS_NOTHING);
} }
// Notify observers (if anything has changed) // Notify observers (if anything has changed)
@ -502,6 +520,10 @@ public class StandardRadioMedium extends RadioMedium {
registeredPositions.add(position); registeredPositions.add(position);
registeredRadios.add(radio); registeredRadios.add(radio);
radio.addObserver(radioDataObserver); radio.addObserver(radioDataObserver);
// Set initial signal strenth
radio.setCurrentSignalStrength(SS_NOTHING);
} // else logger.warn("Standard Radio Medium not registering mote"); } // else logger.warn("Standard Radio Medium not registering mote");
} }