[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++;
|
c.gridx++;
|
||||||
serverSelectPanel.add(serverPortField, c);
|
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.gridx++;
|
||||||
c.weightx = 0.1;
|
c.weightx = 0.1;
|
||||||
c.anchor = GridBagConstraints.EAST;
|
c.anchor = GridBagConstraints.EAST;
|
||||||
|
@ -232,11 +244,16 @@ public class SerialSocketClient extends VisPlugin implements MotePlugin {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void actionPerformed(ActionEvent e) {
|
public void actionPerformed(ActionEvent e) {
|
||||||
try {
|
if (e.getActionCommand().equals("Connect")) {
|
||||||
serverPortField.commitEdit();
|
try {
|
||||||
startClient(serverHostField.getText(), ((Long) serverPortField.getValue()).intValue());
|
serverPortField.commitEdit();
|
||||||
} catch (ParseException ex) {
|
startClient(serverHostField.getText(), ((Long) serverPortField.getValue()).intValue());
|
||||||
logger.error(ex);
|
} 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);
|
socketStatusLabel.setForeground(ST_COLOR_CONNECTED);
|
||||||
serverHostField.setEnabled(false);
|
serverHostField.setEnabled(false);
|
||||||
serverPortField.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");
|
socketStatusLabel.setText("Disconnected");
|
||||||
serverHostField.setEnabled(true);
|
serverHostField.setEnabled(true);
|
||||||
serverPortField.setEnabled(true);
|
serverPortField.setEnabled(true);
|
||||||
serverSelectButton.setEnabled(true);
|
serverSelectButton.setText("Connect");
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -386,13 +403,13 @@ public class SerialSocketClient extends VisPlugin implements MotePlugin {
|
||||||
try {
|
try {
|
||||||
numRead = in.read(data);
|
numRead = in.read(data);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
logger.error(e.getMessage());
|
logger.info(e.getMessage());
|
||||||
numRead = -1;
|
numRead = -1;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (numRead >= 0) {
|
if (numRead >= 0) {
|
||||||
for (int i=0; i < numRead; i++) {
|
for (int i = 0; i < numRead; i++) {
|
||||||
serialPort.writeByte(data[i]);
|
serialPort.writeByte(data[i]);
|
||||||
}
|
}
|
||||||
inBytes += numRead;
|
inBytes += numRead;
|
||||||
|
@ -401,8 +418,8 @@ public class SerialSocketClient extends VisPlugin implements MotePlugin {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.warn("Incoming data thread shut down");
|
logger.info("Incoming data thread shut down");
|
||||||
cleanup();
|
cleanup();
|
||||||
notifyClientDisconnected();
|
notifyClientDisconnected();
|
||||||
}
|
}
|
||||||
|
|
|
@ -151,7 +151,19 @@ public class SerialSocketServer extends VisPlugin implements MotePlugin {
|
||||||
c.weightx = 0.0;
|
c.weightx = 0.0;
|
||||||
socketPanel.add(serverPortField, c);
|
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.gridx++;
|
||||||
c.weightx = 0.1;
|
c.weightx = 0.1;
|
||||||
c.anchor = GridBagConstraints.EAST;
|
c.anchor = GridBagConstraints.EAST;
|
||||||
|
@ -221,12 +233,16 @@ public class SerialSocketServer extends VisPlugin implements MotePlugin {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void actionPerformed(ActionEvent e) {
|
public void actionPerformed(ActionEvent e) {
|
||||||
try {
|
if (e.getActionCommand().equals("Start")) {
|
||||||
serverPortField.commitEdit();
|
try {
|
||||||
} catch (ParseException ex) {
|
serverPortField.commitEdit();
|
||||||
java.util.logging.Logger.getLogger(SerialSocketClient.class.getName()).log(Level.SEVERE, null, ex);
|
} 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
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
|
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));
|
||||||
serverStartButton.setEnabled(false);
|
|
||||||
serverPortField.setEnabled(false);
|
serverPortField.setEnabled(false);
|
||||||
|
serverStartButton.setText("Stop");
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -290,8 +307,8 @@ public class SerialSocketServer extends VisPlugin implements MotePlugin {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
serverStartButton.setEnabled(true);
|
|
||||||
serverPortField.setEnabled(true);
|
serverPortField.setEnabled(true);
|
||||||
|
serverStartButton.setText("Start");
|
||||||
socketStatusLabel.setForeground(COLOR_NEUTRAL);
|
socketStatusLabel.setForeground(COLOR_NEUTRAL);
|
||||||
socketStatusLabel.setText("Idle");
|
socketStatusLabel.setText("Idle");
|
||||||
}
|
}
|
||||||
|
@ -381,16 +398,16 @@ public class SerialSocketServer extends VisPlugin implements MotePlugin {
|
||||||
try {
|
try {
|
||||||
// wait for next client
|
// wait for next client
|
||||||
Socket candidateSocket = serverSocket.accept();
|
Socket candidateSocket = serverSocket.accept();
|
||||||
|
|
||||||
// reject connection if already one client connected
|
// reject connection if already one client connected
|
||||||
if (clientSocket != null && !clientSocket.isClosed()) {
|
if (clientSocket != null && !clientSocket.isClosed()) {
|
||||||
logger.info("Refused connection of client " + candidateSocket.getInetAddress());
|
logger.info("Refused connection of client " + candidateSocket.getInetAddress());
|
||||||
candidateSocket.close();
|
candidateSocket.close();
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
clientSocket = candidateSocket;
|
clientSocket = candidateSocket;
|
||||||
|
|
||||||
in = new DataInputStream(clientSocket.getInputStream());
|
in = new DataInputStream(clientSocket.getInputStream());
|
||||||
out = new DataOutputStream(clientSocket.getOutputStream());
|
out = new DataOutputStream(clientSocket.getOutputStream());
|
||||||
out.flush();
|
out.flush();
|
||||||
|
@ -416,14 +433,14 @@ public class SerialSocketServer extends VisPlugin implements MotePlugin {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
inBytes = outBytes = 0;
|
inBytes = outBytes = 0;
|
||||||
|
|
||||||
logger.info("Client connected: " + clientSocket.getInetAddress());
|
logger.info("Client connected: " + clientSocket.getInetAddress());
|
||||||
notifyClientConnected(clientSocket);
|
notifyClientConnected(clientSocket);
|
||||||
|
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
logger.fatal("Listening thread shut down: " + e.getMessage());
|
logger.info("Listening thread shut down: " + e.getMessage());
|
||||||
try {
|
try {
|
||||||
serverSocket.close();
|
serverSocket.close();
|
||||||
} catch (IOException ex) {
|
} catch (IOException ex) {
|
||||||
|
@ -436,6 +453,17 @@ public class SerialSocketServer extends VisPlugin implements MotePlugin {
|
||||||
}
|
}
|
||||||
}.start();
|
}.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) {
|
private void startSocketReadThread(final DataInputStream in) {
|
||||||
/* Forward data: virtual port -> mote */
|
/* Forward data: virtual port -> mote */
|
||||||
|
@ -454,7 +482,7 @@ public class SerialSocketServer extends VisPlugin implements MotePlugin {
|
||||||
try {
|
try {
|
||||||
numRead = in.read(data);
|
numRead = in.read(data);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
logger.error(e.getMessage());
|
logger.info(e.getMessage());
|
||||||
numRead = -1;
|
numRead = -1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue