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;
@ -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,12 +157,14 @@ 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) {
@ -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];
@ -254,14 +278,16 @@ public class RadioLogger extends VisPlugin {
* @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 {
@ -306,7 +332,7 @@ 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() {
@ -319,7 +345,7 @@ 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() {
@ -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,7 +420,7 @@ 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() {
@ -405,7 +433,7 @@ 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() {
@ -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,7 +506,7 @@ 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() {
@ -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;
}
@ -543,17 +575,21 @@ public class RadioLogger extends VisPlugin {
}
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;
}
@ -571,20 +607,25 @@ 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;
@ -49,9 +49,6 @@ import se.sics.cooja.plugins.Visualizer2D;
*
* 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.
*
@ -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();
}
@ -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());
@ -405,9 +410,9 @@ 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) {
@ -420,10 +425,12 @@ public class UDGM extends AbstractRadioMedium {
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);