[cooja/serialsocket] Allow to stop server / disconnect client
This commit is contained in:
parent
f419274050
commit
6c0e7ae15e
|
@ -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,11 +244,16 @@ public class SerialSocketClient extends VisPlugin implements MotePlugin {
|
|||
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
try {
|
||||
serverPortField.commitEdit();
|
||||
startClient(serverHostField.getText(), ((Long) serverPortField.getValue()).intValue());
|
||||
} catch (ParseException ex) {
|
||||
logger.error(ex);
|
||||
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,13 +403,13 @@ 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;
|
||||
}
|
||||
|
||||
if (numRead >= 0) {
|
||||
for (int i=0; i < numRead; i++) {
|
||||
for (int i = 0; i < numRead; i++) {
|
||||
serialPort.writeByte(data[i]);
|
||||
}
|
||||
inBytes += numRead;
|
||||
|
@ -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();
|
||||
}
|
||||
|
|
|
@ -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) {
|
||||
try {
|
||||
serverPortField.commitEdit();
|
||||
} catch (ParseException ex) {
|
||||
java.util.logging.Logger.getLogger(SerialSocketClient.class.getName()).log(Level.SEVERE, null, ex);
|
||||
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();
|
||||
}
|
||||
startServer(((Long) serverPortField.getValue()).intValue());
|
||||
}
|
||||
});
|
||||
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue