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

#story[1188]
main
hba56 8 years ago
parent 960b1213fb
commit 9161f69547

@ -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);

@ -17,6 +17,7 @@ import shared.model.Bearing;
import shared.model.Constants;
import java.io.IOException;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.nio.charset.StandardCharsets;
import java.time.ZonedDateTime;
@ -206,8 +207,8 @@ public class Event {
* @return hostGame Ac35DataType
*/
public HostGame getHostedGameData() throws IOException{
InetAddress ip = InetAddress.getByName("localhost");
return new HostGame(ip.getHostAddress(), 3779,(byte) 1,(byte) 1, RaceStatusEnum.PRESTART, (byte) 1, (byte) 1);
String ip = Inet4Address.getLocalHost().getHostAddress();
return new HostGame(ip, 3779,(byte) 1,(byte) 1, RaceStatusEnum.PRESTART, (byte) 1, (byte) 1);
}
}

@ -21,13 +21,14 @@ public class HostedGamesRequestDecoder implements MessageDecoder{
List<HostGame> knownGames = new ArrayList<>();
int byteIndex = 4;
for (int i = 0; i < numberOfGames; i++){
knownGames.add((HostGame) lineDecoder.decode(Arrays.copyOfRange(encodedMessage, byteIndex, byteIndex+14)));
knownGames.add((HostGame) lineDecoder.decode(Arrays.copyOfRange(encodedMessage, byteIndex, byteIndex+13)));
byteIndex += 13;
}
return new HostGamesRequest(knownGames);
} catch (Exception e) {
e.printStackTrace();
throw new InvalidMessageException("Could not decode Host game message.", e);
}
}

@ -9,11 +9,9 @@ import network.Messages.Enums.MessageType;
import network.Messages.HostGamesRequest;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.net.*;
import java.util.ArrayList;
import java.util.Enumeration;
/**
* UDP interface for the matchBrowser to send out hosted game info and get in other hosts info
@ -31,6 +29,7 @@ public class MatchBrowserInterface {
try {
this.IPAddress = InetAddress.getByName("localhost");
} catch (UnknownHostException e) {
e.printStackTrace();
}
this.port = 3779;
}

Loading…
Cancel
Save