Check method moved to new class called ping so that refresh will be called on new thread.
#story[1087]
This commit is contained in:
@@ -48,10 +48,13 @@ public class LobbyController extends Controller {
|
||||
statusColumn.setCellValueFactory(cellData -> cellData.getValue().statusProperty());
|
||||
|
||||
lobbyTable.getSelectionModel().selectedItemProperty().addListener((obs, prev, curr) -> {
|
||||
if (curr != null && ((RaceConnection)curr).check())
|
||||
{joinGameBtn.setDisable(false);}
|
||||
|
||||
else {joinGameBtn.setDisable(true);}
|
||||
if (curr != null && curr.statusProperty().getValue().equals("Ready")) {
|
||||
joinGameBtn.setDisable(false);
|
||||
}
|
||||
else {
|
||||
joinGameBtn.setDisable(true);
|
||||
System.out.println(curr.statusProperty().getValue());
|
||||
}
|
||||
});
|
||||
joinGameBtn.setDisable(true);
|
||||
}
|
||||
@@ -60,6 +63,14 @@ public class LobbyController extends Controller {
|
||||
for(RaceConnection connection: connections) {
|
||||
connection.check();
|
||||
}
|
||||
|
||||
try {
|
||||
if (lobbyTable.getSelectionModel().getSelectedItem().statusProperty().getValue().equals("Ready")) {
|
||||
joinGameBtn.setDisable(false);
|
||||
} else {
|
||||
joinGameBtn.setDisable(true);
|
||||
}
|
||||
} catch (Exception e){}
|
||||
}
|
||||
|
||||
public void connectSocket() {
|
||||
@@ -68,7 +79,9 @@ public class LobbyController extends Controller {
|
||||
Socket socket = new Socket(connection.getHostname(), connection.getPort());
|
||||
lobbyWrapper.setVisible(false);
|
||||
parent.enterLobby(socket);
|
||||
} catch (IOException e) { /* Never reached */ }
|
||||
} catch (IOException e) { /* Never reached */
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public AnchorPane startWrapper(){
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
package visualiser.model;
|
||||
|
||||
import javafx.beans.property.SimpleStringProperty;
|
||||
import javafx.beans.property.StringProperty;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.Socket;
|
||||
|
||||
/**
|
||||
* Created by David on 26/07/2017.
|
||||
*/
|
||||
public class Ping implements Runnable {
|
||||
|
||||
private final String hostname;
|
||||
private final int port;
|
||||
private final RaceConnection rc;
|
||||
|
||||
public Ping(String hostname, int port, RaceConnection rc){
|
||||
this.hostname = hostname;
|
||||
this.port = port;
|
||||
this.rc = rc;
|
||||
}
|
||||
|
||||
public boolean pingPort(){
|
||||
//TODO the connection needs to be moved to its own thread, so it doesn't block fx thread.
|
||||
InetSocketAddress i = new InetSocketAddress(hostname, port);
|
||||
try (Socket s = new Socket()){
|
||||
s.connect(i, 750);//TODO this should be at least a second or two, once moved to its own thread
|
||||
s.shutdownInput();
|
||||
s.shutdownOutput();
|
||||
s.close();
|
||||
rc.statusProperty().set("Ready");
|
||||
//System.out.println(String.valueOf(s.isClosed()));
|
||||
return true;
|
||||
} catch (IOException e) {
|
||||
rc.statusProperty().set("Offline");
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
pingPort();
|
||||
}
|
||||
}
|
||||
@@ -35,21 +35,9 @@ public class RaceConnection {
|
||||
* @return true if socket can connect
|
||||
*/
|
||||
public boolean check() {
|
||||
//TODO the connection needs to be moved to its own thread, so it doesn't block fx thread.
|
||||
InetSocketAddress i = new InetSocketAddress(hostname.get(), port);
|
||||
try (Socket s = new Socket()){
|
||||
s.connect(i, 750);//TODO this should be at least a second or two, once moved to its own thread
|
||||
status.set("Ready");
|
||||
s.shutdownInput();
|
||||
s.shutdownOutput();
|
||||
s.close();
|
||||
//System.out.println(String.valueOf(s.isClosed()));
|
||||
Ping ping = new Ping(hostname.get(), port, this);
|
||||
new Thread(ping).start();
|
||||
return true;
|
||||
} catch (IOException e) {
|
||||
}
|
||||
|
||||
status.set("Offline");
|
||||
return false;
|
||||
}
|
||||
|
||||
public String getHostname() {
|
||||
|
||||
@@ -32,8 +32,6 @@ public abstract class ResizableCanvas extends Canvas {
|
||||
public abstract void draw();
|
||||
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public boolean isResizable() {
|
||||
return true;
|
||||
|
||||
Reference in New Issue
Block a user