Issue-1677 Fix (#1688)

* Added timeout to control panel to variable server connection attempts

* Updated 127.0.0.1 to localhost
This commit is contained in:
Pherring04 2024-04-18 13:46:05 -05:00 committed by GitHub
parent 5065d96a15
commit 14708ca9cc
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 45 additions and 6 deletions

View File

@ -7,7 +7,11 @@ import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
import java.net.SocketAddress;
import java.net.InetSocketAddress;
import java.net.UnknownHostException;
import java.net.SocketTimeoutException;
/**
* a generic variable server client that provides for sending commands and
@ -54,12 +58,30 @@ public class VariableServerConnection implements AutoCloseable {
* @throws IOException IOException
* @throws SecurityException SecurityException
*/
public VariableServerConnection(String host, int port) throws UnknownHostException, IOException, SecurityException {
public VariableServerConnection(String host, int port) throws UnknownHostException, IOException, SecurityException, SocketTimeoutException {
socket = new Socket(host, port);
inputStream = new BufferedReader(new InputStreamReader(socket.getInputStream()));
outputStream = new DataOutputStream(new BufferedOutputStream(socket.getOutputStream()));
}
/**
* attempts to connect to the Variable Server on the given host and port with a timeout
*
* @param host Variable Server machine name
* @param port Variable Server port number
* @param timeout Time out in milliseconds
* @throws UnknownHostException UnknownHostException
* @throws IOException IOException
* @throws SecurityException SecurityException
*/
public VariableServerConnection(String host, int port, int timeout) throws UnknownHostException, IOException, SecurityException, SocketTimeoutException {
socket = new Socket();
SocketAddress addr = new InetSocketAddress(host, port);
socket.connect(addr, timeout);
inputStream = new BufferedReader(new InputStreamReader(socket.getInputStream()));
outputStream = new DataOutputStream(new BufferedOutputStream(socket.getOutputStream()));
}
/**
* sends the given command and any commands in the output stream's buffer
* to the Variable Server. The command is written to the output stream,

View File

@ -29,6 +29,7 @@ import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.MulticastSocket;
import java.net.UnknownHostException;
import java.net.SocketTimeoutException;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.channels.NotYetConnectedException;
@ -171,6 +172,10 @@ public class SimControlApplication extends TrickApplication implements PropertyC
private static String host;
private static int port = -1;
private static boolean isRestartOptionOn;
//True if an error was encountered during the attempt to connect to Variable Server during intialize()
private boolean errOnInitConnect = false;
//Time out when attempting to establish connection with Variable Server in milliseconds
private int varServerTimeout = 5000;
// The object of SimState that has Sim state data.
private SimState simState;
@ -405,22 +410,30 @@ public class SimControlApplication extends TrickApplication implements PropertyC
String errMsg = "Error: SimControlApplication:getInitializationPacket()";
try {
if (host != null && port != -1) {
commandSimcom = new VariableServerConnection(host, port);
commandSimcom = new VariableServerConnection(host, port, varServerTimeout);
} else {
commandSimcom = null;
}
} catch (UnknownHostException host_exception) {
/** The IP address of the host could not be determined. */
errMsg += "\n Unknown host \""+host+"\"";
errMsg += "\n Please use a valid host name (e.g. localhost)";
printErrorMessage(errMsg);
errMsg += "\n Please use a valid host name (e.g. localhost)";
errOnInitConnect = true;
printErrorMessage(errMsg);
} catch (SocketTimeoutException ste) {
/** Connection attempt timed out. */
errMsg += "\n Connection Timeout \""+host+"\"";
errMsg += "\n Please try a different host name (e.g. localhost)";
errOnInitConnect = true;
printErrorMessage(errMsg);
} catch (IOException ioe) {
/** Port number is unavailable, or there is no connection, etc. */
errMsg += "\n Invalid TCP/IP port number \""+port+"\"";
errMsg += "\n Please check the server and enter a proper port number!";
errMsg += "\n IOException ..." + ioe;
errMsg += "\n If there is no connection, please make sure SIM is up running properly!";
printErrorMessage(errMsg);
errOnInitConnect = true;
printErrorMessage(errMsg);
}
if (commandSimcom == null) {
@ -644,6 +657,10 @@ public class SimControlApplication extends TrickApplication implements PropertyC
view.setToolBar(createToolBar());
view.setStatusBar(createStatusBar());
if(errOnInitConnect && (runningSimList != null) ) {
runningSimList.addItem("localhost : " + port);
}
show(view);
}
@ -1396,7 +1413,7 @@ public class SimControlApplication extends TrickApplication implements PropertyC
// Reset the packet length or future messages will be clipped.
packet.setLength(buffer.length);
// version Trick 10 or later
if (info[7] != null && info[7].startsWith("10.")) {
if (info[7] != null) {
if (runningSimList != null) {
String hostPort = info[0] + " : " + info[1] + " (" + info[5] + " " + info[6] + ")";
if (!UIUtils.comboBoxContains((DefaultComboBoxModel)runningSimList.getModel(), hostPort)) {