packets can now be sent from client to match server
#story[1188]
This commit is contained in:
@@ -4,16 +4,11 @@ import javafx.fxml.FXML;
|
||||
import javafx.scene.control.TextField;
|
||||
import javafx.scene.layout.AnchorPane;
|
||||
import mock.app.Event;
|
||||
import org.xml.sax.SAXException;
|
||||
import mock.exceptions.EventConstructionException;
|
||||
import shared.exceptions.InvalidBoatDataException;
|
||||
import shared.exceptions.InvalidRaceDataException;
|
||||
import shared.exceptions.InvalidRegattaDataException;
|
||||
import shared.exceptions.XMLReaderException;
|
||||
import visualiser.network.MatchBrowserInterface;
|
||||
|
||||
import javax.xml.bind.JAXBException;
|
||||
import javax.xml.parsers.ParserConfigurationException;
|
||||
import java.io.IOException;
|
||||
import java.net.DatagramSocket;
|
||||
import java.net.Socket;
|
||||
import java.net.URL;
|
||||
import java.util.ResourceBundle;
|
||||
@@ -36,6 +31,7 @@ public class HostController extends Controller {
|
||||
AnchorPane hostWrapper;
|
||||
|
||||
private Event game;
|
||||
private DatagramSocket udpSocket;
|
||||
|
||||
|
||||
@Override
|
||||
@@ -50,12 +46,25 @@ public class HostController extends Controller {
|
||||
try {
|
||||
this.game = new Event(false);
|
||||
connectSocket("localhost", 4942);
|
||||
alertMatchBrowser();
|
||||
} catch (EventConstructionException e) {
|
||||
Logger.getGlobal().log(Level.SEVERE, "Could not create Event.", e);
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends info to the match browser so clients can see it
|
||||
*/
|
||||
public void alertMatchBrowser(){
|
||||
MatchBrowserInterface matchBrowserInterface = new MatchBrowserInterface();
|
||||
try{
|
||||
matchBrowserInterface.sendOutGameInfo(this.game.getHostedGameData(), udpSocket);
|
||||
}catch (IOException e){
|
||||
System.err.println("failed to send out hosted game info");
|
||||
}
|
||||
}
|
||||
|
||||
public void endEvent() throws IOException {
|
||||
game.endEvent();
|
||||
}
|
||||
@@ -80,8 +89,9 @@ public class HostController extends Controller {
|
||||
/**
|
||||
* Hosts a game.
|
||||
*/
|
||||
public void hostGame(){
|
||||
public void hostGame(DatagramSocket udpSocket){
|
||||
hostWrapper.setVisible(true);
|
||||
this.udpSocket = udpSocket;
|
||||
}
|
||||
|
||||
public void menuBtnPressed(){
|
||||
|
||||
@@ -11,6 +11,7 @@ import javafx.scene.layout.AnchorPane;
|
||||
import visualiser.model.RaceConnection;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.DatagramSocket;
|
||||
import java.net.Socket;
|
||||
import java.net.URL;
|
||||
import java.util.ResourceBundle;
|
||||
@@ -39,6 +40,9 @@ public class LobbyController extends Controller {
|
||||
|
||||
private ObservableList<RaceConnection> connections;
|
||||
|
||||
//the socket for match browser
|
||||
private DatagramSocket udpSocket;
|
||||
|
||||
|
||||
@Override
|
||||
public void initialize(URL location, ResourceBundle resources) {
|
||||
@@ -120,7 +124,8 @@ public class LobbyController extends Controller {
|
||||
/**
|
||||
* Enter the lobby page.
|
||||
*/
|
||||
public void enterLobby(){
|
||||
public void enterLobby(DatagramSocket udpSocket){
|
||||
lobbyWrapper.setVisible(true);
|
||||
this.udpSocket = udpSocket;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,12 +3,13 @@ package visualiser.Controllers;
|
||||
import javafx.collections.ObservableList;
|
||||
import javafx.fxml.FXML;
|
||||
import javafx.scene.layout.AnchorPane;
|
||||
import visualiser.app.App;
|
||||
import visualiser.gameController.ControllerClient;
|
||||
import visualiser.model.VisualiserBoat;
|
||||
import visualiser.model.VisualiserRaceEvent;
|
||||
import visualiser.network.MatchBrowserInterface;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.DatagramSocket;
|
||||
import java.net.Socket;
|
||||
import java.net.URL;
|
||||
import java.util.ResourceBundle;
|
||||
@@ -27,11 +28,20 @@ public class MainController extends Controller {
|
||||
@FXML private HostController hostController;
|
||||
@FXML private LobbyController lobbyController;
|
||||
|
||||
private MatchBrowserInterface matchBrowserInterface;
|
||||
private DatagramSocket udpSocket;
|
||||
|
||||
|
||||
/**
|
||||
* Ctor.
|
||||
*/
|
||||
public MainController() {
|
||||
this.matchBrowserInterface = new MatchBrowserInterface();
|
||||
try{
|
||||
this.udpSocket = matchBrowserInterface.setupMatchBowserConnection();
|
||||
}catch (IOException e){
|
||||
System.err.println("Error in setting up connection with match browser");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -74,12 +84,12 @@ public class MainController extends Controller {
|
||||
/**
|
||||
* Transitions into lobby screen
|
||||
*/
|
||||
public void enterLobby(){ lobbyController.enterLobby(); }
|
||||
public void enterLobby(){ lobbyController.enterLobby(udpSocket); }
|
||||
|
||||
/**
|
||||
* Transitions into host game screen
|
||||
*/
|
||||
public void hostGame(){ hostController.hostGame(); }
|
||||
public void hostGame(){ hostController.hostGame(udpSocket); }
|
||||
|
||||
/**
|
||||
* Sets up the css for the start of the program
|
||||
|
||||
@@ -3,31 +3,81 @@ package visualiser.network;
|
||||
import network.BinaryMessageEncoder;
|
||||
import network.Exceptions.InvalidMessageException;
|
||||
import network.MessageEncoders.HostGameMessageEncoder;
|
||||
import network.MessageEncoders.HostedGamesRequestEncoder;
|
||||
import network.Messages.AC35Data;
|
||||
import network.Messages.Enums.MessageType;
|
||||
import network.Messages.HostGamesRequest;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.DatagramPacket;
|
||||
import java.net.DatagramSocket;
|
||||
import java.net.InetAddress;
|
||||
import java.net.UnknownHostException;
|
||||
import java.util.ArrayList;
|
||||
|
||||
/**
|
||||
* UDP interface for the matchBrowser to send out hosted game info and get in other hosts info
|
||||
*/
|
||||
public class MatchBrowserInterface {
|
||||
DatagramSocket clientSocket;
|
||||
//ip of the server
|
||||
private InetAddress IPAddress;
|
||||
//port server is hosted on
|
||||
private int port;
|
||||
|
||||
public void sendOutGameInfo(AC35Data gameInfo){
|
||||
|
||||
|
||||
public MatchBrowserInterface() {
|
||||
//TODO change to ip of cloud server hosting this
|
||||
try {
|
||||
this.IPAddress = InetAddress.getByName("localhost");
|
||||
} catch (UnknownHostException e) {
|
||||
}
|
||||
this.port = 3779;
|
||||
}
|
||||
|
||||
/**
|
||||
* Used by host to send out race information to the server
|
||||
* @param gameInfo the hostGame info for message
|
||||
* @param socket the udp socket assigned on startup
|
||||
*/
|
||||
public void sendOutGameInfo(AC35Data gameInfo, DatagramSocket socket) throws IOException{
|
||||
byte[] fullMessageToSend;
|
||||
try{
|
||||
HostGameMessageEncoder encoder = new HostGameMessageEncoder();
|
||||
byte[] message = encoder.encode(gameInfo);
|
||||
BinaryMessageEncoder messageEncoder = new BinaryMessageEncoder(MessageType.HOST_GAME
|
||||
,System.currentTimeMillis(), 1,(short) 14 ,message);
|
||||
,System.currentTimeMillis(), 1,(short) 13 ,message);
|
||||
fullMessageToSend = messageEncoder.getFullMessage();
|
||||
|
||||
DatagramPacket sendPacket = new DatagramPacket(fullMessageToSend, fullMessageToSend.length, IPAddress, port);
|
||||
socket.send(sendPacket);
|
||||
|
||||
}catch (InvalidMessageException e){
|
||||
System.err.println("HostGameMessage could not be encoded");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Used by a client to setup a connection with the match browser server
|
||||
* @return the socket created for this connection
|
||||
*/
|
||||
public DatagramSocket setupMatchBowserConnection() throws IOException{
|
||||
DatagramSocket clientSocket = new DatagramSocket();
|
||||
|
||||
//creates and empty hostedGamesRequest packet that can be sent to the server
|
||||
//this lets the server check the udp fields for ip and port to know that this client exists
|
||||
try{
|
||||
HostedGamesRequestEncoder encoder = new HostedGamesRequestEncoder();
|
||||
byte[] message = encoder.encode(new HostGamesRequest(new ArrayList()));
|
||||
BinaryMessageEncoder messageEncoder = new BinaryMessageEncoder(MessageType.HOSTED_GAMES_REQUEST
|
||||
,System.currentTimeMillis(), 1,(short) 13 ,message);
|
||||
|
||||
DatagramPacket sendPacket = new DatagramPacket(messageEncoder.getFullMessage(), messageEncoder.getFullMessage().length, IPAddress, port);
|
||||
clientSocket.send(sendPacket);
|
||||
}catch (InvalidMessageException e){
|
||||
System.err.println("HostedGamesRequestMessage could not be encoded");
|
||||
}
|
||||
|
||||
return clientSocket;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user