Strano ma funziona

This commit is contained in:
Lorenzo Iovino 2017-07-13 23:30:51 +02:00
parent 3305d6500b
commit 71ff3c4820
9 changed files with 310 additions and 286 deletions

View file

@ -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 {

View file

@ -1,7 +1,6 @@
package com.texttwist.server.models;
import com.texttwist.server.Server;
import javax.swing.*;
import java.io.*;
import java.util.Random;

View file

@ -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<String, Integer> 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<String> letters){

View file

@ -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<Boolean> {
protected final ExecutorService threadPool = Executors.newCachedThreadPool();
private final Message request;
@ -128,7 +125,6 @@ public class ProxyDispatcher implements Callable<Boolean> {
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();

View file

@ -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<Match> 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;
}

View file

@ -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));
}
}
}
}
}

View file

@ -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<Boolean>{
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));
}
}
}
}
}