osd-contiki/tools/cooja/examples/project_uaodv/java/VisUAODV.java

114 lines
4.1 KiB
Java
Raw Normal View History

/*
* Copyright (c) 2006, Swedish Institute of Computer Science.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
2008-10-15 11:00:52 +02:00
* $Id: VisUAODV.java,v 1.2 2008/10/15 09:00:52 nifi Exp $
*/
import java.awt.*;
import se.sics.cooja.*;
import se.sics.cooja.interfaces.*;
import se.sics.cooja.plugins.*;
/**
* VisUAODV is a 2D graphical visualizer for simulations with motes running
* UAODV protocol.
* RREQs are painted red, and RREPs green.
* The rest of sent data is painted black.
*
* Interactions with motes are available via registered mote plugins.
*
* @author Fredrik Osterlind
*/
@ClassDescription("uAODV Visualizer")
@PluginType(PluginType.SIM_PLUGIN)
public class VisUAODV extends VisTraffic {
private static final long serialVersionUID = 1L;
/**
* Creates a new VisUAODV visualizer.
* @param simulationToVisualize Simulation to visualize
*/
public VisUAODV(Simulation simulationToVisualize, GUI gui) {
super(simulationToVisualize, gui);
setTitle("uAODV Visualizer");
}
protected void paintConnection(RadioConnection connection, Graphics g2d) {
Point sourcePixelPosition = transformPositionToPixel(connection.getSource().getPosition());
for (Radio destRadio: connection.getDestinations()) {
Position destPosition = destRadio.getPosition();
Point destPixelPosition = transformPositionToPixel(destPosition);
g2d.setColor(getColorOf(connection));
2008-10-15 11:00:52 +02:00
RadioPacket radioPacket = destRadio.getLastPacketReceived();
if (radioPacket != null) {
byte[] packet = radioPacket.getPacketData();
if (isRouteReply(packet)) {
((Graphics2D) g2d).setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1.0f));
}
2008-10-15 11:00:52 +02:00
g2d.drawLine(sourcePixelPosition.x, sourcePixelPosition.y,
destPixelPosition.x, destPixelPosition.y);
2008-10-15 11:00:52 +02:00
int hopCount = getHopCount(packet);
if (hopCount >= 0)
g2d.drawString("" + hopCount, sourcePixelPosition.x, sourcePixelPosition.y);
}
}
}
protected Color getColorOf(RadioConnection conn) {
2008-10-15 11:00:52 +02:00
RadioPacket radioPacket = conn.getSource().getLastPacketReceived();
if (radioPacket != null) {
byte[] packet = radioPacket.getPacketData();
if (isRouteRequest(packet))
return Color.RED;
else if (isRouteReply(packet))
return Color.GREEN;
}
return Color.BLACK;
}
private boolean isRouteRequest(byte[] data) {
if (data.length > 28)
return data[28] == 1;
return false;
}
private boolean isRouteReply(byte[] data) {
if (data.length > 28)
return data[28] == 2;
return false;
}
private int getHopCount(byte[] data) {
if (data.length > 31)
2008-10-15 11:00:52 +02:00
return data[31];
return -1;
}
}