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;
public class ClientAdress {
public class ClientAddress {
private String ip;
private int port;
public ClientAdress(String ip, int port) {
public ClientAddress(String ip, int port) {
this.ip = ip;
this.port = port;
}
@ -19,19 +19,13 @@ public class ClientAdress {
@Override
public boolean equals(Object o) {
if (this == o) return true;
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;
return o != null && o instanceof ClientAddress && hashCode() == o.hashCode();
}
@Override
public int hashCode() {
int result = ip != null ? ip.hashCode() : 0;
result = 31 * result + port;
result *= 31;
return result;
}

@ -11,29 +11,17 @@ import java.util.List;
* Holds a table object that stores current games
*/
public class MatchTable {
private HashMap<TableKey, List> matchTable;
private HashMap<ClientAddress, HostGame> matchTable;
public MatchTable() {
this.matchTable = new HashMap<TableKey, List>();
this.matchTable = new HashMap<>();
}
public void addEntry(String ip, int port, HostGame newEntry) {
//create a key from the ip and port
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 void addEntry(ClientAddress address, HostGame newEntry) {
this.matchTable.put(address, newEntry);
}
public HashMap<TableKey, List> getMatchTable() {
public HashMap<ClientAddress, HostGame> getMatchTable() {
return matchTable;
}

@ -1,38 +1,52 @@
package networkInterface;
import model.ClientAdress;
import model.ClientAddress;
import model.MatchTable;
import model.TableKey;
import network.BinaryMessageDecoder;
import network.BinaryMessageEncoder;
import network.Exceptions.InvalidMessageException;
import network.MessageDecoders.HostGameMessageDecoder;
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.HostGamesRequest;
import network.Messages.RaceStatus;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.time.LocalDateTime;
import java.util.*;
/**
* Holds the output for the network for
*/
public class NetworkInterface {
private Timer scheduler;
private DatagramSocket serverSocket;
private byte[] receiveData = new byte[1024];
private byte[] sendData = new byte[1024];
private Set<ClientAdress> clientsAddresses;
private Set<ClientAddress> clientsAddresses;
private MatchTable matchTable;
private InetAddress groupAddress;
private int groupPort;
public NetworkInterface(){
this.clientsAddresses = new HashSet<ClientAdress>();
this.clientsAddresses = new HashSet<>();
this.matchTable = new MatchTable();
this.scheduler = new Timer(true);
try {
this.groupAddress = InetAddress.getByName("239.0.0.1");
this.groupPort = 4941;
this.serverSocket = new DatagramSocket(3779);
startBroadcast(10000);
this.run();
} catch (IOException e) {
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{
while(true) {
DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
serverSocket.receive(receivePacket);
BinaryMessageDecoder messageDecoder = new BinaryMessageDecoder(receivePacket.getData());
switch (messageDecoder.getHeaderMessageType()){
case 108:
switch (MessageType.fromByte(messageDecoder.getHeaderMessageType())){
case HOST_GAME:
//decode and update table
HostGameMessageDecoder decoder = new HostGameMessageDecoder();
HostGame newKnownGame;
try{
newKnownGame = (HostGame) decoder.decode(messageDecoder.getMessageBody());
this.matchTable.addEntry(receivePacket.getAddress().getHostAddress(), receivePacket.getPort(), newKnownGame);
System.out.println(matchTable);
this.matchTable.addEntry(new ClientAddress(receivePacket.getAddress().getHostAddress(), receivePacket.getPort()), newKnownGame);
}catch (InvalidMessageException e){
System.out.println(e);
System.err.println("Message received that is not a hostedGame packet");
}
break;
case 109:
case HOSTED_GAMES_REQUEST:
//update known clients
HostedGamesRequestDecoder decoder2 = new HostedGamesRequestDecoder();
HostGamesRequest newKnownGames;
@ -71,20 +107,14 @@ public class NetworkInterface {
newKnownGames = (HostGamesRequest) decoder2.decode(messageDecoder.getMessageBody());
if (newKnownGames.getKnownGames().size() == 0){
//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){
System.out.println(e);
System.err.println("Message received that is not a hostedGamesRequest packet");
}
break;
}
//client ip and port
InetAddress IPAddress = receivePacket.getAddress();
int port = receivePacket.getPort();
}
}
}

@ -22,8 +22,8 @@ public class MatchTableTest {
public void testTable() {
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