[cooja/serialsocket] SerialSocketClient: New visual appearance with

input for server hostname and address including connect button
This commit is contained in:
Enrico Joerns 2014-04-12 03:07:21 +02:00
parent 404fd82399
commit d90aec2376

View file

@ -31,21 +31,34 @@ package org.contikios.cooja.serialsocket;
*/ */
import java.awt.BorderLayout; import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.ComponentOrientation;
import java.awt.Dimension; import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.DataInputStream; import java.io.DataInputStream;
import java.io.DataOutputStream; 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.util.Collection; import java.util.Collection;
import java.util.Observable; import java.util.Observable;
import java.util.Observer; import java.util.Observer;
import javax.swing.BorderFactory; import javax.swing.BorderFactory;
import javax.swing.Box; import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComponent; import javax.swing.JComponent;
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.JTextField;
import javax.swing.SwingUtilities; import javax.swing.SwingUtilities;
import javax.swing.text.NumberFormatter;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
import org.jdom.Element; import org.jdom.Element;
@ -94,57 +107,137 @@ public class SerialSocketClient extends VisPlugin implements MotePlugin {
/* GUI components */ /* GUI components */
if (Cooja.isVisualized()) { if (Cooja.isVisualized()) {
Box northBox = Box.createHorizontalBox();
northBox.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
statusLabel = configureLabel(northBox, "", "");
Box mainBox = Box.createHorizontalBox(); setResizable(false);
mainBox.setBorder(BorderFactory.createEmptyBorder(0, 5, 5, 5)); setLayout(new BorderLayout());
inLabel = configureLabel(mainBox, "socket -> mote:", "0 bytes");
outLabel = configureLabel(mainBox, "mote -> socket", "0 bytes");
getContentPane().add(BorderLayout.NORTH, northBox); GridBagConstraints c = new GridBagConstraints();
getContentPane().add(BorderLayout.CENTER, mainBox); JPanel serverSelectPanel = new JPanel(new GridBagLayout());
pack(); pack();
} c.gridx = 0;
c.gridy = 0;
serverSelectPanel.setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2));
/* Mote serial port */ label = new JLabel("Host:");
serialPort = (SerialPort) mote.getInterfaces().getLog(); c.gridx++;
if (serialPort == null) { serverSelectPanel.add(label, c);
throw new RuntimeException("No mote serial port");
}
try { final JTextField serverHostField = new JTextField("localhost");
logger.info("Connecting: " + SERVER_HOST + ":" + SERVER_PORT); serverHostField.setColumns(10);
socket = new Socket(SERVER_HOST, SERVER_PORT); c.gridx++;
in = new DataInputStream(socket.getInputStream()); c.weightx = 1.0;
out = new DataOutputStream(socket.getOutputStream()); serverSelectPanel.add(serverHostField, c);
out.flush();
startSocketReadThread(in);
} catch (Exception e) {
throw (RuntimeException) new RuntimeException(
"Connection error: " + e.getMessage()).initCause(e);
}
/* Observe serial port for outgoing data */ label = new JLabel("Port:");
serialPort.addSerialDataObserver(serialDataObserver = new Observer() { c.gridx++;
@Override c.weightx = 0.0;
public void update(Observable obs, Object obj) { serverSelectPanel.add(label, c);
try {
if (out == null) { final JFormattedTextField serverPort = new JFormattedTextField(
return; new NumberFormatter(NumberFormat.getIntegerInstance()));
serverPort.setColumns(5);
serverPort.setText("1234");
c.gridx++;
serverSelectPanel.add(serverPort, c);
final JButton serverSelectButton = new JButton("Connect");
c.gridx++;
serverSelectPanel.add(serverSelectButton, c);
c.gridx = 0;
c.gridy++;
c.gridwidth = GridBagConstraints.REMAINDER;
c.fill = GridBagConstraints.HORIZONTAL;
serverSelectPanel.add(new JSeparator(JSeparator.HORIZONTAL), c);
add(BorderLayout.NORTH, serverSelectPanel);
serverSelectButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
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);
} }
out.write(serialPort.getLastSerialData());
out.flush();
outBytes++;
if (Cooja.isVisualized()) {
outLabel.setText(outBytes + " bytes");
}
} catch (IOException e) {
e.printStackTrace();
} }
});
JPanel connectionInfoPanel = new JPanel(new GridBagLayout());
connectionInfoPanel.setBorder(BorderFactory.createEmptyBorder(3, 3, 3, 3));
c = new GridBagConstraints();
label = new JLabel("Status:");
c.gridx = 0;
c.gridy = 0;
c.anchor = GridBagConstraints.EAST;
c.ipadx = 5;
connectionInfoPanel.add(label, c);
final JLabel socketStatusLabel = new JLabel("disconnected");
c.gridx++;
c.anchor = GridBagConstraints.WEST;
connectionInfoPanel.add(socketStatusLabel, c);
label = new JLabel("socket -> mote:");
c.gridx = 0;
c.gridy++;
c.anchor = GridBagConstraints.EAST;
connectionInfoPanel.add(label, c);
final JLabel socketToMoteLabel = new JLabel("0 bytes");
c.gridx++;
c.anchor = GridBagConstraints.WEST;
connectionInfoPanel.add(socketToMoteLabel, c);
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);
/* Mote serial port */
serialPort = (SerialPort) mote.getInterfaces().getLog();
if (serialPort == null) {
throw new RuntimeException("No mote serial port");
} }
});
/* Observe serial port for outgoing data */
serialPort.addSerialDataObserver(serialDataObserver = new Observer() {
@Override
public void update(Observable obs, Object obj) {
try {
if (out == null) {
return;
}
out.write(serialPort.getLastSerialData());
out.flush();
outBytes++;
if (Cooja.isVisualized()) {
outLabel.setText(outBytes + " bytes");
}
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
} }
private void startSocketReadThread(final DataInputStream in) { private void startSocketReadThread(final DataInputStream in) {