up
This commit is contained in:
parent
bc64f03ab3
commit
e5c3202bb2
10 changed files with 927 additions and 416 deletions
|
|
@ -1,31 +1,19 @@
|
|||
|
||||
package com.texttwist.server.components;
|
||||
import com.sun.org.apache.xpath.internal.operations.Bool;
|
||||
import com.texttwist.server.models.Dictionary;
|
||||
import com.texttwist.server.models.Match;
|
||||
import com.texttwist.server.tasks.SendInvitations;
|
||||
import com.texttwist.server.tasks.WaitForPlayers;
|
||||
import constants.Config;
|
||||
import jdk.nashorn.internal.parser.JSONParser;
|
||||
import models.Message;
|
||||
import models.Session;
|
||||
import org.json.simple.JsonObject;
|
||||
import utilities.Logger;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.io.InputStream;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.net.*;
|
||||
import java.io.IOException;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.channels.*;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.Future;
|
||||
|
|
@ -35,15 +23,12 @@ import static java.nio.channels.SelectionKey.OP_READ;
|
|||
|
||||
public class GameServer implements Runnable{
|
||||
|
||||
protected int serverPort;
|
||||
protected ServerSocketChannel serverSocketChannel = null;
|
||||
protected ThreadProxy proxy;
|
||||
ByteBuffer buffer = ByteBuffer.allocate(1024);
|
||||
ByteBuffer buffer2 = ByteBuffer.allocate(1024);
|
||||
private int serverPort;
|
||||
private ThreadProxy proxy;
|
||||
|
||||
DatagramChannel datagramChannel;
|
||||
protected Selector selector = null;
|
||||
protected ExecutorService threadPool = Executors.newCachedThreadPool();
|
||||
private DatagramChannel datagramChannel;
|
||||
private Selector selector = null;
|
||||
private ExecutorService threadPool = Executors.newCachedThreadPool();
|
||||
private String dictionaryPath = "./Server/resources/dictionary";
|
||||
public static Dictionary dict;
|
||||
|
||||
|
|
@ -60,11 +45,11 @@ public class GameServer implements Runnable{
|
|||
dict = new Dictionary(dictionaryPath);
|
||||
try {
|
||||
selector = Selector.open();
|
||||
serverSocketChannel = ServerSocketChannel.open();
|
||||
|
||||
ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
|
||||
serverSocketChannel.configureBlocking(false);
|
||||
serverSocketChannel.socket().bind(new InetSocketAddress(serverPort));
|
||||
serverSocketChannel.register(selector, OP_ACCEPT);
|
||||
// datagramSocket = new DatagramSocket(Config.WordsReceiverServerPort);
|
||||
InetSocketAddress address = new InetSocketAddress(Config.WordsReceiverServerPort);
|
||||
datagramChannel = DatagramChannel.open();
|
||||
DatagramSocket datagramSocket = datagramChannel.socket();
|
||||
|
|
@ -84,7 +69,7 @@ public class GameServer implements Runnable{
|
|||
|
||||
Iterator<SelectionKey> iter = selector.selectedKeys().iterator();
|
||||
while (iter.hasNext()) {
|
||||
buffer = ByteBuffer.allocate(1024);
|
||||
ByteBuffer bufferMessages = ByteBuffer.allocate(1024);
|
||||
SocketChannel client = null;
|
||||
SelectionKey key = iter.next();
|
||||
iter.remove();
|
||||
|
|
@ -99,16 +84,16 @@ public class GameServer implements Runnable{
|
|||
|
||||
case OP_READ:
|
||||
client = (SocketChannel) key.channel();
|
||||
//buffer.clear();
|
||||
if (client.read(buffer) != -1) {
|
||||
buffer.flip();
|
||||
String line = new String(buffer.array(), buffer.position(), buffer.remaining());
|
||||
if (client.read(bufferMessages) != -1) {
|
||||
bufferMessages.flip();
|
||||
String line = new String(bufferMessages.array(), bufferMessages.position(), bufferMessages.remaining());
|
||||
|
||||
if (line.startsWith("MESSAGE")) {
|
||||
SessionsManager.getInstance().printAll();
|
||||
Message msg = Message.toMessage(line);
|
||||
proxy = new ThreadProxy(msg, client, datagramChannel, buffer2);
|
||||
Future<Boolean> identifyMessage = threadPool.submit(proxy);
|
||||
ByteBuffer bufferWords = ByteBuffer.allocate(1024);
|
||||
proxy = new ThreadProxy(msg, client, datagramChannel, bufferWords);
|
||||
threadPool.submit(proxy);
|
||||
System.out.println(line);
|
||||
}
|
||||
|
||||
|
|
@ -136,9 +121,8 @@ public class GameServer implements Runnable{
|
|||
} catch (IOException e1) {
|
||||
e1.printStackTrace();
|
||||
}
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -27,6 +27,7 @@ public class ThreadProxy implements Callable<Boolean> {
|
|||
private final SocketChannel socketChannel;
|
||||
private final DatagramChannel datagramChannel;
|
||||
private ByteBuffer buffer;
|
||||
boolean matchNotAvailable =false;
|
||||
|
||||
|
||||
ThreadProxy(Message request, SocketChannel socketChannel, DatagramChannel datagramChannel, ByteBuffer buffer){
|
||||
|
|
@ -86,6 +87,7 @@ public class ThreadProxy implements Callable<Boolean> {
|
|||
new SendMessageToAllPlayers(match, new Message("JOIN_TIMEOUT", "", "", new DefaultListModel<>()), socketChannel));
|
||||
Boolean sendMessageJoinTimeoutRes = sendMessageJoinTimeout.get();
|
||||
if(!sendMessageJoinTimeoutRes){
|
||||
System.out.println("RIMOSSO");
|
||||
activeMatches.remove(Match.findMatchIndex(activeMatches,match.matchCreator));
|
||||
return sendMessageJoinTimeoutRes;
|
||||
}
|
||||
|
|
@ -145,59 +147,76 @@ public class ThreadProxy implements Callable<Boolean> {
|
|||
System.out.print("START THE GAME!!!!");
|
||||
|
||||
final Match match = Match.findMatch(activeMatches, request.data.get(0));
|
||||
System.out.println(match.matchCreator);
|
||||
if(match.joinTimeout == false) {
|
||||
Future<DefaultListModel<String>> generateLetters = threadPool.submit(new GenerateLetters());
|
||||
match.setLetters(generateLetters.get());
|
||||
match.letters.addElement(String.valueOf(match.multicastId));
|
||||
|
||||
Future<DefaultListModel<String>> generateLetters = threadPool.submit(new GenerateLetters());
|
||||
match.setLetters(generateLetters.get());
|
||||
match.letters.addElement(String.valueOf(match.multicastId));
|
||||
for (int i = 0; i < match.playersSocket.size(); i++) {
|
||||
System.out.println("INVIO");
|
||||
SocketChannel socketClient = match.playersSocket.get(i).getValue();
|
||||
if (socketClient != null) {
|
||||
buffer.clear();
|
||||
Message message = new Message("GAME_STARTED", "", "", match.letters);
|
||||
match.startGame();
|
||||
byteMessage = message.toString().getBytes();
|
||||
|
||||
for (int i =0; i< match.playersSocket.size(); i++) {
|
||||
System.out.println("INVIO");
|
||||
SocketChannel socketClient = match.playersSocket.get(i).getValue();
|
||||
if(socketClient != null) {
|
||||
buffer.clear();
|
||||
Message message = new Message("GAME_STARTED", "", "", match.letters);
|
||||
match.startGame();
|
||||
byteMessage = message.toString().getBytes();
|
||||
|
||||
buffer = ByteBuffer.wrap(byteMessage);
|
||||
try {
|
||||
socketClient.write(buffer);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
buffer = ByteBuffer.wrap(byteMessage);
|
||||
try {
|
||||
socketClient.write(buffer);
|
||||
} catch (IOException e) {
|
||||
System.out.println("GAME NOT EXIST");
|
||||
buffer = ByteBuffer.allocate(1024);
|
||||
if (socketChannel != null) {
|
||||
Message msg = new Message("MATCH_NOT_AVAILABLE", "", null, new DefaultListModel<>());
|
||||
buffer.clear();
|
||||
System.out.println("Il match richiesto non è più disponibile ");
|
||||
byteMessage = msg.toString().getBytes();
|
||||
buffer = ByteBuffer.wrap(byteMessage);
|
||||
socketChannel.write(buffer);
|
||||
e.printStackTrace();
|
||||
matchNotAvailable = true;
|
||||
}
|
||||
}
|
||||
//clientSocket.close();
|
||||
}
|
||||
//clientSocket.close();
|
||||
}
|
||||
if (!matchNotAvailable) {
|
||||
|
||||
}
|
||||
//Start receive words: tempo masimo 5 minuti per completare l'invio delle lettere.
|
||||
Future<Boolean> receiveWords = threadPool.submit(new ReceiveWords(match, datagramChannel, buffer));
|
||||
Boolean receiveWordsRes = receiveWords.get();
|
||||
|
||||
//Start receive words: tempo masimo 5 minuti per completare l'invio delle lettere.
|
||||
Future<Boolean> receiveWords = threadPool.submit(new ReceiveWords(match, datagramChannel, buffer));
|
||||
Boolean receiveWordsRes = receiveWords.get();
|
||||
if (receiveWordsRes) {
|
||||
System.out.println("ZERO PUNTI a chi non ha ancora inviato le lettere, TIMER SCADUTO");
|
||||
} else {
|
||||
System.out.println("TUTTI I GIOCATORI HANNO CONSEGNATO IN TEMPO");
|
||||
}
|
||||
|
||||
if(receiveWordsRes){
|
||||
System.out.println("ZERO PUNTI a chi non ha ancora inviato le lettere, TIMER SCADUTO");
|
||||
} else {
|
||||
System.out.println("TUTTI I GIOCATORI HANNO CONSEGNATO IN TEMPO");
|
||||
}
|
||||
match.setUndefinedScorePlayersToZero();
|
||||
|
||||
match.setUndefinedScorePlayersToZero();
|
||||
while (true) {
|
||||
Message msg = new Message("FINALSCORE", "SERVER", "", match.getMatchPlayersScoreAsStringList());
|
||||
|
||||
while(true) {
|
||||
Message msg = new Message("FINALSCORE", "SERVER", "", match.getMatchPlayersScoreAsStringList());
|
||||
MulticastSocket multicastSocket = new MulticastSocket(match.multicastId);
|
||||
System.out.println(multicastSocket);
|
||||
System.out.println(match.multicastId);
|
||||
InetAddress ia = InetAddress.getByName(Config.ScoreMulticastServerURI);
|
||||
DatagramPacket hi = new DatagramPacket(msg.toString().getBytes(), msg.toString().length(), ia, match.multicastId);
|
||||
System.out.println(msg.toString());
|
||||
multicastSocket.send(hi);
|
||||
|
||||
MulticastSocket multicastSocket = new MulticastSocket(match.multicastId);
|
||||
System.out.println(multicastSocket);
|
||||
System.out.println(match.multicastId);
|
||||
InetAddress ia = InetAddress.getByName(Config.ScoreMulticastServerURI);
|
||||
DatagramPacket hi = new DatagramPacket(msg.toString().getBytes(), msg.toString().length(), ia, match.multicastId);
|
||||
System.out.println(msg.toString());
|
||||
multicastSocket.send(hi);
|
||||
System.out.println(Match.findMatchIndex(activeMatches, match.matchCreator));
|
||||
|
||||
System.out.println(Match.findMatchIndex(activeMatches, match.matchCreator));
|
||||
activeMatches.remove(Match.findMatchIndex(activeMatches, match.matchCreator));
|
||||
//multicastSocket.disconnect();
|
||||
//multicastSocket.close();
|
||||
}
|
||||
|
||||
activeMatches.remove(Match.findMatchIndex(activeMatches, match.matchCreator));
|
||||
//multicastSocket.disconnect();
|
||||
//multicastSocket.close();
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
//RISPONDI CON LA CLASSIFICA
|
||||
// break;
|
||||
|
|
|
|||
|
|
@ -21,6 +21,7 @@ public class Match {
|
|||
private boolean started = false;
|
||||
public final String matchCreator;
|
||||
public Integer multicastId;
|
||||
public boolean joinTimeout =true;
|
||||
public DefaultListModel<String> letters;
|
||||
public final List<Pair<String,Integer>> playersScore = Collections.synchronizedList(new ArrayList<>());
|
||||
|
||||
|
|
|
|||
|
|
@ -1,10 +1,13 @@
|
|||
package com.texttwist.server.tasks;
|
||||
|
||||
import com.sun.org.apache.xml.internal.security.algorithms.MessageDigestAlgorithm;
|
||||
import com.texttwist.server.models.Match;
|
||||
import javafx.util.Pair;
|
||||
import models.Message;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.net.Socket;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.channels.SocketChannel;
|
||||
import java.util.concurrent.Callable;
|
||||
|
||||
|
|
@ -63,6 +66,7 @@ public class JoinMatch implements Callable<Boolean> {
|
|||
return false;
|
||||
}
|
||||
}
|
||||
match.joinTimeout=false;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,12 +1,5 @@
|
|||
package com.texttwist.server.tasks;
|
||||
|
||||
import com.texttwist.client.App;
|
||||
import com.texttwist.server.models.Match;
|
||||
import models.Message;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.channels.SocketChannel;
|
||||
import java.util.concurrent.*;
|
||||
|
||||
/**
|
||||
|
|
@ -18,16 +11,22 @@ public class JoinTimeout implements Callable<Boolean> {
|
|||
|
||||
public JoinTimeout(Match match) {
|
||||
this.match = match;
|
||||
System.out.println("Math started, countdown for join!");
|
||||
System.out.println("Match started, countdown for join!");
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean call() throws Exception {
|
||||
try {
|
||||
Thread.currentThread().sleep(7*60*1000);
|
||||
Thread.currentThread().sleep(1*5*1000);
|
||||
|
||||
System.out.println("TIMEOUT - MANDA MESSAGGIO ERRORE A TUTTI GLI UTENTI DEL MATCH");
|
||||
return false;
|
||||
if(match.joinTimeout) {
|
||||
return false;
|
||||
}
|
||||
else {
|
||||
return true;
|
||||
}
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -29,19 +29,23 @@ public class SendMessageToAllPlayers implements Callable<Boolean> {
|
|||
|
||||
if(!match.isStarted()) {
|
||||
for (int i = 0; i < match.playersSocket.size(); i++) {
|
||||
System.out.println(match.playersSocket.size());
|
||||
socketChannel = match.playersSocket.get(i).getValue();
|
||||
if (socketChannel != null) {
|
||||
buffer.clear();
|
||||
System.out.println("INVIO MESSAGGIO TIMEOUT A " + match.playersSocket.get(i).getKey());
|
||||
byte[] byteMessage = message.toString().getBytes();
|
||||
buffer = ByteBuffer.wrap(byteMessage);
|
||||
socketChannel.write(buffer);
|
||||
//clientSocket.close();
|
||||
System.out.println("SEND TO ALL" + message.toString());
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
return false;
|
||||
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue