This commit is contained in:
Lorenzo Iovino 2017-07-07 15:50:00 +02:00
parent e5c3202bb2
commit 3a528f307b
25 changed files with 1138 additions and 473 deletions

View file

@ -94,7 +94,6 @@ public class GameServer implements Runnable{
ByteBuffer bufferWords = ByteBuffer.allocate(1024);
proxy = new ThreadProxy(msg, client, datagramChannel, bufferWords);
threadPool.submit(proxy);
System.out.println(line);
}
if (line.startsWith("CLOSE")) {

View file

@ -24,7 +24,7 @@ public class NotificationServer implements INotificationServer {
public synchronized void registerForCallback(INotificationClient clientInterface) throws RemoteException {
if(!clients.contains(clientInterface)){
clients.add(clientInterface);
System.out.print("New client registered");
System.out.println("New client registered");
}
}
@ -37,16 +37,20 @@ public class NotificationServer implements INotificationServer {
}
public synchronized void sendInvitations(String username, DefaultListModel<String> users){
System.out.println("Starting callbacks");
Iterator i = clients.iterator();
while(i.hasNext()){
INotificationClient client = (INotificationClient) i.next();
try{
INotificationClient client = null;
System.out.println("Starting callbacks");
while (i.hasNext()) {
client = (INotificationClient) i.next();
try {
client.sendInvite(username, users);
} catch (RemoteException e) {
e.printStackTrace();
try {
unregisterForCallback(client);
} catch (RemoteException e1) {
e1.printStackTrace();
}
}
}
}
}

View file

@ -3,6 +3,7 @@ package com.texttwist.server.components;
import com.texttwist.server.models.Match;
import com.texttwist.server.tasks.*;
import constants.Config;
import javafx.util.Pair;
import models.Message;
import javax.swing.*;
@ -46,7 +47,6 @@ public class ThreadProxy implements Callable<Boolean> {
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){
case "START_GAME":
@ -87,7 +87,6 @@ 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;
}
@ -142,19 +141,16 @@ public class ThreadProxy implements Callable<Boolean> {
case "JOIN_GAME":
Future<Boolean> joinMatch = threadPool.submit(new JoinMatch(request.sender, request.data, socketChannel));
try {
Match match = Match.findMatch(activeMatches, request.data.get(0));;
Boolean joinMatchRes = joinMatch.get();
if(joinMatchRes){
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));
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();
@ -166,18 +162,7 @@ public class ThreadProxy implements Callable<Boolean> {
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();
}
@ -200,15 +185,9 @@ public class ThreadProxy implements Callable<Boolean> {
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);
System.out.println(Match.findMatchIndex(activeMatches, match.matchCreator));
activeMatches.remove(Match.findMatchIndex(activeMatches, match.matchCreator));
//multicastSocket.disconnect();
//multicastSocket.close();
@ -222,7 +201,19 @@ public class ThreadProxy implements Callable<Boolean> {
// break;
//ULTIMO A JOINARE! INIZIA GIOCO
} else {
System.out.print("WAIT FRIENDS");
if(match == null){
buffer = ByteBuffer.allocate(1024);
if (socketChannel != null) {
Message msg = new Message("MATCH_NOT_AVAILABLE", "", null, new DefaultListModel<>());
buffer.clear();
byteMessage = msg.toString().getBytes();
buffer = ByteBuffer.wrap(byteMessage);
socketChannel.write(buffer);
matchNotAvailable = true;
}
//Match non disponibile
}
//NON FARE NULLA, ASPETA GLI ALTRI
}
} catch (InterruptedException e) {

View file

@ -89,11 +89,9 @@ public class Match {
final Match m = findMatchByPlayer(player);
m.printAll();
System.out.println("ENTRO");
for (int i = 0; i < m.playersScore.size(); i++) {
if (m.playersScore.get(i).getKey().equals(player)) {
m.playersScore.set(i, new Pair<String, Integer>(player, score));
System.out.println("SEtting score of " + m.playersScore.get(i).getKey() + " to " + score);
}
}
}

View file

@ -17,7 +17,6 @@ public class CheckOnlineUsers implements Callable<Boolean> {
@Override
public Boolean call() throws Exception {
System.out.print("Check If users are online!");
for(int i = 0; i < users.size(); i++){
if(!(SessionsManager.getInstance().exists(users.get(i)))){
return false;

View file

@ -14,7 +14,6 @@ public class ComputeHighscores implements Callable<DefaultListModel<String>> {
@Override
public DefaultListModel<String> call() throws Exception {
System.out.println("COMPUTE HIGHSCORE");
DefaultListModel<String> l = new DefaultListModel<>();
for(int i =0; i< AccountsManager.getInstance().users.size(); i++){
l.addElement(AccountsManager.getInstance().users.get(i).userName+":"+AccountsManager.getInstance().users.get(i).score);

View file

@ -40,12 +40,11 @@ public class JoinMatch implements Callable<Boolean> {
thisMatch.playersStatus.add(new Pair<>(name, 1));
thisMatch.playersSocket.remove(j);
thisMatch.playersSocket.add(new Pair<>(name, socketChannel));
System.out.println(playerName + ": JOINED");
return allJoined(thisMatch);
}
}
}
return allJoined(thisMatch);
return false;
}

View file

@ -11,7 +11,6 @@ public class JoinTimeout implements Callable<Boolean> {
public JoinTimeout(Match match) {
this.match = match;
System.out.println("Match started, countdown for join!");
}
@ -20,7 +19,6 @@ public class JoinTimeout implements Callable<Boolean> {
try {
Thread.currentThread().sleep(1*5*1000);
System.out.println("TIMEOUT - MANDA MESSAGGIO ERRORE A TUTTI GLI UTENTI DEL MATCH");
if(match.joinTimeout) {
return false;
}

View file

@ -12,7 +12,6 @@ public class MatchTimeout implements Callable<Boolean> {
public MatchTimeout() {
System.out.println("GamePage started, countdown for end words!");
}
@ -20,11 +19,8 @@ public class MatchTimeout implements Callable<Boolean> {
public Boolean call() throws Exception {
try {
Thread.currentThread().sleep(3*60*1000); //TODO 5*60*1000
System.out.println("timer scaduto");
System.out.println("TIMEOUT - SETTA A 0 il punteggio degli utenti che non hanno inviato le parole");
return false;
} catch (InterruptedException e) {
System.out.println("TIMER BLOCATO PRIMA");
return true;
}
}

View file

@ -40,7 +40,6 @@ public class ReceiveWords implements Callable<Boolean>{
@Override
public Boolean call() throws Exception {
System.out.print("READY TO Receive words !!!!");
Future<Boolean> matchTimeout = threadPool.submit(new MatchTimeout());
@ -49,14 +48,11 @@ public class ReceiveWords implements Callable<Boolean>{
while(true) {
DatagramChannel.receive(buffer);
buffer.flip();
System.out.println(buffer.limit());
int limits = buffer.limit();
byte bytes[] = new byte[limits];
buffer.get(bytes, 0, limits);
String rcv = new String(bytes);
System.out.println("RECEIVED: " + rcv);
buffer.rewind();
msg = Message.toMessage(rcv);
if(msg.message.equals("WORDS")){
@ -67,12 +63,9 @@ public class ReceiveWords implements Callable<Boolean>{
//Se tutti hanno inviato le parole, blocca il timer e restituisci true
computeScore.get();
System.out.println(match.matchCreator);
System.out.println(match.allPlayersSendedHisScore());
if(match.allPlayersSendedHisScore()){
System.out.println("TIMEOUT BLOCCATO, OK");
match.setUndefinedScorePlayersToZero();
matchTimeout.cancel(true);

View file

@ -26,7 +26,6 @@ public class SendInvitations implements Callable<Boolean> {
@Override
public Boolean call() throws Exception {
System.out.print("SendInvitations!");
try {
Server.notificationServer.sendInvitations(sender, users);

View file

@ -29,16 +29,12 @@ 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());
}
}

View file

@ -25,8 +25,6 @@ public class WaitForPlayers implements Callable<Boolean> {
@Override
public Boolean call() throws Exception {
System.out.print("Wait for players!");
return true;
}
}