Linked table to show games from received packets. #story[1188]

main
Joseph 8 years ago
parent 5676d435b0
commit f8e1a539c2

@ -42,7 +42,7 @@ public class NetworkInterface {
this.matchTable = new MatchTable(); this.matchTable = new MatchTable();
this.scheduler = new Timer(true); this.scheduler = new Timer(true);
try { try {
this.groupAddress = InetAddress.getByName("239.0.0.1"); this.groupAddress = InetAddress.getLocalHost(); //InetAddress.getByName("239.0.0.1");
this.groupPort = 4941; this.groupPort = 4941;
this.serverSocket = new DatagramSocket(3779); this.serverSocket = new DatagramSocket(3779);

@ -10,6 +10,7 @@
<modules> <modules>
<module>racevisionGame</module> <module>racevisionGame</module>
<module>dedicatedServer</module> <module>dedicatedServer</module>
<module>matchBrowser</module>
</modules> </modules>
<url>https://eng-git.canterbury.ac.nz/SENG302-2016/team-7</url> <url>https://eng-git.canterbury.ac.nz/SENG302-2016/team-7</url>

@ -8,12 +8,19 @@ import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView; import javafx.scene.control.TableView;
import javafx.scene.control.TextField; import javafx.scene.control.TextField;
import javafx.scene.layout.AnchorPane; import javafx.scene.layout.AnchorPane;
import network.Messages.HostGame;
import visualiser.model.RaceConnection; import visualiser.model.RaceConnection;
import visualiser.network.MatchBrowserClientRunnable;
import visualiser.network.MatchBrowserInterface;
import visualiser.network.MatchBrowserLobbyInterface;
import java.io.IOException; import java.io.IOException;
import java.net.DatagramSocket; import java.net.DatagramSocket;
import java.net.Socket; import java.net.Socket;
import java.net.SocketException;
import java.net.URL; import java.net.URL;
import java.util.Observable;
import java.util.Observer;
import java.util.ResourceBundle; import java.util.ResourceBundle;
/** /**
@ -40,13 +47,18 @@ public class LobbyController extends Controller {
private ObservableList<RaceConnection> connections; private ObservableList<RaceConnection> connections;
private ObservableList<RaceConnection> customConnections;
//the socket for match browser //the socket for match browser
private DatagramSocket udpSocket; private DatagramSocket udpSocket;
private MatchBrowserLobbyInterface matchBrowserLobbyInterface;
@Override @Override
public void initialize(URL location, ResourceBundle resources) { public void initialize(URL location, ResourceBundle resources) {
connections = FXCollections.observableArrayList(); connections = FXCollections.observableArrayList();
customConnections = FXCollections.observableArrayList();
//connections.add(new RaceConnection("localhost", 4942, "Local Game")); //connections.add(new RaceConnection("localhost", 4942, "Local Game"));
lobbyTable.setItems(connections); lobbyTable.setItems(connections);
@ -70,6 +82,7 @@ public class LobbyController extends Controller {
* Refreshes the connections in the lobby * Refreshes the connections in the lobby
*/ */
public void refreshBtnPressed(){ public void refreshBtnPressed(){
addServerGames();
for(RaceConnection connection: connections) { for(RaceConnection connection: connections) {
connection.check(); connection.check();
} }
@ -97,6 +110,7 @@ public class LobbyController extends Controller {
} }
public void menuBtnPressed(){ public void menuBtnPressed(){
matchBrowserLobbyInterface.closeSocket();
lobbyWrapper.setVisible(false); lobbyWrapper.setVisible(false);
parent.enterTitle(); parent.enterTitle();
} }
@ -109,7 +123,8 @@ public class LobbyController extends Controller {
String portString = portFld.getText(); String portString = portFld.getText();
try{ try{
int port = Integer.parseInt(portString); int port = Integer.parseInt(portString);
connections.add(new RaceConnection(hostName, port, "Boat Game")); customConnections.add(new RaceConnection(hostName, port, "Boat Game"));
connections.addAll(customConnections);
addressFld.clear(); addressFld.clear();
portFld.clear(); portFld.clear();
}catch(NumberFormatException e){ }catch(NumberFormatException e){
@ -127,5 +142,29 @@ public class LobbyController extends Controller {
public void enterLobby(DatagramSocket udpSocket){ public void enterLobby(DatagramSocket udpSocket){
lobbyWrapper.setVisible(true); lobbyWrapper.setVisible(true);
this.udpSocket = udpSocket; this.udpSocket = udpSocket;
matchBrowserLobbyInterface = new MatchBrowserLobbyInterface();
try {
matchBrowserLobbyInterface.startReceivingHostData(new DatagramSocket(4941));
Observer o = new Observer() {
@Override
public void update(Observable o, Object arg) {
refreshBtnPressed();
}
};
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);
for (HostGame game : matchBrowserLobbyInterface.getGames()) {
connections.add(new RaceConnection(game.getIp(), game.getPort(), "Boat Game"));
}
} }
} }

@ -38,7 +38,7 @@ public class MainController extends Controller {
public MainController() { public MainController() {
this.matchBrowserInterface = new MatchBrowserInterface(); this.matchBrowserInterface = new MatchBrowserInterface();
try{ try{
this.udpSocket = matchBrowserInterface.setupMatchBowserConnection(); this.udpSocket = matchBrowserInterface.setupMatchBrowserConnection();
}catch (IOException e){ }catch (IOException e){
System.err.println("Error in setting up connection with match browser"); System.err.println("Error in setting up connection with match browser");
} }

@ -0,0 +1,34 @@
package visualiser.network;
import network.Messages.AC35Data;
import shared.model.RunnableWithFramePeriod;
import java.io.IOException;
import java.net.DatagramSocket;
public class MatchBrowserClientRunnable implements RunnableWithFramePeriod {
private MatchBrowserLobbyInterface matchBrowserLobbyInterface;
private DatagramSocket socket;
public MatchBrowserClientRunnable(MatchBrowserLobbyInterface matchBrowserInterface, DatagramSocket socket) {
this.matchBrowserLobbyInterface = matchBrowserInterface;
this.socket = socket;
}
@Override
public void run(){
long previousFrameTime = System.currentTimeMillis();
while (!Thread.interrupted()) {
try{
matchBrowserLobbyInterface.receiveGameInfo(socket);
}catch (IOException e){
System.err.println("HostGameMessage could not be received");
}
long currentFrameTime = System.currentTimeMillis();
waitForFramePeriod(previousFrameTime, currentFrameTime, 10000);
previousFrameTime = currentFrameTime;
}
}
}

@ -2,6 +2,7 @@ package visualiser.network;
import network.BinaryMessageEncoder; import network.BinaryMessageEncoder;
import network.Exceptions.InvalidMessageException; import network.Exceptions.InvalidMessageException;
import network.MessageDecoders.HostedGamesRequestDecoder;
import network.MessageEncoders.HostGameMessageEncoder; import network.MessageEncoders.HostGameMessageEncoder;
import network.MessageEncoders.HostedGamesRequestEncoder; import network.MessageEncoders.HostedGamesRequestEncoder;
import network.Messages.AC35Data; import network.Messages.AC35Data;
@ -25,8 +26,6 @@ public class MatchBrowserInterface {
//port server is hosted on //port server is hosted on
private int port; private int port;
public MatchBrowserInterface() { public MatchBrowserInterface() {
//TODO change to ip of cloud server hosting this //TODO change to ip of cloud server hosting this
try { try {
@ -71,7 +70,7 @@ public class MatchBrowserInterface {
* Used by a client to setup a connection with the match browser server * Used by a client to setup a connection with the match browser server
* @return the socket created for this connection * @return the socket created for this connection
*/ */
public DatagramSocket setupMatchBowserConnection() throws IOException{ public DatagramSocket setupMatchBrowserConnection() throws IOException{
DatagramSocket clientSocket = new DatagramSocket(); DatagramSocket clientSocket = new DatagramSocket();
//creates and empty hostedGamesRequest packet that can be sent to the server //creates and empty hostedGamesRequest packet that can be sent to the server

@ -0,0 +1,69 @@
package visualiser.network;
import network.Exceptions.InvalidMessageException;
import network.MessageDecoders.HostedGamesRequestDecoder;
import network.Messages.HostGame;
import network.Messages.HostGamesRequest;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
/**
* Used to receive lobby information from server
*/
public class MatchBrowserLobbyInterface extends Observable {
private DatagramSocket socket;
private MatchBrowserClientRunnable clientRunnable;
private List<HostGame> games = new ArrayList<>();
public MatchBrowserLobbyInterface() {
}
/**
* start receiving game info
*/
public void startReceivingHostData(DatagramSocket socket) {
this.socket = socket;
clientRunnable = new MatchBrowserClientRunnable(this, socket);
Thread clientRunnableThread = new Thread(clientRunnable, "Socket: " + socket.toString());
clientRunnableThread.start();
}
/**
* Used by client to received race information from the server
*/
protected void receiveGameInfo(DatagramSocket socket) throws IOException {
byte[] data = new byte[64];
DatagramPacket receivedPacket = new DatagramPacket(data, 64);
socket.receive(receivedPacket);
HostedGamesRequestDecoder hostedGamesRequestDecoder = new HostedGamesRequestDecoder();
try {
HostGamesRequest message = (HostGamesRequest) hostedGamesRequestDecoder.decode(data);
games = message.getKnownGames();
setChanged();
notifyObservers();
} catch (InvalidMessageException e) {
System.err.println("HostedGamesRequestMessage could not be decoded");
}
}
/**
* Gets the host games
*/
public List<HostGame> getGames() {
return games;
}
/**
* Used to close the socket once out of the lobby
*/
public void closeSocket() {
this.socket.close();
}
}
Loading…
Cancel
Save