From 71ff3c482038acf3e684bf261f74aa0254ff8741 Mon Sep 17 00:00:00 2001 From: Lorenzo Iovino Date: Thu, 13 Jul 2017 23:30:51 +0200 Subject: [PATCH] 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