[cooja/serialsocket] SerialSocketClient: Status bar and improved

action/event handling and visualization
This commit is contained in:
Enrico Joerns 2014-04-12 15:14:11 +02:00
parent d90aec2376
commit 153457a151

View file

@ -1,6 +1,7 @@
package org.contikios.cooja.serialsocket; package org.contikios.cooja.serialsocket;
/* /*
* Copyright (c) 2014, TU Braunschweig.
* Copyright (c) 2010, Swedish Institute of Computer Science. * Copyright (c) 2010, Swedish Institute of Computer Science.
* All rights reserved. * All rights reserved.
* *
@ -31,11 +32,10 @@ package org.contikios.cooja.serialsocket;
*/ */
import java.awt.BorderLayout; import java.awt.BorderLayout;
import java.awt.Component; import java.awt.Color;
import java.awt.ComponentOrientation;
import java.awt.Dimension;
import java.awt.GridBagConstraints; import java.awt.GridBagConstraints;
import java.awt.GridBagLayout; import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent; import java.awt.event.ActionEvent;
import java.awt.event.ActionListener; import java.awt.event.ActionListener;
import java.io.DataInputStream; import java.io.DataInputStream;
@ -43,21 +43,22 @@ import java.io.DataOutputStream;
import java.io.IOException; import java.io.IOException;
import java.net.Socket; import java.net.Socket;
import java.text.NumberFormat; import java.text.NumberFormat;
import java.text.ParseException;
import java.util.Collection; import java.util.Collection;
import java.util.Observable; import java.util.Observable;
import java.util.Observer; import java.util.Observer;
import java.util.logging.Level;
import javax.swing.BorderFactory; import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout; import javax.swing.BoxLayout;
import javax.swing.JButton; import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFormattedTextField; import javax.swing.JFormattedTextField;
import javax.swing.JLabel; import javax.swing.JLabel;
import javax.swing.JPanel; import javax.swing.JPanel;
import javax.swing.JSeparator; import javax.swing.JSeparator;
import javax.swing.JTextField; import javax.swing.JTextField;
import javax.swing.SwingUtilities; import javax.swing.SwingUtilities;
import javax.swing.border.EtchedBorder;
import javax.swing.text.NumberFormatter; import javax.swing.text.NumberFormatter;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
@ -76,30 +77,36 @@ import org.contikios.cooja.interfaces.SerialPort;
* Socket to simulated serial port forwarder. Client version. * Socket to simulated serial port forwarder. Client version.
* *
* @author Fredrik Osterlind * @author Fredrik Osterlind
* @author Enrico Jorns
*/ */
@ClassDescription("Serial Socket (CLIENT)") @ClassDescription("Serial Socket (CLIENT)")
@PluginType(PluginType.MOTE_PLUGIN) @PluginType(PluginType.MOTE_PLUGIN)
public class SerialSocketClient extends VisPlugin implements MotePlugin { public class SerialSocketClient extends VisPlugin implements MotePlugin {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
private static Logger logger = Logger.getLogger(SerialSocketClient.class); private static final Logger logger = Logger.getLogger(SerialSocketClient.class);
private final static int LABEL_WIDTH = 100; private static final String SERVER_DEFAULT_HOST = "localhost";
private final static int LABEL_HEIGHT = 15; private static final int SERVER_DEFAULT_PORT = 1234;
public final static String SERVER_HOST = "localhost"; private static final Color ST_COLOR_UNCONNECTED = Color.DARK_GRAY;
public final static int SERVER_PORT = 1234; private static final Color ST_COLOR_CONNECTED = new Color(0, 161, 83);
private static final Color ST_COLOR_FAILED = Color.RED;
private SerialPort serialPort; private SerialPort serialPort;
private Observer serialDataObserver; private Observer serialDataObserver;
private JLabel statusLabel, inLabel, outLabel; private JLabel socketToMoteLabel;
private JLabel moteToSocketLabel;
private JLabel socketStatusLabel;
private JButton serverSelectButton;
private int inBytes = 0, outBytes = 0; private int inBytes = 0, outBytes = 0;
private Socket socket; private Socket socket;
private DataInputStream in; private DataInputStream in;
private DataOutputStream out; private DataOutputStream out;
private Mote mote; private final Mote mote;
public SerialSocketClient(Mote mote, Simulation simulation, final Cooja gui) { public SerialSocketClient(Mote mote, Simulation simulation, final Cooja gui) {
super("Serial Socket (CLIENT) (" + mote + ")", gui, false); super("Serial Socket (CLIENT) (" + mote + ")", gui, false);
@ -111,6 +118,8 @@ public class SerialSocketClient extends VisPlugin implements MotePlugin {
setResizable(false); setResizable(false);
setLayout(new BorderLayout()); setLayout(new BorderLayout());
// --- Server setup
GridBagConstraints c = new GridBagConstraints(); GridBagConstraints c = new GridBagConstraints();
JPanel serverSelectPanel = new JPanel(new GridBagLayout()); JPanel serverSelectPanel = new JPanel(new GridBagLayout());
pack(); pack();
@ -122,7 +131,7 @@ public class SerialSocketClient extends VisPlugin implements MotePlugin {
c.gridx++; c.gridx++;
serverSelectPanel.add(label, c); serverSelectPanel.add(label, c);
final JTextField serverHostField = new JTextField("localhost"); final JTextField serverHostField = new JTextField(SERVER_DEFAULT_HOST);
serverHostField.setColumns(10); serverHostField.setColumns(10);
c.gridx++; c.gridx++;
c.weightx = 1.0; c.weightx = 1.0;
@ -133,14 +142,15 @@ public class SerialSocketClient extends VisPlugin implements MotePlugin {
c.weightx = 0.0; c.weightx = 0.0;
serverSelectPanel.add(label, c); serverSelectPanel.add(label, c);
final JFormattedTextField serverPort = new JFormattedTextField( NumberFormat nf = NumberFormat.getIntegerInstance();
new NumberFormatter(NumberFormat.getIntegerInstance())); nf.setGroupingUsed(false);
serverPort.setColumns(5); final JFormattedTextField serverPortField = new JFormattedTextField(new NumberFormatter(nf));
serverPort.setText("1234"); serverPortField.setColumns(5);
serverPortField.setText(String.valueOf(SERVER_DEFAULT_PORT));
c.gridx++; c.gridx++;
serverSelectPanel.add(serverPort, c); serverSelectPanel.add(serverPortField, c);
final JButton serverSelectButton = new JButton("Connect"); serverSelectButton = new JButton("Connect");
c.gridx++; c.gridx++;
serverSelectPanel.add(serverSelectButton, c); serverSelectPanel.add(serverSelectButton, c);
@ -152,73 +162,102 @@ public class SerialSocketClient extends VisPlugin implements MotePlugin {
add(BorderLayout.NORTH, serverSelectPanel); add(BorderLayout.NORTH, serverSelectPanel);
serverSelectButton.addActionListener(new ActionListener() { // --- Incoming / outgoing info
@Override JPanel connectionInfoPanel = new JPanel(new GridLayout(0, 2));
public void actionPerformed(ActionEvent e) { connectionInfoPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
try {
logger.info("Connecting: " + SERVER_HOST + ":" + SERVER_PORT);
socket = new Socket(SERVER_HOST, SERVER_PORT);
in = new DataInputStream(socket.getInputStream());
out = new DataOutputStream(socket.getOutputStream());
out.flush();
startSocketReadThread(in);
} catch (Exception ex) {
throw (RuntimeException) new RuntimeException(
"Connection error: " + ex.getMessage()).initCause(ex);
}
}
});
JPanel connectionInfoPanel = new JPanel(new GridBagLayout());
connectionInfoPanel.setBorder(BorderFactory.createEmptyBorder(3, 3, 3, 3));
c = new GridBagConstraints(); c = new GridBagConstraints();
label = new JLabel("Status:"); label = new JLabel("socket -> mote: ");
label.setHorizontalAlignment(JLabel.RIGHT);
c.gridx = 0; c.gridx = 0;
c.gridy = 0; c.gridy = 0;
c.anchor = GridBagConstraints.EAST; c.anchor = GridBagConstraints.EAST;
c.ipadx = 5; connectionInfoPanel.add(label);
connectionInfoPanel.add(label, c);
final JLabel socketStatusLabel = new JLabel("disconnected"); socketToMoteLabel = new JLabel("0 bytes");
c.gridx++; c.gridx++;
c.anchor = GridBagConstraints.WEST; c.anchor = GridBagConstraints.WEST;
connectionInfoPanel.add(socketStatusLabel, c); connectionInfoPanel.add(socketToMoteLabel);
label = new JLabel("socket -> mote:"); label = new JLabel("mote -> socket: ");
label.setHorizontalAlignment(JLabel.RIGHT);
c.gridx = 0; c.gridx = 0;
c.gridy++; c.gridy++;
c.anchor = GridBagConstraints.EAST; c.anchor = GridBagConstraints.EAST;
connectionInfoPanel.add(label, c); connectionInfoPanel.add(label);
final JLabel socketToMoteLabel = new JLabel("0 bytes"); moteToSocketLabel = new JLabel("0 bytes");
c.gridx++; c.gridx++;
c.anchor = GridBagConstraints.WEST; c.anchor = GridBagConstraints.WEST;
connectionInfoPanel.add(socketToMoteLabel, c); connectionInfoPanel.add(moteToSocketLabel);
label = new JLabel("mote -> socket:");
c.gridx = 0;
c.gridy++;
c.anchor = GridBagConstraints.EAST;
connectionInfoPanel.add(label, c);
final JLabel moteToSocketLabel = new JLabel("0 bytes");
c.gridx++;
c.anchor = GridBagConstraints.WEST;
connectionInfoPanel.add(moteToSocketLabel, c);
add(BorderLayout.CENTER, connectionInfoPanel); add(BorderLayout.CENTER, connectionInfoPanel);
// --- Status bar
JPanel statusBarPanel = new JPanel(new BorderLayout());
statusBarPanel.setLayout(new BoxLayout(statusBarPanel, BoxLayout.LINE_AXIS));
statusBarPanel.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.RAISED));
label = new JLabel("Status: ");
statusBarPanel.add(label);
socketStatusLabel = new JLabel("disconnected");
socketStatusLabel.setForeground(Color.DARK_GRAY);
statusBarPanel.add(socketStatusLabel);
add(BorderLayout.SOUTH, statusBarPanel);
/* Mote serial port */ /* Mote serial port */
serialPort = (SerialPort) mote.getInterfaces().getLog(); serialPort = (SerialPort) mote.getInterfaces().getLog();
if (serialPort == null) { if (serialPort == null) {
throw new RuntimeException("No mote serial port"); throw new RuntimeException("No mote serial port");
} }
serverSelectButton.addActionListener(new ActionListener() {
/* Observe serial port for outgoing data */ @Override
public void actionPerformed(ActionEvent e) {
try {
serverPortField.commitEdit();
} catch (ParseException ex) {
java.util.logging.Logger.getLogger(SerialSocketClient.class.getName()).log(Level.SEVERE, null, ex);
}
if (socket == null) {
// connect to serer
try {
logger.info("Connecting: " + serverHostField.getText() + ":" + serverPortField.getValue());
socket = new Socket(serverHostField.getText(), ((Long) serverPortField.getValue()).intValue());
in = new DataInputStream(socket.getInputStream());
out = new DataOutputStream(socket.getOutputStream());
out.flush();
startSocketReadThread(in);
socketStatusLabel.setText("connected");
socketStatusLabel.setForeground(ST_COLOR_CONNECTED);
serverSelectButton.setEnabled(false);
} catch (IOException ex) {
logger.error(ex.getMessage());
socketStatusLabel.setText("failed");
socketStatusLabel.setForeground(ST_COLOR_FAILED);
}
} else {
// disconnect from server
try {
logger.info("Closing connection to serer...");
socket.close();
socketStatusLabel.setText("disconnected");
socketStatusLabel.setForeground(ST_COLOR_UNCONNECTED);
} catch (IOException ex) {
logger.error(ex);
socketStatusLabel.setText("failed");
socketStatusLabel.setForeground(ST_COLOR_FAILED);
}
}
}
});
/* Observe serial port for outgoing data and write to socket */
serialPort.addSerialDataObserver(serialDataObserver = new Observer() { serialPort.addSerialDataObserver(serialDataObserver = new Observer() {
@Override @Override
public void update(Observable obs, Object obj) { public void update(Observable obs, Object obj) {
@ -230,10 +269,12 @@ public class SerialSocketClient extends VisPlugin implements MotePlugin {
out.flush(); out.flush();
outBytes++; outBytes++;
if (Cooja.isVisualized()) { if (Cooja.isVisualized()) {
outLabel.setText(outBytes + " bytes"); moteToSocketLabel.setText(outBytes + " bytes");
} }
} catch (IOException e) { } catch (IOException ex) {
e.printStackTrace(); logger.error(ex.getMessage());
socketStatusLabel.setText("failed");
socketStatusLabel.setForeground(ST_COLOR_FAILED);
} }
} }
}); });
@ -247,13 +288,13 @@ public class SerialSocketClient extends VisPlugin implements MotePlugin {
public void run() { public void run() {
int numRead = 0; int numRead = 0;
byte[] data = new byte[1024]; byte[] data = new byte[1024];
logger.info("Forwarder: socket -> serial port"); logger.info("Start forwarding: socket -> serial port");
while (true) { while (true) {
numRead = -1; numRead = -1;
try { try {
numRead = in.read(data); numRead = in.read(data);
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); logger.error(e.getMessage());
return; return;
} }
@ -263,11 +304,18 @@ public class SerialSocketClient extends VisPlugin implements MotePlugin {
} }
inBytes += numRead; inBytes += numRead;
if (Cooja.isVisualized()) { if (Cooja.isVisualized()) {
inLabel.setText(inBytes + " bytes"); socketToMoteLabel.setText(inBytes + " bytes");
} }
} else { } else {
logger.warn("Incoming data thread shut down"); logger.warn("Incoming data thread shut down");
cleanup(); SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
socketStatusLabel.setForeground(ST_COLOR_FAILED);
socketStatusLabel.setText("Disconnected from server");
serverSelectButton.setEnabled(true);
}
});
break; break;
} }
} }
@ -276,18 +324,6 @@ public class SerialSocketClient extends VisPlugin implements MotePlugin {
incomingDataThread.start(); incomingDataThread.start();
} }
private JLabel configureLabel(JComponent pane, String desc, String value) {
JPanel smallPane = new JPanel(new BorderLayout());
JLabel label = new JLabel(desc);
label.setPreferredSize(new Dimension(LABEL_WIDTH,LABEL_HEIGHT));
smallPane.add(BorderLayout.WEST, label);
label = new JLabel(value);
label.setPreferredSize(new Dimension(LABEL_WIDTH,LABEL_HEIGHT));
smallPane.add(BorderLayout.CENTER, label);
pane.add(smallPane);
return label;
}
@Override @Override
public boolean setConfigXML(Collection<Element> configXML, boolean visAvailable) { public boolean setConfigXML(Collection<Element> configXML, boolean visAvailable) {
return true; return true;
@ -307,6 +343,7 @@ public class SerialSocketClient extends VisPlugin implements MotePlugin {
socket = null; socket = null;
} }
} catch (IOException e1) { } catch (IOException e1) {
logger.warn(e1.getMessage());
} }
try { try {
if (in != null) { if (in != null) {
@ -314,6 +351,7 @@ public class SerialSocketClient extends VisPlugin implements MotePlugin {
in = null; in = null;
} }
} catch (IOException e) { } catch (IOException e) {
logger.warn(e.getMessage());
} }
try { try {
if (out != null) { if (out != null) {
@ -321,15 +359,8 @@ public class SerialSocketClient extends VisPlugin implements MotePlugin {
out = null; out = null;
} }
} catch (IOException e) { } catch (IOException e) {
logger.warn(e.getMessage());
} }
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
SerialSocketClient.this.setTitle(SerialSocketClient.this.getTitle() + " *DISCONNECTED*");
statusLabel.setText("Disconnected from server");
}
});
} }
@Override @Override