updating match table to store ip and port of clients and hosts

#story[1188]
This commit is contained in:
hba56
2017-08-31 13:45:04 +12:00
parent 960b1213fb
commit 9161f69547
8 changed files with 99 additions and 26 deletions
@@ -0,0 +1,43 @@
package model;
public class ClientAdress {
private String ip;
private int port;
public ClientAdress(String ip, int port) {
this.ip = ip;
this.port = port;
}
public String getIp() {
return ip;
}
public int getPort() {
return port;
}
@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;
}
@Override
public int hashCode() {
int result = ip != null ? ip.hashCode() : 0;
result = 31 * result + port;
return result;
}
@Override
public String toString() {
return "[ip='" + ip + '\'' +
", port=" + port+"]";
}
}
@@ -1,6 +1,8 @@
package model;
import network.Messages.HostGame;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
@@ -15,15 +17,18 @@ public class MatchTable {
this.matchTable = new HashMap<TableKey, List>();
}
public void addEntry(ArrayList newEntry) {
public void addEntry(HostGame newEntry) {
//create a key from the ip and port
TableKey entryKey = new TableKey((String) newEntry.get(0), (Integer) newEntry.get(1));
TableKey entryKey = new TableKey( newEntry.getIp(), newEntry.getPort());
//get the rest of the entry and use it as the value
List entryItems = new ArrayList();
for(int i = 2; i<newEntry.size(); i++) {
entryItems.add(newEntry.get(i));
}
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);
}
@@ -31,4 +36,10 @@ public class MatchTable {
public HashMap<TableKey, List> getMatchTable() {
return matchTable;
}
@Override
public String toString() {
return "[MatchTable=" + matchTable +
']';
}
}
@@ -27,4 +27,11 @@ public class TableKey {
result = 31 * result + ip.hashCode();
return result;
}
@Override
public String toString() {
return "[ip='" + ip + '\'' +
", port=" + port +
']';
}
}
@@ -1,5 +1,8 @@
package networkInterface;
import model.ClientAdress;
import model.MatchTable;
import model.TableKey;
import network.BinaryMessageDecoder;
import network.Exceptions.InvalidMessageException;
import network.MessageDecoders.HostGameMessageDecoder;
@@ -11,6 +14,7 @@ import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.*;
/**
* Holds the output for the network for
@@ -20,7 +24,12 @@ public class NetworkInterface {
private byte[] receiveData = new byte[1024];
private byte[] sendData = new byte[1024];
private Set<ClientAdress> clientsAddresses;
private MatchTable matchTable;
public NetworkInterface(){
this.clientsAddresses = new HashSet<ClientAdress>();
this.matchTable = new MatchTable();
try {
this.serverSocket = new DatagramSocket(3779);
@@ -45,19 +54,28 @@ public class NetworkInterface {
HostGame newKnownGame;
try{
newKnownGame = (HostGame) decoder.decode(messageDecoder.getMessageBody());
System.out.println(newKnownGame.getIp());
this.matchTable.addEntry(newKnownGame);
System.out.println(matchTable);
}catch (InvalidMessageException e){
System.out.println(e);
System.err.println("Message received that is not a hostedGame packet");
}
break;
case 109:
//decode and update table
//update known clients
HostedGamesRequestDecoder decoder2 = new HostedGamesRequestDecoder();
HostGamesRequest newKnownGames;
try{
newKnownGames = (HostGamesRequest) decoder2.decode(receivePacket.getData());
System.out.println(newKnownGames);
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()));
}
System.out.println(clientsAddresses);
}catch (InvalidMessageException e){
System.out.println(e);
System.err.println("Message received that is not a hostedGamesRequest packet");
}
break;
@@ -67,15 +85,6 @@ public class NetworkInterface {
//client ip and port
InetAddress IPAddress = receivePacket.getAddress();
int port = receivePacket.getPort();
// String capitalizedSentence = sentence.toUpperCase();
// sendData = capitalizedSentence.getBytes();
// DatagramPacket sendPacket =
// new DatagramPacket(sendData, sendData.length, IPAddress, port);
// serverSocket.send(sendPacket);
}
}
}
@@ -1,5 +1,7 @@
package model;
import network.Messages.Enums.RaceStatusEnum;
import network.Messages.HostGame;
import org.junit.Before;
import org.junit.Test;
@@ -18,7 +20,7 @@ public class MatchTableTest {
@Test
public void testTable() {
ArrayList entry = new ArrayList(Arrays.asList("127.0.0.1", 4942, 1, 1, 2, 6, 1));
HostGame entry = new HostGame("127.0.0.1", 4942, (byte)1, (byte)1, RaceStatusEnum.PRESTART, (byte)6, (byte)1);
testTable.addEntry(entry);