Added scheduled match list broadcast to MatchBrowser

- Changed MatchTable to map ClientAddress to HostGame
- Send match list as HostedGamesRequest to multicast address (239.0.0.1:4941)

#story[1188]
main
cbt24 8 years ago
parent d1c0797db7
commit 2f8daf757a

@ -1,10 +1,10 @@
package model; package model;
public class ClientAdress { public class ClientAddress {
private String ip; private String ip;
private int port; private int port;
public ClientAdress(String ip, int port) { public ClientAddress(String ip, int port) {
this.ip = ip; this.ip = ip;
this.port = port; this.port = port;
} }
@ -19,19 +19,13 @@ public class ClientAdress {
@Override @Override
public boolean equals(Object o) { public boolean equals(Object o) {
if (this == o) return true; return o != null && o instanceof ClientAddress && hashCode() == o.hashCode();
if (o == null || getClass() != o.getClass()) return false;
ClientAdress that = (ClientAdress) o;
if (port != that.port) return false;
return ip != null ? ip.equals(that.ip) : that.ip == null;
} }
@Override @Override
public int hashCode() { public int hashCode() {
int result = ip != null ? ip.hashCode() : 0; int result = ip != null ? ip.hashCode() : 0;
result = 31 * result + port; result *= 31;
return result; return result;
} }

@ -11,29 +11,17 @@ import java.util.List;
* Holds a table object that stores current games * Holds a table object that stores current games
*/ */
public class MatchTable { public class MatchTable {
private HashMap<TableKey, List> matchTable; private HashMap<ClientAddress, HostGame> matchTable;
public MatchTable() { public MatchTable() {
this.matchTable = new HashMap<TableKey, List>(); this.matchTable = new HashMap<>();
} }
public void addEntry(String ip, int port, HostGame newEntry) { public void addEntry(ClientAddress address, HostGame newEntry) {
//create a key from the ip and port this.matchTable.put(address, newEntry);
TableKey entryKey = new TableKey(ip, port);
//get the rest of the entry and use it as the value
List entryItems = new ArrayList();
entryItems.add(newEntry.getMap());
entryItems.add(newEntry.getSpeed());
entryItems.add(newEntry.getStatus());
entryItems.add(newEntry.getRequiredNumPlayers());
entryItems.add(newEntry.getCurrentNumPlayers());
this.matchTable.put(entryKey, entryItems);
} }
public HashMap<TableKey, List> getMatchTable() { public HashMap<ClientAddress, HostGame> getMatchTable() {
return matchTable; return matchTable;
} }

@ -1,38 +1,52 @@
package networkInterface; package networkInterface;
import model.ClientAdress; import model.ClientAddress;
import model.MatchTable; import model.MatchTable;
import model.TableKey; import model.TableKey;
import network.BinaryMessageDecoder; import network.BinaryMessageDecoder;
import network.BinaryMessageEncoder;
import network.Exceptions.InvalidMessageException; import network.Exceptions.InvalidMessageException;
import network.MessageDecoders.HostGameMessageDecoder; import network.MessageDecoders.HostGameMessageDecoder;
import network.MessageDecoders.HostedGamesRequestDecoder; import network.MessageDecoders.HostedGamesRequestDecoder;
import network.MessageEncoders.HostGameMessageEncoder;
import network.MessageEncoders.HostedGamesRequestEncoder;
import network.Messages.Enums.MessageType;
import network.Messages.Enums.RaceStatusEnum;
import network.Messages.HostGame; import network.Messages.HostGame;
import network.Messages.HostGamesRequest; import network.Messages.HostGamesRequest;
import network.Messages.RaceStatus;
import java.io.IOException; import java.io.IOException;
import java.net.DatagramPacket; import java.net.DatagramPacket;
import java.net.DatagramSocket; import java.net.DatagramSocket;
import java.net.InetAddress; import java.net.InetAddress;
import java.time.LocalDateTime;
import java.util.*; import java.util.*;
/** /**
* Holds the output for the network for * Holds the output for the network for
*/ */
public class NetworkInterface { public class NetworkInterface {
private Timer scheduler;
private DatagramSocket serverSocket; private DatagramSocket serverSocket;
private byte[] receiveData = new byte[1024]; private byte[] receiveData = new byte[1024];
private byte[] sendData = new byte[1024];
private Set<ClientAdress> clientsAddresses; private Set<ClientAddress> clientsAddresses;
private MatchTable matchTable; private MatchTable matchTable;
private InetAddress groupAddress;
private int groupPort;
public NetworkInterface(){ public NetworkInterface(){
this.clientsAddresses = new HashSet<ClientAdress>(); this.clientsAddresses = new HashSet<>();
this.matchTable = new MatchTable(); this.matchTable = new MatchTable();
this.scheduler = new Timer(true);
try { try {
this.groupAddress = InetAddress.getByName("239.0.0.1");
this.groupPort = 4941;
this.serverSocket = new DatagramSocket(3779); this.serverSocket = new DatagramSocket(3779);
startBroadcast(10000);
this.run(); this.run();
} catch (IOException e) { } catch (IOException e) {
System.err.println("Error listening on port: " + this.serverSocket.getLocalPort() + "."); System.err.println("Error listening on port: " + this.serverSocket.getLocalPort() + ".");
@ -41,29 +55,51 @@ public class NetworkInterface {
} }
private void startBroadcast(int period) {
scheduler.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
List<HostGame> games = new ArrayList<>();
for(ClientAddress address: clientsAddresses) {
HostGame game = matchTable.getMatchTable().get(address);
if(game != null) {
games.add(game);
}
}
HostedGamesRequestEncoder encoder = new HostedGamesRequestEncoder();
try {
byte[] message = encoder.encode(new HostGamesRequest(games));
System.out.println(LocalDateTime.now() + ": Sending " + games.size() + " game/s");
serverSocket.send(new DatagramPacket(message, message.length, groupAddress, groupPort));
} catch (InvalidMessageException | IOException e) {
e.printStackTrace();
}
}
}, period, period);
}
private void run() throws IOException{ private void run() throws IOException{
while(true) { while(true) {
DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length); DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
serverSocket.receive(receivePacket); serverSocket.receive(receivePacket);
BinaryMessageDecoder messageDecoder = new BinaryMessageDecoder(receivePacket.getData()); BinaryMessageDecoder messageDecoder = new BinaryMessageDecoder(receivePacket.getData());
switch (messageDecoder.getHeaderMessageType()){ switch (MessageType.fromByte(messageDecoder.getHeaderMessageType())){
case 108: case HOST_GAME:
//decode and update table //decode and update table
HostGameMessageDecoder decoder = new HostGameMessageDecoder(); HostGameMessageDecoder decoder = new HostGameMessageDecoder();
HostGame newKnownGame; HostGame newKnownGame;
try{ try{
newKnownGame = (HostGame) decoder.decode(messageDecoder.getMessageBody()); newKnownGame = (HostGame) decoder.decode(messageDecoder.getMessageBody());
this.matchTable.addEntry(new ClientAddress(receivePacket.getAddress().getHostAddress(), receivePacket.getPort()), newKnownGame);
this.matchTable.addEntry(receivePacket.getAddress().getHostAddress(), receivePacket.getPort(), newKnownGame);
System.out.println(matchTable);
}catch (InvalidMessageException e){ }catch (InvalidMessageException e){
System.out.println(e); System.out.println(e);
System.err.println("Message received that is not a hostedGame packet"); System.err.println("Message received that is not a hostedGame packet");
} }
break; break;
case 109: case HOSTED_GAMES_REQUEST:
//update known clients //update known clients
HostedGamesRequestDecoder decoder2 = new HostedGamesRequestDecoder(); HostedGamesRequestDecoder decoder2 = new HostedGamesRequestDecoder();
HostGamesRequest newKnownGames; HostGamesRequest newKnownGames;
@ -71,20 +107,14 @@ public class NetworkInterface {
newKnownGames = (HostGamesRequest) decoder2.decode(messageDecoder.getMessageBody()); newKnownGames = (HostGamesRequest) decoder2.decode(messageDecoder.getMessageBody());
if (newKnownGames.getKnownGames().size() == 0){ if (newKnownGames.getKnownGames().size() == 0){
//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 ClientAddress(receivePacket.getAddress().getHostAddress(), receivePacket.getPort()));
} }
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");
} }
break; break;
} }
//client ip and port
InetAddress IPAddress = receivePacket.getAddress();
int port = receivePacket.getPort();
} }
} }
} }

@ -22,8 +22,8 @@ public class MatchTableTest {
public void testTable() { public void testTable() {
HostGame entry = new HostGame("127.0.0.1", 4942, (byte)1, (byte)1, RaceStatusEnum.PRESTART, (byte)6, (byte)1); HostGame entry = new HostGame("127.0.0.1", 4942, (byte)1, (byte)1, RaceStatusEnum.PRESTART, (byte)6, (byte)1);
testTable.addEntry("127.0.0.1", 3779, entry); testTable.addEntry(new ClientAddress("127.0.0.1", 3779), entry);
assertEquals(testTable.getMatchTable().get(new TableKey("127.0.0.1", 4942)), Arrays.asList(1, 1, 2, 6, 1)); assertEquals(testTable.getMatchTable().get(new ClientAddress("127.0.0.1", 4942)), entry);
} }
} }

Loading…
Cancel
Save