[cooja/serialsocket] SerialSocketServer: Allow only a single client,

reject further
This commit is contained in:
Enrico Joerns 2014-04-13 21:36:38 +02:00
parent a18a7fa717
commit b31818521d

View file

@ -1,6 +1,7 @@
package org.contikios.cooja.serialsocket; package org.contikios.cooja.serialsocket;
/* /*
* Copyright (c) 2014, TU Braunschweig.
* Copyright (c) 2010, Swedish Institute of Computer Science. * Copyright (c) 2010, Swedish Institute of Computer Science.
* All rights reserved. * All rights reserved.
* *
@ -80,6 +81,7 @@ import org.contikios.cooja.interfaces.SerialPort;
* Socket to simulated serial port forwarder. Server version. * Socket to simulated serial port forwarder. Server version.
* *
* @author Fredrik Osterlind * @author Fredrik Osterlind
* @author Enrico Jorns
*/ */
@ClassDescription("Serial Socket (SERVER)") @ClassDescription("Serial Socket (SERVER)")
@PluginType(PluginType.MOTE_PLUGIN) @PluginType(PluginType.MOTE_PLUGIN)
@ -101,11 +103,12 @@ 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 JButton serverStartButton; private JButton serverStartButton;
private int inBytes = 0, outBytes = 0; private int inBytes = 0, outBytes = 0;
private ServerSocket server; private ServerSocket serverSocket;
private Socket clientSocket; private Socket clientSocket;
private DataInputStream in; private DataInputStream in;
private DataOutputStream out; private DataOutputStream out;
@ -135,14 +138,17 @@ public class SerialSocketServer extends VisPlugin implements MotePlugin {
JLabel label = new JLabel("Listen port: "); JLabel label = new JLabel("Listen port: ");
c.gridx = 0; c.gridx = 0;
c.gridy = 0; c.gridy = 0;
c.weightx = 0.1;
c.anchor = GridBagConstraints.EAST;
socketPanel.add(label, c); socketPanel.add(label, c);
NumberFormat nf = NumberFormat.getIntegerInstance(); NumberFormat nf = NumberFormat.getIntegerInstance();
nf.setGroupingUsed(false); nf.setGroupingUsed(false);
final JFormattedTextField serverPortField = new JFormattedTextField(new NumberFormatter(nf)); serverPortField = new JFormattedTextField(new NumberFormatter(nf));
serverPortField.setColumns(5); serverPortField.setColumns(5);
serverPortField.setText(String.valueOf(SERVER_DEFAULT_PORT)); serverPortField.setText(String.valueOf(SERVER_DEFAULT_PORT));
c.gridx++; c.gridx++;
c.weightx = 0.0;
socketPanel.add(serverPortField, c); socketPanel.add(serverPortField, c);
serverStartButton = new JButton("Start"); serverStartButton = new JButton("Start");
@ -205,7 +211,7 @@ public class SerialSocketServer extends VisPlugin implements MotePlugin {
label = new JLabel("Status: "); label = new JLabel("Status: ");
statusBarPanel.add(label); statusBarPanel.add(label);
socketStatusLabel = new JLabel("Not started"); socketStatusLabel = new JLabel("Idle");
socketStatusLabel.setForeground(Color.DARK_GRAY); socketStatusLabel.setForeground(Color.DARK_GRAY);
statusBarPanel.add(socketStatusLabel); statusBarPanel.add(socketStatusLabel);
@ -246,6 +252,7 @@ public class SerialSocketServer extends VisPlugin implements MotePlugin {
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); serverStartButton.setEnabled(false);
serverPortField.setEnabled(false);
} }
}); });
} }
@ -268,8 +275,11 @@ public class SerialSocketServer extends VisPlugin implements MotePlugin {
@Override @Override
public void run() { public void run() {
// XXX check why needed
if (serverSocket != null) {
socketStatusLabel.setForeground(COLOR_NEUTRAL); socketStatusLabel.setForeground(COLOR_NEUTRAL);
socketStatusLabel.setText("Listening on port " + String.valueOf(server.getLocalPort())); socketStatusLabel.setText("Listening on port " + String.valueOf(serverSocket.getLocalPort()));
}
} }
}); });
} }
@ -281,6 +291,7 @@ public class SerialSocketServer extends VisPlugin implements MotePlugin {
@Override @Override
public void run() { public void run() {
serverStartButton.setEnabled(true); serverStartButton.setEnabled(true);
serverPortField.setEnabled(true);
socketStatusLabel.setForeground(COLOR_NEUTRAL); socketStatusLabel.setForeground(COLOR_NEUTRAL);
socketStatusLabel.setText("Idle"); socketStatusLabel.setText("Idle");
} }
@ -300,7 +311,6 @@ public class SerialSocketServer extends VisPlugin implements MotePlugin {
} }
}); });
} }
} }
@ -355,7 +365,7 @@ public class SerialSocketServer extends VisPlugin implements MotePlugin {
*/ */
public void startServer(int port) { public void startServer(int port) {
try { try {
server = new ServerSocket(port); serverSocket = new ServerSocket(port);
logger.info("Listening on port: " + port); logger.info("Listening on port: " + port);
notifyServerStarted(port); notifyServerStarted(port);
} catch (IOException ex) { } catch (IOException ex) {
@ -367,10 +377,20 @@ public class SerialSocketServer extends VisPlugin implements MotePlugin {
new Thread() { new Thread() {
@Override @Override
public void run() { public void run() {
while (!server.isClosed()) { while (!serverSocket.isClosed()) {
try { try {
// wait for next client // wait for next client
clientSocket = server.accept(); Socket candidateSocket = serverSocket.accept();
// reject connection if already one client connected
if (clientSocket != null && !clientSocket.isClosed()) {
logger.info("Refused connection of client " + candidateSocket.getInetAddress());
candidateSocket.close();
continue;
}
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();
@ -405,7 +425,7 @@ public class SerialSocketServer extends VisPlugin implements MotePlugin {
} catch (IOException e) { } catch (IOException e) {
logger.fatal("Listening thread shut down: " + e.getMessage()); logger.fatal("Listening thread shut down: " + e.getMessage());
try { try {
server.close(); serverSocket.close();
} catch (IOException ex) { } catch (IOException ex) {
logger.error(ex); logger.error(ex);
} }
@ -486,13 +506,17 @@ public class SerialSocketServer extends VisPlugin implements MotePlugin {
} }
private boolean closed = false; private boolean closed = false;
@Override @Override
public void closePlugin() { public void closePlugin() {
closed = true; closed = true;
cleanupClient(); cleanupClient();
try { try {
server.close(); if (serverSocket != null) {
} catch (IOException e) { serverSocket.close();
}
} catch (IOException ex) {
logger.error(ex);
} }
} }