[cooja/serialsocket] Allow to stop server / disconnect client

This commit is contained in:
Enrico Joerns 2014-04-14 14:24:55 +02:00
parent f419274050
commit 6c0e7ae15e
2 changed files with 71 additions and 26 deletions

View file

@ -156,7 +156,19 @@ public class SerialSocketClient extends VisPlugin implements MotePlugin {
c.gridx++;
serverSelectPanel.add(serverPortField, c);
serverSelectButton = new JButton("Connect");
serverSelectButton = new JButton("Connect") { // Button for label toggeling
private final String altString = "Disconnect";
@Override
public Dimension getPreferredSize() {
String origText = getText();
Dimension origDim = super.getPreferredSize();
setText(altString);
Dimension altDim = super.getPreferredSize();
setText(origText);
return new Dimension(Math.max(origDim.width, altDim.width), origDim.height);
}
};
c.gridx++;
c.weightx = 0.1;
c.anchor = GridBagConstraints.EAST;
@ -232,12 +244,17 @@ public class SerialSocketClient extends VisPlugin implements MotePlugin {
@Override
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("Connect")) {
try {
serverPortField.commitEdit();
startClient(serverHostField.getText(), ((Long) serverPortField.getValue()).intValue());
} catch (ParseException ex) {
logger.error(ex);
}
} else {
// close socket
cleanup();
}
}
});
@ -288,7 +305,7 @@ public class SerialSocketClient extends VisPlugin implements MotePlugin {
socketStatusLabel.setForeground(ST_COLOR_CONNECTED);
serverHostField.setEnabled(false);
serverPortField.setEnabled(false);
serverSelectButton.setEnabled(false);
serverSelectButton.setText("Disconnect");
}
});
}
@ -302,7 +319,7 @@ public class SerialSocketClient extends VisPlugin implements MotePlugin {
socketStatusLabel.setText("Disconnected");
serverHostField.setEnabled(true);
serverPortField.setEnabled(true);
serverSelectButton.setEnabled(true);
serverSelectButton.setText("Connect");
}
});
}
@ -386,7 +403,7 @@ public class SerialSocketClient extends VisPlugin implements MotePlugin {
try {
numRead = in.read(data);
} catch (IOException e) {
logger.error(e.getMessage());
logger.info(e.getMessage());
numRead = -1;
continue;
}
@ -402,7 +419,7 @@ public class SerialSocketClient extends VisPlugin implements MotePlugin {
}
}
logger.warn("Incoming data thread shut down");
logger.info("Incoming data thread shut down");
cleanup();
notifyClientDisconnected();
}

View file

@ -151,7 +151,19 @@ public class SerialSocketServer extends VisPlugin implements MotePlugin {
c.weightx = 0.0;
socketPanel.add(serverPortField, c);
serverStartButton = new JButton("Start");
serverStartButton = new JButton("Start") { // Button for label toggeling
private final String altString = "Stop";
@Override
public Dimension getPreferredSize() {
String origText = getText();
Dimension origDim = super.getPreferredSize();
setText(altString);
Dimension altDim = super.getPreferredSize();
setText(origText);
return new Dimension(Math.max(origDim.width, altDim.width), origDim.height);
}
};
c.gridx++;
c.weightx = 0.1;
c.anchor = GridBagConstraints.EAST;
@ -221,12 +233,16 @@ public class SerialSocketServer extends VisPlugin implements MotePlugin {
@Override
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("Start")) {
try {
serverPortField.commitEdit();
} catch (ParseException ex) {
java.util.logging.Logger.getLogger(SerialSocketClient.class.getName()).log(Level.SEVERE, null, ex);
}
startServer(((Long) serverPortField.getValue()).intValue());
} else {
stopServer();
}
}
});
@ -249,10 +265,11 @@ public class SerialSocketServer extends VisPlugin implements MotePlugin {
@Override
public void run() {
System.out.println("onServerStarted");
socketStatusLabel.setForeground(COLOR_NEUTRAL);
socketStatusLabel.setText("Listening on port " + String.valueOf(port));
serverStartButton.setEnabled(false);
serverPortField.setEnabled(false);
serverStartButton.setText("Stop");
}
});
}
@ -290,8 +307,8 @@ public class SerialSocketServer extends VisPlugin implements MotePlugin {
@Override
public void run() {
serverStartButton.setEnabled(true);
serverPortField.setEnabled(true);
serverStartButton.setText("Start");
socketStatusLabel.setForeground(COLOR_NEUTRAL);
socketStatusLabel.setText("Idle");
}
@ -423,7 +440,7 @@ public class SerialSocketServer extends VisPlugin implements MotePlugin {
notifyClientConnected(clientSocket);
} catch (IOException e) {
logger.fatal("Listening thread shut down: " + e.getMessage());
logger.info("Listening thread shut down: " + e.getMessage());
try {
serverSocket.close();
} catch (IOException ex) {
@ -437,6 +454,17 @@ public class SerialSocketServer extends VisPlugin implements MotePlugin {
}.start();
}
/**
* Stops server by closing server listen socket.
*/
public void stopServer() {
try {
serverSocket.close();
} catch (IOException ex) {
logger.error(ex);
}
}
private void startSocketReadThread(final DataInputStream in) {
/* Forward data: virtual port -> mote */
Thread incomingDataThread = new Thread(new Runnable() {
@ -454,7 +482,7 @@ public class SerialSocketServer extends VisPlugin implements MotePlugin {
try {
numRead = in.read(data);
} catch (IOException e) {
logger.error(e.getMessage());
logger.info(e.getMessage());
numRead = -1;
}
}