|
|
|
@ -29,20 +29,16 @@ public class NetworkInterface {
|
|
|
|
private Set<ClientAddress> 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<>();
|
|
|
|
this.clientsAddresses = new HashSet<>();
|
|
|
|
this.matchTable = new MatchTable();
|
|
|
|
this.matchTable = new MatchTable();
|
|
|
|
this.scheduler = new Timer(true);
|
|
|
|
this.scheduler = new Timer(true);
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
this.groupAddress = InetAddress.getLocalHost(); //InetAddress.getByName("239.0.0.1");
|
|
|
|
|
|
|
|
this.groupPort = 4941;
|
|
|
|
|
|
|
|
this.serverSocket = new DatagramSocket(3779);
|
|
|
|
this.serverSocket = new DatagramSocket(3779);
|
|
|
|
|
|
|
|
|
|
|
|
startBroadcast(5000);
|
|
|
|
startBroadcast(5000);
|
|
|
|
scheduleFlush(30000);
|
|
|
|
scheduleFlush(70000);
|
|
|
|
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() + ".");
|
|
|
|
@ -73,7 +69,8 @@ public class NetworkInterface {
|
|
|
|
byte[] message = encoder.encode(new HostGamesRequest(games));
|
|
|
|
byte[] message = encoder.encode(new HostGamesRequest(games));
|
|
|
|
System.out.println(LocalDateTime.now() + ": Sending " + games.size() + " game/s");
|
|
|
|
System.out.println(LocalDateTime.now() + ": Sending " + games.size() + " game/s");
|
|
|
|
for(ClientAddress address: clientsAddresses) {
|
|
|
|
for(ClientAddress address: clientsAddresses) {
|
|
|
|
serverSocket.send(new DatagramPacket(message, message.length, InetAddress.getByName(address.getIp()), groupPort));
|
|
|
|
System.out.println("sending to: " + address.getIp());
|
|
|
|
|
|
|
|
serverSocket.send(new DatagramPacket(message, message.length, InetAddress.getByName(address.getIp()), 4941));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (InvalidMessageException | IOException e) {
|
|
|
|
} catch (InvalidMessageException | IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
e.printStackTrace();
|
|
|
|
@ -90,6 +87,7 @@ public class NetworkInterface {
|
|
|
|
scheduler.scheduleAtFixedRate(new TimerTask() {
|
|
|
|
scheduler.scheduleAtFixedRate(new TimerTask() {
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
public void run() {
|
|
|
|
|
|
|
|
System.out.println("flush");
|
|
|
|
matchTable.getMatchTable().clear();
|
|
|
|
matchTable.getMatchTable().clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}, period, period);
|
|
|
|
}, period, period);
|
|
|
|
|