Merge remote-tracking branch 'remotes/origin/master' into minimap_test
This commit is contained in:
+3
-3
@@ -41,8 +41,8 @@ public class BoatsXMLMessageCommand implements Command {
|
||||
|
||||
@Override
|
||||
public void execute() {
|
||||
|
||||
visualiserRace.setBoatDataSource(boatDataSource);
|
||||
|
||||
if (boatDataSource.getSequenceNumber() > visualiserRace.getBoatDataSource().getSequenceNumber()) {
|
||||
visualiserRace.setBoatDataSource(boatDataSource);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+3
-1
@@ -38,7 +38,9 @@ public class RaceXMLMessageCommand implements Command {
|
||||
@Override
|
||||
public void execute() {
|
||||
|
||||
visualiserRace.setRaceDataSource(raceDataSource);
|
||||
if (raceDataSource.getSequenceNumber() > visualiserRace.getRaceDataSource().getSequenceNumber()) {
|
||||
visualiserRace.setRaceDataSource(raceDataSource);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
+3
-2
@@ -37,8 +37,9 @@ public class RegattaXMLMessageCommand implements Command {
|
||||
|
||||
@Override
|
||||
public void execute() {
|
||||
|
||||
visualiserRace.setRegattaDataSource(regattaDataSource);
|
||||
if (regattaDataSource.getSequenceNumber() > visualiserRace.getRegattaDataSource().getSequenceNumber()) {
|
||||
visualiserRace.setRegattaDataSource(regattaDataSource);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
+6
-3
@@ -34,17 +34,20 @@ public class XMLMessageCommandFactory {
|
||||
switch (message.getXmlMsgSubType()) {
|
||||
|
||||
case BOAT:
|
||||
BoatDataSource boatDataSource = new BoatXMLReader(message.getXmlMessage(), XMLFileType.Contents);
|
||||
BoatXMLReader boatDataSource = new BoatXMLReader(message.getXmlMessage(), XMLFileType.Contents);
|
||||
boatDataSource.setSequenceNumber(message.getSequenceNumber());
|
||||
return new BoatsXMLMessageCommand(boatDataSource, visualiserRace);
|
||||
|
||||
|
||||
case RACE:
|
||||
RaceDataSource raceDataSource = new RaceXMLReader(message.getXmlMessage(), XMLFileType.Contents);
|
||||
RaceXMLReader raceDataSource = new RaceXMLReader(message.getXmlMessage(), XMLFileType.Contents);
|
||||
raceDataSource.setSequenceNumber(message.getSequenceNumber());
|
||||
return new RaceXMLMessageCommand(raceDataSource, visualiserRace);
|
||||
|
||||
|
||||
case REGATTA:
|
||||
RegattaDataSource regattaDataSource = new RegattaXMLReader(message.getXmlMessage(), XMLFileType.Contents);
|
||||
RegattaXMLReader regattaDataSource = new RegattaXMLReader(message.getXmlMessage(), XMLFileType.Contents);
|
||||
regattaDataSource.setSequenceNumber(message.getSequenceNumber());
|
||||
return new RegattaXMLMessageCommand(regattaDataSource, visualiserRace);
|
||||
|
||||
|
||||
|
||||
@@ -8,8 +8,10 @@ import javafx.scene.image.Image;
|
||||
import javafx.scene.image.ImageView;
|
||||
import mock.app.Event;
|
||||
import mock.exceptions.EventConstructionException;
|
||||
import network.Messages.Enums.RequestToJoinEnum;
|
||||
import visualiser.app.App;
|
||||
import visualiser.app.MatchBrowserSingleton;
|
||||
import visualiser.network.HttpMatchBrowserHost;
|
||||
import visualiser.network.MatchBrowserInterface;
|
||||
|
||||
import java.io.IOException;
|
||||
@@ -64,8 +66,11 @@ public class HostGameController extends Controller {
|
||||
try {
|
||||
App.game = new Event(false, currentMapIndex);
|
||||
App.gameType = currentMapIndex;
|
||||
|
||||
HttpMatchBrowserHost matchBrowserHost = new HttpMatchBrowserHost();
|
||||
new Thread(matchBrowserHost).start();
|
||||
connectSocket("localhost", 4942);
|
||||
alertMatchBrowser();
|
||||
//alertMatchBrowser();
|
||||
|
||||
} catch (EventConstructionException e) {
|
||||
Logger.getGlobal().log(Level.SEVERE, "Could not create Event.", e);
|
||||
@@ -98,7 +103,7 @@ public class HostGameController extends Controller {
|
||||
public void connectSocket(String address, int port) throws IOException {
|
||||
Socket socket = new Socket(address, port);
|
||||
InGameLobbyController iglc = (InGameLobbyController)loadScene("gameLobby.fxml");
|
||||
iglc.enterGameLobby(socket, true);
|
||||
iglc.enterGameLobby(socket, true, RequestToJoinEnum.PARTICIPANT);//TODO may want to let the host spectate if they wish.
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -15,11 +15,13 @@ import javafx.scene.control.Label;
|
||||
import javafx.scene.image.ImageView;
|
||||
import javafx.scene.layout.AnchorPane;
|
||||
import javafx.scene.layout.GridPane;
|
||||
import javafx.scene.paint.Color;
|
||||
import javafx.scene.paint.PhongMaterial;
|
||||
import javafx.scene.shape.MeshView;
|
||||
import mock.app.Event;
|
||||
import network.Messages.Enums.RaceStatusEnum;
|
||||
import network.Messages.Enums.RequestToJoinEnum;
|
||||
import shared.model.Boat;
|
||||
import visualiser.app.App;
|
||||
import visualiser.gameController.ControllerClient;
|
||||
import visualiser.layout.SeaSurface;
|
||||
@@ -28,6 +30,7 @@ import visualiser.layout.View3D;
|
||||
import visualiser.model.VisualiserBoat;
|
||||
import visualiser.model.VisualiserRaceEvent;
|
||||
import visualiser.model.VisualiserRaceState;
|
||||
import visualiser.network.HttpMatchBrowserHost;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.Socket;
|
||||
@@ -124,61 +127,69 @@ public class InGameLobbyController extends Controller {
|
||||
|
||||
@Override
|
||||
public void onChanged(Change change) {
|
||||
Platform.runLater(
|
||||
() -> {
|
||||
while (change.next()){
|
||||
if (change.wasAdded() || change.wasRemoved() || change.wasUpdated()){
|
||||
try{
|
||||
resetLobby();
|
||||
int count = 0;
|
||||
int row = 0;
|
||||
for (VisualiserBoat boat :visualiserRaceEvent.getVisualiserRaceState().getBoats()) {
|
||||
View3D playerBoatToSet = new View3D();
|
||||
playerBoatToSet.setItems(subjects);
|
||||
|
||||
playerContainer.add(playerBoatToSet, (count % 3) , row);
|
||||
playerContainer.setMargin(playerBoatToSet, new Insets(10, 10, 10, 10));
|
||||
|
||||
SeaSurface sea = new SeaSurface(750, 200);
|
||||
sea.setX(250);
|
||||
sea.setZ(210);
|
||||
subjects.add(sea);
|
||||
|
||||
MeshView mesh = new MeshView(importer.getImport());
|
||||
PhongMaterial boatColorMat = new PhongMaterial(boat.getColor());
|
||||
mesh.setMaterial(boatColorMat);
|
||||
Subject3D subject = new Subject3D(mesh,0);
|
||||
subjects.add(subject);
|
||||
|
||||
playerBoatToSet.setDistance(50);
|
||||
playerBoatToSet.setYaw(45);
|
||||
playerBoatToSet.setPitch(20);
|
||||
|
||||
|
||||
|
||||
AnimationTimer rotate = new AnimationTimer() {
|
||||
@Override
|
||||
public void handle(long now) {
|
||||
subject.setHeading(subject.getHeading().getAngle() + 0.1);
|
||||
}
|
||||
};
|
||||
rotate.start();
|
||||
|
||||
allPlayerLabels.get(count).setText(boat.getName());
|
||||
allPlayerLabels.get(count).toFront();
|
||||
count += 1;
|
||||
if (count > 2){
|
||||
row = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
catch(ConcurrentModificationException e){
|
||||
}
|
||||
}
|
||||
}
|
||||
Platform.runLater(() -> {
|
||||
while (change.next()) {
|
||||
if (change.wasAdded() || change.wasRemoved() || change.wasUpdated()) {
|
||||
populateLobby();
|
||||
}
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public void populateLobby() {
|
||||
|
||||
try{
|
||||
resetLobby();
|
||||
int count = 0;
|
||||
int row = 0;
|
||||
|
||||
ArrayList<VisualiserBoat> copy = new ArrayList<>(visualiserRaceEvent.getVisualiserRaceState().getBoats());
|
||||
|
||||
for (VisualiserBoat boat : copy) {
|
||||
View3D playerBoatToSet = new View3D();
|
||||
playerBoatToSet.setItems(subjects);
|
||||
|
||||
playerContainer.add(playerBoatToSet, (count % 3) , row);
|
||||
playerContainer.setMargin(playerBoatToSet, new Insets(10, 10, 10, 10));
|
||||
|
||||
SeaSurface sea = new SeaSurface(750, 200);
|
||||
sea.setX(250);
|
||||
sea.setZ(210);
|
||||
subjects.add(sea);
|
||||
|
||||
MeshView mesh = new MeshView(importer.getImport());
|
||||
PhongMaterial boatColorMat = new PhongMaterial(boat.getColor());
|
||||
mesh.setMaterial(boatColorMat);
|
||||
Subject3D subject = new Subject3D(mesh,0);
|
||||
subjects.add(subject);
|
||||
|
||||
playerBoatToSet.setDistance(50);
|
||||
playerBoatToSet.setYaw(45);
|
||||
playerBoatToSet.setPitch(20);
|
||||
|
||||
|
||||
|
||||
AnimationTimer rotate = new AnimationTimer() {
|
||||
@Override
|
||||
public void handle(long now) {
|
||||
subject.setHeading(subject.getHeading().getAngle() + 0.1);
|
||||
}
|
||||
};
|
||||
rotate.start();
|
||||
|
||||
allPlayerLabels.get(count).setText(boat.getName());
|
||||
allPlayerLabels.get(count).toFront();
|
||||
count += 1;
|
||||
if (count > 2){
|
||||
row = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
catch(ConcurrentModificationException e){
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
/*
|
||||
private void populatePlayers(ListChangeListener.Change change){
|
||||
@@ -191,7 +202,6 @@ public class InGameLobbyController extends Controller {
|
||||
//Initialises the race clock.
|
||||
initialiseRaceClock(this.visualiserRaceEvent.getVisualiserRaceState());
|
||||
|
||||
|
||||
//Starts the race countdown timer.
|
||||
countdownTimer();
|
||||
}
|
||||
@@ -227,10 +237,9 @@ public class InGameLobbyController extends Controller {
|
||||
new AnimationTimer() {
|
||||
@Override
|
||||
public void handle(long arg0) {
|
||||
|
||||
//Get the current race status.
|
||||
RaceStatusEnum raceStatus = visualiserRaceEvent.getVisualiserRaceState().getRaceStatusEnum();
|
||||
|
||||
|
||||
//Try catch for getting interval times
|
||||
try {
|
||||
long interval = ChronoUnit.MILLIS.between(visualiserRaceEvent.getVisualiserRaceState().getRaceClock().getCurrentTime(), visualiserRaceEvent.getVisualiserRaceState().getRaceClock().getStartingTime());
|
||||
@@ -267,10 +276,16 @@ public class InGameLobbyController extends Controller {
|
||||
}.start();
|
||||
}
|
||||
|
||||
public void enterGameLobby(Socket socket, boolean isHost){
|
||||
/**
|
||||
* Joins a game and enters the game lobby for it.
|
||||
* @param socket Socket to connect to.
|
||||
* @param isHost Whether this client is the host.
|
||||
* @param joinType How the client wishes to join (e.g., participant).
|
||||
*/
|
||||
public void enterGameLobby(Socket socket, boolean isHost, RequestToJoinEnum joinType){
|
||||
try {
|
||||
|
||||
this.visualiserRaceEvent = new VisualiserRaceEvent(socket, RequestToJoinEnum.PARTICIPANT);
|
||||
this.visualiserRaceEvent = new VisualiserRaceEvent(socket, joinType);
|
||||
this.isHost = isHost;
|
||||
this.controllerClient = visualiserRaceEvent.getControllerClient();
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package visualiser.Controllers;
|
||||
|
||||
import javafx.collections.FXCollections;
|
||||
import javafx.collections.ListChangeListener;
|
||||
import javafx.collections.ObservableList;
|
||||
import javafx.fxml.FXML;
|
||||
import javafx.scene.control.Button;
|
||||
@@ -8,10 +9,15 @@ import javafx.scene.control.TableColumn;
|
||||
import javafx.scene.control.TableView;
|
||||
import javafx.scene.control.TextField;
|
||||
import javafx.scene.layout.AnchorPane;
|
||||
import network.Messages.Enums.RequestToJoinEnum;
|
||||
import javafx.scene.media.AudioClip;
|
||||
import network.Messages.HostGame;
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONObject;
|
||||
import shared.utils.JsonReader;
|
||||
import visualiser.app.MatchBrowserSingleton;
|
||||
import visualiser.model.RaceConnection;
|
||||
import visualiser.network.HttpMatchBrowserClient;
|
||||
import visualiser.network.MatchBrowserLobbyInterface;
|
||||
|
||||
import java.io.IOException;
|
||||
@@ -30,6 +36,7 @@ public class LobbyController extends Controller {
|
||||
private @FXML TableColumn<RaceConnection, String> hostNameColumn;
|
||||
private @FXML TableColumn<RaceConnection, String> statusColumn;
|
||||
private @FXML Button joinGameBtn;
|
||||
private @FXML Button spectateButton;
|
||||
private @FXML TextField addressFld;
|
||||
private @FXML TextField portFld;
|
||||
|
||||
@@ -39,17 +46,17 @@ public class LobbyController extends Controller {
|
||||
private AudioClip sound;
|
||||
|
||||
//the socket for match browser
|
||||
private DatagramSocket udpSocket;
|
||||
private MatchBrowserLobbyInterface matchBrowserLobbyInterface;
|
||||
private HttpMatchBrowserClient httpMatchBrowserClient;
|
||||
|
||||
|
||||
public void initialize() {
|
||||
httpMatchBrowserClient = new HttpMatchBrowserClient();
|
||||
new Thread(httpMatchBrowserClient, "Match Client").start();
|
||||
// set up the connection table
|
||||
connections = FXCollections.observableArrayList();
|
||||
customConnections = FXCollections.observableArrayList();
|
||||
//connections.add(new RaceConnection("localhost", 4942, "Local Game"));
|
||||
|
||||
lobbyTable.setItems(connections);
|
||||
lobbyTable.setItems(httpMatchBrowserClient.connections);
|
||||
gameNameColumn.setCellValueFactory(cellData -> cellData.getValue().gamenameProperty());
|
||||
hostNameColumn.setCellValueFactory(cellData -> cellData.getValue().hostnameProperty());
|
||||
statusColumn.setCellValueFactory(cellData -> cellData.getValue().statusProperty());
|
||||
@@ -57,13 +64,15 @@ public class LobbyController extends Controller {
|
||||
lobbyTable.getSelectionModel().selectedItemProperty().addListener((obs, prev, curr) -> {
|
||||
if (curr != null && curr.statusProperty().getValue().equals("Ready")) {
|
||||
joinGameBtn.setDisable(false);
|
||||
spectateButton.setDisable(false);
|
||||
} else {
|
||||
joinGameBtn.setDisable(true);
|
||||
spectateButton.setDisable(true);
|
||||
}
|
||||
});
|
||||
joinGameBtn.setDisable(true);
|
||||
spectateButton.setDisable(true);
|
||||
|
||||
this.udpSocket = MatchBrowserSingleton.getInstance().getUdpSocket();
|
||||
receiveMatchData();
|
||||
}
|
||||
|
||||
@@ -80,27 +89,47 @@ public class LobbyController extends Controller {
|
||||
try {
|
||||
if (lobbyTable.getSelectionModel().getSelectedItem().statusProperty().getValue().equals("Ready")) {
|
||||
joinGameBtn.setDisable(false);
|
||||
spectateButton.setDisable(false);
|
||||
} else {
|
||||
joinGameBtn.setDisable(true);
|
||||
spectateButton.setDisable(true);
|
||||
}
|
||||
} catch (Exception ignored){}
|
||||
}
|
||||
|
||||
/**
|
||||
* Connect to a connection.
|
||||
* @param joinType How the client wishes to join (e.g., participant).
|
||||
* @throws IOException socket error
|
||||
*/
|
||||
public void connectSocket() throws IOException {
|
||||
public void connectSocket(RequestToJoinEnum joinType) throws IOException {
|
||||
httpMatchBrowserClient.interrupt();
|
||||
RaceConnection connection = lobbyTable.getSelectionModel().getSelectedItem();
|
||||
Socket socket = new Socket(connection.getHostname(), connection.getPort());
|
||||
InGameLobbyController iglc = (InGameLobbyController)loadScene("gameLobby.fxml");
|
||||
iglc.enterGameLobby(socket, false);
|
||||
iglc.enterGameLobby(socket, false, joinType);
|
||||
}
|
||||
|
||||
/**
|
||||
* Requests to join the game as a participant.
|
||||
* @throws IOException socket error.
|
||||
*/
|
||||
public void connectParticipate() throws IOException {
|
||||
connectSocket(RequestToJoinEnum.PARTICIPANT);
|
||||
}
|
||||
|
||||
/**
|
||||
* Requests to join the game as a spectator.
|
||||
* @throws IOException socket error.
|
||||
*/
|
||||
public void connectSpectate() throws IOException {
|
||||
connectSocket(RequestToJoinEnum.SPECTATOR);
|
||||
}
|
||||
|
||||
public void menuBtnPressed() throws IOException {
|
||||
sound = new AudioClip(this.getClass().getResource("/visualiser/sounds/buttonpress.wav").toExternalForm());
|
||||
sound.play();
|
||||
matchBrowserLobbyInterface.closeSocket();
|
||||
httpMatchBrowserClient.interrupt();
|
||||
loadScene("title.fxml");
|
||||
}
|
||||
|
||||
@@ -124,6 +153,7 @@ public class LobbyController extends Controller {
|
||||
}
|
||||
|
||||
public void receiveMatchData(){
|
||||
/*
|
||||
matchBrowserLobbyInterface = new MatchBrowserLobbyInterface();
|
||||
try {
|
||||
matchBrowserLobbyInterface.startReceivingHostData(new DatagramSocket(4941));
|
||||
@@ -136,17 +166,24 @@ public class LobbyController extends Controller {
|
||||
matchBrowserLobbyInterface.addObserver(o);
|
||||
} catch (SocketException e) {
|
||||
System.err.println("Socket 4941 in use");
|
||||
}
|
||||
}*/
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds the games received from the server
|
||||
*/
|
||||
private void addServerGames() {
|
||||
connections.clear();
|
||||
connections.addAll(customConnections);
|
||||
httpMatchBrowserClient.connections.addAll(customConnections);
|
||||
httpMatchBrowserClient.connections.addListener(new ListChangeListener<RaceConnection>() {
|
||||
@Override
|
||||
public void onChanged(Change<? extends RaceConnection> c) {
|
||||
refreshBtnPressed();
|
||||
}
|
||||
});
|
||||
|
||||
/*
|
||||
for (HostGame game : matchBrowserLobbyInterface.getGames()) {
|
||||
connections.add(new RaceConnection(game.getIp(), 4942, "Boat Game"));
|
||||
}
|
||||
}*/
|
||||
}
|
||||
}
|
||||
|
||||
@@ -433,12 +433,14 @@ public class RaceViewController extends Controller {
|
||||
@Override
|
||||
public void handle(long now) {
|
||||
CompoundMark target = boat.getCurrentLeg().getEndCompoundMark();
|
||||
Bearing headingToMark = GPSCoordinate.calculateBearing(boat.getPosition(), target.getAverageGPSCoordinate());
|
||||
if (target != null) {
|
||||
Bearing headingToMark = GPSCoordinate.calculateBearing(boat.getPosition(), target.getAverageGPSCoordinate());
|
||||
|
||||
nextMarkArrow.setX(view3D.getPivot().getX());
|
||||
nextMarkArrow.setY(view3D.getPivot().getY());
|
||||
nextMarkArrow.setZ(view3D.getPivot().getZ() + 15);
|
||||
nextMarkArrow.setHeading(headingToMark.degrees());
|
||||
nextMarkArrow.setX(view3D.getPivot().getX());
|
||||
nextMarkArrow.setY(view3D.getPivot().getY());
|
||||
nextMarkArrow.setZ(view3D.getPivot().getZ() + 15);
|
||||
nextMarkArrow.setHeading(headingToMark.degrees());
|
||||
}
|
||||
}
|
||||
};
|
||||
arrowToNextMark.start();
|
||||
|
||||
@@ -14,15 +14,25 @@ public class ThisBoat {
|
||||
}
|
||||
|
||||
public void setSailsOut(boolean sailsOut) {
|
||||
this.boat.setSailsOut(sailsOut);
|
||||
if (this.boat != null) {
|
||||
this.boat.setSailsOut(sailsOut);
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isSailsOut() {
|
||||
return this.boat.isSailsOut();
|
||||
if (this.boat != null) {
|
||||
return this.boat.isSailsOut();
|
||||
} else {
|
||||
return true;//TODO junk value to allow the client to spectate.
|
||||
}
|
||||
}
|
||||
|
||||
public int getSourceID() {
|
||||
return this.boat.getSourceID();
|
||||
if (this.boat != null) {
|
||||
return this.boat.getSourceID();
|
||||
} else {
|
||||
return 0;//TODO junk value to allow the client to spectate.
|
||||
}
|
||||
}
|
||||
|
||||
public void setBoat(VisualiserBoat boat) {
|
||||
|
||||
@@ -8,6 +8,7 @@ import shared.dataInput.EmptyBoatDataSource;
|
||||
import shared.dataInput.EmptyRaceDataSource;
|
||||
import shared.dataInput.EmptyRegattaDataSource;
|
||||
import visualiser.gameController.ControllerClient;
|
||||
import visualiser.network.HttpMatchBrowserHost;
|
||||
import visualiser.network.ServerConnection;
|
||||
|
||||
import java.io.IOException;
|
||||
@@ -115,5 +116,8 @@ public class VisualiserRaceEvent {
|
||||
this.visualiserRaceServiceThread.interrupt();
|
||||
this.serverConnectionThread.interrupt();
|
||||
serverConnection.terminate();
|
||||
if (HttpMatchBrowserHost.httpMatchBrowserHost != null) {
|
||||
HttpMatchBrowserHost.httpMatchBrowserHost.interrupt();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -94,7 +94,7 @@ public class VisualiserRaceState extends RaceState {
|
||||
super.setRaceDataSource(raceDataSource);
|
||||
|
||||
if (getBoatDataSource() != null) {
|
||||
this.generateVisualiserBoats(this.boats, getBoatDataSource().getBoats(), raceDataSource.getParticipants(), unassignedColors);
|
||||
this.generateVisualiserBoats(this.boats, getBoatDataSource().getBoats(), getRaceDataSource().getParticipants(), unassignedColors);
|
||||
}
|
||||
|
||||
initialiseLegCompletionOrder();
|
||||
@@ -109,7 +109,7 @@ public class VisualiserRaceState extends RaceState {
|
||||
super.setBoatDataSource(boatDataSource);
|
||||
|
||||
if (getRaceDataSource() != null) {
|
||||
this.generateVisualiserBoats(this.boats, boatDataSource.getBoats(), getRaceDataSource().getParticipants(), unassignedColors);
|
||||
this.generateVisualiserBoats(this.boats, getBoatDataSource().getBoats(), getRaceDataSource().getParticipants(), unassignedColors);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
package visualiser.network;
|
||||
|
||||
import javafx.collections.FXCollections;
|
||||
import javafx.collections.ObservableList;
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONObject;
|
||||
import shared.utils.JsonReader;
|
||||
import visualiser.model.RaceConnection;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* Created by Gondr on 19/09/2017.
|
||||
*/
|
||||
public class HttpMatchBrowserClient extends Thread {
|
||||
public ObservableList<RaceConnection> connections = FXCollections.observableArrayList();
|
||||
|
||||
/**
|
||||
* Get all the matches that have been running in the past 5 seconds.
|
||||
*/
|
||||
@Override
|
||||
public void run() {
|
||||
while(!Thread.interrupted()) {
|
||||
try {
|
||||
JSONArray cons = JsonReader.readJsonFromUrlArray("http://api.umbrasheep.com/seng/get_matches/");
|
||||
connections.clear();
|
||||
|
||||
for (int i = 0; i < cons.length(); i++) {
|
||||
JSONObject con = (JSONObject) cons.get(i);
|
||||
//using "ip_address" will give their public ip
|
||||
connections.add(new RaceConnection((String) con.get("local_ip"), con.getInt("port"), "Boat Game"));
|
||||
}
|
||||
|
||||
Thread.sleep(5000);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,115 @@
|
||||
package visualiser.network;
|
||||
|
||||
|
||||
import org.apache.http.HttpEntity;
|
||||
import org.apache.http.HttpResponse;
|
||||
import org.apache.http.NameValuePair;
|
||||
import org.apache.http.client.HttpClient;
|
||||
import org.apache.http.client.entity.UrlEncodedFormEntity;
|
||||
import org.apache.http.client.methods.HttpPost;
|
||||
import org.apache.http.impl.client.HttpClients;
|
||||
import org.apache.http.message.BasicNameValuePair;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.net.InetAddress;
|
||||
import java.net.NetworkInterface;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Enumeration;
|
||||
import java.util.List;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
* Creates an Http connection that hosts a game
|
||||
*/
|
||||
public class HttpMatchBrowserHost extends Thread {
|
||||
private HttpClient httpClient;
|
||||
private List<NameValuePair> params;
|
||||
|
||||
public static HttpMatchBrowserHost httpMatchBrowserHost = null;
|
||||
|
||||
/**
|
||||
* Constructor, this sends out the creation message of the race.
|
||||
* the thread should be run as soon as possible as the race is only valid for 5 seconds
|
||||
* until it requires a heartbeat from the start function.
|
||||
* @throws IOException if the hosting url is unreachable.
|
||||
*/
|
||||
public HttpMatchBrowserHost() throws IOException {
|
||||
httpMatchBrowserHost = this;
|
||||
httpClient = HttpClients.createDefault();
|
||||
|
||||
Enumeration<NetworkInterface> e = NetworkInterface.getNetworkInterfaces();
|
||||
boolean matches = false;
|
||||
String ip = "";
|
||||
Pattern ipPattern = Pattern.compile("192.168.1.*");
|
||||
while(e.hasMoreElements())
|
||||
{
|
||||
if (matches){
|
||||
break;
|
||||
}
|
||||
NetworkInterface n = (NetworkInterface) e.nextElement();
|
||||
Enumeration<InetAddress> ee = n.getInetAddresses();
|
||||
while (ee.hasMoreElements())
|
||||
{
|
||||
InetAddress i = ee.nextElement();
|
||||
matches = ipPattern.matcher(i.getHostAddress()).matches();
|
||||
if (matches){
|
||||
ip = i.getHostAddress();
|
||||
//System.out.println(ip);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Request parameters and other properties.
|
||||
params = new ArrayList<>(2);
|
||||
params.add(new BasicNameValuePair("ip", ip));
|
||||
params.add(new BasicNameValuePair("port", "4942"));
|
||||
params.add(new BasicNameValuePair("magic", "Thomas and Seng"));
|
||||
|
||||
sendHttp("http://api.umbrasheep.com/seng/registermatch/");
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends a post to a server.
|
||||
* @param domain url of to send to
|
||||
* @throws IOException if the url is unreachable.
|
||||
*/
|
||||
public void sendHttp(String domain) throws IOException {
|
||||
HttpPost httppost = new HttpPost(domain);
|
||||
httppost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
|
||||
|
||||
//Execute and get the response.
|
||||
HttpResponse response = httpClient.execute(httppost);
|
||||
HttpEntity entity = response.getEntity();
|
||||
|
||||
if (entity != null) {
|
||||
InputStream instream = entity.getContent();
|
||||
try {
|
||||
// do something useful
|
||||
} finally {
|
||||
instream.close();
|
||||
}
|
||||
} else {
|
||||
throw new IOException("No Response from Host");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* THe host starts sending out heartbeat messages every 2 seconds.
|
||||
*/
|
||||
@Override
|
||||
public void run() {
|
||||
while(!Thread.interrupted()){
|
||||
try {
|
||||
sendHttp("http://api.umbrasheep.com/seng/keep_match_alive/");
|
||||
Thread.sleep(2000);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -26,8 +26,9 @@ public class MatchBrowserInterface {
|
||||
|
||||
public MatchBrowserInterface() {
|
||||
try {//132.181.16.13 is the ip of the CI as of 13/9/17
|
||||
//this.IPAddress = InetAddress.getByName("132.181.16.13"); //InetAddress.getLocalHost();
|
||||
//this.IPAddress = InetAddress.getByName("umbrasheep.com"); //InetAddress.getLocalHost();
|
||||
this.IPAddress = InetAddress.getByName("132.181.16.13"); //InetAddress.getLocalHost();
|
||||
this.IPAddress = InetAddress.getByName("191.101.233.116"); //InetAddress.getLocalHost();
|
||||
} catch (UnknownHostException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user