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

This commit is contained in:
Joseph
2017-09-04 09:46:13 +12:00
parent 5676d435b0
commit f8e1a539c2
7 changed files with 148 additions and 6 deletions
@@ -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();
}
}