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.scheduler = new Timer(true);
try {
this.groupAddress = InetAddress.getByName("239.0.0.1");
this.groupAddress = InetAddress.getLocalHost(); //InetAddress.getByName("239.0.0.1");
this.groupPort = 4941;
this.serverSocket = new DatagramSocket(3779);

@ -10,6 +10,7 @@
<modules>
<module>racevisionGame</module>
<module>dedicatedServer</module>
<module>matchBrowser</module>
</modules>
<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.TextField;
import javafx.scene.layout.AnchorPane;
import network.Messages.HostGame;
import visualiser.model.RaceConnection;
import visualiser.network.MatchBrowserClientRunnable;
import visualiser.network.MatchBrowserInterface;
import visualiser.network.MatchBrowserLobbyInterface;
import java.io.IOException;
import java.net.DatagramSocket;
import java.net.Socket;
import java.net.SocketException;
import java.net.URL;
import java.util.Observable;
import java.util.Observer;
import java.util.ResourceBundle;
/**
@ -40,13 +47,18 @@ public class LobbyController extends Controller {
private ObservableList<RaceConnection> connections;
private ObservableList<RaceConnection> customConnections;
//the socket for match browser
private DatagramSocket udpSocket;
private MatchBrowserLobbyInterface matchBrowserLobbyInterface;
@Override
public void initialize(URL location, ResourceBundle resources) {
connections = FXCollections.observableArrayList();
customConnections = FXCollections.observableArrayList();
//connections.add(new RaceConnection("localhost", 4942, "Local Game"));
lobbyTable.setItems(connections);
@ -70,6 +82,7 @@ public class LobbyController extends Controller {
* Refreshes the connections in the lobby
*/
public void refreshBtnPressed(){
addServerGames();
for(RaceConnection connection: connections) {
connection.check();
}
@ -97,6 +110,7 @@ public class LobbyController extends Controller {
}
public void menuBtnPressed(){
matchBrowserLobbyInterface.closeSocket();
lobbyWrapper.setVisible(false);
parent.enterTitle();
}
@ -109,7 +123,8 @@ public class LobbyController extends Controller {
String portString = portFld.getText();
try{
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();
portFld.clear();
}catch(NumberFormatException e){
@ -127,5 +142,29 @@ public class LobbyController extends Controller {
public void enterLobby(DatagramSocket udpSocket){
lobbyWrapper.setVisible(true);
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() {
this.matchBrowserInterface = new MatchBrowserInterface();
try{
this.udpSocket = matchBrowserInterface.setupMatchBowserConnection();
this.udpSocket = matchBrowserInterface.setupMatchBrowserConnection();
}catch (IOException e){
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.Exceptions.InvalidMessageException;
import network.MessageDecoders.HostedGamesRequestDecoder;
import network.MessageEncoders.HostGameMessageEncoder;
import network.MessageEncoders.HostedGamesRequestEncoder;
import network.Messages.AC35Data;
@ -25,8 +26,6 @@ public class MatchBrowserInterface {
//port server is hosted on
private int port;
public MatchBrowserInterface() {
//TODO change to ip of cloud server hosting this
try {
@ -71,7 +70,7 @@ public class MatchBrowserInterface {
* 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{
public DatagramSocket setupMatchBrowserConnection() throws IOException{
DatagramSocket clientSocket = new DatagramSocket();
//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