gateway plugin does not extend visplugin; it can be used when cooja is not visualized (such as during tests)

This commit is contained in:
fros4943 2008-12-17 12:15:43 +00:00
parent 1a2794d961
commit 67d2761fcc

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: NativeIPGateway.java,v 1.2 2008/12/12 16:27:40 fros4943 Exp $ * $Id: NativeIPGateway.java,v 1.3 2008/12/17 12:15:43 fros4943 Exp $
*/ */
package se.sics.cooja.plugins; package se.sics.cooja.plugins;
@ -48,9 +48,13 @@ import javax.swing.Box;
import javax.swing.BoxLayout; import javax.swing.BoxLayout;
import javax.swing.JCheckBox; import javax.swing.JCheckBox;
import javax.swing.JComboBox; import javax.swing.JComboBox;
import javax.swing.JInternalFrame;
import javax.swing.JLabel; import javax.swing.JLabel;
import javax.swing.JPanel; import javax.swing.JPanel;
import javax.swing.JScrollPane; import javax.swing.JScrollPane;
import javax.swing.event.InternalFrameEvent;
import javax.swing.event.InternalFrameListener;
import jpcap.JpcapCaptor; import jpcap.JpcapCaptor;
import jpcap.JpcapSender; import jpcap.JpcapSender;
import jpcap.NetworkInterface; import jpcap.NetworkInterface;
@ -62,14 +66,14 @@ import org.jdom.Element;
import se.sics.cooja.ClassDescription; import se.sics.cooja.ClassDescription;
import se.sics.cooja.GUI; import se.sics.cooja.GUI;
import se.sics.cooja.Mote; import se.sics.cooja.Mote;
import se.sics.cooja.Plugin;
import se.sics.cooja.PluginType; import se.sics.cooja.PluginType;
import se.sics.cooja.Simulation; import se.sics.cooja.Simulation;
import se.sics.cooja.VisPlugin;
import se.sics.cooja.interfaces.SerialPort; import se.sics.cooja.interfaces.SerialPort;
@ClassDescription("Open Native IP Gateway") @ClassDescription("Open Native IP Gateway")
@PluginType(PluginType.MOTE_PLUGIN) @PluginType(PluginType.MOTE_PLUGIN)
public class NativeIPGateway extends VisPlugin { public class NativeIPGateway implements Plugin {
private static Logger logger = Logger.getLogger(NativeIPGateway.class); private static Logger logger = Logger.getLogger(NativeIPGateway.class);
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
@ -84,6 +88,8 @@ public class NativeIPGateway extends VisPlugin {
private final static int LABEL_WIDTH = 170; private final static int LABEL_WIDTH = 170;
private final static int LABEL_HEIGHT = 20; private final static int LABEL_HEIGHT = 20;
private Object coojaTag = null; /* Used by Cooja for book-keeping */
private Mote mote; private Mote mote;
private SerialPort serialPort = null; private SerialPort serialPort = null;
private boolean registeredGateway = false; private boolean registeredGateway = false;
@ -102,6 +108,7 @@ public class NativeIPGateway extends VisPlugin {
private int inPkts = 0, outPkts = 0; private int inPkts = 0, outPkts = 0;
private JInternalFrame pluginGUI = null;
private JLabel gatewayLabel = null; private JLabel gatewayLabel = null;
private JLabel interfaceLabel = null; private JLabel interfaceLabel = null;
private JLabel macLabel = null; private JLabel macLabel = null;
@ -126,8 +133,7 @@ public class NativeIPGateway extends VisPlugin {
private final int READ_SLIP_BUFFER_SIZE = 256; private final int READ_SLIP_BUFFER_SIZE = 256;
private byte[] readSlipBuffer = new byte[READ_SLIP_BUFFER_SIZE]; private byte[] readSlipBuffer = new byte[READ_SLIP_BUFFER_SIZE];
public NativeIPGateway(Mote mote, Simulation simulation, GUI gui) { public NativeIPGateway(Mote mote, Simulation simulation, final GUI gui) {
super("Native IP Gateway (" + mote + ")", gui);
this.mote = mote; this.mote = mote;
/* Native OS - plugin depends on platform specific commands */ /* Native OS - plugin depends on platform specific commands */
@ -241,6 +247,28 @@ public class NativeIPGateway extends VisPlugin {
}); });
/* GUI components */ /* GUI components */
if (GUI.isVisualized()) {
pluginGUI = new JInternalFrame(
"Native IP Gateway (" + mote + ")",
true, true, true, true);
pluginGUI.addInternalFrameListener(new InternalFrameListener() {
public void internalFrameClosing(InternalFrameEvent e) {
gui.removePlugin(NativeIPGateway.this, true);
}
public void internalFrameClosed(InternalFrameEvent e) { }
public void internalFrameOpened(InternalFrameEvent e) { }
public void internalFrameIconified(InternalFrameEvent e) { }
public void internalFrameDeiconified(InternalFrameEvent e) { }
public void internalFrameActivated(InternalFrameEvent e) {
/* Highlight mote in COOJA */
if (NativeIPGateway.this.coojaTag != null && coojaTag instanceof Mote) {
gui.signalMoteHighlight((Mote) coojaTag);
}
}
public void internalFrameDeactivated(InternalFrameEvent e) { }
}
);
JPanel mainPane = new JPanel(); JPanel mainPane = new JPanel();
mainPane.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10)); mainPane.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
mainPane.setLayout(new BoxLayout(mainPane, BoxLayout.Y_AXIS)); mainPane.setLayout(new BoxLayout(mainPane, BoxLayout.Y_AXIS));
@ -260,13 +288,14 @@ public class NativeIPGateway extends VisPlugin {
outLabel = addInfo(mainPane, "Packets from simulation:", "0"); outLabel = addInfo(mainPane, "Packets from simulation:", "0");
outLabel.setToolTipText(null); outLabel.setToolTipText(null);
this.getContentPane().add(BorderLayout.CENTER, pluginGUI.getContentPane().add(BorderLayout.CENTER,
new JScrollPane(mainPane, new JScrollPane(mainPane,
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED)); JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED));
pack(); pluginGUI.pack();
setSize(getWidth()+10, getHeight()+10); pluginGUI.setSize(pluginGUI.getWidth()+10, pluginGUI.getHeight()+10);
}
/* Start capturing network traffic for simulated network */ /* Start capturing network traffic for simulated network */
if (tunnelInterface != null) { if (tunnelInterface != null) {
@ -274,12 +303,10 @@ public class NativeIPGateway extends VisPlugin {
} else { } else {
startCapturingPackets(networkInterfacesAll[0]); startCapturingPackets(networkInterfacesAll[0]);
} }
try {
setSelected(true);
} catch (java.beans.PropertyVetoException e) {
// Could not select
} }
public JInternalFrame getGUI() {
return pluginGUI;
} }
private void startCapturingPackets(NetworkInterface intf) { private void startCapturingPackets(NetworkInterface intf) {
@ -366,6 +393,7 @@ public class NativeIPGateway extends VisPlugin {
} }
/* Update GUI */ /* Update GUI */
if (GUI.isVisualized()) {
interfaceLabel.setText(networkInterface.description); interfaceLabel.setText(networkInterface.description);
interfaceLabel.setToolTipText(networkInterface.description); interfaceLabel.setToolTipText(networkInterface.description);
if (networkInterface.addresses.length > 0) { if (networkInterface.addresses.length > 0) {
@ -390,6 +418,7 @@ public class NativeIPGateway extends VisPlugin {
selectNICComboBox.setSelectedItem(networkInterface.description + " (" + networkInterface.name + ")"); selectNICComboBox.setSelectedItem(networkInterface.description + " (" + networkInterface.name + ")");
} }
}
private void configureLoopbackInterface() { private void configureLoopbackInterface() {
if (ON_WINDOWS) { if (ON_WINDOWS) {
@ -510,7 +539,7 @@ public class NativeIPGateway extends VisPlugin {
private void createTunInterface() { private void createTunInterface() {
if (ON_WINDOWS) { if (ON_WINDOWS) {
logger.warn("Cannot create tunnel network interface on Windows. Try using VMware interfaces."); /*logger.warn("Cannot create tunnel network interface on Windows. Try using VMware interfaces.");*/
} else { } else {
createTunInterfaceLinux(); createTunInterfaceLinux();
} }
@ -695,10 +724,13 @@ public class NativeIPGateway extends VisPlugin {
System.arraycopy(packet.data, 0, packetData, packet.header.length-offset, packet.data.length); System.arraycopy(packet.data, 0, packetData, packet.header.length-offset, packet.data.length);
writeAsSlip(packetData, serialPort); writeAsSlip(packetData, serialPort);
/* Update GUI */
inPkts++; inPkts++;
/* Update GUI */
if (GUI.isVisualized()) {
inLabel.setText("" + inPkts); inLabel.setText("" + inPkts);
} }
}
private void handleOutgoingPacket(byte[] packetData) { private void handleOutgoingPacket(byte[] packetData) {
@ -744,11 +776,13 @@ public class NativeIPGateway extends VisPlugin {
/*logger.info("Sending packet (" + packet.len + " bytes) to native network: " + sender);*/ /*logger.info("Sending packet (" + packet.len + " bytes) to native network: " + sender);*/
sender.sendPacket(packet); sender.sendPacket(packet);
outPkts++;
/* Update GUI */ /* Update GUI */
outPkts++; if (GUI.isVisualized()) {
outLabel.setText("" + outPkts); outLabel.setText("" + outPkts);
} }
}
/** /**
* Wraps packet as SLIP. * Wraps packet as SLIP.
@ -869,7 +903,7 @@ public class NativeIPGateway extends VisPlugin {
} }
if (!ok) { if (!ok) {
logger.warn("Network interface not available: " + element.getText()); logger.warn("Network interface not available: " + element.getText());
logger.warn("Instead capturing on default network interface: " + selectNICComboBox.getItemAt(0)); logger.warn("Instead capturing on default network interface: " + networkInterface.name);
} }
} else if (element.getName().equals("register_routes")) { } else if (element.getName().equals("register_routes")) {
autoRegisterRoutes.setSelected(Boolean.parseBoolean(element.getText())); autoRegisterRoutes.setSelected(Boolean.parseBoolean(element.getText()));
@ -927,4 +961,12 @@ public class NativeIPGateway extends VisPlugin {
deleteTunInterface(); deleteTunInterface();
} }
public void tagWithObject(Object tag) {
this.coojaTag = tag;
}
public Object getTag() {
return coojaTag;
}
} }