hosting a game now continues to send out the info of the race

#story[1188]
This commit is contained in:
hba56
2017-08-31 15:06:43 +12:00
parent a1fa09962a
commit d1c0797db7
7 changed files with 61 additions and 10 deletions
@@ -59,7 +59,7 @@ public class HostController extends Controller {
public void alertMatchBrowser(){
MatchBrowserInterface matchBrowserInterface = new MatchBrowserInterface();
try{
matchBrowserInterface.sendOutGameInfo(this.game.getHostedGameData(), udpSocket);
matchBrowserInterface.startSendingHostData(this.game.getHostedGameData(), udpSocket);
}catch (IOException e){
System.err.println("failed to send out hosted game info");
}
@@ -0,0 +1,41 @@
package visualiser.network;
import network.Exceptions.InvalidMessageException;
import network.Messages.AC35Data;
import network.Messages.HostGame;
import shared.model.RunnableWithFramePeriod;
import java.io.IOException;
import java.net.DatagramSocket;
public class MatchBrowserHostRunnable implements RunnableWithFramePeriod {
private MatchBrowserInterface matchBrowserInterface;
private DatagramSocket socket;
private AC35Data gameInfo;
public MatchBrowserHostRunnable(MatchBrowserInterface matchBrowserInterface, DatagramSocket socket, AC35Data gameInfo) {
this.matchBrowserInterface = matchBrowserInterface;
this.socket = socket;
this.gameInfo = gameInfo;
}
@Override
public void run(){
long previousFrameTime = System.currentTimeMillis();
while (!Thread.interrupted()) {
long currentFrameTime = System.currentTimeMillis();
waitForFramePeriod(previousFrameTime, currentFrameTime, 10000);
previousFrameTime = currentFrameTime;
try{
matchBrowserInterface.sendOutGameInfo(gameInfo, socket);
}catch (IOException e){
System.err.println("HostGameMessage could not be sent");
}
}
}
}
@@ -7,11 +7,14 @@ import network.MessageEncoders.HostedGamesRequestEncoder;
import network.Messages.AC35Data;
import network.Messages.Enums.MessageType;
import network.Messages.HostGamesRequest;
import shared.model.RunnableWithFramePeriod;
import java.io.IOException;
import java.net.*;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* UDP interface for the matchBrowser to send out hosted game info and get in other hosts info
@@ -39,7 +42,7 @@ public class MatchBrowserInterface {
* @param gameInfo the hostGame info for message
* @param socket the udp socket assigned on startup
*/
public void sendOutGameInfo(AC35Data gameInfo, DatagramSocket socket) throws IOException{
protected void sendOutGameInfo(AC35Data gameInfo, DatagramSocket socket) throws IOException{
byte[] fullMessageToSend;
try{
HostGameMessageEncoder encoder = new HostGameMessageEncoder();
@@ -50,12 +53,20 @@ public class MatchBrowserInterface {
DatagramPacket sendPacket = new DatagramPacket(fullMessageToSend, fullMessageToSend.length, IPAddress, port);
socket.send(sendPacket);
}catch (InvalidMessageException e){
System.err.println("HostGameMessage could not be encoded");
}
}
/**
* start to send these messages on repeat until game stopped
*/
public void startSendingHostData(AC35Data gameInfo, DatagramSocket socket){
MatchBrowserHostRunnable hostRunnable = new MatchBrowserHostRunnable(this, socket, gameInfo);
Thread hostRunnableThread = new Thread(hostRunnable, "Socket: " + socket.toString());
hostRunnableThread.start();
}
/**
* Used by a client to setup a connection with the match browser server
* @return the socket created for this connection