Added synchronization for match

This commit is contained in:
Lorenzo Iovino 2017-06-25 19:43:22 +02:00
parent 9cc0297a5b
commit 648ea2ef7c
17 changed files with 2902 additions and 615 deletions

View file

@ -20,6 +20,7 @@ public class ThreadProxy implements Callable<Boolean> {
private Message request;
private SocketChannel socketChannel;
ThreadProxy(Message request, SocketChannel socketChannel){
this.request = request;
this.socketChannel = socketChannel;
@ -33,7 +34,7 @@ public class ThreadProxy implements Callable<Boolean> {
@Override
public Boolean call() {
ByteBuffer buffer = ByteBuffer.allocate(1024);
byte[] byteMessage = null;
System.out.println("Selecting right task for new thread");
if(isValidToken(request.token)){
switch(request.message){
@ -60,22 +61,33 @@ public class ThreadProxy implements Callable<Boolean> {
if(!joinMatchRes){
//NON FARE NULLA, ASPETTA GLI ALTRI
Message message = new Message("INVITES_ALL_SENDED", "", "", new DefaultListModel<String>());
byte[] byteMessage = new String(message.toString()).getBytes();
byteMessage = new String(message.toString()).getBytes();
buffer = ByteBuffer.wrap(byteMessage);
socketChannel.write(buffer);
Future<Boolean> matchTimeout = threadPool.submit(new MatchTimeout(match));
Boolean matchTimeoutRes = matchTimeout.get();
if(matchTimeoutRes){
System.out.println("TUTTO OKEY, INIZIA GIOCO");
} else {
System.out.println("MESSAGGIO ERRORE A TUTTI");
//socketChannel.close();
//TODO If game is started not send this message
if(!match.isStarted()) {
for (int i = 0; i < match.playersSocket.size(); i++) {
socketChannel = match.playersSocket.get(i).getValue();
if (socketChannel != null) {
buffer.clear();
message = new Message("TIMEOUT", "", "", new DefaultListModel<>());
byteMessage = new String(message.toString()).getBytes();
buffer = ByteBuffer.wrap(byteMessage);
socketChannel.write(buffer);
//clientSocket.close();
}
}
}
}
return matchTimeoutRes;
@ -92,9 +104,10 @@ public class ThreadProxy implements Callable<Boolean> {
} else {
Message message = new Message("USER_NOT_ONLINE", "", "", new DefaultListModel<String>());
byte[] byteMessage = new String(message.toString()).getBytes();
byteMessage = new String(message.toString()).getBytes();
buffer.clear();
buffer = ByteBuffer.wrap(byteMessage);
socketChannel.write(buffer);
this.socketChannel.write(buffer);
//socketChannel.close();
return false;
}
@ -112,6 +125,33 @@ public class ThreadProxy implements Callable<Boolean> {
Boolean joinMatchRes = joinMatch.get();
if(joinMatchRes){
System.out.print("START THE FUCKING GAME!!!!");
//Find the game, send broadcast communication
// buffer.flip();
Match match = Match.findMatch(request.data.get(0));
for (int i =0; i< match.playersSocket.size(); i++) {
System.out.println("INVIO");
socketChannel = match.playersSocket.get(i).getValue();
if(socketChannel!=null) {
buffer.clear();
Future<DefaultListModel<String>> generateWords = threadPool.submit(new GenerateWords());
Message message = new Message("GAME_STARTED", "", "", generateWords.get());
match.startGame();
byteMessage = new String(message.toString()).getBytes();
buffer = ByteBuffer.wrap(byteMessage);
try {
socketChannel.write(buffer);
} catch (IOException e) {
e.printStackTrace();
}
//clientSocket.close();
}
}
//ULTIMO A JOINARE! INIZIA GIOCO
} else {
System.out.print("WAIT FRIENDS");

View file

@ -7,13 +7,15 @@ import java.net.Socket;
import java.nio.channels.SocketChannel;
import java.util.*;
import static com.texttwist.server.components.GameServer.activeMatches;
/**
* Created by loke on 23/06/2017.
*/
public class Match {
public DefaultListModel<Pair<String,Integer>> playersStatus = new DefaultListModel<Pair<String, Integer>>();
public DefaultListModel<Pair<String,SocketChannel>> playersSocket = new DefaultListModel<Pair<String, SocketChannel>>();
private boolean started = false;
public String matchCreator;
public DefaultListModel<Pair<String,Integer>> playersScore = new DefaultListModel<Pair<String, Integer>>();
@ -26,4 +28,23 @@ public class Match {
this.matchCreator = matchCreator;
}
public static Match findMatch(String matchName){
for(int i = 0; i<activeMatches.size(); i++) {
if (activeMatches.get(i).matchCreator.equals(matchName)) {
return activeMatches.get(i);
}
}
return null;
}
public boolean isStarted(){
return started;
}
public void startGame(){
this.started=true;
}
}

View file

@ -0,0 +1,29 @@
package com.texttwist.server.tasks;
import javax.swing.*;
import java.util.concurrent.Callable;
/**
* Created by loke on 25/06/2017.
*/
public class GenerateWords implements Callable<DefaultListModel<String>> {
public GenerateWords(){
}
@Override
public DefaultListModel<String> call() throws Exception {
DefaultListModel l = new DefaultListModel<String>();
l.addElement("D");
l.addElement("S");
l.addElement("Q");
l.addElement("A");
return l;
}
}

View file

@ -9,6 +9,7 @@ import java.nio.channels.SocketChannel;
import java.util.concurrent.Callable;
import static com.texttwist.server.components.GameServer.activeMatches;
import static com.texttwist.server.models.Match.findMatch;
/**
* Created by loke on 23/06/2017.
@ -24,14 +25,6 @@ public class JoinMatch implements Callable<Boolean> {
this.socketChannel = socketChannel;
}
private Match findMatch(String matchName){
for(int i = 0; i<activeMatches.size(); i++) {
if (activeMatches.get(i).matchCreator.equals(matchName)) {
return activeMatches.get(i);
}
}
return null;
}
@Override
public Boolean call() throws Exception {
@ -40,14 +33,11 @@ public class JoinMatch implements Callable<Boolean> {
for(int j = 0; j<thisMatch.playersStatus.size(); j++){
String name = thisMatch.playersStatus.get(j).getKey();
if (name.equals(playerName)){
printAll(thisMatch);
thisMatch.playersStatus.remove(j);
thisMatch.playersStatus.addElement(new Pair<>(name,1));
thisMatch.playersSocket.remove(j);
thisMatch.playersSocket.addElement(new Pair<>(name,socketChannel));
System.out.println(playerName + ": JOINED");
printAll(thisMatch);
return allJoined(thisMatch);
}
}
@ -61,6 +51,8 @@ public class JoinMatch implements Callable<Boolean> {
System.out.println(match.playersStatus.get(i).getKey());
System.out.println(match.playersStatus.get(i).getValue());
System.out.println(match.playersSocket.get(i).getKey());
System.out.println(match.playersSocket.get(i).getValue());
}
}

View file

@ -27,19 +27,10 @@ public class MatchTimeout implements Callable<Boolean> {
ByteBuffer buffer = ByteBuffer.allocate(1024);
try {
Thread.currentThread().sleep(700000);
Thread.currentThread().sleep(7*60*1000);
System.out.println("TIMEOUT - MANDA MESSAGGIO ERRORE A TUTTI GLI UTENTI DEL MATCH");
for (int i =0; i< match.playersSocket.size(); i++) {
SocketChannel clientSocket = match.playersSocket.get(i).getValue();
Message message = new Message("TIMEOUT", "", "", new DefaultListModel<>());
byte[] byteMessage = new String(message.toString()).getBytes();
buffer = ByteBuffer.wrap(byteMessage);
clientSocket.write(buffer);
clientSocket.close();
}
return false;
} catch (InterruptedException e) {
e.printStackTrace();
}