parent
5676d435b0
commit
f8e1a539c2
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -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…
Reference in new issue