From 5117b28e2cd298d6827fc073626d874e946b22a5 Mon Sep 17 00:00:00 2001 From: Lorenzo Iovino Date: Thu, 13 Jul 2017 14:17:32 +0200 Subject: [PATCH 01/13] non funziona --- .idea/workspace.xml | 1432 ++++++++--------- Client/src/com/texttwist/client/App.java | 2 +- .../com/texttwist/client/pages/GamePage.java | 2 +- Commons/src/constants/Config.java | 9 +- Server/src/com/texttwist/server/Main.java | 11 +- Server/src/com/texttwist/server/Server.java | 79 +- .../server/components/SessionsManager.java | 88 - .../texttwist/server/models/Dictionary.java | 26 +- .../com/texttwist/server/models/Match.java | 161 +- .../ProxyDispatcher.java} | 435 +++-- .../AccountsService.java} | 159 +- .../Auth.java => services/AuthService.java} | 167 +- .../JedisService.java | 6 +- .../MessageService.java} | 277 ++-- .../NotificationService.java} | 125 +- .../server/services/SessionsService.java | 77 + .../server/tasks/CheckOnlineUsers.java | 7 +- .../server/tasks/ComputeHighscores.java | 17 +- .../texttwist/server/tasks/ComputeScore.java | 21 +- .../server/tasks/GenerateLetters.java | 11 +- .../com/texttwist/server/tasks/JoinMatch.java | 5 +- .../texttwist/server/tasks/JoinTimeout.java | 3 +- .../texttwist/server/tasks/MatchTimeout.java | 14 +- .../texttwist/server/tasks/ReceiveWords.java | 9 +- .../server/tasks/SendInvitations.java | 10 +- .../server/tasks/SendMessageToAllPlayers.java | 3 +- .../texttwist/server/tasks/SendScores.java | 46 + .../texttwist/server/tasks/TokenInvalid.java | 9 +- notificationServer.log | 159 ++ 29 files changed, 1707 insertions(+), 1663 deletions(-) delete mode 100644 Server/src/com/texttwist/server/components/SessionsManager.java rename Server/src/com/texttwist/server/{components/ThreadProxy.java => servers/ProxyDispatcher.java} (92%) rename Server/src/com/texttwist/server/{components/AccountsManager.java => services/AccountsService.java} (80%) rename Server/src/com/texttwist/server/{components/Auth.java => services/AuthService.java} (79%) rename Server/src/com/texttwist/server/{components => services}/JedisService.java (95%) rename Server/src/com/texttwist/server/{components/GameServer.java => services/MessageService.java} (90%) rename Server/src/com/texttwist/server/{components/NotificationServer.java => services/NotificationService.java} (86%) create mode 100644 Server/src/com/texttwist/server/services/SessionsService.java create mode 100644 Server/src/com/texttwist/server/tasks/SendScores.java diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 0a31912..8380487 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -6,41 +6,44 @@ - + + + + + + + + + + + + + + + + + - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + @@ -63,11 +66,31 @@ - - + + - - + + + + + + + + + + + + + + + + + + + + + + @@ -75,73 +98,34 @@ - - + + - - - - - - - - - - - - + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + @@ -150,18 +134,42 @@ - - + + + + + + + + + + + + + + - + - - + + + + + + + + + + + + + + @@ -178,36 +186,36 @@ - Insert y - Insert you - Insert your - Insert your - Insert your d - Registration - Registration uns - R - regisr - regis - registration - registration unsu - registration unsuc - registration unsucce - registration unsuccess - G - GameService is - GameService is - T - TIMEO - TIMEOUT - Inse - Insert - Insert w - Insert wo - ga - game is rea - game is read - le - letters + m + ti + jo + joinTi + joinTimeo + joinTimeout= + joinTimeout + joinTimeout = + ma + math. + math + matc + match + match.jo + match.joi + match.join + match.joint + j + jin + ji + join + joinTime + joinTimeou + threadPool + 1000 + waiti + waiting + waiting f + waiting for + waiting for u C:\Users\loke\TextTwist\out\production @@ -346,38 +354,6 @@ @@ -416,10 +424,10 @@ - @@ -437,7 +445,7 @@ - + @@ -451,6 +459,138 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -535,58 +675,6 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -1443,47 +1321,72 @@ - + + - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - + - + - - + + + - - + + - - - - - @@ -1503,246 +1406,10 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -1750,9 +1417,6 @@ - - - @@ -1760,9 +1424,6 @@ - - - @@ -1770,9 +1431,6 @@ - - - @@ -1781,24 +1439,15 @@ - + - - - - - - - - - @@ -1806,17 +1455,6 @@ - - - - - - - - - - - @@ -1825,7 +1463,7 @@ - + @@ -1846,43 +1484,10 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -1890,56 +1495,31 @@ - - - + + - + - - - - - - - - - - - - - - - - - - - - - - - - - - + + - - + + @@ -1947,29 +1527,317 @@ - - - - - - - - - + - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Client/src/com/texttwist/client/App.java b/Client/src/com/texttwist/client/App.java index de9185d..e575db2 100644 --- a/Client/src/com/texttwist/client/App.java +++ b/Client/src/com/texttwist/client/App.java @@ -68,7 +68,7 @@ public class App extends JFrame { logger.write("APP: Font not found!"); } - /*Services*/ + /*services*/ gameService = new GameService(); authService = new AuthService(); diff --git a/Client/src/com/texttwist/client/pages/GamePage.java b/Client/src/com/texttwist/client/pages/GamePage.java index fc7e10e..ed3571a 100644 --- a/Client/src/com/texttwist/client/pages/GamePage.java +++ b/Client/src/com/texttwist/client/pages/GamePage.java @@ -141,7 +141,7 @@ public class GamePage extends Page { return null; } }), - Config.timeoutGame + Config.gameTimeout ); } } diff --git a/Commons/src/constants/Config.java b/Commons/src/constants/Config.java index 6c34a01..1496241 100644 --- a/Commons/src/constants/Config.java +++ b/Commons/src/constants/Config.java @@ -18,9 +18,14 @@ public class Config { public static String ScoreMulticastServerURI = "226.226.226.226"; public static Integer NotificationServerPort = 20000; - public static Integer NotificationServerStubPort = 5000; + public static Integer NotificationServerStubPort = 30000; public static String NotificationServerName ="notification"; - public static int timeoutGame = 120; + + public static String RedisServerURI = "localhost"; + + public static int gameTimeout = 10; //2 minuti in sec + public static int joinMatchTimeout = 5000; //7 minuti in millisec + public static int sendWordsTimeout = 3000; //5 minuti in millisec public static String getAuthServerURI(){ return "rmi://".concat(AuthServerURI).concat(":").concat(AuthServerPort.toString()); diff --git a/Server/src/com/texttwist/server/Main.java b/Server/src/com/texttwist/server/Main.java index f1484af..a292745 100644 --- a/Server/src/com/texttwist/server/Main.java +++ b/Server/src/com/texttwist/server/Main.java @@ -1,13 +1,12 @@ package com.texttwist.server; - -import utilities.Logger; - -import java.io.File; import java.io.IOException; +/** + * Author: Lorenzo Iovino on 14/06/2017. + * Description: Main + */ public class Main { - public static void main(String[] args) throws IOException { - Server ttServer = new Server(); + new Server(); } } diff --git a/Server/src/com/texttwist/server/Server.java b/Server/src/com/texttwist/server/Server.java index b2bee6d..7955285 100644 --- a/Server/src/com/texttwist/server/Server.java +++ b/Server/src/com/texttwist/server/Server.java @@ -1,65 +1,78 @@ package com.texttwist.server; -import com.texttwist.server.components.Auth; -import com.texttwist.server.components.GameServer; -import com.texttwist.server.components.NotificationServer; +import com.texttwist.server.services.AuthService; +import com.texttwist.server.services.MessageService; +import com.texttwist.server.services.NotificationService; import constants.Config; import interfaces.INotificationServer; -import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; import redis.clients.jedis.JedisPoolConfig; import utilities.Logger; - import java.io.File; import java.io.IOException; import java.rmi.AlreadyBoundException; +import java.rmi.RemoteException; import java.rmi.registry.LocateRegistry; import java.rmi.registry.Registry; import java.rmi.server.UnicastRemoteObject; + /** - * Created by loke on 15/06/2017. + * Author: Lorenzo Iovino on 15/06/2017. + * Description: Server. Initialize all services. */ public class Server { - public static NotificationServer notificationServer; + public static NotificationService notificationServer; public static JedisPool jedisPool; public static Logger logger; + public static AuthService auth; public Server() throws IOException { logger = new Logger(new File("./notificationServer.log"), "Server", true); - Server.logger.write("Server starting ..."); - - //Start services + Server.logger.write("Services starting ..."); + startAuthService(); + startJedisService(); + startMessageService(); + startNotificationService(); + Server.logger.write("Services started correctly ..."); + } + private void startAuthService(){ + //Starting Auth service based on RMI try { - //Definitions of registry for auth - Auth auth = new Auth(Config.AuthServerPort); + auth = new AuthService(Config.AuthServerPort); Registry authRegistry = LocateRegistry.createRegistry(auth.serverPort); authRegistry.bind("auth", auth); - - //Connecting to Redis server on localhost - jedisPool = new JedisPool(new JedisPoolConfig(), "localhost"); - - GameServer gameServer = new GameServer(Config.GameServerPort); - new Thread(gameServer).start(); - - try { - /*registrazione presso il registry */ - notificationServer = new NotificationServer(); - INotificationServer stub = (INotificationServer) UnicastRemoteObject.exportObject(notificationServer, Config.NotificationServerPort); - LocateRegistry.createRegistry(Config.NotificationServerStubPort); - Registry notificationRegistry = LocateRegistry.getRegistry(Config.NotificationServerStubPort); - notificationRegistry.bind(Config.NotificationServerName, stub); - - - } catch (Exception e) { - System.out.println("Eccezione" + e); - } - Server.logger.write("Server started"); + } catch (RemoteException e) { + Server.logger.write("SERVER: RMI authentication service error (Remote exception)"); } catch (AlreadyBoundException e) { - e.printStackTrace(); + Server.logger.write("SERVER: RMI authentication service can't use this port because is busy "); } } + private void startJedisService(){ + //Starting Jedis pool for Redis connection + jedisPool = new JedisPool(new JedisPoolConfig(), Config.RedisServerURI); + } + private void startMessageService(){ + //Starting the Message service based on TCP + new Thread(new MessageService(Config.GameServerPort)).start(); + } + + private void startNotificationService(){ + //Starting Notification service based on RMI + try { + notificationServer = new NotificationService(); + INotificationServer stub = (INotificationServer) UnicastRemoteObject.exportObject(notificationServer, Config.NotificationServerPort); + LocateRegistry.createRegistry(Config.NotificationServerStubPort); + + Registry notificationRegistry = LocateRegistry.getRegistry(Config.NotificationServerStubPort); + notificationRegistry.bind(Config.NotificationServerName, stub); + } catch (RemoteException e) { + Server.logger.write("SERVER: RMI notification service error (Remote exception)"); + } catch (AlreadyBoundException e) { + Server.logger.write("SERVER: RMI notification service can't use this port because is busy "); + } + } } diff --git a/Server/src/com/texttwist/server/components/SessionsManager.java b/Server/src/com/texttwist/server/components/SessionsManager.java deleted file mode 100644 index a2fa7bd..0000000 --- a/Server/src/com/texttwist/server/components/SessionsManager.java +++ /dev/null @@ -1,88 +0,0 @@ -package com.texttwist.server.components; -import interfaces.INotificationClient; -import models.Session; -import models.User; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.Iterator; -import java.util.List; - -/** - * Created by loke on 17/06/2017. - */ -public class SessionsManager { - - private List sessions = Collections.synchronizedList(new ArrayList()); - private static class Holder { - static final SessionsManager INSTANCE = new SessionsManager(); - } - - public static SessionsManager getInstance() { - return Holder.INSTANCE; - } - - private SessionsManager(){} - - public boolean add(String userName, String token) { - remove(userName); - return sessions.add(new Session(new User(userName,"",0), token)); - } - - public void printAll(){ - Iterator i = sessions.iterator(); - while (i.hasNext()) { - Session elem = i.next(); - System.out.println(elem.account.userName + " | " + elem.token); - } - - } - - public boolean remove(String userName){ - if(exists(userName)) { - Session s = getSession(userName); - if(s != null) { - sessions.remove(s); - return true; - } - } - return false; - } - - public Session getSession(String userName) { - Iterator i = sessions.iterator(); - while (i.hasNext()) { - Session elem = i.next(); - if (elem.account.userName.equals(userName)) { - return elem; - } - } - return null; - - } - - public boolean exists(String userName) { - Iterator i = sessions.iterator(); - while (i.hasNext()) { - Session elem = i.next(); - if (elem.account.userName.equals(userName)) { - return true; - } - } - return false; - - } - - - public boolean isValidToken(String token) { - Iterator i = sessions.iterator(); - while (i.hasNext()) { - if (i.next().token.equals(token)) { - return true; - } - } - return false; - - } - -} diff --git a/Server/src/com/texttwist/server/models/Dictionary.java b/Server/src/com/texttwist/server/models/Dictionary.java index 287e576..5d0a452 100644 --- a/Server/src/com/texttwist/server/models/Dictionary.java +++ b/Server/src/com/texttwist/server/models/Dictionary.java @@ -1,23 +1,20 @@ package com.texttwist.server.models; +import com.texttwist.server.Server; + import javax.swing.*; import java.io.*; -import java.nio.ByteBuffer; -import java.nio.channels.FileChannel; -import java.nio.charset.Charset; -import java.nio.file.Files; -import java.nio.file.Paths; -import java.nio.file.StandardOpenOption; import java.util.Random; -import java.util.stream.Stream; + /** - * Created by loke on 26/06/2017. + * Author: Lorenzo Iovino on 26/06/2017. + * Description: Dictionary Model. Provides the dictionary and methods for manage it */ + public class Dictionary { - static DefaultListModel wordList = new DefaultListModel<>(); - private Random randomGenerator; + private static DefaultListModel wordList = new DefaultListModel<>(); public Dictionary(String dictionaryPath) { try (BufferedReader br = new BufferedReader(new FileReader(new File(dictionaryPath)))) { @@ -25,15 +22,16 @@ public class Dictionary { wordList.addElement(line); } } catch (FileNotFoundException e) { - e.printStackTrace(); + Server.logger.write("DICTIONARY: Dictionary file not found!"); } catch (IOException e) { - e.printStackTrace(); + Server.logger.write("DICTIONARY: Can't read dictionary file!"); } } + //Get a random word in wordsList with minimumWordSize < size < maximumWordSize public String getRandomWord(int minimumWordSize, int maximumWordSize){ - randomGenerator = new Random(); + Random randomGenerator = new Random(); int index = randomGenerator.nextInt(wordList.size()); String word = wordList.get(index); if(word.length() >= minimumWordSize && word.length() <= maximumWordSize) { @@ -55,6 +53,7 @@ public class Dictionary { } } + //Check if a word is contained in dictionary public static Boolean isContainedInDictionary(String word){ if(word.equals("")){ return true; @@ -66,5 +65,4 @@ public class Dictionary { } return false; } - } diff --git a/Server/src/com/texttwist/server/models/Match.java b/Server/src/com/texttwist/server/models/Match.java index 0f16cd7..a9f8066 100644 --- a/Server/src/com/texttwist/server/models/Match.java +++ b/Server/src/com/texttwist/server/models/Match.java @@ -1,40 +1,57 @@ package com.texttwist.server.models; -import com.texttwist.server.components.GameServer; +import com.texttwist.server.services.MessageService; import com.texttwist.server.tasks.MatchTimeout; -import constants.Config; import javafx.util.Pair; -import models.Message; import javax.swing.*; -import java.io.IOException; -import java.net.*; import java.nio.channels.SocketChannel; import java.util.*; -import java.util.concurrent.Callable; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; -import java.util.concurrent.Future; -import static com.texttwist.server.components.GameServer.activeMatches; +import static com.texttwist.server.services.MessageService.activeMatches; + /** - * Created by loke on 23/06/2017. + * Author: Lorenzo Iovino on 23/06/2017. + * Description: Match Model. Methods for manage the matches and model of single match. + * Single point of concurrent access. */ public class Match { - public final List> playersStatus = Collections.synchronizedList(new ArrayList<>()); //Usare Liste!!!!!!! - public final List> playersSocket = Collections.synchronizedList(new ArrayList<>()); - private boolean started = false; - public final String matchCreator; - public Integer multicastId; - public Future timeout; - public boolean matchTimeout = true; - public boolean joinTimeout =true; - public DefaultListModel letters; - protected ExecutorService threadPool = Executors.newSingleThreadExecutor(); + /****GLOBAL AREA OF ALL MATCHES****/ + //Players status: A list of pair where elements are . + // status is 0 if user is not currently in a match, and 1 otherwise + public final List> playersStatus = Collections.synchronizedList(new ArrayList<>()); + + //Players socket: A list of pair where elements are . + // socketChannel is the TCP socket associated with client for messages exchange + public final List> playersSocket = Collections.synchronizedList(new ArrayList<>()); + + //Players score: A list of pair where elements are . public final List> playersScore = Collections.synchronizedList(new ArrayList<>()); + + /****SINGLE INSTANCE OF MATCH****/ + //If match is started + private boolean started = false; + + //Name of the creator of the match + public final String matchCreator; + + //MulticastID associated with this match + public Integer multicastId; + + //True if happen timeout, false otherwise + public boolean matchTimeout; + public boolean joinTimeout; + + //Letters of the match + public DefaultListModel letters; + + protected ExecutorService matchTimeoutThread = Executors.newSingleThreadExecutor(); + public Match(String matchCreator, DefaultListModel players){ for (int i =0; i < players.size(); i++){ this.playersStatus.add(new Pair<>(players.get(i), 0)); @@ -47,116 +64,90 @@ public class Match { } - public Void sendScores(){ - while (true) { - System.out.println("SENDING"); - Message msg = new Message("FINALSCORE", "SERVER", "", this.getMatchPlayersScoreAsStringList()); - - MulticastSocket multicastSocket = null; - try { - multicastSocket = new MulticastSocket(this.multicastId); - InetAddress ia = null; - ia = InetAddress.getByName(Config.ScoreMulticastServerURI); - DatagramPacket hi = new DatagramPacket(msg.toString().getBytes(), msg.toString().length(), ia, this.multicastId); - multicastSocket.send(hi); - } catch (IOException e) { - e.printStackTrace(); - } - activeMatches.remove(Match.findMatchIndex(activeMatches, this.matchCreator)); - } - } public static Match findMatch(List matches, String matchName){ - for (int i = 0; i < matches.size(); i++) { - if (matches.get(i).matchCreator.equals(matchName)) { - return matches.get(i); + for (Match match : matches) { + if (match.matchCreator.equals(matchName)) { + return match; } } return null; - } public void printAll(){ - for (int i = 0; i < playersScore.size(); i++) { - System.out.println(playersScore.get(i).getKey() + " : " +playersScore.get(i).getValue()); - + for (Pair aPlayersScore : playersScore) { + System.out.println(aPlayersScore.getKey() + " : " + aPlayersScore.getValue()); } } + public static int findMatchIndex(List matches, String matchName){ - for (int i = 0; i < matches.size(); i++) { - if (matches.get(i).matchCreator.equals(matchName)) { - return i; - } + for (int i = 0; i < matches.size(); i++) { + if (matches.get(i).matchCreator.equals(matchName)) { + return i; } - return -1; + } + return -1; } public boolean isStarted(){ return started; } - public static Match findMatchByPlayer(String player){ - for (int i = 0; i < activeMatches.size(); i++) { - for (int j = 0; j < activeMatches.get(i).playersStatus.size(); j++) { - if (activeMatches.get(i).playersStatus.get(j).getKey().equals(player)) { - return activeMatches.get(i); + public static Match findMatchByPlayerName(String player){ + for (Match activeMatch : activeMatches) { + for (int j = 0; j < activeMatch.playersStatus.size(); j++) { + if (activeMatch.playersStatus.get(j).getKey().equals(player)) { + return activeMatch; } } - /* if (matches.get(i).matchCreator.equals(matchName)) { - return i; - }*/ } return null; } public void startGame(){ - this.started=true; - threadPool.submit(new MatchTimeout(this)); - + this.started = true; + matchTimeoutThread.submit(new MatchTimeout(this)); } public void setScore(String player, Integer score){ - Match m = findMatchByPlayer(player); - m.printAll(); - - for (int i = 0; i < m.playersScore.size(); i++) { - if (m.playersScore.get(i).getKey().equals(player)) { - m.playersScore.set(i, new Pair(player, score)); + Match m = findMatchByPlayerName(player); + if(m!=null) { + for (int i = 0; i < m.playersScore.size(); i++) { + if (m.playersScore.get(i).getKey().equals(player)) { + m.playersScore.set(i, new Pair<>(player, score)); + } } } } public Boolean allPlayersSendedHisScore(){ - System.out.println(matchCreator); - printAll(); - for (int i = 0; i < playersScore.size(); i++) { - if (playersScore.get(i).getValue() == -1) { - return false; - } + for (Pair player : playersScore) { + if (player.getValue() == -1) { + return false; } - return true; + } + return true; } public void setUndefinedScorePlayersToZero(){ - for (int i = 0; i < playersScore.size(); i++) { - if (playersScore.get(i).getValue() == -1) { - playersScore.set(i, new Pair(playersScore.get(i).getKey(), 0)); - } + for (int i = 0; i < playersScore.size(); i++) { + if (playersScore.get(i).getValue() == -1) { + playersScore.set(i, new Pair<>(playersScore.get(i).getKey(), 0)); } + } } public DefaultListModel getMatchPlayersScoreAsStringList(){ - DefaultListModel l = new DefaultListModel<>(); - for (int i = 0; i < playersScore.size(); i++) { - l.addElement(playersScore.get(i).getKey() + ":" + playersScore.get(i).getValue()); - } - return l; - + DefaultListModel l = new DefaultListModel<>(); + for (Pair player : playersScore) { + l.addElement(player.getKey() + ":" + player.getValue()); + } + return l; } - private int generateMulticastId(){ - return GameServer.multicastID++; + return MessageService.multicastID++; } + public void setLetters(DefaultListModel letters){ this.letters = letters; } diff --git a/Server/src/com/texttwist/server/components/ThreadProxy.java b/Server/src/com/texttwist/server/servers/ProxyDispatcher.java similarity index 92% rename from Server/src/com/texttwist/server/components/ThreadProxy.java rename to Server/src/com/texttwist/server/servers/ProxyDispatcher.java index 4d94634..dfe24f8 100644 --- a/Server/src/com/texttwist/server/components/ThreadProxy.java +++ b/Server/src/com/texttwist/server/servers/ProxyDispatcher.java @@ -1,221 +1,214 @@ -package com.texttwist.server.components; - -import com.sun.org.apache.xpath.internal.operations.Bool; -import com.texttwist.client.App; -import com.texttwist.server.models.Match; -import com.texttwist.server.tasks.*; -import constants.Config; -import javafx.util.Pair; -import models.Message; - -import javax.swing.*; -import java.io.IOException; -import java.net.DatagramPacket; -import java.net.DatagramSocket; -import java.net.InetAddress; -import java.net.MulticastSocket; -import java.nio.ByteBuffer; -import java.nio.channels.DatagramChannel; -import java.nio.channels.SocketChannel; -import java.util.concurrent.*; - -import static com.texttwist.server.components.GameServer.activeMatches; - -/** - * Created by loke on 18/06/2017. - */ -public class ThreadProxy implements Callable { - protected final ExecutorService threadPool = Executors.newCachedThreadPool(); - private final Message request; - private final SocketChannel socketChannel; - private ByteBuffer bufferMessage; - boolean matchNotAvailable =false; - - - ThreadProxy(Message request, SocketChannel socketChannel, ByteBuffer bufferMessage) { - this.request = request; - this.socketChannel = socketChannel; - this.bufferMessage = bufferMessage; - - } - - @Override - public Boolean call() { - bufferMessage = ByteBuffer.allocate(1024); - byte[] byteMessage = null; - if(SessionsManager.getInstance().isValidToken(request.token)){ - switch(request.message){ - case "START_GAME": - Future onlineUsers = threadPool.submit(new CheckOnlineUsers(request.data)); - try { - Boolean usersOnline = onlineUsers.get(); - if(usersOnline){ - Future sendInvitations = threadPool.submit(new SendInvitations(request.sender, request.data)); - try { - Boolean invitationSended = sendInvitations.get(); - if (invitationSended) { - - //Crea nuova partita e attendi i giocatori - request.data.addElement(request.sender); - final Match match = new Match(request.sender, request.data); - match.printAll(); - - activeMatches.add(match); - - DefaultListModel matchName = new DefaultListModel<>(); - matchName.addElement(request.sender); - - Future joinMatch = threadPool.submit(new JoinMatch(request.sender, matchName, socketChannel)); - Boolean joinMatchRes = joinMatch.get(); - - if(!joinMatchRes){ - bufferMessage = ByteBuffer.allocate(1024); - - //NON FARE NULLA, ASPETTA GLI ALTRI - Message message = new Message("INVITES_ALL_SENDED", "", "", new DefaultListModel<>()); - byteMessage = message.toString().getBytes(); - bufferMessage = ByteBuffer.wrap(byteMessage); - socketChannel.write(bufferMessage); - } - - Future joinTimeout = threadPool.submit(new JoinTimeout(match)); - joinTimeout.get(); - if(match.joinTimeout){ - Future sendMessageJoinTimeout = threadPool.submit( - new SendMessageToAllPlayers(match, new Message("JOIN_TIMEOUT", "", "", new DefaultListModel<>()), socketChannel)); - Boolean sendMessageJoinTimeoutRes = sendMessageJoinTimeout.get(); - if(!sendMessageJoinTimeoutRes){ - activeMatches.remove(Match.findMatchIndex(activeMatches, match.matchCreator)); - return sendMessageJoinTimeoutRes; - } - } else { - System.out.println("TIMEOUT FINITO SENZA EFFETTI"); - return true; - } - - } else { - return false; - } - } catch (InterruptedException e) { - e.printStackTrace(); - } catch (ExecutionException e) { - e.printStackTrace(); - } - } else { - - Message message = new Message("USER_NOT_ONLINE", "", "", new DefaultListModel<>()); - byteMessage = new String(message.toString()).getBytes(); - bufferMessage.clear(); - bufferMessage = ByteBuffer.wrap(byteMessage); - this.socketChannel.write(bufferMessage); - return false; - } - } catch (InterruptedException e) { - e.printStackTrace(); - } catch (ExecutionException e) { - e.printStackTrace(); - } catch (IOException e) { - e.printStackTrace(); - } - - case "FETCH_HIGHSCORES": - Future> computeHighscores = threadPool.submit(new ComputeHighscores()); - try { - DefaultListModel computeHighscoresRes = computeHighscores.get(); - bufferMessage.clear(); - bufferMessage = ByteBuffer.allocate(1024); - - Message message = new Message("HIGHSCORES", "", "", computeHighscoresRes); - byteMessage = message.toString().getBytes(); - - bufferMessage = ByteBuffer.wrap(byteMessage); - try { - String s = new String(bufferMessage.array(), bufferMessage.position(), bufferMessage.remaining()); - System.out.println("INVIO HIGHSCORES "+ s); - socketChannel.write(bufferMessage); - } catch (IOException e) { - e.printStackTrace(); - } - return false; - } catch (InterruptedException e) { - e.printStackTrace(); - } catch (ExecutionException e) { - e.printStackTrace(); - } - - case "JOIN_GAME": - Future 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){ - - if(!match.joinTimeout) { - Future> 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++) { - SocketChannel socketClient = match.playersSocket.get(i).getValue(); - if (socketClient != null) { - bufferMessage.clear(); - bufferMessage = ByteBuffer.allocate(1024); - - Message message = new Message("GAME_STARTED", "", "", match.letters); - match.startGame(); - - System.out.println("TIMEOUT CANCELLEd"); - byteMessage = message.toString().getBytes(); - - bufferMessage = ByteBuffer.wrap(byteMessage); - try { - String s = new String(bufferMessage.array(), bufferMessage.position(), bufferMessage.remaining()); - System.out.println("INVIO GAME_STARTED "+ s); - socketClient.write(bufferMessage); - } catch (IOException e) { - e.printStackTrace(); - } - } - - } - if (matchNotAvailable) { - return false; - } - } - } else { - if(match == null){ - bufferMessage = ByteBuffer.allocate(1024); - if (socketChannel != null) { - bufferMessage = ByteBuffer.allocate(1024); - - Message msg = new Message("MATCH_NOT_AVAILABLE", "", null, new DefaultListModel<>()); - bufferMessage.clear(); - byteMessage = msg.toString().getBytes(); - bufferMessage = ByteBuffer.wrap(byteMessage); - socketChannel.write(bufferMessage); - matchNotAvailable = true; - } - - } - } - } catch (InterruptedException e) { - e.printStackTrace(); - } catch (ExecutionException e) { - e.printStackTrace(); - } catch (IOException e) { - e.printStackTrace(); - } - - default: - - break; - } - - } else { - threadPool.submit(new TokenInvalid(request.sender, socketChannel, bufferMessage)); - return false; - } - - return false; - } -} +package com.texttwist.server.servers; + +import com.texttwist.server.services.SessionsService; +import com.texttwist.server.models.Match; +import com.texttwist.server.tasks.*; +import models.Message; + +import javax.swing.*; +import java.io.IOException; +import java.nio.ByteBuffer; +import java.nio.channels.SocketChannel; +import java.util.concurrent.*; + +import static com.texttwist.server.services.MessageService.activeMatches; + +/** + * Author: Lorenzo Iovino on 18/06/2017. + * Description: Jedis Service + */ +public class ProxyDispatcher implements Callable { + protected final ExecutorService threadPool = Executors.newCachedThreadPool(); + private final Message request; + private final SocketChannel socketChannel; + private ByteBuffer bufferMessage; + boolean matchNotAvailable =false; + + + public ProxyDispatcher(Message request, SocketChannel socketChannel, ByteBuffer bufferMessage) { + this.request = request; + this.socketChannel = socketChannel; + this.bufferMessage = bufferMessage; + + } + + @Override + public Boolean call() { + bufferMessage = ByteBuffer.allocate(1024); + byte[] byteMessage = null; + if(SessionsService.getInstance().isValidToken(request.token)){ + switch(request.message){ + case "START_GAME": + Future onlineUsers = threadPool.submit(new CheckOnlineUsers(request.data)); + try { + Boolean usersOnline = onlineUsers.get(); + if(usersOnline){ + Future sendInvitations = threadPool.submit(new SendInvitations(request.sender, request.data)); + try { + Boolean invitationSended = sendInvitations.get(); + if (invitationSended) { + + //Crea nuova partita e attendi i giocatori + request.data.addElement(request.sender); + final Match match = new Match(request.sender, request.data); + match.printAll(); + + activeMatches.add(match); + + DefaultListModel matchName = new DefaultListModel<>(); + matchName.addElement(request.sender); + + Future joinMatch = threadPool.submit(new JoinMatch(request.sender, matchName, socketChannel)); + Boolean joinMatchRes = joinMatch.get(); + + if(!joinMatchRes){ + bufferMessage = ByteBuffer.allocate(1024); + + //NON FARE NULLA, ASPETTA GLI ALTRI + Message message = new Message("INVITES_ALL_SENDED", "", "", new DefaultListModel<>()); + byteMessage = message.toString().getBytes(); + bufferMessage = ByteBuffer.wrap(byteMessage); + socketChannel.write(bufferMessage); + } + + Future joinTimeout = threadPool.submit(new JoinTimeout(match)); + joinTimeout.get(); + if(match.joinTimeout){ + Future sendMessageJoinTimeout = threadPool.submit( + new SendMessageToAllPlayers(match, new Message("JOIN_TIMEOUT", "", "", new DefaultListModel<>()), socketChannel)); + Boolean sendMessageJoinTimeoutRes = sendMessageJoinTimeout.get(); + if(!sendMessageJoinTimeoutRes){ + activeMatches.remove(Match.findMatchIndex(activeMatches, match.matchCreator)); + return sendMessageJoinTimeoutRes; + } + } else { + System.out.println("TIMEOUT FINITO SENZA EFFETTI"); + return true; + } + + } else { + return false; + } + } catch (InterruptedException e) { + e.printStackTrace(); + } catch (ExecutionException e) { + e.printStackTrace(); + } + } else { + + Message message = new Message("USER_NOT_ONLINE", "", "", new DefaultListModel<>()); + byteMessage = new String(message.toString()).getBytes(); + bufferMessage.clear(); + bufferMessage = ByteBuffer.wrap(byteMessage); + this.socketChannel.write(bufferMessage); + return false; + } + } catch (InterruptedException e) { + e.printStackTrace(); + } catch (ExecutionException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + + case "FETCH_HIGHSCORES": + Future> computeHighscores = threadPool.submit(new ComputeHighscores()); + try { + DefaultListModel computeHighscoresRes = computeHighscores.get(); + bufferMessage.clear(); + bufferMessage = ByteBuffer.allocate(1024); + + Message message = new Message("HIGHSCORES", "", "", computeHighscoresRes); + byteMessage = message.toString().getBytes(); + + bufferMessage = ByteBuffer.wrap(byteMessage); + try { + String s = new String(bufferMessage.array(), bufferMessage.position(), bufferMessage.remaining()); + System.out.println("INVIO HIGHSCORES "+ s); + socketChannel.write(bufferMessage); + } catch (IOException e) { + e.printStackTrace(); + } + return false; + } catch (InterruptedException e) { + e.printStackTrace(); + } catch (ExecutionException e) { + e.printStackTrace(); + } + + case "JOIN_GAME": + Future 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){ + + if(!match.joinTimeout) { + Future> 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++) { + SocketChannel socketClient = match.playersSocket.get(i).getValue(); + if (socketClient != null) { + bufferMessage.clear(); + bufferMessage = ByteBuffer.allocate(1024); + + Message message = new Message("GAME_STARTED", "", "", match.letters); + match.startGame(); + + System.out.println("TIMEOUT CANCELLEd"); + byteMessage = message.toString().getBytes(); + + bufferMessage = ByteBuffer.wrap(byteMessage); + try { + String s = new String(bufferMessage.array(), bufferMessage.position(), bufferMessage.remaining()); + System.out.println("INVIO GAME_STARTED "+ s); + socketClient.write(bufferMessage); + } catch (IOException e) { + e.printStackTrace(); + } + } + + } + if (matchNotAvailable) { + return false; + } + } + } else { + if(match == null){ + bufferMessage = ByteBuffer.allocate(1024); + if (socketChannel != null) { + bufferMessage = ByteBuffer.allocate(1024); + + Message msg = new Message("MATCH_NOT_AVAILABLE", "", null, new DefaultListModel<>()); + bufferMessage.clear(); + byteMessage = msg.toString().getBytes(); + bufferMessage = ByteBuffer.wrap(byteMessage); + socketChannel.write(bufferMessage); + matchNotAvailable = true; + } + + } + } + } catch (InterruptedException e) { + e.printStackTrace(); + } catch (ExecutionException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + + default: + + break; + } + + } else { + threadPool.submit(new TokenInvalid(request.sender, socketChannel, bufferMessage)); + return false; + } + + return false; + } +} diff --git a/Server/src/com/texttwist/server/components/AccountsManager.java b/Server/src/com/texttwist/server/services/AccountsService.java similarity index 80% rename from Server/src/com/texttwist/server/components/AccountsManager.java rename to Server/src/com/texttwist/server/services/AccountsService.java index 5fd93fa..5d5515d 100644 --- a/Server/src/com/texttwist/server/components/AccountsManager.java +++ b/Server/src/com/texttwist/server/services/AccountsService.java @@ -1,79 +1,80 @@ -package com.texttwist.server.components; - -import models.User; -import java.io.Serializable; -import java.util.*; - - -/** - * Created by loke on 18/06/2017. - */ -public class AccountsManager { - - public List users = Collections.synchronizedList(new ArrayList()); - - private static class Holder { - static final AccountsManager INSTANCE = new AccountsManager(); - } - - public static AccountsManager getInstance() { - return AccountsManager.Holder.INSTANCE; - } - - private AccountsManager(){ - List l = JedisService.get("users"); - for(int i=0; i i = users.iterator(); - while (i.hasNext()) { - if (i.next().userName.equals(userName)) { - return true; - } - } - return false; - - } - - public boolean checkPassword(String userName, String password) { - Iterator i = users.iterator(); - while (i.hasNext()) { - User account = i.next(); - if (account.userName.equals(userName) && account.password.equals(password)) { - return true; - } - } - return false; - - } - - public User findUser(String userName){ - Iterator i = users.iterator(); - while (i.hasNext()) { - User u = i.next(); - if (u.userName.equals(userName)) { - return u; - } - } - return null; - } - - public int size(){ - return users.size(); - } - -} +package com.texttwist.server.services; + +import models.User; +import java.io.Serializable; +import java.util.*; + + +/** + * Author: Lorenzo Iovino on 18/06/2017. + * Description: AccountsService + */ +public class AccountsService { + + public List users = Collections.synchronizedList(new ArrayList()); + + private static class Holder { + static final AccountsService INSTANCE = new AccountsService(); + } + + public static AccountsService getInstance() { + return AccountsService.Holder.INSTANCE; + } + + private AccountsService(){ + List l = JedisService.get("users"); + for(int i=0; i i = users.iterator(); + while (i.hasNext()) { + if (i.next().userName.equals(userName)) { + return true; + } + } + return false; + + } + + public boolean checkPassword(String userName, String password) { + Iterator i = users.iterator(); + while (i.hasNext()) { + User account = i.next(); + if (account.userName.equals(userName) && account.password.equals(password)) { + return true; + } + } + return false; + + } + + public User findUser(String userName){ + Iterator i = users.iterator(); + while (i.hasNext()) { + User u = i.next(); + if (u.userName.equals(userName)) { + return u; + } + } + return null; + } + + public int size(){ + return users.size(); + } + +} diff --git a/Server/src/com/texttwist/server/components/Auth.java b/Server/src/com/texttwist/server/services/AuthService.java similarity index 79% rename from Server/src/com/texttwist/server/components/Auth.java rename to Server/src/com/texttwist/server/services/AuthService.java index 08dc66b..675b10c 100644 --- a/Server/src/com/texttwist/server/components/Auth.java +++ b/Server/src/com/texttwist/server/services/AuthService.java @@ -1,83 +1,84 @@ -package com.texttwist.server.components; - -import com.texttwist.server.Server; -import interfaces.IAuth; -import interfaces.INotificationClient; -import models.Response; -import org.json.simple.JsonObject; -import java.math.BigInteger; -import java.rmi.RemoteException; -import java.rmi.server.UnicastRemoteObject; -import java.security.SecureRandom; - -import static com.texttwist.server.Server.notificationServer; - -/** - * Created by loke on 15/06/2017. - */ -public class Auth extends UnicastRemoteObject implements IAuth { - - private SecureRandom random = new SecureRandom(); - public int serverPort = 9999; - - - public Auth(int serverPort) throws RemoteException{ - this.serverPort=serverPort; - Server.logger.write("Auth Service running at "+serverPort+" port..."); - } - - @Override - public Response register(String userName, String password) throws RemoteException { - Server.logger.write("Invoked register with username=" + userName + " AND " + " password=" + password); - if ((userName != null && !userName.isEmpty()) && (password != null && !password.equals(""))) { - if(AccountsManager.getInstance().register(userName, password)){ - Server.logger.write("Registration successfull"); - return new Response("Registration successfull", 200, null); - } else { - Server.logger.write("Registration unsuccessfull"); - return new Response("
Registration unsuccessfull:
Username exist!
", 400, null); - } - } - return new Response("
Registration unsuccessfull!
All fields are mandatories
", 400, null); - } - - @Override - public Response login(String userName, String password) throws RemoteException { - Server.logger.write("Invoked login with username=" + userName + " AND " + " password=" + password); - if ((userName != null && !userName.isEmpty()) && (password != null && !password.equals(""))) { - if(AccountsManager.getInstance().exists(userName) && AccountsManager.getInstance().checkPassword(userName, password)) { - JsonObject data = new JsonObject(); - String token = nextSessionId(); - data.put("token", token); - SessionsManager.getInstance().add(userName,token); - Server.logger.write("Login successfull"); - return new Response("Login successfull", 200, data); - } - } - Server.logger.write("Login unsuccessfull"); - return new Response("Login unsuccessfull", 400, null); - } - - @Override - public Response logout(String userName, String token, INotificationClient stub) throws RemoteException { - Server.logger.write("Invoked logout with username=" + userName + " AND " + " token=" + token); - notificationServer.unregisterForCallback(stub); - - if ((userName != null && !userName.isEmpty()) && (token != null && !token.isEmpty())) { - boolean res = SessionsManager.getInstance().remove(userName); - if(res) { - Server.logger.write("Logout successfull"); - - } - } - - SessionsManager.getInstance().remove(userName); - Server.logger.write("Logout successfull (but something gone wrong)"); - return new Response("Logout successfull (but something gone wrong)", 200, null); - } - - public String nextSessionId() { - return new BigInteger(130, random).toString(32); - } - -} +package com.texttwist.server.services; + +import com.texttwist.server.Server; +import interfaces.IAuth; +import interfaces.INotificationClient; +import models.Response; +import org.json.simple.JsonObject; +import java.math.BigInteger; +import java.rmi.RemoteException; +import java.rmi.server.UnicastRemoteObject; +import java.security.SecureRandom; + +import static com.texttwist.server.Server.notificationServer; + +/** + * Author: Lorenzo Iovino on 15/06/2017. + * Description: AuthService + */ +public class AuthService extends UnicastRemoteObject implements IAuth { + + private SecureRandom random = new SecureRandom(); + public int serverPort = 9999; + + + public AuthService(int serverPort) throws RemoteException{ + this.serverPort=serverPort; + Server.logger.write("AuthService Service running at "+serverPort+" port..."); + } + + @Override + public Response register(String userName, String password) throws RemoteException { + Server.logger.write("Invoked register with username=" + userName + " AND " + " password=" + password); + if ((userName != null && !userName.isEmpty()) && (password != null && !password.equals(""))) { + if(AccountsService.getInstance().register(userName, password)){ + Server.logger.write("Registration successfull"); + return new Response("Registration successfull", 200, null); + } else { + Server.logger.write("Registration unsuccessfull"); + return new Response("
Registration unsuccessfull:
Username exist!
", 400, null); + } + } + return new Response("
Registration unsuccessfull!
All fields are mandatories
", 400, null); + } + + @Override + public Response login(String userName, String password) throws RemoteException { + Server.logger.write("Invoked login with username=" + userName + " AND " + " password=" + password); + if ((userName != null && !userName.isEmpty()) && (password != null && !password.equals(""))) { + if(AccountsService.getInstance().exists(userName) && AccountsService.getInstance().checkPassword(userName, password)) { + JsonObject data = new JsonObject(); + String token = nextSessionId(); + data.put("token", token); + SessionsService.getInstance().add(userName,token); + Server.logger.write("Login successfull"); + return new Response("Login successfull", 200, data); + } + } + Server.logger.write("Login unsuccessfull"); + return new Response("Login unsuccessfull", 400, null); + } + + @Override + public Response logout(String userName, String token, INotificationClient stub) throws RemoteException { + Server.logger.write("Invoked logout with username=" + userName + " AND " + " token=" + token); + notificationServer.unregisterForCallback(stub); + + if ((userName != null && !userName.isEmpty()) && (token != null && !token.isEmpty())) { + boolean res = SessionsService.getInstance().remove(userName); + if(res) { + Server.logger.write("Logout successfull"); + + } + } + + SessionsService.getInstance().remove(userName); + Server.logger.write("Logout successfull (but something gone wrong)"); + return new Response("Logout successfull (but something gone wrong)", 200, null); + } + + public String nextSessionId() { + return new BigInteger(130, random).toString(32); + } + +} diff --git a/Server/src/com/texttwist/server/components/JedisService.java b/Server/src/com/texttwist/server/services/JedisService.java similarity index 95% rename from Server/src/com/texttwist/server/components/JedisService.java rename to Server/src/com/texttwist/server/services/JedisService.java index 9b1b627..6d796cd 100644 --- a/Server/src/com/texttwist/server/components/JedisService.java +++ b/Server/src/com/texttwist/server/services/JedisService.java @@ -1,10 +1,9 @@ -package com.texttwist.server.components; +package com.texttwist.server.services; import models.User; import redis.clients.jedis.Jedis; import java.io.*; -import java.lang.reflect.Type; import java.util.ArrayList; import java.util.Base64; import java.util.List; @@ -12,7 +11,8 @@ import java.util.List; import static com.texttwist.server.Server.jedisPool; /** - * Created by loke on 11/07/2017. + * Author: Lorenzo Iovino on 11/07/2017. + * Description: Jedis Service */ public class JedisService { diff --git a/Server/src/com/texttwist/server/components/GameServer.java b/Server/src/com/texttwist/server/services/MessageService.java similarity index 90% rename from Server/src/com/texttwist/server/components/GameServer.java rename to Server/src/com/texttwist/server/services/MessageService.java index 2680cea..2c42788 100644 --- a/Server/src/com/texttwist/server/components/GameServer.java +++ b/Server/src/com/texttwist/server/services/MessageService.java @@ -1,138 +1,141 @@ - -package com.texttwist.server.components; -import com.texttwist.client.App; -import com.texttwist.server.Server; -import com.texttwist.server.models.Dictionary; -import com.texttwist.server.models.Match; -import com.texttwist.server.tasks.ReceiveWords; -import constants.Config; -import models.Message; -import utilities.Logger; - -import java.net.*; -import java.io.IOException; -import java.nio.ByteBuffer; -import java.nio.channels.*; -import java.util.ArrayList; -import java.util.Collections; -import java.util.Iterator; -import java.util.List; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; - -import static java.nio.channels.SelectionKey.OP_ACCEPT; -import static java.nio.channels.SelectionKey.OP_READ; - -public class GameServer implements Runnable{ - - private int serverPort; - private ThreadProxy proxy; - private ReceiveWords wordsReceiver; - - private DatagramChannel datagramChannel; - private Selector selector = null; - private ExecutorService threadPool = Executors.newCachedThreadPool(); - private String dictionaryPath = "./Server/resources/dictionary"; - public static Dictionary dict; - SocketChannel client; - ByteBuffer bufferWords = ByteBuffer.allocate(1024); - ByteBuffer bufferMessages = ByteBuffer.allocate(1024); - - - - public static List activeMatches = Collections.synchronizedList(new ArrayList<>()); - public static Integer multicastID = 4000; - - public GameServer(int port){ - this.serverPort = port; - } - - public void run(){ - - dict = new Dictionary(dictionaryPath); - try { - selector = Selector.open(); - - ServerSocketChannel serverSocketChannel = ServerSocketChannel.open(); - serverSocketChannel.configureBlocking(false); - serverSocketChannel.socket().bind(new InetSocketAddress(serverPort)); - serverSocketChannel.register(selector, OP_ACCEPT); - InetSocketAddress address = new InetSocketAddress(Config.WordsReceiverServerURI,Config.WordsReceiverServerPort); - datagramChannel = DatagramChannel.open(); - datagramChannel.configureBlocking(true); - datagramChannel.connect(address); - Server.logger.write("GameService Service is running at "+this.serverPort+" port..."); - - wordsReceiver = new ReceiveWords(datagramChannel, bufferWords, bufferMessages, client); - threadPool.submit(wordsReceiver); - - } catch (IOException e) { - e.printStackTrace(); - } - - while (true) { - System.out.println("WAITING FOR MSG"); - try { - selector.select(); - } catch (IOException e) { - e.printStackTrace(); - } - - Iterator iter = selector.selectedKeys().iterator(); - while (iter.hasNext()) { - bufferMessages = ByteBuffer.allocate(1024); - bufferMessages.clear(); - client = null; - SelectionKey key = iter.next(); - iter.remove(); - - try { - switch (key.readyOps()) { - case OP_ACCEPT: - client = ((ServerSocketChannel) key.channel()).accept(); - client.configureBlocking(false); - client.register(selector, OP_READ); - break; - - case OP_READ: - client = (SocketChannel) key.channel(); - if (client.read(bufferMessages) != -1) { - bufferMessages.flip(); - String line = new String(bufferMessages.array(), bufferMessages.position(), bufferMessages.remaining()); - System.out.println(line); - if (line.startsWith("MESSAGE")) { - SessionsManager.getInstance().printAll(); - Message msg = Message.toMessage(line); - proxy = new ThreadProxy(msg, client, bufferMessages); - threadPool.submit(proxy); - } - - if (line.startsWith("CLOSE")) { - client.close(); - } else if (line.startsWith("QUIT")) { - for (SelectionKey k : selector.keys()) { - k.cancel(); - k.channel().close(); - } - selector.close(); - return; - } - } else { - key.cancel(); - } - break; - - default: - break; - } - } catch (IOException e) { - try { - client.close(); - } catch (IOException e1) { - e1.printStackTrace(); - } - } - } - } - } + +package com.texttwist.server.services; +import com.texttwist.server.Server; +import com.texttwist.server.servers.ProxyDispatcher; +import com.texttwist.server.models.Dictionary; +import com.texttwist.server.models.Match; +import com.texttwist.server.tasks.ReceiveWords; +import constants.Config; +import models.Message; +import java.net.*; +import java.io.IOException; +import java.nio.ByteBuffer; +import java.nio.channels.*; +import java.util.ArrayList; +import java.util.Collections; +import java.util.Iterator; +import java.util.List; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; + +import static java.nio.channels.SelectionKey.OP_ACCEPT; +import static java.nio.channels.SelectionKey.OP_READ; + + +/** + * Author: Lorenzo Iovino on 17/06/2017. + * Description: Game Server + */ +public class MessageService implements Runnable{ + + private int serverPort; + private ProxyDispatcher proxy; + private ReceiveWords wordsReceiver; + + private DatagramChannel datagramChannel; + private Selector selector = null; + private ExecutorService threadPool = Executors.newCachedThreadPool(); + private String dictionaryPath = "./Server/resources/dictionary"; + public static Dictionary dict; + SocketChannel client; + ByteBuffer bufferWords = ByteBuffer.allocate(1024); + ByteBuffer bufferMessages = ByteBuffer.allocate(1024); + + + + public static List activeMatches = Collections.synchronizedList(new ArrayList<>()); + public static Integer multicastID = 4000; + + public MessageService(int port){ + this.serverPort = port; + } + + public void run(){ + + dict = new Dictionary(dictionaryPath); + try { + selector = Selector.open(); + + ServerSocketChannel serverSocketChannel = ServerSocketChannel.open(); + serverSocketChannel.configureBlocking(false); + serverSocketChannel.socket().bind(new InetSocketAddress(serverPort)); + serverSocketChannel.register(selector, OP_ACCEPT); + InetSocketAddress address = new InetSocketAddress(Config.WordsReceiverServerURI,Config.WordsReceiverServerPort); + datagramChannel = DatagramChannel.open(); + datagramChannel.configureBlocking(true); + datagramChannel.connect(address); + Server.logger.write("GameService Service is running at "+this.serverPort+" port..."); + + wordsReceiver = new ReceiveWords(datagramChannel, bufferWords, bufferMessages, client); + threadPool.submit(wordsReceiver); + + } catch (IOException e) { + e.printStackTrace(); + } + + while (true) { + System.out.println("WAITING FOR MSG"); + try { + selector.select(); + } catch (IOException e) { + e.printStackTrace(); + } + + Iterator iter = selector.selectedKeys().iterator(); + while (iter.hasNext()) { + bufferMessages = ByteBuffer.allocate(1024); + bufferMessages.clear(); + client = null; + SelectionKey key = iter.next(); + iter.remove(); + + try { + switch (key.readyOps()) { + case OP_ACCEPT: + client = ((ServerSocketChannel) key.channel()).accept(); + client.configureBlocking(false); + client.register(selector, OP_READ); + break; + + case OP_READ: + client = (SocketChannel) key.channel(); + if (client.read(bufferMessages) != -1) { + bufferMessages.flip(); + String line = new String(bufferMessages.array(), bufferMessages.position(), bufferMessages.remaining()); + System.out.println(line); + if (line.startsWith("MESSAGE")) { + SessionsService.getInstance().printAll(); + Message msg = Message.toMessage(line); + proxy = new ProxyDispatcher(msg, client, bufferMessages); + threadPool.submit(proxy); + } + + if (line.startsWith("CLOSE")) { + client.close(); + } else if (line.startsWith("QUIT")) { + for (SelectionKey k : selector.keys()) { + k.cancel(); + k.channel().close(); + } + selector.close(); + return; + } + } else { + key.cancel(); + } + break; + + default: + break; + } + } catch (IOException e) { + try { + client.close(); + } catch (IOException e1) { + e1.printStackTrace(); + } + } + } + } + } } \ No newline at end of file diff --git a/Server/src/com/texttwist/server/components/NotificationServer.java b/Server/src/com/texttwist/server/services/NotificationService.java similarity index 86% rename from Server/src/com/texttwist/server/components/NotificationServer.java rename to Server/src/com/texttwist/server/services/NotificationService.java index 7ac298b..f630028 100644 --- a/Server/src/com/texttwist/server/components/NotificationServer.java +++ b/Server/src/com/texttwist/server/services/NotificationService.java @@ -1,62 +1,63 @@ -package com.texttwist.server.components; - -import interfaces.INotificationClient; -import interfaces.INotificationServer; - -import javax.swing.*; -import java.rmi.RemoteException; -import java.util.ArrayList; -import java.util.Iterator; -import java.util.List; - - -/** - * Created by loke on 19/06/2017. - */ -public class NotificationServer implements INotificationServer { - - private List clients; - public NotificationServer() throws RemoteException { - super(); - clients = new ArrayList<>(); - } - - public synchronized void registerForCallback(INotificationClient clientInterface) throws RemoteException { - if(!clients.contains(clientInterface)){ - clients.add(clientInterface); - System.out.println(clientInterface); - System.out.println("New client registered"); - } - } - - public synchronized void unregisterForCallback(INotificationClient client) throws RemoteException { - if (clients.remove(client)) { - System.out.println("Client unregistered"); - } else { - System.out.println("Unable to unregister client"); - } - - } - - public synchronized void sendInvitations(String username, DefaultListModel users){ - Iterator i = clients.iterator(); - INotificationClient client = null; - System.out.println("Starting callbacks"); - while (i.hasNext()) { - client = (INotificationClient) i.next(); - try { - - System.out.println("SENDING INVITE TO "+users); - client.sendInvite(username, users); - } catch (RemoteException e) { - System.out.println("Sembra down"); - try { - unregisterForCallback(client); - } catch (RemoteException e1) { - e1.printStackTrace(); - } - } - } - - } -} +package com.texttwist.server.services; + +import interfaces.INotificationClient; +import interfaces.INotificationServer; + +import javax.swing.*; +import java.rmi.RemoteException; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + + +/** + * Author: Lorenzo Iovino on 19/06/2017. + * Description: Jedis Service + */ +public class NotificationService implements INotificationServer { + + private List clients; + public NotificationService() throws RemoteException { + super(); + clients = new ArrayList<>(); + } + + public synchronized void registerForCallback(INotificationClient clientInterface) throws RemoteException { + if(!clients.contains(clientInterface)){ + clients.add(clientInterface); + System.out.println(clientInterface); + System.out.println("New client registered"); + } + } + + public synchronized void unregisterForCallback(INotificationClient client) throws RemoteException { + if (clients.remove(client)) { + System.out.println("Client unregistered"); + } else { + System.out.println("Unable to unregister client"); + } + + } + + public synchronized void sendInvitations(String username, DefaultListModel users){ + Iterator i = clients.iterator(); + INotificationClient client = null; + System.out.println("Starting callbacks"); + while (i.hasNext()) { + client = (INotificationClient) i.next(); + try { + + System.out.println("SENDING INVITE TO "+users); + client.sendInvite(username, users); + } catch (RemoteException e) { + System.out.println("Sembra down"); + try { + unregisterForCallback(client); + } catch (RemoteException e1) { + e1.printStackTrace(); + } + } + } + + } +} diff --git a/Server/src/com/texttwist/server/services/SessionsService.java b/Server/src/com/texttwist/server/services/SessionsService.java new file mode 100644 index 0000000..9489a99 --- /dev/null +++ b/Server/src/com/texttwist/server/services/SessionsService.java @@ -0,0 +1,77 @@ +package com.texttwist.server.services; +import models.Session; +import models.User; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.Iterator; +import java.util.List; + + +/** + * Author: Lorenzo Iovino on 17/06/2017. + * Description: SessionsService. It is a singleton that provides the model and methods for manage sessions + */ +public class SessionsService { + + private List sessions = Collections.synchronizedList(new ArrayList()); + + private static class Holder { + static final SessionsService INSTANCE = new SessionsService(); + } + + public static SessionsService getInstance() { + return Holder.INSTANCE; + } + + private Session getSession(String userName) { + for (Session elem : sessions) { + if (elem.account.userName.equals(userName)) { + return elem; + } + } + return null; + } + + public SessionsService(){} + + public boolean add(String userName, String token) { + remove(userName); + return sessions.add(new Session(new User(userName,"",0), token)); + } + + public void printAll(){ + for (Session elem : sessions) { + System.out.println(elem.account.userName + " | " + elem.token); + } + } + + public boolean remove(String userName){ + if(exists(userName)) { + Session s = getSession(userName); + if(s != null) { + sessions.remove(s); + return true; + } + } + return false; + } + + public boolean exists(String userName) { + for (Session elem : sessions) { + if (elem.account.userName.equals(userName)) { + return true; + } + } + return false; + } + + public boolean isValidToken(String token) { + for (Session session : sessions) { + if (session.token.equals(token)) { + return true; + } + } + return false; + } +} diff --git a/Server/src/com/texttwist/server/tasks/CheckOnlineUsers.java b/Server/src/com/texttwist/server/tasks/CheckOnlineUsers.java index 4379a1e..46b66ba 100644 --- a/Server/src/com/texttwist/server/tasks/CheckOnlineUsers.java +++ b/Server/src/com/texttwist/server/tasks/CheckOnlineUsers.java @@ -1,12 +1,13 @@ package com.texttwist.server.tasks; -import com.texttwist.server.components.SessionsManager; +import com.texttwist.server.services.SessionsService; import javax.swing.*; import java.util.concurrent.Callable; /** - * Created by loke on 19/06/2017. + * Author: Lorenzo Iovino on 19/06/2017. + * Description: Jedis Service */ public class CheckOnlineUsers implements Callable { private final DefaultListModel users; @@ -18,7 +19,7 @@ public class CheckOnlineUsers implements Callable { @Override public Boolean call() throws Exception { for(int i = 0; i < users.size(); i++){ - if(!(SessionsManager.getInstance().exists(users.get(i)))){ + if(!(SessionsService.getInstance().exists(users.get(i)))){ return false; } } diff --git a/Server/src/com/texttwist/server/tasks/ComputeHighscores.java b/Server/src/com/texttwist/server/tasks/ComputeHighscores.java index 4f52a8c..4c143e2 100644 --- a/Server/src/com/texttwist/server/tasks/ComputeHighscores.java +++ b/Server/src/com/texttwist/server/tasks/ComputeHighscores.java @@ -1,17 +1,16 @@ package com.texttwist.server.tasks; -import com.texttwist.server.components.AccountsManager; -import com.texttwist.server.components.JedisService; +import com.texttwist.server.services.AccountsService; +import com.texttwist.server.services.JedisService; import models.User; import javax.swing.*; -import java.util.ArrayList; import java.util.Comparator; -import java.util.List; import java.util.concurrent.Callable; /** - * Created by loke on 28/06/2017. + * Author: Lorenzo Iovino on 28/06/2017. + * Description: Jedis Service */ public class ComputeHighscores implements Callable> { @@ -21,16 +20,16 @@ public class ComputeHighscores implements Callable> { public DefaultListModel call() throws Exception { DefaultListModel l = new DefaultListModel<>(); - AccountsManager.getInstance().users.sort(new Comparator() { + AccountsService.getInstance().users.sort(new Comparator() { @Override public int compare(User o1, User o2) { return o2.score.compareTo(o1.score); } }); JedisService.removeAll("users"); - for(int i =0; i< AccountsManager.getInstance().users.size(); i++){ - l.addElement(AccountsManager.getInstance().users.get(i).userName+":"+AccountsManager.getInstance().users.get(i).score); - JedisService.add("users",AccountsManager.getInstance().users.get(i)); + for(int i = 0; i< AccountsService.getInstance().users.size(); i++){ + l.addElement(AccountsService.getInstance().users.get(i).userName+":"+ AccountsService.getInstance().users.get(i).score); + JedisService.add("users", AccountsService.getInstance().users.get(i)); } return l; } diff --git a/Server/src/com/texttwist/server/tasks/ComputeScore.java b/Server/src/com/texttwist/server/tasks/ComputeScore.java index 72a0762..6ea1ca0 100644 --- a/Server/src/com/texttwist/server/tasks/ComputeScore.java +++ b/Server/src/com/texttwist/server/tasks/ComputeScore.java @@ -1,26 +1,15 @@ package com.texttwist.server.tasks; -import com.texttwist.client.App; -import com.texttwist.server.components.AccountsManager; -import com.texttwist.server.components.JedisService; +import com.texttwist.server.services.AccountsService; import com.texttwist.server.models.Dictionary; import com.texttwist.server.models.Match; -import constants.Config; -import models.Message; import models.User; -import redis.clients.jedis.Jedis; import javax.swing.*; -import java.io.Serializable; -import java.net.DatagramPacket; -import java.net.InetAddress; -import java.net.MulticastSocket; -import java.util.List; import java.util.concurrent.Callable; -import static com.texttwist.server.components.GameServer.activeMatches; - /** - * Created by loke on 27/06/2017. + * Author: Lorenzo Iovino on 27/06/2017. + * Description: Jedis Service */ public class ComputeScore implements Callable { @@ -52,7 +41,7 @@ public class ComputeScore implements Callable { match.setScore(sender, score); System.out.println(score); - User u = AccountsManager.getInstance().findUser(sender); + User u = AccountsService.getInstance().findUser(sender); u.addScore(score); if(match.allPlayersSendedHisScore()) { @@ -61,7 +50,7 @@ public class ComputeScore implements Callable { System.out.println("MATCH TIMEOUT CANCELLATO"); match.setUndefinedScorePlayersToZero(); - match.sendScores(); + new SendScores(match).call(); } return score; diff --git a/Server/src/com/texttwist/server/tasks/GenerateLetters.java b/Server/src/com/texttwist/server/tasks/GenerateLetters.java index a15d09e..a5a5394 100644 --- a/Server/src/com/texttwist/server/tasks/GenerateLetters.java +++ b/Server/src/com/texttwist/server/tasks/GenerateLetters.java @@ -1,16 +1,13 @@ package com.texttwist.server.tasks; -import com.texttwist.server.components.GameServer; +import com.texttwist.server.services.MessageService; import javax.swing.*; -import java.nio.ByteBuffer; -import java.nio.channels.FileChannel; -import java.nio.charset.Charset; -import java.nio.file.*; import java.util.concurrent.Callable; /** - * Created by loke on 25/06/2017. + * Author: Lorenzo Iovino on 25/06/2017. + * Description: Jedis Service */ public class GenerateLetters implements Callable> { @@ -23,7 +20,7 @@ public class GenerateLetters implements Callable> { public DefaultListModel call() throws Exception { DefaultListModel l = new DefaultListModel(); - String word = GameServer.dict.getRandomWord(6, 7); + String word = MessageService.dict.getRandomWord(6, 7); for (int i = 0;i < word.length(); i++){ l.addElement(String.valueOf(word.charAt(i))); } diff --git a/Server/src/com/texttwist/server/tasks/JoinMatch.java b/Server/src/com/texttwist/server/tasks/JoinMatch.java index e931c13..84cf13c 100644 --- a/Server/src/com/texttwist/server/tasks/JoinMatch.java +++ b/Server/src/com/texttwist/server/tasks/JoinMatch.java @@ -5,10 +5,11 @@ import javafx.util.Pair; import javax.swing.*; import java.nio.channels.SocketChannel; import java.util.concurrent.Callable; -import static com.texttwist.server.components.GameServer.activeMatches; +import static com.texttwist.server.services.MessageService.activeMatches; /** - * Created by loke on 23/06/2017. + * Author: Lorenzo Iovino on 23/06/2017. + * Description: Jedis Service */ public class JoinMatch implements Callable { public final String matchName; diff --git a/Server/src/com/texttwist/server/tasks/JoinTimeout.java b/Server/src/com/texttwist/server/tasks/JoinTimeout.java index e188b29..cf0a0f9 100644 --- a/Server/src/com/texttwist/server/tasks/JoinTimeout.java +++ b/Server/src/com/texttwist/server/tasks/JoinTimeout.java @@ -17,13 +17,14 @@ public class JoinTimeout implements Callable { @Override public Boolean call() throws Exception { try { - Thread.currentThread().sleep(7*60*1000); + Thread.currentThread().sleep(1*60*1000); System.out.println("TIMEOUTTTT"); if(match.joinTimeout) { return false; } else { + match.joinTimeout=true; return true; } } catch (InterruptedException e) { diff --git a/Server/src/com/texttwist/server/tasks/MatchTimeout.java b/Server/src/com/texttwist/server/tasks/MatchTimeout.java index f878e63..9e45b84 100644 --- a/Server/src/com/texttwist/server/tasks/MatchTimeout.java +++ b/Server/src/com/texttwist/server/tasks/MatchTimeout.java @@ -1,19 +1,13 @@ package com.texttwist.server.tasks; -import com.sun.org.apache.xpath.internal.operations.Bool; import com.texttwist.server.models.Match; import constants.Config; -import models.Message; -import java.net.DatagramPacket; -import java.net.InetAddress; -import java.net.MulticastSocket; import java.util.concurrent.Callable; -import static com.texttwist.server.components.GameServer.activeMatches; - /** - * Created by loke on 27/06/2017. + * Author: Lorenzo Iovino on 27/06/2017. + * Description: Jedis Service */ public class MatchTimeout implements Callable { @@ -26,12 +20,12 @@ public class MatchTimeout implements Callable { @Override public Boolean call() throws Exception { try { - Thread.currentThread().sleep(5*60*1000); //TODO 5*60*1000 + Thread.currentThread().sleep(Config.sendWordsTimeout); match.setUndefinedScorePlayersToZero(); if(match.matchTimeout) { System.out.println("SEND BROADCAST BECAUSE TIMEOUT"); - match.sendScores(); + new SendScores(match).call(); return true; } return false; diff --git a/Server/src/com/texttwist/server/tasks/ReceiveWords.java b/Server/src/com/texttwist/server/tasks/ReceiveWords.java index cb5bbc1..c2786c8 100644 --- a/Server/src/com/texttwist/server/tasks/ReceiveWords.java +++ b/Server/src/com/texttwist/server/tasks/ReceiveWords.java @@ -1,6 +1,6 @@ package com.texttwist.server.tasks; -import com.texttwist.server.components.SessionsManager; +import com.texttwist.server.services.SessionsService; import com.texttwist.server.models.Match; import constants.Config; import models.Message; @@ -14,7 +14,8 @@ import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; /** - * Created by loke on 27/06/2017. + * Author: Lorenzo Iovino on 27/06/2017. + * Description: Jedis Service */ public class ReceiveWords implements Callable{ @@ -53,9 +54,9 @@ public class ReceiveWords implements Callable{ System.out.println(rcv); if (rcv.startsWith("MESSAGE")) { msg = Message.toMessage(rcv); - if(SessionsManager.getInstance().isValidToken(msg.token)) { + if(SessionsService.getInstance().isValidToken(msg.token)) { System.out.println(msg.sender); - Match match = Match.findMatchByPlayer(msg.sender); + Match match = Match.findMatchByPlayerName(msg.sender); threadPool.submit(new ComputeScore(msg.sender, msg.data, match)); } else { threadPool.submit(new TokenInvalid(msg.sender, socketChannel, bufferMessages)); diff --git a/Server/src/com/texttwist/server/tasks/SendInvitations.java b/Server/src/com/texttwist/server/tasks/SendInvitations.java index db1693c..7b05d97 100644 --- a/Server/src/com/texttwist/server/tasks/SendInvitations.java +++ b/Server/src/com/texttwist/server/tasks/SendInvitations.java @@ -1,19 +1,13 @@ package com.texttwist.server.tasks; import com.texttwist.server.Server; -import com.texttwist.server.components.NotificationServer; -import com.texttwist.server.components.SessionsManager; -import constants.Config; -import interfaces.INotificationServer; import javax.swing.*; -import java.rmi.registry.LocateRegistry; -import java.rmi.registry.Registry; -import java.rmi.server.UnicastRemoteObject; import java.util.concurrent.Callable; /** - * Created by loke on 19/06/2017. + * Author: Lorenzo Iovino on 19/06/2017. + * Description: Jedis Service */ public class SendInvitations implements Callable { private DefaultListModel users; diff --git a/Server/src/com/texttwist/server/tasks/SendMessageToAllPlayers.java b/Server/src/com/texttwist/server/tasks/SendMessageToAllPlayers.java index 23c8775..96012cc 100644 --- a/Server/src/com/texttwist/server/tasks/SendMessageToAllPlayers.java +++ b/Server/src/com/texttwist/server/tasks/SendMessageToAllPlayers.java @@ -9,7 +9,8 @@ import java.nio.channels.SocketChannel; import java.util.concurrent.Callable; /** - * Created by loke on 27/06/2017. + * Author: Lorenzo Iovino on 27/06/2017. + * Description: Jedis Service */ public class SendMessageToAllPlayers implements Callable { diff --git a/Server/src/com/texttwist/server/tasks/SendScores.java b/Server/src/com/texttwist/server/tasks/SendScores.java new file mode 100644 index 0000000..62ec42f --- /dev/null +++ b/Server/src/com/texttwist/server/tasks/SendScores.java @@ -0,0 +1,46 @@ +package com.texttwist.server.tasks; + +import com.texttwist.server.models.Match; +import constants.Config; +import models.Message; + +import java.io.IOException; +import java.net.DatagramPacket; +import java.net.InetAddress; +import java.net.MulticastSocket; +import java.util.concurrent.Callable; + +import static com.texttwist.server.services.MessageService.activeMatches; + +/** + * Created by loke on 13/07/2017. + */ +public class SendScores implements Callable { + + private Match match; + + public SendScores(Match match){ + this.match = match; + } + + @Override + public Void call() throws Exception { + + while (true) { + System.out.println("SENDING"); + Message msg = new Message("FINALSCORE", "SERVER", "", match.getMatchPlayersScoreAsStringList()); + + MulticastSocket multicastSocket = null; + try { + multicastSocket = new MulticastSocket(match.multicastId); + InetAddress ia = InetAddress.getByName(Config.ScoreMulticastServerURI); + DatagramPacket hi = new DatagramPacket(msg.toString().getBytes(), msg.toString().length(), ia, match.multicastId); + multicastSocket.send(hi); + } catch (IOException e) { + e.printStackTrace(); + } + activeMatches.remove(Match.findMatchIndex(activeMatches, match.matchCreator)); + return null; + } + } +} diff --git a/Server/src/com/texttwist/server/tasks/TokenInvalid.java b/Server/src/com/texttwist/server/tasks/TokenInvalid.java index 22245b2..b190c15 100644 --- a/Server/src/com/texttwist/server/tasks/TokenInvalid.java +++ b/Server/src/com/texttwist/server/tasks/TokenInvalid.java @@ -5,12 +5,12 @@ import models.Message; import javax.swing.*; import java.nio.ByteBuffer; -import java.nio.channels.Channel; import java.nio.channels.SocketChannel; import java.util.concurrent.Callable; /** - * Created by loke on 11/07/2017. + * Author: Lorenzo Iovino on 11/07/2017. + * Description: Task: Token Invalid Service */ public class TokenInvalid implements Callable { private String sender; @@ -25,9 +25,8 @@ public class TokenInvalid implements Callable { @Override public Boolean call()throws Exception { - System.out.print("TOKEN NON VALIDO"); - buffer = ByteBuffer.allocate(1024); - Message msg = new Message("MATCH_NOT_AVAILABLE", "", null, new DefaultListModel<>()); + Server.logger.write("TOKEN INVALID: TOKEN USED BY "+ sender+ " IS NOT VALID"); + Message msg = new Message("TOKEN_NOT_VALID", "", null, new DefaultListModel<>()); buffer.clear(); byte[] byteMessage = msg.toString().getBytes(); buffer = ByteBuffer.wrap(byteMessage); diff --git a/notificationServer.log b/notificationServer.log index 3efbe52..1279a2c 100644 --- a/notificationServer.log +++ b/notificationServer.log @@ -1296,3 +1296,162 @@ LOGGER (Server): Thu Jul 13 03:30:57 CEST 2017 - Invoked login with username=asd LOGGER (Server): Thu Jul 13 03:30:57 CEST 2017 - Login successfull LOGGER (Server): Thu Jul 13 03:31:02 CEST 2017 - Invoked login with username=a AND password=a LOGGER (Server): Thu Jul 13 03:31:02 CEST 2017 - Login successfull +LOGGER (Server): Thu Jul 13 12:23:43 CEST 2017 - Services starting ... +LOGGER (Server): Thu Jul 13 12:23:44 CEST 2017 - AuthService Service running at 9999 port... +LOGGER (Server): Thu Jul 13 12:23:44 CEST 2017 - GameService Service is running at 10000 port... +LOGGER (Server): Thu Jul 13 12:23:44 CEST 2017 - Services started correctly ... +LOGGER (Server): Thu Jul 13 12:23:48 CEST 2017 - Invoked login with username=a AND password=a +LOGGER (Server): Thu Jul 13 12:23:48 CEST 2017 - Login successfull +LOGGER (Server): Thu Jul 13 12:23:51 CEST 2017 - Invoked login with username=b AND password=b +LOGGER (Server): Thu Jul 13 12:23:51 CEST 2017 - Login successfull +LOGGER (Server): Thu Jul 13 12:57:17 CEST 2017 - Services starting ... +LOGGER (Server): Thu Jul 13 12:57:17 CEST 2017 - AuthService Service running at 9999 port... +LOGGER (Server): Thu Jul 13 12:57:17 CEST 2017 - GameService Service is running at 10000 port... +LOGGER (Server): Thu Jul 13 12:57:17 CEST 2017 - Services started correctly ... +LOGGER (Server): Thu Jul 13 12:57:23 CEST 2017 - Invoked login with username=a AND password=a +LOGGER (Server): Thu Jul 13 12:57:23 CEST 2017 - Login successfull +LOGGER (Server): Thu Jul 13 12:57:26 CEST 2017 - Invoked login with username=b AND password=b +LOGGER (Server): Thu Jul 13 12:57:26 CEST 2017 - Login successfull +LOGGER (Server): Thu Jul 13 13:02:35 CEST 2017 - Services starting ... +LOGGER (Server): Thu Jul 13 13:02:36 CEST 2017 - AuthService Service running at 9999 port... +LOGGER (Server): Thu Jul 13 13:02:36 CEST 2017 - GameService Service is running at 10000 port... +LOGGER (Server): Thu Jul 13 13:02:36 CEST 2017 - Services started correctly ... +LOGGER (Server): Thu Jul 13 13:02:41 CEST 2017 - Invoked login with username=a AND password=a +LOGGER (Server): Thu Jul 13 13:02:42 CEST 2017 - Login successfull +LOGGER (Server): Thu Jul 13 13:02:44 CEST 2017 - Invoked login with username=b AND password=b +LOGGER (Server): Thu Jul 13 13:02:44 CEST 2017 - Login successfull +LOGGER (Server): Thu Jul 13 13:04:46 CEST 2017 - Services starting ... +LOGGER (Server): Thu Jul 13 13:04:46 CEST 2017 - AuthService Service running at 9999 port... +LOGGER (Server): Thu Jul 13 13:04:46 CEST 2017 - Services started correctly ... +LOGGER (Server): Thu Jul 13 13:04:46 CEST 2017 - GameService Service is running at 10000 port... +LOGGER (Server): Thu Jul 13 13:04:51 CEST 2017 - Invoked login with username=a AND password=a +LOGGER (Server): Thu Jul 13 13:04:51 CEST 2017 - Login successfull +LOGGER (Server): Thu Jul 13 13:04:54 CEST 2017 - Invoked login with username=b AND password=b +LOGGER (Server): Thu Jul 13 13:04:54 CEST 2017 - Login successfull +LOGGER (Server): Thu Jul 13 13:05:29 CEST 2017 - Services starting ... +LOGGER (Server): Thu Jul 13 13:05:29 CEST 2017 - AuthService Service running at 9999 port... +LOGGER (Server): Thu Jul 13 13:05:29 CEST 2017 - Services started correctly ... +LOGGER (Server): Thu Jul 13 13:05:29 CEST 2017 - GameService Service is running at 10000 port... +LOGGER (Server): Thu Jul 13 13:05:36 CEST 2017 - Invoked login with username=a AND password=a +LOGGER (Server): Thu Jul 13 13:05:36 CEST 2017 - Login successfull +LOGGER (Server): Thu Jul 13 13:05:38 CEST 2017 - Invoked login with username=b AND password=b +LOGGER (Server): Thu Jul 13 13:05:38 CEST 2017 - Login successfull +LOGGER (Server): Thu Jul 13 13:09:00 CEST 2017 - Services starting ... +LOGGER (Server): Thu Jul 13 13:09:00 CEST 2017 - AuthService Service running at 9999 port... +LOGGER (Server): Thu Jul 13 13:09:01 CEST 2017 - Services started correctly ... +LOGGER (Server): Thu Jul 13 13:09:01 CEST 2017 - GameService Service is running at 10000 port... +LOGGER (Server): Thu Jul 13 13:09:04 CEST 2017 - Invoked login with username=a AND password=a +LOGGER (Server): Thu Jul 13 13:09:04 CEST 2017 - Login successfull +LOGGER (Server): Thu Jul 13 13:09:07 CEST 2017 - Invoked login with username=b AND password=b +LOGGER (Server): Thu Jul 13 13:09:07 CEST 2017 - Login successfull +LOGGER (Server): Thu Jul 13 13:10:10 CEST 2017 - Services starting ... +LOGGER (Server): Thu Jul 13 13:10:10 CEST 2017 - AuthService Service running at 9999 port... +LOGGER (Server): Thu Jul 13 13:10:10 CEST 2017 - Services started correctly ... +LOGGER (Server): Thu Jul 13 13:10:10 CEST 2017 - GameService Service is running at 10000 port... +LOGGER (Server): Thu Jul 13 13:10:15 CEST 2017 - Invoked login with username=a AND password=a +LOGGER (Server): Thu Jul 13 13:10:15 CEST 2017 - Login successfull +LOGGER (Server): Thu Jul 13 13:10:17 CEST 2017 - Invoked login with username=b AND password=b +LOGGER (Server): Thu Jul 13 13:10:17 CEST 2017 - Login successfull +LOGGER (Server): Thu Jul 13 13:13:01 CEST 2017 - Services starting ... +LOGGER (Server): Thu Jul 13 13:13:01 CEST 2017 - AuthService Service running at 9999 port... +LOGGER (Server): Thu Jul 13 13:13:01 CEST 2017 - Services started correctly ... +LOGGER (Server): Thu Jul 13 13:13:01 CEST 2017 - GameService Service is running at 10000 port... +LOGGER (Server): Thu Jul 13 13:13:06 CEST 2017 - Invoked login with username=a AND password=a +LOGGER (Server): Thu Jul 13 13:13:06 CEST 2017 - Login successfull +LOGGER (Server): Thu Jul 13 13:13:08 CEST 2017 - Invoked login with username=b AND password=b +LOGGER (Server): Thu Jul 13 13:13:08 CEST 2017 - Login successfull +LOGGER (Server): Thu Jul 13 13:14:51 CEST 2017 - Services starting ... +LOGGER (Server): Thu Jul 13 13:14:51 CEST 2017 - AuthService Service running at 9999 port... +LOGGER (Server): Thu Jul 13 13:14:51 CEST 2017 - Services started correctly ... +LOGGER (Server): Thu Jul 13 13:14:51 CEST 2017 - GameService Service is running at 10000 port... +LOGGER (Server): Thu Jul 13 13:14:56 CEST 2017 - Invoked login with username=a AND password=a +LOGGER (Server): Thu Jul 13 13:14:56 CEST 2017 - Login successfull +LOGGER (Server): Thu Jul 13 13:14:58 CEST 2017 - Invoked login with username=b AND password=b +LOGGER (Server): Thu Jul 13 13:14:58 CEST 2017 - Login successfull +LOGGER (Server): Thu Jul 13 13:15:47 CEST 2017 - Services starting ... +LOGGER (Server): Thu Jul 13 13:15:47 CEST 2017 - AuthService Service running at 9999 port... +LOGGER (Server): Thu Jul 13 13:15:47 CEST 2017 - Services started correctly ... +LOGGER (Server): Thu Jul 13 13:15:47 CEST 2017 - GameService Service is running at 10000 port... +LOGGER (Server): Thu Jul 13 13:15:51 CEST 2017 - Invoked login with username=a AND password=a +LOGGER (Server): Thu Jul 13 13:15:51 CEST 2017 - Login successfull +LOGGER (Server): Thu Jul 13 13:15:53 CEST 2017 - Invoked logout with username=a AND token=8e462nrn7or1750ik15gsm1ha2 +LOGGER (Server): Thu Jul 13 13:15:53 CEST 2017 - Logout successfull +LOGGER (Server): Thu Jul 13 13:15:53 CEST 2017 - Logout successfull (but something gone wrong) +LOGGER (Server): Thu Jul 13 13:15:55 CEST 2017 - Invoked login with username=b AND password=b +LOGGER (Server): Thu Jul 13 13:15:55 CEST 2017 - Login successfull +LOGGER (Server): Thu Jul 13 13:16:02 CEST 2017 - Invoked login with username=a AND password=a +LOGGER (Server): Thu Jul 13 13:16:02 CEST 2017 - Login successfull +LOGGER (Server): Thu Jul 13 13:19:30 CEST 2017 - Services starting ... +LOGGER (Server): Thu Jul 13 13:19:30 CEST 2017 - AuthService Service running at 9999 port... +LOGGER (Server): Thu Jul 13 13:19:30 CEST 2017 - Services started correctly ... +LOGGER (Server): Thu Jul 13 13:19:30 CEST 2017 - GameService Service is running at 10000 port... +LOGGER (Server): Thu Jul 13 13:19:35 CEST 2017 - Invoked login with username=a AND password=a +LOGGER (Server): Thu Jul 13 13:19:35 CEST 2017 - Login successfull +LOGGER (Server): Thu Jul 13 13:19:37 CEST 2017 - Invoked login with username=b AND password=b +LOGGER (Server): Thu Jul 13 13:19:37 CEST 2017 - Login successfull +LOGGER (Server): Thu Jul 13 13:53:43 CEST 2017 - Services starting ... +LOGGER (Server): Thu Jul 13 13:53:43 CEST 2017 - AuthService Service running at 9999 port... +LOGGER (Server): Thu Jul 13 13:53:43 CEST 2017 - Services started correctly ... +LOGGER (Server): Thu Jul 13 13:53:43 CEST 2017 - GameService Service is running at 10000 port... +LOGGER (Server): Thu Jul 13 13:53:49 CEST 2017 - Invoked login with username=a AND password=a +LOGGER (Server): Thu Jul 13 13:53:49 CEST 2017 - Login successfull +LOGGER (Server): Thu Jul 13 13:53:52 CEST 2017 - Invoked login with username=b AND password=b +LOGGER (Server): Thu Jul 13 13:53:52 CEST 2017 - Login successfull +LOGGER (Server): Thu Jul 13 13:59:25 CEST 2017 - Services starting ... +LOGGER (Server): Thu Jul 13 13:59:25 CEST 2017 - AuthService Service running at 9999 port... +LOGGER (Server): Thu Jul 13 13:59:25 CEST 2017 - Services started correctly ... +LOGGER (Server): Thu Jul 13 13:59:25 CEST 2017 - GameService Service is running at 10000 port... +LOGGER (Server): Thu Jul 13 13:59:37 CEST 2017 - Invoked login with username=a AND password=a +LOGGER (Server): Thu Jul 13 13:59:37 CEST 2017 - Login successfull +LOGGER (Server): Thu Jul 13 13:59:41 CEST 2017 - Invoked login with username=b AND password=b +LOGGER (Server): Thu Jul 13 13:59:41 CEST 2017 - Login successfull +LOGGER (Server): Thu Jul 13 13:59:46 CEST 2017 - Invoked login with username=asd AND password=sad +LOGGER (Server): Thu Jul 13 13:59:46 CEST 2017 - Login unsuccessfull +LOGGER (Server): Thu Jul 13 13:59:50 CEST 2017 - Invoked login with username=asd AND password=asd +LOGGER (Server): Thu Jul 13 13:59:50 CEST 2017 - Login successfull +LOGGER (Server): Thu Jul 13 13:59:55 CEST 2017 - Invoked login with username=dd AND password=dd +LOGGER (Server): Thu Jul 13 13:59:55 CEST 2017 - Login successfull +LOGGER (Server): Thu Jul 13 14:01:58 CEST 2017 - Invoked register with username=ss AND password=ss +LOGGER (Server): Thu Jul 13 14:01:58 CEST 2017 - Registration successfull +LOGGER (Server): Thu Jul 13 14:02:00 CEST 2017 - Invoked login with username=ss AND password=ss +LOGGER (Server): Thu Jul 13 14:02:00 CEST 2017 - Login successfull +LOGGER (Server): Thu Jul 13 14:02:05 CEST 2017 - Invoked register with username=aa AND password=aa +LOGGER (Server): Thu Jul 13 14:02:05 CEST 2017 - Registration successfull +LOGGER (Server): Thu Jul 13 14:02:08 CEST 2017 - Invoked login with username=aa AND password=aa +LOGGER (Server): Thu Jul 13 14:02:08 CEST 2017 - Login successfull +LOGGER (Server): Thu Jul 13 14:02:43 CEST 2017 - Services starting ... +LOGGER (Server): Thu Jul 13 14:02:44 CEST 2017 - AuthService Service running at 9999 port... +LOGGER (Server): Thu Jul 13 14:02:44 CEST 2017 - Services started correctly ... +LOGGER (Server): Thu Jul 13 14:02:44 CEST 2017 - GameService Service is running at 10000 port... +LOGGER (Server): Thu Jul 13 14:03:08 CEST 2017 - Invoked login with username=a AND password=a +LOGGER (Server): Thu Jul 13 14:03:08 CEST 2017 - Login successfull +LOGGER (Server): Thu Jul 13 14:03:10 CEST 2017 - Invoked login with username=b AND password=b +LOGGER (Server): Thu Jul 13 14:03:10 CEST 2017 - Login successfull +LOGGER (Server): Thu Jul 13 14:03:12 CEST 2017 - Invoked login with username=asd AND password=asd +LOGGER (Server): Thu Jul 13 14:03:12 CEST 2017 - Login successfull +LOGGER (Server): Thu Jul 13 14:03:14 CEST 2017 - Invoked login with username=dd AND password=dd +LOGGER (Server): Thu Jul 13 14:03:14 CEST 2017 - Login successfull +LOGGER (Server): Thu Jul 13 14:10:29 CEST 2017 - Services starting ... +LOGGER (Server): Thu Jul 13 14:10:29 CEST 2017 - AuthService Service running at 9999 port... +LOGGER (Server): Thu Jul 13 14:10:30 CEST 2017 - Services started correctly ... +LOGGER (Server): Thu Jul 13 14:10:30 CEST 2017 - GameService Service is running at 10000 port... +LOGGER (Server): Thu Jul 13 14:10:34 CEST 2017 - Invoked login with username=a AND password=a +LOGGER (Server): Thu Jul 13 14:10:34 CEST 2017 - Login successfull +LOGGER (Server): Thu Jul 13 14:10:40 CEST 2017 - Invoked login with username=b AND password=b +LOGGER (Server): Thu Jul 13 14:10:40 CEST 2017 - Login successfull +LOGGER (Server): Thu Jul 13 14:10:45 CEST 2017 - Invoked login with username=dd AND password=dd +LOGGER (Server): Thu Jul 13 14:10:45 CEST 2017 - Login successfull +LOGGER (Server): Thu Jul 13 14:10:49 CEST 2017 - Invoked login with username=asd AND password=asd +LOGGER (Server): Thu Jul 13 14:10:49 CEST 2017 - Login successfull +LOGGER (Server): Thu Jul 13 14:14:11 CEST 2017 - Services starting ... +LOGGER (Server): Thu Jul 13 14:14:11 CEST 2017 - AuthService Service running at 9999 port... +LOGGER (Server): Thu Jul 13 14:14:11 CEST 2017 - Services started correctly ... +LOGGER (Server): Thu Jul 13 14:14:11 CEST 2017 - GameService Service is running at 10000 port... +LOGGER (Server): Thu Jul 13 14:14:24 CEST 2017 - Invoked login with username=a AND password=a +LOGGER (Server): Thu Jul 13 14:14:24 CEST 2017 - Login successfull +LOGGER (Server): Thu Jul 13 14:14:26 CEST 2017 - Invoked login with username=b AND password=b +LOGGER (Server): Thu Jul 13 14:14:26 CEST 2017 - Login successfull +LOGGER (Server): Thu Jul 13 14:14:30 CEST 2017 - Invoked login with username=asd AND password=asd +LOGGER (Server): Thu Jul 13 14:14:30 CEST 2017 - Login successfull +LOGGER (Server): Thu Jul 13 14:14:36 CEST 2017 - Invoked login with username=dd AND password=dd +LOGGER (Server): Thu Jul 13 14:14:36 CEST 2017 - Login successfull From e1b02284b6e621a8080c33c69eca2dab1f8b124b Mon Sep 17 00:00:00 2001 From: Lorenzo Iovino Date: Thu, 13 Jul 2017 15:43:42 +0200 Subject: [PATCH 02/13] ora funziona --- .idea/workspace.xml | 1045 ++++++++--------- .../client/tasks/WaitForPlayers.java | 2 + .../texttwist/server/tasks/JoinTimeout.java | 2 +- notificationServer.log | 70 ++ 4 files changed, 580 insertions(+), 539 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 8380487..9f0ea26 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -5,45 +5,11 @@
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + - - - - - @@ -66,88 +32,64 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - - - - - - - - - - - - - - - - - - - - + + - + + - - + + - - - - - - - - - - - - - - - + + - - + + - - - - - - - - - - - - - - - - - - - - - - - - + + @@ -156,25 +98,46 @@ - - - - - + + + - - + + - - + + + + + + + + + + + + + + + + + + + + + + + + + @@ -186,36 +149,36 @@ - m - ti - jo - joinTi - joinTimeo - joinTimeout= - joinTimeout - joinTimeout = - ma - math. - math - matc - match - match.jo - match.joi - match.join - match.joint - j - jin - ji - join - joinTime - joinTimeou - threadPool - 1000 - waiti + waitingforu + waitingfor + waitingfo + waitingf + waiting waiting waiting f - waiting for - waiting for u + G + GAME + GAME_S + GAME_ST + GAME_STA + GAME_STAR + match timeout + timeout canc + GAME_START + TI + TIMEOUT + timeout cancelled + TIMEOUTT + findMath + match.joinTime + ma + match + match. + match.joi + match.join + match.joinT + match.joinTi + match.joinTim C:\Users\loke\TextTwist\out\production @@ -354,6 +317,25 @@ @@ -445,7 +408,7 @@ - + @@ -459,31 +422,8 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + @@ -609,7 +523,7 @@ + @@ -1321,13 +1314,14 @@ - - + + + - @@ -1343,14 +1337,14 @@ - + - + - + @@ -1358,35 +1352,11 @@ - - - - - - - - - - - - - - - - - - - - - - - - @@ -1406,6 +1376,94 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -1438,9 +1496,13 @@ - - - + + + + + + + @@ -1448,6 +1510,7 @@ + @@ -1491,6 +1554,13 @@ + + + + + + + @@ -1534,307 +1604,206 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + - - + + - + - - + + - + - - - - - - - - - - - - - - - - - - - - + + - - + + + + + + + + + + + + + + + + + + + + + + + + + + - + - + - - - + + + + + + + + + + + + + + + + + + + + + + + + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + - - - - - - - - - - - - + + - - + + + + + + + + + + + + + - + - - + + + + + + + + + + + + + + + + + + + + + + + + + + - + + + + + + + + + + + + + + + + + + + + + + + + + - - - - - + + + diff --git a/Client/src/com/texttwist/client/tasks/WaitForPlayers.java b/Client/src/com/texttwist/client/tasks/WaitForPlayers.java index cdc096a..e0dfabb 100644 --- a/Client/src/com/texttwist/client/tasks/WaitForPlayers.java +++ b/Client/src/com/texttwist/client/tasks/WaitForPlayers.java @@ -35,6 +35,8 @@ public class WaitForPlayers extends SwingWorker { while (App.clientTCP.read(buffer) != -1) { String line = new String(buffer.array(), buffer.position(), buffer.remaining()); buffer.clear(); + System.out.println("ASPETTO GAM_STARTED"); + System.out.println(line); if (line.startsWith("MESSAGE")) { buffer.clear(); diff --git a/Server/src/com/texttwist/server/tasks/JoinTimeout.java b/Server/src/com/texttwist/server/tasks/JoinTimeout.java index cf0a0f9..435a4ad 100644 --- a/Server/src/com/texttwist/server/tasks/JoinTimeout.java +++ b/Server/src/com/texttwist/server/tasks/JoinTimeout.java @@ -21,10 +21,10 @@ public class JoinTimeout implements Callable { System.out.println("TIMEOUTTTT"); if(match.joinTimeout) { + match.joinTimeout = false; return false; } else { - match.joinTimeout=true; return true; } } catch (InterruptedException e) { diff --git a/notificationServer.log b/notificationServer.log index 1279a2c..6f5bdbf 100644 --- a/notificationServer.log +++ b/notificationServer.log @@ -1455,3 +1455,73 @@ LOGGER (Server): Thu Jul 13 14:14:30 CEST 2017 - Invoked login with username=asd LOGGER (Server): Thu Jul 13 14:14:30 CEST 2017 - Login successfull LOGGER (Server): Thu Jul 13 14:14:36 CEST 2017 - Invoked login with username=dd AND password=dd LOGGER (Server): Thu Jul 13 14:14:36 CEST 2017 - Login successfull +LOGGER (Server): Thu Jul 13 15:21:33 CEST 2017 - Services starting ... +LOGGER (Server): Thu Jul 13 15:21:33 CEST 2017 - AuthService Service running at 9999 port... +LOGGER (Server): Thu Jul 13 15:21:33 CEST 2017 - Services started correctly ... +LOGGER (Server): Thu Jul 13 15:21:33 CEST 2017 - GameService Service is running at 10000 port... +LOGGER (Server): Thu Jul 13 15:21:37 CEST 2017 - Invoked login with username=a AND password=a +LOGGER (Server): Thu Jul 13 15:21:37 CEST 2017 - Login successfull +LOGGER (Server): Thu Jul 13 15:21:43 CEST 2017 - Invoked login with username=b AND password=b +LOGGER (Server): Thu Jul 13 15:21:43 CEST 2017 - Login successfull +LOGGER (Server): Thu Jul 13 15:22:27 CEST 2017 - Invoked login with username=asd AND password=asd +LOGGER (Server): Thu Jul 13 15:22:27 CEST 2017 - Login successfull +LOGGER (Server): Thu Jul 13 15:22:30 CEST 2017 - Invoked login with username=dd AND password=dd +LOGGER (Server): Thu Jul 13 15:22:30 CEST 2017 - Login successfull +LOGGER (Server): Thu Jul 13 15:24:34 CEST 2017 - Services starting ... +LOGGER (Server): Thu Jul 13 15:24:35 CEST 2017 - AuthService Service running at 9999 port... +LOGGER (Server): Thu Jul 13 15:24:35 CEST 2017 - Services started correctly ... +LOGGER (Server): Thu Jul 13 15:24:35 CEST 2017 - GameService Service is running at 10000 port... +LOGGER (Server): Thu Jul 13 15:24:45 CEST 2017 - Invoked login with username=a AND password=a +LOGGER (Server): Thu Jul 13 15:24:45 CEST 2017 - Login successfull +LOGGER (Server): Thu Jul 13 15:24:48 CEST 2017 - Invoked login with username=b AND password=b +LOGGER (Server): Thu Jul 13 15:24:48 CEST 2017 - Login successfull +LOGGER (Server): Thu Jul 13 15:24:52 CEST 2017 - Invoked login with username=asd AND password=asd +LOGGER (Server): Thu Jul 13 15:24:52 CEST 2017 - Login successfull +LOGGER (Server): Thu Jul 13 15:24:58 CEST 2017 - Invoked login with username=dd AND password=dd +LOGGER (Server): Thu Jul 13 15:24:58 CEST 2017 - Login successfull +LOGGER (Server): Thu Jul 13 15:30:01 CEST 2017 - Services starting ... +LOGGER (Server): Thu Jul 13 15:30:01 CEST 2017 - AuthService Service running at 9999 port... +LOGGER (Server): Thu Jul 13 15:30:01 CEST 2017 - Services started correctly ... +LOGGER (Server): Thu Jul 13 15:30:01 CEST 2017 - GameService Service is running at 10000 port... +LOGGER (Server): Thu Jul 13 15:30:09 CEST 2017 - Invoked login with username=a AND password=a +LOGGER (Server): Thu Jul 13 15:30:09 CEST 2017 - Login successfull +LOGGER (Server): Thu Jul 13 15:30:16 CEST 2017 - Invoked login with username=b AND password=b +LOGGER (Server): Thu Jul 13 15:30:16 CEST 2017 - Login successfull +LOGGER (Server): Thu Jul 13 15:30:19 CEST 2017 - Invoked login with username=asd AND password=asd +LOGGER (Server): Thu Jul 13 15:30:19 CEST 2017 - Login successfull +LOGGER (Server): Thu Jul 13 15:30:22 CEST 2017 - Invoked login with username=dd AND password=dd +LOGGER (Server): Thu Jul 13 15:30:22 CEST 2017 - Login successfull +LOGGER (Server): Thu Jul 13 15:32:29 CEST 2017 - Services starting ... +LOGGER (Server): Thu Jul 13 15:32:29 CEST 2017 - AuthService Service running at 9999 port... +LOGGER (Server): Thu Jul 13 15:32:30 CEST 2017 - Services started correctly ... +LOGGER (Server): Thu Jul 13 15:32:30 CEST 2017 - GameService Service is running at 10000 port... +LOGGER (Server): Thu Jul 13 15:32:41 CEST 2017 - Invoked login with username=a AND password=a +LOGGER (Server): Thu Jul 13 15:32:41 CEST 2017 - Login successfull +LOGGER (Server): Thu Jul 13 15:32:42 CEST 2017 - Invoked login with username=b AND password=b +LOGGER (Server): Thu Jul 13 15:32:42 CEST 2017 - Login successfull +LOGGER (Server): Thu Jul 13 15:32:46 CEST 2017 - Invoked login with username=dd AND password=dd +LOGGER (Server): Thu Jul 13 15:32:46 CEST 2017 - Login successfull +LOGGER (Server): Thu Jul 13 15:32:52 CEST 2017 - Invoked login with username=asd AND password=asd +LOGGER (Server): Thu Jul 13 15:32:52 CEST 2017 - Login successfull +LOGGER (Server): Thu Jul 13 15:33:10 CEST 2017 - Invoked register with username=ff AND password=ff +LOGGER (Server): Thu Jul 13 15:33:10 CEST 2017 - Registration successfull +LOGGER (Server): Thu Jul 13 15:33:16 CEST 2017 - Invoked login with username=ff AND password=ff +LOGGER (Server): Thu Jul 13 15:33:16 CEST 2017 - Login successfull +LOGGER (Server): Thu Jul 13 15:33:28 CEST 2017 - Invoked login with username=ss AND password=ss +LOGGER (Server): Thu Jul 13 15:33:28 CEST 2017 - Login successfull +LOGGER (Server): Thu Jul 13 15:39:59 CEST 2017 - Services starting ... +LOGGER (Server): Thu Jul 13 15:39:59 CEST 2017 - AuthService Service running at 9999 port... +LOGGER (Server): Thu Jul 13 15:39:59 CEST 2017 - GameService Service is running at 10000 port... +LOGGER (Server): Thu Jul 13 15:39:59 CEST 2017 - Services started correctly ... +LOGGER (Server): Thu Jul 13 15:40:03 CEST 2017 - Invoked login with username=a AND password=a +LOGGER (Server): Thu Jul 13 15:40:03 CEST 2017 - Login successfull +LOGGER (Server): Thu Jul 13 15:40:11 CEST 2017 - Invoked login with username=b AND password=b +LOGGER (Server): Thu Jul 13 15:40:11 CEST 2017 - Login successfull +LOGGER (Server): Thu Jul 13 15:40:18 CEST 2017 - Invoked login with username=asd AND password=asd +LOGGER (Server): Thu Jul 13 15:40:18 CEST 2017 - Login successfull +LOGGER (Server): Thu Jul 13 15:40:22 CEST 2017 - Invoked login with username=dd AND password=dd +LOGGER (Server): Thu Jul 13 15:40:22 CEST 2017 - Login successfull +LOGGER (Server): Thu Jul 13 15:40:44 CEST 2017 - Invoked login with username=ss AND password=ss +LOGGER (Server): Thu Jul 13 15:40:44 CEST 2017 - Login successfull +LOGGER (Server): Thu Jul 13 15:40:53 CEST 2017 - Invoked login with username=aa AND password=aa +LOGGER (Server): Thu Jul 13 15:40:53 CEST 2017 - Login successfull From 3305d6500bcabef3f33cb2b9d250efbdd0ee3a60 Mon Sep 17 00:00:00 2001 From: Lorenzo Iovino Date: Thu, 13 Jul 2017 21:31:49 +0200 Subject: [PATCH 03/13] FUNZIONAAAAAHAHHA --- .idea/workspace.xml | 548 +++++++++--------- .../com/texttwist/client/tasks/SendWords.java | 3 +- .../com/texttwist/server/models/Match.java | 7 +- .../server/servers/ProxyDispatcher.java | 8 +- .../texttwist/server/tasks/ComputeScore.java | 8 +- .../com/texttwist/server/tasks/JoinMatch.java | 3 +- .../texttwist/server/tasks/JoinTimeout.java | 9 +- .../texttwist/server/tasks/MatchTimeout.java | 3 +- .../texttwist/server/tasks/ReceiveWords.java | 12 +- notificationServer.log | 216 +++++++ 10 files changed, 508 insertions(+), 309 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 9f0ea26..0bf1fe1 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -7,9 +7,14 @@ - - + + + + + + + @@ -32,74 +37,34 @@ - - + + - - + + - - - - - - + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + - - + + - - + + @@ -108,31 +73,40 @@ - - + + - - + + - - + + - - + + - - + + + + + + + + + + + + - - + @@ -149,36 +123,36 @@ - waitingforu - waitingfor - waitingfo - waitingf - waiting - waiting - waiting f - G - GAME - GAME_S - GAME_ST - GAME_STA - GAME_STAR - match timeout - timeout canc - GAME_START - TI - TIMEOUT - timeout cancelled - TIMEOUTT - findMath - match.joinTime - ma - match - match. - match.joi - match.join - match.joinT - match.joinTi - match.joinTim + s + send invit + sendi + sending i + sending invi + wo + words + words r + wor + words re + y + ti + tim + timeoy + timeo + timeou + timeout + timeout f + timeout fi + timeout fini + timeout finit + i + inio g + inio + inio + in + inv + invio h + invio + invio g C:\Users\loke\TextTwist\out\production @@ -317,16 +291,12 @@ @@ -1316,12 +1290,12 @@ - + - @@ -1337,7 +1311,7 @@ - + @@ -1350,7 +1324,7 @@ - + @@ -1367,7 +1341,7 @@ - @@ -1376,59 +1350,6 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -1471,13 +1392,6 @@ - - - - - - - @@ -1554,13 +1468,6 @@ - - - - - - - @@ -1612,16 +1519,6 @@ - - - - - - - - - - @@ -1630,14 +1527,6 @@ - - - - - - - - @@ -1654,14 +1543,6 @@ - - - - - - - - @@ -1680,55 +1561,13 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + @@ -1741,72 +1580,207 @@ - + - - + + - + - - - - - - - - - - - - - - - - - - + + - - - - - - + + + + + + + + + + + - - + + - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - + + - + - - + + + + + + + + + + + + diff --git a/Client/src/com/texttwist/client/tasks/SendWords.java b/Client/src/com/texttwist/client/tasks/SendWords.java index d84a200..4b0a74d 100644 --- a/Client/src/com/texttwist/client/tasks/SendWords.java +++ b/Client/src/com/texttwist/client/tasks/SendWords.java @@ -36,8 +36,7 @@ public class SendWords extends SwingWorker { buffer.flip(); App.clientUDP.send(buffer, App.clientUDPSocketAddress); - - return null; + System.out.println("SENDED" + sentence); } catch (UnknownHostException e) { App.logger.write("SEND WORDS: Host address not correct"); } catch (IOException e) { diff --git a/Server/src/com/texttwist/server/models/Match.java b/Server/src/com/texttwist/server/models/Match.java index a9f8066..11d077e 100644 --- a/Server/src/com/texttwist/server/models/Match.java +++ b/Server/src/com/texttwist/server/models/Match.java @@ -44,8 +44,8 @@ public class Match { public Integer multicastId; //True if happen timeout, false otherwise - public boolean matchTimeout; - public boolean joinTimeout; + public boolean matchTimeout = false; + public boolean joinTimeout = false; //Letters of the match public DefaultListModel letters; @@ -109,6 +109,7 @@ public class Match { } public void setScore(String player, Integer score){ + Match m = findMatchByPlayerName(player); if(m!=null) { for (int i = 0; i < m.playersScore.size(); i++) { @@ -121,6 +122,7 @@ public class Match { public Boolean allPlayersSendedHisScore(){ for (Pair player : playersScore) { + System.out.println(player.getValue()); if (player.getValue() == -1) { return false; } @@ -129,6 +131,7 @@ public class Match { } public void setUndefinedScorePlayersToZero(){ + for (int i = 0; i < playersScore.size(); i++) { if (playersScore.get(i).getValue() == -1) { playersScore.set(i, new Pair<>(playersScore.get(i).getKey(), 0)); diff --git a/Server/src/com/texttwist/server/servers/ProxyDispatcher.java b/Server/src/com/texttwist/server/servers/ProxyDispatcher.java index dfe24f8..edc2122 100644 --- a/Server/src/com/texttwist/server/servers/ProxyDispatcher.java +++ b/Server/src/com/texttwist/server/servers/ProxyDispatcher.java @@ -1,8 +1,10 @@ package com.texttwist.server.servers; +import com.sun.org.apache.xpath.internal.operations.Bool; import com.texttwist.server.services.SessionsService; import com.texttwist.server.models.Match; import com.texttwist.server.tasks.*; +import javafx.util.Pair; import models.Message; import javax.swing.*; @@ -71,11 +73,13 @@ public class ProxyDispatcher implements Callable { socketChannel.write(bufferMessage); } + Future joinTimeout = threadPool.submit(new JoinTimeout(match)); - joinTimeout.get(); - if(match.joinTimeout){ + Boolean joinTimeoutRes = joinTimeout.get(); + if(joinTimeoutRes){ Future sendMessageJoinTimeout = threadPool.submit( new SendMessageToAllPlayers(match, new Message("JOIN_TIMEOUT", "", "", new DefaultListModel<>()), socketChannel)); + Boolean sendMessageJoinTimeoutRes = sendMessageJoinTimeout.get(); if(!sendMessageJoinTimeoutRes){ activeMatches.remove(Match.findMatchIndex(activeMatches, match.matchCreator)); diff --git a/Server/src/com/texttwist/server/tasks/ComputeScore.java b/Server/src/com/texttwist/server/tasks/ComputeScore.java index 6ea1ca0..4aa5453 100644 --- a/Server/src/com/texttwist/server/tasks/ComputeScore.java +++ b/Server/src/com/texttwist/server/tasks/ComputeScore.java @@ -2,6 +2,7 @@ package com.texttwist.server.tasks; import com.texttwist.server.services.AccountsService; import com.texttwist.server.models.Dictionary; import com.texttwist.server.models.Match; +import javafx.util.Pair; import models.User; import javax.swing.*; @@ -37,8 +38,13 @@ public class ComputeScore implements Callable { } } + System.out.println(sender +" totalize SCORE = " + score); match.setScore(sender, score); + for (Pair player : match.playersScore) { + System.out.println(player.getValue()); + + } System.out.println(score); User u = AccountsService.getInstance().findUser(sender); @@ -54,8 +60,6 @@ public class ComputeScore implements Callable { } return score; - - } private Boolean isValid(String word, DefaultListModel letters) { diff --git a/Server/src/com/texttwist/server/tasks/JoinMatch.java b/Server/src/com/texttwist/server/tasks/JoinMatch.java index 84cf13c..78ad16c 100644 --- a/Server/src/com/texttwist/server/tasks/JoinMatch.java +++ b/Server/src/com/texttwist/server/tasks/JoinMatch.java @@ -58,7 +58,8 @@ public class JoinMatch implements Callable { return false; } } - match.joinTimeout=false; + match.printAll(); + match.joinTimeout = false; return true; } } diff --git a/Server/src/com/texttwist/server/tasks/JoinTimeout.java b/Server/src/com/texttwist/server/tasks/JoinTimeout.java index 435a4ad..2f39eff 100644 --- a/Server/src/com/texttwist/server/tasks/JoinTimeout.java +++ b/Server/src/com/texttwist/server/tasks/JoinTimeout.java @@ -17,15 +17,16 @@ public class JoinTimeout implements Callable { @Override public Boolean call() throws Exception { try { - Thread.currentThread().sleep(1*60*1000); - System.out.println("TIMEOUTTTT"); + match.joinTimeout=true; + Thread.currentThread().sleep(5000); if(match.joinTimeout) { match.joinTimeout = false; - return false; + return true; } else { - return true; + match.joinTimeout = false; + return false; } } catch (InterruptedException e) { e.printStackTrace(); diff --git a/Server/src/com/texttwist/server/tasks/MatchTimeout.java b/Server/src/com/texttwist/server/tasks/MatchTimeout.java index 9e45b84..91755f5 100644 --- a/Server/src/com/texttwist/server/tasks/MatchTimeout.java +++ b/Server/src/com/texttwist/server/tasks/MatchTimeout.java @@ -21,10 +21,9 @@ public class MatchTimeout implements Callable { public Boolean call() throws Exception { try { Thread.currentThread().sleep(Config.sendWordsTimeout); - match.setUndefinedScorePlayersToZero(); if(match.matchTimeout) { - System.out.println("SEND BROADCAST BECAUSE TIMEOUT"); + match.setUndefinedScorePlayersToZero(); new SendScores(match).call(); return true; } diff --git a/Server/src/com/texttwist/server/tasks/ReceiveWords.java b/Server/src/com/texttwist/server/tasks/ReceiveWords.java index c2786c8..d6877fb 100644 --- a/Server/src/com/texttwist/server/tasks/ReceiveWords.java +++ b/Server/src/com/texttwist/server/tasks/ReceiveWords.java @@ -3,6 +3,7 @@ package com.texttwist.server.tasks; import com.texttwist.server.services.SessionsService; import com.texttwist.server.models.Match; import constants.Config; +import javafx.util.Pair; import models.Message; import java.net.*; @@ -40,16 +41,18 @@ public class ReceiveWords implements Callable{ Message msg; DatagramSocket s = new DatagramSocket(Config.WordsReceiverServerPort); - + DatagramPacket packet; while(true) { + byte[] buf = new byte[1024]; System.out.println("RECEIVIN WORDS"); - DatagramPacket packet = new DatagramPacket(buf, buf.length); + packet = new DatagramPacket(buf, buf.length); s.receive(packet); System.out.println("WORDS RECEIVED"); + String rcv = new String(packet.getData()); System.out.println(rcv); if (rcv.startsWith("MESSAGE")) { @@ -60,13 +63,8 @@ public class ReceiveWords implements Callable{ threadPool.submit(new ComputeScore(msg.sender, msg.data, match)); } else { threadPool.submit(new TokenInvalid(msg.sender, socketChannel, bufferMessages)); - return false; } } - } - - } - } diff --git a/notificationServer.log b/notificationServer.log index 6f5bdbf..d735343 100644 --- a/notificationServer.log +++ b/notificationServer.log @@ -1525,3 +1525,219 @@ LOGGER (Server): Thu Jul 13 15:40:44 CEST 2017 - Invoked login with username=ss LOGGER (Server): Thu Jul 13 15:40:44 CEST 2017 - Login successfull LOGGER (Server): Thu Jul 13 15:40:53 CEST 2017 - Invoked login with username=aa AND password=aa LOGGER (Server): Thu Jul 13 15:40:53 CEST 2017 - Login successfull +LOGGER (Server): Thu Jul 13 20:22:13 CEST 2017 - Services starting ... +LOGGER (Server): Thu Jul 13 20:22:13 CEST 2017 - AuthService Service running at 9999 port... +LOGGER (Server): Thu Jul 13 20:22:13 CEST 2017 - Services started correctly ... +LOGGER (Server): Thu Jul 13 20:22:13 CEST 2017 - GameService Service is running at 10000 port... +LOGGER (Server): Thu Jul 13 20:22:19 CEST 2017 - Invoked login with username=a AND password=a +LOGGER (Server): Thu Jul 13 20:22:19 CEST 2017 - Login successfull +LOGGER (Server): Thu Jul 13 20:22:26 CEST 2017 - Invoked login with username=b AND password=b +LOGGER (Server): Thu Jul 13 20:22:26 CEST 2017 - Login successfull +LOGGER (Server): Thu Jul 13 20:23:55 CEST 2017 - Services starting ... +LOGGER (Server): Thu Jul 13 20:23:55 CEST 2017 - AuthService Service running at 9999 port... +LOGGER (Server): Thu Jul 13 20:23:55 CEST 2017 - GameService Service is running at 10000 port... +LOGGER (Server): Thu Jul 13 20:23:55 CEST 2017 - Services started correctly ... +LOGGER (Server): Thu Jul 13 20:23:59 CEST 2017 - Invoked login with username=a AND password=a +LOGGER (Server): Thu Jul 13 20:23:59 CEST 2017 - Login successfull +LOGGER (Server): Thu Jul 13 20:24:02 CEST 2017 - Invoked login with username=b AND password=b +LOGGER (Server): Thu Jul 13 20:24:02 CEST 2017 - Login successfull +LOGGER (Server): Thu Jul 13 20:27:33 CEST 2017 - Services starting ... +LOGGER (Server): Thu Jul 13 20:27:33 CEST 2017 - AuthService Service running at 9999 port... +LOGGER (Server): Thu Jul 13 20:27:33 CEST 2017 - Services started correctly ... +LOGGER (Server): Thu Jul 13 20:27:33 CEST 2017 - GameService Service is running at 10000 port... +LOGGER (Server): Thu Jul 13 20:27:39 CEST 2017 - Invoked login with username=a AND password=a +LOGGER (Server): Thu Jul 13 20:27:39 CEST 2017 - Login successfull +LOGGER (Server): Thu Jul 13 20:27:45 CEST 2017 - Invoked login with username=b AND password=b +LOGGER (Server): Thu Jul 13 20:27:45 CEST 2017 - Login successfull +LOGGER (Server): Thu Jul 13 20:28:51 CEST 2017 - Services starting ... +LOGGER (Server): Thu Jul 13 20:28:51 CEST 2017 - AuthService Service running at 9999 port... +LOGGER (Server): Thu Jul 13 20:28:51 CEST 2017 - Services started correctly ... +LOGGER (Server): Thu Jul 13 20:28:51 CEST 2017 - GameService Service is running at 10000 port... +LOGGER (Server): Thu Jul 13 20:28:57 CEST 2017 - Invoked login with username=a AND password=a +LOGGER (Server): Thu Jul 13 20:28:57 CEST 2017 - Login successfull +LOGGER (Server): Thu Jul 13 20:29:00 CEST 2017 - Invoked login with username=b AND password=b +LOGGER (Server): Thu Jul 13 20:29:00 CEST 2017 - Login successfull +LOGGER (Server): Thu Jul 13 20:31:17 CEST 2017 - Services starting ... +LOGGER (Server): Thu Jul 13 20:31:18 CEST 2017 - AuthService Service running at 9999 port... +LOGGER (Server): Thu Jul 13 20:31:18 CEST 2017 - GameService Service is running at 10000 port... +LOGGER (Server): Thu Jul 13 20:31:18 CEST 2017 - Services started correctly ... +LOGGER (Server): Thu Jul 13 20:31:24 CEST 2017 - Invoked login with username=a AND password=a +LOGGER (Server): Thu Jul 13 20:31:24 CEST 2017 - Login successfull +LOGGER (Server): Thu Jul 13 20:31:29 CEST 2017 - Invoked login with username=b AND password=b +LOGGER (Server): Thu Jul 13 20:31:29 CEST 2017 - Login successfull +LOGGER (Server): Thu Jul 13 20:35:54 CEST 2017 - Services starting ... +LOGGER (Server): Thu Jul 13 20:35:54 CEST 2017 - AuthService Service running at 9999 port... +LOGGER (Server): Thu Jul 13 20:35:54 CEST 2017 - GameService Service is running at 10000 port... +LOGGER (Server): Thu Jul 13 20:35:54 CEST 2017 - Services started correctly ... +LOGGER (Server): Thu Jul 13 20:36:48 CEST 2017 - Services starting ... +LOGGER (Server): Thu Jul 13 20:36:48 CEST 2017 - AuthService Service running at 9999 port... +LOGGER (Server): Thu Jul 13 20:36:48 CEST 2017 - Services started correctly ... +LOGGER (Server): Thu Jul 13 20:36:48 CEST 2017 - GameService Service is running at 10000 port... +LOGGER (Server): Thu Jul 13 20:36:53 CEST 2017 - Invoked login with username=a AND password=a +LOGGER (Server): Thu Jul 13 20:36:53 CEST 2017 - Login successfull +LOGGER (Server): Thu Jul 13 20:36:57 CEST 2017 - Invoked login with username=b AND password=b +LOGGER (Server): Thu Jul 13 20:36:57 CEST 2017 - Login successfull +LOGGER (Server): Thu Jul 13 20:46:02 CEST 2017 - Services starting ... +LOGGER (Server): Thu Jul 13 20:46:02 CEST 2017 - AuthService Service running at 9999 port... +LOGGER (Server): Thu Jul 13 20:46:02 CEST 2017 - GameService Service is running at 10000 port... +LOGGER (Server): Thu Jul 13 20:46:02 CEST 2017 - Services started correctly ... +LOGGER (Server): Thu Jul 13 20:46:08 CEST 2017 - Invoked login with username=a AND password=a +LOGGER (Server): Thu Jul 13 20:46:08 CEST 2017 - Login successfull +LOGGER (Server): Thu Jul 13 20:46:11 CEST 2017 - Invoked login with username=b AND password=b +LOGGER (Server): Thu Jul 13 20:46:11 CEST 2017 - Login successfull +LOGGER (Server): Thu Jul 13 20:48:29 CEST 2017 - Services starting ... +LOGGER (Server): Thu Jul 13 20:48:29 CEST 2017 - AuthService Service running at 9999 port... +LOGGER (Server): Thu Jul 13 20:48:29 CEST 2017 - Services started correctly ... +LOGGER (Server): Thu Jul 13 20:48:29 CEST 2017 - GameService Service is running at 10000 port... +LOGGER (Server): Thu Jul 13 20:48:35 CEST 2017 - Invoked login with username=a AND password=a +LOGGER (Server): Thu Jul 13 20:48:35 CEST 2017 - Login successfull +LOGGER (Server): Thu Jul 13 20:48:39 CEST 2017 - Invoked login with username=b AND password=b +LOGGER (Server): Thu Jul 13 20:48:39 CEST 2017 - Login successfull +LOGGER (Server): Thu Jul 13 20:53:07 CEST 2017 - Services starting ... +LOGGER (Server): Thu Jul 13 20:53:08 CEST 2017 - AuthService Service running at 9999 port... +LOGGER (Server): Thu Jul 13 20:53:08 CEST 2017 - Services started correctly ... +LOGGER (Server): Thu Jul 13 20:53:08 CEST 2017 - GameService Service is running at 10000 port... +LOGGER (Server): Thu Jul 13 20:53:11 CEST 2017 - Invoked login with username=a AND password=a +LOGGER (Server): Thu Jul 13 20:53:11 CEST 2017 - Login successfull +LOGGER (Server): Thu Jul 13 20:53:16 CEST 2017 - Invoked login with username=b AND password=b +LOGGER (Server): Thu Jul 13 20:53:16 CEST 2017 - Login successfull +LOGGER (Server): Thu Jul 13 20:57:34 CEST 2017 - Services starting ... +LOGGER (Server): Thu Jul 13 20:57:34 CEST 2017 - AuthService Service running at 9999 port... +LOGGER (Server): Thu Jul 13 20:57:34 CEST 2017 - GameService Service is running at 10000 port... +LOGGER (Server): Thu Jul 13 20:57:34 CEST 2017 - Services started correctly ... +LOGGER (Server): Thu Jul 13 20:57:38 CEST 2017 - Invoked login with username=a AND password=a +LOGGER (Server): Thu Jul 13 20:57:38 CEST 2017 - Login successfull +LOGGER (Server): Thu Jul 13 20:57:41 CEST 2017 - Invoked login with username=b AND password=b +LOGGER (Server): Thu Jul 13 20:57:41 CEST 2017 - Login successfull +LOGGER (Server): Thu Jul 13 20:59:27 CEST 2017 - Services starting ... +LOGGER (Server): Thu Jul 13 20:59:27 CEST 2017 - AuthService Service running at 9999 port... +LOGGER (Server): Thu Jul 13 20:59:28 CEST 2017 - Services started correctly ... +LOGGER (Server): Thu Jul 13 20:59:28 CEST 2017 - GameService Service is running at 10000 port... +LOGGER (Server): Thu Jul 13 20:59:34 CEST 2017 - Invoked login with username=a AND password=a +LOGGER (Server): Thu Jul 13 20:59:34 CEST 2017 - Login successfull +LOGGER (Server): Thu Jul 13 20:59:36 CEST 2017 - Invoked login with username=b AND password=b +LOGGER (Server): Thu Jul 13 20:59:36 CEST 2017 - Login successfull +LOGGER (Server): Thu Jul 13 21:00:46 CEST 2017 - Services starting ... +LOGGER (Server): Thu Jul 13 21:00:46 CEST 2017 - AuthService Service running at 9999 port... +LOGGER (Server): Thu Jul 13 21:00:46 CEST 2017 - Services started correctly ... +LOGGER (Server): Thu Jul 13 21:00:46 CEST 2017 - GameService Service is running at 10000 port... +LOGGER (Server): Thu Jul 13 21:00:51 CEST 2017 - Invoked login with username=a AND password=a +LOGGER (Server): Thu Jul 13 21:00:51 CEST 2017 - Login successfull +LOGGER (Server): Thu Jul 13 21:00:54 CEST 2017 - Invoked login with username=b AND password=b +LOGGER (Server): Thu Jul 13 21:00:54 CEST 2017 - Login successfull +LOGGER (Server): Thu Jul 13 21:03:01 CEST 2017 - Services starting ... +LOGGER (Server): Thu Jul 13 21:03:02 CEST 2017 - AuthService Service running at 9999 port... +LOGGER (Server): Thu Jul 13 21:03:02 CEST 2017 - Services started correctly ... +LOGGER (Server): Thu Jul 13 21:03:02 CEST 2017 - GameService Service is running at 10000 port... +LOGGER (Server): Thu Jul 13 21:03:09 CEST 2017 - Invoked login with username=a AND password=a +LOGGER (Server): Thu Jul 13 21:03:09 CEST 2017 - Login successfull +LOGGER (Server): Thu Jul 13 21:03:13 CEST 2017 - Invoked login with username=b AND password=b +LOGGER (Server): Thu Jul 13 21:03:13 CEST 2017 - Login successfull +LOGGER (Server): Thu Jul 13 21:04:34 CEST 2017 - Services starting ... +LOGGER (Server): Thu Jul 13 21:04:34 CEST 2017 - AuthService Service running at 9999 port... +LOGGER (Server): Thu Jul 13 21:04:34 CEST 2017 - Services started correctly ... +LOGGER (Server): Thu Jul 13 21:04:34 CEST 2017 - GameService Service is running at 10000 port... +LOGGER (Server): Thu Jul 13 21:04:40 CEST 2017 - Invoked login with username=a AND password=a +LOGGER (Server): Thu Jul 13 21:04:40 CEST 2017 - Login successfull +LOGGER (Server): Thu Jul 13 21:04:45 CEST 2017 - Invoked login with username=b AND password=b +LOGGER (Server): Thu Jul 13 21:04:45 CEST 2017 - Login successfull +LOGGER (Server): Thu Jul 13 21:05:48 CEST 2017 - Services starting ... +LOGGER (Server): Thu Jul 13 21:05:49 CEST 2017 - AuthService Service running at 9999 port... +LOGGER (Server): Thu Jul 13 21:05:49 CEST 2017 - Services started correctly ... +LOGGER (Server): Thu Jul 13 21:05:49 CEST 2017 - GameService Service is running at 10000 port... +LOGGER (Server): Thu Jul 13 21:05:55 CEST 2017 - Invoked login with username=a AND password=a +LOGGER (Server): Thu Jul 13 21:05:55 CEST 2017 - Login successfull +LOGGER (Server): Thu Jul 13 21:06:00 CEST 2017 - Invoked login with username=b AND password=b +LOGGER (Server): Thu Jul 13 21:06:00 CEST 2017 - Login successfull +LOGGER (Server): Thu Jul 13 21:07:32 CEST 2017 - Services starting ... +LOGGER (Server): Thu Jul 13 21:07:33 CEST 2017 - AuthService Service running at 9999 port... +LOGGER (Server): Thu Jul 13 21:07:33 CEST 2017 - GameService Service is running at 10000 port... +LOGGER (Server): Thu Jul 13 21:07:33 CEST 2017 - Services started correctly ... +LOGGER (Server): Thu Jul 13 21:07:40 CEST 2017 - Invoked login with username=a AND password=a +LOGGER (Server): Thu Jul 13 21:07:40 CEST 2017 - Login successfull +LOGGER (Server): Thu Jul 13 21:07:45 CEST 2017 - Invoked login with username=b AND password=b +LOGGER (Server): Thu Jul 13 21:07:45 CEST 2017 - Login successfull +LOGGER (Server): Thu Jul 13 21:09:09 CEST 2017 - Services starting ... +LOGGER (Server): Thu Jul 13 21:09:10 CEST 2017 - AuthService Service running at 9999 port... +LOGGER (Server): Thu Jul 13 21:09:10 CEST 2017 - GameService Service is running at 10000 port... +LOGGER (Server): Thu Jul 13 21:09:10 CEST 2017 - Services started correctly ... +LOGGER (Server): Thu Jul 13 21:09:14 CEST 2017 - Invoked login with username=a AND password=a +LOGGER (Server): Thu Jul 13 21:09:14 CEST 2017 - Login successfull +LOGGER (Server): Thu Jul 13 21:09:19 CEST 2017 - Invoked login with username=b AND password=b +LOGGER (Server): Thu Jul 13 21:09:19 CEST 2017 - Login successfull +LOGGER (Server): Thu Jul 13 21:10:27 CEST 2017 - Services starting ... +LOGGER (Server): Thu Jul 13 21:10:27 CEST 2017 - AuthService Service running at 9999 port... +LOGGER (Server): Thu Jul 13 21:10:27 CEST 2017 - Services started correctly ... +LOGGER (Server): Thu Jul 13 21:10:27 CEST 2017 - GameService Service is running at 10000 port... +LOGGER (Server): Thu Jul 13 21:10:31 CEST 2017 - Services starting ... +LOGGER (Server): Thu Jul 13 21:10:31 CEST 2017 - AuthService Service running at 9999 port... +LOGGER (Server): Thu Jul 13 21:10:31 CEST 2017 - GameService Service is running at 10000 port... +LOGGER (Server): Thu Jul 13 21:10:31 CEST 2017 - Services started correctly ... +LOGGER (Server): Thu Jul 13 21:10:39 CEST 2017 - Invoked login with username=a AND password=a +LOGGER (Server): Thu Jul 13 21:10:39 CEST 2017 - Login successfull +LOGGER (Server): Thu Jul 13 21:10:41 CEST 2017 - Invoked login with username=b AND password=b +LOGGER (Server): Thu Jul 13 21:10:41 CEST 2017 - Login successfull +LOGGER (Server): Thu Jul 13 21:13:46 CEST 2017 - Services starting ... +LOGGER (Server): Thu Jul 13 21:13:46 CEST 2017 - AuthService Service running at 9999 port... +LOGGER (Server): Thu Jul 13 21:13:46 CEST 2017 - Services started correctly ... +LOGGER (Server): Thu Jul 13 21:13:46 CEST 2017 - GameService Service is running at 10000 port... +LOGGER (Server): Thu Jul 13 21:13:52 CEST 2017 - Invoked login with username=a AND password=a +LOGGER (Server): Thu Jul 13 21:13:52 CEST 2017 - Login successfull +LOGGER (Server): Thu Jul 13 21:13:55 CEST 2017 - Invoked login with username=b AND password=b +LOGGER (Server): Thu Jul 13 21:13:55 CEST 2017 - Login successfull +LOGGER (Server): Thu Jul 13 21:18:26 CEST 2017 - Services starting ... +LOGGER (Server): Thu Jul 13 21:18:26 CEST 2017 - AuthService Service running at 9999 port... +LOGGER (Server): Thu Jul 13 21:18:26 CEST 2017 - GameService Service is running at 10000 port... +LOGGER (Server): Thu Jul 13 21:18:26 CEST 2017 - Services started correctly ... +LOGGER (Server): Thu Jul 13 21:18:33 CEST 2017 - Invoked login with username=a AND password=a +LOGGER (Server): Thu Jul 13 21:18:33 CEST 2017 - Login successfull +LOGGER (Server): Thu Jul 13 21:18:38 CEST 2017 - Invoked login with username=b AND password=b +LOGGER (Server): Thu Jul 13 21:18:38 CEST 2017 - Login successfull +LOGGER (Server): Thu Jul 13 21:20:46 CEST 2017 - Services starting ... +LOGGER (Server): Thu Jul 13 21:20:46 CEST 2017 - AuthService Service running at 9999 port... +LOGGER (Server): Thu Jul 13 21:20:46 CEST 2017 - Services started correctly ... +LOGGER (Server): Thu Jul 13 21:20:46 CEST 2017 - GameService Service is running at 10000 port... +LOGGER (Server): Thu Jul 13 21:20:51 CEST 2017 - Invoked login with username=a AND password=a +LOGGER (Server): Thu Jul 13 21:20:51 CEST 2017 - Login successfull +LOGGER (Server): Thu Jul 13 21:20:55 CEST 2017 - Invoked login with username=b AND password=b +LOGGER (Server): Thu Jul 13 21:20:55 CEST 2017 - Login successfull +LOGGER (Server): Thu Jul 13 21:21:41 CEST 2017 - Services starting ... +LOGGER (Server): Thu Jul 13 21:21:41 CEST 2017 - AuthService Service running at 9999 port... +LOGGER (Server): Thu Jul 13 21:21:41 CEST 2017 - Services started correctly ... +LOGGER (Server): Thu Jul 13 21:21:41 CEST 2017 - GameService Service is running at 10000 port... +LOGGER (Server): Thu Jul 13 21:21:50 CEST 2017 - Invoked login with username=a AND password=a +LOGGER (Server): Thu Jul 13 21:21:51 CEST 2017 - Login successfull +LOGGER (Server): Thu Jul 13 21:21:55 CEST 2017 - Invoked login with username=b AND password=b +LOGGER (Server): Thu Jul 13 21:21:55 CEST 2017 - Login successfull +LOGGER (Server): Thu Jul 13 21:23:43 CEST 2017 - Services starting ... +LOGGER (Server): Thu Jul 13 21:23:43 CEST 2017 - AuthService Service running at 9999 port... +LOGGER (Server): Thu Jul 13 21:23:43 CEST 2017 - Services started correctly ... +LOGGER (Server): Thu Jul 13 21:23:43 CEST 2017 - GameService Service is running at 10000 port... +LOGGER (Server): Thu Jul 13 21:23:48 CEST 2017 - Invoked login with username=a AND password=a +LOGGER (Server): Thu Jul 13 21:23:48 CEST 2017 - Login successfull +LOGGER (Server): Thu Jul 13 21:23:52 CEST 2017 - Invoked login with username=b AND password=b +LOGGER (Server): Thu Jul 13 21:23:52 CEST 2017 - Login successfull +LOGGER (Server): Thu Jul 13 21:25:43 CEST 2017 - Services starting ... +LOGGER (Server): Thu Jul 13 21:25:44 CEST 2017 - AuthService Service running at 9999 port... +LOGGER (Server): Thu Jul 13 21:25:44 CEST 2017 - Services started correctly ... +LOGGER (Server): Thu Jul 13 21:25:44 CEST 2017 - GameService Service is running at 10000 port... +LOGGER (Server): Thu Jul 13 21:25:51 CEST 2017 - Invoked login with username=a AND password=a +LOGGER (Server): Thu Jul 13 21:25:51 CEST 2017 - Login successfull +LOGGER (Server): Thu Jul 13 21:25:55 CEST 2017 - Invoked login with username=b AND password=b +LOGGER (Server): Thu Jul 13 21:25:55 CEST 2017 - Login successfull +LOGGER (Server): Thu Jul 13 21:27:20 CEST 2017 - Services starting ... +LOGGER (Server): Thu Jul 13 21:27:20 CEST 2017 - AuthService Service running at 9999 port... +LOGGER (Server): Thu Jul 13 21:27:20 CEST 2017 - Services started correctly ... +LOGGER (Server): Thu Jul 13 21:27:20 CEST 2017 - GameService Service is running at 10000 port... +LOGGER (Server): Thu Jul 13 21:27:51 CEST 2017 - Invoked login with username=a AND password=a +LOGGER (Server): Thu Jul 13 21:27:51 CEST 2017 - Login successfull +LOGGER (Server): Thu Jul 13 21:27:53 CEST 2017 - Invoked login with username=b AND password=b +LOGGER (Server): Thu Jul 13 21:27:53 CEST 2017 - Login successfull +LOGGER (Server): Thu Jul 13 21:27:56 CEST 2017 - Invoked login with username=dd AND password=dd +LOGGER (Server): Thu Jul 13 21:27:56 CEST 2017 - Login successfull +LOGGER (Server): Thu Jul 13 21:27:58 CEST 2017 - Invoked login with username=ss AND password=ss +LOGGER (Server): Thu Jul 13 21:27:58 CEST 2017 - Login successfull +LOGGER (Server): Thu Jul 13 21:28:01 CEST 2017 - Invoked login with username=asd AND password=asd +LOGGER (Server): Thu Jul 13 21:28:01 CEST 2017 - Login successfull +LOGGER (Server): Thu Jul 13 21:28:08 CEST 2017 - Invoked login with username=ff AND password=ff +LOGGER (Server): Thu Jul 13 21:28:08 CEST 2017 - Login successfull From 71ff3c482038acf3e684bf261f74aa0254ff8741 Mon Sep 17 00:00:00 2001 From: Lorenzo Iovino Date: Thu, 13 Jul 2017 23:30:51 +0200 Subject: [PATCH 04/13] Strano ma funziona --- .idea/workspace.xml | 406 +++++++++--------- Server/src/com/texttwist/server/Server.java | 7 + .../texttwist/server/models/Dictionary.java | 1 - .../com/texttwist/server/models/Match.java | 5 +- .../server/servers/ProxyDispatcher.java | 8 +- .../server/services/MessageService.java | 16 +- .../server/services/ReceiveWordsService.java | 63 +++ .../texttwist/server/tasks/ReceiveWords.java | 70 --- notificationServer.log | 20 + 9 files changed, 310 insertions(+), 286 deletions(-) create mode 100644 Server/src/com/texttwist/server/services/ReceiveWordsService.java delete mode 100644 Server/src/com/texttwist/server/tasks/ReceiveWords.java diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 0bf1fe1..2979491 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -7,14 +7,13 @@ - + + + + - - - - - + @@ -26,7 +25,7 @@ - + com.texttwist.client.* @@ -37,44 +36,25 @@ - - + + - - + + - - + + + - - + + - - - - - - - - - - - - - - - - - - - - - - + + @@ -83,31 +63,79 @@ - - + + - - + + - - + + - - + + - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -123,19 +151,6 @@ - s - send invit - sendi - sending i - sending invi - wo - words - words r - wor - words re - y - ti - tim timeoy timeo timeou @@ -152,7 +167,20 @@ inv invio h invio + multic + datagramChannel invio g + C + Config + Config. + Config.W + Config.Wor + Config.Word + Config.Words + Config.WordsR + Config.WordsRe + Config.WordsRec + Config.WordsRece C:\Users\loke\TextTwist\out\production @@ -291,8 +319,6 @@ @@ -1290,47 +1318,47 @@ - + - - + + + + + + + + + + + - - - - + + - - - - - - - - @@ -1350,14 +1378,6 @@ - - - - - - - - @@ -1551,27 +1571,6 @@ - - - - - - - - - - - - - - - - - - - - - @@ -1588,46 +1587,6 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -1644,14 +1603,6 @@ - - - - - - - - @@ -1704,14 +1655,6 @@ - - - - - - - - @@ -1720,14 +1663,6 @@ - - - - - - - - @@ -1744,39 +1679,122 @@ - + - - + + - - + - + - - - + + + + + - - + + - + - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - + + + diff --git a/Server/src/com/texttwist/server/Server.java b/Server/src/com/texttwist/server/Server.java index 7955285..0dd6373 100644 --- a/Server/src/com/texttwist/server/Server.java +++ b/Server/src/com/texttwist/server/Server.java @@ -3,6 +3,7 @@ package com.texttwist.server; import com.texttwist.server.services.AuthService; import com.texttwist.server.services.MessageService; import com.texttwist.server.services.NotificationService; +import com.texttwist.server.services.ReceiveWordsService; import constants.Config; import interfaces.INotificationServer; import redis.clients.jedis.JedisPool; @@ -33,6 +34,7 @@ public class Server { startAuthService(); startJedisService(); startMessageService(); + startWordsReceiverService(); startNotificationService(); Server.logger.write("Services started correctly ..."); } @@ -60,6 +62,11 @@ public class Server { new Thread(new MessageService(Config.GameServerPort)).start(); } + private void startWordsReceiverService(){ + //Starting the Receive Words service based on UDP + new Thread(new ReceiveWordsService()).start(); + } + private void startNotificationService(){ //Starting Notification service based on RMI try { diff --git a/Server/src/com/texttwist/server/models/Dictionary.java b/Server/src/com/texttwist/server/models/Dictionary.java index 5d0a452..1977db4 100644 --- a/Server/src/com/texttwist/server/models/Dictionary.java +++ b/Server/src/com/texttwist/server/models/Dictionary.java @@ -1,7 +1,6 @@ package com.texttwist.server.models; import com.texttwist.server.Server; - import javax.swing.*; import java.io.*; import java.util.Random; diff --git a/Server/src/com/texttwist/server/models/Match.java b/Server/src/com/texttwist/server/models/Match.java index 11d077e..45edfb0 100644 --- a/Server/src/com/texttwist/server/models/Match.java +++ b/Server/src/com/texttwist/server/models/Match.java @@ -109,7 +109,6 @@ public class Match { } public void setScore(String player, Integer score){ - Match m = findMatchByPlayerName(player); if(m!=null) { for (int i = 0; i < m.playersScore.size(); i++) { @@ -122,7 +121,6 @@ public class Match { public Boolean allPlayersSendedHisScore(){ for (Pair player : playersScore) { - System.out.println(player.getValue()); if (player.getValue() == -1) { return false; } @@ -131,7 +129,6 @@ public class Match { } public void setUndefinedScorePlayersToZero(){ - for (int i = 0; i < playersScore.size(); i++) { if (playersScore.get(i).getValue() == -1) { playersScore.set(i, new Pair<>(playersScore.get(i).getKey(), 0)); @@ -148,7 +145,7 @@ public class Match { } private int generateMulticastId(){ - return MessageService.multicastID++; + return MessageService.multicastId++; } public void setLetters(DefaultListModel letters){ diff --git a/Server/src/com/texttwist/server/servers/ProxyDispatcher.java b/Server/src/com/texttwist/server/servers/ProxyDispatcher.java index edc2122..b823a7d 100644 --- a/Server/src/com/texttwist/server/servers/ProxyDispatcher.java +++ b/Server/src/com/texttwist/server/servers/ProxyDispatcher.java @@ -1,12 +1,9 @@ package com.texttwist.server.servers; -import com.sun.org.apache.xpath.internal.operations.Bool; import com.texttwist.server.services.SessionsService; import com.texttwist.server.models.Match; import com.texttwist.server.tasks.*; -import javafx.util.Pair; import models.Message; - import javax.swing.*; import java.io.IOException; import java.nio.ByteBuffer; @@ -17,8 +14,8 @@ import static com.texttwist.server.services.MessageService.activeMatches; /** * Author: Lorenzo Iovino on 18/06/2017. - * Description: Jedis Service - */ + * Description: Proxy Dispatcher + * */ public class ProxyDispatcher implements Callable { protected final ExecutorService threadPool = Executors.newCachedThreadPool(); private final Message request; @@ -128,7 +125,6 @@ public class ProxyDispatcher implements Callable { bufferMessage = ByteBuffer.wrap(byteMessage); try { String s = new String(bufferMessage.array(), bufferMessage.position(), bufferMessage.remaining()); - System.out.println("INVIO HIGHSCORES "+ s); socketChannel.write(bufferMessage); } catch (IOException e) { e.printStackTrace(); diff --git a/Server/src/com/texttwist/server/services/MessageService.java b/Server/src/com/texttwist/server/services/MessageService.java index 2c42788..a09f033 100644 --- a/Server/src/com/texttwist/server/services/MessageService.java +++ b/Server/src/com/texttwist/server/services/MessageService.java @@ -4,7 +4,6 @@ import com.texttwist.server.Server; import com.texttwist.server.servers.ProxyDispatcher; import com.texttwist.server.models.Dictionary; import com.texttwist.server.models.Match; -import com.texttwist.server.tasks.ReceiveWords; import constants.Config; import models.Message; import java.net.*; @@ -30,7 +29,7 @@ public class MessageService implements Runnable{ private int serverPort; private ProxyDispatcher proxy; - private ReceiveWords wordsReceiver; + private ReceiveWordsService wordsReceiver; private DatagramChannel datagramChannel; private Selector selector = null; @@ -44,7 +43,7 @@ public class MessageService implements Runnable{ public static List activeMatches = Collections.synchronizedList(new ArrayList<>()); - public static Integer multicastID = 4000; + public static Integer multicastId = 4000; public MessageService(int port){ this.serverPort = port; @@ -60,14 +59,11 @@ public class MessageService implements Runnable{ serverSocketChannel.configureBlocking(false); serverSocketChannel.socket().bind(new InetSocketAddress(serverPort)); serverSocketChannel.register(selector, OP_ACCEPT); - InetSocketAddress address = new InetSocketAddress(Config.WordsReceiverServerURI,Config.WordsReceiverServerPort); - datagramChannel = DatagramChannel.open(); - datagramChannel.configureBlocking(true); - datagramChannel.connect(address); + + + Server.logger.write("GameService Service is running at "+this.serverPort+" port..."); - wordsReceiver = new ReceiveWords(datagramChannel, bufferWords, bufferMessages, client); - threadPool.submit(wordsReceiver); } catch (IOException e) { e.printStackTrace(); @@ -102,7 +98,6 @@ public class MessageService implements Runnable{ if (client.read(bufferMessages) != -1) { bufferMessages.flip(); String line = new String(bufferMessages.array(), bufferMessages.position(), bufferMessages.remaining()); - System.out.println(line); if (line.startsWith("MESSAGE")) { SessionsService.getInstance().printAll(); Message msg = Message.toMessage(line); @@ -124,7 +119,6 @@ public class MessageService implements Runnable{ key.cancel(); } break; - default: break; } diff --git a/Server/src/com/texttwist/server/services/ReceiveWordsService.java b/Server/src/com/texttwist/server/services/ReceiveWordsService.java new file mode 100644 index 0000000..8078794 --- /dev/null +++ b/Server/src/com/texttwist/server/services/ReceiveWordsService.java @@ -0,0 +1,63 @@ +package com.texttwist.server.services; + +import com.texttwist.server.models.Match; +import com.texttwist.server.tasks.ComputeScore; +import com.texttwist.server.tasks.TokenInvalid; +import constants.Config; +import models.Message; + +import java.io.IOException; +import java.net.*; +import java.nio.ByteBuffer; +import java.nio.channels.DatagramChannel; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; + +/** + * Author: Lorenzo Iovino on 27/06/2017. + * Description: Jedis Service + */ +public class ReceiveWordsService implements Runnable { + + private ExecutorService threadPool = Executors.newCachedThreadPool(); + + public ReceiveWordsService() { + + } + + @Override + public void run(){ + + + Message msg; + DatagramSocket s = null; + + try { + s = new DatagramSocket(Config.WordsReceiverServerPort); + } catch (SocketException e) { + e.printStackTrace(); + } + DatagramPacket packet; + + while(true) { + + byte[] buf = new byte[1024]; + + packet = new DatagramPacket(buf, buf.length); + try { + s.receive(packet); + } catch (IOException e) { + e.printStackTrace(); + } + + String rcv = new String(packet.getData()); + if (rcv.startsWith("MESSAGE")) { + msg = Message.toMessage(rcv); + if(SessionsService.getInstance().isValidToken(msg.token)) { + Match match = Match.findMatchByPlayerName(msg.sender); + threadPool.submit(new ComputeScore(msg.sender, msg.data, match)); + } + } + } + } +} diff --git a/Server/src/com/texttwist/server/tasks/ReceiveWords.java b/Server/src/com/texttwist/server/tasks/ReceiveWords.java deleted file mode 100644 index d6877fb..0000000 --- a/Server/src/com/texttwist/server/tasks/ReceiveWords.java +++ /dev/null @@ -1,70 +0,0 @@ -package com.texttwist.server.tasks; - -import com.texttwist.server.services.SessionsService; -import com.texttwist.server.models.Match; -import constants.Config; -import javafx.util.Pair; -import models.Message; - -import java.net.*; -import java.nio.ByteBuffer; -import java.nio.channels.DatagramChannel; -import java.nio.channels.SocketChannel; -import java.util.concurrent.Callable; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; - -/** - * Author: Lorenzo Iovino on 27/06/2017. - * Description: Jedis Service - */ -public class ReceiveWords implements Callable{ - - protected ExecutorService threadPool = Executors.newCachedThreadPool(); - - public DatagramChannel channel; - ByteBuffer bufferWords; - ByteBuffer bufferMessages; - public SocketChannel socketChannel; - - - public ReceiveWords(DatagramChannel channel, ByteBuffer buffer, ByteBuffer bufferMessages, SocketChannel socketChannel) { - this.bufferWords = buffer; - this.channel = channel; - this.bufferMessages = bufferMessages; - this.socketChannel = socketChannel; - } - - @Override - public Boolean call() throws Exception { - - - Message msg; - DatagramSocket s = new DatagramSocket(Config.WordsReceiverServerPort); - DatagramPacket packet; - - while(true) { - - byte[] buf = new byte[1024]; - System.out.println("RECEIVIN WORDS"); - - packet = new DatagramPacket(buf, buf.length); - s.receive(packet); - - System.out.println("WORDS RECEIVED"); - - String rcv = new String(packet.getData()); - System.out.println(rcv); - if (rcv.startsWith("MESSAGE")) { - msg = Message.toMessage(rcv); - if(SessionsService.getInstance().isValidToken(msg.token)) { - System.out.println(msg.sender); - Match match = Match.findMatchByPlayerName(msg.sender); - threadPool.submit(new ComputeScore(msg.sender, msg.data, match)); - } else { - threadPool.submit(new TokenInvalid(msg.sender, socketChannel, bufferMessages)); - } - } - } - } -} diff --git a/notificationServer.log b/notificationServer.log index d735343..b067fe7 100644 --- a/notificationServer.log +++ b/notificationServer.log @@ -1741,3 +1741,23 @@ LOGGER (Server): Thu Jul 13 21:28:01 CEST 2017 - Invoked login with username=asd LOGGER (Server): Thu Jul 13 21:28:01 CEST 2017 - Login successfull LOGGER (Server): Thu Jul 13 21:28:08 CEST 2017 - Invoked login with username=ff AND password=ff LOGGER (Server): Thu Jul 13 21:28:08 CEST 2017 - Login successfull +LOGGER (Server): Thu Jul 13 23:25:44 CEST 2017 - Services starting ... +LOGGER (Server): Thu Jul 13 23:25:44 CEST 2017 - AuthService Service running at 9999 port... +LOGGER (Server): Thu Jul 13 23:25:44 CEST 2017 - GameService Service is running at 10000 port... +LOGGER (Server): Thu Jul 13 23:25:44 CEST 2017 - Services started correctly ... +LOGGER (Server): Thu Jul 13 23:25:50 CEST 2017 - Invoked login with username=a AND password=a +LOGGER (Server): Thu Jul 13 23:25:50 CEST 2017 - Login successfull +LOGGER (Server): Thu Jul 13 23:25:55 CEST 2017 - Invoked login with username=b AND password=b +LOGGER (Server): Thu Jul 13 23:25:55 CEST 2017 - Login successfull +LOGGER (Server): Thu Jul 13 23:26:55 CEST 2017 - Invoked login with username=ss AND password=ss +LOGGER (Server): Thu Jul 13 23:26:55 CEST 2017 - Login successfull +LOGGER (Server): Thu Jul 13 23:26:55 CEST 2017 - Invoked login with username=asd AND password=asd +LOGGER (Server): Thu Jul 13 23:26:55 CEST 2017 - Login successfull +LOGGER (Server): Thu Jul 13 23:29:48 CEST 2017 - Services starting ... +LOGGER (Server): Thu Jul 13 23:29:48 CEST 2017 - AuthService Service running at 9999 port... +LOGGER (Server): Thu Jul 13 23:29:48 CEST 2017 - GameService Service is running at 10000 port... +LOGGER (Server): Thu Jul 13 23:29:48 CEST 2017 - Services started correctly ... +LOGGER (Server): Thu Jul 13 23:29:53 CEST 2017 - Invoked login with username=a AND password=a +LOGGER (Server): Thu Jul 13 23:29:53 CEST 2017 - Login successfull +LOGGER (Server): Thu Jul 13 23:29:57 CEST 2017 - Invoked login with username=b AND password=b +LOGGER (Server): Thu Jul 13 23:29:57 CEST 2017 - Login successfull From e1622dfaa1960b838b0b54feacd2086686d13799 Mon Sep 17 00:00:00 2001 From: Lorenzo Iovino Date: Fri, 14 Jul 2017 00:06:59 +0200 Subject: [PATCH 05/13] UP --- .idea/workspace.xml | 705 ++++++++---------- .../client/controllers/HomeController.java | 5 + .../client/services/AuthService.java | 5 - .../com/texttwist/client/tasks/SendWords.java | 1 - .../client/tasks/WaitForPlayers.java | 2 - Server/src/com/texttwist/server/Server.java | 2 +- .../com/texttwist/server/models/Match.java | 14 +- .../MessageDispatcher.java} | 44 +- .../server/services/JedisService.java | 7 - .../server/services/MessageService.java | 52 +- .../server/services/NotificationService.java | 13 +- .../server/services/SessionsService.java | 6 - .../texttwist/server/tasks/ComputeScore.java | 20 +- .../com/texttwist/server/tasks/JoinMatch.java | 16 +- .../server/tasks/SendInvitations.java | 3 - .../texttwist/server/tasks/SendScores.java | 6 +- notificationServer.log | 123 +++ 17 files changed, 463 insertions(+), 561 deletions(-) rename Server/src/com/texttwist/server/{servers/ProxyDispatcher.java => proxies/MessageDispatcher.java} (86%) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 2979491..db72c2d 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -7,13 +7,22 @@ - + + + + + - - + + + + + + + @@ -36,55 +45,11 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + @@ -93,19 +58,21 @@ - - + + - - + + - - - + + + + + @@ -113,33 +80,67 @@ - - + + - - + + - - + + - - + + - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -151,20 +152,6 @@ - timeoy - timeo - timeou - timeout - timeout f - timeout fi - timeout fini - timeout finit - i - inio g - inio - inio - in - inv invio h invio multic @@ -181,6 +168,20 @@ Config.WordsRe Config.WordsRec Config.WordsRece + remo + login unsu + s + client + add + print + printall + Syst + System + System. + System.o + System.ou + thread + System.out C:\Users\loke\TextTwist\out\production @@ -319,12 +320,6 @@ @@ -424,6 +425,7 @@ + @@ -639,7 +641,7 @@ @@ -669,125 +671,8 @@ - @@ -1318,47 +1203,48 @@ - + + - - - - - - - - - - - - + + + - - + + - + + + + + + + + + @@ -1378,61 +1264,6 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -1440,14 +1271,6 @@ - - - - - - - - @@ -1559,26 +1382,16 @@ - - - - - - - - - - - - - - - - - + + + + + + + @@ -1595,14 +1408,6 @@ - - - - - - - - @@ -1611,16 +1416,6 @@ - - - - - - - - - - @@ -1639,10 +1434,70 @@ - + - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -1655,43 +1510,85 @@ - + - - + + - + - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - + + + + + - + - - + + - + - + @@ -1701,100 +1598,96 @@ - - - + + + + + - + - - - + + + + + - + - - + + - - + + - - + + - + - - + + - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - diff --git a/Client/src/com/texttwist/client/controllers/HomeController.java b/Client/src/com/texttwist/client/controllers/HomeController.java index 2197bbd..30faf9c 100644 --- a/Client/src/com/texttwist/client/controllers/HomeController.java +++ b/Client/src/com/texttwist/client/controllers/HomeController.java @@ -18,6 +18,11 @@ public class HomeController { public Response login(String userName, String password) throws RemoteException, NotBoundException, MalformedURLException { Response res = authService.login(userName,password); if (res.code == 200){ + try { + App.registerForNotifications(); + } catch (RemoteException e) { + App.logger.write("AUTH SERVICE: Can't register for notification"); + } App.session = (new Session(new User(userName,password,0), res.data.get("token").toString())); } return res; diff --git a/Client/src/com/texttwist/client/services/AuthService.java b/Client/src/com/texttwist/client/services/AuthService.java index 7944483..5936dcf 100644 --- a/Client/src/com/texttwist/client/services/AuthService.java +++ b/Client/src/com/texttwist/client/services/AuthService.java @@ -20,11 +20,6 @@ public class AuthService { private String baseUrl = Config.getAuthServerURI().concat("/auth"); public Response login(String userName, String password) throws RemoteException, NotBoundException, MalformedURLException { - try { - App.registerForNotifications(); - } catch (RemoteException e) { - App.logger.write("AUTH SERVICE: Can't register for notification"); - } IAuth auth = (IAuth) Naming.lookup(baseUrl); return auth.login(userName, password); } diff --git a/Client/src/com/texttwist/client/tasks/SendWords.java b/Client/src/com/texttwist/client/tasks/SendWords.java index 4b0a74d..cb4c1c5 100644 --- a/Client/src/com/texttwist/client/tasks/SendWords.java +++ b/Client/src/com/texttwist/client/tasks/SendWords.java @@ -36,7 +36,6 @@ public class SendWords extends SwingWorker { buffer.flip(); App.clientUDP.send(buffer, App.clientUDPSocketAddress); - System.out.println("SENDED" + sentence); } catch (UnknownHostException e) { App.logger.write("SEND WORDS: Host address not correct"); } catch (IOException e) { diff --git a/Client/src/com/texttwist/client/tasks/WaitForPlayers.java b/Client/src/com/texttwist/client/tasks/WaitForPlayers.java index e0dfabb..cdc096a 100644 --- a/Client/src/com/texttwist/client/tasks/WaitForPlayers.java +++ b/Client/src/com/texttwist/client/tasks/WaitForPlayers.java @@ -35,8 +35,6 @@ public class WaitForPlayers extends SwingWorker { while (App.clientTCP.read(buffer) != -1) { String line = new String(buffer.array(), buffer.position(), buffer.remaining()); buffer.clear(); - System.out.println("ASPETTO GAM_STARTED"); - System.out.println(line); if (line.startsWith("MESSAGE")) { buffer.clear(); diff --git a/Server/src/com/texttwist/server/Server.java b/Server/src/com/texttwist/server/Server.java index 0dd6373..f57822b 100644 --- a/Server/src/com/texttwist/server/Server.java +++ b/Server/src/com/texttwist/server/Server.java @@ -59,7 +59,7 @@ public class Server { private void startMessageService(){ //Starting the Message service based on TCP - new Thread(new MessageService(Config.GameServerPort)).start(); + new Thread(new MessageService()).start(); } private void startWordsReceiverService(){ diff --git a/Server/src/com/texttwist/server/models/Match.java b/Server/src/com/texttwist/server/models/Match.java index 45edfb0..7592a42 100644 --- a/Server/src/com/texttwist/server/models/Match.java +++ b/Server/src/com/texttwist/server/models/Match.java @@ -10,8 +10,6 @@ import java.util.*; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; -import static com.texttwist.server.services.MessageService.activeMatches; - /** * Author: Lorenzo Iovino on 23/06/2017. @@ -32,6 +30,9 @@ public class Match { //Players score: A list of pair where elements are . public final List> playersScore = Collections.synchronizedList(new ArrayList<>()); + //Players score: A list of active matches. + public static List activeMatches = Collections.synchronizedList(new ArrayList<>()); + /****SINGLE INSTANCE OF MATCH****/ //If match is started @@ -50,7 +51,7 @@ public class Match { //Letters of the match public DefaultListModel letters; - protected ExecutorService matchTimeoutThread = Executors.newSingleThreadExecutor(); + private ExecutorService matchTimeoutThread = Executors.newSingleThreadExecutor(); public Match(String matchCreator, DefaultListModel players){ for (int i =0; i < players.size(); i++){ @@ -61,7 +62,6 @@ public class Match { this.multicastId = this.generateMulticastId(); this.matchCreator = matchCreator; - } public static Match findMatch(List matches, String matchName){ @@ -73,12 +73,6 @@ public class Match { return null; } - public void printAll(){ - for (Pair aPlayersScore : playersScore) { - System.out.println(aPlayersScore.getKey() + " : " + aPlayersScore.getValue()); - } - } - public static int findMatchIndex(List matches, String matchName){ for (int i = 0; i < matches.size(); i++) { if (matches.get(i).matchCreator.equals(matchName)) { diff --git a/Server/src/com/texttwist/server/servers/ProxyDispatcher.java b/Server/src/com/texttwist/server/proxies/MessageDispatcher.java similarity index 86% rename from Server/src/com/texttwist/server/servers/ProxyDispatcher.java rename to Server/src/com/texttwist/server/proxies/MessageDispatcher.java index b823a7d..2f0c217 100644 --- a/Server/src/com/texttwist/server/servers/ProxyDispatcher.java +++ b/Server/src/com/texttwist/server/proxies/MessageDispatcher.java @@ -1,4 +1,4 @@ -package com.texttwist.server.servers; +package com.texttwist.server.proxies; import com.texttwist.server.services.SessionsService; import com.texttwist.server.models.Match; @@ -10,25 +10,22 @@ import java.nio.ByteBuffer; import java.nio.channels.SocketChannel; import java.util.concurrent.*; -import static com.texttwist.server.services.MessageService.activeMatches; /** * Author: Lorenzo Iovino on 18/06/2017. * Description: Proxy Dispatcher * */ -public class ProxyDispatcher implements Callable { - protected final ExecutorService threadPool = Executors.newCachedThreadPool(); +public class MessageDispatcher implements Callable { + private final ExecutorService threadPool = Executors.newCachedThreadPool(); private final Message request; private final SocketChannel socketChannel; private ByteBuffer bufferMessage; - boolean matchNotAvailable =false; + private boolean matchNotAvailable = false; - - public ProxyDispatcher(Message request, SocketChannel socketChannel, ByteBuffer bufferMessage) { + public MessageDispatcher(Message request, SocketChannel socketChannel, ByteBuffer bufferMessage) { this.request = request; this.socketChannel = socketChannel; this.bufferMessage = bufferMessage; - } @Override @@ -47,12 +44,10 @@ public class ProxyDispatcher implements Callable { Boolean invitationSended = sendInvitations.get(); if (invitationSended) { - //Crea nuova partita e attendi i giocatori + //Create new match and wait users joins request.data.addElement(request.sender); final Match match = new Match(request.sender, request.data); - match.printAll(); - - activeMatches.add(match); + Match.activeMatches.add(match); DefaultListModel matchName = new DefaultListModel<>(); matchName.addElement(request.sender); @@ -62,28 +57,24 @@ public class ProxyDispatcher implements Callable { if(!joinMatchRes){ bufferMessage = ByteBuffer.allocate(1024); - - //NON FARE NULLA, ASPETTA GLI ALTRI Message message = new Message("INVITES_ALL_SENDED", "", "", new DefaultListModel<>()); byteMessage = message.toString().getBytes(); bufferMessage = ByteBuffer.wrap(byteMessage); socketChannel.write(bufferMessage); } - Future joinTimeout = threadPool.submit(new JoinTimeout(match)); Boolean joinTimeoutRes = joinTimeout.get(); if(joinTimeoutRes){ Future sendMessageJoinTimeout = threadPool.submit( - new SendMessageToAllPlayers(match, new Message("JOIN_TIMEOUT", "", "", new DefaultListModel<>()), socketChannel)); - + new SendMessageToAllPlayers(match, + new Message("JOIN_TIMEOUT", "", "", new DefaultListModel<>()), socketChannel)); Boolean sendMessageJoinTimeoutRes = sendMessageJoinTimeout.get(); if(!sendMessageJoinTimeoutRes){ - activeMatches.remove(Match.findMatchIndex(activeMatches, match.matchCreator)); + Match.activeMatches.remove(Match.findMatchIndex(Match.activeMatches, match.matchCreator)); return sendMessageJoinTimeoutRes; } } else { - System.out.println("TIMEOUT FINITO SENZA EFFETTI"); return true; } @@ -124,7 +115,6 @@ public class ProxyDispatcher implements Callable { bufferMessage = ByteBuffer.wrap(byteMessage); try { - String s = new String(bufferMessage.array(), bufferMessage.position(), bufferMessage.remaining()); socketChannel.write(bufferMessage); } catch (IOException e) { e.printStackTrace(); @@ -139,7 +129,7 @@ public class ProxyDispatcher implements Callable { case "JOIN_GAME": Future joinMatch = threadPool.submit(new JoinMatch(request.sender, request.data, socketChannel)); try { - Match match = Match.findMatch(activeMatches, request.data.get(0));; + Match match = Match.findMatch(Match.activeMatches, request.data.get(0));; Boolean joinMatchRes = joinMatch.get(); if(joinMatchRes){ @@ -153,23 +143,17 @@ public class ProxyDispatcher implements Callable { if (socketClient != null) { bufferMessage.clear(); bufferMessage = ByteBuffer.allocate(1024); - Message message = new Message("GAME_STARTED", "", "", match.letters); match.startGame(); - - System.out.println("TIMEOUT CANCELLEd"); byteMessage = message.toString().getBytes(); - bufferMessage = ByteBuffer.wrap(byteMessage); try { String s = new String(bufferMessage.array(), bufferMessage.position(), bufferMessage.remaining()); - System.out.println("INVIO GAME_STARTED "+ s); socketClient.write(bufferMessage); } catch (IOException e) { e.printStackTrace(); } } - } if (matchNotAvailable) { return false; @@ -180,7 +164,6 @@ public class ProxyDispatcher implements Callable { bufferMessage = ByteBuffer.allocate(1024); if (socketChannel != null) { bufferMessage = ByteBuffer.allocate(1024); - Message msg = new Message("MATCH_NOT_AVAILABLE", "", null, new DefaultListModel<>()); bufferMessage.clear(); byteMessage = msg.toString().getBytes(); @@ -188,7 +171,6 @@ public class ProxyDispatcher implements Callable { socketChannel.write(bufferMessage); matchNotAvailable = true; } - } } } catch (InterruptedException e) { @@ -198,17 +180,13 @@ public class ProxyDispatcher implements Callable { } catch (IOException e) { e.printStackTrace(); } - default: - break; } - } else { threadPool.submit(new TokenInvalid(request.sender, socketChannel, bufferMessage)); return false; } - return false; } } diff --git a/Server/src/com/texttwist/server/services/JedisService.java b/Server/src/com/texttwist/server/services/JedisService.java index 6d796cd..78f519f 100644 --- a/Server/src/com/texttwist/server/services/JedisService.java +++ b/Server/src/com/texttwist/server/services/JedisService.java @@ -16,10 +16,6 @@ import static com.texttwist.server.Server.jedisPool; */ public class JedisService { - public JedisService(){ - - } - /** Read the object from Base64 string. */ public static Object fromString(String s) throws IOException, ClassNotFoundException { byte [] data = Base64.getDecoder().decode(s); @@ -57,11 +53,8 @@ public class JedisService { Jedis jedis = null; List l = new ArrayList<>(); try { - System.out.println("USER ss"); - jedis = jedisPool.getResource(); String usersString = jedis.get(key); - System.out.println("USER "+usersString); if(usersString!=null) { String[] lines = usersString.split("\n"); for (int i = 0; i < lines.length; i++) { diff --git a/Server/src/com/texttwist/server/services/MessageService.java b/Server/src/com/texttwist/server/services/MessageService.java index a09f033..2d94cd1 100644 --- a/Server/src/com/texttwist/server/services/MessageService.java +++ b/Server/src/com/texttwist/server/services/MessageService.java @@ -1,76 +1,53 @@ - package com.texttwist.server.services; + import com.texttwist.server.Server; -import com.texttwist.server.servers.ProxyDispatcher; +import com.texttwist.server.proxies.MessageDispatcher; import com.texttwist.server.models.Dictionary; -import com.texttwist.server.models.Match; import constants.Config; import models.Message; import java.net.*; import java.io.IOException; import java.nio.ByteBuffer; import java.nio.channels.*; -import java.util.ArrayList; -import java.util.Collections; import java.util.Iterator; -import java.util.List; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; - import static java.nio.channels.SelectionKey.OP_ACCEPT; import static java.nio.channels.SelectionKey.OP_READ; /** * Author: Lorenzo Iovino on 17/06/2017. - * Description: Game Server + * Description: Message Service */ public class MessageService implements Runnable{ - private int serverPort; - private ProxyDispatcher proxy; - private ReceiveWordsService wordsReceiver; - - private DatagramChannel datagramChannel; private Selector selector = null; - private ExecutorService threadPool = Executors.newCachedThreadPool(); + private ExecutorService dispatcherPool = Executors.newCachedThreadPool(); private String dictionaryPath = "./Server/resources/dictionary"; public static Dictionary dict; - SocketChannel client; - ByteBuffer bufferWords = ByteBuffer.allocate(1024); - ByteBuffer bufferMessages = ByteBuffer.allocate(1024); - - - public static List activeMatches = Collections.synchronizedList(new ArrayList<>()); public static Integer multicastId = 4000; - public MessageService(int port){ - this.serverPort = port; - } - - public void run(){ - + public MessageService() + { dict = new Dictionary(dictionaryPath); try { selector = Selector.open(); ServerSocketChannel serverSocketChannel = ServerSocketChannel.open(); serverSocketChannel.configureBlocking(false); - serverSocketChannel.socket().bind(new InetSocketAddress(serverPort)); + serverSocketChannel.socket().bind(new InetSocketAddress(Config.GameServerPort)); serverSocketChannel.register(selector, OP_ACCEPT); - - - - Server.logger.write("GameService Service is running at "+this.serverPort+" port..."); - + Server.logger.write("GameService Service is running at "+Config.GameServerPort+" port..."); } catch (IOException e) { e.printStackTrace(); } + } + public void run(){ while (true) { - System.out.println("WAITING FOR MSG"); try { selector.select(); } catch (IOException e) { @@ -79,9 +56,9 @@ public class MessageService implements Runnable{ Iterator iter = selector.selectedKeys().iterator(); while (iter.hasNext()) { - bufferMessages = ByteBuffer.allocate(1024); + ByteBuffer bufferMessages = ByteBuffer.allocate(1024); bufferMessages.clear(); - client = null; + SocketChannel client = null; SelectionKey key = iter.next(); iter.remove(); @@ -99,10 +76,9 @@ public class MessageService implements Runnable{ bufferMessages.flip(); String line = new String(bufferMessages.array(), bufferMessages.position(), bufferMessages.remaining()); if (line.startsWith("MESSAGE")) { - SessionsService.getInstance().printAll(); Message msg = Message.toMessage(line); - proxy = new ProxyDispatcher(msg, client, bufferMessages); - threadPool.submit(proxy); + MessageDispatcher proxy = new MessageDispatcher(msg, client, bufferMessages); + dispatcherPool.submit(proxy); } if (line.startsWith("CLOSE")) { diff --git a/Server/src/com/texttwist/server/services/NotificationService.java b/Server/src/com/texttwist/server/services/NotificationService.java index f630028..7c085e6 100644 --- a/Server/src/com/texttwist/server/services/NotificationService.java +++ b/Server/src/com/texttwist/server/services/NotificationService.java @@ -25,32 +25,21 @@ public class NotificationService implements INotificationServer { public synchronized void registerForCallback(INotificationClient clientInterface) throws RemoteException { if(!clients.contains(clientInterface)){ clients.add(clientInterface); - System.out.println(clientInterface); - System.out.println("New client registered"); } } public synchronized void unregisterForCallback(INotificationClient client) throws RemoteException { - if (clients.remove(client)) { - System.out.println("Client unregistered"); - } else { - System.out.println("Unable to unregister client"); - } - + clients.remove(client); } public synchronized void sendInvitations(String username, DefaultListModel users){ Iterator i = clients.iterator(); INotificationClient client = null; - System.out.println("Starting callbacks"); while (i.hasNext()) { client = (INotificationClient) i.next(); try { - - System.out.println("SENDING INVITE TO "+users); client.sendInvite(username, users); } catch (RemoteException e) { - System.out.println("Sembra down"); try { unregisterForCallback(client); } catch (RemoteException e1) { diff --git a/Server/src/com/texttwist/server/services/SessionsService.java b/Server/src/com/texttwist/server/services/SessionsService.java index 9489a99..2f80540 100644 --- a/Server/src/com/texttwist/server/services/SessionsService.java +++ b/Server/src/com/texttwist/server/services/SessionsService.java @@ -40,12 +40,6 @@ public class SessionsService { return sessions.add(new Session(new User(userName,"",0), token)); } - public void printAll(){ - for (Session elem : sessions) { - System.out.println(elem.account.userName + " | " + elem.token); - } - } - public boolean remove(String userName){ if(exists(userName)) { Session s = getSession(userName); diff --git a/Server/src/com/texttwist/server/tasks/ComputeScore.java b/Server/src/com/texttwist/server/tasks/ComputeScore.java index 4aa5453..97113c4 100644 --- a/Server/src/com/texttwist/server/tasks/ComputeScore.java +++ b/Server/src/com/texttwist/server/tasks/ComputeScore.java @@ -14,10 +14,10 @@ import java.util.concurrent.Callable; */ public class ComputeScore implements Callable { - public DefaultListModel words; - public final String sender; + private DefaultListModel words; + private final String sender; public Match match; - public DefaultListModel wordsValid; + private DefaultListModel wordsValid; public ComputeScore(String sender, DefaultListModel words, Match match){ this.words = words; @@ -33,31 +33,18 @@ public class ComputeScore implements Callable { for (int i = 0; i < words.size(); i++) { if (isValid(words.get(i), match.letters)) { score += words.get(i).length(); - System.out.println(words.get(i) + " is valid!" + " : " + score ); wordsValid.addElement(words.get(i)); } } - - - System.out.println(sender +" totalize SCORE = " + score); match.setScore(sender, score); - for (Pair player : match.playersScore) { - System.out.println(player.getValue()); - - } - System.out.println(score); User u = AccountsService.getInstance().findUser(sender); u.addScore(score); if(match.allPlayersSendedHisScore()) { - match.matchTimeout = false; - System.out.println("MATCH TIMEOUT CANCELLATO"); - match.setUndefinedScorePlayersToZero(); new SendScores(match).call(); - } return score; } @@ -74,7 +61,6 @@ public class ComputeScore implements Callable { if(word.equals("")){ return true; } - if(!isCharacterPresent || wordsValid.indexOf(word)!=-1){ return false; } diff --git a/Server/src/com/texttwist/server/tasks/JoinMatch.java b/Server/src/com/texttwist/server/tasks/JoinMatch.java index 78ad16c..d9c7b36 100644 --- a/Server/src/com/texttwist/server/tasks/JoinMatch.java +++ b/Server/src/com/texttwist/server/tasks/JoinMatch.java @@ -5,7 +5,6 @@ import javafx.util.Pair; import javax.swing.*; import java.nio.channels.SocketChannel; import java.util.concurrent.Callable; -import static com.texttwist.server.services.MessageService.activeMatches; /** * Author: Lorenzo Iovino on 23/06/2017. @@ -24,7 +23,7 @@ public class JoinMatch implements Callable { @Override public Boolean call() throws Exception { - final Match thisMatch = Match.findMatch(activeMatches, this.matchName); + final Match thisMatch = Match.findMatch(Match.activeMatches, this.matchName); if (thisMatch != null) { for (int j = 0; j < thisMatch.playersStatus.size(); j++) { String name = thisMatch.playersStatus.get(j).getKey(); @@ -40,25 +39,12 @@ public class JoinMatch implements Callable { return false; } - - private void printAll(Match match){ - for (int i = 0; i < match.playersStatus.size(); i++) { - - 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()); - - } - } - private Boolean allJoined(Match match) { for (int i = 0; i < match.playersStatus.size(); i++) { if (match.playersStatus.get(i).getValue() == 0) { return false; } } - match.printAll(); match.joinTimeout = false; return true; } diff --git a/Server/src/com/texttwist/server/tasks/SendInvitations.java b/Server/src/com/texttwist/server/tasks/SendInvitations.java index 7b05d97..768094e 100644 --- a/Server/src/com/texttwist/server/tasks/SendInvitations.java +++ b/Server/src/com/texttwist/server/tasks/SendInvitations.java @@ -21,12 +21,9 @@ public class SendInvitations implements Callable { @Override public Boolean call() throws Exception { try { - System.out.println("INVIA INVITO A" + users); - Server.notificationServer.sendInvitations(sender, users); } catch (Exception e) { - System.out.println("Eccezione" + e); e.printStackTrace(); } diff --git a/Server/src/com/texttwist/server/tasks/SendScores.java b/Server/src/com/texttwist/server/tasks/SendScores.java index 62ec42f..b5383ac 100644 --- a/Server/src/com/texttwist/server/tasks/SendScores.java +++ b/Server/src/com/texttwist/server/tasks/SendScores.java @@ -10,8 +10,6 @@ import java.net.InetAddress; import java.net.MulticastSocket; import java.util.concurrent.Callable; -import static com.texttwist.server.services.MessageService.activeMatches; - /** * Created by loke on 13/07/2017. */ @@ -27,9 +25,7 @@ public class SendScores implements Callable { public Void call() throws Exception { while (true) { - System.out.println("SENDING"); Message msg = new Message("FINALSCORE", "SERVER", "", match.getMatchPlayersScoreAsStringList()); - MulticastSocket multicastSocket = null; try { multicastSocket = new MulticastSocket(match.multicastId); @@ -39,7 +35,7 @@ public class SendScores implements Callable { } catch (IOException e) { e.printStackTrace(); } - activeMatches.remove(Match.findMatchIndex(activeMatches, match.matchCreator)); + Match.activeMatches.remove(Match.findMatchIndex(Match.activeMatches, match.matchCreator)); return null; } } diff --git a/notificationServer.log b/notificationServer.log index b067fe7..1892600 100644 --- a/notificationServer.log +++ b/notificationServer.log @@ -1761,3 +1761,126 @@ LOGGER (Server): Thu Jul 13 23:29:53 CEST 2017 - Invoked login with username=a A LOGGER (Server): Thu Jul 13 23:29:53 CEST 2017 - Login successfull LOGGER (Server): Thu Jul 13 23:29:57 CEST 2017 - Invoked login with username=b AND password=b LOGGER (Server): Thu Jul 13 23:29:57 CEST 2017 - Login successfull +LOGGER (Server): Thu Jul 13 23:32:24 CEST 2017 - Services starting ... +LOGGER (Server): Thu Jul 13 23:32:25 CEST 2017 - AuthService Service running at 9999 port... +LOGGER (Server): Thu Jul 13 23:32:25 CEST 2017 - GameService Service is running at 10000 port... +LOGGER (Server): Thu Jul 13 23:32:25 CEST 2017 - Services started correctly ... +LOGGER (Server): Thu Jul 13 23:32:37 CEST 2017 - Invoked login with username=a AND password=a +LOGGER (Server): Thu Jul 13 23:32:37 CEST 2017 - Login unsuccessfull +LOGGER (Server): Thu Jul 13 23:32:40 CEST 2017 - Invoked register with username=a AND password=a +LOGGER (Server): Thu Jul 13 23:32:40 CEST 2017 - Registration successfull +LOGGER (Server): Thu Jul 13 23:32:42 CEST 2017 - Invoked login with username=a AND password=a +LOGGER (Server): Thu Jul 13 23:32:42 CEST 2017 - Login successfull +LOGGER (Server): Thu Jul 13 23:32:49 CEST 2017 - Invoked register with username=b AND password=b +LOGGER (Server): Thu Jul 13 23:32:49 CEST 2017 - Registration successfull +LOGGER (Server): Thu Jul 13 23:32:51 CEST 2017 - Invoked login with username=b AND password=b +LOGGER (Server): Thu Jul 13 23:32:51 CEST 2017 - Login successfull +LOGGER (Server): Thu Jul 13 23:32:59 CEST 2017 - Invoked register with username=c AND password=c +LOGGER (Server): Thu Jul 13 23:32:59 CEST 2017 - Registration successfull +LOGGER (Server): Thu Jul 13 23:33:01 CEST 2017 - Invoked login with username=c AND password=c +LOGGER (Server): Thu Jul 13 23:33:01 CEST 2017 - Login successfull +LOGGER (Server): Thu Jul 13 23:33:05 CEST 2017 - Invoked register with username=d AND password=d +LOGGER (Server): Thu Jul 13 23:33:05 CEST 2017 - Registration successfull +LOGGER (Server): Thu Jul 13 23:33:07 CEST 2017 - Invoked login with username=d AND password=d +LOGGER (Server): Thu Jul 13 23:33:07 CEST 2017 - Login successfull +LOGGER (Server): Thu Jul 13 23:36:00 CEST 2017 - Invoked register with username=e AND password=e +LOGGER (Server): Thu Jul 13 23:36:00 CEST 2017 - Registration successfull +LOGGER (Server): Thu Jul 13 23:36:01 CEST 2017 - Invoked login with username=e AND password=e +LOGGER (Server): Thu Jul 13 23:36:01 CEST 2017 - Login successfull +LOGGER (Server): Thu Jul 13 23:36:06 CEST 2017 - Invoked register with username=f AND password=f +LOGGER (Server): Thu Jul 13 23:36:06 CEST 2017 - Registration successfull +LOGGER (Server): Thu Jul 13 23:36:07 CEST 2017 - Invoked login with username=f AND password=f +LOGGER (Server): Thu Jul 13 23:36:07 CEST 2017 - Login successfull +LOGGER (Server): Thu Jul 13 23:36:55 CEST 2017 - Invoked logout with username=e AND token=4fhehlcg3dhur4c4fm32gjeglm +LOGGER (Server): Thu Jul 13 23:36:55 CEST 2017 - Logout successfull +LOGGER (Server): Thu Jul 13 23:36:55 CEST 2017 - Logout successfull (but something gone wrong) +LOGGER (Server): Thu Jul 13 23:36:57 CEST 2017 - Invoked login with username=e AND password=dd +LOGGER (Server): Thu Jul 13 23:36:57 CEST 2017 - Login unsuccessfull +LOGGER (Server): Thu Jul 13 23:37:00 CEST 2017 - Invoked login with username=e AND password=e +LOGGER (Server): Thu Jul 13 23:37:00 CEST 2017 - Login successfull +LOGGER (Server): Thu Jul 13 23:37:09 CEST 2017 - Invoked logout with username=e AND token=o8eehhsq8og3ct9b6g5lo9k3v9 +LOGGER (Server): Thu Jul 13 23:37:09 CEST 2017 - Logout successfull +LOGGER (Server): Thu Jul 13 23:37:09 CEST 2017 - Logout successfull (but something gone wrong) +LOGGER (Server): Thu Jul 13 23:37:13 CEST 2017 - Invoked login with username=e AND password=fdf +LOGGER (Server): Thu Jul 13 23:37:13 CEST 2017 - Login unsuccessfull +LOGGER (Server): Thu Jul 13 23:37:20 CEST 2017 - Invoked login with username=e AND password=dd +LOGGER (Server): Thu Jul 13 23:37:20 CEST 2017 - Login unsuccessfull +LOGGER (Server): Thu Jul 13 23:37:22 CEST 2017 - Invoked login with username=e AND password=e +LOGGER (Server): Thu Jul 13 23:37:22 CEST 2017 - Login successfull +LOGGER (Server): Thu Jul 13 23:37:30 CEST 2017 - Invoked logout with username=e AND token=rjtq0cis3fqhdb2c5pipui8nmd +LOGGER (Server): Thu Jul 13 23:37:30 CEST 2017 - Logout successfull +LOGGER (Server): Thu Jul 13 23:37:30 CEST 2017 - Logout successfull (but something gone wrong) +LOGGER (Server): Thu Jul 13 23:37:33 CEST 2017 - Invoked login with username=e AND password=e +LOGGER (Server): Thu Jul 13 23:37:33 CEST 2017 - Login successfull +LOGGER (Server): Thu Jul 13 23:40:08 CEST 2017 - Services starting ... +LOGGER (Server): Thu Jul 13 23:40:08 CEST 2017 - AuthService Service running at 9999 port... +LOGGER (Server): Thu Jul 13 23:40:09 CEST 2017 - Services started correctly ... +LOGGER (Server): Thu Jul 13 23:40:09 CEST 2017 - GameService Service is running at 10000 port... +LOGGER (Server): Thu Jul 13 23:40:14 CEST 2017 - Invoked login with username=a AND password=a +LOGGER (Server): Thu Jul 13 23:40:14 CEST 2017 - Login successfull +LOGGER (Server): Thu Jul 13 23:40:19 CEST 2017 - Invoked login with username=b AND password=b +LOGGER (Server): Thu Jul 13 23:40:19 CEST 2017 - Login successfull +LOGGER (Server): Thu Jul 13 23:40:37 CEST 2017 - Invoked logout with username=b AND token=68b5nnp61fsopvg2s1826kismv +LOGGER (Server): Thu Jul 13 23:40:37 CEST 2017 - Logout successfull +LOGGER (Server): Thu Jul 13 23:40:37 CEST 2017 - Logout successfull (but something gone wrong) +LOGGER (Server): Thu Jul 13 23:40:39 CEST 2017 - Invoked login with username=b AND password=fd +LOGGER (Server): Thu Jul 13 23:40:39 CEST 2017 - Login unsuccessfull +LOGGER (Server): Thu Jul 13 23:40:41 CEST 2017 - Invoked login with username=b AND password=b +LOGGER (Server): Thu Jul 13 23:40:41 CEST 2017 - Login successfull +LOGGER (Server): Thu Jul 13 23:41:17 CEST 2017 - Services starting ... +LOGGER (Server): Thu Jul 13 23:41:17 CEST 2017 - AuthService Service running at 9999 port... +LOGGER (Server): Thu Jul 13 23:41:17 CEST 2017 - GameService Service is running at 10000 port... +LOGGER (Server): Thu Jul 13 23:41:17 CEST 2017 - Services started correctly ... +LOGGER (Server): Thu Jul 13 23:41:24 CEST 2017 - Invoked login with username=a AND password=a +LOGGER (Server): Thu Jul 13 23:41:24 CEST 2017 - Login successfull +LOGGER (Server): Thu Jul 13 23:41:27 CEST 2017 - Invoked login with username=b AND password=b +LOGGER (Server): Thu Jul 13 23:41:27 CEST 2017 - Login successfull +LOGGER (Server): Thu Jul 13 23:41:45 CEST 2017 - Invoked logout with username=b AND token=vs8ti17hhbdjfsg48elebpa3ld +LOGGER (Server): Thu Jul 13 23:41:45 CEST 2017 - Logout successfull +LOGGER (Server): Thu Jul 13 23:41:45 CEST 2017 - Logout successfull (but something gone wrong) +LOGGER (Server): Thu Jul 13 23:41:47 CEST 2017 - Invoked login with username=b AND password=ds +LOGGER (Server): Thu Jul 13 23:41:47 CEST 2017 - Login unsuccessfull +LOGGER (Server): Thu Jul 13 23:41:49 CEST 2017 - Invoked login with username=b AND password=b +LOGGER (Server): Thu Jul 13 23:41:49 CEST 2017 - Login successfull +LOGGER (Server): Thu Jul 13 23:43:16 CEST 2017 - Services starting ... +LOGGER (Server): Thu Jul 13 23:43:16 CEST 2017 - AuthService Service running at 9999 port... +LOGGER (Server): Thu Jul 13 23:43:16 CEST 2017 - Services started correctly ... +LOGGER (Server): Thu Jul 13 23:43:16 CEST 2017 - GameService Service is running at 10000 port... +LOGGER (Server): Thu Jul 13 23:43:20 CEST 2017 - Invoked login with username=a AND password=a +LOGGER (Server): Thu Jul 13 23:43:20 CEST 2017 - Login successfull +LOGGER (Server): Thu Jul 13 23:44:11 CEST 2017 - Invoked login with username=a AND password=a +LOGGER (Server): Thu Jul 13 23:44:11 CEST 2017 - Login successfull +LOGGER (Server): Thu Jul 13 23:44:22 CEST 2017 - Services starting ... +LOGGER (Server): Thu Jul 13 23:44:22 CEST 2017 - AuthService Service running at 9999 port... +LOGGER (Server): Thu Jul 13 23:44:22 CEST 2017 - Services started correctly ... +LOGGER (Server): Thu Jul 13 23:44:22 CEST 2017 - GameService Service is running at 10000 port... +LOGGER (Server): Thu Jul 13 23:44:29 CEST 2017 - Invoked login with username=a AND password=a +LOGGER (Server): Thu Jul 13 23:44:29 CEST 2017 - Login successfull +LOGGER (Server): Thu Jul 13 23:44:33 CEST 2017 - Invoked login with username=b AND password=b +LOGGER (Server): Thu Jul 13 23:44:33 CEST 2017 - Login successfull +LOGGER (Server): Thu Jul 13 23:59:14 CEST 2017 - Services starting ... +LOGGER (Server): Thu Jul 13 23:59:14 CEST 2017 - AuthService Service running at 9999 port... +LOGGER (Server): Thu Jul 13 23:59:14 CEST 2017 - GameService Service is running at 10000 port... +LOGGER (Server): Thu Jul 13 23:59:14 CEST 2017 - Services started correctly ... +LOGGER (Server): Thu Jul 13 23:59:20 CEST 2017 - Invoked login with username=a AND password=aa +LOGGER (Server): Thu Jul 13 23:59:20 CEST 2017 - Login unsuccessfull +LOGGER (Server): Thu Jul 13 23:59:21 CEST 2017 - Invoked login with username=a AND password=a +LOGGER (Server): Thu Jul 13 23:59:21 CEST 2017 - Login successfull +LOGGER (Server): Thu Jul 13 23:59:26 CEST 2017 - Invoked login with username=b AND password=b +LOGGER (Server): Thu Jul 13 23:59:26 CEST 2017 - Login successfull +LOGGER (Server): Fri Jul 14 00:00:11 CEST 2017 - Invoked login with username=c AND password=c +LOGGER (Server): Fri Jul 14 00:00:11 CEST 2017 - Login successfull +LOGGER (Server): Fri Jul 14 00:00:17 CEST 2017 - Invoked login with username=d AND password=d +LOGGER (Server): Fri Jul 14 00:00:17 CEST 2017 - Login successfull +LOGGER (Server): Fri Jul 14 00:04:50 CEST 2017 - Services starting ... +LOGGER (Server): Fri Jul 14 00:04:50 CEST 2017 - AuthService Service running at 9999 port... +LOGGER (Server): Fri Jul 14 00:04:50 CEST 2017 - GameService Service is running at 10000 port... +LOGGER (Server): Fri Jul 14 00:04:50 CEST 2017 - Services started correctly ... +LOGGER (Server): Fri Jul 14 00:04:57 CEST 2017 - Invoked login with username=a AND password=a +LOGGER (Server): Fri Jul 14 00:04:57 CEST 2017 - Login successfull +LOGGER (Server): Fri Jul 14 00:05:00 CEST 2017 - Invoked login with username=b AND password=b +LOGGER (Server): Fri Jul 14 00:05:00 CEST 2017 - Login successfull +LOGGER (Server): Fri Jul 14 00:05:27 CEST 2017 - Invoked login with username=c AND password=c +LOGGER (Server): Fri Jul 14 00:05:27 CEST 2017 - Login successfull +LOGGER (Server): Fri Jul 14 00:05:31 CEST 2017 - Invoked login with username=d AND password=d +LOGGER (Server): Fri Jul 14 00:05:31 CEST 2017 - Login successfull From ab307b0a9791ac083ac00986f4db03e3b3ade684 Mon Sep 17 00:00:00 2001 From: Lorenzo Iovino Date: Fri, 14 Jul 2017 00:46:37 +0200 Subject: [PATCH 06/13] UPPPP FINISH --- .idea/workspace.xml | 1083 ++++++++++------- .../client/services/GameService.java | 3 +- .../client/tasks/WaitForPlayers.java | 4 + Commons/src/constants/Config.java | 6 +- .../src/interfaces/INotificationServer.java | 2 +- Server/src/com/texttwist/server/Server.java | 4 +- .../server/proxies/MessageDispatcher.java | 2 +- .../server/services/AuthService.java | 8 +- .../server/services/JedisService.java | 6 + .../server/services/NotificationService.java | 5 +- .../server/services/ReceiveWordsService.java | 12 +- .../server/services/SessionsService.java | 2 - .../server/tasks/CheckOnlineUsers.java | 3 +- .../server/tasks/ComputeHighscores.java | 4 +- .../texttwist/server/tasks/ComputeScore.java | 39 +- .../server/tasks/GenerateLetters.java | 10 +- .../com/texttwist/server/tasks/JoinMatch.java | 30 +- .../texttwist/server/tasks/JoinTimeout.java | 10 +- .../texttwist/server/tasks/MatchTimeout.java | 3 +- .../server/tasks/SendInvitations.java | 5 +- .../server/tasks/SendMessageToAllPlayers.java | 11 +- .../texttwist/server/tasks/SendScores.java | 27 +- .../texttwist/server/tasks/TokenInvalid.java | 2 +- notificationServer.log | 102 ++ 24 files changed, 857 insertions(+), 526 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index db72c2d..1456423 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -7,22 +7,28 @@ - - - - - + + + - + + - + + + + + + + + @@ -45,65 +51,34 @@ - - + + - - + + - - + + - - + + - - + + - - + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + @@ -112,35 +87,76 @@ - - + + - - + + - - + + - - + + - - + + + + + + + + + + + + - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -152,36 +168,36 @@ - invio h - invio - multic - datagramChannel - invio g - C - Config - Config. - Config.W - Config.Wor - Config.Word - Config.Words - Config.WordsR - Config.WordsRe - Config.WordsRec - Config.WordsRece - remo - login unsu - s - client - add - print - printall - Syst - System - System. - System.o - System.ou - thread - System.out + wait fo + wait + wait us + wait u + waiting f + waiting for + gameIsSt + waitingF + waitingforU + waitingfor + waitingfo + waitingf + waiting + waiting for + is + i + gam + gameIs + gameIsS + iswa + ga + gameIS + gameISS + n + new inv + new invi + w + wait + waiting + gameISSt C:\Users\loke\TextTwist\out\production @@ -320,15 +336,8 @@ @@ -671,6 +687,222 @@ - @@ -1225,12 +1457,12 @@ - + - + @@ -1264,86 +1496,6 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -1370,14 +1522,6 @@ - - - - - - - - @@ -1392,140 +1536,6 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -1558,28 +1568,173 @@ - + - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - + + + + + + + + + + + + + + + + + + + + @@ -1588,7 +1743,7 @@ - + @@ -1596,80 +1751,74 @@ - + - - - - - - - - - - - - - - - - - - - - - - + + - + - - + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -1682,16 +1831,110 @@ - + - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Client/src/com/texttwist/client/services/GameService.java b/Client/src/com/texttwist/client/services/GameService.java index 5b3634e..e80e7b9 100644 --- a/Client/src/com/texttwist/client/services/GameService.java +++ b/Client/src/com/texttwist/client/services/GameService.java @@ -27,6 +27,7 @@ public class GameService { public DefaultListModel> globalRanks = new DefaultListModel<>(); public DefaultListModel> ranks = new DefaultListModel<>(); private Boolean gameIsStarted = false; + public Boolean isWaiting = false; private void addToPendingList(String username) throws IOException { pendingList.addElement(username); @@ -44,7 +45,7 @@ public class GameService { e.printStackTrace(); } - if(!App.gameService.gameIsStarted) { + if(!App.gameService.gameIsStarted && !App.gameService.isWaiting) { //Show invite popup new TTDialog("success", "New invite from: " + userName + "!", new Callable() { diff --git a/Client/src/com/texttwist/client/tasks/WaitForPlayers.java b/Client/src/com/texttwist/client/tasks/WaitForPlayers.java index cdc096a..8799eac 100644 --- a/Client/src/com/texttwist/client/tasks/WaitForPlayers.java +++ b/Client/src/com/texttwist/client/tasks/WaitForPlayers.java @@ -28,6 +28,7 @@ public class WaitForPlayers extends SwingWorker { @Override public Void doInBackground() { try { + App.gameService.isWaiting = true; ByteBuffer buffer = ByteBuffer.allocate(1024); TTDialog loading = new TTDialog("alert", "Waiting for users joins",null,null); buffer.flip(); @@ -42,6 +43,7 @@ public class WaitForPlayers extends SwingWorker { Message msg = Message.toMessage(line); if (msg.message.equals("JOIN_TIMEOUT")) { loading.dispose(); + App.gameService.isWaiting = false; joinTimeout = true; new TTDialog("alert", "TIMEOUT!", @@ -58,6 +60,7 @@ public class WaitForPlayers extends SwingWorker { if (msg.message.equals("MATCH_NOT_AVAILABLE")) { loading.dispose(); joinTimeout = true; + App.gameService.isWaiting = false; new TTDialog("alert", "THE GAME IS NOT MORE AVAILABLE!", new Callable() { @@ -72,6 +75,7 @@ public class WaitForPlayers extends SwingWorker { if (msg.message.equals("GAME_STARTED")) { loading.dispose(); + App.gameService.isWaiting = false; if(msg.data !=null ) { DefaultListModel data = msg.data; diff --git a/Commons/src/constants/Config.java b/Commons/src/constants/Config.java index 1496241..c337269 100644 --- a/Commons/src/constants/Config.java +++ b/Commons/src/constants/Config.java @@ -23,9 +23,9 @@ public class Config { public static String RedisServerURI = "localhost"; - public static int gameTimeout = 10; //2 minuti in sec - public static int joinMatchTimeout = 5000; //7 minuti in millisec - public static int sendWordsTimeout = 3000; //5 minuti in millisec + public static int gameTimeout = 120; //2 minuti in sec + public static int joinMatchTimeout = 7*1000*60; //7 minuti in millisec + public static int sendWordsTimeout = 5*1000*60; //5 minuti in millisec public static String getAuthServerURI(){ return "rmi://".concat(AuthServerURI).concat(":").concat(AuthServerPort.toString()); diff --git a/Commons/src/interfaces/INotificationServer.java b/Commons/src/interfaces/INotificationServer.java index 2c72c23..f7cad3c 100644 --- a/Commons/src/interfaces/INotificationServer.java +++ b/Commons/src/interfaces/INotificationServer.java @@ -9,5 +9,5 @@ import java.rmi.RemoteException; */ public interface INotificationServer extends Remote { void registerForCallback (INotificationClient ClientInterface) throws RemoteException; - void unregisterForCallback (INotificationClient ClientInterface) throws RemoteException; + void unregisterForCallback (INotificationClient ClientInterface) throws RemoteException; } diff --git a/Server/src/com/texttwist/server/Server.java b/Server/src/com/texttwist/server/Server.java index f57822b..aebc817 100644 --- a/Server/src/com/texttwist/server/Server.java +++ b/Server/src/com/texttwist/server/Server.java @@ -42,8 +42,8 @@ public class Server { private void startAuthService(){ //Starting Auth service based on RMI try { - auth = new AuthService(Config.AuthServerPort); - Registry authRegistry = LocateRegistry.createRegistry(auth.serverPort); + auth = new AuthService(); + Registry authRegistry = LocateRegistry.createRegistry(Config.AuthServerPort); authRegistry.bind("auth", auth); } catch (RemoteException e) { Server.logger.write("SERVER: RMI authentication service error (Remote exception)"); diff --git a/Server/src/com/texttwist/server/proxies/MessageDispatcher.java b/Server/src/com/texttwist/server/proxies/MessageDispatcher.java index 2f0c217..1adeb92 100644 --- a/Server/src/com/texttwist/server/proxies/MessageDispatcher.java +++ b/Server/src/com/texttwist/server/proxies/MessageDispatcher.java @@ -13,7 +13,7 @@ import java.util.concurrent.*; /** * Author: Lorenzo Iovino on 18/06/2017. - * Description: Proxy Dispatcher + * Description: Message Dispatcher * */ public class MessageDispatcher implements Callable { private final ExecutorService threadPool = Executors.newCachedThreadPool(); diff --git a/Server/src/com/texttwist/server/services/AuthService.java b/Server/src/com/texttwist/server/services/AuthService.java index 675b10c..85e07a0 100644 --- a/Server/src/com/texttwist/server/services/AuthService.java +++ b/Server/src/com/texttwist/server/services/AuthService.java @@ -1,6 +1,7 @@ package com.texttwist.server.services; import com.texttwist.server.Server; +import constants.Config; import interfaces.IAuth; import interfaces.INotificationClient; import models.Response; @@ -19,12 +20,9 @@ import static com.texttwist.server.Server.notificationServer; public class AuthService extends UnicastRemoteObject implements IAuth { private SecureRandom random = new SecureRandom(); - public int serverPort = 9999; - - public AuthService(int serverPort) throws RemoteException{ - this.serverPort=serverPort; - Server.logger.write("AuthService Service running at "+serverPort+" port..."); + public AuthService() throws RemoteException{ + Server.logger.write("AuthService Service running at "+ Config.AuthServerPort+" port..."); } @Override diff --git a/Server/src/com/texttwist/server/services/JedisService.java b/Server/src/com/texttwist/server/services/JedisService.java index 78f519f..134e0ab 100644 --- a/Server/src/com/texttwist/server/services/JedisService.java +++ b/Server/src/com/texttwist/server/services/JedisService.java @@ -1,5 +1,6 @@ package com.texttwist.server.services; +import com.texttwist.server.Server; import models.User; import redis.clients.jedis.Jedis; @@ -16,6 +17,11 @@ import static com.texttwist.server.Server.jedisPool; */ public class JedisService { + + JedisService(){ + Server.logger.write("Jedis Service running on localhost..."); + } + /** Read the object from Base64 string. */ public static Object fromString(String s) throws IOException, ClassNotFoundException { byte [] data = Base64.getDecoder().decode(s); diff --git a/Server/src/com/texttwist/server/services/NotificationService.java b/Server/src/com/texttwist/server/services/NotificationService.java index 7c085e6..863f107 100644 --- a/Server/src/com/texttwist/server/services/NotificationService.java +++ b/Server/src/com/texttwist/server/services/NotificationService.java @@ -1,5 +1,7 @@ package com.texttwist.server.services; +import com.texttwist.server.Server; +import constants.Config; import interfaces.INotificationClient; import interfaces.INotificationServer; @@ -12,13 +14,14 @@ import java.util.List; /** * Author: Lorenzo Iovino on 19/06/2017. - * Description: Jedis Service + * Description: Notification Service */ public class NotificationService implements INotificationServer { private List clients; public NotificationService() throws RemoteException { super(); + Server.logger.write("Notification Service running at "+ Config.NotificationServerPort+" port..."); clients = new ArrayList<>(); } diff --git a/Server/src/com/texttwist/server/services/ReceiveWordsService.java b/Server/src/com/texttwist/server/services/ReceiveWordsService.java index 8078794..bedd48d 100644 --- a/Server/src/com/texttwist/server/services/ReceiveWordsService.java +++ b/Server/src/com/texttwist/server/services/ReceiveWordsService.java @@ -1,34 +1,29 @@ package com.texttwist.server.services; +import com.texttwist.server.Server; import com.texttwist.server.models.Match; import com.texttwist.server.tasks.ComputeScore; -import com.texttwist.server.tasks.TokenInvalid; import constants.Config; import models.Message; - import java.io.IOException; import java.net.*; -import java.nio.ByteBuffer; -import java.nio.channels.DatagramChannel; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; /** * Author: Lorenzo Iovino on 27/06/2017. - * Description: Jedis Service + * Description: Receive Words Service */ public class ReceiveWordsService implements Runnable { private ExecutorService threadPool = Executors.newCachedThreadPool(); public ReceiveWordsService() { - + Server.logger.write("ReceiveWords Service running at "+Config.WordsReceiverServerPort+" port..."); } @Override public void run(){ - - Message msg; DatagramSocket s = null; @@ -40,7 +35,6 @@ public class ReceiveWordsService implements Runnable { DatagramPacket packet; while(true) { - byte[] buf = new byte[1024]; packet = new DatagramPacket(buf, buf.length); diff --git a/Server/src/com/texttwist/server/services/SessionsService.java b/Server/src/com/texttwist/server/services/SessionsService.java index 2f80540..43f6030 100644 --- a/Server/src/com/texttwist/server/services/SessionsService.java +++ b/Server/src/com/texttwist/server/services/SessionsService.java @@ -33,8 +33,6 @@ public class SessionsService { return null; } - public SessionsService(){} - public boolean add(String userName, String token) { remove(userName); return sessions.add(new Session(new User(userName,"",0), token)); diff --git a/Server/src/com/texttwist/server/tasks/CheckOnlineUsers.java b/Server/src/com/texttwist/server/tasks/CheckOnlineUsers.java index 46b66ba..30949aa 100644 --- a/Server/src/com/texttwist/server/tasks/CheckOnlineUsers.java +++ b/Server/src/com/texttwist/server/tasks/CheckOnlineUsers.java @@ -1,13 +1,12 @@ package com.texttwist.server.tasks; import com.texttwist.server.services.SessionsService; - import javax.swing.*; import java.util.concurrent.Callable; /** * Author: Lorenzo Iovino on 19/06/2017. - * Description: Jedis Service + * Description: Task: Check Online Users */ public class CheckOnlineUsers implements Callable { private final DefaultListModel users; diff --git a/Server/src/com/texttwist/server/tasks/ComputeHighscores.java b/Server/src/com/texttwist/server/tasks/ComputeHighscores.java index 4c143e2..6b8c29d 100644 --- a/Server/src/com/texttwist/server/tasks/ComputeHighscores.java +++ b/Server/src/com/texttwist/server/tasks/ComputeHighscores.java @@ -10,12 +10,10 @@ import java.util.concurrent.Callable; /** * Author: Lorenzo Iovino on 28/06/2017. - * Description: Jedis Service + * Description: Task: Compute Highscores */ public class ComputeHighscores implements Callable> { - public ComputeHighscores(){} - @Override public DefaultListModel call() throws Exception { DefaultListModel l = new DefaultListModel<>(); diff --git a/Server/src/com/texttwist/server/tasks/ComputeScore.java b/Server/src/com/texttwist/server/tasks/ComputeScore.java index 97113c4..f8bc85e 100644 --- a/Server/src/com/texttwist/server/tasks/ComputeScore.java +++ b/Server/src/com/texttwist/server/tasks/ComputeScore.java @@ -1,23 +1,22 @@ package com.texttwist.server.tasks; + import com.texttwist.server.services.AccountsService; import com.texttwist.server.models.Dictionary; import com.texttwist.server.models.Match; -import javafx.util.Pair; import models.User; - import javax.swing.*; import java.util.concurrent.Callable; /** * Author: Lorenzo Iovino on 27/06/2017. - * Description: Jedis Service + * Description: Task: Ccmpute Score */ public class ComputeScore implements Callable { private DefaultListModel words; private final String sender; public Match match; - private DefaultListModel wordsValid; + private DefaultListModel wordsValid = new DefaultListModel<>(); public ComputeScore(String sender, DefaultListModel words, Match match){ this.words = words; @@ -27,26 +26,24 @@ public class ComputeScore implements Callable { @Override public Integer call() throws Exception { - wordsValid = new DefaultListModel<>(); - Integer score = 0; - - for (int i = 0; i < words.size(); i++) { - if (isValid(words.get(i), match.letters)) { - score += words.get(i).length(); - wordsValid.addElement(words.get(i)); - } + Integer score = 0; + for (int i = 0; i < words.size(); i++) { + if (isValid(words.get(i), match.letters)) { + score += words.get(i).length(); + wordsValid.addElement(words.get(i)); } - match.setScore(sender, score); + } + match.setScore(sender, score); - User u = AccountsService.getInstance().findUser(sender); - u.addScore(score); + User u = AccountsService.getInstance().findUser(sender); + u.addScore(score); - if(match.allPlayersSendedHisScore()) { - match.matchTimeout = false; - match.setUndefinedScorePlayersToZero(); - new SendScores(match).call(); - } - return score; + if(match.allPlayersSendedHisScore()) { + match.matchTimeout = false; + match.setUndefinedScorePlayersToZero(); + new SendScores(match).call(); + } + return score; } private Boolean isValid(String word, DefaultListModel letters) { diff --git a/Server/src/com/texttwist/server/tasks/GenerateLetters.java b/Server/src/com/texttwist/server/tasks/GenerateLetters.java index a5a5394..71f3e26 100644 --- a/Server/src/com/texttwist/server/tasks/GenerateLetters.java +++ b/Server/src/com/texttwist/server/tasks/GenerateLetters.java @@ -1,24 +1,18 @@ package com.texttwist.server.tasks; import com.texttwist.server.services.MessageService; - import javax.swing.*; import java.util.concurrent.Callable; /** * Author: Lorenzo Iovino on 25/06/2017. - * Description: Jedis Service + * Description: Task: Generate Letters */ public class GenerateLetters implements Callable> { - - public GenerateLetters(){ - - } - @Override public DefaultListModel call() throws Exception { - DefaultListModel l = new DefaultListModel(); + DefaultListModel l = new DefaultListModel<>(); String word = MessageService.dict.getRandomWord(6, 7); for (int i = 0;i < word.length(); i++){ diff --git a/Server/src/com/texttwist/server/tasks/JoinMatch.java b/Server/src/com/texttwist/server/tasks/JoinMatch.java index d9c7b36..c7cb369 100644 --- a/Server/src/com/texttwist/server/tasks/JoinMatch.java +++ b/Server/src/com/texttwist/server/tasks/JoinMatch.java @@ -8,12 +8,12 @@ import java.util.concurrent.Callable; /** * Author: Lorenzo Iovino on 23/06/2017. - * Description: Jedis Service + * Description: Task: Join Match */ public class JoinMatch implements Callable { - public final String matchName; - public final String playerName; - public final SocketChannel socketChannel; + private final String matchName; + private final String playerName; + private final SocketChannel socketChannel; public JoinMatch(String playerName, DefaultListModel matchName, SocketChannel socketChannel) { this.playerName = playerName; @@ -24,19 +24,19 @@ public class JoinMatch implements Callable { @Override public Boolean call() throws Exception { final Match thisMatch = Match.findMatch(Match.activeMatches, this.matchName); - if (thisMatch != null) { - for (int j = 0; j < thisMatch.playersStatus.size(); j++) { - String name = thisMatch.playersStatus.get(j).getKey(); - if (name.equals(playerName)) { - thisMatch.playersStatus.remove(j); - thisMatch.playersStatus.add(new Pair<>(name, 1)); - thisMatch.playersSocket.remove(j); - thisMatch.playersSocket.add(new Pair<>(name, socketChannel)); - return allJoined(thisMatch); - } + if (thisMatch != null) { + for (int j = 0; j < thisMatch.playersStatus.size(); j++) { + String name = thisMatch.playersStatus.get(j).getKey(); + if (name.equals(playerName)) { + thisMatch.playersStatus.remove(j); + thisMatch.playersStatus.add(new Pair<>(name, 1)); + thisMatch.playersSocket.remove(j); + thisMatch.playersSocket.add(new Pair<>(name, socketChannel)); + return allJoined(thisMatch); } } - return false; + } + return false; } private Boolean allJoined(Match match) { diff --git a/Server/src/com/texttwist/server/tasks/JoinTimeout.java b/Server/src/com/texttwist/server/tasks/JoinTimeout.java index 2f39eff..0178235 100644 --- a/Server/src/com/texttwist/server/tasks/JoinTimeout.java +++ b/Server/src/com/texttwist/server/tasks/JoinTimeout.java @@ -1,9 +1,12 @@ package com.texttwist.server.tasks; import com.texttwist.server.models.Match; +import constants.Config; + import java.util.concurrent.*; /** - * Created by loke on 23/06/2017. + * Author: Lorenzo Iovino on 23/06/2017. + * Description: Task: Join Timeout */ public class JoinTimeout implements Callable { @@ -11,14 +14,13 @@ public class JoinTimeout implements Callable { public JoinTimeout(Match match) { this.match = match; - } @Override public Boolean call() throws Exception { try { - match.joinTimeout=true; - Thread.currentThread().sleep(5000); + match.joinTimeout = true; + Thread.currentThread().sleep(Config.joinMatchTimeout); if(match.joinTimeout) { match.joinTimeout = false; diff --git a/Server/src/com/texttwist/server/tasks/MatchTimeout.java b/Server/src/com/texttwist/server/tasks/MatchTimeout.java index 91755f5..2fd14e2 100644 --- a/Server/src/com/texttwist/server/tasks/MatchTimeout.java +++ b/Server/src/com/texttwist/server/tasks/MatchTimeout.java @@ -2,12 +2,11 @@ package com.texttwist.server.tasks; import com.texttwist.server.models.Match; import constants.Config; - import java.util.concurrent.Callable; /** * Author: Lorenzo Iovino on 27/06/2017. - * Description: Jedis Service + * Description: Task: Match Timeout */ public class MatchTimeout implements Callable { diff --git a/Server/src/com/texttwist/server/tasks/SendInvitations.java b/Server/src/com/texttwist/server/tasks/SendInvitations.java index 768094e..12dca8c 100644 --- a/Server/src/com/texttwist/server/tasks/SendInvitations.java +++ b/Server/src/com/texttwist/server/tasks/SendInvitations.java @@ -1,13 +1,12 @@ package com.texttwist.server.tasks; import com.texttwist.server.Server; - import javax.swing.*; import java.util.concurrent.Callable; /** * Author: Lorenzo Iovino on 19/06/2017. - * Description: Jedis Service + * Description: Task: Send Invitations */ public class SendInvitations implements Callable { private DefaultListModel users; @@ -22,11 +21,9 @@ public class SendInvitations implements Callable { public Boolean call() throws Exception { try { Server.notificationServer.sendInvitations(sender, users); - } catch (Exception e) { e.printStackTrace(); } - return true; } } diff --git a/Server/src/com/texttwist/server/tasks/SendMessageToAllPlayers.java b/Server/src/com/texttwist/server/tasks/SendMessageToAllPlayers.java index 96012cc..379be33 100644 --- a/Server/src/com/texttwist/server/tasks/SendMessageToAllPlayers.java +++ b/Server/src/com/texttwist/server/tasks/SendMessageToAllPlayers.java @@ -2,22 +2,21 @@ package com.texttwist.server.tasks; 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.Callable; /** * Author: Lorenzo Iovino on 27/06/2017. - * Description: Jedis Service + * Description: Task: Send Message To All Players */ public class SendMessageToAllPlayers implements Callable { public final Match match; - public final Message message; - public SocketChannel socketChannel; + private final Message message; + private SocketChannel socketChannel; + public SendMessageToAllPlayers(Match match, Message message, SocketChannel socketChannel){ this.match = match; this.message = message; @@ -37,10 +36,8 @@ public class SendMessageToAllPlayers implements Callable { buffer = ByteBuffer.wrap(byteMessage); socketChannel.write(buffer); } - } return false; - } else { return true; } diff --git a/Server/src/com/texttwist/server/tasks/SendScores.java b/Server/src/com/texttwist/server/tasks/SendScores.java index b5383ac..69569ce 100644 --- a/Server/src/com/texttwist/server/tasks/SendScores.java +++ b/Server/src/com/texttwist/server/tasks/SendScores.java @@ -11,7 +11,8 @@ import java.net.MulticastSocket; import java.util.concurrent.Callable; /** - * Created by loke on 13/07/2017. + * Author: Lorenzo Iovino on 13/07/2017. + * Description: Task: Send Scores */ public class SendScores implements Callable { @@ -24,19 +25,17 @@ public class SendScores implements Callable { @Override public Void call() throws Exception { - while (true) { - Message msg = new Message("FINALSCORE", "SERVER", "", match.getMatchPlayersScoreAsStringList()); - MulticastSocket multicastSocket = null; - try { - multicastSocket = new MulticastSocket(match.multicastId); - InetAddress ia = InetAddress.getByName(Config.ScoreMulticastServerURI); - DatagramPacket hi = new DatagramPacket(msg.toString().getBytes(), msg.toString().length(), ia, match.multicastId); - multicastSocket.send(hi); - } catch (IOException e) { - e.printStackTrace(); - } - Match.activeMatches.remove(Match.findMatchIndex(Match.activeMatches, match.matchCreator)); - return null; + Message msg = new Message("FINALSCORE", "SERVER", "", match.getMatchPlayersScoreAsStringList()); + MulticastSocket multicastSocket = null; + try { + multicastSocket = new MulticastSocket(match.multicastId); + InetAddress ia = InetAddress.getByName(Config.ScoreMulticastServerURI); + DatagramPacket hi = new DatagramPacket(msg.toString().getBytes(), msg.toString().length(), ia, match.multicastId); + multicastSocket.send(hi); + } catch (IOException e) { + e.printStackTrace(); } + Match.activeMatches.remove(Match.findMatchIndex(Match.activeMatches, match.matchCreator)); + return null; } } diff --git a/Server/src/com/texttwist/server/tasks/TokenInvalid.java b/Server/src/com/texttwist/server/tasks/TokenInvalid.java index b190c15..5ae0617 100644 --- a/Server/src/com/texttwist/server/tasks/TokenInvalid.java +++ b/Server/src/com/texttwist/server/tasks/TokenInvalid.java @@ -10,7 +10,7 @@ import java.util.concurrent.Callable; /** * Author: Lorenzo Iovino on 11/07/2017. - * Description: Task: Token Invalid Service + * Description: Task: Token Invalid */ public class TokenInvalid implements Callable { private String sender; diff --git a/notificationServer.log b/notificationServer.log index 1892600..078493a 100644 --- a/notificationServer.log +++ b/notificationServer.log @@ -1884,3 +1884,105 @@ LOGGER (Server): Fri Jul 14 00:05:27 CEST 2017 - Invoked login with username=c A LOGGER (Server): Fri Jul 14 00:05:27 CEST 2017 - Login successfull LOGGER (Server): Fri Jul 14 00:05:31 CEST 2017 - Invoked login with username=d AND password=d LOGGER (Server): Fri Jul 14 00:05:31 CEST 2017 - Login successfull +LOGGER (Server): Fri Jul 14 00:07:15 CEST 2017 - Services starting ... +LOGGER (Server): Fri Jul 14 00:07:15 CEST 2017 - AuthService Service running at 9999 port... +LOGGER (Server): Fri Jul 14 00:07:15 CEST 2017 - GameService Service is running at 10000 port... +LOGGER (Server): Fri Jul 14 00:07:15 CEST 2017 - Services started correctly ... +LOGGER (Server): Fri Jul 14 00:10:48 CEST 2017 - Services starting ... +LOGGER (Server): Fri Jul 14 00:10:48 CEST 2017 - AuthService Service running at 9999 port... +LOGGER (Server): Fri Jul 14 00:10:48 CEST 2017 - GameService Service is running at 10000 port... +LOGGER (Server): Fri Jul 14 00:10:48 CEST 2017 - ReceiveWords Service running at 10001 port... +LOGGER (Server): Fri Jul 14 00:10:48 CEST 2017 - Notification Service running at 20000 port... +LOGGER (Server): Fri Jul 14 00:10:48 CEST 2017 - Services started correctly ... +LOGGER (Server): Fri Jul 14 00:10:59 CEST 2017 - Invoked login with username=a AND password=a +LOGGER (Server): Fri Jul 14 00:10:59 CEST 2017 - Login successfull +LOGGER (Server): Fri Jul 14 00:11:02 CEST 2017 - Invoked login with username=b AND password=b +LOGGER (Server): Fri Jul 14 00:11:02 CEST 2017 - Login successfull +LOGGER (Server): Fri Jul 14 00:11:25 CEST 2017 - Invoked login with username=c AND password=c +LOGGER (Server): Fri Jul 14 00:11:25 CEST 2017 - Login successfull +LOGGER (Server): Fri Jul 14 00:11:28 CEST 2017 - Invoked login with username=d AND password=d +LOGGER (Server): Fri Jul 14 00:11:28 CEST 2017 - Login successfull +LOGGER (Server): Fri Jul 14 00:13:37 CEST 2017 - Services starting ... +LOGGER (Server): Fri Jul 14 00:13:37 CEST 2017 - AuthService Service running at 9999 port... +LOGGER (Server): Fri Jul 14 00:13:38 CEST 2017 - GameService Service is running at 10000 port... +LOGGER (Server): Fri Jul 14 00:13:38 CEST 2017 - ReceiveWords Service running at 10001 port... +LOGGER (Server): Fri Jul 14 00:13:38 CEST 2017 - Notification Service running at 20000 port... +LOGGER (Server): Fri Jul 14 00:13:38 CEST 2017 - Services started correctly ... +LOGGER (Server): Fri Jul 14 00:13:42 CEST 2017 - Invoked login with username=a AND password=a +LOGGER (Server): Fri Jul 14 00:13:42 CEST 2017 - Login successfull +LOGGER (Server): Fri Jul 14 00:13:48 CEST 2017 - Invoked login with username=b AND password=b +LOGGER (Server): Fri Jul 14 00:13:48 CEST 2017 - Login successfull +LOGGER (Server): Fri Jul 14 00:14:10 CEST 2017 - Invoked login with username=c AND password=c +LOGGER (Server): Fri Jul 14 00:14:10 CEST 2017 - Login successfull +LOGGER (Server): Fri Jul 14 00:14:14 CEST 2017 - Invoked login with username=d AND password=d +LOGGER (Server): Fri Jul 14 00:14:14 CEST 2017 - Login successfull +LOGGER (Server): Fri Jul 14 00:21:35 CEST 2017 - Services starting ... +LOGGER (Server): Fri Jul 14 00:21:35 CEST 2017 - AuthService Service running at 9999 port... +LOGGER (Server): Fri Jul 14 00:21:35 CEST 2017 - GameService Service is running at 10000 port... +LOGGER (Server): Fri Jul 14 00:21:35 CEST 2017 - ReceiveWords Service running at 10001 port... +LOGGER (Server): Fri Jul 14 00:21:35 CEST 2017 - Notification Service running at 20000 port... +LOGGER (Server): Fri Jul 14 00:21:35 CEST 2017 - Services started correctly ... +LOGGER (Server): Fri Jul 14 00:21:47 CEST 2017 - Invoked login with username=a AND password=a +LOGGER (Server): Fri Jul 14 00:21:47 CEST 2017 - Login successfull +LOGGER (Server): Fri Jul 14 00:21:56 CEST 2017 - Invoked login with username=b AND password=b +LOGGER (Server): Fri Jul 14 00:21:56 CEST 2017 - Login successfull +LOGGER (Server): Fri Jul 14 00:22:00 CEST 2017 - Invoked login with username=c AND password=c +LOGGER (Server): Fri Jul 14 00:22:00 CEST 2017 - Login successfull +LOGGER (Server): Fri Jul 14 00:22:03 CEST 2017 - Invoked login with username=d AND password=d +LOGGER (Server): Fri Jul 14 00:22:03 CEST 2017 - Login successfull +LOGGER (Server): Fri Jul 14 00:23:15 CEST 2017 - Invoked login with username=e AND password=e +LOGGER (Server): Fri Jul 14 00:23:15 CEST 2017 - Login successfull +LOGGER (Server): Fri Jul 14 00:23:19 CEST 2017 - Invoked login with username=f AND password=f +LOGGER (Server): Fri Jul 14 00:23:19 CEST 2017 - Login successfull +LOGGER (Server): Fri Jul 14 00:25:05 CEST 2017 - Services starting ... +LOGGER (Server): Fri Jul 14 00:25:05 CEST 2017 - AuthService Service running at 9999 port... +LOGGER (Server): Fri Jul 14 00:25:05 CEST 2017 - GameService Service is running at 10000 port... +LOGGER (Server): Fri Jul 14 00:25:05 CEST 2017 - ReceiveWords Service running at 10001 port... +LOGGER (Server): Fri Jul 14 00:25:05 CEST 2017 - Notification Service running at 20000 port... +LOGGER (Server): Fri Jul 14 00:25:05 CEST 2017 - Services started correctly ... +LOGGER (Server): Fri Jul 14 00:25:11 CEST 2017 - Invoked login with username=a AND password=a +LOGGER (Server): Fri Jul 14 00:25:11 CEST 2017 - Login successfull +LOGGER (Server): Fri Jul 14 00:25:14 CEST 2017 - Invoked login with username=b AND password=b +LOGGER (Server): Fri Jul 14 00:25:14 CEST 2017 - Login successfull +LOGGER (Server): Fri Jul 14 00:25:24 CEST 2017 - Invoked login with username=c AND password=c +LOGGER (Server): Fri Jul 14 00:25:24 CEST 2017 - Login successfull +LOGGER (Server): Fri Jul 14 00:25:31 CEST 2017 - Invoked login with username=d AND password=d +LOGGER (Server): Fri Jul 14 00:25:31 CEST 2017 - Login successfull +LOGGER (Server): Fri Jul 14 00:25:59 CEST 2017 - Services starting ... +LOGGER (Server): Fri Jul 14 00:25:59 CEST 2017 - AuthService Service running at 9999 port... +LOGGER (Server): Fri Jul 14 00:25:59 CEST 2017 - GameService Service is running at 10000 port... +LOGGER (Server): Fri Jul 14 00:25:59 CEST 2017 - ReceiveWords Service running at 10001 port... +LOGGER (Server): Fri Jul 14 00:25:59 CEST 2017 - Notification Service running at 20000 port... +LOGGER (Server): Fri Jul 14 00:25:59 CEST 2017 - Services started correctly ... +LOGGER (Server): Fri Jul 14 00:26:03 CEST 2017 - Invoked login with username=a AND password=a +LOGGER (Server): Fri Jul 14 00:26:03 CEST 2017 - Login successfull +LOGGER (Server): Fri Jul 14 00:26:06 CEST 2017 - Invoked login with username=b AND password=b +LOGGER (Server): Fri Jul 14 00:26:06 CEST 2017 - Login successfull +LOGGER (Server): Fri Jul 14 00:26:15 CEST 2017 - Invoked login with username=c AND password=c +LOGGER (Server): Fri Jul 14 00:26:15 CEST 2017 - Login successfull +LOGGER (Server): Fri Jul 14 00:29:01 CEST 2017 - Services starting ... +LOGGER (Server): Fri Jul 14 00:29:01 CEST 2017 - AuthService Service running at 9999 port... +LOGGER (Server): Fri Jul 14 00:29:01 CEST 2017 - GameService Service is running at 10000 port... +LOGGER (Server): Fri Jul 14 00:29:01 CEST 2017 - ReceiveWords Service running at 10001 port... +LOGGER (Server): Fri Jul 14 00:29:01 CEST 2017 - Notification Service running at 20000 port... +LOGGER (Server): Fri Jul 14 00:29:01 CEST 2017 - Services started correctly ... +LOGGER (Server): Fri Jul 14 00:29:05 CEST 2017 - Invoked login with username=a AND password=a +LOGGER (Server): Fri Jul 14 00:29:05 CEST 2017 - Login successfull +LOGGER (Server): Fri Jul 14 00:29:08 CEST 2017 - Invoked login with username=b AND password=b +LOGGER (Server): Fri Jul 14 00:29:08 CEST 2017 - Login successfull +LOGGER (Server): Fri Jul 14 00:29:12 CEST 2017 - Invoked login with username=c AND password=c +LOGGER (Server): Fri Jul 14 00:29:12 CEST 2017 - Login successfull +LOGGER (Server): Fri Jul 14 00:34:04 CEST 2017 - Services starting ... +LOGGER (Server): Fri Jul 14 00:34:04 CEST 2017 - AuthService Service running at 9999 port... +LOGGER (Server): Fri Jul 14 00:34:04 CEST 2017 - GameService Service is running at 10000 port... +LOGGER (Server): Fri Jul 14 00:34:04 CEST 2017 - ReceiveWords Service running at 10001 port... +LOGGER (Server): Fri Jul 14 00:34:04 CEST 2017 - Notification Service running at 20000 port... +LOGGER (Server): Fri Jul 14 00:34:04 CEST 2017 - Services started correctly ... +LOGGER (Server): Fri Jul 14 00:34:11 CEST 2017 - Invoked login with username=a AND password=a +LOGGER (Server): Fri Jul 14 00:34:11 CEST 2017 - Login successfull +LOGGER (Server): Fri Jul 14 00:34:14 CEST 2017 - Invoked login with username=b AND password=b +LOGGER (Server): Fri Jul 14 00:34:14 CEST 2017 - Login successfull +LOGGER (Server): Fri Jul 14 00:34:17 CEST 2017 - Invoked login with username=c AND password=c +LOGGER (Server): Fri Jul 14 00:34:17 CEST 2017 - Login successfull +LOGGER (Server): Fri Jul 14 00:39:01 CEST 2017 - Invoked login with username=d AND password=d +LOGGER (Server): Fri Jul 14 00:39:01 CEST 2017 - Login successfull From afbd45f51e3fae701a8e1de4a2ce9e679c382fb1 Mon Sep 17 00:00:00 2001 From: Lorenzo Iovino Date: Fri, 14 Jul 2017 01:19:13 +0200 Subject: [PATCH 07/13] up --- .idea/workspace.xml | 337 +++++++++++++++++------------------------ notificationServer.log | 48 ++++++ 2 files changed, 190 insertions(+), 195 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 1456423..5388904 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -7,28 +7,7 @@ - - - - - - - - - - - - - - - - - - - - - - + @@ -61,29 +40,68 @@ - + - - + + - - + + - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - + + + + + + + + + + + @@ -94,69 +112,6 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -168,15 +123,6 @@ - wait fo - wait - wait us - wait u - waiting f - waiting for - gameIsSt - waitingF - waitingforU waitingfor waitingfo waitingf @@ -196,8 +142,17 @@ new invi w wait - waiting gameISSt + waiting + Na + Namng + Namn + Nam + Naming + Naming + Naming.re + Naming.r + Naming. C:\Users\loke\TextTwist\out\production @@ -336,11 +291,6 @@ @@ -952,7 +907,7 @@ - + - - @@ -1457,14 +1411,14 @@ - + - + - + @@ -1496,44 +1450,6 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -1751,22 +1667,6 @@ - - - - - - - - - - - - - - - - @@ -1783,14 +1683,6 @@ - - - - - - - - @@ -1876,14 +1768,6 @@ - - - - - - - - @@ -1904,10 +1788,7 @@ - - - - + @@ -1929,8 +1810,74 @@ - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/notificationServer.log b/notificationServer.log index 078493a..22ad330 100644 --- a/notificationServer.log +++ b/notificationServer.log @@ -1986,3 +1986,51 @@ LOGGER (Server): Fri Jul 14 00:34:17 CEST 2017 - Invoked login with username=c A LOGGER (Server): Fri Jul 14 00:34:17 CEST 2017 - Login successfull LOGGER (Server): Fri Jul 14 00:39:01 CEST 2017 - Invoked login with username=d AND password=d LOGGER (Server): Fri Jul 14 00:39:01 CEST 2017 - Login successfull +LOGGER (Server): Fri Jul 14 01:11:18 CEST 2017 - Services starting ... +LOGGER (Server): Fri Jul 14 01:11:19 CEST 2017 - AuthService Service running at 9999 port... +LOGGER (Server): Fri Jul 14 01:11:19 CEST 2017 - GameService Service is running at 10000 port... +LOGGER (Server): Fri Jul 14 01:11:19 CEST 2017 - ReceiveWords Service running at 10001 port... +LOGGER (Server): Fri Jul 14 01:11:19 CEST 2017 - Notification Service running at 20000 port... +LOGGER (Server): Fri Jul 14 01:11:19 CEST 2017 - Services started correctly ... +LOGGER (Server): Fri Jul 14 01:11:44 CEST 2017 - Services starting ... +LOGGER (Server): Fri Jul 14 01:11:44 CEST 2017 - AuthService Service running at 9999 port... +LOGGER (Server): Fri Jul 14 01:11:44 CEST 2017 - GameService Service is running at 10000 port... +LOGGER (Server): Fri Jul 14 01:11:44 CEST 2017 - ReceiveWords Service running at 10001 port... +LOGGER (Server): Fri Jul 14 01:11:44 CEST 2017 - Notification Service running at 20000 port... +LOGGER (Server): Fri Jul 14 01:11:44 CEST 2017 - Services started correctly ... +LOGGER (Server): Fri Jul 14 01:11:58 CEST 2017 - Services starting ... +LOGGER (Server): Fri Jul 14 01:11:58 CEST 2017 - AuthService Service running at 9999 port... +LOGGER (Server): Fri Jul 14 01:11:59 CEST 2017 - GameService Service is running at 10000 port... +LOGGER (Server): Fri Jul 14 01:11:59 CEST 2017 - ReceiveWords Service running at 10001 port... +LOGGER (Server): Fri Jul 14 01:11:59 CEST 2017 - Notification Service running at 20000 port... +LOGGER (Server): Fri Jul 14 01:11:59 CEST 2017 - Services started correctly ... +LOGGER (Server): Fri Jul 14 01:12:11 CEST 2017 - Services starting ... +LOGGER (Server): Fri Jul 14 01:12:12 CEST 2017 - AuthService Service running at 9999 port... +LOGGER (Server): Fri Jul 14 01:12:12 CEST 2017 - GameService Service is running at 10000 port... +LOGGER (Server): Fri Jul 14 01:12:12 CEST 2017 - ReceiveWords Service running at 10001 port... +LOGGER (Server): Fri Jul 14 01:12:12 CEST 2017 - Notification Service running at 20000 port... +LOGGER (Server): Fri Jul 14 01:12:12 CEST 2017 - Services started correctly ... +LOGGER (Server): Fri Jul 14 01:13:27 CEST 2017 - Services starting ... +LOGGER (Server): Fri Jul 14 01:13:28 CEST 2017 - AuthService Service running at 9999 port... +LOGGER (Server): Fri Jul 14 01:13:28 CEST 2017 - GameService Service is running at 10000 port... +LOGGER (Server): Fri Jul 14 01:13:28 CEST 2017 - ReceiveWords Service running at 10001 port... +LOGGER (Server): Fri Jul 14 01:13:28 CEST 2017 - Notification Service running at 20000 port... +LOGGER (Server): Fri Jul 14 01:13:28 CEST 2017 - Services started correctly ... +LOGGER (Server): Fri Jul 14 01:13:48 CEST 2017 - Services starting ... +LOGGER (Server): Fri Jul 14 01:13:49 CEST 2017 - AuthService Service running at 9999 port... +LOGGER (Server): Fri Jul 14 01:13:49 CEST 2017 - GameService Service is running at 10000 port... +LOGGER (Server): Fri Jul 14 01:13:49 CEST 2017 - ReceiveWords Service running at 10001 port... +LOGGER (Server): Fri Jul 14 01:13:49 CEST 2017 - Notification Service running at 20000 port... +LOGGER (Server): Fri Jul 14 01:13:49 CEST 2017 - Services started correctly ... +LOGGER (Server): Fri Jul 14 01:15:25 CEST 2017 - Services starting ... +LOGGER (Server): Fri Jul 14 01:15:26 CEST 2017 - AuthService Service running at 9999 port... +LOGGER (Server): Fri Jul 14 01:15:26 CEST 2017 - GameService Service is running at 10000 port... +LOGGER (Server): Fri Jul 14 01:15:26 CEST 2017 - ReceiveWords Service running at 10001 port... +LOGGER (Server): Fri Jul 14 01:15:26 CEST 2017 - Notification Service running at 20000 port... +LOGGER (Server): Fri Jul 14 01:15:26 CEST 2017 - Services started correctly ... +LOGGER (Server): Fri Jul 14 01:17:01 CEST 2017 - Services starting ... +LOGGER (Server): Fri Jul 14 01:17:02 CEST 2017 - AuthService Service running at 9999 port... +LOGGER (Server): Fri Jul 14 01:17:02 CEST 2017 - GameService Service is running at 10000 port... +LOGGER (Server): Fri Jul 14 01:17:02 CEST 2017 - ReceiveWords Service running at 10001 port... +LOGGER (Server): Fri Jul 14 01:17:02 CEST 2017 - Notification Service running at 20000 port... +LOGGER (Server): Fri Jul 14 01:17:02 CEST 2017 - Services started correctly ... From 44a9e43cd490d806800e4ff1f0a2e263bb6bbdc8 Mon Sep 17 00:00:00 2001 From: Lorenzo Iovino Date: Fri, 14 Jul 2017 01:43:06 +0200 Subject: [PATCH 08/13] up --- .idea/workspace.xml | 418 ++++++++++-------- .../server/proxies/MessageDispatcher.java | 111 ++--- .../server/services/AccountsService.java | 5 - .../server/services/MessageService.java | 2 +- .../server/services/NotificationService.java | 1 - notificationServer.log | 32 ++ 6 files changed, 318 insertions(+), 251 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 5388904..d572e78 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -8,6 +8,10 @@ + + + + @@ -30,61 +34,35 @@ - - + + - - + + + + + + + + + + + + + + + + - - + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + @@ -92,21 +70,76 @@ - - + + - - + + - - + + - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -123,11 +156,6 @@ - waitingfor - waitingfo - waitingf - waiting - waiting for is i gam @@ -153,6 +181,11 @@ Naming.re Naming.r Naming. + ma + match + matchNot + matchNotA + matchNotAv C:\Users\loke\TextTwist\out\production @@ -305,14 +338,12 @@ @@ -907,7 +940,7 @@ - + - + @@ -1416,7 +1450,7 @@ - + @@ -1522,26 +1556,6 @@ - - - - - - - - - - - - - - - - - - - - @@ -1580,73 +1594,6 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -1757,17 +1704,6 @@ - - - - - - - - - - - @@ -1776,14 +1712,6 @@ - - - - - - - - @@ -1800,14 +1728,6 @@ - - - - - - - - @@ -1848,14 +1768,6 @@ - - - - - - - - @@ -1882,6 +1794,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Server/src/com/texttwist/server/proxies/MessageDispatcher.java b/Server/src/com/texttwist/server/proxies/MessageDispatcher.java index 1adeb92..e61461e 100644 --- a/Server/src/com/texttwist/server/proxies/MessageDispatcher.java +++ b/Server/src/com/texttwist/server/proxies/MessageDispatcher.java @@ -1,5 +1,6 @@ package com.texttwist.server.proxies; +import com.texttwist.server.Server; import com.texttwist.server.services.SessionsService; import com.texttwist.server.models.Match; import com.texttwist.server.tasks.*; @@ -34,76 +35,75 @@ public class MessageDispatcher implements Callable { byte[] byteMessage = null; if(SessionsService.getInstance().isValidToken(request.token)){ switch(request.message){ + case "START_GAME": Future onlineUsers = threadPool.submit(new CheckOnlineUsers(request.data)); try { + //Check if invited users are online Boolean usersOnline = onlineUsers.get(); if(usersOnline){ Future sendInvitations = threadPool.submit(new SendInvitations(request.sender, request.data)); - try { - Boolean invitationSended = sendInvitations.get(); - if (invitationSended) { + Boolean invitationSended = sendInvitations.get(); + if (invitationSended) { - //Create new match and wait users joins - request.data.addElement(request.sender); - final Match match = new Match(request.sender, request.data); - Match.activeMatches.add(match); + //Server create new match + request.data.addElement(request.sender); + final Match match = new Match(request.sender, request.data); + Match.activeMatches.add(match); - DefaultListModel matchName = new DefaultListModel<>(); - matchName.addElement(request.sender); + DefaultListModel matchName = new DefaultListModel<>(); + matchName.addElement(request.sender); - Future joinMatch = threadPool.submit(new JoinMatch(request.sender, matchName, socketChannel)); - Boolean joinMatchRes = joinMatch.get(); + //Match creator join match + Future joinMatch = threadPool.submit(new JoinMatch(request.sender, matchName, socketChannel)); + Boolean joinMatchRes = joinMatch.get(); - if(!joinMatchRes){ - bufferMessage = ByteBuffer.allocate(1024); - Message message = new Message("INVITES_ALL_SENDED", "", "", new DefaultListModel<>()); - byteMessage = message.toString().getBytes(); - bufferMessage = ByteBuffer.wrap(byteMessage); - socketChannel.write(bufferMessage); - } - - Future joinTimeout = threadPool.submit(new JoinTimeout(match)); - Boolean joinTimeoutRes = joinTimeout.get(); - if(joinTimeoutRes){ - Future sendMessageJoinTimeout = threadPool.submit( - new SendMessageToAllPlayers(match, - new Message("JOIN_TIMEOUT", "", "", new DefaultListModel<>()), socketChannel)); - Boolean sendMessageJoinTimeoutRes = sendMessageJoinTimeout.get(); - if(!sendMessageJoinTimeoutRes){ - Match.activeMatches.remove(Match.findMatchIndex(Match.activeMatches, match.matchCreator)); - return sendMessageJoinTimeoutRes; - } - } else { - return true; - } - - } else { - return false; + //Notify to the client that invites was sents correctly + if(!joinMatchRes){ + bufferMessage = ByteBuffer.allocate(1024); + Message message = new Message("INVITES_ALL_SENDED", "", "", new DefaultListModel<>()); + byteMessage = message.toString().getBytes(); + bufferMessage = ByteBuffer.wrap(byteMessage); + socketChannel.write(bufferMessage); + } + + //Starts to wait until all player joins + Future joinTimeout = threadPool.submit(new JoinTimeout(match)); + Boolean joinTimeoutRes = joinTimeout.get(); + //If joinTimeoutRes==true timeout happen, need to notify to all waiting clients + if(joinTimeoutRes){ + Future sendMessageJoinTimeout = threadPool.submit( + new SendMessageToAllPlayers(match, + new Message("JOIN_TIMEOUT", "", "", new DefaultListModel<>()), socketChannel)); + Boolean sendMessageJoinTimeoutRes = sendMessageJoinTimeout.get(); + if(!sendMessageJoinTimeoutRes){ + Match.activeMatches.remove(Match.findMatchIndex(Match.activeMatches, match.matchCreator)); + return sendMessageJoinTimeoutRes; + } + } else { + //All done, all player joined + return true; } - } catch (InterruptedException e) { - e.printStackTrace(); - } catch (ExecutionException e) { - e.printStackTrace(); } } else { - + //Some user in the list is not online Message message = new Message("USER_NOT_ONLINE", "", "", new DefaultListModel<>()); - byteMessage = new String(message.toString()).getBytes(); + byteMessage = message.toString().getBytes(); bufferMessage.clear(); bufferMessage = ByteBuffer.wrap(byteMessage); this.socketChannel.write(bufferMessage); return false; } } catch (InterruptedException e) { - e.printStackTrace(); + Server.logger.write("MESSAGE DISPATCHER - START GAME: InterruptedException"); } catch (ExecutionException e) { - e.printStackTrace(); + Server.logger.write("MESSAGE DISPATCHER - START GAME: ExecutionException"); } catch (IOException e) { - e.printStackTrace(); + Server.logger.write("MESSAGE DISPATCHER - START GAME: IOException"); } case "FETCH_HIGHSCORES": + //Fetch hisghscore and send back to client Future> computeHighscores = threadPool.submit(new ComputeHighscores()); try { DefaultListModel computeHighscoresRes = computeHighscores.get(); @@ -121,19 +121,25 @@ public class MessageDispatcher implements Callable { } return false; } catch (InterruptedException e) { - e.printStackTrace(); + Server.logger.write("MESSAGE DISPATCHER - FETCH HIGHSCORES: InterruptedException"); } catch (ExecutionException e) { - e.printStackTrace(); + Server.logger.write("MESSAGE DISPATCHER - FETCH HIGHSCORES: ExecutionException"); } case "JOIN_GAME": + //An user joined the game Future joinMatch = threadPool.submit(new JoinMatch(request.sender, request.data, socketChannel)); try { Match match = Match.findMatch(Match.activeMatches, request.data.get(0));; Boolean joinMatchRes = joinMatch.get(); + + //If joinMatchRes=true start the game! Because all player joined if(joinMatchRes){ + //If match not fired join timeout, notify all player that game is started if(!match.joinTimeout) { + + //Generate letters to send to clients Future> generateLetters = threadPool.submit(new GenerateLetters()); match.setLetters(generateLetters.get()); match.letters.addElement(String.valueOf(match.multicastId)); @@ -143,6 +149,7 @@ public class MessageDispatcher implements Callable { if (socketClient != null) { bufferMessage.clear(); bufferMessage = ByteBuffer.allocate(1024); + Message message = new Message("GAME_STARTED", "", "", match.letters); match.startGame(); byteMessage = message.toString().getBytes(); @@ -156,12 +163,15 @@ public class MessageDispatcher implements Callable { } } if (matchNotAvailable) { + matchNotAvailable = false; return false; } } } else { + //Match doesn't exist more because a timeout happen if(match == null){ bufferMessage = ByteBuffer.allocate(1024); + if (socketChannel != null) { bufferMessage = ByteBuffer.allocate(1024); Message msg = new Message("MATCH_NOT_AVAILABLE", "", null, new DefaultListModel<>()); @@ -174,16 +184,17 @@ public class MessageDispatcher implements Callable { } } } catch (InterruptedException e) { - e.printStackTrace(); + Server.logger.write("MESSAGE DISPATCHER - JOIN GAME: InterruptedException"); } catch (ExecutionException e) { - e.printStackTrace(); + Server.logger.write("MESSAGE DISPATCHER - JOIN GAME: ExecutionException"); } catch (IOException e) { - e.printStackTrace(); + Server.logger.write("MESSAGE DISPATCHER - JOIN GAME: IOException"); } default: break; } } else { + //If token is invalid, return error message to client threadPool.submit(new TokenInvalid(request.sender, socketChannel, bufferMessage)); return false; } diff --git a/Server/src/com/texttwist/server/services/AccountsService.java b/Server/src/com/texttwist/server/services/AccountsService.java index 5d5515d..160064a 100644 --- a/Server/src/com/texttwist/server/services/AccountsService.java +++ b/Server/src/com/texttwist/server/services/AccountsService.java @@ -72,9 +72,4 @@ public class AccountsService { } return null; } - - public int size(){ - return users.size(); - } - } diff --git a/Server/src/com/texttwist/server/services/MessageService.java b/Server/src/com/texttwist/server/services/MessageService.java index 2d94cd1..2c24899 100644 --- a/Server/src/com/texttwist/server/services/MessageService.java +++ b/Server/src/com/texttwist/server/services/MessageService.java @@ -27,7 +27,7 @@ public class MessageService implements Runnable{ private String dictionaryPath = "./Server/resources/dictionary"; public static Dictionary dict; - public static Integer multicastId = 4000; + public static Integer multicastId = Config.NotificationServerStubPort; public MessageService() { diff --git a/Server/src/com/texttwist/server/services/NotificationService.java b/Server/src/com/texttwist/server/services/NotificationService.java index 863f107..1bd1763 100644 --- a/Server/src/com/texttwist/server/services/NotificationService.java +++ b/Server/src/com/texttwist/server/services/NotificationService.java @@ -50,6 +50,5 @@ public class NotificationService implements INotificationServer { } } } - } } diff --git a/notificationServer.log b/notificationServer.log index 22ad330..d9dd748 100644 --- a/notificationServer.log +++ b/notificationServer.log @@ -2034,3 +2034,35 @@ LOGGER (Server): Fri Jul 14 01:17:02 CEST 2017 - GameService Service is running LOGGER (Server): Fri Jul 14 01:17:02 CEST 2017 - ReceiveWords Service running at 10001 port... LOGGER (Server): Fri Jul 14 01:17:02 CEST 2017 - Notification Service running at 20000 port... LOGGER (Server): Fri Jul 14 01:17:02 CEST 2017 - Services started correctly ... +LOGGER (Server): Fri Jul 14 01:38:10 CEST 2017 - Services starting ... +LOGGER (Server): Fri Jul 14 01:38:10 CEST 2017 - AuthService Service running at 9999 port... +LOGGER (Server): Fri Jul 14 01:38:11 CEST 2017 - GameService Service is running at 10000 port... +LOGGER (Server): Fri Jul 14 01:38:11 CEST 2017 - ReceiveWords Service running at 10001 port... +LOGGER (Server): Fri Jul 14 01:38:11 CEST 2017 - Notification Service running at 20000 port... +LOGGER (Server): Fri Jul 14 01:38:11 CEST 2017 - Services started correctly ... +LOGGER (Server): Fri Jul 14 01:38:15 CEST 2017 - Invoked login with username=a AND password=a +LOGGER (Server): Fri Jul 14 01:38:15 CEST 2017 - Login successfull +LOGGER (Server): Fri Jul 14 01:38:18 CEST 2017 - Invoked login with username=b AND password=b +LOGGER (Server): Fri Jul 14 01:38:18 CEST 2017 - Login successfull +LOGGER (Server): Fri Jul 14 01:38:43 CEST 2017 - Invoked login with username=c AND password=c +LOGGER (Server): Fri Jul 14 01:38:43 CEST 2017 - Login successfull +LOGGER (Server): Fri Jul 14 01:38:48 CEST 2017 - Invoked login with username=d AND password=d +LOGGER (Server): Fri Jul 14 01:38:48 CEST 2017 - Login successfull +LOGGER (Server): Fri Jul 14 01:39:23 CEST 2017 - Services starting ... +LOGGER (Server): Fri Jul 14 01:39:23 CEST 2017 - AuthService Service running at 9999 port... +LOGGER (Server): Fri Jul 14 01:39:23 CEST 2017 - GameService Service is running at 10000 port... +LOGGER (Server): Fri Jul 14 01:39:23 CEST 2017 - ReceiveWords Service running at 10001 port... +LOGGER (Server): Fri Jul 14 01:39:23 CEST 2017 - Notification Service running at 20000 port... +LOGGER (Server): Fri Jul 14 01:39:23 CEST 2017 - Services started correctly ... +LOGGER (Server): Fri Jul 14 01:39:27 CEST 2017 - Invoked login with username=a AND password=a +LOGGER (Server): Fri Jul 14 01:39:27 CEST 2017 - Login successfull +LOGGER (Server): Fri Jul 14 01:39:30 CEST 2017 - Invoked login with username=b AND password=b +LOGGER (Server): Fri Jul 14 01:39:30 CEST 2017 - Login successfull +LOGGER (Server): Fri Jul 14 01:40:16 CEST 2017 - Invoked login with username=c AND password=c +LOGGER (Server): Fri Jul 14 01:40:16 CEST 2017 - Login successfull +LOGGER (Server): Fri Jul 14 01:40:19 CEST 2017 - Invoked login with username=d AND password=d +LOGGER (Server): Fri Jul 14 01:40:19 CEST 2017 - Login successfull +LOGGER (Server): Fri Jul 14 01:40:51 CEST 2017 - Invoked login with username=e AND password=e +LOGGER (Server): Fri Jul 14 01:40:51 CEST 2017 - Login successfull +LOGGER (Server): Fri Jul 14 01:40:54 CEST 2017 - Invoked login with username=f AND password=f +LOGGER (Server): Fri Jul 14 01:40:54 CEST 2017 - Login successfull From 8eab4d97c14eda5f3f0d8548fe62924af30c5fe3 Mon Sep 17 00:00:00 2001 From: Lorenzo Iovino Date: Sat, 15 Jul 2017 04:32:35 +0200 Subject: [PATCH 09/13] up --- .idea/workspace.xml | 1055 +- .../controllers/MatchRequestController.java | 4 +- .../controllers/MatchSetupController.java | 5 +- .../client/pages/MatchSetupPage.java | 2 +- .../client/services/GameService.java | 58 +- .../services/NotificationClientService.java | 3 +- .../texttwist/client/tasks/BeginMatch.java | 52 + .../com/texttwist/client/tasks/JoinMatch.java | 47 + .../client/tasks/WaitForPlayers.java | 1 + .../src/interfaces/INotificationServer.java | 2 + .../AccountsManager.java} | 15 +- .../SessionsManager.java} | 11 +- .../com/texttwist/server/models/Match.java | 4 +- .../server/proxies/MessageDispatcher.java | 6 +- .../server/services/AuthService.java | 12 +- .../server/services/ReceiveWordsService.java | 3 +- .../server/tasks/CheckOnlineUsers.java | 5 +- .../server/tasks/ComputeHighscores.java | 10 +- .../texttwist/server/tasks/ComputeScore.java | 4 +- .../{JoinTimeout.java => TimeoutJoin.java} | 4 +- .../{MatchTimeout.java => TimeoutMatch.java} | 4 +- client_1.log | 9134 ----------------- notificationServer.log | 2068 ---- 23 files changed, 714 insertions(+), 11795 deletions(-) create mode 100644 Client/src/com/texttwist/client/tasks/BeginMatch.java create mode 100644 Client/src/com/texttwist/client/tasks/JoinMatch.java rename Server/src/com/texttwist/server/{services/AccountsService.java => managers/AccountsManager.java} (82%) rename Server/src/com/texttwist/server/{services/SessionsService.java => managers/SessionsManager.java} (84%) rename Server/src/com/texttwist/server/tasks/{JoinTimeout.java => TimeoutJoin.java} (89%) rename Server/src/com/texttwist/server/tasks/{MatchTimeout.java => TimeoutMatch.java} (88%) delete mode 100644 client_1.log delete mode 100644 notificationServer.log diff --git a/.idea/workspace.xml b/.idea/workspace.xml index d572e78..27c0759 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -7,12 +7,29 @@ + + + + + + + + + + + + + + + + - - - - + + + + +