Added connection screen to allow users to select available hosts from a preconfigured list.

- Added ConnectionController for initial client setup
- Added RaceConnection class for socket data and checking
- Configures StartController with a socket drawn from a host table

#story[782]
This commit is contained in:
Connor Taylor-Brown
2017-05-03 03:40:36 +12:00
parent ad58407021
commit ac4fbda848
9 changed files with 236 additions and 111 deletions
+2 -2
View File
@@ -22,9 +22,9 @@ public class App extends Application {
}
public void start(Stage stage) throws Exception {
FXMLLoader loader = new FXMLLoader(getClass().getResource("/scenes/connect.fxml"));
FXMLLoader loader = new FXMLLoader(getClass().getResource("/scenes/main.fxml"));
Parent root = loader.load();
Scene scene = new Scene(root, 600, 400);
Scene scene = new Scene(root, 1200, 800);
stage.setScene(scene);
stage.show();
}
@@ -1,10 +1,19 @@
package seng302.Controllers;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.layout.AnchorPane;
import seng302.RaceConnection;
import javax.swing.text.TableView;
import java.io.IOException;
import java.net.Socket;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;
/**
@@ -12,10 +21,53 @@ import java.util.ResourceBundle;
*/
public class ConnectionController extends Controller {
@FXML
TableView connectionTable;
private AnchorPane connectionWrapper;
@FXML
private TableView connectionTable;
@FXML
private TableColumn<RaceConnection, String> hostnameColumn;
@FXML
private TableColumn<RaceConnection, String> statusColumn;
@FXML
private Button connectButton;
private List<RaceConnection> connections;
@Override
public void initialize(URL location, ResourceBundle resources) {
// TODO - replace with config file
connections = new ArrayList<>();
connections.add(new RaceConnection("livedata.americascup.com", 4941));
connections.add(new RaceConnection("localhost", 4942));
connectionTable.setItems(FXCollections.observableArrayList(connections));
hostnameColumn.setCellValueFactory(cellData -> cellData.getValue().hostnameProperty());
statusColumn.setCellValueFactory(cellData -> cellData.getValue().statusProperty());
connectionTable.getSelectionModel().selectedItemProperty().addListener((obs, prev, curr) -> {
if (curr != null && ((RaceConnection)curr).check()) connectButton.setDisable(false);
else connectButton.setDisable(true);
});
}
/**
* Sets current status of all connections.
*/
public void checkConnections() {
for(RaceConnection connection: connections) {
connection.check();
}
}
/**
* Connects to host currently selected in table. Button enabled only if host is ready.
*/
public void connectSocket() {
try{
RaceConnection connection = (RaceConnection)connectionTable.getSelectionModel().getSelectedItem();
Socket socket = new Socket(connection.getHostname(), connection.getPort());
connectionWrapper.setVisible(false);
parent.enterLobby(socket);
} catch (IOException e) { /* Never reached */ }
}
}
@@ -5,6 +5,7 @@ import javafx.scene.layout.AnchorPane;
import seng302.RaceDataSource;
import seng302.VisualiserInput;
import java.net.Socket;
import java.net.URL;
import java.util.ResourceBundle;
@@ -23,6 +24,9 @@ public class MainController extends Controller {
raceController.startRace(visualiserInput);
}
public void enterLobby(Socket socket) {
startController.enterLobby(socket);
}
/**
* Main Controller for the applications will house the menu and the displayed pane.
@@ -12,24 +12,18 @@ import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.GridPane;
import org.xml.sax.SAXException;
import seng302.Mock.*;
import seng302.Model.Boat;
import seng302.Model.RaceClock;
import seng302.VisualiserInput;
import seng302.RaceDataSource;
import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
import java.net.URL;
import java.text.ParseException;
import java.util.List;
import java.util.Observable;
import java.util.Observer;
import java.util.ResourceBundle;
import java.util.stream.Stream;
/**
* Created by esa46 on 6/04/17.
@@ -88,29 +82,8 @@ public class StartController extends Controller implements Observer {
@Override
public void initialize(URL location, ResourceBundle resources){
raceData = null;
try {
raceData = new StreamedCourse();
raceData.addObserver(this);
// visualiserInput = new VisualiserInput(new Socket("livedata.americascup.com", 4941), raceData);
// visualiserInput = new VisualiserInput(new Socket("132.181.15.116", 8085), raceData);
visualiserInput = new VisualiserInput(new Socket(InetAddress.getLocalHost(), 4942), raceData);
new Thread(visualiserInput).start();
// StreamedCourse streamedCourse = new StreamedCourse(new BoatXMLReader("mockXML/boatXML/boatTest.xml"));
// streamedCourse.setStreamedCourseXMLReader(new StreamedCourseXMLReader("mockXML/raceXML/raceTest.xml"));
// streamedCourse.setRegattaXMLReader(new RegattaXMLReader("mockXML/regattaXML/regattaTest.xml"));
// raceData = streamedCourse;
// }catch (ParseException e) {
// e.printStackTrace();
// } catch (ParserConfigurationException e) {
// e.printStackTrace();
// } catch (SAXException e) {
// e.printStackTrace();
// } catch (StreamedCourseXMLException e) {
// e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
raceData = new StreamedCourse();
raceData.addObserver(this);
}
public AnchorPane startWrapper(){
@@ -204,4 +177,18 @@ public class StartController extends Controller implements Observer {
}
}
}
/**
* Show starting information for a race given a socket.
* @param socket network source of information
*/
public void enterLobby(Socket socket) {
startWrapper.setVisible(true);
try {
visualiserInput = new VisualiserInput(socket, raceData);
new Thread(visualiserInput).start();
} catch (IOException e) {
e.printStackTrace();
}
}
}
@@ -1,14 +1,53 @@
package seng302;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import java.io.IOException;
import java.net.Socket;
/**
* Created by cbt24 on 3/05/17.
*/
public class RaceConnection {
private String hostname;
private StringProperty hostname;
private int port;
private StringProperty status;
public RaceConnection(String hostname, int port) {
this.hostname = hostname;
this.hostname = new SimpleStringProperty(hostname);
this.port = port;
this.status = new SimpleStringProperty("");
check();
}
/**
* Tries to create a socket to hostname and port, indicates status after test.
*/
@SuppressWarnings("unused")
public boolean check() {
try(Socket s = new Socket(hostname.get(), port)) {
status.set("Ready");
return true;
} catch (IOException e) {}
status.set("Offline");
return false;
}
public String getHostname() {
return hostname.get();
}
public StringProperty hostnameProperty() {
return hostname;
}
public int getPort() {
return port;
}
public StringProperty statusProperty() {
return status;
}
}