[cooja/serialsocket] Support for xml import/export
This commit is contained in:
parent
6c0e7ae15e
commit
dea03493bd
|
@ -45,11 +45,13 @@ 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.text.ParseException;
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.LinkedList;
|
import java.util.LinkedList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
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.BoxLayout;
|
import javax.swing.BoxLayout;
|
||||||
|
@ -428,14 +430,90 @@ public class SerialSocketClient extends VisPlugin implements MotePlugin {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean setConfigXML(Collection<Element> configXML, boolean visAvailable) {
|
public Collection<Element> getConfigXML() {
|
||||||
return true;
|
List<Element> config = new ArrayList<>();
|
||||||
|
Element element;
|
||||||
|
|
||||||
|
// XXX isVisualized guards?
|
||||||
|
element = new Element("host");
|
||||||
|
if (socket == null || !socket.isBound()) {
|
||||||
|
element.setText(serverHostField.getText());
|
||||||
|
} else {
|
||||||
|
element.setText(socket.getInetAddress().getHostName());
|
||||||
|
}
|
||||||
|
config.add(element);
|
||||||
|
|
||||||
|
element = new Element("port");
|
||||||
|
if (socket == null || !socket.isBound()) {
|
||||||
|
try {
|
||||||
|
serverPortField.commitEdit();
|
||||||
|
element.setText(String.valueOf((Long) serverPortField.getValue()));
|
||||||
|
} catch (ParseException ex) {
|
||||||
|
logger.error(ex.getMessage());
|
||||||
|
serverPortField.setText("null");
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
element.setText(String.valueOf(socket.getPort()));
|
||||||
|
}
|
||||||
|
config.add(element);
|
||||||
|
|
||||||
|
element = new Element("bound");
|
||||||
|
if (socket == null) {
|
||||||
|
element.setText(String.valueOf(false));
|
||||||
|
} else {
|
||||||
|
element.setText(String.valueOf(socket.isBound()));
|
||||||
|
}
|
||||||
|
config.add(element);
|
||||||
|
|
||||||
|
return config;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Collection<Element> getConfigXML() {
|
public boolean setConfigXML(Collection<Element> configXML, boolean visAvailable) {
|
||||||
return null;
|
String host = null;
|
||||||
|
Integer port = null;
|
||||||
|
boolean bound = false;
|
||||||
|
|
||||||
|
for (Element element : configXML) {
|
||||||
|
switch (element.getName()) {
|
||||||
|
case "host":
|
||||||
|
host = element.getText();
|
||||||
|
break;
|
||||||
|
case "port":
|
||||||
|
port = Integer.parseInt(element.getText());
|
||||||
|
break;
|
||||||
|
case "bound":
|
||||||
|
bound = Boolean.parseBoolean(element.getText());
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
logger.warn("Unknwon config element: " + element.getName());
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// XXX binding might fail if server not configured yet
|
||||||
|
if (Cooja.isVisualized()) {
|
||||||
|
if (host != null) {
|
||||||
|
serverHostField.setText(host);
|
||||||
|
}
|
||||||
|
if (port != null) {
|
||||||
|
serverPortField.setText(String.valueOf(port));
|
||||||
|
}
|
||||||
|
if (bound) {
|
||||||
|
serverSelectButton.doClick();
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// if bound and all set up, start client
|
||||||
|
if (host != null && port != null) {
|
||||||
|
startClient(host, port);
|
||||||
|
} else {
|
||||||
|
logger.error("Client not started due to incomplete configuration");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
private void cleanup() {
|
private void cleanup() {
|
||||||
serialPort.deleteSerialDataObserver(serialDataObserver);
|
serialPort.deleteSerialDataObserver(serialDataObserver);
|
||||||
|
|
|
@ -46,6 +46,7 @@ import java.net.ServerSocket;
|
||||||
import java.net.Socket;
|
import java.net.Socket;
|
||||||
import java.text.NumberFormat;
|
import java.text.NumberFormat;
|
||||||
import java.text.ParseException;
|
import java.text.ParseException;
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.LinkedList;
|
import java.util.LinkedList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
@ -103,7 +104,7 @@ public class SerialSocketServer extends VisPlugin implements MotePlugin {
|
||||||
private JLabel socketToMoteLabel;
|
private JLabel socketToMoteLabel;
|
||||||
private JLabel moteToSocketLabel;
|
private JLabel moteToSocketLabel;
|
||||||
private JLabel socketStatusLabel;
|
private JLabel socketStatusLabel;
|
||||||
private JFormattedTextField serverPortField;
|
private JFormattedTextField listenPortField;
|
||||||
private JButton serverStartButton;
|
private JButton serverStartButton;
|
||||||
|
|
||||||
private int inBytes = 0, outBytes = 0;
|
private int inBytes = 0, outBytes = 0;
|
||||||
|
@ -144,12 +145,12 @@ public class SerialSocketServer extends VisPlugin implements MotePlugin {
|
||||||
|
|
||||||
NumberFormat nf = NumberFormat.getIntegerInstance();
|
NumberFormat nf = NumberFormat.getIntegerInstance();
|
||||||
nf.setGroupingUsed(false);
|
nf.setGroupingUsed(false);
|
||||||
serverPortField = new JFormattedTextField(new NumberFormatter(nf));
|
listenPortField = new JFormattedTextField(new NumberFormatter(nf));
|
||||||
serverPortField.setColumns(5);
|
listenPortField.setColumns(5);
|
||||||
serverPortField.setText(String.valueOf(SERVER_DEFAULT_PORT));
|
listenPortField.setText(String.valueOf(SERVER_DEFAULT_PORT));
|
||||||
c.gridx++;
|
c.gridx++;
|
||||||
c.weightx = 0.0;
|
c.weightx = 0.0;
|
||||||
socketPanel.add(serverPortField, c);
|
socketPanel.add(listenPortField, c);
|
||||||
|
|
||||||
serverStartButton = new JButton("Start") { // Button for label toggeling
|
serverStartButton = new JButton("Start") { // Button for label toggeling
|
||||||
private final String altString = "Stop";
|
private final String altString = "Stop";
|
||||||
|
@ -235,11 +236,11 @@ public class SerialSocketServer extends VisPlugin implements MotePlugin {
|
||||||
public void actionPerformed(ActionEvent e) {
|
public void actionPerformed(ActionEvent e) {
|
||||||
if (e.getActionCommand().equals("Start")) {
|
if (e.getActionCommand().equals("Start")) {
|
||||||
try {
|
try {
|
||||||
serverPortField.commitEdit();
|
listenPortField.commitEdit();
|
||||||
} catch (ParseException ex) {
|
} catch (ParseException ex) {
|
||||||
java.util.logging.Logger.getLogger(SerialSocketClient.class.getName()).log(Level.SEVERE, null, ex);
|
java.util.logging.Logger.getLogger(SerialSocketClient.class.getName()).log(Level.SEVERE, null, ex);
|
||||||
}
|
}
|
||||||
startServer(((Long) serverPortField.getValue()).intValue());
|
startServer(((Long) listenPortField.getValue()).intValue());
|
||||||
} else {
|
} else {
|
||||||
stopServer();
|
stopServer();
|
||||||
}
|
}
|
||||||
|
@ -268,7 +269,7 @@ public class SerialSocketServer extends VisPlugin implements MotePlugin {
|
||||||
System.out.println("onServerStarted");
|
System.out.println("onServerStarted");
|
||||||
socketStatusLabel.setForeground(COLOR_NEUTRAL);
|
socketStatusLabel.setForeground(COLOR_NEUTRAL);
|
||||||
socketStatusLabel.setText("Listening on port " + String.valueOf(port));
|
socketStatusLabel.setText("Listening on port " + String.valueOf(port));
|
||||||
serverPortField.setEnabled(false);
|
listenPortField.setEnabled(false);
|
||||||
serverStartButton.setText("Stop");
|
serverStartButton.setText("Stop");
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
@ -307,7 +308,7 @@ public class SerialSocketServer extends VisPlugin implements MotePlugin {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
serverPortField.setEnabled(true);
|
listenPortField.setEnabled(true);
|
||||||
serverStartButton.setText("Start");
|
serverStartButton.setText("Start");
|
||||||
socketStatusLabel.setForeground(COLOR_NEUTRAL);
|
socketStatusLabel.setForeground(COLOR_NEUTRAL);
|
||||||
socketStatusLabel.setText("Idle");
|
socketStatusLabel.setText("Idle");
|
||||||
|
@ -494,13 +495,72 @@ public class SerialSocketServer extends VisPlugin implements MotePlugin {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean setConfigXML(Collection<Element> configXML, boolean visAvailable) {
|
public Collection<Element> getConfigXML() {
|
||||||
return true;
|
List<Element> config = new ArrayList<>();
|
||||||
|
Element element;
|
||||||
|
|
||||||
|
// XXX isVisualized guards?
|
||||||
|
|
||||||
|
element = new Element("port");
|
||||||
|
if (serverSocket == null || !serverSocket.isBound()) {
|
||||||
|
try {
|
||||||
|
listenPortField.commitEdit();
|
||||||
|
element.setText(String.valueOf((Long) listenPortField.getValue()));
|
||||||
|
} catch (ParseException ex) {
|
||||||
|
logger.error(ex.getMessage());
|
||||||
|
listenPortField.setText("null");
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
element.setText(String.valueOf(serverSocket.getLocalPort()));
|
||||||
|
}
|
||||||
|
config.add(element);
|
||||||
|
|
||||||
|
element = new Element("bound");
|
||||||
|
if (serverSocket == null) {
|
||||||
|
element.setText(String.valueOf(false));
|
||||||
|
} else {
|
||||||
|
element.setText(String.valueOf(!serverSocket.isClosed()));
|
||||||
|
}
|
||||||
|
config.add(element);
|
||||||
|
|
||||||
|
return config;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Collection<Element> getConfigXML() {
|
public boolean setConfigXML(Collection<Element> configXML, boolean visAvailable) {
|
||||||
return null;
|
Integer port = null;
|
||||||
|
boolean bound = false;
|
||||||
|
|
||||||
|
for (Element element : configXML) {
|
||||||
|
switch (element.getName()) {
|
||||||
|
case "port":
|
||||||
|
port = Integer.parseInt(element.getText());
|
||||||
|
break;
|
||||||
|
case "bound":
|
||||||
|
bound = Boolean.parseBoolean(element.getText());
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
logger.warn("Unknwon config element: " + element.getName());
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (Cooja.isVisualized()) {
|
||||||
|
if (port != null) {
|
||||||
|
listenPortField.setText(String.valueOf(port));
|
||||||
|
}
|
||||||
|
if (bound) {
|
||||||
|
serverStartButton.doClick();
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// if bound and all set up, start client
|
||||||
|
if (port != null) {
|
||||||
|
startServer(port);
|
||||||
|
} else {
|
||||||
|
logger.error("Server not started due to incomplete configuration");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void cleanupClient() {
|
private void cleanupClient() {
|
||||||
|
|
Loading…
Reference in a new issue