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,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();
}
}