From 44a9e43cd490d806800e4ff1f0a2e263bb6bbdc8 Mon Sep 17 00:00:00 2001 From: Lorenzo Iovino Date: Fri, 14 Jul 2017 01:43:06 +0200 Subject: [PATCH] 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