packets can now be sent from client to match server
#story[1188]
This commit is contained in:
@@ -1,7 +1,10 @@
|
|||||||
package networkInterface;
|
package networkInterface;
|
||||||
|
|
||||||
|
import network.BinaryMessageDecoder;
|
||||||
import network.Exceptions.InvalidMessageException;
|
import network.Exceptions.InvalidMessageException;
|
||||||
|
import network.MessageDecoders.HostGameMessageDecoder;
|
||||||
import network.MessageDecoders.HostedGamesRequestDecoder;
|
import network.MessageDecoders.HostedGamesRequestDecoder;
|
||||||
|
import network.Messages.HostGame;
|
||||||
import network.Messages.HostGamesRequest;
|
import network.Messages.HostGamesRequest;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
@@ -34,15 +37,32 @@ public class NetworkInterface {
|
|||||||
DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
|
DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
|
||||||
serverSocket.receive(receivePacket);
|
serverSocket.receive(receivePacket);
|
||||||
|
|
||||||
|
BinaryMessageDecoder messageDecoder = new BinaryMessageDecoder(receivePacket.getData());
|
||||||
|
switch (messageDecoder.getHeaderMessageType()){
|
||||||
|
case 108:
|
||||||
//decode and update table
|
//decode and update table
|
||||||
HostedGamesRequestDecoder decoder = new HostedGamesRequestDecoder();
|
HostGameMessageDecoder decoder = new HostGameMessageDecoder();
|
||||||
|
HostGame newKnownGame;
|
||||||
|
try{
|
||||||
|
newKnownGame = (HostGame) decoder.decode(messageDecoder.getMessageBody());
|
||||||
|
System.out.println(newKnownGame.getIp());
|
||||||
|
}catch (InvalidMessageException e){
|
||||||
|
System.err.println("Message received that is not a hostedGame packet");
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case 109:
|
||||||
|
//decode and update table
|
||||||
|
HostedGamesRequestDecoder decoder2 = new HostedGamesRequestDecoder();
|
||||||
HostGamesRequest newKnownGames;
|
HostGamesRequest newKnownGames;
|
||||||
try{
|
try{
|
||||||
newKnownGames = (HostGamesRequest) decoder.decode(receivePacket.getData());
|
newKnownGames = (HostGamesRequest) decoder2.decode(receivePacket.getData());
|
||||||
System.out.println(newKnownGames.getKnownGames().get(0));
|
System.out.println(newKnownGames);
|
||||||
}catch (InvalidMessageException e){
|
}catch (InvalidMessageException e){
|
||||||
System.err.println("Message received that is not a hostedGamesRequest packet");
|
System.err.println("Message received that is not a hostedGamesRequest packet");
|
||||||
}
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
//client ip and port
|
//client ip and port
|
||||||
InetAddress IPAddress = receivePacket.getAddress();
|
InetAddress IPAddress = receivePacket.getAddress();
|
||||||
|
|||||||
@@ -4,9 +4,9 @@ import mock.dataInput.PolarParser;
|
|||||||
import mock.exceptions.EventConstructionException;
|
import mock.exceptions.EventConstructionException;
|
||||||
import mock.model.*;
|
import mock.model.*;
|
||||||
import mock.model.commandFactory.CompositeCommand;
|
import mock.model.commandFactory.CompositeCommand;
|
||||||
import mock.xml.RaceXMLCreator;
|
import network.Messages.Enums.RaceStatusEnum;
|
||||||
|
import network.Messages.HostGame;
|
||||||
import network.Messages.LatestMessages;
|
import network.Messages.LatestMessages;
|
||||||
import org.xml.sax.SAXException;
|
|
||||||
import shared.dataInput.*;
|
import shared.dataInput.*;
|
||||||
import shared.enums.XMLFileType;
|
import shared.enums.XMLFileType;
|
||||||
import shared.exceptions.InvalidBoatDataException;
|
import shared.exceptions.InvalidBoatDataException;
|
||||||
@@ -16,18 +16,11 @@ import shared.exceptions.XMLReaderException;
|
|||||||
import shared.model.Bearing;
|
import shared.model.Bearing;
|
||||||
import shared.model.Constants;
|
import shared.model.Constants;
|
||||||
|
|
||||||
import javax.xml.bind.JAXBException;
|
|
||||||
import javax.xml.parsers.ParserConfigurationException;
|
|
||||||
import javax.xml.transform.TransformerException;
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.UnknownHostException;
|
import java.net.InetAddress;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.time.Duration;
|
|
||||||
import java.time.ZonedDateTime;
|
import java.time.ZonedDateTime;
|
||||||
import java.time.format.DateTimeFormatter;
|
import java.time.format.DateTimeFormatter;
|
||||||
import java.util.concurrent.TimeUnit;
|
|
||||||
import java.util.logging.Level;
|
|
||||||
import java.util.logging.Logger;
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -208,4 +201,13 @@ public class Event {
|
|||||||
return raceXML;
|
return raceXML;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates the needed data type for a network packet
|
||||||
|
* @return hostGame Ac35DataType
|
||||||
|
*/
|
||||||
|
public HostGame getHostedGameData() throws IOException{
|
||||||
|
InetAddress ip = InetAddress.getByName("localhost");
|
||||||
|
return new HostGame(ip.getHostAddress(), 3779,(byte) 1,(byte) 1, RaceStatusEnum.PRESTART, (byte) 1, (byte) 1);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,16 +4,11 @@ import javafx.fxml.FXML;
|
|||||||
import javafx.scene.control.TextField;
|
import javafx.scene.control.TextField;
|
||||||
import javafx.scene.layout.AnchorPane;
|
import javafx.scene.layout.AnchorPane;
|
||||||
import mock.app.Event;
|
import mock.app.Event;
|
||||||
import org.xml.sax.SAXException;
|
|
||||||
import mock.exceptions.EventConstructionException;
|
import mock.exceptions.EventConstructionException;
|
||||||
import shared.exceptions.InvalidBoatDataException;
|
import visualiser.network.MatchBrowserInterface;
|
||||||
import shared.exceptions.InvalidRaceDataException;
|
|
||||||
import shared.exceptions.InvalidRegattaDataException;
|
|
||||||
import shared.exceptions.XMLReaderException;
|
|
||||||
|
|
||||||
import javax.xml.bind.JAXBException;
|
|
||||||
import javax.xml.parsers.ParserConfigurationException;
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.net.DatagramSocket;
|
||||||
import java.net.Socket;
|
import java.net.Socket;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.util.ResourceBundle;
|
import java.util.ResourceBundle;
|
||||||
@@ -36,6 +31,7 @@ public class HostController extends Controller {
|
|||||||
AnchorPane hostWrapper;
|
AnchorPane hostWrapper;
|
||||||
|
|
||||||
private Event game;
|
private Event game;
|
||||||
|
private DatagramSocket udpSocket;
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -50,12 +46,25 @@ public class HostController extends Controller {
|
|||||||
try {
|
try {
|
||||||
this.game = new Event(false);
|
this.game = new Event(false);
|
||||||
connectSocket("localhost", 4942);
|
connectSocket("localhost", 4942);
|
||||||
|
alertMatchBrowser();
|
||||||
} catch (EventConstructionException e) {
|
} catch (EventConstructionException e) {
|
||||||
Logger.getGlobal().log(Level.SEVERE, "Could not create Event.", e);
|
Logger.getGlobal().log(Level.SEVERE, "Could not create Event.", e);
|
||||||
throw new RuntimeException(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 {
|
public void endEvent() throws IOException {
|
||||||
game.endEvent();
|
game.endEvent();
|
||||||
}
|
}
|
||||||
@@ -80,8 +89,9 @@ public class HostController extends Controller {
|
|||||||
/**
|
/**
|
||||||
* Hosts a game.
|
* Hosts a game.
|
||||||
*/
|
*/
|
||||||
public void hostGame(){
|
public void hostGame(DatagramSocket udpSocket){
|
||||||
hostWrapper.setVisible(true);
|
hostWrapper.setVisible(true);
|
||||||
|
this.udpSocket = udpSocket;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void menuBtnPressed(){
|
public void menuBtnPressed(){
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ import javafx.scene.layout.AnchorPane;
|
|||||||
import visualiser.model.RaceConnection;
|
import visualiser.model.RaceConnection;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.net.DatagramSocket;
|
||||||
import java.net.Socket;
|
import java.net.Socket;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.util.ResourceBundle;
|
import java.util.ResourceBundle;
|
||||||
@@ -39,6 +40,9 @@ public class LobbyController extends Controller {
|
|||||||
|
|
||||||
private ObservableList<RaceConnection> connections;
|
private ObservableList<RaceConnection> connections;
|
||||||
|
|
||||||
|
//the socket for match browser
|
||||||
|
private DatagramSocket udpSocket;
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void initialize(URL location, ResourceBundle resources) {
|
public void initialize(URL location, ResourceBundle resources) {
|
||||||
@@ -120,7 +124,8 @@ public class LobbyController extends Controller {
|
|||||||
/**
|
/**
|
||||||
* Enter the lobby page.
|
* Enter the lobby page.
|
||||||
*/
|
*/
|
||||||
public void enterLobby(){
|
public void enterLobby(DatagramSocket udpSocket){
|
||||||
lobbyWrapper.setVisible(true);
|
lobbyWrapper.setVisible(true);
|
||||||
|
this.udpSocket = udpSocket;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,12 +3,13 @@ package visualiser.Controllers;
|
|||||||
import javafx.collections.ObservableList;
|
import javafx.collections.ObservableList;
|
||||||
import javafx.fxml.FXML;
|
import javafx.fxml.FXML;
|
||||||
import javafx.scene.layout.AnchorPane;
|
import javafx.scene.layout.AnchorPane;
|
||||||
import visualiser.app.App;
|
|
||||||
import visualiser.gameController.ControllerClient;
|
import visualiser.gameController.ControllerClient;
|
||||||
import visualiser.model.VisualiserBoat;
|
import visualiser.model.VisualiserBoat;
|
||||||
import visualiser.model.VisualiserRaceEvent;
|
import visualiser.model.VisualiserRaceEvent;
|
||||||
|
import visualiser.network.MatchBrowserInterface;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.net.DatagramSocket;
|
||||||
import java.net.Socket;
|
import java.net.Socket;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.util.ResourceBundle;
|
import java.util.ResourceBundle;
|
||||||
@@ -27,11 +28,20 @@ public class MainController extends Controller {
|
|||||||
@FXML private HostController hostController;
|
@FXML private HostController hostController;
|
||||||
@FXML private LobbyController lobbyController;
|
@FXML private LobbyController lobbyController;
|
||||||
|
|
||||||
|
private MatchBrowserInterface matchBrowserInterface;
|
||||||
|
private DatagramSocket udpSocket;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Ctor.
|
* Ctor.
|
||||||
*/
|
*/
|
||||||
public MainController() {
|
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
|
* Transitions into lobby screen
|
||||||
*/
|
*/
|
||||||
public void enterLobby(){ lobbyController.enterLobby(); }
|
public void enterLobby(){ lobbyController.enterLobby(udpSocket); }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Transitions into host game screen
|
* 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
|
* Sets up the css for the start of the program
|
||||||
|
|||||||
@@ -3,31 +3,81 @@ package visualiser.network;
|
|||||||
import network.BinaryMessageEncoder;
|
import network.BinaryMessageEncoder;
|
||||||
import network.Exceptions.InvalidMessageException;
|
import network.Exceptions.InvalidMessageException;
|
||||||
import network.MessageEncoders.HostGameMessageEncoder;
|
import network.MessageEncoders.HostGameMessageEncoder;
|
||||||
|
import network.MessageEncoders.HostedGamesRequestEncoder;
|
||||||
import network.Messages.AC35Data;
|
import network.Messages.AC35Data;
|
||||||
import network.Messages.Enums.MessageType;
|
import network.Messages.Enums.MessageType;
|
||||||
|
import network.Messages.HostGamesRequest;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.net.DatagramPacket;
|
||||||
import java.net.DatagramSocket;
|
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
|
* UDP interface for the matchBrowser to send out hosted game info and get in other hosts info
|
||||||
*/
|
*/
|
||||||
public class MatchBrowserInterface {
|
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;
|
byte[] fullMessageToSend;
|
||||||
try{
|
try{
|
||||||
HostGameMessageEncoder encoder = new HostGameMessageEncoder();
|
HostGameMessageEncoder encoder = new HostGameMessageEncoder();
|
||||||
byte[] message = encoder.encode(gameInfo);
|
byte[] message = encoder.encode(gameInfo);
|
||||||
BinaryMessageEncoder messageEncoder = new BinaryMessageEncoder(MessageType.HOST_GAME
|
BinaryMessageEncoder messageEncoder = new BinaryMessageEncoder(MessageType.HOST_GAME
|
||||||
,System.currentTimeMillis(), 1,(short) 14 ,message);
|
,System.currentTimeMillis(), 1,(short) 13 ,message);
|
||||||
fullMessageToSend = messageEncoder.getFullMessage();
|
fullMessageToSend = messageEncoder.getFullMessage();
|
||||||
|
|
||||||
|
DatagramPacket sendPacket = new DatagramPacket(fullMessageToSend, fullMessageToSend.length, IPAddress, port);
|
||||||
|
socket.send(sendPacket);
|
||||||
|
|
||||||
}catch (InvalidMessageException e){
|
}catch (InvalidMessageException e){
|
||||||
System.err.println("HostGameMessage could not be encoded");
|
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