Skip to content

Commit

Permalink
[cooja/serialsocket] Support for xml import/export
Browse files Browse the repository at this point in the history
  • Loading branch information
Enrico Joerns committed Aug 28, 2014
1 parent 6c0e7ae commit dea0349
Show file tree
Hide file tree
Showing 2 changed files with 155 additions and 17 deletions.
Expand Up @@ -45,11 +45,13 @@
import java.net.Socket;
import java.text.NumberFormat;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;
import java.util.logging.Level;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
Expand Down Expand Up @@ -426,16 +428,92 @@ public void run() {
});
incomingDataThread.start();
}

@Override
public Collection<Element> getConfigXML() {
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
public boolean setConfigXML(Collection<Element> configXML, boolean visAvailable) {
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;
}

@Override
public Collection<Element> getConfigXML() {
return null;
}

private void cleanup() {
serialPort.deleteSerialDataObserver(serialDataObserver);
Expand Down
Expand Up @@ -46,6 +46,7 @@
import java.net.Socket;
import java.text.NumberFormat;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
Expand Down Expand Up @@ -103,7 +104,7 @@ public class SerialSocketServer extends VisPlugin implements MotePlugin {
private JLabel socketToMoteLabel;
private JLabel moteToSocketLabel;
private JLabel socketStatusLabel;
private JFormattedTextField serverPortField;
private JFormattedTextField listenPortField;
private JButton serverStartButton;

private int inBytes = 0, outBytes = 0;
Expand Down Expand Up @@ -144,12 +145,12 @@ public SerialSocketServer(Mote mote, Simulation simulation, final Cooja gui) {

NumberFormat nf = NumberFormat.getIntegerInstance();
nf.setGroupingUsed(false);
serverPortField = new JFormattedTextField(new NumberFormatter(nf));
serverPortField.setColumns(5);
serverPortField.setText(String.valueOf(SERVER_DEFAULT_PORT));
listenPortField = new JFormattedTextField(new NumberFormatter(nf));
listenPortField.setColumns(5);
listenPortField.setText(String.valueOf(SERVER_DEFAULT_PORT));
c.gridx++;
c.weightx = 0.0;
socketPanel.add(serverPortField, c);
socketPanel.add(listenPortField, c);

serverStartButton = new JButton("Start") { // Button for label toggeling
private final String altString = "Stop";
Expand Down Expand Up @@ -235,11 +236,11 @@ public Dimension getPreferredSize() {
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("Start")) {
try {
serverPortField.commitEdit();
listenPortField.commitEdit();
} catch (ParseException 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 {
stopServer();
}
Expand Down Expand Up @@ -268,7 +269,7 @@ public void run() {
System.out.println("onServerStarted");
socketStatusLabel.setForeground(COLOR_NEUTRAL);
socketStatusLabel.setText("Listening on port " + String.valueOf(port));
serverPortField.setEnabled(false);
listenPortField.setEnabled(false);
serverStartButton.setText("Stop");
}
});
Expand Down Expand Up @@ -307,7 +308,7 @@ public void onServerStopped() {

@Override
public void run() {
serverPortField.setEnabled(true);
listenPortField.setEnabled(true);
serverStartButton.setText("Start");
socketStatusLabel.setForeground(COLOR_NEUTRAL);
socketStatusLabel.setText("Idle");
Expand Down Expand Up @@ -494,13 +495,72 @@ public void run() {
}

@Override
public boolean setConfigXML(Collection<Element> configXML, boolean visAvailable) {
return true;
public Collection<Element> getConfigXML() {
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
public Collection<Element> getConfigXML() {
return null;
public boolean setConfigXML(Collection<Element> configXML, boolean visAvailable) {
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() {
Expand Down

0 comments on commit dea0349

Please sign in to comment.