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

#story[1188]
main
hba56 8 years ago
parent a1fa09962a
commit d1c0797db7

@ -37,7 +37,7 @@ public class ClientAdress {
@Override @Override
public String toString() { public String toString() {
return "[ip='" + ip + '\'' + return "{ip='" + ip + '\'' +
", port=" + port+"]"; ", port=" + port+"}";
} }
} }

@ -29,8 +29,8 @@ public class MatchTable {
entryItems.add(newEntry.getRequiredNumPlayers()); entryItems.add(newEntry.getRequiredNumPlayers());
entryItems.add(newEntry.getCurrentNumPlayers()); entryItems.add(newEntry.getCurrentNumPlayers());
this.matchTable.put(entryKey, entryItems); this.matchTable.put(entryKey, entryItems);
} }
public HashMap<TableKey, List> getMatchTable() { public HashMap<TableKey, List> getMatchTable() {
@ -39,7 +39,6 @@ public class MatchTable {
@Override @Override
public String toString() { public String toString() {
return "[MatchTable=" + matchTable + return "MatchTable=" + matchTable;
']';
} }
} }

@ -18,7 +18,7 @@ public class TableKey {
if (this == o) return true; if (this == o) return true;
if (!(o instanceof TableKey)) return false; if (!(o instanceof TableKey)) return false;
TableKey key = (TableKey) o; TableKey key = (TableKey) o;
return ip == key.ip && port == key.port; return ip.equals(key.ip) && port == key.port;
} }
@Override @Override

@ -73,7 +73,7 @@ public class NetworkInterface {
//this is just an alert message with no content //this is just an alert message with no content
clientsAddresses.add(new ClientAdress(receivePacket.getAddress().getHostAddress(), receivePacket.getPort())); clientsAddresses.add(new ClientAdress(receivePacket.getAddress().getHostAddress(), receivePacket.getPort()));
} }
System.out.println(clientsAddresses); System.out.println("Clients: " + clientsAddresses);
}catch (InvalidMessageException e){ }catch (InvalidMessageException e){
System.out.println(e); System.out.println(e);
System.err.println("Message received that is not a hostedGamesRequest packet"); System.err.println("Message received that is not a hostedGamesRequest packet");

@ -59,7 +59,7 @@ public class HostController extends Controller {
public void alertMatchBrowser(){ public void alertMatchBrowser(){
MatchBrowserInterface matchBrowserInterface = new MatchBrowserInterface(); MatchBrowserInterface matchBrowserInterface = new MatchBrowserInterface();
try{ try{
matchBrowserInterface.sendOutGameInfo(this.game.getHostedGameData(), udpSocket); matchBrowserInterface.startSendingHostData(this.game.getHostedGameData(), udpSocket);
}catch (IOException e){ }catch (IOException e){
System.err.println("failed to send out hosted game info"); 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.AC35Data;
import network.Messages.Enums.MessageType; import network.Messages.Enums.MessageType;
import network.Messages.HostGamesRequest; import network.Messages.HostGamesRequest;
import shared.model.RunnableWithFramePeriod;
import java.io.IOException; import java.io.IOException;
import java.net.*; import java.net.*;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Enumeration; 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 * 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 gameInfo the hostGame info for message
* @param socket the udp socket assigned on startup * @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; byte[] fullMessageToSend;
try{ try{
HostGameMessageEncoder encoder = new HostGameMessageEncoder(); HostGameMessageEncoder encoder = new HostGameMessageEncoder();
@ -50,12 +53,20 @@ public class MatchBrowserInterface {
DatagramPacket sendPacket = new DatagramPacket(fullMessageToSend, fullMessageToSend.length, IPAddress, port); DatagramPacket sendPacket = new DatagramPacket(fullMessageToSend, fullMessageToSend.length, IPAddress, port);
socket.send(sendPacket); socket.send(sendPacket);
}catch (InvalidMessageException e){ }catch (InvalidMessageException e){
System.err.println("HostGameMessage could not be encoded"); 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 * Used by a client to setup a connection with the match browser server
* @return the socket created for this connection * @return the socket created for this connection

Loading…
Cancel
Save