fixed possible null pointer exception

+ some formatting
This commit is contained in:
fros4943 2007-08-21 13:28:52 +00:00
parent d272b062f0
commit 7f25afcfe4
2 changed files with 149 additions and 95 deletions

View file

@ -26,7 +26,7 @@
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $Id: RadioLogger.java,v 1.9 2007/07/17 21:21:19 fros4943 Exp $
* $Id: RadioLogger.java,v 1.10 2007/08/21 13:28:52 fros4943 Exp $
*/
package se.sics.cooja.plugins;
@ -52,7 +52,7 @@ import se.sics.cooja.interfaces.Radio;
* Radio logger listens to the simulation radio medium and lists all transmitted
* data in a table. By overriding the transformDataToString method, protocol
* specific data can be interpreted.
*
*
* @see #transformDataToString(byte[])
* @author Fredrik Osterlind
*/
@ -130,8 +130,9 @@ public class RadioLogger extends VisPlugin {
);
}
if (col == COLUMN_TO)
if (col == COLUMN_TO) {
return true;
}
return false;
}
@ -145,8 +146,9 @@ public class RadioLogger extends VisPlugin {
comboBox.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
if (e.getStateChange() == ItemEvent.SELECTED) {
if (e.getItem() instanceof Mote)
if (e.getItem() instanceof Mote) {
gui.signalMoteHighlight((Mote) e.getItem());
}
}
}
});
@ -155,18 +157,20 @@ public class RadioLogger extends VisPlugin {
public TableCellEditor getCellEditor(int row, int column) {
// Populate combo box
comboBox.removeAllItems();
if (row < 0 || row >= rowData.size())
if (row < 0 || row >= rowData.size()) {
return super.getCellEditor(row, column);
}
RadioConnection conn = (RadioConnection) rowData.get(row)[DATAPOS_CONNECTION];
if (conn == null)
if (conn == null) {
return super.getCellEditor(row, column);
}
for (Radio destRadio: conn.getDestinations()) {
if (destRadio.getMote() != null) {
comboBox.addItem(destRadio.getMote());
comboBox.addItem(destRadio.getMote());
} else {
comboBox.addItem("[standalone radio]");
comboBox.addItem("[standalone radio]");
}
}
@ -204,8 +208,27 @@ public class RadioLogger extends VisPlugin {
dataTable.getSelectionModel().addListSelectionListener(
new ListSelectionListener() {
public void valueChanged(ListSelectionEvent e) {
gui.signalMoteHighlight(((RadioConnection) rowData.get(
dataTable.getSelectedRow())[DATAPOS_CONNECTION]).getSource().getMote());
int selectedRowIndex = dataTable.getSelectedRow();
if (selectedRowIndex < 0) {
return;
}
Object[] row = rowData.get(selectedRowIndex);
if (row == null) {
return;
}
RadioConnection conn = (RadioConnection) row[DATAPOS_CONNECTION];
if (conn == null) {
return;
}
Mote mote = conn.getSource().getMote();
if (mote == null) {
return;
}
gui.signalMoteHighlight(mote);
}
});
@ -220,8 +243,9 @@ public class RadioLogger extends VisPlugin {
simulation.getRadioMedium().addRadioMediumObserver(radioMediumObserver = new Observer() {
public void update(Observable obs, Object obj) {
RadioConnection[] newConnections = radioMedium.getLastTickConnections();
if (newConnections == null)
if (newConnections == null) {
return;
}
for (RadioConnection newConnection: newConnections) {
Object[] data = new Object[3];
@ -249,19 +273,21 @@ public class RadioLogger extends VisPlugin {
/**
* Transform transmitted data to representable object, such as a string.
*
*
* @param data Transmitted data
* @return Representable object
*/
public Object transformDataToString(byte[] data) {
if (data == null)
if (data == null) {
return "[unknown data]";
}
Packet packet = analyzePacket(data);
if (packet == null)
if (packet == null) {
return "Unknown packet, size " + data.length;
else
} else {
return packet.getShortDescription();
}
}
static abstract class Packet {
@ -297,7 +323,7 @@ public class RadioLogger extends VisPlugin {
}
public int getID() {
int id =
int id =
((data[4] & 0xFF) << 24) +
((data[5] & 0xFF) << 16) +
((data[6] & 0xFF) << 8) +
@ -306,11 +332,11 @@ public class RadioLogger extends VisPlugin {
}
public String getDestAddr() {
return (int) (0xff&data[8]) + "." + (int) (0xff&data[9]) + "." + (int) (0xff&data[10]) + "." + (int) (0xff&data[11]);
return (0xff&data[8]) + "." + (0xff&data[9]) + "." + (0xff&data[10]) + "." + (0xff&data[11]);
}
public int getDestSeqNo() {
int seqNo =
int seqNo =
((data[12] & 0xFF) << 24) +
((data[13] & 0xFF) << 16) +
((data[14] & 0xFF) << 8) +
@ -319,11 +345,11 @@ public class RadioLogger extends VisPlugin {
}
public String getOrigAddr() {
return (int) (0xff&data[16]) + "." + (int) (0xff&data[17]) + "." + (int) (0xff&data[18]) + "." + (int) (0xff&data[19]);
return (0xff&data[16]) + "." + (0xff&data[17]) + "." + (0xff&data[18]) + "." + (0xff&data[19]);
}
public int getOrigSeqNo() {
int seqNo =
int seqNo =
((data[20] & 0xFF) << 24) +
((data[21] & 0xFF) << 16) +
((data[22] & 0xFF) << 8) +
@ -350,14 +376,16 @@ public class RadioLogger extends VisPlugin {
}
public static boolean dataFits(byte[] packetData) {
if (packetData.length != SIZE)
if (packetData.length != SIZE) {
return false;
}
byte[] dataNoHeader = new byte[HEADER_SIZE];
System.arraycopy(packetData, packetData.length - HEADER_SIZE, dataNoHeader, 0, HEADER_SIZE);
if (dataNoHeader[0] != TYPE)
if (dataNoHeader[0] != TYPE) {
return false;
}
return true;
}
@ -392,11 +420,11 @@ public class RadioLogger extends VisPlugin {
}
public String getDestAddr() {
return (int) (0xff&data[4]) + "." + (int) (0xff&data[5]) + "." + (int) (0xff&data[6]) + "." + (int) (0xff&data[7]);
return (0xff&data[4]) + "." + (0xff&data[5]) + "." + (0xff&data[6]) + "." + (0xff&data[7]);
}
public int getDestSeqNo() {
int seqNo =
int seqNo =
((data[8] & 0xFF) << 24) +
((data[9] & 0xFF) << 16) +
((data[10] & 0xFF) << 8) +
@ -405,11 +433,11 @@ public class RadioLogger extends VisPlugin {
}
public String getOrigAddr() {
return (int) (0xff&data[12]) + "." + (int) (0xff&data[13]) + "." + (int) (0xff&data[14]) + "." + (int) (0xff&data[15]);
return (0xff&data[12]) + "." + (0xff&data[13]) + "." + (0xff&data[14]) + "." + (0xff&data[15]);
}
public int getLifetime() {
int seqNo =
int seqNo =
((data[16] & 0xFF) << 24) +
((data[17] & 0xFF) << 16) +
((data[18] & 0xFF) << 8) +
@ -423,7 +451,7 @@ public class RadioLogger extends VisPlugin {
public String getToolTip() {
return "<html>" +
"AODV RREP type: " + getType() + "<br>" +
"AODV RREP type: " + getType() + "<br>" +
"AODV RREP flags: " + getFlags() + "<br>" +
"AODV RREP prefix: " + getPrefix() + "<br>" +
"AODV RREP hop_count: " + getHopCount() + "<br>" +
@ -435,14 +463,16 @@ public class RadioLogger extends VisPlugin {
}
public static boolean dataFits(byte[] packetData) {
if (packetData.length != SIZE)
if (packetData.length != SIZE) {
return false;
}
byte[] dataNoHeader = new byte[HEADER_SIZE];
System.arraycopy(packetData, packetData.length - HEADER_SIZE, dataNoHeader, 0, HEADER_SIZE);
if (dataNoHeader[0] != TYPE)
if (dataNoHeader[0] != TYPE) {
return false;
}
return true;
}
@ -476,11 +506,11 @@ public class RadioLogger extends VisPlugin {
}
public String getUnreachAddr() {
return (int) (0xff&data[4]) + "." + (int) (0xff&data[5]) + "." + (int) (0xff&data[6]) + "." + (int) (0xff&data[7]);
return (0xff&data[4]) + "." + (0xff&data[5]) + "." + (0xff&data[6]) + "." + (0xff&data[7]);
}
public int getUnreachSeqNo() {
int seqNo =
int seqNo =
((data[8] & 0xFF) << 24) +
((data[9] & 0xFF) << 16) +
((data[10] & 0xFF) << 8) +
@ -494,7 +524,7 @@ public class RadioLogger extends VisPlugin {
public String getToolTip() {
return "<html>" +
"AODV RERR type: " + getType() + "<br>" +
"AODV RERR type: " + getType() + "<br>" +
"AODV RERR flags: " + getFlags() + "<br>" +
"AODV RERR reserved: " + getReserved() + "<br>" +
"AODV RERR dest_count: " + getDestCount() + "<br>" +
@ -504,14 +534,16 @@ public class RadioLogger extends VisPlugin {
}
public static boolean dataFits(byte[] packetData) {
if (packetData.length != SIZE)
if (packetData.length != SIZE) {
return false;
}
byte[] dataNoHeader = new byte[HEADER_SIZE];
System.arraycopy(packetData, packetData.length - HEADER_SIZE, dataNoHeader, 0, HEADER_SIZE);
if (dataNoHeader[0] != TYPE)
if (dataNoHeader[0] != TYPE) {
return false;
}
return true;
}
@ -526,7 +558,7 @@ public class RadioLogger extends VisPlugin {
}
public int getChecksum() {
int checksum =
int checksum =
((data[3] & 0xFF) << 8) +
((data[4] & 0xFF) << 0);
return checksum;
@ -538,27 +570,31 @@ public class RadioLogger extends VisPlugin {
public String getToolTip() {
return "<html>" +
"ACK checksum: " + getChecksum() + "<br>" +
"ACK checksum: " + getChecksum() + "<br>" +
"</html>";
}
public static boolean dataFits(byte[] packetData) {
if (packetData.length != SIZE)
if (packetData.length != SIZE) {
return false;
}
if (packetData[0] != (byte) 'a')
if (packetData[0] != (byte) 'a') {
return false;
if (packetData[1] != (byte) 'C')
}
if (packetData[1] != (byte) 'C') {
return false;
if (packetData[2] != (byte) 'k')
}
if (packetData[2] != (byte) 'k') {
return false;
}
return true;
}
};
static class ForwardedPacketUnknown extends PacketUnknown {
public final static int MINIMUM_SIZE = 4;
@ -571,21 +607,26 @@ public class RadioLogger extends VisPlugin {
}
public static boolean dataFits(byte[] packetData) {
if (packetData.length < ForwardedPacketUnknown.MINIMUM_SIZE)
if (packetData.length < ForwardedPacketUnknown.MINIMUM_SIZE) {
return false;
if (packetData[0] != (byte) 'f')
}
if (packetData[0] != (byte) 'f') {
return false;
if (packetData[1] != (byte) 'W')
}
if (packetData[1] != (byte) 'W') {
return false;
if (packetData[2] != (byte) 'd')
}
if (packetData[2] != (byte) 'd') {
return false;
if (packetData[3] != (byte) ':')
}
if (packetData[3] != (byte) ':') {
return false;
}
return true;
}
}
@ -626,27 +667,33 @@ public class RadioLogger extends VisPlugin {
private Packet analyzePacket(byte[] data) {
if (PacketAODV_RREQ.dataFits(data))
if (PacketAODV_RREQ.dataFits(data)) {
return new PacketAODV_RREQ(data);
}
if (PacketAODV_RREP.dataFits(data))
if (PacketAODV_RREP.dataFits(data)) {
return new PacketAODV_RREP(data);
}
if (PacketAODV_RERR.dataFits(data))
if (PacketAODV_RERR.dataFits(data)) {
return new PacketAODV_RERR(data);
}
if (ForwardedPacketUnknown.dataFits(data))
if (ForwardedPacketUnknown.dataFits(data)) {
return new ForwardedPacketUnknown(data);
}
if (AckPacket.dataFits(data))
if (AckPacket.dataFits(data)) {
return new AckPacket(data);
}
return new PacketUnknown(data);
}
public void closePlugin() {
if (radioMediumObserver != null)
if (radioMediumObserver != null) {
radioMedium.deleteRadioMediumObserver(radioMediumObserver);
}
}
public Collection<Element> getConfigXML() {

View file

@ -26,7 +26,7 @@
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $Id: UDGM.java,v 1.7 2007/08/21 09:17:18 fros4943 Exp $
* $Id: UDGM.java,v 1.8 2007/08/21 13:31:26 fros4943 Exp $
*/
package se.sics.cooja.radiomediums;
@ -46,26 +46,23 @@ import se.sics.cooja.plugins.Visualizer2D;
/**
* The Unit Disk Graph medium has two different range parameters; one for
* transmitting and one for interfering other transmissions.
*
*
* The radio medium supports both byte and packet radios.
*
* TODO Any transmitted bytes are forwarded immediately with a timestamp
* (more fine-grained than ticks).
*
*
* The radio medium registers a visualizer plugin. Via this plugin the current
* radio states and range parameters can be viewed and changed.
*
*
* The registered radios' signal strengths are updated whenever the radio medium
* changes. There are three fixed levels: no surrounding traffic heard, noise
* heard and data heard.
*
*
* The radio output power indicator (0-100) is used in a very simple way; the
* total transmission (and interfering) range is multiplied with [power_ind]%.
*
*
* @see #SS_OK
* @see #SS_NOISE
* @see #SS_NOTHING
*
*
* @see VisUDGM
* @author Fredrik Osterlind
*/
@ -91,20 +88,20 @@ public class UDGM extends AbstractRadioMedium {
private static double INTERFERENCE_RANGE = 100;
private Simulation mySimulation;
private boolean usingRandom = false;
private Random random = new Random();
/**
* Visualizes radio traffic in the UDGM. Allows a user to
* change transmission ranges.
*
*
* Sending motes are blue, receiving motes are green and motes that hear noise
* are painted red. Motes without radios are painted gray, and the rest are
* white.
*
*
* @author Fredrik Osterlind
*/
@ClassDescription("UDGM Visualizer")
@ -182,8 +179,8 @@ public class UDGM extends AbstractRadioMedium {
successRatioSpinner = new JSpinner(successRatioModel);
editor = new JSpinner.NumberEditor(successRatioSpinner, "0%");
successRatioSpinner.setEditor(editor);
((JSpinner.DefaultEditor) transmissionSpinner.getEditor()).getTextField()
.setColumns(5);
((JSpinner.DefaultEditor) interferenceSpinner.getEditor()).getTextField()
@ -253,10 +250,11 @@ public class UDGM extends AbstractRadioMedium {
// Select one of the clicked motes
if (clickedMotes.contains(selectedMote)) {
int pos = clickedMotes.indexOf(selectedMote);
if (pos < clickedMotes.size() - 1)
if (pos < clickedMotes.size() - 1) {
selectedMote = clickedMotes.get(pos + 1);
else
} else {
selectedMote = clickedMotes.firstElement();
}
} else {
selectedMote = clickedMotes.firstElement();
}
@ -270,7 +268,7 @@ public class UDGM extends AbstractRadioMedium {
// Register change ranges and change success ratio action
addMoteMenuAction(new ChangeRangesMenuAction());
addMoteMenuAction(new ChangeSuccessRadioMenuAction());
// Observe our own radio medium
myRadioMedium
.addRadioMediumObserver(radioMediumObserver = new Observer() {
@ -289,23 +287,29 @@ public class UDGM extends AbstractRadioMedium {
public Color[] getColorOf(Mote mote) {
Radio moteRadio = mote.getInterfaces().getRadio();
if (moteRadio == null)
if (moteRadio == null) {
return new Color[] { Color.GRAY };
}
if (mote.getState() == Mote.State.DEAD)
if (mote.getState() == Mote.State.DEAD) {
return new Color[] { Color.GRAY };
}
if (selectedMote != null && mote == selectedMote)
if (selectedMote != null && mote == selectedMote) {
return new Color[] { Color.CYAN };
}
if (moteRadio.isTransmitting())
if (moteRadio.isTransmitting()) {
return new Color[] { Color.BLUE };
}
if (moteRadio.isInterfered())
if (moteRadio.isInterfered()) {
return new Color[] { Color.RED };
}
if (moteRadio.isReceiving())
if (moteRadio.isReceiving()) {
return new Color[] { Color.GREEN };
}
return new Color[] { Color.WHITE };
}
@ -323,10 +327,10 @@ public class UDGM extends AbstractRadioMedium {
// Fetch current output power indicator (scale with as percent)
if (selectedMote.getInterfaces().getRadio() != null) {
double moteInterferenceRange = INTERFERENCE_RANGE
* (0.01 * (double) selectedMote.getInterfaces().getRadio()
* (0.01 * selectedMote.getInterfaces().getRadio()
.getCurrentOutputPowerIndicator());
double moteTransmissionRange = TRANSMITTING_RANGE
* (0.01 * (double) selectedMote.getInterfaces().getRadio()
* (0.01 * selectedMote.getInterfaces().getRadio()
.getCurrentOutputPowerIndicator());
Point translatedZero = transformPositionToPixel(0.0, 0.0, 0.0);
@ -362,9 +366,10 @@ public class UDGM extends AbstractRadioMedium {
super.visualizeSimulation(g);
// Paint just finished connections
RadioConnection[] conns;
if (myRadioMedium != null
&& myRadioMedium.getLastTickConnections() != null) {
for (RadioConnection conn : myRadioMedium.getLastTickConnections()) {
&& (conns = myRadioMedium.getLastTickConnections()) != null) {
for (RadioConnection conn : conns) {
if (conn != null) {
Point sourcePoint = transformPositionToPixel(conn.getSource()
.getPosition());
@ -388,7 +393,7 @@ public class UDGM extends AbstractRadioMedium {
public UDGM(Simulation simulation) {
super(simulation);
// Register this radio medium's plugins
simulation.getGUI().registerTemporaryPlugin(VisUDGM.class);
@ -405,25 +410,27 @@ public class UDGM extends AbstractRadioMedium {
// Fetch current output power indicator (scale with as percent)
double moteTransmissionRange = TRANSMITTING_RANGE
* (0.01 * (double) sendingRadio.getCurrentOutputPowerIndicator());
* (0.01 * sendingRadio.getCurrentOutputPowerIndicator());
double moteInterferenceRange = INTERFERENCE_RANGE
* (0.01 * (double) sendingRadio.getCurrentOutputPowerIndicator());
* (0.01 * sendingRadio.getCurrentOutputPowerIndicator());
// If in random state, check if transmission fails
if (usingRandom && random.nextDouble() > PACKET_SUCCESS_RATIO) {
return newConnection;
}
// Loop through all radios
for (int listenNr = 0; listenNr < getRegisteredRadios().size(); listenNr++) {
Radio listeningRadio = getRegisteredRadios().get(listenNr);
Position listeningRadioPosition = listeningRadio.getPosition();
// Ignore sending radio and radios on different channels
if (sendingRadio == listeningRadio)
if (sendingRadio == listeningRadio) {
continue;
if (sendingRadio.getChannel() != listeningRadio.getChannel())
}
if (sendingRadio.getChannel() != listeningRadio.getChannel()) {
continue;
}
double distance = sendingPosition.getDistanceTo(listeningRadioPosition);